Code

No more NRMatrix or NRPoint.
[inkscape.git] / src / shape-editor.cpp
index d0dbb8937ba17b03aa40d9b53e6f9280f2606dd6..d73e99e7c8905ca484200ee6e3fead5d6a33e361 100644 (file)
 #include "config.h"
 #endif
 
+#include <string.h>
 #include <glibmm/i18n.h>
 
 #include "sp-object.h"
 #include "sp-item.h"
+#include "live_effects/lpeobject.h"
 #include "selection.h"
 #include "desktop.h"
 #include "desktop-handles.h"
 #include "knotholder.h"
+#include "live_effects/parameter/pointparam-knotholder.h"
 #include "node-context.h"
 #include "xml/node-event-vector.h"
 #include "prefs-utils.h"
@@ -30,7 +33,7 @@
 #include "shape-editor.h"
 
 
-ShapeEditorsCollective::ShapeEditorsCollective(SPDesktop *dt) {
+ShapeEditorsCollective::ShapeEditorsCollective(SPDesktop */*dt*/) {
 }
 
 ShapeEditorsCollective::~ShapeEditorsCollective() {
@@ -93,9 +96,9 @@ bool ShapeEditor::has_knotholder () {
 }
 
 bool ShapeEditor::has_local_change () {
-    if (this->nodepath) 
+    if (this->nodepath)
         return (this->nodepath->local_change != 0);
-    else if (this->knotholder) 
+    else if (this->knotholder)
         return (this->knotholder->local_change != 0);
     else
         return false;
@@ -113,7 +116,7 @@ void ShapeEditor::decrement_local_change () {
 SPItem *ShapeEditor::get_item () {
     SPItem *item = NULL;
     if (this->has_nodepath()) {
-        item = SP_ITEM(this->nodepath->object);
+        item = this->nodepath->item;
     } else if (this->has_knotholder()) {
         item = SP_ITEM(this->knotholder->item);
     }
@@ -131,26 +134,26 @@ void ShapeEditor::restore_nodepath_selection (GList *saved) {
         ::restore_nodepath_selection (this->nodepath, saved);
 }
 
+bool ShapeEditor::nodepath_edits_repr_key(gchar const *name) {
+    if (nodepath && name) {
+        return ( !strcmp(name, nodepath->repr_key) || !strcmp(name, nodepath->repr_nodetypes_key) );
+    }
 
-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)
+    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)
 {
-    SPItem *item = NULL;
     gboolean changed = FALSE;
 
     g_assert(data);
     ShapeEditor *sh = ((ShapeEditor *) data);
 
-    item = sh->get_item();
-
-    if ( ( (sh->has_nodepath())
-           && (  !strcmp(name, "d") // With paths, we only need to act if one of the path-affecting attributes has changed.
-                 || !strcmp(name, "sodipodi:nodetypes")
-                 || !strcmp(name, "inkscape:original-d") ) )
-          || sh->has_knotholder() )
+    if ( sh->has_knotholder() || ( sh->has_nodepath() && sh->nodepath_edits_repr_key(name) ) )
     {
-        changed = !sh->has_local_change(); 
+        changed = !sh->has_local_change();
         sh->decrement_local_change();
     }
 
@@ -160,7 +163,7 @@ static void shapeeditor_event_attr_changed(Inkscape::XML::Node *repr, gchar cons
             saved = sh->save_nodepath_selection();
         }
 
-        sh->set_item (item);
+        sh->reset_item ();
 
         if (sh->has_nodepath() && saved) {
             sh->restore_nodepath_selection(saved);
@@ -189,7 +192,7 @@ void ShapeEditor::set_item(SPItem *item) {
     if (item) {
         this->nodepath = sp_nodepath_new(desktop, item, (prefs_get_int_attribute("tools.nodes", "show_handles", 1) != 0));
         if (this->nodepath) {
-            this->nodepath->shape_editor = this; 
+            this->nodepath->shape_editor = this;
         }
         this->knotholder = sp_item_knot_holder(item, desktop);
 
@@ -208,29 +211,24 @@ void ShapeEditor::set_item(SPItem *item) {
     }
 }
 
-void ShapeEditor::set_livepatheffect_parameter(SPObject *lpeobject, const char * key) {
-
+/** 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)
+{
     unset_item();
 
     this->grab_node = -1;
 
     if (lpeobject) {
-        this->nodepath = sp_nodepath_new( desktop, lpeobject, 
+        this->nodepath = sp_nodepath_new( desktop, lpeobject,
                                           (prefs_get_int_attribute("tools.nodes", "show_handles", 1) != 0),
-                                          key);
+                                          key, item);
         if (this->nodepath) {
-            this->nodepath->shape_editor = this; 
-        }
-        //this->knotholder = sp_item_knot_holder(item, desktop);
-        g_message("create knotholder?");
+            this->nodepath->shape_editor = this;
 
-        if (this->nodepath || this->knotholder) {
             // setting new listener
-            Inkscape::XML::Node *repr;
-            if (this->knotholder)
-                repr = this->knotholder->repr;
-            else
-                repr = SP_OBJECT_REPR(lpeobject);
+            Inkscape::XML::Node *repr = SP_OBJECT_REPR(lpeobject);
             if (repr) {
                 Inkscape::GC::anchor(repr);
                 sp_repr_add_listener(repr, &shapeeditor_repr_events, this);
@@ -239,6 +237,38 @@ void ShapeEditor::set_livepatheffect_parameter(SPObject *lpeobject, const char *
     }
 }
 
+/** 
+*  pass a new knotholder to ShapeEditor to manage (and delete)
+*/
+void
+ShapeEditor::set_knotholder(SPKnotHolder * knot_holder)
+{
+    unset_item();
+
+    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?
+   Why not make a reload function in NodePath and in KnotHolder? */
+void ShapeEditor::reset_item ()
+{
+    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
+        g_free(key);
+    } else {
+        SPItem * item = get_item();
+        set_item(item);
+    }
+}
+
 void ShapeEditor::nodepath_destroyed () {
     this->nodepath = NULL;
 }
@@ -250,7 +280,7 @@ void ShapeEditor::update_statusbar () {
 
 bool ShapeEditor::is_over_stroke (NR::Point event_p, bool remember) {
 
-    if (!this->nodepath) 
+    if (!this->nodepath)
         return false; // no stroke in knotholder
 
     SPItem *item = get_item();
@@ -272,13 +302,12 @@ bool ShapeEditor::is_over_stroke (NR::Point event_p, bool remember) {
     delta = desktop->d2w(delta);
 
     double stroke_tolerance =
-        ( !SP_OBJECT_STYLE(item)->stroke.isNone() ?
-         desktop->current_zoom() *
-         SP_OBJECT_STYLE (item)->stroke_width.computed *
-         sp_item_i2d_affine (item).expansion() * 0.5
+        (( !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))
          : 0.0)
-        + prefs_get_int_attribute_limited("options.dragtolerance", "value", 0, 0, 100); //(double) SP_EVENT_CONTEXT(nc)->tolerance;
-
+         + 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);
 
     if (remember && close) {
@@ -437,11 +466,11 @@ void ShapeEditor::scale_nodes_screen(gdouble const grow, int const which) {
 }
 
 void ShapeEditor::select_all (bool invert) {
-    if (this->nodepath) 
+    if (this->nodepath)
         sp_nodepath_select_all (this->nodepath, invert);
 }
 void ShapeEditor::select_all_from_subpath (bool invert) {
-    if (this->nodepath) 
+    if (this->nodepath)
         sp_nodepath_select_all_from_subpath (this->nodepath, invert);
 }
 void ShapeEditor::select_next () {
@@ -462,22 +491,26 @@ void ShapeEditor::select_prev () {
 }
 
 void ShapeEditor::show_handles (bool show) {
-    if (this->nodepath
+    if (this->nodepath && !this->nodepath->straight_path)
         sp_nodepath_show_handles (this->nodepath, show);
 }
 
+void ShapeEditor::show_helperpath (bool show) {
+    if (this->nodepath)
+        sp_nodepath_show_helperpath (this->nodepath, show);
+}
 
 void ShapeEditor::flip (NR::Dim2 axis, NR::Maybe<NR::Point> center) {
-    if (this->nodepath) 
+    if (this->nodepath)
         sp_nodepath_flip (this->nodepath, axis, center);
 }
 
 void ShapeEditor::distribute (NR::Dim2 axis) {
-    if (this->nodepath) 
+    if (this->nodepath)
         sp_nodepath_selected_distribute (this->nodepath, axis);
 }
 void ShapeEditor::align (NR::Dim2 axis) {
-    if (this->nodepath) 
+    if (this->nodepath)
         sp_nodepath_selected_align (this->nodepath, axis);
 }