Code

fix crash: separate np and kh listeners so that np listener is not fired twice when...
[inkscape.git] / src / shape-editor.cpp
1 #define __SHAPE_EDITOR_CPP__
3 /*
4  * Inkscape::ShapeEditor
5  *
6  * Authors:
7  *   bulia byak <buliabyak@users.sf.net>
8  *
9  */
11 #ifdef HAVE_CONFIG_H
12 #include "config.h"
13 #endif
15 #include <string.h>
16 #include <glibmm/i18n.h>
18 #include "sp-object.h"
19 #include "sp-item.h"
20 #include "live_effects/lpeobject.h"
21 #include "selection.h"
22 #include "desktop.h"
23 #include "desktop-handles.h"
24 #include "knotholder.h"
25 #include "live_effects/parameter/pointparam-knotholder.h"
26 #include "nodepath.h"
27 #include "xml/node-event-vector.h"
28 #include "prefs-utils.h"
29 #include "object-edit.h"
30 #include "style.h"
31 #include "display/curve.h"
32 #include <2geom/pathvector.h>
33 #include "sp-shape.h"
35 #include "shape-editor.h"
38 ShapeEditorsCollective::ShapeEditorsCollective(SPDesktop */*dt*/) {
39 }
41 ShapeEditorsCollective::~ShapeEditorsCollective() {
42 }
45 void ShapeEditorsCollective::update_statusbar() {
47 //!!! move from nodepath: sp_nodepath_update_statusbar but summing for all nodepaths
49 }
51 ShapeEditor::ShapeEditor(SPDesktop *dt) {
52     this->desktop = dt;
53     this->grab_node = -1;
54     this->nodepath = NULL;
55     this->knotholder = NULL;
56     this->hit = false;
57 }
59 ShapeEditor::~ShapeEditor() {
60     unset_item(SH_KNOTHOLDER);
61     unset_item(SH_NODEPATH);
62 }
64 void ShapeEditor::unset_item(SubType type, bool keep_knotholder) {
65     Inkscape::XML::Node *old_repr = NULL;
67     switch (type) {
68         case SH_NODEPATH:
69             if (this->nodepath) {
70                 old_repr = this->nodepath->repr;
71                 sp_repr_remove_listener_by_data(old_repr, this);
72                 Inkscape::GC::release(old_repr);
74                 this->grab_node = -1;
75                 sp_nodepath_destroy(this->nodepath);
76                 this->nodepath = NULL;
77             }
78             break;
79         case SH_KNOTHOLDER:
80             if (this->knotholder) {
81                 old_repr = this->knotholder->repr;
82                 sp_repr_remove_listener_by_data(old_repr, this);
83                 Inkscape::GC::release(old_repr);
85                 if (!keep_knotholder) {
86                     delete this->knotholder;
87                     this->knotholder = NULL;
88                 }
89             }
90             break;
91     }
92 }
94 bool ShapeEditor::has_nodepath () {
95     return (this->nodepath != NULL);
96 }
98 bool ShapeEditor::has_knotholder () {
99     return (this->knotholder != NULL);
102 void ShapeEditor::update_knotholder () {
103     if (this->knotholder)
104         this->knotholder->update_knots();
107 bool ShapeEditor::has_local_change (SubType type) {
108     switch (type) {
109         case SH_NODEPATH:
110             return (this->nodepath && this->nodepath->local_change);
111         case SH_KNOTHOLDER:
112             return (this->knotholder && this->knotholder->local_change != 0);
113         default:
114             g_assert_not_reached();
115     }
118 void ShapeEditor::decrement_local_change (SubType type) {
119     switch (type) {
120         case SH_NODEPATH:
121             if (this->nodepath && this->nodepath->local_change > 0) {
122                 this->nodepath->local_change--;
123             }
124             break;
125         case SH_KNOTHOLDER:
126             if (this->knotholder) {
127                 this->knotholder->local_change = FALSE;
128             }
129             break;
130         default:
131             g_assert_not_reached();
132     }
135 const SPItem *ShapeEditor::get_item (SubType type) {
136     const SPItem *item = NULL;
137     switch (type) {
138         case SH_NODEPATH:
139             if (this->has_nodepath()) {
140                 item = this->nodepath->item;
141             }
142             break;
143         case SH_KNOTHOLDER:
144             if (this->has_knotholder()) {
145                 item = this->knotholder->getItem();
146             }
147             break;
148     }
149     return item;
152 GList *ShapeEditor::save_nodepath_selection () {
153     if (this->nodepath)
154         return ::save_nodepath_selection (this->nodepath);
155     return NULL;
158 void ShapeEditor::restore_nodepath_selection (GList *saved) {
159     if (this->nodepath && saved)
160         ::restore_nodepath_selection (this->nodepath, saved);
163 bool ShapeEditor::nodepath_edits_repr_key(gchar const *name) {
164     if (nodepath && name) {
165         return ( !strcmp(name, nodepath->repr_key) || !strcmp(name, nodepath->repr_nodetypes_key) );
166     }
168     return false;
171 static void shapeeditor_np_event_attr_changed(Inkscape::XML::Node */*repr*/, gchar const *name,
172                                            gchar const */*old_value*/, gchar const */*new_value*/,
173                                            bool /*is_interactive*/, gpointer data)
175     gboolean changed_np = FALSE;
177     g_assert(data);
178     ShapeEditor *sh = ((ShapeEditor *) data);
180     if (sh->has_nodepath() && sh->nodepath_edits_repr_key(name))
181     {
182         changed_np = !sh->has_local_change(SH_NODEPATH);
183         sh->decrement_local_change(SH_NODEPATH);
184     }
186     if (changed_np) {
187         GList *saved = NULL;
188         if (sh->has_nodepath()) {
189             saved = sh->save_nodepath_selection();
190         }
192         sh->reset_item(SH_NODEPATH);
194         if (sh->has_nodepath() && saved) {
195             sh->restore_nodepath_selection(saved);
196             g_list_free (saved);
197         }
198     }
200     sh->update_statusbar(); //TODO: sh->get_container()->update_statusbar();
203 static void shapeeditor_kh_event_attr_changed(Inkscape::XML::Node */*repr*/, gchar const *name,
204                                            gchar const */*old_value*/, gchar const */*new_value*/,
205                                            bool /*is_interactive*/, gpointer data)
207     gboolean changed_kh = FALSE;
209     g_assert(data);
210     ShapeEditor *sh = ((ShapeEditor *) data);
212     if (sh->has_knotholder())
213     {
214         changed_kh = !sh->has_local_change(SH_KNOTHOLDER);
215         sh->decrement_local_change(SH_KNOTHOLDER);
216         if (changed_kh) {
217             // this can happen if an LPEItem's knotholder handle was dragged, in which case we want
218             // to keep the knotholder; in all other cases (e.g., if the LPE itself changes) we delete it
219             sh->reset_item(SH_KNOTHOLDER, !strcmp(name, "d"));
220         }
221     }
223     sh->update_statusbar(); //TODO: sh->get_container()->update_statusbar();
228 static Inkscape::XML::NodeEventVector shapeeditor_np_repr_events = {
229     NULL, /* child_added */
230     NULL, /* child_removed */
231     shapeeditor_np_event_attr_changed,
232     NULL, /* content_changed */
233     NULL  /* order_changed */
234 };
236 static Inkscape::XML::NodeEventVector shapeeditor_kh_repr_events = {
237     NULL, /* child_added */
238     NULL, /* child_removed */
239     shapeeditor_kh_event_attr_changed,
240     NULL, /* content_changed */
241     NULL  /* order_changed */
242 };
245 void ShapeEditor::set_item(SPItem *item, SubType type, bool keep_knotholder) {
246     // this happens (and should only happen) when for an LPEItem having both knotholder and nodepath the knotholder
247     // is adapted; in this case we don't want to delete the knotholder since this freezes the handles
248     unset_item(type, keep_knotholder);
250     this->grab_node = -1;
252     if (item) {
253         Inkscape::XML::Node *repr;
254         switch(type) {
255             case SH_NODEPATH:
256                 if (SP_IS_LPE_ITEM(item)) {
257                     this->nodepath = sp_nodepath_new(desktop, item, (prefs_get_int_attribute("tools.nodes", "show_handles", 1) != 0));
258                 }
259                 if (this->nodepath) {
260                     this->nodepath->shape_editor = this;
262                     // setting new listener
263                     repr = SP_OBJECT_REPR(item);
264                     Inkscape::GC::anchor(repr);
265                     sp_repr_add_listener(repr, &shapeeditor_np_repr_events, this);
266                 }
267                 break;
269             case SH_KNOTHOLDER:
270                 if (!this->knotholder) {
271                     // only recreate knotholder if none is present
272                     this->knotholder = sp_item_knot_holder(item, desktop);
273                 }
274                 if (this->knotholder) {
275                     this->knotholder->update_knots();
276                     // setting new listener
277                     repr = this->knotholder->repr;
278                     Inkscape::GC::anchor(repr);
279                     sp_repr_add_listener(repr, &shapeeditor_kh_repr_events, this);
280                 }
281                 break;
282         }
283     }
286 /** Please note that this function only works for path parameters.
287 *  All other parameters probably will crash Inkscape!
288 */
289 void ShapeEditor::set_item_lpe_path_parameter(SPItem *item, SPObject *lpeobject, const char * key)
291     unset_item(SH_NODEPATH);
293     this->grab_node = -1;
295     if (lpeobject) {
296         this->nodepath = sp_nodepath_new( desktop, lpeobject,
297                                           (prefs_get_int_attribute("tools.nodes", "show_handles", 1) != 0),
298                                           key, item);
299         if (this->nodepath) {
300             this->nodepath->shape_editor = this;
302             // setting new listener
303             Inkscape::XML::Node *repr = SP_OBJECT_REPR(lpeobject);
304             if (repr) {
305                 Inkscape::GC::anchor(repr);
306                 sp_repr_add_listener(repr, &shapeeditor_np_repr_events, this);
307             }
308         }
309     }
312 /** 
313 *  pass a new knotholder to ShapeEditor to manage (and delete)
314 */
315 void
316 ShapeEditor::set_knotholder(KnotHolder * knot_holder)
318     unset_item(SH_KNOTHOLDER);
320     this->grab_node = -1;
322     if (knot_holder) {
323         this->knotholder = knot_holder;
324     }
328 /** FIXME: think about this. Is this thing only called when the item needs to be updated?
329    Why not make a reload function in NodePath and in KnotHolder? */
330 void ShapeEditor::reset_item (SubType type, bool keep_knotholder)
332     switch (type) {
333         case SH_NODEPATH:
334             if ( (this->nodepath) && (IS_LIVEPATHEFFECT(this->nodepath->object)) ) {
335                 SPItem * item = this->nodepath->item;
336                 SPObject *obj = this->nodepath->object;
337                 char * key = g_strdup(this->nodepath->repr_key);
338                 set_item_lpe_path_parameter(item, obj, key); // the above checks for nodepath, so it is indeed a path that we are editing
339                 g_free(key);
340             } else {
341                 SPItem * item = (SPItem *) get_item(SH_NODEPATH);
342                 set_item(item, SH_NODEPATH);
343             }                
344             break;
345         case SH_KNOTHOLDER:
346             if (this->knotholder) {
347                 SPItem * item = (SPItem *) get_item(SH_KNOTHOLDER);
348                 set_item(item, SH_KNOTHOLDER, keep_knotholder);
349             }
350             break;
351     }
354 void ShapeEditor::nodepath_destroyed () {
355     this->nodepath = NULL;
358 void ShapeEditor::update_statusbar () {
359     if (this->nodepath)
360         sp_nodepath_update_statusbar(this->nodepath);
363 bool ShapeEditor::is_over_stroke (NR::Point event_p, bool remember) {
364     if (!this->nodepath)
365         return false; // no stroke in knotholder
367     const SPItem *item = get_item(SH_NODEPATH);
369     //Translate click point into proper coord system
370     this->curvepoint_doc = desktop->w2d(event_p);
371     this->curvepoint_doc *= sp_item_dt2i_affine(item);
373     SPCurve *curve;
374     if (SP_IS_SHAPE(item)) {
375         curve = sp_shape_get_curve(SP_SHAPE(item));
376     } else {
377         curve = this->nodepath->curve;   // not sure if np->curve is always up to date...
378     }
379     Geom::PathVector const &pathv = curve->get_pathvector();
380     Geom::PathVectorPosition pvpos = Geom::nearestPoint(pathv, this->curvepoint_doc);
382     NR::Point nearest = pathv[pvpos.path_nr].pointAt(pvpos.t);
383     NR::Point delta = nearest - this->curvepoint_doc;
385     delta = desktop->d2w(delta);
387     double stroke_tolerance =
388         (( !SP_OBJECT_STYLE(item)->stroke.isNone() ?
389            desktop->current_zoom() *
390            SP_OBJECT_STYLE (item)->stroke_width.computed * 0.5 *
391            NR::expansion(sp_item_i2d_affine(item))
392          : 0.0)
393          + prefs_get_int_attribute_limited("options.dragtolerance", "value", 0, 0, 100)) /NR::expansion(sp_item_i2d_affine(item)); 
394     bool close = (NR::L2 (delta) < stroke_tolerance);
396     if (remember && close) {
397         // calculate index for nodepath's representation.
398         double int_part;
399         double t = std::modf(pvpos.t, &int_part);
400         unsigned int segment_index = (unsigned int)int_part + 1;
401         for (unsigned int i = 0; i < pvpos.path_nr; ++i) {
402             segment_index += pathv[i].size() + 1;
403             if (pathv[i].closed())
404                 segment_index += 1;
405         }
407         this->curvepoint_event[NR::X] = (gint) event_p [NR::X];
408         this->curvepoint_event[NR::Y] = (gint) event_p [NR::Y];
409         this->hit = true;
410         this->grab_t = t;
411         this->grab_node = segment_index;
412     }
414     return close;
417 void ShapeEditor::add_node_near_point() {
418     if (this->nodepath) {
419         sp_nodepath_add_node_near_point(this->nodepath, this->curvepoint_doc);
420     } else if (this->knotholder) {
421         // we do not add nodes in knotholder... yet
422     }
425 void ShapeEditor::select_segment_near_point(bool toggle) {
426     if (this->nodepath) {
427         sp_nodepath_select_segment_near_point(this->nodepath, this->curvepoint_doc, toggle);
428     }
429     if (this->knotholder) {
430         // we do not select segments in knotholder... yet?
431     }
434 void ShapeEditor::cancel_hit() {
435     this->hit = false;
438 bool ShapeEditor::hits_curve() {
439     return (this->hit);
443 void ShapeEditor::curve_drag(gdouble eventx, gdouble eventy) {
444     if (this->nodepath && !this->nodepath->straight_path) {
446         if (this->grab_node == -1) // don't know which segment to drag
447             return;
449         // We round off the extra precision in the motion coordinates provided
450         // by some input devices (like tablets). As we'll store the coordinates
451         // as integers in curvepoint_event we need to do this rounding before
452         // comparing them with the last coordinates from curvepoint_event.
453         // See bug #1593499 for details.
455         gint x = (gint) Inkscape::round(eventx);
456         gint y = (gint) Inkscape::round(eventy);
459         // The coordinates hasn't changed since the last motion event, abort
460         if (this->curvepoint_event[NR::X] == x &&
461             this->curvepoint_event[NR::Y] == y)
462             return;
464         NR::Point const delta_w(eventx - this->curvepoint_event[NR::X],
465                                 eventy - this->curvepoint_event[NR::Y]);
466         NR::Point const delta_dt(this->desktop->w2d(delta_w));
468         sp_nodepath_curve_drag (this->nodepath, this->grab_node, this->grab_t, delta_dt);
469         this->curvepoint_event[NR::X] = x;
470         this->curvepoint_event[NR::Y] = y;
472     }
473     if (this->knotholder) {
474         // we do not drag curve in knotholder
475     }
479 void ShapeEditor::finish_drag() {
480     if (this->nodepath && this->hit) {
481         sp_nodepath_update_repr (this->nodepath, _("Drag curve"));
482     }
485 void ShapeEditor::select_rect(NR::Rect const &rect, bool add) {
486     if (this->nodepath) {
487         sp_nodepath_select_rect(this->nodepath, rect, add);
488     }
491 bool ShapeEditor::has_selection() {
492     if (this->nodepath)
493         return this->nodepath->selected;
494     return false; //  so far, knotholder cannot have selection
497 void ShapeEditor::deselect() {
498     if (this->nodepath)
499         sp_nodepath_deselect(this->nodepath);
502 void ShapeEditor::add_node () {
503     sp_node_selected_add_node(this->nodepath);
506 void ShapeEditor::delete_nodes () {
507     sp_node_selected_delete(this->nodepath);
510 void ShapeEditor::delete_nodes_preserving_shape () {
511     if (this->nodepath && this->nodepath->selected) {
512         sp_node_delete_preserve(g_list_copy(this->nodepath->selected));
513     }
516 void ShapeEditor::delete_segment () {
517     sp_node_selected_delete_segment(this->nodepath);
520 void ShapeEditor::set_node_type(int type) {
521     sp_node_selected_set_type(this->nodepath, (Inkscape::NodePath::NodeType) type);
524 void ShapeEditor::break_at_nodes() {
525     sp_node_selected_break(this->nodepath);
528 void ShapeEditor::join_nodes() {
529     sp_node_selected_join(this->nodepath);
532 void ShapeEditor::join_segments() {
533     sp_node_selected_join_segment(this->nodepath);
536 void ShapeEditor::duplicate_nodes() {
537     sp_node_selected_duplicate(this->nodepath);
540 void ShapeEditor::set_type_of_segments(NRPathcode code) {
541     sp_node_selected_set_line_type(this->nodepath, code);
544 void ShapeEditor::move_nodes_screen(SPDesktop *desktop, gdouble dx, gdouble dy) {
545     sp_node_selected_move_screen(desktop, this->nodepath, dx, dy);
547 void ShapeEditor::move_nodes(gdouble dx, gdouble dy) {
548     sp_node_selected_move(this->nodepath, dx, dy);
551 void ShapeEditor::rotate_nodes(gdouble angle, int which, bool screen) {
552     if (this->nodepath)
553         sp_nodepath_selected_nodes_rotate (this->nodepath, angle, which, screen);
556 void ShapeEditor::scale_nodes(gdouble const grow, int const which) {
557     sp_nodepath_selected_nodes_scale(this->nodepath, grow, which);
559 void ShapeEditor::scale_nodes_screen(gdouble const grow, int const which) {
560     sp_nodepath_selected_nodes_scale_screen(this->nodepath, grow, which);
563 void ShapeEditor::select_all (bool invert) {
564     if (this->nodepath)
565         sp_nodepath_select_all (this->nodepath, invert);
567 void ShapeEditor::select_all_from_subpath (bool invert) {
568     if (this->nodepath)
569         sp_nodepath_select_all_from_subpath (this->nodepath, invert);
571 void ShapeEditor::select_next () {
572     if (this->nodepath) {
573         sp_nodepath_select_next (this->nodepath);
574         if (this->nodepath->numSelected() >= 1) {
575             this->desktop->scroll_to_point(&(this->nodepath->singleSelectedCoords()), 1.0);
576         }
577     }
579 void ShapeEditor::select_prev () {
580     if (this->nodepath) {
581         sp_nodepath_select_prev (this->nodepath);
582         if (this->nodepath->numSelected() >= 1) {
583             this->desktop->scroll_to_point(&(this->nodepath->singleSelectedCoords()), 1.0);
584         }
585     }
588 void ShapeEditor::show_handles (bool show) {
589     if (this->nodepath && !this->nodepath->straight_path)
590         sp_nodepath_show_handles (this->nodepath, show);
593 void ShapeEditor::show_helperpath (bool show) {
594     if (this->nodepath)
595         sp_nodepath_show_helperpath (this->nodepath, show);
598 void ShapeEditor::flip (NR::Dim2 axis, boost::optional<NR::Point> center) {
599     if (this->nodepath)
600         sp_nodepath_flip (this->nodepath, axis, center);
603 void ShapeEditor::distribute (NR::Dim2 axis) {
604     if (this->nodepath)
605         sp_nodepath_selected_distribute (this->nodepath, axis);
607 void ShapeEditor::align (NR::Dim2 axis) {
608     if (this->nodepath)
609         sp_nodepath_selected_align (this->nodepath, axis);
613 /*
614   Local Variables:
615   mode:c++
616   c-file-style:"stroustrup"
617   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
618   indent-tabs-mode:nil
619   fill-column:99
620   End:
621 */
622 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :