Code

Add checkbox for LPEs to temporarily disable them on canvas (but keep them applied...
[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         sp_knot_holder_destroy(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         SPLPEItem *lpeitem = SP_LPE_ITEM(item);
194         if (!sp_lpe_item_has_path_effect(lpeitem) ||
195             !sp_lpe_item_get_livepatheffect(lpeitem)->isVisible() ||
196             !sp_lpe_item_get_livepatheffect(lpeitem)->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 =
200                 sp_nodepath_new(desktop, item, (prefs_get_int_attribute("tools.nodes", "show_handles", 1) != 0));
201         }
202         if (this->nodepath) {
203             this->nodepath->shape_editor = this;
204         }
205         this->knotholder = sp_item_knot_holder(item, desktop);
207         if (this->nodepath || this->knotholder) {
208             // setting new listener
209             Inkscape::XML::Node *repr;
210             if (this->knotholder)
211                 repr = this->knotholder->repr;
212             else
213                 repr = SP_OBJECT_REPR(item);
214             if (repr) {
215                 Inkscape::GC::anchor(repr);
216                 sp_repr_add_listener(repr, &shapeeditor_repr_events, this);
217             }
218         }
219     }
222 /** Please note that this function only works for path parameters.
223 *  All other parameters probably will crash Inkscape!
224 */
225 void ShapeEditor::set_item_lpe_path_parameter(SPItem *item, SPObject *lpeobject, const char * key)
227     unset_item();
229     this->grab_node = -1;
231     if (lpeobject) {
232         this->nodepath = sp_nodepath_new( desktop, lpeobject,
233                                           (prefs_get_int_attribute("tools.nodes", "show_handles", 1) != 0),
234                                           key, item);
235         if (this->nodepath) {
236             this->nodepath->shape_editor = this;
238             // setting new listener
239             Inkscape::XML::Node *repr = SP_OBJECT_REPR(lpeobject);
240             if (repr) {
241                 Inkscape::GC::anchor(repr);
242                 sp_repr_add_listener(repr, &shapeeditor_repr_events, this);
243             }
244         }
245     }
248 /** 
249 *  pass a new knotholder to ShapeEditor to manage (and delete)
250 */
251 void
252 ShapeEditor::set_knotholder(SPKnotHolder * knot_holder)
254     unset_item();
256     this->grab_node = -1;
258     if (knot_holder) {
259         this->knotholder = knot_holder;
260     }
264 /** FIXME: think about this. Is this thing only called when the item needs to be updated?
265    Why not make a reload function in NodePath and in KnotHolder? */
266 void ShapeEditor::reset_item ()
268     if ( (this->nodepath) && (IS_LIVEPATHEFFECT(this->nodepath->object)) ) {
269         SPItem * item = this->nodepath->item;
270         SPObject *obj = this->nodepath->object;
271         char * key = g_strdup(this->nodepath->repr_key);
272         set_item_lpe_path_parameter(item, obj, key); // the above checks for nodepath, so it is indeed a path that we are editing
273         g_free(key);
274     } else {
275         SPItem * item = get_item();
276         set_item(item);
277     }
280 void ShapeEditor::nodepath_destroyed () {
281     this->nodepath = NULL;
284 void ShapeEditor::update_statusbar () {
285     if (this->nodepath)
286         sp_nodepath_update_statusbar(this->nodepath);
289 bool ShapeEditor::is_over_stroke (NR::Point event_p, bool remember) {
291     if (!this->nodepath)
292         return false; // no stroke in knotholder
294     SPItem *item = get_item();
296     //Translate click point into proper coord system
297     this->curvepoint_doc = desktop->w2d(event_p);
298     this->curvepoint_doc *= sp_item_dt2i_affine(item);
300     sp_nodepath_ensure_livarot_path(this->nodepath);
302     NR::Maybe<Path::cut_position> position = get_nearest_position_on_Path(this->nodepath->livarot_path, this->curvepoint_doc);
303     if (!position) {
304         return false;
305     }
307     NR::Point nearest = get_point_on_Path(this->nodepath->livarot_path, position->piece, position->t);
308     NR::Point delta = nearest - this->curvepoint_doc;
310     delta = desktop->d2w(delta);
312     double stroke_tolerance =
313         (( !SP_OBJECT_STYLE(item)->stroke.isNone() ?
314            desktop->current_zoom() *
315            SP_OBJECT_STYLE (item)->stroke_width.computed * 0.5 *
316            NR::expansion(sp_item_i2d_affine(item))
317          : 0.0)
318          + prefs_get_int_attribute_limited("options.dragtolerance", "value", 0, 0, 100)) /NR::expansion(sp_item_i2d_affine(item)); 
319     bool close = (NR::L2 (delta) < stroke_tolerance);
321     if (remember && close) {
322         this->curvepoint_event[NR::X] = (gint) event_p [NR::X];
323         this->curvepoint_event[NR::Y] = (gint) event_p [NR::Y];
324         this->hit = true;
325         this->grab_t = position->t;
326         this->grab_node = position->piece;
327     }
329     return close;
332 void ShapeEditor::add_node_near_point() {
333     if (this->nodepath) {
334         sp_nodepath_add_node_near_point(this->nodepath, this->curvepoint_doc);
335     } else if (this->knotholder) {
336         // we do not add nodes in knotholder... yet
337     }
340 void ShapeEditor::select_segment_near_point(bool toggle) {
341     if (this->nodepath) {
342         sp_nodepath_select_segment_near_point(this->nodepath, this->curvepoint_doc, toggle);
343     } else if (this->knotholder) {
344         // we do not select segments in knotholder... yet?
345     }
348 void ShapeEditor::cancel_hit() {
349     this->hit = false;
352 bool ShapeEditor::hits_curve() {
353     return (this->hit);
357 void ShapeEditor::curve_drag(gdouble eventx, gdouble eventy) {
358     if (this->nodepath && !this->nodepath->straight_path) {
360         if (this->grab_node == -1) // don't know which segment to drag
361             return;
363         // We round off the extra precision in the motion coordinates provided
364         // by some input devices (like tablets). As we'll store the coordinates
365         // as integers in curvepoint_event we need to do this rounding before
366         // comparing them with the last coordinates from curvepoint_event.
367         // See bug #1593499 for details.
369         gint x = (gint) Inkscape::round(eventx);
370         gint y = (gint) Inkscape::round(eventy);
373         // The coordinates hasn't changed since the last motion event, abort
374         if (this->curvepoint_event[NR::X] == x &&
375             this->curvepoint_event[NR::Y] == y)
376             return;
378         NR::Point const delta_w(eventx - this->curvepoint_event[NR::X],
379                                 eventy - this->curvepoint_event[NR::Y]);
380         NR::Point const delta_dt(this->desktop->w2d(delta_w));
382         sp_nodepath_curve_drag (this->grab_node, this->grab_t, delta_dt); //!!! FIXME: which nodepath?!!! also uses current!!!
383         this->curvepoint_event[NR::X] = x;
384         this->curvepoint_event[NR::Y] = y;
386     } else if (this->knotholder) {
387         // we do not drag curve in knotholder
388     }
392 void ShapeEditor::finish_drag() {
393     if (this->nodepath && this->hit) {
394         sp_nodepath_update_repr (this->nodepath, _("Drag curve"));
395     }
398 void ShapeEditor::select_rect(NR::Rect const &rect, bool add) {
399     if (this->nodepath) {
400         sp_nodepath_select_rect(this->nodepath, rect, add);
401     }
404 bool ShapeEditor::has_selection() {
405     if (this->nodepath)
406         return this->nodepath->selected;
407     return false; //  so far, knotholder cannot have selection
410 void ShapeEditor::deselect() {
411     if (this->nodepath)
412         sp_nodepath_deselect(this->nodepath);
415 void ShapeEditor::add_node () {
416     sp_node_selected_add_node(this->nodepath);
419 void ShapeEditor::delete_nodes () {
420     sp_node_selected_delete(this->nodepath);
423 void ShapeEditor::delete_nodes_preserving_shape () {
424     if (this->nodepath && this->nodepath->selected) {
425         sp_node_delete_preserve(g_list_copy(this->nodepath->selected));
426     }
429 void ShapeEditor::delete_segment () {
430     sp_node_selected_delete_segment(this->nodepath);
433 void ShapeEditor::set_node_type(int type) {
434     sp_node_selected_set_type(this->nodepath, (Inkscape::NodePath::NodeType) type);
437 void ShapeEditor::break_at_nodes() {
438     sp_node_selected_break(this->nodepath);
441 void ShapeEditor::join_nodes() {
442     sp_node_selected_join(this->nodepath);
445 void ShapeEditor::join_segments() {
446     sp_node_selected_join_segment(this->nodepath);
449 void ShapeEditor::duplicate_nodes() {
450     sp_node_selected_duplicate(this->nodepath);
453 void ShapeEditor::set_type_of_segments(NRPathcode code) {
454     sp_node_selected_set_line_type(this->nodepath, code);
457 void ShapeEditor::move_nodes_screen(gdouble dx, gdouble dy) {
458     sp_node_selected_move_screen(this->nodepath, dx, dy);
460 void ShapeEditor::move_nodes(gdouble dx, gdouble dy) {
461     sp_node_selected_move(this->nodepath, dx, dy);
464 void ShapeEditor::rotate_nodes(gdouble angle, int which, bool screen) {
465     if (this->nodepath)
466         sp_nodepath_selected_nodes_rotate (this->nodepath, angle, which, screen);
469 void ShapeEditor::scale_nodes(gdouble const grow, int const which) {
470     sp_nodepath_selected_nodes_scale(this->nodepath, grow, which);
472 void ShapeEditor::scale_nodes_screen(gdouble const grow, int const which) {
473     sp_nodepath_selected_nodes_scale_screen(this->nodepath, grow, which);
476 void ShapeEditor::select_all (bool invert) {
477     if (this->nodepath)
478         sp_nodepath_select_all (this->nodepath, invert);
480 void ShapeEditor::select_all_from_subpath (bool invert) {
481     if (this->nodepath)
482         sp_nodepath_select_all_from_subpath (this->nodepath, invert);
484 void ShapeEditor::select_next () {
485     if (this->nodepath) {
486         sp_nodepath_select_next (this->nodepath);
487         if (this->nodepath->numSelected() >= 1) {
488             this->desktop->scroll_to_point(&(this->nodepath->singleSelectedCoords()), 1.0);
489         }
490     }
492 void ShapeEditor::select_prev () {
493     if (this->nodepath) {
494         sp_nodepath_select_prev (this->nodepath);
495         if (this->nodepath->numSelected() >= 1) {
496             this->desktop->scroll_to_point(&(this->nodepath->singleSelectedCoords()), 1.0);
497         }
498     }
501 void ShapeEditor::show_handles (bool show) {
502     if (this->nodepath && !this->nodepath->straight_path)
503         sp_nodepath_show_handles (this->nodepath, show);
506 void ShapeEditor::show_helperpath (bool show) {
507     if (this->nodepath)
508         sp_nodepath_show_helperpath (this->nodepath, show);
511 void ShapeEditor::flip (NR::Dim2 axis, NR::Maybe<NR::Point> center) {
512     if (this->nodepath)
513         sp_nodepath_flip (this->nodepath, axis, center);
516 void ShapeEditor::distribute (NR::Dim2 axis) {
517     if (this->nodepath)
518         sp_nodepath_selected_distribute (this->nodepath, axis);
520 void ShapeEditor::align (NR::Dim2 axis) {
521     if (this->nodepath)
522         sp_nodepath_selected_align (this->nodepath, axis);
526 /*
527   Local Variables:
528   mode:c++
529   c-file-style:"stroustrup"
530   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
531   indent-tabs-mode:nil
532   fill-column:99
533   End:
534 */
535 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :