Code

b1da900f40a3153d2a38244ec60151bd33536f90
[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 "node-context.h"
26 #include "xml/node-event-vector.h"
27 #include "prefs-utils.h"
28 #include "object-edit.h"
29 #include "splivarot.h"
30 #include "style.h"
32 #include "shape-editor.h"
35 ShapeEditorsCollective::ShapeEditorsCollective(SPDesktop */*dt*/) {
36 }
38 ShapeEditorsCollective::~ShapeEditorsCollective() {
39 }
42 void ShapeEditorsCollective::update_statusbar() {
44 //!!! move from nodepath: sp_nodepath_update_statusbar but summing for all nodepaths
46 }
48 ShapeEditor::ShapeEditor(SPDesktop *dt) {
49     this->desktop = dt;
50     this->grab_node = -1;
51     this->nodepath = NULL;
52     this->knotholder = NULL;
53     this->hit = false;
54 }
56 ShapeEditor::~ShapeEditor() {
57     unset_item();
58 }
60 void ShapeEditor::unset_item() {
62     Inkscape::XML::Node *old_repr = NULL;
64     if (this->nodepath) {
65         old_repr = this->nodepath->repr;
66     }
68     if (!old_repr && this->knotholder) {
69         old_repr = this->knotholder->repr;
70     }
72     if (old_repr) { // remove old listener
73         sp_repr_remove_listener_by_data(old_repr, this);
74         Inkscape::GC::release(old_repr);
75     }
77     if (this->nodepath) {
78         this->grab_node = -1;
79         sp_nodepath_destroy(this->nodepath);
80         this->nodepath = NULL;
81     }
83     if (this->knotholder) {
84         sp_knot_holder_destroy(this->knotholder);
85         this->knotholder = NULL;
86     }
87 }
89 bool ShapeEditor::has_nodepath () {
90     return (this->nodepath != NULL);
91 }
93 bool ShapeEditor::has_knotholder () {
94     return (this->knotholder != NULL);
95 }
97 bool ShapeEditor::has_local_change () {
98     if (this->nodepath)
99         return (this->nodepath->local_change != 0);
100     else if (this->knotholder)
101         return (this->knotholder->local_change != 0);
102     else
103         return false;
106 void ShapeEditor::decrement_local_change () {
107     if (this->nodepath) {
108         if (this->nodepath->local_change > 0)
109             this->nodepath->local_change--;
110     } else if (this->knotholder) {
111         this->knotholder->local_change = FALSE;
112     }
115 SPItem *ShapeEditor::get_item () {
116     SPItem *item = NULL;
117     if (this->has_nodepath()) {
118         item = this->nodepath->item;
119     } else if (this->has_knotholder()) {
120         item = SP_ITEM(this->knotholder->item);
121     }
122     return item;
125 GList *ShapeEditor::save_nodepath_selection () {
126     if (this->nodepath)
127         return ::save_nodepath_selection (this->nodepath);
128     return NULL;
131 void ShapeEditor::restore_nodepath_selection (GList *saved) {
132     if (this->nodepath && saved)
133         ::restore_nodepath_selection (this->nodepath, saved);
136 bool ShapeEditor::nodepath_edits_repr_key(gchar const *name) {
137     if (nodepath && name) {
138         return ( !strcmp(name, nodepath->repr_key) || !strcmp(name, nodepath->repr_nodetypes_key) );
139     }
141     return false;
144 static void shapeeditor_event_attr_changed(Inkscape::XML::Node */*repr*/, gchar const *name,
145                                            gchar const */*old_value*/, gchar const */*new_value*/,
146                                            bool /*is_interactive*/, gpointer data)
148     gboolean changed = FALSE;
150     g_assert(data);
151     ShapeEditor *sh = ((ShapeEditor *) data);
153     if ( sh->has_knotholder() || ( sh->has_nodepath() && sh->nodepath_edits_repr_key(name) ) )
154     {
155         changed = !sh->has_local_change();
156         sh->decrement_local_change();
157     }
159     if (changed) {
160         GList *saved = NULL;
161         if (sh->has_nodepath()) {
162             saved = sh->save_nodepath_selection();
163         }
165         sh->reset_item ();
167         if (sh->has_nodepath() && saved) {
168             sh->restore_nodepath_selection(saved);
169             g_list_free (saved);
170         }
171     }
173     sh->update_statusbar(); //TODO: sh->get_container()->update_statusbar();
176 static Inkscape::XML::NodeEventVector shapeeditor_repr_events = {
177     NULL, /* child_added */
178     NULL, /* child_removed */
179     shapeeditor_event_attr_changed,
180     NULL, /* content_changed */
181     NULL  /* order_changed */
182 };
185 void ShapeEditor::set_item(SPItem *item) {
187     unset_item();
189     this->grab_node = -1;
191     if (item) {
192         this->nodepath = sp_nodepath_new(desktop, item, (prefs_get_int_attribute("tools.nodes", "show_handles", 1) != 0));
193         if (this->nodepath) {
194             this->nodepath->shape_editor = this;
195         }
196         this->knotholder = sp_item_knot_holder(item, desktop);
198         if (this->nodepath || this->knotholder) {
199             // setting new listener
200             Inkscape::XML::Node *repr;
201             if (this->knotholder)
202                 repr = this->knotholder->repr;
203             else
204                 repr = SP_OBJECT_REPR(item);
205             if (repr) {
206                 Inkscape::GC::anchor(repr);
207                 sp_repr_add_listener(repr, &shapeeditor_repr_events, this);
208             }
209         }
210     }
213 /** Please note that this function only works for path parameters.
214 *  All other parameters probably will crash Inkscape!
215 */
216 void ShapeEditor::set_item_lpe_path_parameter(SPItem *item, SPObject *lpeobject, const char * key) {
218     unset_item();
220     this->grab_node = -1;
222     if (lpeobject) {
223         this->nodepath = sp_nodepath_new( desktop, lpeobject,
224                                           (prefs_get_int_attribute("tools.nodes", "show_handles", 1) != 0),
225                                           key, item);
226         if (this->nodepath) {
227             this->nodepath->shape_editor = this;
229             // setting new listener
230             Inkscape::XML::Node *repr = SP_OBJECT_REPR(lpeobject);
231             if (repr) {
232                 Inkscape::GC::anchor(repr);
233                 sp_repr_add_listener(repr, &shapeeditor_repr_events, this);
234             }
235         }
236     }
239 /** Please note that this function only works for point parameters.
240 *  All other parameters probably will crash Inkscape!
241 */
242 void ShapeEditor::set_item_lpe_point_parameter(SPItem */*item*/, SPObject */*lpeobject*/, const char * /*key*/) {
243     g_message("ShapeEditor::set_item_lpe_point_parameter has not been implemented yet!");
247 /** FIXME: think about this. Is this thing only called when the item needs to be updated?
248    Why not make a reload function in NodePath and in KnotHolder? */
249 void ShapeEditor::reset_item ()
251     if ( (this->nodepath) && (IS_LIVEPATHEFFECT(this->nodepath->object)) ) {
252         SPItem * item = this->nodepath->item;
253         SPObject *obj = this->nodepath->object;
254         char * key = g_strdup(this->nodepath->repr_key);
255         set_item_lpe_path_parameter(item, obj, key); // the above checks for nodepath, so it is indeed a path that we are editing
256         g_free(key);
257     } else {
258         SPItem * item = get_item();
259         set_item(item);
260     }
263 void ShapeEditor::nodepath_destroyed () {
264     this->nodepath = NULL;
267 void ShapeEditor::update_statusbar () {
268     if (this->nodepath)
269         sp_nodepath_update_statusbar(this->nodepath);
272 bool ShapeEditor::is_over_stroke (NR::Point event_p, bool remember) {
274     if (!this->nodepath)
275         return false; // no stroke in knotholder
277     SPItem *item = get_item();
279     //Translate click point into proper coord system
280     this->curvepoint_doc = desktop->w2d(event_p);
281     this->curvepoint_doc *= sp_item_dt2i_affine(item);
283     sp_nodepath_ensure_livarot_path(this->nodepath);
285     NR::Maybe<Path::cut_position> position = get_nearest_position_on_Path(this->nodepath->livarot_path, this->curvepoint_doc);
286     if (!position) {
287         return false;
288     }
290     NR::Point nearest = get_point_on_Path(this->nodepath->livarot_path, position->piece, position->t);
291     NR::Point delta = nearest - this->curvepoint_doc;
293     delta = desktop->d2w(delta);
295     double stroke_tolerance =
296         (( !SP_OBJECT_STYLE(item)->stroke.isNone() ?
297            desktop->current_zoom() *
298            SP_OBJECT_STYLE (item)->stroke_width.computed * 0.5 *
299            sp_item_i2d_affine (item).expansion()
300          : 0.0)
301          + prefs_get_int_attribute_limited("options.dragtolerance", "value", 0, 0, 100)) /sp_item_i2d_affine (item).expansion(); 
302     bool close = (NR::L2 (delta) < stroke_tolerance);
304     if (remember && close) {
305         this->curvepoint_event[NR::X] = (gint) event_p [NR::X];
306         this->curvepoint_event[NR::Y] = (gint) event_p [NR::Y];
307         this->hit = true;
308         this->grab_t = position->t;
309         this->grab_node = position->piece;
310     }
312     return close;
315 void ShapeEditor::add_node_near_point() {
316     if (this->nodepath) {
317         sp_nodepath_add_node_near_point(this->nodepath, this->curvepoint_doc);
318     } else if (this->knotholder) {
319         // we do not add nodes in knotholder... yet
320     }
323 void ShapeEditor::select_segment_near_point(bool toggle) {
324     if (this->nodepath) {
325         sp_nodepath_select_segment_near_point(this->nodepath, this->curvepoint_doc, toggle);
326     } else if (this->knotholder) {
327         // we do not select segments in knotholder... yet?
328     }
331 void ShapeEditor::cancel_hit() {
332     this->hit = false;
335 bool ShapeEditor::hits_curve() {
336     return (this->hit);
340 void ShapeEditor::curve_drag(gdouble eventx, gdouble eventy) {
341     if (this->nodepath && !this->nodepath->straight_path) {
343         if (this->grab_node == -1) // don't know which segment to drag
344             return;
346         // We round off the extra precision in the motion coordinates provided
347         // by some input devices (like tablets). As we'll store the coordinates
348         // as integers in curvepoint_event we need to do this rounding before
349         // comparing them with the last coordinates from curvepoint_event.
350         // See bug #1593499 for details.
352         gint x = (gint) Inkscape::round(eventx);
353         gint y = (gint) Inkscape::round(eventy);
356         // The coordinates hasn't changed since the last motion event, abort
357         if (this->curvepoint_event[NR::X] == x &&
358             this->curvepoint_event[NR::Y] == y)
359             return;
361         NR::Point const delta_w(eventx - this->curvepoint_event[NR::X],
362                                 eventy - this->curvepoint_event[NR::Y]);
363         NR::Point const delta_dt(this->desktop->w2d(delta_w));
365         sp_nodepath_curve_drag (this->grab_node, this->grab_t, delta_dt); //!!! FIXME: which nodepath?!!! also uses current!!!
366         this->curvepoint_event[NR::X] = x;
367         this->curvepoint_event[NR::Y] = y;
369     } else if (this->knotholder) {
370         // we do not drag curve in knotholder
371     }
375 void ShapeEditor::finish_drag() {
376     if (this->nodepath && this->hit) {
377         sp_nodepath_update_repr (this->nodepath, _("Drag curve"));
378     }
381 void ShapeEditor::select_rect(NR::Rect const &rect, bool add) {
382     if (this->nodepath) {
383         sp_nodepath_select_rect(this->nodepath, rect, add);
384     }
387 bool ShapeEditor::has_selection() {
388     if (this->nodepath)
389         return this->nodepath->selected;
390     return false; //  so far, knotholder cannot have selection
393 void ShapeEditor::deselect() {
394     if (this->nodepath)
395         sp_nodepath_deselect(this->nodepath);
398 void ShapeEditor::add_node () {
399     sp_node_selected_add_node(this->nodepath);
402 void ShapeEditor::delete_nodes () {
403     sp_node_selected_delete(this->nodepath);
406 void ShapeEditor::delete_nodes_preserving_shape () {
407     if (this->nodepath && this->nodepath->selected) {
408         sp_node_delete_preserve(g_list_copy(this->nodepath->selected));
409     }
412 void ShapeEditor::delete_segment () {
413     sp_node_selected_delete_segment(this->nodepath);
416 void ShapeEditor::set_node_type(int type) {
417     sp_node_selected_set_type(this->nodepath, (Inkscape::NodePath::NodeType) type);
420 void ShapeEditor::break_at_nodes() {
421     sp_node_selected_break(this->nodepath);
424 void ShapeEditor::join_nodes() {
425     sp_node_selected_join(this->nodepath);
428 void ShapeEditor::join_segments() {
429     sp_node_selected_join_segment(this->nodepath);
432 void ShapeEditor::duplicate_nodes() {
433     sp_node_selected_duplicate(this->nodepath);
436 void ShapeEditor::set_type_of_segments(NRPathcode code) {
437     sp_node_selected_set_line_type(this->nodepath, code);
440 void ShapeEditor::move_nodes_screen(gdouble dx, gdouble dy) {
441     sp_node_selected_move_screen(this->nodepath, dx, dy);
443 void ShapeEditor::move_nodes(gdouble dx, gdouble dy) {
444     sp_node_selected_move(this->nodepath, dx, dy);
447 void ShapeEditor::rotate_nodes(gdouble angle, int which, bool screen) {
448     if (this->nodepath)
449         sp_nodepath_selected_nodes_rotate (this->nodepath, angle, which, screen);
452 void ShapeEditor::scale_nodes(gdouble const grow, int const which) {
453     sp_nodepath_selected_nodes_scale(this->nodepath, grow, which);
455 void ShapeEditor::scale_nodes_screen(gdouble const grow, int const which) {
456     sp_nodepath_selected_nodes_scale_screen(this->nodepath, grow, which);
459 void ShapeEditor::select_all (bool invert) {
460     if (this->nodepath)
461         sp_nodepath_select_all (this->nodepath, invert);
463 void ShapeEditor::select_all_from_subpath (bool invert) {
464     if (this->nodepath)
465         sp_nodepath_select_all_from_subpath (this->nodepath, invert);
467 void ShapeEditor::select_next () {
468     if (this->nodepath) {
469         sp_nodepath_select_next (this->nodepath);
470         if (this->nodepath->numSelected() >= 1) {
471             this->desktop->scroll_to_point(&(this->nodepath->singleSelectedCoords()), 1.0);
472         }
473     }
475 void ShapeEditor::select_prev () {
476     if (this->nodepath) {
477         sp_nodepath_select_prev (this->nodepath);
478         if (this->nodepath->numSelected() >= 1) {
479             this->desktop->scroll_to_point(&(this->nodepath->singleSelectedCoords()), 1.0);
480         }
481     }
484 void ShapeEditor::show_handles (bool show) {
485     if (this->nodepath && !this->nodepath->straight_path)
486         sp_nodepath_show_handles (this->nodepath, show);
489 void ShapeEditor::show_helperpath (bool show) {
490     if (this->nodepath)
491         sp_nodepath_show_helperpath (this->nodepath, show);
494 void ShapeEditor::flip (NR::Dim2 axis, NR::Maybe<NR::Point> center) {
495     if (this->nodepath)
496         sp_nodepath_flip (this->nodepath, axis, center);
499 void ShapeEditor::distribute (NR::Dim2 axis) {
500     if (this->nodepath)
501         sp_nodepath_selected_distribute (this->nodepath, axis);
503 void ShapeEditor::align (NR::Dim2 axis) {
504     if (this->nodepath)
505         sp_nodepath_selected_align (this->nodepath, axis);
509 /*
510   Local Variables:
511   mode:c++
512   c-file-style:"stroustrup"
513   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
514   indent-tabs-mode:nil
515   fill-column:99
516   End:
517 */
518 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :