Code

No more NRMatrix or NRPoint.
[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         this->nodepath = sp_nodepath_new(desktop, item, (prefs_get_int_attribute("tools.nodes", "show_handles", 1) != 0));
194         if (this->nodepath) {
195             this->nodepath->shape_editor = this;
196         }
197         this->knotholder = sp_item_knot_holder(item, desktop);
199         if (this->nodepath || this->knotholder) {
200             // setting new listener
201             Inkscape::XML::Node *repr;
202             if (this->knotholder)
203                 repr = this->knotholder->repr;
204             else
205                 repr = SP_OBJECT_REPR(item);
206             if (repr) {
207                 Inkscape::GC::anchor(repr);
208                 sp_repr_add_listener(repr, &shapeeditor_repr_events, this);
209             }
210         }
211     }
214 /** Please note that this function only works for path parameters.
215 *  All other parameters probably will crash Inkscape!
216 */
217 void ShapeEditor::set_item_lpe_path_parameter(SPItem *item, SPObject *lpeobject, const char * key)
219     unset_item();
221     this->grab_node = -1;
223     if (lpeobject) {
224         this->nodepath = sp_nodepath_new( desktop, lpeobject,
225                                           (prefs_get_int_attribute("tools.nodes", "show_handles", 1) != 0),
226                                           key, item);
227         if (this->nodepath) {
228             this->nodepath->shape_editor = this;
230             // setting new listener
231             Inkscape::XML::Node *repr = SP_OBJECT_REPR(lpeobject);
232             if (repr) {
233                 Inkscape::GC::anchor(repr);
234                 sp_repr_add_listener(repr, &shapeeditor_repr_events, this);
235             }
236         }
237     }
240 /** 
241 *  pass a new knotholder to ShapeEditor to manage (and delete)
242 */
243 void
244 ShapeEditor::set_knotholder(SPKnotHolder * knot_holder)
246     unset_item();
248     this->grab_node = -1;
250     if (knot_holder) {
251         this->knotholder = knot_holder;
252     }
256 /** FIXME: think about this. Is this thing only called when the item needs to be updated?
257    Why not make a reload function in NodePath and in KnotHolder? */
258 void ShapeEditor::reset_item ()
260     if ( (this->nodepath) && (IS_LIVEPATHEFFECT(this->nodepath->object)) ) {
261         SPItem * item = this->nodepath->item;
262         SPObject *obj = this->nodepath->object;
263         char * key = g_strdup(this->nodepath->repr_key);
264         set_item_lpe_path_parameter(item, obj, key); // the above checks for nodepath, so it is indeed a path that we are editing
265         g_free(key);
266     } else {
267         SPItem * item = get_item();
268         set_item(item);
269     }
272 void ShapeEditor::nodepath_destroyed () {
273     this->nodepath = NULL;
276 void ShapeEditor::update_statusbar () {
277     if (this->nodepath)
278         sp_nodepath_update_statusbar(this->nodepath);
281 bool ShapeEditor::is_over_stroke (NR::Point event_p, bool remember) {
283     if (!this->nodepath)
284         return false; // no stroke in knotholder
286     SPItem *item = get_item();
288     //Translate click point into proper coord system
289     this->curvepoint_doc = desktop->w2d(event_p);
290     this->curvepoint_doc *= sp_item_dt2i_affine(item);
292     sp_nodepath_ensure_livarot_path(this->nodepath);
294     NR::Maybe<Path::cut_position> position = get_nearest_position_on_Path(this->nodepath->livarot_path, this->curvepoint_doc);
295     if (!position) {
296         return false;
297     }
299     NR::Point nearest = get_point_on_Path(this->nodepath->livarot_path, position->piece, position->t);
300     NR::Point delta = nearest - this->curvepoint_doc;
302     delta = desktop->d2w(delta);
304     double stroke_tolerance =
305         (( !SP_OBJECT_STYLE(item)->stroke.isNone() ?
306            desktop->current_zoom() *
307            SP_OBJECT_STYLE (item)->stroke_width.computed * 0.5 *
308            NR::expansion(sp_item_i2d_affine(item))
309          : 0.0)
310          + prefs_get_int_attribute_limited("options.dragtolerance", "value", 0, 0, 100)) /NR::expansion(sp_item_i2d_affine(item)); 
311     bool close = (NR::L2 (delta) < stroke_tolerance);
313     if (remember && close) {
314         this->curvepoint_event[NR::X] = (gint) event_p [NR::X];
315         this->curvepoint_event[NR::Y] = (gint) event_p [NR::Y];
316         this->hit = true;
317         this->grab_t = position->t;
318         this->grab_node = position->piece;
319     }
321     return close;
324 void ShapeEditor::add_node_near_point() {
325     if (this->nodepath) {
326         sp_nodepath_add_node_near_point(this->nodepath, this->curvepoint_doc);
327     } else if (this->knotholder) {
328         // we do not add nodes in knotholder... yet
329     }
332 void ShapeEditor::select_segment_near_point(bool toggle) {
333     if (this->nodepath) {
334         sp_nodepath_select_segment_near_point(this->nodepath, this->curvepoint_doc, toggle);
335     } else if (this->knotholder) {
336         // we do not select segments in knotholder... yet?
337     }
340 void ShapeEditor::cancel_hit() {
341     this->hit = false;
344 bool ShapeEditor::hits_curve() {
345     return (this->hit);
349 void ShapeEditor::curve_drag(gdouble eventx, gdouble eventy) {
350     if (this->nodepath && !this->nodepath->straight_path) {
352         if (this->grab_node == -1) // don't know which segment to drag
353             return;
355         // We round off the extra precision in the motion coordinates provided
356         // by some input devices (like tablets). As we'll store the coordinates
357         // as integers in curvepoint_event we need to do this rounding before
358         // comparing them with the last coordinates from curvepoint_event.
359         // See bug #1593499 for details.
361         gint x = (gint) Inkscape::round(eventx);
362         gint y = (gint) Inkscape::round(eventy);
365         // The coordinates hasn't changed since the last motion event, abort
366         if (this->curvepoint_event[NR::X] == x &&
367             this->curvepoint_event[NR::Y] == y)
368             return;
370         NR::Point const delta_w(eventx - this->curvepoint_event[NR::X],
371                                 eventy - this->curvepoint_event[NR::Y]);
372         NR::Point const delta_dt(this->desktop->w2d(delta_w));
374         sp_nodepath_curve_drag (this->grab_node, this->grab_t, delta_dt); //!!! FIXME: which nodepath?!!! also uses current!!!
375         this->curvepoint_event[NR::X] = x;
376         this->curvepoint_event[NR::Y] = y;
378     } else if (this->knotholder) {
379         // we do not drag curve in knotholder
380     }
384 void ShapeEditor::finish_drag() {
385     if (this->nodepath && this->hit) {
386         sp_nodepath_update_repr (this->nodepath, _("Drag curve"));
387     }
390 void ShapeEditor::select_rect(NR::Rect const &rect, bool add) {
391     if (this->nodepath) {
392         sp_nodepath_select_rect(this->nodepath, rect, add);
393     }
396 bool ShapeEditor::has_selection() {
397     if (this->nodepath)
398         return this->nodepath->selected;
399     return false; //  so far, knotholder cannot have selection
402 void ShapeEditor::deselect() {
403     if (this->nodepath)
404         sp_nodepath_deselect(this->nodepath);
407 void ShapeEditor::add_node () {
408     sp_node_selected_add_node(this->nodepath);
411 void ShapeEditor::delete_nodes () {
412     sp_node_selected_delete(this->nodepath);
415 void ShapeEditor::delete_nodes_preserving_shape () {
416     if (this->nodepath && this->nodepath->selected) {
417         sp_node_delete_preserve(g_list_copy(this->nodepath->selected));
418     }
421 void ShapeEditor::delete_segment () {
422     sp_node_selected_delete_segment(this->nodepath);
425 void ShapeEditor::set_node_type(int type) {
426     sp_node_selected_set_type(this->nodepath, (Inkscape::NodePath::NodeType) type);
429 void ShapeEditor::break_at_nodes() {
430     sp_node_selected_break(this->nodepath);
433 void ShapeEditor::join_nodes() {
434     sp_node_selected_join(this->nodepath);
437 void ShapeEditor::join_segments() {
438     sp_node_selected_join_segment(this->nodepath);
441 void ShapeEditor::duplicate_nodes() {
442     sp_node_selected_duplicate(this->nodepath);
445 void ShapeEditor::set_type_of_segments(NRPathcode code) {
446     sp_node_selected_set_line_type(this->nodepath, code);
449 void ShapeEditor::move_nodes_screen(gdouble dx, gdouble dy) {
450     sp_node_selected_move_screen(this->nodepath, dx, dy);
452 void ShapeEditor::move_nodes(gdouble dx, gdouble dy) {
453     sp_node_selected_move(this->nodepath, dx, dy);
456 void ShapeEditor::rotate_nodes(gdouble angle, int which, bool screen) {
457     if (this->nodepath)
458         sp_nodepath_selected_nodes_rotate (this->nodepath, angle, which, screen);
461 void ShapeEditor::scale_nodes(gdouble const grow, int const which) {
462     sp_nodepath_selected_nodes_scale(this->nodepath, grow, which);
464 void ShapeEditor::scale_nodes_screen(gdouble const grow, int const which) {
465     sp_nodepath_selected_nodes_scale_screen(this->nodepath, grow, which);
468 void ShapeEditor::select_all (bool invert) {
469     if (this->nodepath)
470         sp_nodepath_select_all (this->nodepath, invert);
472 void ShapeEditor::select_all_from_subpath (bool invert) {
473     if (this->nodepath)
474         sp_nodepath_select_all_from_subpath (this->nodepath, invert);
476 void ShapeEditor::select_next () {
477     if (this->nodepath) {
478         sp_nodepath_select_next (this->nodepath);
479         if (this->nodepath->numSelected() >= 1) {
480             this->desktop->scroll_to_point(&(this->nodepath->singleSelectedCoords()), 1.0);
481         }
482     }
484 void ShapeEditor::select_prev () {
485     if (this->nodepath) {
486         sp_nodepath_select_prev (this->nodepath);
487         if (this->nodepath->numSelected() >= 1) {
488             this->desktop->scroll_to_point(&(this->nodepath->singleSelectedCoords()), 1.0);
489         }
490     }
493 void ShapeEditor::show_handles (bool show) {
494     if (this->nodepath && !this->nodepath->straight_path)
495         sp_nodepath_show_handles (this->nodepath, show);
498 void ShapeEditor::show_helperpath (bool show) {
499     if (this->nodepath)
500         sp_nodepath_show_helperpath (this->nodepath, show);
503 void ShapeEditor::flip (NR::Dim2 axis, NR::Maybe<NR::Point> center) {
504     if (this->nodepath)
505         sp_nodepath_flip (this->nodepath, axis, center);
508 void ShapeEditor::distribute (NR::Dim2 axis) {
509     if (this->nodepath)
510         sp_nodepath_selected_distribute (this->nodepath, axis);
512 void ShapeEditor::align (NR::Dim2 axis) {
513     if (this->nodepath)
514         sp_nodepath_selected_align (this->nodepath, axis);
518 /*
519   Local Variables:
520   mode:c++
521   c-file-style:"stroustrup"
522   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
523   indent-tabs-mode:nil
524   fill-column:99
525   End:
526 */
527 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :