Code

Enable simultaneous knotholder and nodepath
[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 "node-context.h"
27 #include "xml/node-event-vector.h"
28 #include "prefs-utils.h"
29 #include "object-edit.h"
30 #include "splivarot.h"
31 #include "style.h"
33 #include "shape-editor.h"
36 ShapeEditorsCollective::ShapeEditorsCollective(SPDesktop */*dt*/) {
37 }
39 ShapeEditorsCollective::~ShapeEditorsCollective() {
40 }
43 void ShapeEditorsCollective::update_statusbar() {
45 //!!! move from nodepath: sp_nodepath_update_statusbar but summing for all nodepaths
47 }
49 ShapeEditor::ShapeEditor(SPDesktop *dt) {
50     this->desktop = dt;
51     this->grab_node = -1;
52     this->nodepath = NULL;
53     this->knotholder = NULL;
54     this->hit = false;
55 }
57 ShapeEditor::~ShapeEditor() {
58     unset_item(SH_KNOTHOLDER);
59     unset_item(SH_NODEPATH);
60 }
62 void ShapeEditor::unset_item(SubType type, bool keep_knotholder) {
63     Inkscape::XML::Node *old_repr = NULL;
65     switch (type) {
66         case SH_NODEPATH:
67             if (this->nodepath) {
68                 old_repr = this->nodepath->repr;
69                 sp_repr_remove_listener_by_data(old_repr, this);
70                 Inkscape::GC::release(old_repr);
72                 this->grab_node = -1;
73                 sp_nodepath_destroy(this->nodepath);
74                 this->nodepath = NULL;
75             }
76             break;
77         case SH_KNOTHOLDER:
78             if (this->knotholder) {
79                 old_repr = this->knotholder->repr;
80                 sp_repr_remove_listener_by_data(old_repr, this);
81                 Inkscape::GC::release(old_repr);
83                 if (!keep_knotholder) {
84                     delete this->knotholder;
85                     this->knotholder = NULL;
86                 }
87             }
88             break;
89     }
90 }
92 bool ShapeEditor::has_nodepath () {
93     return (this->nodepath != NULL);
94 }
96 bool ShapeEditor::has_knotholder () {
97     return (this->knotholder != NULL);
98 }
100 void ShapeEditor::update_knotholder () {
101     if (this->knotholder)
102         this->knotholder->update_knots();
105 bool ShapeEditor::has_local_change (SubType type) {
106     switch (type) {
107         case SH_NODEPATH:
108             return (this->nodepath && this->nodepath->local_change);
109         case SH_KNOTHOLDER:
110             return (this->knotholder && this->knotholder->local_change != 0);
111         default:
112             g_assert_not_reached();
113     }
116 void ShapeEditor::decrement_local_change (SubType type) {
117     switch (type) {
118         case SH_NODEPATH:
119             if (this->nodepath && this->nodepath->local_change > 0) {
120                 this->nodepath->local_change--;
121             }
122             break;
123         case SH_KNOTHOLDER:
124             if (this->knotholder) {
125                 this->knotholder->local_change = FALSE;
126             }
127             break;
128         default:
129             g_assert_not_reached();
130     }
133 SPItem *ShapeEditor::get_item (SubType type) {
134     SPItem *item = NULL;
135     switch (type) {
136         case SH_NODEPATH:
137             if (this->has_nodepath()) {
138                 item = this->nodepath->item;
139             }
140             break;
141         case SH_KNOTHOLDER:
142             if (this->has_knotholder()) {
143                 item = this->nodepath->item;
144             }
145             break;
146     }
147     return item;
150 GList *ShapeEditor::save_nodepath_selection () {
151     if (this->nodepath)
152         return ::save_nodepath_selection (this->nodepath);
153     return NULL;
156 void ShapeEditor::restore_nodepath_selection (GList *saved) {
157     if (this->nodepath && saved)
158         ::restore_nodepath_selection (this->nodepath, saved);
161 bool ShapeEditor::nodepath_edits_repr_key(gchar const *name) {
162     if (nodepath && name) {
163         return ( !strcmp(name, nodepath->repr_key) || !strcmp(name, nodepath->repr_nodetypes_key) );
164     }
166     return false;
169 static void shapeeditor_event_attr_changed(Inkscape::XML::Node */*repr*/, gchar const *name,
170                                            gchar const */*old_value*/, gchar const */*new_value*/,
171                                            bool /*is_interactive*/, gpointer data)
173     gboolean changed_np = FALSE;
174     gboolean changed_kh = FALSE;
176     g_assert(data);
177     ShapeEditor *sh = ((ShapeEditor *) data);
179     if (sh->has_nodepath() && sh->nodepath_edits_repr_key(name))
180     {
181         changed_np = !sh->has_local_change(SH_NODEPATH);
182         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     }
201     if (sh->has_knotholder())
202     {
203         changed_kh = !sh->has_local_change(SH_KNOTHOLDER);
204         sh->decrement_local_change(SH_KNOTHOLDER);
205         if (changed_kh) {
206             // this can happen if an LPEItem's knotholder handle was dragged, in which case we want
207             // to keep the knotholder; in all other cases (e.g., if the LPE itself changes) we delete it
208             sh->reset_item(SH_KNOTHOLDER, !strcmp(name, "d"));
209         }
210     }
212     sh->update_statusbar(); //TODO: sh->get_container()->update_statusbar();
215 static Inkscape::XML::NodeEventVector shapeeditor_repr_events = {
216     NULL, /* child_added */
217     NULL, /* child_removed */
218     shapeeditor_event_attr_changed,
219     NULL, /* content_changed */
220     NULL  /* order_changed */
221 };
224 void ShapeEditor::set_item(SPItem *item, SubType type, bool keep_knotholder) {
225     // this happens (and should only happen) when for an LPEItem having both knotholder and nodepath the knotholder
226     // is adapted; in this case we don't want to delete the knotholder since this freezes the handles
227     unset_item(type, keep_knotholder);
229     this->grab_node = -1;
231     if (item) {
232         Inkscape::XML::Node *repr;
233         switch(type) {
234             case SH_NODEPATH:
235                 if (SP_IS_LPE_ITEM(item)) {
236                     //SPLPEItem *lpeitem = SP_LPE_ITEM(item);
237                     //Inkscape::LivePathEffect::Effect *lpe = sp_lpe_item_get_current_lpe(lpeitem);
238                     //if (!(lpe && lpe->isVisible() && lpe->providesKnotholder())) {
239                         // only create nodepath if the item either doesn't have an LPE
240                         // or the LPE is invisible or it doesn't provide a knotholder itself
241                         this->nodepath = sp_nodepath_new(desktop, item,
242                                                          (prefs_get_int_attribute("tools.nodes", "show_handles", 1) != 0));
243                     //} else if (lpe && lpe->isVisible()) {
244                     //    sp_lpe_item_add_temporary_canvasitems(lpeitem, desktop);
245                     //}
246                 }
247                 if (this->nodepath) {
248                     this->nodepath->shape_editor = this;
250                     // setting new listener
251                     repr = SP_OBJECT_REPR(item);
252                     Inkscape::GC::anchor(repr);
253                     sp_repr_add_listener(repr, &shapeeditor_repr_events, this);
254                 }
255                 break;
257             case SH_KNOTHOLDER:
258                 if (!this->knotholder) {
259                     // only recreate knotholder if none is present
260                     this->knotholder = sp_item_knot_holder(item, desktop);
261                 }
262                 if (this->knotholder) {
263                     this->knotholder->update_knots();
264                     // setting new listener
265                     repr = this->knotholder->repr;
266                     Inkscape::GC::anchor(repr);
267                     sp_repr_add_listener(repr, &shapeeditor_repr_events, this);
268                 }
269                 break;
270         }
271     }
274 /** Please note that this function only works for path parameters.
275 *  All other parameters probably will crash Inkscape!
276 */
277 void ShapeEditor::set_item_lpe_path_parameter(SPItem *item, SPObject *lpeobject, const char * key)
279     unset_item(SH_NODEPATH);
281     this->grab_node = -1;
283     if (lpeobject) {
284         this->nodepath = sp_nodepath_new( desktop, lpeobject,
285                                           (prefs_get_int_attribute("tools.nodes", "show_handles", 1) != 0),
286                                           key, item);
287         if (this->nodepath) {
288             this->nodepath->shape_editor = this;
290             // setting new listener
291             Inkscape::XML::Node *repr = SP_OBJECT_REPR(lpeobject);
292             if (repr) {
293                 Inkscape::GC::anchor(repr);
294                 sp_repr_add_listener(repr, &shapeeditor_repr_events, this);
295             }
296         }
297     }
300 /** 
301 *  pass a new knotholder to ShapeEditor to manage (and delete)
302 */
303 void
304 ShapeEditor::set_knotholder(KnotHolder * knot_holder)
306     unset_item(SH_KNOTHOLDER);
308     this->grab_node = -1;
310     if (knot_holder) {
311         this->knotholder = knot_holder;
312     }
316 /** FIXME: think about this. Is this thing only called when the item needs to be updated?
317    Why not make a reload function in NodePath and in KnotHolder? */
318 void ShapeEditor::reset_item (SubType type, bool keep_knotholder)
320     switch (type) {
321         case SH_NODEPATH:
322             if ( (this->nodepath) && (IS_LIVEPATHEFFECT(this->nodepath->object)) ) {
323                 SPItem * item = this->nodepath->item;
324                 SPObject *obj = this->nodepath->object;
325                 char * key = g_strdup(this->nodepath->repr_key);
326                 set_item_lpe_path_parameter(item, obj, key); // the above checks for nodepath, so it is indeed a path that we are editing
327                 g_free(key);
328             }            
329             break;
330         case SH_KNOTHOLDER:
331             if (this->knotholder) {
332                 SPItem * item = get_item(SH_KNOTHOLDER);
333                 set_item(item, SH_KNOTHOLDER, keep_knotholder);
334             }
335             break;
336     }
339 void ShapeEditor::nodepath_destroyed () {
340     this->nodepath = NULL;
343 void ShapeEditor::update_statusbar () {
344     if (this->nodepath)
345         sp_nodepath_update_statusbar(this->nodepath);
348 bool ShapeEditor::is_over_stroke (NR::Point event_p, bool remember) {
350     if (!this->nodepath)
351         return false; // no stroke in knotholder
353     SPItem *item = get_item(SH_NODEPATH);
355     //Translate click point into proper coord system
356     this->curvepoint_doc = desktop->w2d(event_p);
357     this->curvepoint_doc *= from_2geom(sp_item_dt2i_affine(item));
359     sp_nodepath_ensure_livarot_path(this->nodepath);
361     NR::Maybe<Path::cut_position> position = get_nearest_position_on_Path(this->nodepath->livarot_path, this->curvepoint_doc);
362     if (!position) {
363         return false;
364     }
366     NR::Point nearest = get_point_on_Path(this->nodepath->livarot_path, position->piece, position->t);
367     NR::Point delta = nearest - this->curvepoint_doc;
369     delta = desktop->d2w(delta);
371     double stroke_tolerance =
372         (( !SP_OBJECT_STYLE(item)->stroke.isNone() ?
373            desktop->current_zoom() *
374            SP_OBJECT_STYLE (item)->stroke_width.computed * 0.5 *
375            NR::expansion(from_2geom(sp_item_i2d_affine(item)))
376          : 0.0)
377          + prefs_get_int_attribute_limited("options.dragtolerance", "value", 0, 0, 100)) /NR::expansion(from_2geom(sp_item_i2d_affine(item))); 
378     bool close = (NR::L2 (delta) < stroke_tolerance);
380     if (remember && close) {
381         this->curvepoint_event[NR::X] = (gint) event_p [NR::X];
382         this->curvepoint_event[NR::Y] = (gint) event_p [NR::Y];
383         this->hit = true;
384         this->grab_t = position->t;
385         this->grab_node = position->piece;
386     }
388     return close;
391 void ShapeEditor::add_node_near_point() {
392     if (this->nodepath) {
393         sp_nodepath_add_node_near_point(this->nodepath, this->curvepoint_doc);
394     } else if (this->knotholder) {
395         // we do not add nodes in knotholder... yet
396     }
399 void ShapeEditor::select_segment_near_point(bool toggle) {
400     if (this->nodepath) {
401         sp_nodepath_select_segment_near_point(this->nodepath, this->curvepoint_doc, toggle);
402     }
403     if (this->knotholder) {
404         // we do not select segments in knotholder... yet?
405     }
408 void ShapeEditor::cancel_hit() {
409     this->hit = false;
412 bool ShapeEditor::hits_curve() {
413     return (this->hit);
417 void ShapeEditor::curve_drag(gdouble eventx, gdouble eventy) {
418     if (this->nodepath && !this->nodepath->straight_path) {
420         if (this->grab_node == -1) // don't know which segment to drag
421             return;
423         // We round off the extra precision in the motion coordinates provided
424         // by some input devices (like tablets). As we'll store the coordinates
425         // as integers in curvepoint_event we need to do this rounding before
426         // comparing them with the last coordinates from curvepoint_event.
427         // See bug #1593499 for details.
429         gint x = (gint) Inkscape::round(eventx);
430         gint y = (gint) Inkscape::round(eventy);
433         // The coordinates hasn't changed since the last motion event, abort
434         if (this->curvepoint_event[NR::X] == x &&
435             this->curvepoint_event[NR::Y] == y)
436             return;
438         NR::Point const delta_w(eventx - this->curvepoint_event[NR::X],
439                                 eventy - this->curvepoint_event[NR::Y]);
440         NR::Point const delta_dt(this->desktop->w2d(delta_w));
442         sp_nodepath_curve_drag (this->grab_node, this->grab_t, delta_dt); //!!! FIXME: which nodepath?!!! also uses current!!!
443         this->curvepoint_event[NR::X] = x;
444         this->curvepoint_event[NR::Y] = y;
446     }
447     if (this->knotholder) {
448         // we do not drag curve in knotholder
449     }
453 void ShapeEditor::finish_drag() {
454     if (this->nodepath && this->hit) {
455         sp_nodepath_update_repr (this->nodepath, _("Drag curve"));
456     }
459 void ShapeEditor::select_rect(NR::Rect const &rect, bool add) {
460     if (this->nodepath) {
461         sp_nodepath_select_rect(this->nodepath, rect, add);
462     }
465 bool ShapeEditor::has_selection() {
466     if (this->nodepath)
467         return this->nodepath->selected;
468     return false; //  so far, knotholder cannot have selection
471 void ShapeEditor::deselect() {
472     if (this->nodepath)
473         sp_nodepath_deselect(this->nodepath);
476 void ShapeEditor::add_node () {
477     sp_node_selected_add_node(this->nodepath);
480 void ShapeEditor::delete_nodes () {
481     sp_node_selected_delete(this->nodepath);
484 void ShapeEditor::delete_nodes_preserving_shape () {
485     if (this->nodepath && this->nodepath->selected) {
486         sp_node_delete_preserve(g_list_copy(this->nodepath->selected));
487     }
490 void ShapeEditor::delete_segment () {
491     sp_node_selected_delete_segment(this->nodepath);
494 void ShapeEditor::set_node_type(int type) {
495     sp_node_selected_set_type(this->nodepath, (Inkscape::NodePath::NodeType) type);
498 void ShapeEditor::break_at_nodes() {
499     sp_node_selected_break(this->nodepath);
502 void ShapeEditor::join_nodes() {
503     sp_node_selected_join(this->nodepath);
506 void ShapeEditor::join_segments() {
507     sp_node_selected_join_segment(this->nodepath);
510 void ShapeEditor::duplicate_nodes() {
511     sp_node_selected_duplicate(this->nodepath);
514 void ShapeEditor::set_type_of_segments(NRPathcode code) {
515     sp_node_selected_set_line_type(this->nodepath, code);
518 void ShapeEditor::move_nodes_screen(gdouble dx, gdouble dy) {
519     sp_node_selected_move_screen(this->nodepath, dx, dy);
521 void ShapeEditor::move_nodes(gdouble dx, gdouble dy) {
522     sp_node_selected_move(this->nodepath, dx, dy);
525 void ShapeEditor::rotate_nodes(gdouble angle, int which, bool screen) {
526     if (this->nodepath)
527         sp_nodepath_selected_nodes_rotate (this->nodepath, angle, which, screen);
530 void ShapeEditor::scale_nodes(gdouble const grow, int const which) {
531     sp_nodepath_selected_nodes_scale(this->nodepath, grow, which);
533 void ShapeEditor::scale_nodes_screen(gdouble const grow, int const which) {
534     sp_nodepath_selected_nodes_scale_screen(this->nodepath, grow, which);
537 void ShapeEditor::select_all (bool invert) {
538     if (this->nodepath)
539         sp_nodepath_select_all (this->nodepath, invert);
541 void ShapeEditor::select_all_from_subpath (bool invert) {
542     if (this->nodepath)
543         sp_nodepath_select_all_from_subpath (this->nodepath, invert);
545 void ShapeEditor::select_next () {
546     if (this->nodepath) {
547         sp_nodepath_select_next (this->nodepath);
548         if (this->nodepath->numSelected() >= 1) {
549             this->desktop->scroll_to_point(&(this->nodepath->singleSelectedCoords()), 1.0);
550         }
551     }
553 void ShapeEditor::select_prev () {
554     if (this->nodepath) {
555         sp_nodepath_select_prev (this->nodepath);
556         if (this->nodepath->numSelected() >= 1) {
557             this->desktop->scroll_to_point(&(this->nodepath->singleSelectedCoords()), 1.0);
558         }
559     }
562 void ShapeEditor::show_handles (bool show) {
563     if (this->nodepath && !this->nodepath->straight_path)
564         sp_nodepath_show_handles (this->nodepath, show);
567 void ShapeEditor::show_helperpath (bool show) {
568     if (this->nodepath)
569         sp_nodepath_show_helperpath (this->nodepath, show);
572 void ShapeEditor::flip (NR::Dim2 axis, NR::Maybe<NR::Point> center) {
573     if (this->nodepath)
574         sp_nodepath_flip (this->nodepath, axis, center);
577 void ShapeEditor::distribute (NR::Dim2 axis) {
578     if (this->nodepath)
579         sp_nodepath_selected_distribute (this->nodepath, axis);
581 void ShapeEditor::align (NR::Dim2 axis) {
582     if (this->nodepath)
583         sp_nodepath_selected_align (this->nodepath, axis);
587 /*
588   Local Variables:
589   mode:c++
590   c-file-style:"stroustrup"
591   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
592   indent-tabs-mode:nil
593   fill-column:99
594   End:
595 */
596 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :