Code

Remove obsolete 'sodipodi:docbase' attribute when opening old Sodipodi/Inkscape files.
[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         delete 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     return ((this->nodepath && this->nodepath->local_change) ||
100             (this->knotholder && this->knotholder->local_change != 0));
103 void ShapeEditor::decrement_local_change () {
104     if (this->nodepath && this->nodepath->local_change > 0) {
105         this->nodepath->local_change--;
106     } else if (this->knotholder) {
107         this->knotholder->local_change = FALSE;
108     }
111 SPItem *ShapeEditor::get_item () {
112     SPItem *item = NULL;
113     if (this->has_nodepath()) {
114         item = this->nodepath->item;
115     } else if (this->has_knotholder()) {
116         item = SP_ITEM(this->knotholder->item);
117     }
118     return item;
121 GList *ShapeEditor::save_nodepath_selection () {
122     if (this->nodepath)
123         return ::save_nodepath_selection (this->nodepath);
124     return NULL;
127 void ShapeEditor::restore_nodepath_selection (GList *saved) {
128     if (this->nodepath && saved)
129         ::restore_nodepath_selection (this->nodepath, saved);
132 bool ShapeEditor::nodepath_edits_repr_key(gchar const *name) {
133     if (nodepath && name) {
134         return ( !strcmp(name, nodepath->repr_key) || !strcmp(name, nodepath->repr_nodetypes_key) );
135     }
137     return false;
140 static void shapeeditor_event_attr_changed(Inkscape::XML::Node */*repr*/, gchar const *name,
141                                            gchar const */*old_value*/, gchar const */*new_value*/,
142                                            bool /*is_interactive*/, gpointer data)
144     gboolean changed = FALSE;
146     g_assert(data);
147     ShapeEditor *sh = ((ShapeEditor *) data);
149     if ( sh->has_knotholder() || ( sh->has_nodepath() && sh->nodepath_edits_repr_key(name) ) )
150     {
151         changed = !sh->has_local_change();
152         sh->decrement_local_change();
153     }
155     if (changed) {
156         GList *saved = NULL;
157         if (sh->has_nodepath()) {
158             saved = sh->save_nodepath_selection();
159         }
161         sh->reset_item ();
163         if (sh->has_nodepath() && saved) {
164             sh->restore_nodepath_selection(saved);
165             g_list_free (saved);
166         }
167     }
169     sh->update_statusbar(); //TODO: sh->get_container()->update_statusbar();
172 static Inkscape::XML::NodeEventVector shapeeditor_repr_events = {
173     NULL, /* child_added */
174     NULL, /* child_removed */
175     shapeeditor_event_attr_changed,
176     NULL, /* content_changed */
177     NULL  /* order_changed */
178 };
181 void ShapeEditor::set_item(SPItem *item) {
183     unset_item();
185     this->grab_node = -1;
187     if (item) {
188         if (SP_IS_LPE_ITEM(item)) {
189             SPLPEItem *lpeitem = SP_LPE_ITEM(item);
190             Inkscape::LivePathEffect::Effect *lpe = sp_lpe_item_get_current_lpe(lpeitem);
191             if (!(lpe && lpe->isVisible() && lpe->providesKnotholder())) {
192                 // only create nodepath if the item either doesn't have an LPE
193                 // or the LPE is invisible or it doesn't provide a knotholder itself
194                 this->nodepath = sp_nodepath_new(desktop, item,
195                                                  (prefs_get_int_attribute("tools.nodes", "show_handles", 1) != 0));
196             } else if (lpe && lpe->isVisible()) {
197                 sp_lpe_item_add_temporary_canvasitems(lpeitem, desktop);
198             }
199         }
201         if (this->nodepath) {
202             this->nodepath->shape_editor = this;
203         }
204         this->knotholder = sp_item_knot_holder(item, desktop);
206         if (this->nodepath || this->knotholder) {
207             // setting new listener
208             Inkscape::XML::Node *repr;
209             if (this->knotholder)
210                 repr = this->knotholder->repr;
211             else
212                 repr = SP_OBJECT_REPR(item);
213             if (repr) {
214                 Inkscape::GC::anchor(repr);
215                 sp_repr_add_listener(repr, &shapeeditor_repr_events, this);
216             }
217         }
218     }
221 /** Please note that this function only works for path parameters.
222 *  All other parameters probably will crash Inkscape!
223 */
224 void ShapeEditor::set_item_lpe_path_parameter(SPItem *item, SPObject *lpeobject, const char * key)
226     unset_item();
228     this->grab_node = -1;
230     if (lpeobject) {
231         this->nodepath = sp_nodepath_new( desktop, lpeobject,
232                                           (prefs_get_int_attribute("tools.nodes", "show_handles", 1) != 0),
233                                           key, item);
234         if (this->nodepath) {
235             this->nodepath->shape_editor = this;
237             // setting new listener
238             Inkscape::XML::Node *repr = SP_OBJECT_REPR(lpeobject);
239             if (repr) {
240                 Inkscape::GC::anchor(repr);
241                 sp_repr_add_listener(repr, &shapeeditor_repr_events, this);
242             }
243         }
244     }
247 /** 
248 *  pass a new knotholder to ShapeEditor to manage (and delete)
249 */
250 void
251 ShapeEditor::set_knotholder(KnotHolder * knot_holder)
253     unset_item();
255     this->grab_node = -1;
257     if (knot_holder) {
258         this->knotholder = knot_holder;
259     }
263 /** FIXME: think about this. Is this thing only called when the item needs to be updated?
264    Why not make a reload function in NodePath and in KnotHolder? */
265 void ShapeEditor::reset_item ()
267     if ( (this->nodepath) && (IS_LIVEPATHEFFECT(this->nodepath->object)) ) {
268         SPItem * item = this->nodepath->item;
269         SPObject *obj = this->nodepath->object;
270         char * key = g_strdup(this->nodepath->repr_key);
271         set_item_lpe_path_parameter(item, obj, key); // the above checks for nodepath, so it is indeed a path that we are editing
272         g_free(key);
273     } else {
274         SPItem * item = get_item();
275         set_item(item);
276     }
279 void ShapeEditor::nodepath_destroyed () {
280     this->nodepath = NULL;
283 void ShapeEditor::update_statusbar () {
284     if (this->nodepath)
285         sp_nodepath_update_statusbar(this->nodepath);
288 bool ShapeEditor::is_over_stroke (NR::Point event_p, bool remember) {
290     if (!this->nodepath)
291         return false; // no stroke in knotholder
293     SPItem *item = get_item();
295     //Translate click point into proper coord system
296     this->curvepoint_doc = desktop->w2d(event_p);
297     this->curvepoint_doc *= from_2geom(sp_item_dt2i_affine(item));
299     sp_nodepath_ensure_livarot_path(this->nodepath);
301     NR::Maybe<Path::cut_position> position = get_nearest_position_on_Path(this->nodepath->livarot_path, this->curvepoint_doc);
302     if (!position) {
303         return false;
304     }
306     NR::Point nearest = get_point_on_Path(this->nodepath->livarot_path, position->piece, position->t);
307     NR::Point delta = nearest - this->curvepoint_doc;
309     delta = desktop->d2w(delta);
311     double stroke_tolerance =
312         (( !SP_OBJECT_STYLE(item)->stroke.isNone() ?
313            desktop->current_zoom() *
314            SP_OBJECT_STYLE (item)->stroke_width.computed * 0.5 *
315            NR::expansion(from_2geom(sp_item_i2d_affine(item)))
316          : 0.0)
317          + prefs_get_int_attribute_limited("options.dragtolerance", "value", 0, 0, 100)) /NR::expansion(from_2geom(sp_item_i2d_affine(item))); 
318     bool close = (NR::L2 (delta) < stroke_tolerance);
320     if (remember && close) {
321         this->curvepoint_event[NR::X] = (gint) event_p [NR::X];
322         this->curvepoint_event[NR::Y] = (gint) event_p [NR::Y];
323         this->hit = true;
324         this->grab_t = position->t;
325         this->grab_node = position->piece;
326     }
328     return close;
331 void ShapeEditor::add_node_near_point() {
332     if (this->nodepath) {
333         sp_nodepath_add_node_near_point(this->nodepath, this->curvepoint_doc);
334     } else if (this->knotholder) {
335         // we do not add nodes in knotholder... yet
336     }
339 void ShapeEditor::select_segment_near_point(bool toggle) {
340     if (this->nodepath) {
341         sp_nodepath_select_segment_near_point(this->nodepath, this->curvepoint_doc, toggle);
342     } else if (this->knotholder) {
343         // we do not select segments in knotholder... yet?
344     }
347 void ShapeEditor::cancel_hit() {
348     this->hit = false;
351 bool ShapeEditor::hits_curve() {
352     return (this->hit);
356 void ShapeEditor::curve_drag(gdouble eventx, gdouble eventy) {
357     if (this->nodepath && !this->nodepath->straight_path) {
359         if (this->grab_node == -1) // don't know which segment to drag
360             return;
362         // We round off the extra precision in the motion coordinates provided
363         // by some input devices (like tablets). As we'll store the coordinates
364         // as integers in curvepoint_event we need to do this rounding before
365         // comparing them with the last coordinates from curvepoint_event.
366         // See bug #1593499 for details.
368         gint x = (gint) Inkscape::round(eventx);
369         gint y = (gint) Inkscape::round(eventy);
372         // The coordinates hasn't changed since the last motion event, abort
373         if (this->curvepoint_event[NR::X] == x &&
374             this->curvepoint_event[NR::Y] == y)
375             return;
377         NR::Point const delta_w(eventx - this->curvepoint_event[NR::X],
378                                 eventy - this->curvepoint_event[NR::Y]);
379         NR::Point const delta_dt(this->desktop->w2d(delta_w));
381         sp_nodepath_curve_drag (this->grab_node, this->grab_t, delta_dt); //!!! FIXME: which nodepath?!!! also uses current!!!
382         this->curvepoint_event[NR::X] = x;
383         this->curvepoint_event[NR::Y] = y;
385     } else if (this->knotholder) {
386         // we do not drag curve in knotholder
387     }
391 void ShapeEditor::finish_drag() {
392     if (this->nodepath && this->hit) {
393         sp_nodepath_update_repr (this->nodepath, _("Drag curve"));
394     }
397 void ShapeEditor::select_rect(NR::Rect const &rect, bool add) {
398     if (this->nodepath) {
399         sp_nodepath_select_rect(this->nodepath, rect, add);
400     }
403 bool ShapeEditor::has_selection() {
404     if (this->nodepath)
405         return this->nodepath->selected;
406     return false; //  so far, knotholder cannot have selection
409 void ShapeEditor::deselect() {
410     if (this->nodepath)
411         sp_nodepath_deselect(this->nodepath);
414 void ShapeEditor::add_node () {
415     sp_node_selected_add_node(this->nodepath);
418 void ShapeEditor::delete_nodes () {
419     sp_node_selected_delete(this->nodepath);
422 void ShapeEditor::delete_nodes_preserving_shape () {
423     if (this->nodepath && this->nodepath->selected) {
424         sp_node_delete_preserve(g_list_copy(this->nodepath->selected));
425     }
428 void ShapeEditor::delete_segment () {
429     sp_node_selected_delete_segment(this->nodepath);
432 void ShapeEditor::set_node_type(int type) {
433     sp_node_selected_set_type(this->nodepath, (Inkscape::NodePath::NodeType) type);
436 void ShapeEditor::break_at_nodes() {
437     sp_node_selected_break(this->nodepath);
440 void ShapeEditor::join_nodes() {
441     sp_node_selected_join(this->nodepath);
444 void ShapeEditor::join_segments() {
445     sp_node_selected_join_segment(this->nodepath);
448 void ShapeEditor::duplicate_nodes() {
449     sp_node_selected_duplicate(this->nodepath);
452 void ShapeEditor::set_type_of_segments(NRPathcode code) {
453     sp_node_selected_set_line_type(this->nodepath, code);
456 void ShapeEditor::move_nodes_screen(gdouble dx, gdouble dy) {
457     sp_node_selected_move_screen(this->nodepath, dx, dy);
459 void ShapeEditor::move_nodes(gdouble dx, gdouble dy) {
460     sp_node_selected_move(this->nodepath, dx, dy);
463 void ShapeEditor::rotate_nodes(gdouble angle, int which, bool screen) {
464     if (this->nodepath)
465         sp_nodepath_selected_nodes_rotate (this->nodepath, angle, which, screen);
468 void ShapeEditor::scale_nodes(gdouble const grow, int const which) {
469     sp_nodepath_selected_nodes_scale(this->nodepath, grow, which);
471 void ShapeEditor::scale_nodes_screen(gdouble const grow, int const which) {
472     sp_nodepath_selected_nodes_scale_screen(this->nodepath, grow, which);
475 void ShapeEditor::select_all (bool invert) {
476     if (this->nodepath)
477         sp_nodepath_select_all (this->nodepath, invert);
479 void ShapeEditor::select_all_from_subpath (bool invert) {
480     if (this->nodepath)
481         sp_nodepath_select_all_from_subpath (this->nodepath, invert);
483 void ShapeEditor::select_next () {
484     if (this->nodepath) {
485         sp_nodepath_select_next (this->nodepath);
486         if (this->nodepath->numSelected() >= 1) {
487             this->desktop->scroll_to_point(&(this->nodepath->singleSelectedCoords()), 1.0);
488         }
489     }
491 void ShapeEditor::select_prev () {
492     if (this->nodepath) {
493         sp_nodepath_select_prev (this->nodepath);
494         if (this->nodepath->numSelected() >= 1) {
495             this->desktop->scroll_to_point(&(this->nodepath->singleSelectedCoords()), 1.0);
496         }
497     }
500 void ShapeEditor::show_handles (bool show) {
501     if (this->nodepath && !this->nodepath->straight_path)
502         sp_nodepath_show_handles (this->nodepath, show);
505 void ShapeEditor::show_helperpath (bool show) {
506     if (this->nodepath)
507         sp_nodepath_show_helperpath (this->nodepath, show);
510 void ShapeEditor::flip (NR::Dim2 axis, NR::Maybe<NR::Point> center) {
511     if (this->nodepath)
512         sp_nodepath_flip (this->nodepath, axis, center);
515 void ShapeEditor::distribute (NR::Dim2 axis) {
516     if (this->nodepath)
517         sp_nodepath_selected_distribute (this->nodepath, axis);
519 void ShapeEditor::align (NR::Dim2 axis) {
520     if (this->nodepath)
521         sp_nodepath_selected_align (this->nodepath, axis);
525 /*
526   Local Variables:
527   mode:c++
528   c-file-style:"stroustrup"
529   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
530   indent-tabs-mode:nil
531   fill-column:99
532   End:
533 */
534 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :