Code

From trunk
[inkscape.git] / src / shape-editor.cpp
index 77419d1d10393a6cc17a06614f5292a7c267bbcb..04f40929bec2cbfd328b89ee199d59b8d8234ab4 100644 (file)
 #include "desktop-handles.h"
 #include "knotholder.h"
 #include "live_effects/parameter/pointparam-knotholder.h"
-#include "node-context.h"
+#include "nodepath.h"
 #include "xml/node-event-vector.h"
-#include "prefs-utils.h"
+#include "preferences.h"
 #include "object-edit.h"
-#include "splivarot.h"
 #include "style.h"
+#include "display/curve.h"
+#include <2geom/pathvector.h>
 
 #include "shape-editor.h"
 
@@ -52,6 +53,7 @@ ShapeEditor::ShapeEditor(SPDesktop *dt) {
     this->nodepath = NULL;
     this->knotholder = NULL;
     this->hit = false;
+    this->listener_attached_for = NULL;
 }
 
 ShapeEditor::~ShapeEditor() {
@@ -66,8 +68,11 @@ void ShapeEditor::unset_item(SubType type, bool keep_knotholder) {
         case SH_NODEPATH:
             if (this->nodepath) {
                 old_repr = this->nodepath->repr;
-                sp_repr_remove_listener_by_data(old_repr, this);
-                Inkscape::GC::release(old_repr);
+                if (old_repr && old_repr == listener_attached_for) {
+                    sp_repr_remove_listener_by_data(old_repr, this);
+                    Inkscape::GC::release(old_repr);
+                    listener_attached_for = NULL;
+                }
 
                 this->grab_node = -1;
                 sp_nodepath_destroy(this->nodepath);
@@ -77,8 +82,11 @@ void ShapeEditor::unset_item(SubType type, bool keep_knotholder) {
         case SH_KNOTHOLDER:
             if (this->knotholder) {
                 old_repr = this->knotholder->repr;
-                sp_repr_remove_listener_by_data(old_repr, this);
-                Inkscape::GC::release(old_repr);
+                if (old_repr && old_repr == listener_attached_for) {
+                    sp_repr_remove_listener_by_data(old_repr, this);
+                    Inkscape::GC::release(old_repr);
+                    listener_attached_for = NULL;
+                }
 
                 if (!keep_knotholder) {
                     delete this->knotholder;
@@ -130,8 +138,8 @@ void ShapeEditor::decrement_local_change (SubType type) {
     }
 }
 
-SPItem *ShapeEditor::get_item (SubType type) {
-    SPItem *item = NULL;
+const SPItem *ShapeEditor::get_item (SubType type) {
+    const SPItem *item = NULL;
     switch (type) {
         case SH_NODEPATH:
             if (this->has_nodepath()) {
@@ -140,7 +148,7 @@ SPItem *ShapeEditor::get_item (SubType type) {
             break;
         case SH_KNOTHOLDER:
             if (this->has_knotholder()) {
-                item = this->nodepath->item;
+                item = this->knotholder->getItem();
             }
             break;
     }
@@ -233,24 +241,19 @@ void ShapeEditor::set_item(SPItem *item, SubType type, bool keep_knotholder) {
         switch(type) {
             case SH_NODEPATH:
                 if (SP_IS_LPE_ITEM(item)) {
-                    //SPLPEItem *lpeitem = SP_LPE_ITEM(item);
-                    //Inkscape::LivePathEffect::Effect *lpe = sp_lpe_item_get_current_lpe(lpeitem);
-                    //if (!(lpe && lpe->isVisible() && lpe->providesKnotholder())) {
-                        // only create nodepath if the item either doesn't have an LPE
-                        // or the LPE is invisible or it doesn't provide a knotholder itself
-                        this->nodepath = sp_nodepath_new(desktop, item,
-                                                         (prefs_get_int_attribute("tools.nodes", "show_handles", 1) != 0));
-                    //} else if (lpe && lpe->isVisible()) {
-                    //    sp_lpe_item_add_temporary_canvasitems(lpeitem, desktop);
-                    //}
+                    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);
-                    Inkscape::GC::anchor(repr);
-                    sp_repr_add_listener(repr, &shapeeditor_repr_events, this);
+                    if (repr != listener_attached_for) {
+                        Inkscape::GC::anchor(repr);
+                        sp_repr_add_listener(repr, &shapeeditor_repr_events, this);
+                        listener_attached_for = repr;
+                    }
                 }
                 break;
 
@@ -263,8 +266,11 @@ void ShapeEditor::set_item(SPItem *item, SubType type, bool keep_knotholder) {
                     this->knotholder->update_knots();
                     // setting new listener
                     repr = this->knotholder->repr;
-                    Inkscape::GC::anchor(repr);
-                    sp_repr_add_listener(repr, &shapeeditor_repr_events, this);
+                    if (repr != listener_attached_for) {
+                        Inkscape::GC::anchor(repr);
+                        sp_repr_add_listener(repr, &shapeeditor_repr_events, this);
+                        listener_attached_for = repr;
+                    }
                 }
                 break;
         }
@@ -281,17 +287,19 @@ void ShapeEditor::set_item_lpe_path_parameter(SPItem *item, SPObject *lpeobject,
     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) {
+            if (repr && repr != listener_attached_for) {
                 Inkscape::GC::anchor(repr);
                 sp_repr_add_listener(repr, &shapeeditor_repr_events, this);
+                listener_attached_for = repr;
             }
         }
     }
@@ -325,11 +333,14 @@ void ShapeEditor::reset_item (SubType type, bool keep_knotholder)
                 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 = (SPItem *) get_item(SH_NODEPATH);
+                set_item(item, SH_NODEPATH);
+            }                
             break;
         case SH_KNOTHOLDER:
             if (this->knotholder) {
-                SPItem * item = get_item(SH_KNOTHOLDER);
+                SPItem * item = (SPItem *) get_item(SH_KNOTHOLDER);
                 set_item(item, SH_KNOTHOLDER, keep_knotholder);
             }
             break;
@@ -345,44 +356,55 @@ void ShapeEditor::update_statusbar () {
         sp_nodepath_update_statusbar(this->nodepath);
 }
 
-bool ShapeEditor::is_over_stroke (NR::Point event_p, bool remember) {
-
+bool ShapeEditor::is_over_stroke (Geom::Point event_p, bool remember) {
     if (!this->nodepath)
         return false; // no stroke in knotholder
 
-    SPItem *item = get_item(SH_NODEPATH);
+    const SPItem *item = get_item(SH_NODEPATH);
 
     //Translate click point into proper coord system
     this->curvepoint_doc = desktop->w2d(event_p);
-    this->curvepoint_doc *= from_2geom(sp_item_dt2i_affine(item));
+    this->curvepoint_doc *= sp_item_dt2i_affine(item);
 
-    sp_nodepath_ensure_livarot_path(this->nodepath);
-
-    NR::Maybe<Path::cut_position> position = get_nearest_position_on_Path(this->nodepath->livarot_path, this->curvepoint_doc);
-    if (!position) {
+    SPCurve *curve = this->nodepath->curve;   // not sure if np->curve is always up to date...
+    Geom::PathVector const &pathv = curve->get_pathvector();
+    boost::optional<Geom::PathVectorPosition> pvpos = Geom::nearestPoint(pathv, this->curvepoint_doc);
+    if (!pvpos) {
+        g_print("Warning! Possible error?\n");
         return false;
     }
 
-    NR::Point nearest = get_point_on_Path(this->nodepath->livarot_path, position->piece, position->t);
-    NR::Point delta = nearest - this->curvepoint_doc;
+    Geom::Point nearest = pathv[pvpos->path_nr].pointAt(pvpos->t);
+    Geom::Point delta = nearest - this->curvepoint_doc;
 
     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(from_2geom(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(from_2geom(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) {
-        this->curvepoint_event[NR::X] = (gint) event_p [NR::X];
-        this->curvepoint_event[NR::Y] = (gint) event_p [NR::Y];
+        // calculate index for nodepath's representation.
+        double int_part;
+        double t = std::modf(pvpos->t, &int_part);
+        unsigned int segment_index = (unsigned int)int_part + 1;
+        for (unsigned int i = 0; i < pvpos->path_nr; ++i) {
+            segment_index += pathv[i].size() + 1;
+            if (pathv[i].closed())
+                segment_index += 1;
+        }
+
+        this->curvepoint_event[Geom::X] = (gint) event_p [Geom::X];
+        this->curvepoint_event[Geom::Y] = (gint) event_p [Geom::Y];
         this->hit = true;
-        this->grab_t = position->t;
-        this->grab_node = position->piece;
+        this->grab_t = t;
+        this->grab_node = segment_index;
     }
 
     return close;
@@ -431,17 +453,17 @@ void ShapeEditor::curve_drag(gdouble eventx, gdouble eventy) {
 
 
         // The coordinates hasn't changed since the last motion event, abort
-        if (this->curvepoint_event[NR::X] == x &&
-            this->curvepoint_event[NR::Y] == y)
+        if (this->curvepoint_event[Geom::X] == x &&
+            this->curvepoint_event[Geom::Y] == y)
             return;
 
-        NR::Point const delta_w(eventx - this->curvepoint_event[NR::X],
-                                eventy - this->curvepoint_event[NR::Y]);
-        NR::Point const delta_dt(this->desktop->w2d(delta_w));
+        Geom::Point const delta_w(eventx - this->curvepoint_event[Geom::X],
+                                  eventy - this->curvepoint_event[Geom::Y]);
+        Geom::Point const delta_dt(this->desktop->w2d(delta_w));
 
-        sp_nodepath_curve_drag (this->grab_node, this->grab_t, delta_dt); //!!! FIXME: which nodepath?!!! also uses current!!!
-        this->curvepoint_event[NR::X] = x;
-        this->curvepoint_event[NR::Y] = y;
+        sp_nodepath_curve_drag (this->nodepath, this->grab_node, this->grab_t, delta_dt);
+        this->curvepoint_event[Geom::X] = x;
+        this->curvepoint_event[Geom::Y] = y;
 
     }
     if (this->knotholder) {
@@ -456,7 +478,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);
     }
@@ -515,8 +537,8 @@ void ShapeEditor::set_type_of_segments(NRPathcode code) {
     sp_node_selected_set_line_type(this->nodepath, code);
 }
 
-void ShapeEditor::move_nodes_screen(gdouble dx, gdouble dy) {
-    sp_node_selected_move_screen(this->nodepath, dx, dy);
+void ShapeEditor::move_nodes_screen(SPDesktop *desktop, gdouble dx, gdouble dy) {
+    sp_node_selected_move_screen(desktop, this->nodepath, dx, dy);
 }
 void ShapeEditor::move_nodes(gdouble dx, gdouble dy) {
     sp_node_selected_move(this->nodepath, dx, dy);
@@ -546,7 +568,7 @@ void ShapeEditor::select_next () {
     if (this->nodepath) {
         sp_nodepath_select_next (this->nodepath);
         if (this->nodepath->numSelected() >= 1) {
-            this->desktop->scroll_to_point(&(this->nodepath->singleSelectedCoords()), 1.0);
+            this->desktop->scroll_to_point(this->nodepath->singleSelectedCoords(), 1.0);
         }
     }
 }
@@ -554,7 +576,7 @@ void ShapeEditor::select_prev () {
     if (this->nodepath) {
         sp_nodepath_select_prev (this->nodepath);
         if (this->nodepath->numSelected() >= 1) {
-            this->desktop->scroll_to_point(&(this->nodepath->singleSelectedCoords()), 1.0);
+            this->desktop->scroll_to_point(this->nodepath->singleSelectedCoords(), 1.0);
         }
     }
 }
@@ -569,16 +591,16 @@ void ShapeEditor::show_helperpath (bool show) {
         sp_nodepath_show_helperpath (this->nodepath, show);
 }
 
-void ShapeEditor::flip (NR::Dim2 axis, NR::Maybe<NR::Point> center) {
+void ShapeEditor::flip (Geom::Dim2 axis, boost::optional<Geom::Point> center) {
     if (this->nodepath)
         sp_nodepath_flip (this->nodepath, axis, center);
 }
 
-void ShapeEditor::distribute (NR::Dim2 axis) {
+void ShapeEditor::distribute (Geom::Dim2 axis) {
     if (this->nodepath)
         sp_nodepath_selected_distribute (this->nodepath, axis);
 }
-void ShapeEditor::align (NR::Dim2 axis) {
+void ShapeEditor::align (Geom::Dim2 axis) {
     if (this->nodepath)
         sp_nodepath_selected_align (this->nodepath, axis);
 }