Code

Many debugging messages
[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     g_print ("ShapeEditor::unset_item()\n");
66     Inkscape::XML::Node *old_repr = NULL;
68     switch (type) {
69         case SH_NODEPATH:
70             if (this->nodepath) {
71                 old_repr = this->nodepath->repr;
72                 sp_repr_remove_listener_by_data(old_repr, this);
73                 Inkscape::GC::release(old_repr);
75                 this->grab_node = -1;
76                 sp_nodepath_destroy(this->nodepath);
77                 this->nodepath = NULL;
78             }
79             break;
80         case SH_KNOTHOLDER:
81             if (this->knotholder) {
82                 old_repr = this->knotholder->repr;
83                 sp_repr_remove_listener_by_data(old_repr, this);
84                 Inkscape::GC::release(old_repr);
86                 if (!keep_knotholder) {
87                     delete this->knotholder;
88                     this->knotholder = NULL;
89                 }
90             }
91             break;
92     }
93 }
95 bool ShapeEditor::has_nodepath () {
96     return (this->nodepath != NULL);
97 }
99 bool ShapeEditor::has_knotholder () {
100     return (this->knotholder != NULL);
103 void ShapeEditor::update_knotholder () {
104     if (this->knotholder)
105         this->knotholder->update_knots();
108 bool ShapeEditor::has_local_change (SubType type) {
109     switch (type) {
110         case SH_NODEPATH:
111             return (this->nodepath && this->nodepath->local_change);
112         case SH_KNOTHOLDER:
113             return (this->knotholder && this->knotholder->local_change != 0);
114         default:
115             g_assert_not_reached();
116     }
119 void ShapeEditor::decrement_local_change (SubType type) {
120     switch (type) {
121         case SH_NODEPATH:
122             if (this->nodepath && this->nodepath->local_change > 0) {
123                 this->nodepath->local_change--;
124             }
125             break;
126         case SH_KNOTHOLDER:
127             if (this->knotholder) {
128                 this->knotholder->local_change = FALSE;
129             }
130             break;
131         default:
132             g_assert_not_reached();
133     }
136 const SPItem *ShapeEditor::get_item (SubType type) {
137     const SPItem *item = NULL;
138     switch (type) {
139         case SH_NODEPATH:
140             if (this->has_nodepath()) {
141                 item = this->nodepath->item;
142             }
143             break;
144         case SH_KNOTHOLDER:
145             if (this->has_knotholder()) {
146                 item = this->knotholder->getItem();
147             }
148             break;
149     }
150     return item;
153 GList *ShapeEditor::save_nodepath_selection () {
154     if (this->nodepath)
155         return ::save_nodepath_selection (this->nodepath);
156     return NULL;
159 void ShapeEditor::restore_nodepath_selection (GList *saved) {
160     if (this->nodepath && saved)
161         ::restore_nodepath_selection (this->nodepath, saved);
164 bool ShapeEditor::nodepath_edits_repr_key(gchar const *name) {
165     if (nodepath && name) {
166         return ( !strcmp(name, nodepath->repr_key) || !strcmp(name, nodepath->repr_nodetypes_key) );
167     }
169     return false;
172 static void shapeeditor_event_attr_changed(Inkscape::XML::Node */*repr*/, gchar const *name,
173                                            gchar const */*old_value*/, gchar const */*new_value*/,
174                                            bool /*is_interactive*/, gpointer data)
176     g_print ("shapeeditor_event_attr_changed()\n");
177     gboolean changed_np = FALSE;
178     gboolean changed_kh = FALSE;
180     g_assert(data);
181     ShapeEditor *sh = ((ShapeEditor *) data);
183     if (sh->has_nodepath() && sh->nodepath_edits_repr_key(name))
184     {
185         changed_np = !sh->has_local_change(SH_NODEPATH);
186         sh->decrement_local_change(SH_NODEPATH);
188     }
190     if (changed_np) {
191         GList *saved = NULL;
192         if (sh->has_nodepath()) {
193             saved = sh->save_nodepath_selection();
194         }
196         sh->reset_item(SH_NODEPATH);
198         if (sh->has_nodepath() && saved) {
199             sh->restore_nodepath_selection(saved);
200             g_list_free (saved);
201         }
202     }
205     if (sh->has_knotholder())
206     {
207         changed_kh = !sh->has_local_change(SH_KNOTHOLDER);
208         sh->decrement_local_change(SH_KNOTHOLDER);
209         if (changed_kh) {
210             // this can happen if an LPEItem's knotholder handle was dragged, in which case we want
211             // to keep the knotholder; in all other cases (e.g., if the LPE itself changes) we delete it
212             sh->reset_item(SH_KNOTHOLDER, !strcmp(name, "d"));
213         }
214     }
216     sh->update_statusbar(); //TODO: sh->get_container()->update_statusbar();
219 static Inkscape::XML::NodeEventVector shapeeditor_repr_events = {
220     NULL, /* child_added */
221     NULL, /* child_removed */
222     shapeeditor_event_attr_changed,
223     NULL, /* content_changed */
224     NULL  /* order_changed */
225 };
228 void ShapeEditor::set_item(SPItem *item, SubType type, bool keep_knotholder) {
229     g_print ("ShapeEditor::set_item()\n");
230     // this happens (and should only happen) when for an LPEItem having both knotholder and nodepath the knotholder
231     // is adapted; in this case we don't want to delete the knotholder since this freezes the handles
232     unset_item(type, keep_knotholder);
234     this->grab_node = -1;
236     if (item) {
237         Inkscape::XML::Node *repr;
238         switch(type) {
239             case SH_NODEPATH:
240                 if (SP_IS_LPE_ITEM(item)) {
241                     this->nodepath = sp_nodepath_new(desktop, item, (prefs_get_int_attribute("tools.nodes", "show_handles", 1) != 0));
242                 }
243                 if (this->nodepath) {
244                     this->nodepath->shape_editor = this;
246                     // setting new listener
247                     repr = SP_OBJECT_REPR(item);
248                     Inkscape::GC::anchor(repr);
249                     sp_repr_add_listener(repr, &shapeeditor_repr_events, this);
250                 }
251                 break;
253             case SH_KNOTHOLDER:
254                 if (!this->knotholder) {
255                     // only recreate knotholder if none is present
256                     this->knotholder = sp_item_knot_holder(item, desktop);
257                 }
258                 if (this->knotholder) {
259                     this->knotholder->update_knots();
260                     // setting new listener
261                     repr = this->knotholder->repr;
262                     Inkscape::GC::anchor(repr);
263                     sp_repr_add_listener(repr, &shapeeditor_repr_events, this);
264                 }
265                 break;
266         }
267     }
270 /** Please note that this function only works for path parameters.
271 *  All other parameters probably will crash Inkscape!
272 */
273 void ShapeEditor::set_item_lpe_path_parameter(SPItem *item, SPObject *lpeobject, const char * key)
275     g_print ("ShapeEditor::set_item_lpe_path_parameter()\n");
276     unset_item(SH_NODEPATH);
278     this->grab_node = -1;
280     if (lpeobject) {
281         this->nodepath = sp_nodepath_new( desktop, lpeobject,
282                                           (prefs_get_int_attribute("tools.nodes", "show_handles", 1) != 0),
283                                           key, item);
284         if (this->nodepath) {
285             this->nodepath->shape_editor = this;
287             // setting new listener
288             Inkscape::XML::Node *repr = SP_OBJECT_REPR(lpeobject);
289             if (repr) {
290                 Inkscape::GC::anchor(repr);
291                 sp_repr_add_listener(repr, &shapeeditor_repr_events, this);
292             }
293         }
294     }
297 /** 
298 *  pass a new knotholder to ShapeEditor to manage (and delete)
299 */
300 void
301 ShapeEditor::set_knotholder(KnotHolder * knot_holder)
303     unset_item(SH_KNOTHOLDER);
305     this->grab_node = -1;
307     if (knot_holder) {
308         this->knotholder = knot_holder;
309     }
313 /** FIXME: think about this. Is this thing only called when the item needs to be updated?
314    Why not make a reload function in NodePath and in KnotHolder? */
315 void ShapeEditor::reset_item (SubType type, bool keep_knotholder)
317     g_print ("ShapeEditor::reset_item()\n");
318     switch (type) {
319         case SH_NODEPATH:
320             if ( (this->nodepath) && (IS_LIVEPATHEFFECT(this->nodepath->object)) ) {
321                 SPItem * item = this->nodepath->item;
322                 SPObject *obj = this->nodepath->object;
323                 char * key = g_strdup(this->nodepath->repr_key);
324                 set_item_lpe_path_parameter(item, obj, key); // the above checks for nodepath, so it is indeed a path that we are editing
325                 g_free(key);
326             } else {
327                 SPItem * item = (SPItem *) get_item(SH_NODEPATH);
328                 set_item(item, SH_NODEPATH);
329             }                
330             break;
331         case SH_KNOTHOLDER:
332             if (this->knotholder) {
333                 SPItem * item = (SPItem *) get_item(SH_KNOTHOLDER);
334                 set_item(item, SH_KNOTHOLDER, keep_knotholder);
335             }
336             break;
337     }
340 void ShapeEditor::nodepath_destroyed () {
341     this->nodepath = NULL;
344 void ShapeEditor::update_statusbar () {
345     if (this->nodepath)
346         sp_nodepath_update_statusbar(this->nodepath);
349 bool ShapeEditor::is_over_stroke (NR::Point event_p, bool remember) {
350     g_print ("ShapeEditor::is_over_stroke ()");
351     if (!this->nodepath)
352         return false; // no stroke in knotholder
354     const SPItem *item = get_item(SH_NODEPATH);
356     //Translate click point into proper coord system
357     this->curvepoint_doc = desktop->w2d(event_p);
358     this->curvepoint_doc *= sp_item_dt2i_affine(item);
360     SPCurve *curve;
361     if (SP_IS_SHAPE(item)) {
362         g_print ("; getting curve from shape\n");
363         curve = sp_shape_get_curve(SP_SHAPE(item));
364     } else {
365         g_print ("; getting curve from nodepath's curve\n");
366         this->nodepath->curve;   // not sure if np->curve is always up to date...
367     }
368     Geom::PathVector const &pathv = curve->get_pathvector();
369     Geom::PathVectorPosition pvpos = Geom::nearestPoint(pathv, this->curvepoint_doc);
371     NR::Point nearest = pathv[pvpos.path_nr].pointAt(pvpos.t);
372     NR::Point delta = nearest - this->curvepoint_doc;
374     delta = desktop->d2w(delta);
376     double stroke_tolerance =
377         (( !SP_OBJECT_STYLE(item)->stroke.isNone() ?
378            desktop->current_zoom() *
379            SP_OBJECT_STYLE (item)->stroke_width.computed * 0.5 *
380            NR::expansion(sp_item_i2d_affine(item))
381          : 0.0)
382          + prefs_get_int_attribute_limited("options.dragtolerance", "value", 0, 0, 100)) /NR::expansion(sp_item_i2d_affine(item)); 
383     bool close = (NR::L2 (delta) < stroke_tolerance);
385     if (remember && close) {
386         // calculate index for nodepath's representation.
387         double int_part;
388         double t = std::modf(pvpos.t, &int_part);
389         unsigned int segment_index = (unsigned int)int_part + 1;
390         for (unsigned int i = 0; i < pvpos.path_nr; ++i) {
391             segment_index += pathv[i].size() + 1;
392             if (pathv[i].closed())
393                 segment_index += 1;
394         }
396         this->curvepoint_event[NR::X] = (gint) event_p [NR::X];
397         this->curvepoint_event[NR::Y] = (gint) event_p [NR::Y];
398         this->hit = true;
399         this->grab_t = t;
400         this->grab_node = segment_index;
401     }
403     return close;
406 void ShapeEditor::add_node_near_point() {
407     if (this->nodepath) {
408         sp_nodepath_add_node_near_point(this->nodepath, this->curvepoint_doc);
409     } else if (this->knotholder) {
410         // we do not add nodes in knotholder... yet
411     }
414 void ShapeEditor::select_segment_near_point(bool toggle) {
415     if (this->nodepath) {
416         sp_nodepath_select_segment_near_point(this->nodepath, this->curvepoint_doc, toggle);
417     }
418     if (this->knotholder) {
419         // we do not select segments in knotholder... yet?
420     }
423 void ShapeEditor::cancel_hit() {
424     this->hit = false;
427 bool ShapeEditor::hits_curve() {
428     return (this->hit);
432 void ShapeEditor::curve_drag(gdouble eventx, gdouble eventy) {
433     if (this->nodepath && !this->nodepath->straight_path) {
435         if (this->grab_node == -1) // don't know which segment to drag
436             return;
438         // We round off the extra precision in the motion coordinates provided
439         // by some input devices (like tablets). As we'll store the coordinates
440         // as integers in curvepoint_event we need to do this rounding before
441         // comparing them with the last coordinates from curvepoint_event.
442         // See bug #1593499 for details.
444         gint x = (gint) Inkscape::round(eventx);
445         gint y = (gint) Inkscape::round(eventy);
448         // The coordinates hasn't changed since the last motion event, abort
449         if (this->curvepoint_event[NR::X] == x &&
450             this->curvepoint_event[NR::Y] == y)
451             return;
453         NR::Point const delta_w(eventx - this->curvepoint_event[NR::X],
454                                 eventy - this->curvepoint_event[NR::Y]);
455         NR::Point const delta_dt(this->desktop->w2d(delta_w));
457         sp_nodepath_curve_drag (this->nodepath, this->grab_node, this->grab_t, delta_dt);
458         this->curvepoint_event[NR::X] = x;
459         this->curvepoint_event[NR::Y] = y;
461     }
462     if (this->knotholder) {
463         // we do not drag curve in knotholder
464     }
468 void ShapeEditor::finish_drag() {
469     if (this->nodepath && this->hit) {
470         sp_nodepath_update_repr (this->nodepath, _("Drag curve"));
471     }
474 void ShapeEditor::select_rect(NR::Rect const &rect, bool add) {
475     if (this->nodepath) {
476         sp_nodepath_select_rect(this->nodepath, rect, add);
477     }
480 bool ShapeEditor::has_selection() {
481     if (this->nodepath)
482         return this->nodepath->selected;
483     return false; //  so far, knotholder cannot have selection
486 void ShapeEditor::deselect() {
487     if (this->nodepath)
488         sp_nodepath_deselect(this->nodepath);
491 void ShapeEditor::add_node () {
492     sp_node_selected_add_node(this->nodepath);
495 void ShapeEditor::delete_nodes () {
496     sp_node_selected_delete(this->nodepath);
499 void ShapeEditor::delete_nodes_preserving_shape () {
500     if (this->nodepath && this->nodepath->selected) {
501         sp_node_delete_preserve(g_list_copy(this->nodepath->selected));
502     }
505 void ShapeEditor::delete_segment () {
506     sp_node_selected_delete_segment(this->nodepath);
509 void ShapeEditor::set_node_type(int type) {
510     sp_node_selected_set_type(this->nodepath, (Inkscape::NodePath::NodeType) type);
513 void ShapeEditor::break_at_nodes() {
514     sp_node_selected_break(this->nodepath);
517 void ShapeEditor::join_nodes() {
518     sp_node_selected_join(this->nodepath);
521 void ShapeEditor::join_segments() {
522     sp_node_selected_join_segment(this->nodepath);
525 void ShapeEditor::duplicate_nodes() {
526     sp_node_selected_duplicate(this->nodepath);
529 void ShapeEditor::set_type_of_segments(NRPathcode code) {
530     sp_node_selected_set_line_type(this->nodepath, code);
533 void ShapeEditor::move_nodes_screen(SPDesktop *desktop, gdouble dx, gdouble dy) {
534     sp_node_selected_move_screen(desktop, this->nodepath, dx, dy);
536 void ShapeEditor::move_nodes(gdouble dx, gdouble dy) {
537     sp_node_selected_move(this->nodepath, dx, dy);
540 void ShapeEditor::rotate_nodes(gdouble angle, int which, bool screen) {
541     if (this->nodepath)
542         sp_nodepath_selected_nodes_rotate (this->nodepath, angle, which, screen);
545 void ShapeEditor::scale_nodes(gdouble const grow, int const which) {
546     sp_nodepath_selected_nodes_scale(this->nodepath, grow, which);
548 void ShapeEditor::scale_nodes_screen(gdouble const grow, int const which) {
549     sp_nodepath_selected_nodes_scale_screen(this->nodepath, grow, which);
552 void ShapeEditor::select_all (bool invert) {
553     if (this->nodepath)
554         sp_nodepath_select_all (this->nodepath, invert);
556 void ShapeEditor::select_all_from_subpath (bool invert) {
557     if (this->nodepath)
558         sp_nodepath_select_all_from_subpath (this->nodepath, invert);
560 void ShapeEditor::select_next () {
561     if (this->nodepath) {
562         sp_nodepath_select_next (this->nodepath);
563         if (this->nodepath->numSelected() >= 1) {
564             this->desktop->scroll_to_point(&(this->nodepath->singleSelectedCoords()), 1.0);
565         }
566     }
568 void ShapeEditor::select_prev () {
569     if (this->nodepath) {
570         sp_nodepath_select_prev (this->nodepath);
571         if (this->nodepath->numSelected() >= 1) {
572             this->desktop->scroll_to_point(&(this->nodepath->singleSelectedCoords()), 1.0);
573         }
574     }
577 void ShapeEditor::show_handles (bool show) {
578     if (this->nodepath && !this->nodepath->straight_path)
579         sp_nodepath_show_handles (this->nodepath, show);
582 void ShapeEditor::show_helperpath (bool show) {
583     if (this->nodepath)
584         sp_nodepath_show_helperpath (this->nodepath, show);
587 void ShapeEditor::flip (NR::Dim2 axis, boost::optional<NR::Point> center) {
588     if (this->nodepath)
589         sp_nodepath_flip (this->nodepath, axis, center);
592 void ShapeEditor::distribute (NR::Dim2 axis) {
593     if (this->nodepath)
594         sp_nodepath_selected_distribute (this->nodepath, axis);
596 void ShapeEditor::align (NR::Dim2 axis) {
597     if (this->nodepath)
598         sp_nodepath_selected_align (this->nodepath, axis);
602 /*
603   Local Variables:
604   mode:c++
605   c-file-style:"stroustrup"
606   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
607   indent-tabs-mode:nil
608   fill-column:99
609   End:
610 */
611 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :