Code

in shapeeditor to determine whether tool is above the path, use 2geom methods instead...
[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 "style.h"
31 #include "display/curve.h"
32 #include <2geom/pathvector.h>
34 #include "shape-editor.h"
37 ShapeEditorsCollective::ShapeEditorsCollective(SPDesktop */*dt*/) {
38 }
40 ShapeEditorsCollective::~ShapeEditorsCollective() {
41 }
44 void ShapeEditorsCollective::update_statusbar() {
46 //!!! move from nodepath: sp_nodepath_update_statusbar but summing for all nodepaths
48 }
50 ShapeEditor::ShapeEditor(SPDesktop *dt) {
51     this->desktop = dt;
52     this->grab_node = -1;
53     this->nodepath = NULL;
54     this->knotholder = NULL;
55     this->hit = false;
56 }
58 ShapeEditor::~ShapeEditor() {
59     unset_item(SH_KNOTHOLDER);
60     unset_item(SH_NODEPATH);
61 }
63 void ShapeEditor::unset_item(SubType type, bool keep_knotholder) {
64     Inkscape::XML::Node *old_repr = NULL;
66     switch (type) {
67         case SH_NODEPATH:
68             if (this->nodepath) {
69                 old_repr = this->nodepath->repr;
70                 sp_repr_remove_listener_by_data(old_repr, this);
71                 Inkscape::GC::release(old_repr);
73                 this->grab_node = -1;
74                 sp_nodepath_destroy(this->nodepath);
75                 this->nodepath = NULL;
76             }
77             break;
78         case SH_KNOTHOLDER:
79             if (this->knotholder) {
80                 old_repr = this->knotholder->repr;
81                 sp_repr_remove_listener_by_data(old_repr, this);
82                 Inkscape::GC::release(old_repr);
84                 if (!keep_knotholder) {
85                     delete this->knotholder;
86                     this->knotholder = NULL;
87                 }
88             }
89             break;
90     }
91 }
93 bool ShapeEditor::has_nodepath () {
94     return (this->nodepath != NULL);
95 }
97 bool ShapeEditor::has_knotholder () {
98     return (this->knotholder != NULL);
99 }
101 void ShapeEditor::update_knotholder () {
102     if (this->knotholder)
103         this->knotholder->update_knots();
106 bool ShapeEditor::has_local_change (SubType type) {
107     switch (type) {
108         case SH_NODEPATH:
109             return (this->nodepath && this->nodepath->local_change);
110         case SH_KNOTHOLDER:
111             return (this->knotholder && this->knotholder->local_change != 0);
112         default:
113             g_assert_not_reached();
114     }
117 void ShapeEditor::decrement_local_change (SubType type) {
118     switch (type) {
119         case SH_NODEPATH:
120             if (this->nodepath && this->nodepath->local_change > 0) {
121                 this->nodepath->local_change--;
122             }
123             break;
124         case SH_KNOTHOLDER:
125             if (this->knotholder) {
126                 this->knotholder->local_change = FALSE;
127             }
128             break;
129         default:
130             g_assert_not_reached();
131     }
134 const SPItem *ShapeEditor::get_item (SubType type) {
135     const SPItem *item = NULL;
136     switch (type) {
137         case SH_NODEPATH:
138             if (this->has_nodepath()) {
139                 item = this->nodepath->item;
140             }
141             break;
142         case SH_KNOTHOLDER:
143             if (this->has_knotholder()) {
144                 item = this->knotholder->getItem();
145             }
146             break;
147     }
148     return item;
151 GList *ShapeEditor::save_nodepath_selection () {
152     if (this->nodepath)
153         return ::save_nodepath_selection (this->nodepath);
154     return NULL;
157 void ShapeEditor::restore_nodepath_selection (GList *saved) {
158     if (this->nodepath && saved)
159         ::restore_nodepath_selection (this->nodepath, saved);
162 bool ShapeEditor::nodepath_edits_repr_key(gchar const *name) {
163     if (nodepath && name) {
164         return ( !strcmp(name, nodepath->repr_key) || !strcmp(name, nodepath->repr_nodetypes_key) );
165     }
167     return false;
170 static void shapeeditor_event_attr_changed(Inkscape::XML::Node */*repr*/, gchar const *name,
171                                            gchar const */*old_value*/, gchar const */*new_value*/,
172                                            bool /*is_interactive*/, gpointer data)
174     gboolean changed_np = FALSE;
175     gboolean changed_kh = FALSE;
177     g_assert(data);
178     ShapeEditor *sh = ((ShapeEditor *) data);
180     if (sh->has_nodepath() && sh->nodepath_edits_repr_key(name))
181     {
182         changed_np = !sh->has_local_change(SH_NODEPATH);
183         sh->decrement_local_change(SH_NODEPATH);
185     }
187     if (changed_np) {
188         GList *saved = NULL;
189         if (sh->has_nodepath()) {
190             saved = sh->save_nodepath_selection();
191         }
193         sh->reset_item(SH_NODEPATH);
195         if (sh->has_nodepath() && saved) {
196             sh->restore_nodepath_selection(saved);
197             g_list_free (saved);
198         }
199     }
202     if (sh->has_knotholder())
203     {
204         changed_kh = !sh->has_local_change(SH_KNOTHOLDER);
205         sh->decrement_local_change(SH_KNOTHOLDER);
206         if (changed_kh) {
207             // this can happen if an LPEItem's knotholder handle was dragged, in which case we want
208             // to keep the knotholder; in all other cases (e.g., if the LPE itself changes) we delete it
209             sh->reset_item(SH_KNOTHOLDER, !strcmp(name, "d"));
210         }
211     }
213     sh->update_statusbar(); //TODO: sh->get_container()->update_statusbar();
216 static Inkscape::XML::NodeEventVector shapeeditor_repr_events = {
217     NULL, /* child_added */
218     NULL, /* child_removed */
219     shapeeditor_event_attr_changed,
220     NULL, /* content_changed */
221     NULL  /* order_changed */
222 };
225 void ShapeEditor::set_item(SPItem *item, SubType type, bool keep_knotholder) {
226     // this happens (and should only happen) when for an LPEItem having both knotholder and nodepath the knotholder
227     // is adapted; in this case we don't want to delete the knotholder since this freezes the handles
228     unset_item(type, keep_knotholder);
230     this->grab_node = -1;
232     if (item) {
233         Inkscape::XML::Node *repr;
234         switch(type) {
235             case SH_NODEPATH:
236                 if (SP_IS_LPE_ITEM(item)) {
237                     //SPLPEItem *lpeitem = SP_LPE_ITEM(item);
238                     //Inkscape::LivePathEffect::Effect *lpe = sp_lpe_item_get_current_lpe(lpeitem);
239                     //if (!(lpe && lpe->isVisible() && lpe->providesKnotholder())) {
240                         // only create nodepath if the item either doesn't have an LPE
241                         // or the LPE is invisible or it doesn't provide a knotholder itself
242                         this->nodepath = sp_nodepath_new(desktop, item,
243                                                          (prefs_get_int_attribute("tools.nodes", "show_handles", 1) != 0));
244                     //} else if (lpe && lpe->isVisible()) {
245                     //    sp_lpe_item_add_temporary_canvasitems(lpeitem, desktop);
246                     //}
247                 }
248                 if (this->nodepath) {
249                     this->nodepath->shape_editor = this;
251                     // setting new listener
252                     repr = SP_OBJECT_REPR(item);
253                     Inkscape::GC::anchor(repr);
254                     sp_repr_add_listener(repr, &shapeeditor_repr_events, this);
255                 }
256                 break;
258             case SH_KNOTHOLDER:
259                 if (!this->knotholder) {
260                     // only recreate knotholder if none is present
261                     this->knotholder = sp_item_knot_holder(item, desktop);
262                 }
263                 if (this->knotholder) {
264                     this->knotholder->update_knots();
265                     // setting new listener
266                     repr = this->knotholder->repr;
267                     Inkscape::GC::anchor(repr);
268                     sp_repr_add_listener(repr, &shapeeditor_repr_events, this);
269                 }
270                 break;
271         }
272     }
275 /** Please note that this function only works for path parameters.
276 *  All other parameters probably will crash Inkscape!
277 */
278 void ShapeEditor::set_item_lpe_path_parameter(SPItem *item, SPObject *lpeobject, const char * key)
280     unset_item(SH_NODEPATH);
282     this->grab_node = -1;
284     if (lpeobject) {
285         this->nodepath = sp_nodepath_new( desktop, lpeobject,
286                                           (prefs_get_int_attribute("tools.nodes", "show_handles", 1) != 0),
287                                           key, item);
288         if (this->nodepath) {
289             this->nodepath->shape_editor = this;
291             // setting new listener
292             Inkscape::XML::Node *repr = SP_OBJECT_REPR(lpeobject);
293             if (repr) {
294                 Inkscape::GC::anchor(repr);
295                 sp_repr_add_listener(repr, &shapeeditor_repr_events, this);
296             }
297         }
298     }
301 /** 
302 *  pass a new knotholder to ShapeEditor to manage (and delete)
303 */
304 void
305 ShapeEditor::set_knotholder(KnotHolder * knot_holder)
307     unset_item(SH_KNOTHOLDER);
309     this->grab_node = -1;
311     if (knot_holder) {
312         this->knotholder = knot_holder;
313     }
317 /** FIXME: think about this. Is this thing only called when the item needs to be updated?
318    Why not make a reload function in NodePath and in KnotHolder? */
319 void ShapeEditor::reset_item (SubType type, bool keep_knotholder)
321     switch (type) {
322         case SH_NODEPATH:
323             if ( (this->nodepath) && (IS_LIVEPATHEFFECT(this->nodepath->object)) ) {
324                 SPItem * item = this->nodepath->item;
325                 SPObject *obj = this->nodepath->object;
326                 char * key = g_strdup(this->nodepath->repr_key);
327                 set_item_lpe_path_parameter(item, obj, key); // the above checks for nodepath, so it is indeed a path that we are editing
328                 g_free(key);
329             } else {
330                 SPItem * item = (SPItem *) get_item(SH_NODEPATH);
331                 set_item(item, SH_NODEPATH);
332             }                
333             break;
334         case SH_KNOTHOLDER:
335             if (this->knotholder) {
336                 SPItem * item = (SPItem *) get_item(SH_KNOTHOLDER);
337                 set_item(item, SH_KNOTHOLDER, keep_knotholder);
338             }
339             break;
340     }
343 void ShapeEditor::nodepath_destroyed () {
344     this->nodepath = NULL;
347 void ShapeEditor::update_statusbar () {
348     if (this->nodepath)
349         sp_nodepath_update_statusbar(this->nodepath);
352 bool ShapeEditor::is_over_stroke (NR::Point event_p, bool remember) {
354     if (!this->nodepath)
355         return false; // no stroke in knotholder
357     const SPItem *item = get_item(SH_NODEPATH);
359     //Translate click point into proper coord system
360     this->curvepoint_doc = desktop->w2d(event_p);
361     this->curvepoint_doc *= from_2geom(sp_item_dt2i_affine(item));
363     SPCurve *curve = this->nodepath->curve;   // not sure if np->curve is always up to date...
364     Geom::PathVector const &pathv = curve->get_pathvector();
365     Geom::PathVectorPosition pvpos = Geom::nearestPoint(pathv, this->curvepoint_doc);
367     NR::Point nearest = pathv[pvpos.path_nr].pointAt(pvpos.t);
368     NR::Point delta = nearest - this->curvepoint_doc;
370     delta = desktop->d2w(delta);
372     double stroke_tolerance =
373         (( !SP_OBJECT_STYLE(item)->stroke.isNone() ?
374            desktop->current_zoom() *
375            SP_OBJECT_STYLE (item)->stroke_width.computed * 0.5 *
376            NR::expansion(from_2geom(sp_item_i2d_affine(item)))
377          : 0.0)
378          + prefs_get_int_attribute_limited("options.dragtolerance", "value", 0, 0, 100)) /NR::expansion(from_2geom(sp_item_i2d_affine(item))); 
379     bool close = (NR::L2 (delta) < stroke_tolerance);
381     if (remember && close) {
382         // calculate index for nodepath's representation.
383         double int_part;
384         double t = std::modf(pvpos.t, &int_part);
385         unsigned int segment_index = (unsigned int)int_part + 1;
386         for (unsigned int i = 0; i < pvpos.path_nr; ++i) {
387             segment_index += pathv[i].size() + 1;
388             if (pathv[i].closed())
389                 segment_index += 1;
390         }
392         this->curvepoint_event[NR::X] = (gint) event_p [NR::X];
393         this->curvepoint_event[NR::Y] = (gint) event_p [NR::Y];
394         this->hit = true;
395         this->grab_t = t;
396         this->grab_node = segment_index;
397     }
399     return close;
402 void ShapeEditor::add_node_near_point() {
403     if (this->nodepath) {
404         sp_nodepath_add_node_near_point(this->nodepath, this->curvepoint_doc);
405     } else if (this->knotholder) {
406         // we do not add nodes in knotholder... yet
407     }
410 void ShapeEditor::select_segment_near_point(bool toggle) {
411     if (this->nodepath) {
412         sp_nodepath_select_segment_near_point(this->nodepath, this->curvepoint_doc, toggle);
413     }
414     if (this->knotholder) {
415         // we do not select segments in knotholder... yet?
416     }
419 void ShapeEditor::cancel_hit() {
420     this->hit = false;
423 bool ShapeEditor::hits_curve() {
424     return (this->hit);
428 void ShapeEditor::curve_drag(gdouble eventx, gdouble eventy) {
429     if (this->nodepath && !this->nodepath->straight_path) {
431         if (this->grab_node == -1) // don't know which segment to drag
432             return;
434         // We round off the extra precision in the motion coordinates provided
435         // by some input devices (like tablets). As we'll store the coordinates
436         // as integers in curvepoint_event we need to do this rounding before
437         // comparing them with the last coordinates from curvepoint_event.
438         // See bug #1593499 for details.
440         gint x = (gint) Inkscape::round(eventx);
441         gint y = (gint) Inkscape::round(eventy);
444         // The coordinates hasn't changed since the last motion event, abort
445         if (this->curvepoint_event[NR::X] == x &&
446             this->curvepoint_event[NR::Y] == y)
447             return;
449         NR::Point const delta_w(eventx - this->curvepoint_event[NR::X],
450                                 eventy - this->curvepoint_event[NR::Y]);
451         NR::Point const delta_dt(this->desktop->w2d(delta_w));
453         sp_nodepath_curve_drag (this->grab_node, this->grab_t, delta_dt); //!!! FIXME: which nodepath?!!! also uses current!!!
454         this->curvepoint_event[NR::X] = x;
455         this->curvepoint_event[NR::Y] = y;
457     }
458     if (this->knotholder) {
459         // we do not drag curve in knotholder
460     }
464 void ShapeEditor::finish_drag() {
465     if (this->nodepath && this->hit) {
466         sp_nodepath_update_repr (this->nodepath, _("Drag curve"));
467     }
470 void ShapeEditor::select_rect(NR::Rect const &rect, bool add) {
471     if (this->nodepath) {
472         sp_nodepath_select_rect(this->nodepath, rect, add);
473     }
476 bool ShapeEditor::has_selection() {
477     if (this->nodepath)
478         return this->nodepath->selected;
479     return false; //  so far, knotholder cannot have selection
482 void ShapeEditor::deselect() {
483     if (this->nodepath)
484         sp_nodepath_deselect(this->nodepath);
487 void ShapeEditor::add_node () {
488     sp_node_selected_add_node(this->nodepath);
491 void ShapeEditor::delete_nodes () {
492     sp_node_selected_delete(this->nodepath);
495 void ShapeEditor::delete_nodes_preserving_shape () {
496     if (this->nodepath && this->nodepath->selected) {
497         sp_node_delete_preserve(g_list_copy(this->nodepath->selected));
498     }
501 void ShapeEditor::delete_segment () {
502     sp_node_selected_delete_segment(this->nodepath);
505 void ShapeEditor::set_node_type(int type) {
506     sp_node_selected_set_type(this->nodepath, (Inkscape::NodePath::NodeType) type);
509 void ShapeEditor::break_at_nodes() {
510     sp_node_selected_break(this->nodepath);
513 void ShapeEditor::join_nodes() {
514     sp_node_selected_join(this->nodepath);
517 void ShapeEditor::join_segments() {
518     sp_node_selected_join_segment(this->nodepath);
521 void ShapeEditor::duplicate_nodes() {
522     sp_node_selected_duplicate(this->nodepath);
525 void ShapeEditor::set_type_of_segments(NRPathcode code) {
526     sp_node_selected_set_line_type(this->nodepath, code);
529 void ShapeEditor::move_nodes_screen(gdouble dx, gdouble dy) {
530     sp_node_selected_move_screen(this->nodepath, dx, dy);
532 void ShapeEditor::move_nodes(gdouble dx, gdouble dy) {
533     sp_node_selected_move(this->nodepath, dx, dy);
536 void ShapeEditor::rotate_nodes(gdouble angle, int which, bool screen) {
537     if (this->nodepath)
538         sp_nodepath_selected_nodes_rotate (this->nodepath, angle, which, screen);
541 void ShapeEditor::scale_nodes(gdouble const grow, int const which) {
542     sp_nodepath_selected_nodes_scale(this->nodepath, grow, which);
544 void ShapeEditor::scale_nodes_screen(gdouble const grow, int const which) {
545     sp_nodepath_selected_nodes_scale_screen(this->nodepath, grow, which);
548 void ShapeEditor::select_all (bool invert) {
549     if (this->nodepath)
550         sp_nodepath_select_all (this->nodepath, invert);
552 void ShapeEditor::select_all_from_subpath (bool invert) {
553     if (this->nodepath)
554         sp_nodepath_select_all_from_subpath (this->nodepath, invert);
556 void ShapeEditor::select_next () {
557     if (this->nodepath) {
558         sp_nodepath_select_next (this->nodepath);
559         if (this->nodepath->numSelected() >= 1) {
560             this->desktop->scroll_to_point(&(this->nodepath->singleSelectedCoords()), 1.0);
561         }
562     }
564 void ShapeEditor::select_prev () {
565     if (this->nodepath) {
566         sp_nodepath_select_prev (this->nodepath);
567         if (this->nodepath->numSelected() >= 1) {
568             this->desktop->scroll_to_point(&(this->nodepath->singleSelectedCoords()), 1.0);
569         }
570     }
573 void ShapeEditor::show_handles (bool show) {
574     if (this->nodepath && !this->nodepath->straight_path)
575         sp_nodepath_show_handles (this->nodepath, show);
578 void ShapeEditor::show_helperpath (bool show) {
579     if (this->nodepath)
580         sp_nodepath_show_helperpath (this->nodepath, show);
583 void ShapeEditor::flip (NR::Dim2 axis, NR::Maybe<NR::Point> center) {
584     if (this->nodepath)
585         sp_nodepath_flip (this->nodepath, axis, center);
588 void ShapeEditor::distribute (NR::Dim2 axis) {
589     if (this->nodepath)
590         sp_nodepath_selected_distribute (this->nodepath, axis);
592 void ShapeEditor::align (NR::Dim2 axis) {
593     if (this->nodepath)
594         sp_nodepath_selected_align (this->nodepath, axis);
598 /*
599   Local Variables:
600   mode:c++
601   c-file-style:"stroustrup"
602   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
603   indent-tabs-mode:nil
604   fill-column:99
605   End:
606 */
607 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :