Code

remove many needless references to n-art-bpath.h
[inkscape.git] / src / live_effects / effect.cpp
index 08745a74ea331331117954cd5c2f5b44be2b2e76..64653863b927a402cf8349c70c12323d40d777e0 100644 (file)
@@ -8,7 +8,6 @@
 
 #include "live_effects/effect.h"
 
-#include "display/display-forward.h"
 #include "xml/node-event-vector.h"
 #include "sp-object.h"
 #include "attributes.h"
@@ -28,7 +27,6 @@
 #include "live_effects/lpeobject.h"
 #include "live_effects/parameter/parameter.h"
 #include <glibmm/ustring.h>
-#include "libnr/n-art-bpath-2geom.h"
 #include "display/curve.h"
 #include <gtkmm.h>
 
@@ -36,7 +34,7 @@
 
 #include <2geom/sbasis-to-bezier.h>
 #include <2geom/matrix.h>
-
+#include <2geom/pathvector.h>
 
 // include effects:
 #include "live_effects/lpe-patternalongpath.h"
 #include "live_effects/lpe-constructgrid.h"
 #include "live_effects/lpe-perp_bisector.h"
 #include "live_effects/lpe-tangent_to_curve.h"
-#include "live_effects/lpe-mirror_reflect.h"
+#include "live_effects/lpe-mirror_symmetry.h"
 #include "live_effects/lpe-circle_3pts.h"
 #include "live_effects/lpe-angle_bisector.h"
 #include "live_effects/lpe-parallel.h"
 #include "live_effects/lpe-copy_rotate.h"
+#include "live_effects/lpe-offset.h"
+#include "live_effects/lpe-ruler.h"
+#include "live_effects/lpe-boolops.h"
+#include "live_effects/lpe-interpolate.h"
+#include "live_effects/lpe-text_label.h"
+#include "live_effects/lpe-path_length.h"
 // end of includes
 
 namespace Inkscape {
 
 namespace LivePathEffect {
 
-const Util::EnumData<EffectType> LPETypeData[INVALID_LPE] = {
+const Util::EnumData<EffectType> LPETypeData[] = {
     // {constant defined in effect.h, N_("name of your effect"), "name of your effect in SVG"}
-    {BEND_PATH,             N_("Bend"),                  "bend_path"},
-    {PATTERN_ALONG_PATH,    N_("Pattern Along Path"),    "skeletal"},   // for historic reasons, this effect is called skeletal(strokes) in Inkscape:SVG
-    {SKETCH,                N_("Sketch"),                "sketch"},
-    {VONKOCH,               N_("VonKoch"),               "vonkoch"},
-    {KNOT,                  N_("Knot"),                  "knot"},
+    {ANGLE_BISECTOR,        N_("Angle bisector"),          "angle_bisector"},
+    {BEND_PATH,             N_("Bend"),                     "bend_path"},
+    {BOOLOPS,               N_("Boolops"),                 "boolops"},
+    {CIRCLE_WITH_RADIUS,    N_("Circle (center+radius)"),   "circle_with_radius"},
+    {CIRCLE_3PTS,           N_("Circle through 3 points"), "circle_3pts"},
+    {CONSTRUCT_GRID,        N_("Construct grid"),          "construct_grid"},
 #ifdef LPE_ENABLE_TEST_EFFECTS
-    {DOEFFECTSTACK_TEST,    N_("doEffect stack test"),   "doeffectstacktest"},
+    {DOEFFECTSTACK_TEST,    N_("doEffect stack test"),     "doeffectstacktest"},
 #endif
-    {GEARS,                 N_("Gears"),                 "gears"},
-    {CURVE_STITCH,          N_("Stitch Sub-Paths"),       "curvestitching"},
-    {CIRCLE_WITH_RADIUS,    N_("Circle (center+radius)"), "circle_with_radius"},
-    {PERSPECTIVE_PATH,      N_("Perspective path"),      "perspective_path"},
-    {SPIRO,      N_("Spiro spline"),      "spiro"},
-    {LATTICE,               N_("Lattice Deformation"),   "lattice"},
-    {ENVELOPE,              N_("Envelope Deformation"),  "envelope"},
-    {CONSTRUCT_GRID,        N_("Construct grid"),        "construct_grid"},
-    {PERP_BISECTOR, N_("Perpendicular bisector"), "perp_bisector"},
-    {TANGENT_TO_CURVE, N_("Tangent to curve"), "tangent_to_curve"},
-    {MIRROR_REFLECT, N_("Mirror reflection"), "mirror_reflect"},
-    {CIRCLE_3PTS, N_("Circle through 3 points"), "circle_3pts"},
-    {ANGLE_BISECTOR, N_("Angle bisector"), "angle_bisector"},
-    {PARALLEL, N_("Parallel"), "parallel"},
-    {COPY_ROTATE, N_("Rotate copies"), "copy_rotate"},
+    {ENVELOPE,              N_("Envelope Deformation"),    "envelope"},
+    {FREEHAND_SHAPE,        N_("Freehand Shape"),          "freehand_shape"}, // this is actually a special type of PatternAlongPath, used to paste shapes in pen/pencil tool
+    {GEARS,                 N_("Gears"),                   "gears"},
+    {INTERPOLATE,           N_("Interpolate Sub-Paths"),   "interpolate"},
+    {KNOT,                  N_("Knot"),                    "knot"},
+    {LATTICE,               N_("Lattice Deformation"),     "lattice"},
+    {MIRROR_SYMMETRY,       N_("Mirror symmetry"),         "mirror_symmetry"},
+    {OFFSET,                N_("Offset"),                  "offset"},
+    {PARALLEL,              N_("Parallel"),                "parallel"},
+    {PATH_LENGTH,           N_("Path length"),             "path_length"},
+    {PATTERN_ALONG_PATH,    N_("Pattern Along Path"),      "skeletal"},   // for historic reasons, this effect is called skeletal(strokes) in Inkscape:SVG
+    {PERP_BISECTOR,         N_("Perpendicular bisector"),  "perp_bisector"},
+    {PERSPECTIVE_PATH,      N_("Perspective path"),        "perspective_path"},
+    {COPY_ROTATE,           N_("Rotate copies"),           "copy_rotate"},
+    {RULER,                 N_("Ruler"),                   "ruler"},
+    {SKETCH,                N_("Sketch"),                  "sketch"},
+    {SPIRO,                 N_("Spiro spline"),            "spiro"},
+    {CURVE_STITCH,          N_("Stitch Sub-Paths"),        "curvestitching"},
+    {TANGENT_TO_CURVE,      N_("Tangent to curve"),        "tangent_to_curve"},
+    {TEXT_LABEL,            N_("Text label"),              "text_label"},
+    {VONKOCH,               N_("VonKoch"),                 "vonkoch"},
 };
-const Util::EnumDataConverter<EffectType> LPETypeConverter(LPETypeData, INVALID_LPE);
+const Util::EnumDataConverter<EffectType> LPETypeConverter(LPETypeData, sizeof(LPETypeData)/sizeof(*LPETypeData));
 
 Effect*
 Effect::New(EffectType lpenr, LivePathEffectObject *lpeobj)
@@ -102,6 +113,9 @@ Effect::New(EffectType lpenr, LivePathEffectObject *lpeobj)
         case PATTERN_ALONG_PATH:
             neweffect = static_cast<Effect*> ( new LPEPatternAlongPath(lpeobj) );
             break;
+        case FREEHAND_SHAPE:
+            neweffect = static_cast<Effect*> ( new LPEFreehandShape(lpeobj) );
+            break;
         case BEND_PATH:
             neweffect = static_cast<Effect*> ( new LPEBendPath(lpeobj) );
             break;
@@ -149,8 +163,8 @@ Effect::New(EffectType lpenr, LivePathEffectObject *lpeobj)
         case TANGENT_TO_CURVE:
             neweffect = static_cast<Effect*> ( new LPETangentToCurve(lpeobj) );
             break;
-        case MIRROR_REFLECT:
-            neweffect = static_cast<Effect*> ( new LPEMirrorReflect(lpeobj) );
+        case MIRROR_SYMMETRY:
+            neweffect = static_cast<Effect*> ( new LPEMirrorSymmetry(lpeobj) );
             break;
         case CIRCLE_3PTS:
             neweffect = static_cast<Effect*> ( new LPECircle3Pts(lpeobj) );
@@ -164,6 +178,24 @@ Effect::New(EffectType lpenr, LivePathEffectObject *lpeobj)
         case COPY_ROTATE:
             neweffect = static_cast<Effect*> ( new LPECopyRotate(lpeobj) );
             break;
+        case OFFSET:
+            neweffect = static_cast<Effect*> ( new LPEOffset(lpeobj) );
+            break;
+        case RULER:
+            neweffect = static_cast<Effect*> ( new LPERuler(lpeobj) );
+            break;
+        case BOOLOPS:
+            neweffect = static_cast<Effect*> ( new LPEBoolops(lpeobj) );
+            break;
+        case INTERPOLATE:
+            neweffect = static_cast<Effect*> ( new LPEInterpolate(lpeobj) );
+            break;
+        case TEXT_LABEL:
+            neweffect = static_cast<Effect*> ( new LPETextLabel(lpeobj) );
+            break;
+        case PATH_LENGTH:
+            neweffect = static_cast<Effect*> ( new LPEPathLength(lpeobj) );
+            break;
         default:
             g_warning("LivePathEffect::Effect::New   called with invalid patheffect type (%d)", lpenr);
             neweffect = NULL;
@@ -192,9 +224,6 @@ Effect::createAndApply(const char* name, SPDocument *doc, SPItem *item)
     gchar *href = g_strdup_printf("#%s", repr_id);
     sp_lpe_item_add_path_effect(SP_LPE_ITEM(item), href, true);
     g_free(href);
-
-    sp_document_done(doc, SP_VERB_DIALOG_LIVE_PATH_EFFECT,
-                     _("Create and apply path effect"));
 }
 
 void
@@ -222,7 +251,7 @@ Effect::~Effect()
 Glib::ustring
 Effect::getName()
 {
-    if (lpeobj->effecttype_set && lpeobj->effecttype < INVALID_LPE)
+    if (lpeobj->effecttype_set && LPETypeConverter.is_valid_id(lpeobj->effecttype) )
         return Glib::ustring( _(LPETypeConverter.get_label(lpeobj->effecttype).c_str()) );
     else
         return Glib::ustring( _("No effect") );
@@ -251,9 +280,9 @@ Effect::doBeforeEffect (SPLPEItem */*lpeitem*/)
 }
 
 /**
- * Effects have a parameter path set before they are applied by accepting a nonzero number of mouse
- * clicks. This method activates the pen context, which waits for the specified number of clicks.
- * Override Effect::acceptsNumParams() to set the number of expected mouse clicks.
+ * Effects can have a parameter path set before they are applied by accepting a nonzero number of
+ * mouse clicks. This method activates the pen context, which waits for the specified number of
+ * clicks. Override Effect::acceptsNumParams() to return the number of expected mouse clicks.
  */
 void
 Effect::doAcceptPathPreparations(SPLPEItem *lpeitem)
@@ -290,7 +319,7 @@ Effect::writeParamsToSVG() {
  * your LPE. But don't forget to call the parent method so that done_pathparam_set is set to true!
  */
 void
-Effect::acceptParamPath (SPPath *param_path) {
+Effect::acceptParamPath (SPPath */*param_path*/) {
     done_pathparam_set = true;
 }
 
@@ -304,7 +333,7 @@ Effect::doEffect (SPCurve * curve)
 
     std::vector<Geom::Path> result_pathv = doEffect_path(orig_pathv);
 
-    curve->set_pathv(result_pathv);
+    curve->set_pathvector(result_pathv);
 }
 
 std::vector<Geom::Path>
@@ -401,6 +430,24 @@ Effect::registerKnotHolderHandle(KnotHolderEntity* entity, const char* descr)
  */
 void
 Effect::addHandles(KnotHolder *knotholder, SPDesktop *desktop, SPItem *item) {
+    using namespace Inkscape::LivePathEffect;
+
+    // add handles provided by the effect itself
+    addKnotHolderEntities(knotholder, desktop, item);
+
+    // add handles provided by the effect's parameters (if any)
+    for (std::vector<Parameter *>::iterator p = param_vector.begin(); p != param_vector.end(); ++p) {
+        (*p)->addKnotHolderEntities(knotholder, desktop, item);
+    }
+}
+
+void
+Effect::addKnotHolderEntities(KnotHolder *knotholder, SPDesktop *desktop, SPItem *item) {
+    // TODO: The entities in kh_entity_vector are already instantiated during the call
+    //       to registerKnotHolderHandle(), but they are recreated here. Also, we must not
+    //       delete them when the knotholder is destroyed, whence the clumsy function
+    //       isDeletable(). If we could create entities of different classes dynamically,
+    //       this would be much nicer. How to do this?
     std::vector<std::pair<KnotHolderEntity*, const char*> >::iterator i;
     for (i = kh_entity_vector.begin(); i != kh_entity_vector.end(); ++i) {
         KnotHolderEntity *entity = i->first;
@@ -411,43 +458,49 @@ Effect::addHandles(KnotHolder *knotholder, SPDesktop *desktop, SPItem *item) {
     }
 }
 
-void
-Effect::addPointParamHandles(KnotHolder *knotholder, SPDesktop *desktop, SPItem *item) {
-    using namespace std;
-    for (std::vector<Parameter *>::iterator p = param_vector.begin(); p != param_vector.end(); ++p) {
-        if ((*p)->paramType() == Inkscape::LivePathEffect::POINT_PARAM) {
-            KnotHolderEntity *e = dynamic_cast<KnotHolderEntity *>(*p);
-            e->create(desktop, item, knotholder);
-            knotholder->add(e);
-        }
+/**
+ * Return a vector of PathVectors which contain all helperpaths that should be drawn by the effect.
+ * This is the function called by external code like SPLPEItem.
+ */
+std::vector<Geom::PathVector>
+Effect::getHelperPaths(SPLPEItem *lpeitem)
+{
+    std::vector<Geom::PathVector> hp_vec;
+
+    if (!SP_IS_SHAPE(lpeitem)) {
+        g_print ("How to handle helperpaths for non-shapes?\n");
+        return hp_vec;
     }
-}
 
-void
-Effect::addHelperPaths(SPLPEItem *lpeitem, SPDesktop *desktop)
-{
-    g_return_if_fail(SP_IS_PATH(lpeitem));
-
-    if (providesKnotholder() && showOrigPath()) {
-        // TODO: we assume that if the LPE provides its own knotholder, there is no nodepath so we
-        // must create the helper curve for the original path manually; when we allow nodepaths and
-        // knotholders alongside each other, this needs to be rethought!
-        SPCanvasItem *canvasitem = sp_nodepath_generate_helperpath(desktop, SP_PATH(lpeitem));
-        // TODO: Make sure the tempitem doesn't get destroyed when the mouse leaves the item
-        Inkscape::Display::TemporaryItem* tmpitem = desktop->add_temporary_canvasitem (canvasitem, 0);
-        lpeitem->lpe_helperpaths.push_back(tmpitem);
+    // TODO: we can probably optimize this by using a lot more references
+    //       rather than copying PathVectors all over the place
+    if (show_orig_path) {
+        // add original path to helperpaths
+        SPCurve* curve = sp_shape_get_curve (SP_SHAPE(lpeitem));
+        hp_vec.push_back(curve->get_pathvector());
+    }
+
+    // add other helperpaths provided by the effect itself
+    addCanvasIndicators(lpeitem, hp_vec);
+
+    // add helperpaths provided by the effect's parameters
+    for (std::vector<Parameter *>::iterator p = param_vector.begin(); p != param_vector.end(); ++p) {
+        (*p)->addCanvasIndicators(lpeitem, hp_vec);
     }
 
-    addHelperPathsImpl(lpeitem, desktop);
+    return hp_vec;
 }
 
+/**
+ * Add possible canvas indicators (i.e., helperpaths other than the original path) to \a hp_vec
+ * This function should be overwritten by derived effects if they want to provide their own helperpaths.
+ */
 void
-Effect::addHelperPathsImpl(SPLPEItem *lpeitem, SPDesktop *desktop)
+Effect::addCanvasIndicators(SPLPEItem */*lpeitem*/, std::vector<Geom::PathVector> &/*hp_vec*/)
 {
-    // if this method is overloaded in derived classes, provides_own_flash_paths will be true
-    provides_own_flash_paths = false;
 }
 
+
 /**
  * This *creates* a new widget, management of deletion should be done by the caller
  */
@@ -579,6 +632,7 @@ Effect::transform_multiply(Geom::Matrix const& postmul, bool set)
     }
 }
 
+// TODO: take _all_ parameters into account, not only PointParams
 bool
 Effect::providesKnotholder()
 {
@@ -588,7 +642,8 @@ Effect::providesKnotholder()
 
     // otherwise: are there any PointParams?
     for (std::vector<Parameter *>::iterator p = param_vector.begin(); p != param_vector.end(); ++p) {
-        if ((*p)->paramType() == Inkscape::LivePathEffect::POINT_PARAM) {
+//        if ( Inkscape::LivePathEffect::PointParam *pointparam = dynamic_cast<Inkscape::LivePathEffect::PointParam*>(*p) ) {
+        if (dynamic_cast<Inkscape::LivePathEffect::PointParam*>(*p)) {
             return true;
         }
     }