Code

Connector tool: make connectors avoid the convex hull of shapes.
[inkscape.git] / src / shape-editor.cpp
index b5fd85858c954f9b4dcf515074f367273c653421..44ad9dc9e9cf32addeff77e79348da8bfe0abffd 100644 (file)
@@ -17,6 +17,7 @@
 
 #include "sp-object.h"
 #include "sp-item.h"
+#include "sp-lpe-item.h"
 #include "live_effects/lpeobject.h"
 #include "selection.h"
 #include "desktop.h"
@@ -54,7 +55,8 @@ ShapeEditor::ShapeEditor(SPDesktop *dt) {
     this->nodepath = NULL;
     this->knotholder = NULL;
     this->hit = false;
-    this->listener_attached_for = NULL;
+    this->knotholder_listener_attached_for = NULL;
+    this->nodepath_listener_attached_for = NULL;
 }
 
 ShapeEditor::~ShapeEditor() {
@@ -69,10 +71,10 @@ void ShapeEditor::unset_item(SubType type, bool keep_knotholder) {
         case SH_NODEPATH:
             if (this->nodepath) {
                 old_repr = this->nodepath->repr;
-                if (old_repr && old_repr == listener_attached_for) {
+                if (old_repr && old_repr == nodepath_listener_attached_for) {
                     sp_repr_remove_listener_by_data(old_repr, this);
                     Inkscape::GC::release(old_repr);
-                    listener_attached_for = NULL;
+                    nodepath_listener_attached_for = NULL;
                 }
 
                 this->grab_node = -1;
@@ -83,10 +85,10 @@ void ShapeEditor::unset_item(SubType type, bool keep_knotholder) {
         case SH_KNOTHOLDER:
             if (this->knotholder) {
                 old_repr = this->knotholder->repr;
-                if (old_repr && old_repr == listener_attached_for) {
+                if (old_repr && old_repr == knotholder_listener_attached_for) {
                     sp_repr_remove_listener_by_data(old_repr, this);
                     Inkscape::GC::release(old_repr);
-                    listener_attached_for = NULL;
+                    knotholder_listener_attached_for = NULL;
                 }
 
                 if (!keep_knotholder) {
@@ -256,10 +258,10 @@ void ShapeEditor::set_item(SPItem *item, SubType type, bool keep_knotholder) {
 
                     // setting new listener
                     repr = SP_OBJECT_REPR(item);
-                    if (repr != listener_attached_for) {
+                    if (repr != nodepath_listener_attached_for) {
                         Inkscape::GC::anchor(repr);
                         sp_repr_add_listener(repr, &shapeeditor_repr_events, this);
-                        listener_attached_for = repr;
+                        nodepath_listener_attached_for = repr;
                     }
                 }
                 break;
@@ -273,10 +275,10 @@ void ShapeEditor::set_item(SPItem *item, SubType type, bool keep_knotholder) {
                     this->knotholder->update_knots();
                     // setting new listener
                     repr = this->knotholder->repr;
-                    if (repr != listener_attached_for) {
+                    if (repr != knotholder_listener_attached_for) {
                         Inkscape::GC::anchor(repr);
                         sp_repr_add_listener(repr, &shapeeditor_repr_events, this);
-                        listener_attached_for = repr;
+                        knotholder_listener_attached_for = repr;
                     }
                 }
                 break;
@@ -287,7 +289,7 @@ void ShapeEditor::set_item(SPItem *item, SubType type, bool keep_knotholder) {
 /** Please note that this function only works for path parameters.
 *  All other parameters probably will crash Inkscape!
 */
-void ShapeEditor::set_item_lpe_path_parameter(SPItem *item, SPObject *lpeobject, const char * key)
+void ShapeEditor::set_item_lpe_path_parameter(SPItem *item, LivePathEffectObject *lpeobject, const char * key)
 {
     unset_item(SH_NODEPATH);
 
@@ -303,10 +305,10 @@ void ShapeEditor::set_item_lpe_path_parameter(SPItem *item, SPObject *lpeobject,
 
             // setting new listener
             Inkscape::XML::Node *repr = SP_OBJECT_REPR(lpeobject);
-            if (repr && repr != listener_attached_for) {
+            if (repr && repr != nodepath_listener_attached_for) {
                 Inkscape::GC::anchor(repr);
                 sp_repr_add_listener(repr, &shapeeditor_repr_events, this);
-                listener_attached_for = repr;
+                nodepath_listener_attached_for = repr;
             }
         }
     }
@@ -317,22 +319,21 @@ void ShapeEditor::set_item_lpe_path_parameter(SPItem *item, SPObject *lpeobject,
    Why not make a reload function in NodePath and in KnotHolder? */
 void ShapeEditor::reset_item (SubType type, bool keep_knotholder)
 {
-    SPObject *item = sp_desktop_document(desktop)->getObjectByRepr(listener_attached_for);
-
     switch (type) {
         case SH_NODEPATH:
-            if ( (this->nodepath) && (IS_LIVEPATHEFFECT(this->nodepath->object)) ) {
-                SPObject *obj = this->nodepath->object;
-                char * key = g_strdup(this->nodepath->repr_key);
-                set_item_lpe_path_parameter(SP_ITEM(item), obj, key); // the above checks for nodepath, so it is indeed a path that we are editing
+            if ( (nodepath) && (IS_LIVEPATHEFFECT(nodepath->object)) ) {
+                char * key = g_strdup(nodepath->repr_key);
+                set_item_lpe_path_parameter(nodepath->item, LIVEPATHEFFECT(nodepath->object), key); // the above checks for nodepath, so it is indeed a path that we are editing
                 g_free(key);
             } else {
-                set_item(SP_ITEM(item), SH_NODEPATH);
-            }                
+                SPObject *obj = sp_desktop_document(desktop)->getObjectByRepr(nodepath_listener_attached_for); /// note that it is not certain that this is an SPItem; it could be a LivePathEffectObject.
+                set_item(SP_ITEM(obj), SH_NODEPATH);
+            }
             break;
         case SH_KNOTHOLDER:
-            if (this->knotholder) {
-                set_item(SP_ITEM(item), SH_KNOTHOLDER, keep_knotholder);
+            if ( knotholder ) {
+                SPObject *obj = sp_desktop_document(desktop)->getObjectByRepr(knotholder_listener_attached_for); /// note that it is not certain that this is an SPItem; it could be a LivePathEffectObject.
+                set_item(SP_ITEM(obj), SH_KNOTHOLDER, keep_knotholder);
             }
             break;
     }
@@ -353,6 +354,9 @@ bool ShapeEditor::is_over_stroke (Geom::Point event_p, bool remember) {
 
     const SPItem *item = get_item(SH_NODEPATH);
 
+    if (!item || !SP_IS_ITEM(item))
+        return false;
+
     //Translate click point into proper coord system
     this->curvepoint_doc = desktop->w2d(event_p);
     this->curvepoint_doc *= sp_item_dt2i_affine(item);