Code

Connector tool: make connectors avoid the convex hull of shapes.
[inkscape.git] / src / shape-editor.cpp
index 6cf1182ee0aedb92e71a76a920cb402cfc76716d..44ad9dc9e9cf32addeff77e79348da8bfe0abffd 100644 (file)
 
 #include "sp-object.h"
 #include "sp-item.h"
+#include "sp-lpe-item.h"
 #include "live_effects/lpeobject.h"
 #include "selection.h"
 #include "desktop.h"
+#include "document.h"
 #include "desktop-handles.h"
 #include "knotholder.h"
-#include "live_effects/parameter/pointparam-knotholder.h"
+#include "live_effects/parameter/point.h"
 #include "nodepath.h"
 #include "xml/node-event-vector.h"
-#include "prefs-utils.h"
+#include "preferences.h"
 #include "object-edit.h"
 #include "style.h"
 #include "display/curve.h"
@@ -53,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() {
@@ -68,24 +71,24 @@ 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;
-                sp_nodepath_destroy(this->nodepath);
+                delete this->nodepath;
                 this->nodepath = NULL;
             }
             break;
         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) {
@@ -174,50 +177,55 @@ bool ShapeEditor::nodepath_edits_repr_key(gchar const *name) {
     return false;
 }
 
-static void shapeeditor_event_attr_changed(Inkscape::XML::Node */*repr*/, gchar const *name,
-                                           gchar const */*old_value*/, gchar const */*new_value*/,
-                                           bool /*is_interactive*/, gpointer data)
+
+void ShapeEditor::shapeeditor_event_attr_changed(gchar const *name)
 {
     gboolean changed_np = FALSE;
     gboolean changed_kh = FALSE;
 
-    g_assert(data);
-    ShapeEditor *sh = ((ShapeEditor *) data);
-
-    if (sh->has_nodepath() && sh->nodepath_edits_repr_key(name))
+    if (has_nodepath() && nodepath_edits_repr_key(name))
     {
-        changed_np = !sh->has_local_change(SH_NODEPATH);
-        sh->decrement_local_change(SH_NODEPATH);
-
+        changed_np = !has_local_change(SH_NODEPATH);
+        decrement_local_change(SH_NODEPATH);
     }
 
     if (changed_np) {
         GList *saved = NULL;
-        if (sh->has_nodepath()) {
-            saved = sh->save_nodepath_selection();
+        if (has_nodepath()) {
+            saved = save_nodepath_selection();
         }
 
-        sh->reset_item(SH_NODEPATH);
+        reset_item(SH_NODEPATH);
 
-        if (sh->has_nodepath() && saved) {
-            sh->restore_nodepath_selection(saved);
+        if (has_nodepath() && saved) {
+            restore_nodepath_selection(saved);
             g_list_free (saved);
         }
     }
 
-
-    if (sh->has_knotholder())
+    if (has_knotholder())
     {
-        changed_kh = !sh->has_local_change(SH_KNOTHOLDER);
-        sh->decrement_local_change(SH_KNOTHOLDER);
+        changed_kh = !has_local_change(SH_KNOTHOLDER);
+        decrement_local_change(SH_KNOTHOLDER);
         if (changed_kh) {
             // this can happen if an LPEItem's knotholder handle was dragged, in which case we want
             // to keep the knotholder; in all other cases (e.g., if the LPE itself changes) we delete it
-            sh->reset_item(SH_KNOTHOLDER, !strcmp(name, "d"));
+            reset_item(SH_KNOTHOLDER, !strcmp(name, "d"));
         }
     }
 
-    sh->update_statusbar(); //TODO: sh->get_container()->update_statusbar();
+    update_statusbar(); //TODO: get_container()->update_statusbar();
+}
+
+
+static void shapeeditor_event_attr_changed(Inkscape::XML::Node */*repr*/, gchar const *name,
+                                           gchar const */*old_value*/, gchar const */*new_value*/,
+                                           bool /*is_interactive*/, gpointer data)
+{
+    g_assert(data);
+    ShapeEditor *sh = ((ShapeEditor *) data);
+
+    sh->shapeeditor_event_attr_changed(name);
 }
 
 static Inkscape::XML::NodeEventVector shapeeditor_repr_events = {
@@ -230,8 +238,9 @@ static Inkscape::XML::NodeEventVector shapeeditor_repr_events = {
 
 
 void ShapeEditor::set_item(SPItem *item, SubType type, bool keep_knotholder) {
-    // this happens (and should only happen) when for an LPEItem having both knotholder and nodepath the knotholder
-    // is adapted; in this case we don't want to delete the knotholder since this freezes the handles
+    // this happens (and should only happen) when for an LPEItem having both knotholder and
+    // nodepath the knotholder is adapted; in this case we don't want to delete the knotholder
+    // since this freezes the handles
     unset_item(type, keep_knotholder);
 
     this->grab_node = -1;
@@ -241,17 +250,18 @@ void ShapeEditor::set_item(SPItem *item, SubType type, bool keep_knotholder) {
         switch(type) {
             case SH_NODEPATH:
                 if (SP_IS_LPE_ITEM(item)) {
-                    this->nodepath = sp_nodepath_new(desktop, item, (prefs_get_int_attribute("tools.nodes", "show_handles", 1) != 0));
+                    Inkscape::Preferences *prefs = Inkscape::Preferences::get();
+                    this->nodepath = sp_nodepath_new(desktop, item, (prefs->getBool("/tools/nodes/show_handles", true)));
                 }
                 if (this->nodepath) {
                     this->nodepath->shape_editor = this;
 
                     // 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;
@@ -265,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;
@@ -279,67 +289,51 @@ 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);
 
     this->grab_node = -1;
 
     if (lpeobject) {
+        Inkscape::Preferences *prefs = Inkscape::Preferences::get();
         this->nodepath = sp_nodepath_new( desktop, lpeobject,
-                                          (prefs_get_int_attribute("tools.nodes", "show_handles", 1) != 0),
+                                          (prefs->getInt("/tools/nodes/show_handles", true)),
                                           key, item);
         if (this->nodepath) {
             this->nodepath->shape_editor = this;
 
             // 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;
             }
         }
     }
 }
 
-/** 
-*  pass a new knotholder to ShapeEditor to manage (and delete)
-*/
-void
-ShapeEditor::set_knotholder(KnotHolder * knot_holder)
-{
-    unset_item(SH_KNOTHOLDER);
-
-    this->grab_node = -1;
-
-    if (knot_holder) {
-        this->knotholder = knot_holder;
-    }
-}
-
 
-/** FIXME: think about this. Is this thing only called when the item needs to be updated?
+/** FIXME: This thing is only called when the item needs to be updated in response to repr change.
    Why not make a reload function in NodePath and in KnotHolder? */
 void ShapeEditor::reset_item (SubType type, bool keep_knotholder)
 {
     switch (type) {
         case SH_NODEPATH:
-            if ( (this->nodepath) && (IS_LIVEPATHEFFECT(this->nodepath->object)) ) {
-                SPItem * item = this->nodepath->item;
-                SPObject *obj = this->nodepath->object;
-                char * key = g_strdup(this->nodepath->repr_key);
-                set_item_lpe_path_parameter(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 {
-                SPItem * item = (SPItem *) get_item(SH_NODEPATH);
-                set_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) {
-                SPItem * item = (SPItem *) get_item(SH_KNOTHOLDER);
-                set_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;
     }
@@ -360,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);
@@ -377,14 +374,15 @@ bool ShapeEditor::is_over_stroke (Geom::Point event_p, bool remember) {
 
     delta = desktop->d2w(delta);
 
+    Inkscape::Preferences *prefs = Inkscape::Preferences::get();
     double stroke_tolerance =
         (( !SP_OBJECT_STYLE(item)->stroke.isNone() ?
            desktop->current_zoom() *
            SP_OBJECT_STYLE (item)->stroke_width.computed * 0.5 *
-           NR::expansion(sp_item_i2d_affine(item))
+           to_2geom(sp_item_i2d_affine(item)).descrim()
          : 0.0)
-         + prefs_get_int_attribute_limited("options.dragtolerance", "value", 0, 0, 100)) /NR::expansion(sp_item_i2d_affine(item)); 
-    bool close = (NR::L2 (delta) < stroke_tolerance);
+         + prefs->getIntLimited("/options/dragtolerance/value", 0, 0, 100)) / to_2geom(sp_item_i2d_affine(item)).descrim();
+    bool close = (Geom::L2 (delta) < stroke_tolerance);
 
     if (remember && close) {
         // calculate index for nodepath's representation.
@@ -475,7 +473,7 @@ void ShapeEditor::finish_drag() {
     }
 }
 
-void ShapeEditor::select_rect(NR::Rect const &rect, bool add) {
+void ShapeEditor::select_rect(Geom::Rect const &rect, bool add) {
     if (this->nodepath) {
         sp_nodepath_select_rect(this->nodepath, rect, add);
     }