Code

Fixed copying of old settings, along with some data() vs. c_str() cleanup. Fixes...
[inkscape.git] / src / sp-lpe-item.cpp
index 3bf5a28ee755ff88ec077be3d8d9c5ba8d46e1bc..49264c684c5217b30205a16e08871a8e9f102731 100644 (file)
@@ -18,6 +18,7 @@
 #endif
 
 #include "live_effects/effect.h"
+#include "live_effects/lpe-path_length.h"
 #include "live_effects/lpeobject.h"
 #include "live_effects/lpeobject-reference.h"
 
 #include "sp-lpe-item.h"
 #include "xml/repr.h"
 #include "uri.h"
+#include "message-stack.h"
+#include "inkscape.h"
+#include "desktop.h"
+#include "node-context.h"
+#include "shape-editor.h"
 
 #include <algorithm>
 
@@ -123,8 +129,6 @@ sp_lpe_item_finalize(GObject *object)
     if (((GObjectClass *) (parent_class))->finalize) {
         (* ((GObjectClass *) (parent_class))->finalize)(object);
     }
-
-    delete SP_LPE_ITEM(object)->path_effect_list;
 }
 
 /**
@@ -148,12 +152,21 @@ sp_lpe_item_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *r
 static void
 sp_lpe_item_release(SPObject *object)
 {
-    SPLPEItem *lpeitem;
-    lpeitem = (SPLPEItem *) object;
+    SPLPEItem *lpeitem = (SPLPEItem *) object;
 
     lpeitem->lpe_modified_connection.disconnect();
     lpeitem->lpe_modified_connection.~connection();
 
+    PathEffectList::iterator it = lpeitem->path_effect_list->begin();
+    while ( it != lpeitem->path_effect_list->end() ) {
+        // unlink and delete all references in the list
+        (*it)->unlink();
+        delete *it;
+        it = lpeitem->path_effect_list->erase(it);
+    }
+    // delete the list itself
+    delete SP_LPE_ITEM(object)->path_effect_list;
+
     if (((SPObjectClass *) parent_class)->release)
         ((SPObjectClass *) parent_class)->release(object);
 }
@@ -189,22 +202,24 @@ sp_lpe_item_set(SPObject *object, unsigned int key, gchar const *value)
                     std::string href;
                     while (std::getline(iss, href, ';'))
                     {
-                        Inkscape::LivePathEffect::LPEObjectReference *path_effect_ref;
-                        path_effect_ref = new Inkscape::LivePathEffect::LPEObjectReference(SP_OBJECT(lpeitem));
+                        Inkscape::LivePathEffect::LPEObjectReference *path_effect_ref = new Inkscape::LivePathEffect::LPEObjectReference(SP_OBJECT(lpeitem));
                         path_effect_ref->changedSignal().connect(sigc::bind(sigc::ptr_fun(lpeobject_ref_changed), SP_LPE_ITEM(object)));
                         // Now do the attaching, which emits the changed signal.
                         // Fixme, it should not do this changed signal and updating before all effects are added to the path_effect_list
                         try {
                             path_effect_ref->link(href.c_str());
-                        } catch (Inkscape::BadURIException &e) {
-                            g_warning("BadURIException: %s", e.what());
+                        } catch (Inkscape::BadURIException e) {
+                            g_warning("BadURIException when trying to find LPE: %s", e.what());
                             path_effect_ref->unlink();
                             delete path_effect_ref;
                             path_effect_ref = NULL;
                         }
 
-                        if (path_effect_ref) {
-                            lpeitem->path_effect_list->push_back(path_effect_ref);
+                        lpeitem->path_effect_list->push_back(path_effect_ref);
+                        if ( !(path_effect_ref->lpeobject && path_effect_ref->lpeobject->get_lpe()) ) {
+                            // something has gone wrong in finding the right patheffect.
+                            g_warning("Unknown LPE type specified, LPE stack effectively disabled");
+                            // keep the effect in the lpestack, so the whole stack is effectively disabled but maintained
                         }
                     }
                 }
@@ -229,6 +244,20 @@ sp_lpe_item_update(SPObject *object, SPCtx *ctx, guint flags)
     if (((SPObjectClass *) parent_class)->update) {
         ((SPObjectClass *) parent_class)->update(object, ctx, flags);
     }
+
+    // update the helperpaths of all LPEs applied to the item
+    // TODO: is there a more canonical place for this, since we don't have instant access to the item's nodepath?
+    // FIXME: this is called multiple (at least 3) times; how can we avoid this?
+
+    // FIXME: ditch inkscape_active_event_context()
+    SPEventContext *ec = inkscape_active_event_context();
+    if (!SP_IS_NODE_CONTEXT(ec)) return;
+    ShapeEditor *sh = ec->shape_editor;
+    g_assert(sh);
+    if (!sh->has_nodepath()) return;
+
+    Inkscape::NodePath::Path *np = sh->get_nodepath();
+    sp_nodepath_update_helperpaths(np);
 }
 
 /**
@@ -237,7 +266,6 @@ sp_lpe_item_update(SPObject *object, SPCtx *ctx, guint flags)
 static void
 sp_lpe_item_modified (SPObject *object, unsigned int flags)
 {
-
     if (SP_IS_GROUP(object) && (flags & SP_OBJECT_MODIFIED_FLAG) && (flags & SP_OBJECT_USER_MODIFIED_FLAG_B)) {
         sp_lpe_item_update_patheffect(SP_LPE_ITEM(object), true, true);
     }
@@ -255,11 +283,13 @@ sp_lpe_item_write(SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::
 {
     SPLPEItem *lpeitem = (SPLPEItem *) object;
 
-    if ( sp_lpe_item_has_path_effect(lpeitem) ) {
-        std::string href = patheffectlist_write_svg(*lpeitem->path_effect_list);
-        repr->setAttribute("inkscape:path-effect", href.c_str());
-    } else {
-        repr->setAttribute("inkscape:path-effect", NULL);
+    if (flags & SP_OBJECT_WRITE_EXT) {
+        if ( sp_lpe_item_has_path_effect(lpeitem) ) {
+            std::string href = patheffectlist_write_svg(*lpeitem->path_effect_list);
+            repr->setAttribute("inkscape:path-effect", href.c_str());
+        } else {
+            repr->setAttribute("inkscape:path-effect", NULL);
+        }
     }
 
     if (((SPObjectClass *)(parent_class))->write) {
@@ -269,33 +299,61 @@ sp_lpe_item_write(SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::
     return repr;
 }
 
-void sp_lpe_item_perform_path_effect(SPLPEItem *lpeitem, SPCurve *curve) {
-    if (!lpeitem) return;
-    if (!curve) return;
+/**
+ * returns true when LPE was successful.
+ */
+bool sp_lpe_item_perform_path_effect(SPLPEItem *lpeitem, SPCurve *curve) {
+    if (!lpeitem) return false;
+    if (!curve) return false;
 
     if (sp_lpe_item_has_path_effect(lpeitem) && sp_lpe_item_path_effects_enabled(lpeitem)) {
         for (PathEffectList::iterator it = lpeitem->path_effect_list->begin(); it != lpeitem->path_effect_list->end(); ++it)
         {
             LivePathEffectObject *lpeobj = (*it)->lpeobject;
             if (!lpeobj) {
+                /** \todo Investigate the cause of this.
+                 * For example, this happens when copy pasting an object with LPE applied. Probably because the object is pasted while the effect is not yet pasted to defs, and cannot be found.
+                 */
                 g_warning("sp_lpe_item_perform_path_effect - NULL lpeobj in list!");
-                return;
+                return false;
             }
-            if (!lpeobj->lpe) {
-                g_warning("sp_lpe_item_perform_path_effect - lpeobj without lpe!");
-                return;
+            Inkscape::LivePathEffect::Effect *lpe = lpeobj->get_lpe();
+            if (!lpe) {
+                /** \todo Investigate the cause of this.
+                 * Not sure, but I think this can happen when an unknown effect type is specified...
+                 */
+                g_warning("sp_lpe_item_perform_path_effect - lpeobj with invalid lpe in the stack!");
+                return false;
             }
 
-            if (lpeobj->lpe->isVisible()) {
+            if (lpe->isVisible()) {
+                if (lpe->acceptsNumClicks() > 0 && !lpe->isReady()) {
+                    // if the effect expects mouse input before being applied and the input is not finished
+                    // yet, we don't alter the path
+                    return false;
+                }
+
                 // Groups have their doBeforeEffect called elsewhere
                 if (!SP_IS_GROUP(lpeitem)) {
-                    lpeobj->lpe->doBeforeEffect(lpeitem);
+                    lpe->doBeforeEffect(lpeitem);
                 }
 
-                lpeobj->lpe->doEffect(curve);
+                try {
+                    lpe->doEffect(curve);
+                }
+                catch (std::exception & e) {
+                    g_warning("Exception during LPE %s execution. \n %s", lpe->getName().c_str(), e.what());
+                    if (SP_ACTIVE_DESKTOP && SP_ACTIVE_DESKTOP->messageStack()) {
+                        SP_ACTIVE_DESKTOP->messageStack()->flash( Inkscape::WARNING_MESSAGE,
+                                        _("An exception occurred during execution of the Path Effect.") );
+                    }
+                    return false;
+                }
             }
         }
     }
+
+    return true;
 }
 
 /**
@@ -313,6 +371,21 @@ sp_lpe_item_update_patheffect (SPLPEItem *lpeitem, bool wholetree, bool write)
     if (!sp_lpe_item_path_effects_enabled(lpeitem))
         return;
 
+    // TODO: hack! this will be removed when path length measuring is reimplemented in a better way
+    PathEffectList lpelist = sp_lpe_item_get_effect_list(lpeitem);
+    std::list<Inkscape::LivePathEffect::LPEObjectReference *>::iterator i;
+    for (i = lpelist.begin(); i != lpelist.end(); ++i) {
+        if ((*i)->lpeobject) {
+            Inkscape::LivePathEffect::Effect *lpe = (*i)->lpeobject->get_lpe();
+            if (dynamic_cast<Inkscape::LivePathEffect::LPEPathLength *>(lpe)) {
+                if (!lpe->isVisible()) {
+                    // we manually disable text for LPEPathLength
+                    dynamic_cast<Inkscape::LivePathEffect::LPEPathLength *>(lpe)->hideCanvasText();
+                }
+            }
+        }
+    }
+
     SPLPEItem *top;
 
     if (wholetree) {
@@ -429,14 +502,19 @@ void sp_lpe_item_add_path_effect(SPLPEItem *lpeitem, gchar *value, bool reset)
         sp_lpe_item_create_original_path_recursive(lpeitem);
 
         LivePathEffectObject *lpeobj = lpeitem->path_effect_list->back()->lpeobject;
-        if (lpeobj && lpeobj->lpe) {
+        if (lpeobj && lpeobj->get_lpe()) {
+            Inkscape::LivePathEffect::Effect *lpe = lpeobj->get_lpe();
             // Ask the path effect to reset itself if it doesn't have parameters yet
             if (reset) {
                 // has to be called when all the subitems have their lpes applied
-                lpeobj->lpe->resetDefaults(lpeitem);
+                lpe->resetDefaults(lpeitem);
             }
-            /* perform this once when the effect is applied */
-            lpeobj->lpe->doOnApply(SP_LPE_ITEM(lpeitem));
+
+            // perform this once when the effect is applied
+            lpe->doOnApply(SP_LPE_ITEM(lpeitem));
+
+            // indicate that all necessary preparations are done and the effect can be performed
+            lpe->setReady();
         }
 
         //Enable the path effects now that everything is ready to apply the new path effect
@@ -465,7 +543,11 @@ void sp_lpe_item_remove_current_path_effect(SPLPEItem *lpeitem, bool keep_paths)
     new_list.remove(lperef); //current lpe ref is always our 'own' pointer from the path_effect_list
     std::string r = patheffectlist_write_svg(new_list);
 
-    SP_OBJECT_REPR(lpeitem)->setAttribute("inkscape:path-effect", r.c_str());
+    if (!r.empty()) {
+        SP_OBJECT_REPR(lpeitem)->setAttribute("inkscape:path-effect", r.c_str());
+    } else {
+        SP_OBJECT_REPR(lpeitem)->setAttribute("inkscape:path-effect", NULL);
+    }
 
     if (!keep_paths) {
         sp_lpe_item_cleanup_original_path_recursive(lpeitem);
@@ -522,10 +604,40 @@ void sp_lpe_item_up_current_path_effect(SPLPEItem *lpeitem)
     sp_lpe_item_cleanup_original_path_recursive(lpeitem);
 }
 
+/** used for shapes so they can see if they should also disable shape calculation and read from d= */
+bool sp_lpe_item_has_broken_path_effect(SPLPEItem *lpeitem)
+{
+    if (lpeitem->path_effect_list->empty())
+        return false;
+
+    // go through the list; if some are unknown or invalid, return true
+    PathEffectList effect_list =  sp_lpe_item_get_effect_list(lpeitem);
+    for (PathEffectList::iterator it = effect_list.begin(); it != effect_list.end(); it++)
+    {
+        LivePathEffectObject *lpeobj = (*it)->lpeobject;
+        if (!lpeobj || !lpeobj->get_lpe())
+            return true;
+    }
+
+    return false;
+}
+
 
 bool sp_lpe_item_has_path_effect(SPLPEItem *lpeitem)
 {
-    return !lpeitem->path_effect_list->empty();
+    if (lpeitem->path_effect_list->empty())
+        return false;
+
+    // go through the list; if some are unknown or invalid, we are not an LPE item!
+    PathEffectList effect_list =  sp_lpe_item_get_effect_list(lpeitem);
+    for (PathEffectList::iterator it = effect_list.begin(); it != effect_list.end(); it++)
+    {
+        LivePathEffectObject *lpeobj = (*it)->lpeobject;
+        if (!lpeobj || !lpeobj->get_lpe())
+            return false;
+    }
+
+    return true;
 }
 
 bool sp_lpe_item_has_path_effect_recursive(SPLPEItem *lpeitem)
@@ -539,11 +651,36 @@ bool sp_lpe_item_has_path_effect_recursive(SPLPEItem *lpeitem)
     }
 }
 
+Inkscape::LivePathEffect::Effect*
+sp_lpe_item_has_path_effect_of_type(SPLPEItem *lpeitem, int type)
+{
+    std::list<Inkscape::LivePathEffect::LPEObjectReference *>::iterator i;
+    for (i = lpeitem->path_effect_list->begin(); i != lpeitem->path_effect_list->end(); ++i) {
+        Inkscape::LivePathEffect::Effect* lpe = (*i)->lpeobject->get_lpe();
+        if (lpe && (lpe->effectType() == type)) {
+            return lpe;
+        }
+    }
+    return NULL;
+}
+
+/* Return false if the item is not a path or already has a shape applied */
+bool sp_lpe_item_can_accept_freehand_shape(SPLPEItem *lpeitem)
+{
+    if (!SP_IS_PATH(lpeitem))
+        return false;
+
+    if (sp_lpe_item_has_path_effect_of_type(lpeitem, Inkscape::LivePathEffect::FREEHAND_SHAPE))
+        return false;
+
+    return true;
+}
+
 void sp_lpe_item_edit_next_param_oncanvas(SPLPEItem *lpeitem, SPDesktop *dt)
 {
     Inkscape::LivePathEffect::LPEObjectReference *lperef = sp_lpe_item_get_current_lpereference(lpeitem);
-    if (lperef && lperef->lpeobject && lperef->lpeobject->lpe) {
-        lperef->lpeobject->lpe->editNextParamOncanvas(SP_ITEM(lpeitem), dt);
+    if (lperef && lperef->lpeobject && lperef->lpeobject->get_lpe()) {
+        lperef->lpeobject->get_lpe()->editNextParamOncanvas(SP_ITEM(lpeitem), dt);
     }
 }
 
@@ -627,7 +764,7 @@ Inkscape::LivePathEffect::Effect* sp_lpe_item_get_current_lpe(SPLPEItem *lpeitem
     Inkscape::LivePathEffect::LPEObjectReference* lperef = sp_lpe_item_get_current_lpereference(lpeitem);
 
     if (lperef && lperef->lpeobject)
-        return lperef->lpeobject->lpe;
+        return lperef->lpeobject->get_lpe();
     else
         return NULL;
 }