Code

Improved Color emboss now takes a better account of Blend settings
[inkscape.git] / src / shape-editor.cpp
index a7dc872b62e036257fab0a8232d9d107f2fbd7df..1b81e1a83acbefdda643ae52a4ad912ffea62500 100644 (file)
 #include "desktop.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"
 #include <2geom/pathvector.h>
-#include "sp-shape.h"
 
 #include "shape-editor.h"
 
@@ -54,6 +53,7 @@ ShapeEditor::ShapeEditor(SPDesktop *dt) {
     this->nodepath = NULL;
     this->knotholder = NULL;
     this->hit = false;
+    this->listener_attached_for = NULL;
 }
 
 ShapeEditor::~ShapeEditor() {
@@ -62,26 +62,31 @@ ShapeEditor::~ShapeEditor() {
 }
 
 void ShapeEditor::unset_item(SubType type, bool keep_knotholder) {
-    g_print ("ShapeEditor::unset_item()\n");
     Inkscape::XML::Node *old_repr = NULL;
 
     switch (type) {
         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);
+                delete this->nodepath;
                 this->nodepath = NULL;
             }
             break;
         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;
@@ -169,51 +174,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)
 {
-    g_print ("shapeeditor_event_attr_changed()\n");
     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 = {
@@ -226,9 +235,9 @@ static Inkscape::XML::NodeEventVector shapeeditor_repr_events = {
 
 
 void ShapeEditor::set_item(SPItem *item, SubType type, bool keep_knotholder) {
-    g_print ("ShapeEditor::set_item()\n");
-    // 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;
@@ -238,15 +247,19 @@ 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);
-                    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;
 
@@ -259,8 +272,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;
         }
@@ -272,49 +288,34 @@ void ShapeEditor::set_item(SPItem *item, SubType type, bool keep_knotholder) {
 */
 void ShapeEditor::set_item_lpe_path_parameter(SPItem *item, SPObject *lpeobject, const char * key)
 {
-    g_print ("ShapeEditor::set_item_lpe_path_parameter()\n");
     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) {
+            if (repr && repr != listener_attached_for) {
                 Inkscape::GC::anchor(repr);
                 sp_repr_add_listener(repr, &shapeeditor_repr_events, this);
+                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)
 {
-    g_print ("ShapeEditor::reset_item()\n");
     switch (type) {
         case SH_NODEPATH:
             if ( (this->nodepath) && (IS_LIVEPATHEFFECT(this->nodepath->object)) ) {
@@ -346,8 +347,7 @@ void ShapeEditor::update_statusbar () {
         sp_nodepath_update_statusbar(this->nodepath);
 }
 
-bool ShapeEditor::is_over_stroke (NR::Point event_p, bool remember) {
-    g_print ("ShapeEditor::is_over_stroke ()");
+bool ShapeEditor::is_over_stroke (Geom::Point event_p, bool remember) {
     if (!this->nodepath)
         return false; // no stroke in knotholder
 
@@ -357,44 +357,42 @@ bool ShapeEditor::is_over_stroke (NR::Point event_p, bool remember) {
     this->curvepoint_doc = desktop->w2d(event_p);
     this->curvepoint_doc *= sp_item_dt2i_affine(item);
 
-    SPCurve *curve;
-    if (SP_IS_SHAPE(item)) {
-        g_print ("; getting curve from shape\n");
-        curve = sp_shape_get_curve(SP_SHAPE(item));
-    } else {
-        g_print ("; getting curve from nodepath's curve\n");
-        this->nodepath->curve;   // not sure if np->curve is always up to date...
-    }
+    SPCurve *curve = this->nodepath->curve;   // not sure if np->curve is always up to date...
     Geom::PathVector const &pathv = curve->get_pathvector();
-    Geom::PathVectorPosition pvpos = Geom::nearestPoint(pathv, this->curvepoint_doc);
+    boost::optional<Geom::PathVectorPosition> pvpos = Geom::nearestPoint(pathv, this->curvepoint_doc);
+    if (!pvpos) {
+        g_print("Warning! Possible error?\n");
+        return false;
+    }
 
-    NR::Point nearest = pathv[pvpos.path_nr].pointAt(pvpos.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(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.
         double int_part;
-        double t = std::modf(pvpos.t, &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) {
+        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[NR::X] = (gint) event_p [NR::X];
-        this->curvepoint_event[NR::Y] = (gint) event_p [NR::Y];
+        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 = t;
         this->grab_node = segment_index;
@@ -446,17 +444,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->nodepath, this->grab_node, this->grab_t, delta_dt);
-        this->curvepoint_event[NR::X] = x;
-        this->curvepoint_event[NR::Y] = y;
+        this->curvepoint_event[Geom::X] = x;
+        this->curvepoint_event[Geom::Y] = y;
 
     }
     if (this->knotholder) {
@@ -471,7 +469,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);
     }
@@ -561,7 +559,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);
         }
     }
 }
@@ -569,7 +567,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);
         }
     }
 }
@@ -584,16 +582,16 @@ void ShapeEditor::show_helperpath (bool show) {
         sp_nodepath_show_helperpath (this->nodepath, show);
 }
 
-void ShapeEditor::flip (NR::Dim2 axis, boost::optional<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);
 }