Code

Second step: try to make helper curves respond faster (don't recreate/delete the...
[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();
59 }
61 void ShapeEditor::unset_item() {
63     Inkscape::XML::Node *old_repr = NULL;
65     if (this->nodepath) {
66         old_repr = this->nodepath->repr;
67     }
69     if (!old_repr && this->knotholder) {
70         old_repr = this->knotholder->repr;
71     }
73     if (old_repr) { // remove old listener
74         sp_repr_remove_listener_by_data(old_repr, this);
75         Inkscape::GC::release(old_repr);
76     }
78     if (this->nodepath) {
79         this->grab_node = -1;
80         sp_nodepath_destroy(this->nodepath);
81         this->nodepath = NULL;
82     }
84     if (this->knotholder) {
85         delete this->knotholder;
86         this->knotholder = NULL;
87     }
88 }
90 bool ShapeEditor::has_nodepath () {
91     return (this->nodepath != NULL);
92 }
94 bool ShapeEditor::has_knotholder () {
95     return (this->knotholder != NULL);
96 }
98 bool ShapeEditor::has_local_change () {
99     if (this->nodepath)
100         return (this->nodepath->local_change != 0);
101     else if (this->knotholder)
102         return (this->knotholder->local_change != 0);
103     else
104         return false;
107 void ShapeEditor::decrement_local_change () {
108     if (this->nodepath) {
109         if (this->nodepath->local_change > 0)
110             this->nodepath->local_change--;
111     } else if (this->knotholder) {
112         this->knotholder->local_change = FALSE;
113     }
116 SPItem *ShapeEditor::get_item () {
117     SPItem *item = NULL;
118     if (this->has_nodepath()) {
119         item = this->nodepath->item;
120     } else if (this->has_knotholder()) {
121         item = SP_ITEM(this->knotholder->item);
122     }
123     return item;
126 GList *ShapeEditor::save_nodepath_selection () {
127     if (this->nodepath)
128         return ::save_nodepath_selection (this->nodepath);
129     return NULL;
132 void ShapeEditor::restore_nodepath_selection (GList *saved) {
133     if (this->nodepath && saved)
134         ::restore_nodepath_selection (this->nodepath, saved);
137 bool ShapeEditor::nodepath_edits_repr_key(gchar const *name) {
138     if (nodepath && name) {
139         return ( !strcmp(name, nodepath->repr_key) || !strcmp(name, nodepath->repr_nodetypes_key) );
140     }
142     return false;
145 static void shapeeditor_event_attr_changed(Inkscape::XML::Node */*repr*/, gchar const *name,
146                                            gchar const */*old_value*/, gchar const */*new_value*/,
147                                            bool /*is_interactive*/, gpointer data)
149     gboolean changed = FALSE;
151     g_assert(data);
152     ShapeEditor *sh = ((ShapeEditor *) data);
154     if ( sh->has_knotholder() || ( sh->has_nodepath() && sh->nodepath_edits_repr_key(name) ) )
155     {
156         changed = !sh->has_local_change();
157         sh->decrement_local_change();
158     }
160     if (changed) {
161         GList *saved = NULL;
162         if (sh->has_nodepath()) {
163             saved = sh->save_nodepath_selection();
164         }
166         sh->reset_item ();
168         if (sh->has_nodepath() && saved) {
169             sh->restore_nodepath_selection(saved);
170             g_list_free (saved);
171         }
172     }
174     sh->update_statusbar(); //TODO: sh->get_container()->update_statusbar();
177 static Inkscape::XML::NodeEventVector shapeeditor_repr_events = {
178     NULL, /* child_added */
179     NULL, /* child_removed */
180     shapeeditor_event_attr_changed,
181     NULL, /* content_changed */
182     NULL  /* order_changed */
183 };
186 void ShapeEditor::set_item(SPItem *item) {
188     unset_item();
190     this->grab_node = -1;
192     if (item) {
193         if (SP_IS_LPE_ITEM(item)) {
194             SPLPEItem *lpeitem = SP_LPE_ITEM(item);
195             Inkscape::LivePathEffect::Effect *lpe = sp_lpe_item_get_current_lpe(lpeitem);
196             if (!(lpe && lpe->isVisible() && lpe->providesKnotholder())) {
197                 // only create nodepath if the item either doesn't have an LPE
198                 // or the LPE is invisible or it doesn't provide a knotholder itself
199                 this->nodepath = sp_nodepath_new(desktop, item,
200                                                  (prefs_get_int_attribute("tools.nodes", "show_handles", 1) != 0));
201             } else if (lpe && lpe->isVisible() && lpe->showOrigPath()) {
202                 sp_lpe_item_add_temporary_canvasitems(lpeitem, desktop);
203             }
204         }
206         if (this->nodepath) {
207             this->nodepath->shape_editor = this;
208         }
209         this->knotholder = sp_item_knot_holder(item, desktop);
211         if (this->nodepath || this->knotholder) {
212             // setting new listener
213             Inkscape::XML::Node *repr;
214             if (this->knotholder)
215                 repr = this->knotholder->repr;
216             else
217                 repr = SP_OBJECT_REPR(item);
218             if (repr) {
219                 Inkscape::GC::anchor(repr);
220                 sp_repr_add_listener(repr, &shapeeditor_repr_events, this);
221             }
222         }
223     }
226 /** Please note that this function only works for path parameters.
227 *  All other parameters probably will crash Inkscape!
228 */
229 void ShapeEditor::set_item_lpe_path_parameter(SPItem *item, SPObject *lpeobject, const char * key)
231     unset_item();
233     this->grab_node = -1;
235     if (lpeobject) {
236         this->nodepath = sp_nodepath_new( desktop, lpeobject,
237                                           (prefs_get_int_attribute("tools.nodes", "show_handles", 1) != 0),
238                                           key, item);
239         if (this->nodepath) {
240             this->nodepath->shape_editor = this;
242             // setting new listener
243             Inkscape::XML::Node *repr = SP_OBJECT_REPR(lpeobject);
244             if (repr) {
245                 Inkscape::GC::anchor(repr);
246                 sp_repr_add_listener(repr, &shapeeditor_repr_events, this);
247             }
248         }
249     }
252 /** 
253 *  pass a new knotholder to ShapeEditor to manage (and delete)
254 */
255 void
256 ShapeEditor::set_knotholder(KnotHolder * knot_holder)
258     unset_item();
260     this->grab_node = -1;
262     if (knot_holder) {
263         this->knotholder = knot_holder;
264     }
268 /** FIXME: think about this. Is this thing only called when the item needs to be updated?
269    Why not make a reload function in NodePath and in KnotHolder? */
270 void ShapeEditor::reset_item ()
272     if ( (this->nodepath) && (IS_LIVEPATHEFFECT(this->nodepath->object)) ) {
273         SPItem * item = this->nodepath->item;
274         SPObject *obj = this->nodepath->object;
275         char * key = g_strdup(this->nodepath->repr_key);
276         set_item_lpe_path_parameter(item, obj, key); // the above checks for nodepath, so it is indeed a path that we are editing
277         g_free(key);
278     } else {
279         SPItem * item = get_item();
280         set_item(item);
281     }
284 void ShapeEditor::nodepath_destroyed () {
285     this->nodepath = NULL;
288 void ShapeEditor::update_statusbar () {
289     if (this->nodepath)
290         sp_nodepath_update_statusbar(this->nodepath);
293 bool ShapeEditor::is_over_stroke (NR::Point event_p, bool remember) {
295     if (!this->nodepath)
296         return false; // no stroke in knotholder
298     SPItem *item = get_item();
300     //Translate click point into proper coord system
301     this->curvepoint_doc = desktop->w2d(event_p);
302     this->curvepoint_doc *= from_2geom(sp_item_dt2i_affine(item));
304     sp_nodepath_ensure_livarot_path(this->nodepath);
306     NR::Maybe<Path::cut_position> position = get_nearest_position_on_Path(this->nodepath->livarot_path, this->curvepoint_doc);
307     if (!position) {
308         return false;
309     }
311     NR::Point nearest = get_point_on_Path(this->nodepath->livarot_path, position->piece, position->t);
312     NR::Point delta = nearest - this->curvepoint_doc;
314     delta = desktop->d2w(delta);
316     double stroke_tolerance =
317         (( !SP_OBJECT_STYLE(item)->stroke.isNone() ?
318            desktop->current_zoom() *
319            SP_OBJECT_STYLE (item)->stroke_width.computed * 0.5 *
320            NR::expansion(from_2geom(sp_item_i2d_affine(item)))
321          : 0.0)
322          + prefs_get_int_attribute_limited("options.dragtolerance", "value", 0, 0, 100)) /NR::expansion(from_2geom(sp_item_i2d_affine(item))); 
323     bool close = (NR::L2 (delta) < stroke_tolerance);
325     if (remember && close) {
326         this->curvepoint_event[NR::X] = (gint) event_p [NR::X];
327         this->curvepoint_event[NR::Y] = (gint) event_p [NR::Y];
328         this->hit = true;
329         this->grab_t = position->t;
330         this->grab_node = position->piece;
331     }
333     return close;
336 void ShapeEditor::add_node_near_point() {
337     if (this->nodepath) {
338         sp_nodepath_add_node_near_point(this->nodepath, this->curvepoint_doc);
339     } else if (this->knotholder) {
340         // we do not add nodes in knotholder... yet
341     }
344 void ShapeEditor::select_segment_near_point(bool toggle) {
345     if (this->nodepath) {
346         sp_nodepath_select_segment_near_point(this->nodepath, this->curvepoint_doc, toggle);
347     } else if (this->knotholder) {
348         // we do not select segments in knotholder... yet?
349     }
352 void ShapeEditor::cancel_hit() {
353     this->hit = false;
356 bool ShapeEditor::hits_curve() {
357     return (this->hit);
361 void ShapeEditor::curve_drag(gdouble eventx, gdouble eventy) {
362     if (this->nodepath && !this->nodepath->straight_path) {
364         if (this->grab_node == -1) // don't know which segment to drag
365             return;
367         // We round off the extra precision in the motion coordinates provided
368         // by some input devices (like tablets). As we'll store the coordinates
369         // as integers in curvepoint_event we need to do this rounding before
370         // comparing them with the last coordinates from curvepoint_event.
371         // See bug #1593499 for details.
373         gint x = (gint) Inkscape::round(eventx);
374         gint y = (gint) Inkscape::round(eventy);
377         // The coordinates hasn't changed since the last motion event, abort
378         if (this->curvepoint_event[NR::X] == x &&
379             this->curvepoint_event[NR::Y] == y)
380             return;
382         NR::Point const delta_w(eventx - this->curvepoint_event[NR::X],
383                                 eventy - this->curvepoint_event[NR::Y]);
384         NR::Point const delta_dt(this->desktop->w2d(delta_w));
386         sp_nodepath_curve_drag (this->grab_node, this->grab_t, delta_dt); //!!! FIXME: which nodepath?!!! also uses current!!!
387         this->curvepoint_event[NR::X] = x;
388         this->curvepoint_event[NR::Y] = y;
390     } else if (this->knotholder) {
391         // we do not drag curve in knotholder
392     }
396 void ShapeEditor::finish_drag() {
397     if (this->nodepath && this->hit) {
398         sp_nodepath_update_repr (this->nodepath, _("Drag curve"));
399     }
402 void ShapeEditor::select_rect(NR::Rect const &rect, bool add) {
403     if (this->nodepath) {
404         sp_nodepath_select_rect(this->nodepath, rect, add);
405     }
408 bool ShapeEditor::has_selection() {
409     if (this->nodepath)
410         return this->nodepath->selected;
411     return false; //  so far, knotholder cannot have selection
414 void ShapeEditor::deselect() {
415     if (this->nodepath)
416         sp_nodepath_deselect(this->nodepath);
419 void ShapeEditor::add_node () {
420     sp_node_selected_add_node(this->nodepath);
423 void ShapeEditor::delete_nodes () {
424     sp_node_selected_delete(this->nodepath);
427 void ShapeEditor::delete_nodes_preserving_shape () {
428     if (this->nodepath && this->nodepath->selected) {
429         sp_node_delete_preserve(g_list_copy(this->nodepath->selected));
430     }
433 void ShapeEditor::delete_segment () {
434     sp_node_selected_delete_segment(this->nodepath);
437 void ShapeEditor::set_node_type(int type) {
438     sp_node_selected_set_type(this->nodepath, (Inkscape::NodePath::NodeType) type);
441 void ShapeEditor::break_at_nodes() {
442     sp_node_selected_break(this->nodepath);
445 void ShapeEditor::join_nodes() {
446     sp_node_selected_join(this->nodepath);
449 void ShapeEditor::join_segments() {
450     sp_node_selected_join_segment(this->nodepath);
453 void ShapeEditor::duplicate_nodes() {
454     sp_node_selected_duplicate(this->nodepath);
457 void ShapeEditor::set_type_of_segments(NRPathcode code) {
458     sp_node_selected_set_line_type(this->nodepath, code);
461 void ShapeEditor::move_nodes_screen(gdouble dx, gdouble dy) {
462     sp_node_selected_move_screen(this->nodepath, dx, dy);
464 void ShapeEditor::move_nodes(gdouble dx, gdouble dy) {
465     sp_node_selected_move(this->nodepath, dx, dy);
468 void ShapeEditor::rotate_nodes(gdouble angle, int which, bool screen) {
469     if (this->nodepath)
470         sp_nodepath_selected_nodes_rotate (this->nodepath, angle, which, screen);
473 void ShapeEditor::scale_nodes(gdouble const grow, int const which) {
474     sp_nodepath_selected_nodes_scale(this->nodepath, grow, which);
476 void ShapeEditor::scale_nodes_screen(gdouble const grow, int const which) {
477     sp_nodepath_selected_nodes_scale_screen(this->nodepath, grow, which);
480 void ShapeEditor::select_all (bool invert) {
481     if (this->nodepath)
482         sp_nodepath_select_all (this->nodepath, invert);
484 void ShapeEditor::select_all_from_subpath (bool invert) {
485     if (this->nodepath)
486         sp_nodepath_select_all_from_subpath (this->nodepath, invert);
488 void ShapeEditor::select_next () {
489     if (this->nodepath) {
490         sp_nodepath_select_next (this->nodepath);
491         if (this->nodepath->numSelected() >= 1) {
492             this->desktop->scroll_to_point(&(this->nodepath->singleSelectedCoords()), 1.0);
493         }
494     }
496 void ShapeEditor::select_prev () {
497     if (this->nodepath) {
498         sp_nodepath_select_prev (this->nodepath);
499         if (this->nodepath->numSelected() >= 1) {
500             this->desktop->scroll_to_point(&(this->nodepath->singleSelectedCoords()), 1.0);
501         }
502     }
505 void ShapeEditor::show_handles (bool show) {
506     if (this->nodepath && !this->nodepath->straight_path)
507         sp_nodepath_show_handles (this->nodepath, show);
510 void ShapeEditor::show_helperpath (bool show) {
511     if (this->nodepath)
512         sp_nodepath_show_helperpath (this->nodepath, show);
515 void ShapeEditor::flip (NR::Dim2 axis, NR::Maybe<NR::Point> center) {
516     if (this->nodepath)
517         sp_nodepath_flip (this->nodepath, axis, center);
520 void ShapeEditor::distribute (NR::Dim2 axis) {
521     if (this->nodepath)
522         sp_nodepath_selected_distribute (this->nodepath, axis);
524 void ShapeEditor::align (NR::Dim2 axis) {
525     if (this->nodepath)
526         sp_nodepath_selected_align (this->nodepath, axis);
530 /*
531   Local Variables:
532   mode:c++
533   c-file-style:"stroustrup"
534   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
535   indent-tabs-mode:nil
536   fill-column:99
537   End:
538 */
539 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :