Code

fix lpe-PathPAram when deleting the path that it links to
[inkscape.git] / src / live_effects / effect.cpp
index 0773363dddfe4daa825a83a590cf305efefbc9a8..dc43af0d5073023a93f7fbfaeef94c7fee01e999 100644 (file)
-#define INKSCAPE_LIVEPATHEFFECT_CPP\r
-\r
-/*\r
- * Copyright (C) Johan Engelen 2007 <j.b.c.engelen@utwente.nl>\r
- *\r
- * Released under GNU GPL, read the file 'COPYING' for more information\r
- */\r
-\r
-#include "display/display-forward.h"\r
-#include "xml/node-event-vector.h"\r
-#include "sp-object.h"\r
-#include "attributes.h"\r
-#include "message-stack.h"\r
-#include "desktop.h"\r
-#include "inkscape.h"\r
-#include "document.h"\r
-#include <glibmm/i18n.h>\r
-\r
-#include "live_effects/effect.h"\r
-#include "live_effects/lpeobject.h"\r
-#include "live_effects/parameter/parameter.h"\r
-#include <glibmm/ustring.h>\r
-#include "live_effects/n-art-bpath-2geom.h"\r
-#include "display/curve.h"\r
-#include <2geom/sbasis-to-bezier.h>\r
-#include <gtkmm.h>\r
-\r
-#include <exception>\r
-\r
-// include effects:\r
-#include "live_effects/lpe-skeletalstrokes.h"\r
-#include "live_effects/lpe-slant.h"\r
-#include "live_effects/lpe-test-doEffect-stack.h"\r
-#include "live_effects/lpe-gears.h"\r
-#include "live_effects/lpe-curvestitch.h"\r
-\r
-namespace Inkscape {\r
-\r
-namespace LivePathEffect {\r
-\r
-const Util::EnumData<EffectType> LPETypeData[INVALID_LPE] = {\r
-    // {constant defined in effect.h, N_("name of your effect"), "name of your effect in SVG"}\r
-    {SKELETAL_STROKES,      N_("Path along path"),      "skeletal"},\r
-#ifdef LPE_ENABLE_TEST_EFFECTS\r
-    {SLANT,                 N_("Slant"),                 "slant"},\r
-    {DOEFFECTSTACK_TEST,    N_("doEffect stack test"),   "doeffectstacktest"},\r
-#endif\r
-    {GEARS,                 N_("Gears"),                 "gears"},\r
-    {CURVE_STITCH,          N_("Curve stitching"),       "curvestitching"},\r
-};\r
-const Util::EnumDataConverter<EffectType> LPETypeConverter(LPETypeData, INVALID_LPE);\r
-\r
-Effect*\r
-Effect::New(EffectType lpenr, LivePathEffectObject *lpeobj)\r
-{\r
-    Effect* neweffect = NULL;\r
-    switch (lpenr) {\r
-        case SKELETAL_STROKES:\r
-            neweffect = (Effect*) new LPESkeletalStrokes(lpeobj);\r
-            break;\r
-#ifdef LPE_ENABLE_TEST_EFFECTS\r
-            case SLANT:\r
-            neweffect = (Effect*) new LPESlant(lpeobj);\r
-            break;\r
-        case DOEFFECTSTACK_TEST:\r
-            neweffect = (Effect*) new LPEdoEffectStackTest(lpeobj);\r
-            break;\r
-#endif\r
-        case GEARS:\r
-            neweffect = (Effect*) new LPEGears(lpeobj);\r
-            break;\r
-        case CURVE_STITCH:\r
-            neweffect = (Effect*) new LPECurveStitch(lpeobj);\r
-            break;\r
-        default:\r
-            g_warning("LivePathEffect::Effect::New   called with invalid patheffect type (%d)", lpenr);\r
-            neweffect = NULL;\r
-            break;\r
-    }\r
-\r
-    if (neweffect) {\r
-        neweffect->readallParameters(SP_OBJECT_REPR(lpeobj));\r
-    }\r
-\r
-    return neweffect;\r
-}\r
-\r
-Effect::Effect(LivePathEffectObject *lpeobject)\r
-{\r
-    vbox = NULL;\r
-    tooltips = NULL;\r
-    lpeobj = lpeobject;\r
-}\r
-\r
-Effect::~Effect()\r
-{\r
-    if (tooltips) {\r
-        delete tooltips;\r
-    }\r
-}\r
-\r
-Glib::ustring \r
-Effect::getName()\r
-{\r
-    if (lpeobj->effecttype_set && lpeobj->effecttype < INVALID_LPE)\r
-        return Glib::ustring( _(LPETypeConverter.get_label(lpeobj->effecttype).c_str()) );\r
-    else\r
-        return Glib::ustring( _("No effect") );\r
-}\r
-\r
-/*\r
- *  Here be the doEffect function chain:\r
- */\r
-void\r
-Effect::doEffect (SPCurve * curve)\r
-{\r
-    NArtBpath *new_bpath = doEffect(SP_CURVE_BPATH(curve));\r
-\r
-    if (new_bpath && new_bpath != SP_CURVE_BPATH(curve)) {        // FIXME, add function to SPCurve to change bpath? or a copy function?\r
-        if (curve->_bpath) {\r
-            g_free(curve->_bpath); //delete old bpath\r
-        }\r
-        curve->_bpath = new_bpath;\r
-    }\r
-}\r
-\r
-NArtBpath *\r
-Effect::doEffect (NArtBpath * path_in)\r
-{\r
-    try {\r
-        std::vector<Geom::Path> orig_pathv = BPath_to_2GeomPath(path_in);\r
-\r
-        std::vector<Geom::Path> result_pathv = doEffect(orig_pathv);\r
-\r
-        NArtBpath *new_bpath = BPath_from_2GeomPath(result_pathv);\r
-\r
-        return new_bpath;\r
-    }\r
-    catch (std::exception e) {\r
-        g_warning("An exception occurred during execution of an LPE - %s", e.what());\r
-        SP_ACTIVE_DESKTOP->messageStack()->flash( Inkscape::WARNING_MESSAGE,\r
-            _("An exception occurred during execution of a Path Effect.") );\r
-\r
-        NArtBpath *path_out;\r
-\r
-        unsigned ret = 0;\r
-        while ( path_in[ret].code != NR_END ) {\r
-            ++ret;\r
-        }\r
-        unsigned len = ++ret;\r
-\r
-        path_out = g_new(NArtBpath, len);\r
-        memcpy(path_out, path_in, len * sizeof(NArtBpath));\r
-        return path_out;\r
-    }\r
-}\r
-\r
-std::vector<Geom::Path>\r
-Effect::doEffect (std::vector<Geom::Path> & path_in)\r
-{\r
-    Geom::Piecewise<Geom::D2<Geom::SBasis> > pwd2_in;\r
-\r
-    for (unsigned int i=0; i < path_in.size(); i++) {\r
-        pwd2_in.concat( path_in[i].toPwSb() );\r
-    }\r
-\r
-    Geom::Piecewise<Geom::D2<Geom::SBasis> > pwd2_out = doEffect(pwd2_in);\r
-\r
-    std::vector<Geom::Path> path_out = Geom::path_from_piecewise( pwd2_out, LPE_CONVERSION_TOLERANCE);\r
-\r
-    return path_out;\r
-}\r
-\r
-Geom::Piecewise<Geom::D2<Geom::SBasis> >\r
-Effect::doEffect (Geom::Piecewise<Geom::D2<Geom::SBasis> > & pwd2_in)\r
-{\r
-    g_warning("Effect has no doEffect implementation");\r
-    return pwd2_in;\r
-}\r
-\r
-void\r
-Effect::readallParameters(Inkscape::XML::Node * repr)\r
-{\r
-    param_map_type::iterator it = param_map.begin();\r
-    while (it != param_map.end()) {\r
-        const gchar * key = (*it).first.c_str();\r
-        const gchar * value = repr->attribute(key);\r
-        if(value) {\r
-            setParameter(repr, key, NULL, value);\r
-        }\r
-        it++;\r
-    }\r
-}\r
-\r
-void\r
-Effect::setParameter(Inkscape::XML::Node * repr, const gchar * key, const gchar * old_value, const gchar * new_value)\r
-{\r
-    Glib::ustring stringkey(key);\r
-\r
-    param_map_type::iterator it = param_map.find(stringkey);\r
-    if (it != param_map.end()) {\r
-        if (new_value) {\r
-            bool accepted = it->second->param_readSVGValue(new_value);\r
-            if (!accepted) { \r
-                g_warning("Effect::setParameter - '%s' not accepted for %s", new_value, key);\r
-                // change was not accepted, so change it back.\r
-                // think: can this backfire and create infinite loop when started with unacceptable old_value?\r
-                // repr->setAttribute(key, old_value);\r
-            }\r
-        } else {\r
-            // set default value\r
-            it->second->param_set_default();\r
-        }\r
-    }\r
-}\r
-\r
-void\r
-Effect::registerParameter(Parameter * param)\r
-{\r
-    param_map[param->param_key] = param; // inserts or updates\r
-}\r
-\r
-Gtk::Widget *\r
-Effect::getWidget()\r
-{\r
-    if (!vbox) {\r
-        vbox = Gtk::manage( new Gtk::VBox() ); // use manage here, because after deletion of Effect object, others might still be pointing to this widget.\r
-        //if (!tooltips)\r
-            tooltips = new Gtk::Tooltips();\r
-\r
-        vbox->set_border_width(5);\r
-\r
-        param_map_type::iterator it = param_map.begin();\r
-        while (it != param_map.end()) {\r
-            Parameter * param = it->second;\r
-            Gtk::Widget * widg = param->param_getWidget();\r
-            Glib::ustring * tip = param->param_getTooltip();\r
-            if (widg) {\r
-               vbox->pack_start(*widg, true, true, 2);\r
-                if (tip != NULL) {\r
-                    tooltips->set_tip(*widg, *tip);\r
-                }\r
-            }\r
-\r
-            it++;\r
-        }\r
-    }\r
-\r
-    return dynamic_cast<Gtk::Widget *>(vbox);\r
-}\r
-\r
-\r
-Inkscape::XML::Node * \r
-Effect::getRepr()\r
-{\r
-    return SP_OBJECT_REPR(lpeobj);\r
-}\r
-\r
-SPDocument * \r
-Effect::getSPDoc()\r
-{\r
-    if (SP_OBJECT_DOCUMENT(lpeobj) == NULL) g_message("Effect::getSPDoc() returns NULL");\r
-    return SP_OBJECT_DOCUMENT(lpeobj);\r
-}\r
-\r
-\r
-} /* namespace LivePathEffect */\r
-\r
-} /* namespace Inkscape */\r
-\r
-/*\r
-  Local Variables:\r
-  mode:c++\r
-  c-file-style:"stroustrup"\r
-  c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))\r
-  indent-tabs-mode:nil\r
-  fill-column:99\r
-  End:\r
-*/\r
-// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :\r
+#define INKSCAPE_LIVEPATHEFFECT_CPP
+
+/*
+ * Copyright (C) Johan Engelen 2007 <j.b.c.engelen@utwente.nl>
+ *
+ * Released under GNU GPL, read the file 'COPYING' for more information
+ */
+
+#include "live_effects/effect.h"
+
+#include "display/display-forward.h"
+#include "xml/node-event-vector.h"
+#include "sp-object.h"
+#include "attributes.h"
+#include "message-stack.h"
+#include "desktop.h"
+#include "inkscape.h"
+#include "document.h"
+#include <glibmm/i18n.h>
+
+#include "live_effects/lpeobject.h"
+#include "live_effects/parameter/parameter.h"
+#include <glibmm/ustring.h>
+#include "live_effects/n-art-bpath-2geom.h"
+#include "display/curve.h"
+#include <gtkmm.h>
+
+#include <exception>
+
+#include <2geom/sbasis-to-bezier.h>
+#include <2geom/matrix.h>
+
+
+// include effects:
+#include "live_effects/lpe-skeletalstrokes.h"
+#include "live_effects/lpe-pathalongpath.h"
+//here!!
+#include "live_effects/lpe-sketch.h"
+#include "live_effects/lpe-vonkoch.h"
+#include "live_effects/lpe-knot.h"
+#include "live_effects/lpe-slant.h"
+#include "live_effects/lpe-test-doEffect-stack.h"
+#include "live_effects/lpe-gears.h"
+#include "live_effects/lpe-curvestitch.h"
+
+#include "nodepath.h"
+
+namespace Inkscape {
+
+namespace LivePathEffect {
+
+const Util::EnumData<EffectType> LPETypeData[INVALID_LPE] = {
+    // {constant defined in effect.h, N_("name of your effect"), "name of your effect in SVG"}
+    {PATH_ALONG_PATH,       N_("Bend Path"),             "bend_path"},
+    {SKELETAL_STROKES,      N_("Pattern Along Path"),    "skeletal"},
+    {SKETCH,                N_("Sketch"),                "sketch"},
+    {VONKOCH,               N_("VonKoch"),               "vonkoch"},
+    {KNOT,                  N_("Knot"),                  "knot"},
+#ifdef LPE_ENABLE_TEST_EFFECTS
+    {SLANT,                 N_("Slant"),                 "slant"},
+    {DOEFFECTSTACK_TEST,    N_("doEffect stack test"),   "doeffectstacktest"},
+#endif
+    {GEARS,                 N_("Gears"),                 "gears"},
+    {CURVE_STITCH,          N_("Stitch Sub-Paths"),       "curvestitching"},
+};
+const Util::EnumDataConverter<EffectType> LPETypeConverter(LPETypeData, INVALID_LPE);
+
+Effect*
+Effect::New(EffectType lpenr, LivePathEffectObject *lpeobj)
+{
+    Effect* neweffect = NULL;
+    switch (lpenr) {
+        case SKELETAL_STROKES:
+            neweffect = (Effect*) new LPESkeletalStrokes(lpeobj);
+            break;
+        case PATH_ALONG_PATH:
+            neweffect = (Effect*) new LPEPathAlongPath(lpeobj);
+            break;
+//here!!
+        case SKETCH:
+            neweffect = (Effect*) new LPESketch(lpeobj);
+            break;
+        case VONKOCH:
+            neweffect = (Effect*) new LPEVonKoch(lpeobj);
+            break;
+        case KNOT:
+            neweffect = (Effect*) new LPEKnot(lpeobj);
+            break;
+#ifdef LPE_ENABLE_TEST_EFFECTS
+            case SLANT:
+            neweffect = (Effect*) new LPESlant(lpeobj);
+            break;
+        case DOEFFECTSTACK_TEST:
+            neweffect = (Effect*) new LPEdoEffectStackTest(lpeobj);
+            break;
+#endif
+        case GEARS:
+            neweffect = (Effect*) new LPEGears(lpeobj);
+            break;
+        case CURVE_STITCH:
+            neweffect = (Effect*) new LPECurveStitch(lpeobj);
+            break;
+        default:
+            g_warning("LivePathEffect::Effect::New   called with invalid patheffect type (%d)", lpenr);
+            neweffect = NULL;
+            break;
+    }
+
+    if (neweffect) {
+        neweffect->readallParameters(SP_OBJECT_REPR(lpeobj));
+    }
+
+    return neweffect;
+}
+
+Effect::Effect(LivePathEffectObject *lpeobject)
+{
+    lpeobj = lpeobject;
+    oncanvasedit_it = 0;
+}
+
+Effect::~Effect()
+{
+}
+
+Glib::ustring
+Effect::getName()
+{
+    if (lpeobj->effecttype_set && lpeobj->effecttype < INVALID_LPE)
+        return Glib::ustring( _(LPETypeConverter.get_label(lpeobj->effecttype).c_str()) );
+    else
+        return Glib::ustring( _("No effect") );
+}
+
+void
+Effect::doBeforeEffect (SPLPEItem *lpeitem)
+{
+    //Do nothing for simple effects
+}
+
+
+/*
+ *  Here be the doEffect function chain:
+ */
+void
+Effect::doEffect (SPCurve * curve)
+{
+    NArtBpath *new_bpath = doEffect_nartbpath(SP_CURVE_BPATH(curve));
+
+    if (new_bpath && new_bpath != SP_CURVE_BPATH(curve)) {        // FIXME, add function to SPCurve to change bpath? or a copy function?
+        if (curve->_bpath) {
+            g_free(curve->_bpath); //delete old bpath
+        }
+        curve->_bpath = new_bpath;
+    }
+}
+
+NArtBpath *
+Effect::doEffect_nartbpath (NArtBpath * path_in)
+{
+    try {
+        std::vector<Geom::Path> orig_pathv = BPath_to_2GeomPath(path_in);
+
+        std::vector<Geom::Path> result_pathv = doEffect_path(orig_pathv);
+
+        NArtBpath *new_bpath = BPath_from_2GeomPath(result_pathv);
+
+        return new_bpath;
+    }
+    catch (std::exception & e) {
+        g_warning("Exception during LPE %s execution. \n %s", getName().c_str(), e.what());
+        SP_ACTIVE_DESKTOP->messageStack()->flash( Inkscape::WARNING_MESSAGE,
+            _("An exception occurred during execution of the Path Effect.") );
+
+        NArtBpath *path_out;
+
+        unsigned ret = 0;
+        while ( path_in[ret].code != NR_END ) {
+            ++ret;
+        }
+        unsigned len = ++ret;
+
+        path_out = g_new(NArtBpath, len);
+        memcpy(path_out, path_in, len * sizeof(NArtBpath));
+        return path_out;
+    }
+}
+
+std::vector<Geom::Path>
+Effect::doEffect_path (std::vector<Geom::Path> & path_in)
+{
+    Geom::Piecewise<Geom::D2<Geom::SBasis> > pwd2_in;
+
+    for (unsigned int i=0; i < path_in.size(); i++) {
+        pwd2_in.concat( path_in[i].toPwSb() );
+    }
+
+    Geom::Piecewise<Geom::D2<Geom::SBasis> > pwd2_out = doEffect_pwd2(pwd2_in);
+
+    std::vector<Geom::Path> path_out = Geom::path_from_piecewise( pwd2_out, LPE_CONVERSION_TOLERANCE);
+
+    return path_out;
+}
+
+Geom::Piecewise<Geom::D2<Geom::SBasis> >
+Effect::doEffect_pwd2 (Geom::Piecewise<Geom::D2<Geom::SBasis> > & pwd2_in)
+{
+    g_warning("Effect has no doEffect implementation");
+    return pwd2_in;
+}
+
+void
+Effect::readallParameters(Inkscape::XML::Node * repr)
+{
+    std::vector<Parameter *>::iterator it = param_vector.begin();
+    while (it != param_vector.end()) {
+        Parameter * param = *it;
+        const gchar * key = param->param_key.c_str();
+        const gchar * value = repr->attribute(key);
+        if (value) {
+            bool accepted = param->param_readSVGValue(value);
+            if (!accepted) { 
+                g_warning("Effect::readallParameters - '%s' not accepted for %s", value, key);
+            }
+        } else {
+            // set default value
+            param->param_set_default();
+        }
+
+        it++;
+    }
+}
+
+/* This function does not and SHOULD NOT write to XML */
+void
+Effect::setParameter(const gchar * key, const gchar * new_value)
+{
+    Parameter * param = getParameter(key);
+    if (param) {
+        if (new_value) {
+            bool accepted = param->param_readSVGValue(new_value);
+            if (!accepted) { 
+                g_warning("Effect::setParameter - '%s' not accepted for %s", new_value, key);
+            }
+        } else {
+            // set default value
+            param->param_set_default();
+        }
+    }
+}
+
+void
+Effect::registerParameter(Parameter * param)
+{
+    param_vector.push_back(param);
+}
+
+/**
+* This *creates* a new widget, management of deletion should be done by the caller
+*/
+Gtk::Widget *
+Effect::newWidget(Gtk::Tooltips * tooltips)
+{
+    // use manage here, because after deletion of Effect object, others might still be pointing to this widget.
+    Gtk::VBox * vbox = Gtk::manage( new Gtk::VBox() );
+
+    vbox->set_border_width(5);
+
+    std::vector<Parameter *>::iterator it = param_vector.begin();
+    while (it != param_vector.end()) {
+        Parameter * param = *it;
+        Gtk::Widget * widg = param->param_newWidget(tooltips);
+        Glib::ustring * tip = param->param_getTooltip();
+        if (widg) {
+           vbox->pack_start(*widg, true, true, 2);
+            if (tip != NULL) {
+                tooltips->set_tip(*widg, *tip);
+            }
+        }
+
+        it++;
+    }
+
+    return dynamic_cast<Gtk::Widget *>(vbox);
+}
+
+
+Inkscape::XML::Node *
+Effect::getRepr()
+{
+    return SP_OBJECT_REPR(lpeobj);
+}
+
+SPDocument *
+Effect::getSPDoc()
+{
+    if (SP_OBJECT_DOCUMENT(lpeobj) == NULL) g_message("Effect::getSPDoc() returns NULL");
+    return SP_OBJECT_DOCUMENT(lpeobj);
+}
+
+Parameter *
+Effect::getParameter(const char * key)
+{
+    Glib::ustring stringkey(key);
+
+    std::vector<Parameter *>::iterator it = param_vector.begin();
+    while (it != param_vector.end()) {
+        Parameter * param = *it;
+        if ( param->param_key == key) {
+            return param;
+        }
+
+        it++;
+    }
+
+    return NULL;
+}
+
+Parameter *
+Effect::getNextOncanvasEditableParam()
+{
+    oncanvasedit_it++;
+    if (oncanvasedit_it == static_cast<int>(param_vector.size())) {
+        oncanvasedit_it = 0;
+    }
+    int old_it = oncanvasedit_it;
+
+    do {
+        Parameter * param = param_vector[oncanvasedit_it];
+        if(param && param->oncanvas_editable) {
+            return param;
+        } else {
+            oncanvasedit_it++;
+            if (oncanvasedit_it == static_cast<int>(param_vector.size())) {  // loop round the map
+                oncanvasedit_it = 0;
+            }
+        }
+    } while (oncanvasedit_it != old_it); // iterate until complete loop through map has been made
+
+    return NULL;
+}
+
+void
+Effect::editNextParamOncanvas(SPItem * item, SPDesktop * desktop)
+{
+    if (!desktop) return;
+
+    Parameter * param = getNextOncanvasEditableParam();
+    if (param) {
+        param->param_editOncanvas(item, desktop);
+        gchar *message = g_strdup_printf(_("Editing parameter <b>%s</b>."), param->param_label.c_str());
+        desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, message);
+        g_free(message);
+    } else {
+        desktop->messageStack()->flash( Inkscape::WARNING_MESSAGE,
+                                        _("None of the applied path effect's parameters can be edited on-canvas.") );
+    }
+}
+
+/* This function should reset the defaults and is used for example to initialize an effect right after it has been applied to a path
+* The nice thing about this is that this function can use knowledge of the original path and set things accordingly for example to the size or origin of the original path!
+*/
+void
+Effect::resetDefaults(SPItem * /*item*/)
+{
+    // do nothing for simple effects
+}
+
+void
+Effect::setup_nodepath(Inkscape::NodePath::Path *np)
+{
+    np->show_helperpath = true;
+    np->helperpath_rgba = 0xff0000ff;
+    np->helperpath_width = 1.0;
+}
+
+void
+Effect::transform_multiply(Geom::Matrix const& postmul, bool set)
+{
+    // cycle through all parameters. Most parameters will not need transformation, but path and point params do.
+    for (std::vector<Parameter *>::iterator it = param_vector.begin(); it != param_vector.end(); it++) {
+        Parameter * param = *it;
+        param->param_transform_multiply(postmul, set);
+    }
+}
+
+} /* namespace LivePathEffect */
+
+} /* namespace Inkscape */
+
+/*
+  Local Variables:
+  mode:c++
+  c-file-style:"stroustrup"
+  c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
+  indent-tabs-mode:nil
+  fill-column:99
+  End:
+*/
+// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :