Code

Fixed forgotten variable assignment
[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 "nodepath.h"
27 #include "xml/node-event-vector.h"
28 #include "prefs-utils.h"
29 #include "object-edit.h"
30 #include "style.h"
31 #include "display/curve.h"
32 #include <2geom/pathvector.h>
33 #include "sp-shape.h"
35 #include "shape-editor.h"
38 ShapeEditorsCollective::ShapeEditorsCollective(SPDesktop */*dt*/) {
39 }
41 ShapeEditorsCollective::~ShapeEditorsCollective() {
42 }
45 void ShapeEditorsCollective::update_statusbar() {
47 //!!! move from nodepath: sp_nodepath_update_statusbar but summing for all nodepaths
49 }
51 ShapeEditor::ShapeEditor(SPDesktop *dt) {
52     this->desktop = dt;
53     this->grab_node = -1;
54     this->nodepath = NULL;
55     this->knotholder = NULL;
56     this->hit = false;
57 }
59 ShapeEditor::~ShapeEditor() {
60     unset_item(SH_KNOTHOLDER);
61     unset_item(SH_NODEPATH);
62 }
64 void ShapeEditor::unset_item(SubType type, bool keep_knotholder) {
65     Inkscape::XML::Node *old_repr = NULL;
67     switch (type) {
68         case SH_NODEPATH:
69             if (this->nodepath) {
70                 old_repr = this->nodepath->repr;
71                 sp_repr_remove_listener_by_data(old_repr, this);
72                 Inkscape::GC::release(old_repr);
74                 this->grab_node = -1;
75                 sp_nodepath_destroy(this->nodepath);
76                 this->nodepath = NULL;
77             }
78             break;
79         case SH_KNOTHOLDER:
80             if (this->knotholder) {
81                 old_repr = this->knotholder->repr;
82                 sp_repr_remove_listener_by_data(old_repr, this);
83                 Inkscape::GC::release(old_repr);
85                 if (!keep_knotholder) {
86                     delete this->knotholder;
87                     this->knotholder = NULL;
88                 }
89             }
90             break;
91     }
92 }
94 bool ShapeEditor::has_nodepath () {
95     return (this->nodepath != NULL);
96 }
98 bool ShapeEditor::has_knotholder () {
99     return (this->knotholder != NULL);
102 void ShapeEditor::update_knotholder () {
103     if (this->knotholder)
104         this->knotholder->update_knots();
107 bool ShapeEditor::has_local_change (SubType type) {
108     switch (type) {
109         case SH_NODEPATH:
110             return (this->nodepath && this->nodepath->local_change);
111         case SH_KNOTHOLDER:
112             return (this->knotholder && this->knotholder->local_change != 0);
113         default:
114             g_assert_not_reached();
115     }
118 void ShapeEditor::decrement_local_change (SubType type) {
119     switch (type) {
120         case SH_NODEPATH:
121             if (this->nodepath && this->nodepath->local_change > 0) {
122                 this->nodepath->local_change--;
123             }
124             break;
125         case SH_KNOTHOLDER:
126             if (this->knotholder) {
127                 this->knotholder->local_change = FALSE;
128             }
129             break;
130         default:
131             g_assert_not_reached();
132     }
135 const SPItem *ShapeEditor::get_item (SubType type) {
136     const SPItem *item = NULL;
137     switch (type) {
138         case SH_NODEPATH:
139             if (this->has_nodepath()) {
140                 item = this->nodepath->item;
141             }
142             break;
143         case SH_KNOTHOLDER:
144             if (this->has_knotholder()) {
145                 item = this->knotholder->getItem();
146             }
147             break;
148     }
149     return item;
152 GList *ShapeEditor::save_nodepath_selection () {
153     if (this->nodepath)
154         return ::save_nodepath_selection (this->nodepath);
155     return NULL;
158 void ShapeEditor::restore_nodepath_selection (GList *saved) {
159     if (this->nodepath && saved)
160         ::restore_nodepath_selection (this->nodepath, saved);
163 bool ShapeEditor::nodepath_edits_repr_key(gchar const *name) {
164     if (nodepath && name) {
165         return ( !strcmp(name, nodepath->repr_key) || !strcmp(name, nodepath->repr_nodetypes_key) );
166     }
168     return false;
171 static void shapeeditor_event_attr_changed(Inkscape::XML::Node */*repr*/, gchar const *name,
172                                            gchar const */*old_value*/, gchar const */*new_value*/,
173                                            bool /*is_interactive*/, gpointer data)
175     gboolean changed_np = FALSE;
176     gboolean changed_kh = FALSE;
178     g_assert(data);
179     ShapeEditor *sh = ((ShapeEditor *) data);
181     if (sh->has_nodepath() && sh->nodepath_edits_repr_key(name))
182     {
183         changed_np = !sh->has_local_change(SH_NODEPATH);
184         sh->decrement_local_change(SH_NODEPATH);
186     }
188     if (changed_np) {
189         GList *saved = NULL;
190         if (sh->has_nodepath()) {
191             saved = sh->save_nodepath_selection();
192         }
194         sh->reset_item(SH_NODEPATH);
196         if (sh->has_nodepath() && saved) {
197             sh->restore_nodepath_selection(saved);
198             g_list_free (saved);
199         }
200     }
203     if (sh->has_knotholder())
204     {
205         changed_kh = !sh->has_local_change(SH_KNOTHOLDER);
206         sh->decrement_local_change(SH_KNOTHOLDER);
207         if (changed_kh) {
208             // this can happen if an LPEItem's knotholder handle was dragged, in which case we want
209             // to keep the knotholder; in all other cases (e.g., if the LPE itself changes) we delete it
210             sh->reset_item(SH_KNOTHOLDER, !strcmp(name, "d"));
211         }
212     }
214     sh->update_statusbar(); //TODO: sh->get_container()->update_statusbar();
217 static Inkscape::XML::NodeEventVector shapeeditor_repr_events = {
218     NULL, /* child_added */
219     NULL, /* child_removed */
220     shapeeditor_event_attr_changed,
221     NULL, /* content_changed */
222     NULL  /* order_changed */
223 };
226 void ShapeEditor::set_item(SPItem *item, SubType type, bool keep_knotholder) {
227     // this happens (and should only happen) when for an LPEItem having both knotholder and nodepath the knotholder
228     // is adapted; in this case we don't want to delete the knotholder since this freezes the handles
229     unset_item(type, keep_knotholder);
231     this->grab_node = -1;
233     if (item) {
234         Inkscape::XML::Node *repr;
235         switch(type) {
236             case SH_NODEPATH:
237                 if (SP_IS_LPE_ITEM(item)) {
238                     this->nodepath = sp_nodepath_new(desktop, item, (prefs_get_int_attribute("tools.nodes", "show_handles", 1) != 0));
239                 }
240                 if (this->nodepath) {
241                     this->nodepath->shape_editor = this;
243                     // setting new listener
244                     repr = SP_OBJECT_REPR(item);
245                     Inkscape::GC::anchor(repr);
246                     sp_repr_add_listener(repr, &shapeeditor_repr_events, this);
247                 }
248                 break;
250             case SH_KNOTHOLDER:
251                 if (!this->knotholder) {
252                     // only recreate knotholder if none is present
253                     this->knotholder = sp_item_knot_holder(item, desktop);
254                 }
255                 if (this->knotholder) {
256                     this->knotholder->update_knots();
257                     // setting new listener
258                     repr = this->knotholder->repr;
259                     Inkscape::GC::anchor(repr);
260                     sp_repr_add_listener(repr, &shapeeditor_repr_events, this);
261                 }
262                 break;
263         }
264     }
267 /** Please note that this function only works for path parameters.
268 *  All other parameters probably will crash Inkscape!
269 */
270 void ShapeEditor::set_item_lpe_path_parameter(SPItem *item, SPObject *lpeobject, const char * key)
272     unset_item(SH_NODEPATH);
274     this->grab_node = -1;
276     if (lpeobject) {
277         this->nodepath = sp_nodepath_new( desktop, lpeobject,
278                                           (prefs_get_int_attribute("tools.nodes", "show_handles", 1) != 0),
279                                           key, item);
280         if (this->nodepath) {
281             this->nodepath->shape_editor = this;
283             // setting new listener
284             Inkscape::XML::Node *repr = SP_OBJECT_REPR(lpeobject);
285             if (repr) {
286                 Inkscape::GC::anchor(repr);
287                 sp_repr_add_listener(repr, &shapeeditor_repr_events, this);
288             }
289         }
290     }
293 /** 
294 *  pass a new knotholder to ShapeEditor to manage (and delete)
295 */
296 void
297 ShapeEditor::set_knotholder(KnotHolder * knot_holder)
299     unset_item(SH_KNOTHOLDER);
301     this->grab_node = -1;
303     if (knot_holder) {
304         this->knotholder = knot_holder;
305     }
309 /** FIXME: think about this. Is this thing only called when the item needs to be updated?
310    Why not make a reload function in NodePath and in KnotHolder? */
311 void ShapeEditor::reset_item (SubType type, bool keep_knotholder)
313     switch (type) {
314         case SH_NODEPATH:
315             if ( (this->nodepath) && (IS_LIVEPATHEFFECT(this->nodepath->object)) ) {
316                 SPItem * item = this->nodepath->item;
317                 SPObject *obj = this->nodepath->object;
318                 char * key = g_strdup(this->nodepath->repr_key);
319                 set_item_lpe_path_parameter(item, obj, key); // the above checks for nodepath, so it is indeed a path that we are editing
320                 g_free(key);
321             } else {
322                 SPItem * item = (SPItem *) get_item(SH_NODEPATH);
323                 set_item(item, SH_NODEPATH);
324             }                
325             break;
326         case SH_KNOTHOLDER:
327             if (this->knotholder) {
328                 SPItem * item = (SPItem *) get_item(SH_KNOTHOLDER);
329                 set_item(item, SH_KNOTHOLDER, keep_knotholder);
330             }
331             break;
332     }
335 void ShapeEditor::nodepath_destroyed () {
336     this->nodepath = NULL;
339 void ShapeEditor::update_statusbar () {
340     if (this->nodepath)
341         sp_nodepath_update_statusbar(this->nodepath);
344 bool ShapeEditor::is_over_stroke (NR::Point event_p, bool remember) {
345     if (!this->nodepath)
346         return false; // no stroke in knotholder
348     const SPItem *item = get_item(SH_NODEPATH);
350     //Translate click point into proper coord system
351     this->curvepoint_doc = desktop->w2d(event_p);
352     this->curvepoint_doc *= sp_item_dt2i_affine(item);
354     SPCurve *curve;
355     if (SP_IS_SHAPE(item)) {
356         curve = sp_shape_get_curve(SP_SHAPE(item));
357     } else {
358         curve = this->nodepath->curve;   // not sure if np->curve is always up to date...
359     }
360     Geom::PathVector const &pathv = curve->get_pathvector();
361     Geom::PathVectorPosition pvpos = Geom::nearestPoint(pathv, this->curvepoint_doc);
363     NR::Point nearest = pathv[pvpos.path_nr].pointAt(pvpos.t);
364     NR::Point delta = nearest - this->curvepoint_doc;
366     delta = desktop->d2w(delta);
368     double stroke_tolerance =
369         (( !SP_OBJECT_STYLE(item)->stroke.isNone() ?
370            desktop->current_zoom() *
371            SP_OBJECT_STYLE (item)->stroke_width.computed * 0.5 *
372            NR::expansion(sp_item_i2d_affine(item))
373          : 0.0)
374          + prefs_get_int_attribute_limited("options.dragtolerance", "value", 0, 0, 100)) /NR::expansion(sp_item_i2d_affine(item)); 
375     bool close = (NR::L2 (delta) < stroke_tolerance);
377     if (remember && close) {
378         // calculate index for nodepath's representation.
379         double int_part;
380         double t = std::modf(pvpos.t, &int_part);
381         unsigned int segment_index = (unsigned int)int_part + 1;
382         for (unsigned int i = 0; i < pvpos.path_nr; ++i) {
383             segment_index += pathv[i].size() + 1;
384             if (pathv[i].closed())
385                 segment_index += 1;
386         }
388         this->curvepoint_event[NR::X] = (gint) event_p [NR::X];
389         this->curvepoint_event[NR::Y] = (gint) event_p [NR::Y];
390         this->hit = true;
391         this->grab_t = t;
392         this->grab_node = segment_index;
393     }
395     return close;
398 void ShapeEditor::add_node_near_point() {
399     if (this->nodepath) {
400         sp_nodepath_add_node_near_point(this->nodepath, this->curvepoint_doc);
401     } else if (this->knotholder) {
402         // we do not add nodes in knotholder... yet
403     }
406 void ShapeEditor::select_segment_near_point(bool toggle) {
407     if (this->nodepath) {
408         sp_nodepath_select_segment_near_point(this->nodepath, this->curvepoint_doc, toggle);
409     }
410     if (this->knotholder) {
411         // we do not select segments in knotholder... yet?
412     }
415 void ShapeEditor::cancel_hit() {
416     this->hit = false;
419 bool ShapeEditor::hits_curve() {
420     return (this->hit);
424 void ShapeEditor::curve_drag(gdouble eventx, gdouble eventy) {
425     if (this->nodepath && !this->nodepath->straight_path) {
427         if (this->grab_node == -1) // don't know which segment to drag
428             return;
430         // We round off the extra precision in the motion coordinates provided
431         // by some input devices (like tablets). As we'll store the coordinates
432         // as integers in curvepoint_event we need to do this rounding before
433         // comparing them with the last coordinates from curvepoint_event.
434         // See bug #1593499 for details.
436         gint x = (gint) Inkscape::round(eventx);
437         gint y = (gint) Inkscape::round(eventy);
440         // The coordinates hasn't changed since the last motion event, abort
441         if (this->curvepoint_event[NR::X] == x &&
442             this->curvepoint_event[NR::Y] == y)
443             return;
445         NR::Point const delta_w(eventx - this->curvepoint_event[NR::X],
446                                 eventy - this->curvepoint_event[NR::Y]);
447         NR::Point const delta_dt(this->desktop->w2d(delta_w));
449         sp_nodepath_curve_drag (this->nodepath, this->grab_node, this->grab_t, delta_dt);
450         this->curvepoint_event[NR::X] = x;
451         this->curvepoint_event[NR::Y] = y;
453     }
454     if (this->knotholder) {
455         // we do not drag curve in knotholder
456     }
460 void ShapeEditor::finish_drag() {
461     if (this->nodepath && this->hit) {
462         sp_nodepath_update_repr (this->nodepath, _("Drag curve"));
463     }
466 void ShapeEditor::select_rect(NR::Rect const &rect, bool add) {
467     if (this->nodepath) {
468         sp_nodepath_select_rect(this->nodepath, rect, add);
469     }
472 bool ShapeEditor::has_selection() {
473     if (this->nodepath)
474         return this->nodepath->selected;
475     return false; //  so far, knotholder cannot have selection
478 void ShapeEditor::deselect() {
479     if (this->nodepath)
480         sp_nodepath_deselect(this->nodepath);
483 void ShapeEditor::add_node () {
484     sp_node_selected_add_node(this->nodepath);
487 void ShapeEditor::delete_nodes () {
488     sp_node_selected_delete(this->nodepath);
491 void ShapeEditor::delete_nodes_preserving_shape () {
492     if (this->nodepath && this->nodepath->selected) {
493         sp_node_delete_preserve(g_list_copy(this->nodepath->selected));
494     }
497 void ShapeEditor::delete_segment () {
498     sp_node_selected_delete_segment(this->nodepath);
501 void ShapeEditor::set_node_type(int type) {
502     sp_node_selected_set_type(this->nodepath, (Inkscape::NodePath::NodeType) type);
505 void ShapeEditor::break_at_nodes() {
506     sp_node_selected_break(this->nodepath);
509 void ShapeEditor::join_nodes() {
510     sp_node_selected_join(this->nodepath);
513 void ShapeEditor::join_segments() {
514     sp_node_selected_join_segment(this->nodepath);
517 void ShapeEditor::duplicate_nodes() {
518     sp_node_selected_duplicate(this->nodepath);
521 void ShapeEditor::set_type_of_segments(NRPathcode code) {
522     sp_node_selected_set_line_type(this->nodepath, code);
525 void ShapeEditor::move_nodes_screen(SPDesktop *desktop, gdouble dx, gdouble dy) {
526     sp_node_selected_move_screen(desktop, this->nodepath, dx, dy);
528 void ShapeEditor::move_nodes(gdouble dx, gdouble dy) {
529     sp_node_selected_move(this->nodepath, dx, dy);
532 void ShapeEditor::rotate_nodes(gdouble angle, int which, bool screen) {
533     if (this->nodepath)
534         sp_nodepath_selected_nodes_rotate (this->nodepath, angle, which, screen);
537 void ShapeEditor::scale_nodes(gdouble const grow, int const which) {
538     sp_nodepath_selected_nodes_scale(this->nodepath, grow, which);
540 void ShapeEditor::scale_nodes_screen(gdouble const grow, int const which) {
541     sp_nodepath_selected_nodes_scale_screen(this->nodepath, grow, which);
544 void ShapeEditor::select_all (bool invert) {
545     if (this->nodepath)
546         sp_nodepath_select_all (this->nodepath, invert);
548 void ShapeEditor::select_all_from_subpath (bool invert) {
549     if (this->nodepath)
550         sp_nodepath_select_all_from_subpath (this->nodepath, invert);
552 void ShapeEditor::select_next () {
553     if (this->nodepath) {
554         sp_nodepath_select_next (this->nodepath);
555         if (this->nodepath->numSelected() >= 1) {
556             this->desktop->scroll_to_point(&(this->nodepath->singleSelectedCoords()), 1.0);
557         }
558     }
560 void ShapeEditor::select_prev () {
561     if (this->nodepath) {
562         sp_nodepath_select_prev (this->nodepath);
563         if (this->nodepath->numSelected() >= 1) {
564             this->desktop->scroll_to_point(&(this->nodepath->singleSelectedCoords()), 1.0);
565         }
566     }
569 void ShapeEditor::show_handles (bool show) {
570     if (this->nodepath && !this->nodepath->straight_path)
571         sp_nodepath_show_handles (this->nodepath, show);
574 void ShapeEditor::show_helperpath (bool show) {
575     if (this->nodepath)
576         sp_nodepath_show_helperpath (this->nodepath, show);
579 void ShapeEditor::flip (NR::Dim2 axis, boost::optional<NR::Point> center) {
580     if (this->nodepath)
581         sp_nodepath_flip (this->nodepath, axis, center);
584 void ShapeEditor::distribute (NR::Dim2 axis) {
585     if (this->nodepath)
586         sp_nodepath_selected_distribute (this->nodepath, axis);
588 void ShapeEditor::align (NR::Dim2 axis) {
589     if (this->nodepath)
590         sp_nodepath_selected_align (this->nodepath, axis);
594 /*
595   Local Variables:
596   mode:c++
597   c-file-style:"stroustrup"
598   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
599   indent-tabs-mode:nil
600   fill-column:99
601   End:
602 */
603 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :