Code

fix crash: draw two paths, select both, object - clip - set, node tool, edit clippath...
[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 "document.h"
24 #include "desktop-handles.h"
25 #include "knotholder.h"
26 #include "live_effects/parameter/point.h"
27 #include "nodepath.h"
28 #include "xml/node-event-vector.h"
29 #include "preferences.h"
30 #include "object-edit.h"
31 #include "style.h"
32 #include "display/curve.h"
33 #include <2geom/pathvector.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     this->knotholder_listener_attached_for = NULL;
58     this->nodepath_listener_attached_for = NULL;
59 }
61 ShapeEditor::~ShapeEditor() {
62     unset_item(SH_KNOTHOLDER);
63     unset_item(SH_NODEPATH);
64 }
66 void ShapeEditor::unset_item(SubType type, bool keep_knotholder) {
67     Inkscape::XML::Node *old_repr = NULL;
69     switch (type) {
70         case SH_NODEPATH:
71             if (this->nodepath) {
72                 old_repr = this->nodepath->repr;
73                 if (old_repr && old_repr == nodepath_listener_attached_for) {
74                     sp_repr_remove_listener_by_data(old_repr, this);
75                     Inkscape::GC::release(old_repr);
76                     nodepath_listener_attached_for = NULL;
77                 }
79                 this->grab_node = -1;
80                 delete this->nodepath;
81                 this->nodepath = NULL;
82             }
83             break;
84         case SH_KNOTHOLDER:
85             if (this->knotholder) {
86                 old_repr = this->knotholder->repr;
87                 if (old_repr && old_repr == knotholder_listener_attached_for) {
88                     sp_repr_remove_listener_by_data(old_repr, this);
89                     Inkscape::GC::release(old_repr);
90                     knotholder_listener_attached_for = NULL;
91                 }
93                 if (!keep_knotholder) {
94                     delete this->knotholder;
95                     this->knotholder = NULL;
96                 }
97             }
98             break;
99     }
102 bool ShapeEditor::has_nodepath () {
103     return (this->nodepath != NULL);
106 bool ShapeEditor::has_knotholder () {
107     return (this->knotholder != NULL);
110 void ShapeEditor::update_knotholder () {
111     if (this->knotholder)
112         this->knotholder->update_knots();
115 bool ShapeEditor::has_local_change (SubType type) {
116     switch (type) {
117         case SH_NODEPATH:
118             return (this->nodepath && this->nodepath->local_change);
119         case SH_KNOTHOLDER:
120             return (this->knotholder && this->knotholder->local_change != 0);
121         default:
122             g_assert_not_reached();
123     }
126 void ShapeEditor::decrement_local_change (SubType type) {
127     switch (type) {
128         case SH_NODEPATH:
129             if (this->nodepath && this->nodepath->local_change > 0) {
130                 this->nodepath->local_change--;
131             }
132             break;
133         case SH_KNOTHOLDER:
134             if (this->knotholder) {
135                 this->knotholder->local_change = FALSE;
136             }
137             break;
138         default:
139             g_assert_not_reached();
140     }
143 const SPItem *ShapeEditor::get_item (SubType type) {
144     const SPItem *item = NULL;
145     switch (type) {
146         case SH_NODEPATH:
147             if (this->has_nodepath()) {
148                 item = this->nodepath->item;
149             }
150             break;
151         case SH_KNOTHOLDER:
152             if (this->has_knotholder()) {
153                 item = this->knotholder->getItem();
154             }
155             break;
156     }
157     return item;
160 GList *ShapeEditor::save_nodepath_selection () {
161     if (this->nodepath)
162         return ::save_nodepath_selection (this->nodepath);
163     return NULL;
166 void ShapeEditor::restore_nodepath_selection (GList *saved) {
167     if (this->nodepath && saved)
168         ::restore_nodepath_selection (this->nodepath, saved);
171 bool ShapeEditor::nodepath_edits_repr_key(gchar const *name) {
172     if (nodepath && name) {
173         return ( !strcmp(name, nodepath->repr_key) || !strcmp(name, nodepath->repr_nodetypes_key) );
174     }
176     return false;
180 void ShapeEditor::shapeeditor_event_attr_changed(gchar const *name)
182     gboolean changed_np = FALSE;
183     gboolean changed_kh = FALSE;
185     if (has_nodepath() && nodepath_edits_repr_key(name))
186     {
187         changed_np = !has_local_change(SH_NODEPATH);
188         decrement_local_change(SH_NODEPATH);
189     }
191     if (changed_np) {
192         GList *saved = NULL;
193         if (has_nodepath()) {
194             saved = save_nodepath_selection();
195         }
197         reset_item(SH_NODEPATH);
199         if (has_nodepath() && saved) {
200             restore_nodepath_selection(saved);
201             g_list_free (saved);
202         }
203     }
205     if (has_knotholder())
206     {
207         changed_kh = !has_local_change(SH_KNOTHOLDER);
208         decrement_local_change(SH_KNOTHOLDER);
209         if (changed_kh) {
210             // this can happen if an LPEItem's knotholder handle was dragged, in which case we want
211             // to keep the knotholder; in all other cases (e.g., if the LPE itself changes) we delete it
212             reset_item(SH_KNOTHOLDER, !strcmp(name, "d"));
213         }
214     }
216     update_statusbar(); //TODO: get_container()->update_statusbar();
220 static void shapeeditor_event_attr_changed(Inkscape::XML::Node */*repr*/, gchar const *name,
221                                            gchar const */*old_value*/, gchar const */*new_value*/,
222                                            bool /*is_interactive*/, gpointer data)
224     g_assert(data);
225     ShapeEditor *sh = ((ShapeEditor *) data);
227     sh->shapeeditor_event_attr_changed(name);
230 static Inkscape::XML::NodeEventVector shapeeditor_repr_events = {
231     NULL, /* child_added */
232     NULL, /* child_removed */
233     shapeeditor_event_attr_changed,
234     NULL, /* content_changed */
235     NULL  /* order_changed */
236 };
239 void ShapeEditor::set_item(SPItem *item, SubType type, bool keep_knotholder) {
240     // this happens (and should only happen) when for an LPEItem having both knotholder and
241     // nodepath the knotholder is adapted; in this case we don't want to delete the knotholder
242     // since this freezes the handles
243     unset_item(type, keep_knotholder);
245     this->grab_node = -1;
247     if (item) {
248         Inkscape::XML::Node *repr;
249         switch(type) {
250             case SH_NODEPATH:
251                 if (SP_IS_LPE_ITEM(item)) {
252                     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
253                     this->nodepath = sp_nodepath_new(desktop, item, (prefs->getBool("/tools/nodes/show_handles", true)));
254                 }
255                 if (this->nodepath) {
256                     this->nodepath->shape_editor = this;
258                     // setting new listener
259                     repr = SP_OBJECT_REPR(item);
260                     if (repr != nodepath_listener_attached_for) {
261                         Inkscape::GC::anchor(repr);
262                         sp_repr_add_listener(repr, &shapeeditor_repr_events, this);
263                         nodepath_listener_attached_for = repr;
264                     }
265                 }
266                 break;
268             case SH_KNOTHOLDER:
269                 if (!this->knotholder) {
270                     // only recreate knotholder if none is present
271                     this->knotholder = sp_item_knot_holder(item, desktop);
272                 }
273                 if (this->knotholder) {
274                     this->knotholder->update_knots();
275                     // setting new listener
276                     repr = this->knotholder->repr;
277                     if (repr != knotholder_listener_attached_for) {
278                         Inkscape::GC::anchor(repr);
279                         sp_repr_add_listener(repr, &shapeeditor_repr_events, this);
280                         knotholder_listener_attached_for = repr;
281                     }
282                 }
283                 break;
284         }
285     }
288 /** Please note that this function only works for path parameters.
289 *  All other parameters probably will crash Inkscape!
290 */
291 void ShapeEditor::set_item_lpe_path_parameter(SPItem *item, LivePathEffectObject *lpeobject, const char * key)
293     unset_item(SH_NODEPATH);
295     this->grab_node = -1;
297     if (lpeobject) {
298         Inkscape::Preferences *prefs = Inkscape::Preferences::get();
299         this->nodepath = sp_nodepath_new( desktop, lpeobject,
300                                           (prefs->getInt("/tools/nodes/show_handles", true)),
301                                           key, item);
302         if (this->nodepath) {
303             this->nodepath->shape_editor = this;
305             // setting new listener
306             Inkscape::XML::Node *repr = SP_OBJECT_REPR(lpeobject);
307             if (repr && repr != nodepath_listener_attached_for) {
308                 Inkscape::GC::anchor(repr);
309                 sp_repr_add_listener(repr, &shapeeditor_repr_events, this);
310                 nodepath_listener_attached_for = repr;
311             }
312         }
313     }
317 /** FIXME: This thing is only called when the item needs to be updated in response to repr change.
318    Why not make a reload function in NodePath and in KnotHolder? */
319 void ShapeEditor::reset_item (SubType type, bool keep_knotholder)
321     switch (type) {
322         case SH_NODEPATH:
323             if ( (nodepath) && (IS_LIVEPATHEFFECT(nodepath->object)) ) {
324                 char * key = g_strdup(nodepath->repr_key);
325                 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
326                 g_free(key);
327             } else {
328                 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.
329                 set_item(SP_ITEM(obj), SH_NODEPATH);
330             }
331             break;
332         case SH_KNOTHOLDER:
333             if ( knotholder ) {
334                 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.
335                 set_item(SP_ITEM(obj), SH_KNOTHOLDER, keep_knotholder);
336             }
337             break;
338     }
341 void ShapeEditor::nodepath_destroyed () {
342     this->nodepath = NULL;
345 void ShapeEditor::update_statusbar () {
346     if (this->nodepath)
347         sp_nodepath_update_statusbar(this->nodepath);
350 bool ShapeEditor::is_over_stroke (Geom::Point event_p, bool remember) {
351     if (!this->nodepath)
352         return false; // no stroke in knotholder
354     const SPItem *item = get_item(SH_NODEPATH);
356     if (!item || !SP_IS_ITEM(item))
357         return false;
359     //Translate click point into proper coord system
360     this->curvepoint_doc = desktop->w2d(event_p);
361     this->curvepoint_doc *= sp_item_dt2i_affine(item);
363     SPCurve *curve = this->nodepath->curve;   // not sure if np->curve is always up to date...
364     Geom::PathVector const &pathv = curve->get_pathvector();
365     boost::optional<Geom::PathVectorPosition> pvpos = Geom::nearestPoint(pathv, this->curvepoint_doc);
366     if (!pvpos) {
367         g_print("Warning! Possible error?\n");
368         return false;
369     }
371     Geom::Point nearest = pathv[pvpos->path_nr].pointAt(pvpos->t);
372     Geom::Point delta = nearest - this->curvepoint_doc;
374     delta = desktop->d2w(delta);
376     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
377     double stroke_tolerance =
378         (( !SP_OBJECT_STYLE(item)->stroke.isNone() ?
379            desktop->current_zoom() *
380            SP_OBJECT_STYLE (item)->stroke_width.computed * 0.5 *
381            to_2geom(sp_item_i2d_affine(item)).descrim()
382          : 0.0)
383          + prefs->getIntLimited("/options/dragtolerance/value", 0, 0, 100)) / to_2geom(sp_item_i2d_affine(item)).descrim();
384     bool close = (Geom::L2 (delta) < stroke_tolerance);
386     if (remember && close) {
387         // calculate index for nodepath's representation.
388         double int_part;
389         double t = std::modf(pvpos->t, &int_part);
390         unsigned int segment_index = (unsigned int)int_part + 1;
391         for (unsigned int i = 0; i < pvpos->path_nr; ++i) {
392             segment_index += pathv[i].size() + 1;
393             if (pathv[i].closed())
394                 segment_index += 1;
395         }
397         this->curvepoint_event[Geom::X] = (gint) event_p [Geom::X];
398         this->curvepoint_event[Geom::Y] = (gint) event_p [Geom::Y];
399         this->hit = true;
400         this->grab_t = t;
401         this->grab_node = segment_index;
402     }
404     return close;
407 void ShapeEditor::add_node_near_point() {
408     if (this->nodepath) {
409         sp_nodepath_add_node_near_point(this->nodepath, this->curvepoint_doc);
410     } else if (this->knotholder) {
411         // we do not add nodes in knotholder... yet
412     }
415 void ShapeEditor::select_segment_near_point(bool toggle) {
416     if (this->nodepath) {
417         sp_nodepath_select_segment_near_point(this->nodepath, this->curvepoint_doc, toggle);
418     }
419     if (this->knotholder) {
420         // we do not select segments in knotholder... yet?
421     }
424 void ShapeEditor::cancel_hit() {
425     this->hit = false;
428 bool ShapeEditor::hits_curve() {
429     return (this->hit);
433 void ShapeEditor::curve_drag(gdouble eventx, gdouble eventy) {
434     if (this->nodepath && !this->nodepath->straight_path) {
436         if (this->grab_node == -1) // don't know which segment to drag
437             return;
439         // We round off the extra precision in the motion coordinates provided
440         // by some input devices (like tablets). As we'll store the coordinates
441         // as integers in curvepoint_event we need to do this rounding before
442         // comparing them with the last coordinates from curvepoint_event.
443         // See bug #1593499 for details.
445         gint x = (gint) Inkscape::round(eventx);
446         gint y = (gint) Inkscape::round(eventy);
449         // The coordinates hasn't changed since the last motion event, abort
450         if (this->curvepoint_event[Geom::X] == x &&
451             this->curvepoint_event[Geom::Y] == y)
452             return;
454         Geom::Point const delta_w(eventx - this->curvepoint_event[Geom::X],
455                                   eventy - this->curvepoint_event[Geom::Y]);
456         Geom::Point const delta_dt(this->desktop->w2d(delta_w));
458         sp_nodepath_curve_drag (this->nodepath, this->grab_node, this->grab_t, delta_dt);
459         this->curvepoint_event[Geom::X] = x;
460         this->curvepoint_event[Geom::Y] = y;
462     }
463     if (this->knotholder) {
464         // we do not drag curve in knotholder
465     }
469 void ShapeEditor::finish_drag() {
470     if (this->nodepath && this->hit) {
471         sp_nodepath_update_repr (this->nodepath, _("Drag curve"));
472     }
475 void ShapeEditor::select_rect(Geom::Rect const &rect, bool add) {
476     if (this->nodepath) {
477         sp_nodepath_select_rect(this->nodepath, rect, add);
478     }
481 bool ShapeEditor::has_selection() {
482     if (this->nodepath)
483         return this->nodepath->selected;
484     return false; //  so far, knotholder cannot have selection
487 void ShapeEditor::deselect() {
488     if (this->nodepath)
489         sp_nodepath_deselect(this->nodepath);
492 void ShapeEditor::add_node () {
493     sp_node_selected_add_node(this->nodepath);
496 void ShapeEditor::delete_nodes () {
497     sp_node_selected_delete(this->nodepath);
500 void ShapeEditor::delete_nodes_preserving_shape () {
501     if (this->nodepath && this->nodepath->selected) {
502         sp_node_delete_preserve(g_list_copy(this->nodepath->selected));
503     }
506 void ShapeEditor::delete_segment () {
507     sp_node_selected_delete_segment(this->nodepath);
510 void ShapeEditor::set_node_type(int type) {
511     sp_node_selected_set_type(this->nodepath, (Inkscape::NodePath::NodeType) type);
514 void ShapeEditor::break_at_nodes() {
515     sp_node_selected_break(this->nodepath);
518 void ShapeEditor::join_nodes() {
519     sp_node_selected_join(this->nodepath);
522 void ShapeEditor::join_segments() {
523     sp_node_selected_join_segment(this->nodepath);
526 void ShapeEditor::duplicate_nodes() {
527     sp_node_selected_duplicate(this->nodepath);
530 void ShapeEditor::set_type_of_segments(NRPathcode code) {
531     sp_node_selected_set_line_type(this->nodepath, code);
534 void ShapeEditor::move_nodes_screen(SPDesktop *desktop, gdouble dx, gdouble dy) {
535     sp_node_selected_move_screen(desktop, this->nodepath, dx, dy);
537 void ShapeEditor::move_nodes(gdouble dx, gdouble dy) {
538     sp_node_selected_move(this->nodepath, dx, dy);
541 void ShapeEditor::rotate_nodes(gdouble angle, int which, bool screen) {
542     if (this->nodepath)
543         sp_nodepath_selected_nodes_rotate (this->nodepath, angle, which, screen);
546 void ShapeEditor::scale_nodes(gdouble const grow, int const which) {
547     sp_nodepath_selected_nodes_scale(this->nodepath, grow, which);
549 void ShapeEditor::scale_nodes_screen(gdouble const grow, int const which) {
550     sp_nodepath_selected_nodes_scale_screen(this->nodepath, grow, which);
553 void ShapeEditor::select_all (bool invert) {
554     if (this->nodepath)
555         sp_nodepath_select_all (this->nodepath, invert);
557 void ShapeEditor::select_all_from_subpath (bool invert) {
558     if (this->nodepath)
559         sp_nodepath_select_all_from_subpath (this->nodepath, invert);
561 void ShapeEditor::select_next () {
562     if (this->nodepath) {
563         sp_nodepath_select_next (this->nodepath);
564         if (this->nodepath->numSelected() >= 1) {
565             this->desktop->scroll_to_point(this->nodepath->singleSelectedCoords(), 1.0);
566         }
567     }
569 void ShapeEditor::select_prev () {
570     if (this->nodepath) {
571         sp_nodepath_select_prev (this->nodepath);
572         if (this->nodepath->numSelected() >= 1) {
573             this->desktop->scroll_to_point(this->nodepath->singleSelectedCoords(), 1.0);
574         }
575     }
578 void ShapeEditor::show_handles (bool show) {
579     if (this->nodepath && !this->nodepath->straight_path)
580         sp_nodepath_show_handles (this->nodepath, show);
583 void ShapeEditor::show_helperpath (bool show) {
584     if (this->nodepath)
585         sp_nodepath_show_helperpath (this->nodepath, show);
588 void ShapeEditor::flip (Geom::Dim2 axis, boost::optional<Geom::Point> center) {
589     if (this->nodepath)
590         sp_nodepath_flip (this->nodepath, axis, center);
593 void ShapeEditor::distribute (Geom::Dim2 axis) {
594     if (this->nodepath)
595         sp_nodepath_selected_distribute (this->nodepath, axis);
597 void ShapeEditor::align (Geom::Dim2 axis) {
598     if (this->nodepath)
599         sp_nodepath_selected_align (this->nodepath, axis);
603 /*
604   Local Variables:
605   mode:c++
606   c-file-style:"stroustrup"
607   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
608   indent-tabs-mode:nil
609   fill-column:99
610   End:
611 */
612 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :