Code

scroll desktop to selected node after Tab/Shift-Tab
[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 <glibmm/i18n.h>
17 #include "sp-object.h"
18 #include "sp-item.h"
19 #include "selection.h"
20 #include "desktop.h"
21 #include "desktop-handles.h"
22 #include "knotholder.h"
23 #include "node-context.h"
24 #include "xml/node-event-vector.h"
25 #include "prefs-utils.h"
26 #include "object-edit.h"
27 #include "splivarot.h"
28 #include "style.h"
30 #include "shape-editor.h"
33 ShapeEditorsCollective::ShapeEditorsCollective(SPDesktop *dt) {
34 }
36 ShapeEditorsCollective::~ShapeEditorsCollective() {
37 }
40 void ShapeEditorsCollective::update_statusbar() {
42 //!!! move from nodepath: sp_nodepath_update_statusbar but summing for all nodepaths
44 }
46 ShapeEditor::ShapeEditor(SPDesktop *dt) {
47     this->desktop = dt;
48     this->grab_node = -1;
49     this->nodepath = NULL;
50     this->knotholder = NULL;
51     this->hit = false;
52 }
54 ShapeEditor::~ShapeEditor() {
55     unset_item();
56 }
58 void ShapeEditor::unset_item() {
60     Inkscape::XML::Node *old_repr = NULL;
62     if (this->nodepath) {
63         old_repr = this->nodepath->repr;
64     }
66     if (!old_repr && this->knotholder) {
67         old_repr = this->knotholder->repr;
68     }
70     if (old_repr) { // remove old listener
71         sp_repr_remove_listener_by_data(old_repr, this);
72         Inkscape::GC::release(old_repr);
73     }
75     if (this->nodepath) {
76         this->grab_node = -1;
77         sp_nodepath_destroy(this->nodepath);
78         this->nodepath = NULL;
79     }
81     if (this->knotholder) {
82         sp_knot_holder_destroy(this->knotholder);
83         this->knotholder = NULL;
84     }
85 }
87 bool ShapeEditor::has_nodepath () {
88     return (this->nodepath != NULL);
89 }
91 bool ShapeEditor::has_knotholder () {
92     return (this->knotholder != NULL);
93 }
95 bool ShapeEditor::has_local_change () {
96     if (this->nodepath) 
97         return (this->nodepath->local_change != 0);
98     else if (this->knotholder) 
99         return (this->knotholder->local_change != 0);
100     else
101         return false;
104 void ShapeEditor::decrement_local_change () {
105     if (this->nodepath) {
106         if (this->nodepath->local_change > 0)
107             this->nodepath->local_change--;
108     } else if (this->knotholder) {
109         this->knotholder->local_change = FALSE;
110     }
113 SPItem *ShapeEditor::get_item () {
114     SPItem *item = NULL;
115     if (this->has_nodepath()) {
116         item = SP_ITEM(this->nodepath->object);
117     } else if (this->has_knotholder()) {
118         item = SP_ITEM(this->knotholder->item);
119     }
120     return item;
123 GList *ShapeEditor::save_nodepath_selection () {
124     if (this->nodepath)
125         return ::save_nodepath_selection (this->nodepath);
126     return NULL;
129 void ShapeEditor::restore_nodepath_selection (GList *saved) {
130     if (this->nodepath && saved)
131         ::restore_nodepath_selection (this->nodepath, saved);
135 static void shapeeditor_event_attr_changed(Inkscape::XML::Node *repr, gchar const *name,
136                                            gchar const *old_value, gchar const *new_value,
137                                            bool is_interactive, gpointer data)
139     SPItem *item = NULL;
140     gboolean changed = FALSE;
142     g_assert(data);
143     ShapeEditor *sh = ((ShapeEditor *) data);
145     item = sh->get_item();
147     if (
148         ((sh->has_nodepath()) && (!strcmp(name, "d") || !strcmp(name, "sodipodi:nodetypes")))  // With paths, we only need to act if one of the path-affecting attributes has changed.
149         || sh->has_knotholder()) {
150         changed = !sh->has_local_change(); 
151         sh->decrement_local_change();
152     }
154     if (changed) {
155         GList *saved = NULL;
156         if (sh->has_nodepath()) {
157             saved = sh->save_nodepath_selection();
158         }
160         sh->set_item (item);
162         if (sh->has_nodepath() && saved) {
163             sh->restore_nodepath_selection(saved);
164             g_list_free (saved);
165         }
166     }
168     sh->update_statusbar(); //TODO: sh->get_container()->update_statusbar();
171 static Inkscape::XML::NodeEventVector shapeeditor_repr_events = {
172     NULL, /* child_added */
173     NULL, /* child_removed */
174     shapeeditor_event_attr_changed,
175     NULL, /* content_changed */
176     NULL  /* order_changed */
177 };
180 void ShapeEditor::set_item(SPItem *item) {
182     unset_item();
184     this->grab_node = -1;
186     if (item) {
187         this->nodepath = sp_nodepath_new(desktop, item, (prefs_get_int_attribute("tools.nodes", "show_handles", 1) != 0));
188         if (this->nodepath) {
189             this->nodepath->shape_editor = this; 
190         }
191         this->knotholder = sp_item_knot_holder(item, desktop);
193         if (this->nodepath || this->knotholder) {
194             // setting new listener
195             Inkscape::XML::Node *repr;
196             if (this->knotholder)
197                 repr = this->knotholder->repr;
198             else
199                 repr = SP_OBJECT_REPR(item);
200             if (repr) {
201                 Inkscape::GC::anchor(repr);
202                 sp_repr_add_listener(repr, &shapeeditor_repr_events, this);
203             }
204         }
205     }
208 void ShapeEditor::set_livepatheffect_parameter(SPObject *lpeobject, const char * key) {
210     unset_item();
212     this->grab_node = -1;
214     if (lpeobject) {
215         this->nodepath = sp_nodepath_new( desktop, lpeobject, 
216                                           (prefs_get_int_attribute("tools.nodes", "show_handles", 1) != 0),
217                                           key);
218         if (this->nodepath) {
219             this->nodepath->shape_editor = this; 
220         }
221         //this->knotholder = sp_item_knot_holder(item, desktop);
222         g_message("create knotholder?");
224         if (this->nodepath || this->knotholder) {
225             // setting new listener
226             Inkscape::XML::Node *repr;
227             if (this->knotholder)
228                 repr = this->knotholder->repr;
229             else
230                 repr = SP_OBJECT_REPR(lpeobject);
231             if (repr) {
232                 Inkscape::GC::anchor(repr);
233                 sp_repr_add_listener(repr, &shapeeditor_repr_events, this);
234             }
235         }
236     }
239 void ShapeEditor::nodepath_destroyed () {
240     this->nodepath = NULL;
243 void ShapeEditor::update_statusbar () {
244     if (this->nodepath)
245         sp_nodepath_update_statusbar(this->nodepath);
248 bool ShapeEditor::is_over_stroke (NR::Point event_p, bool remember) {
250     if (!this->nodepath) 
251         return false; // no stroke in knotholder
253     SPItem *item = get_item();
255     //Translate click point into proper coord system
256     this->curvepoint_doc = desktop->w2d(event_p);
257     this->curvepoint_doc *= sp_item_dt2i_affine(item);
259     sp_nodepath_ensure_livarot_path(this->nodepath);
261     NR::Maybe<Path::cut_position> position = get_nearest_position_on_Path(this->nodepath->livarot_path, this->curvepoint_doc);
262     if (!position) {
263         return false;
264     }
266     NR::Point nearest = get_point_on_Path(this->nodepath->livarot_path, position->piece, position->t);
267     NR::Point delta = nearest - this->curvepoint_doc;
269     delta = desktop->d2w(delta);
271     double stroke_tolerance =
272         ( !SP_OBJECT_STYLE(item)->stroke.isNone() ?
273          desktop->current_zoom() *
274          SP_OBJECT_STYLE (item)->stroke_width.computed *
275          sp_item_i2d_affine (item).expansion() * 0.5
276          : 0.0)
277         + prefs_get_int_attribute_limited("options.dragtolerance", "value", 0, 0, 100); //(double) SP_EVENT_CONTEXT(nc)->tolerance;
279     bool close = (NR::L2 (delta) < stroke_tolerance);
281     if (remember && close) {
282         this->curvepoint_event[NR::X] = (gint) event_p [NR::X];
283         this->curvepoint_event[NR::Y] = (gint) event_p [NR::Y];
284         this->hit = true;
285         this->grab_t = position->t;
286         this->grab_node = position->piece;
287     }
289     return close;
292 void ShapeEditor::add_node_near_point() {
293     if (this->nodepath) {
294         sp_nodepath_add_node_near_point(this->nodepath, this->curvepoint_doc);
295     } else if (this->knotholder) {
296         // we do not add nodes in knotholder... yet
297     }
300 void ShapeEditor::select_segment_near_point(bool toggle) {
301     if (this->nodepath) {
302         sp_nodepath_select_segment_near_point(this->nodepath, this->curvepoint_doc, toggle);
303     } else if (this->knotholder) {
304         // we do not select segments in knotholder... yet?
305     }
308 void ShapeEditor::cancel_hit() {
309     this->hit = false;
312 bool ShapeEditor::hits_curve() {
313     return (this->hit);
317 void ShapeEditor::curve_drag(gdouble eventx, gdouble eventy) {
318     if (this->nodepath && !this->nodepath->straight_path) {
320         if (this->grab_node == -1) // don't know which segment to drag
321             return;
323         // We round off the extra precision in the motion coordinates provided
324         // by some input devices (like tablets). As we'll store the coordinates
325         // as integers in curvepoint_event we need to do this rounding before
326         // comparing them with the last coordinates from curvepoint_event.
327         // See bug #1593499 for details.
329         gint x = (gint) Inkscape::round(eventx);
330         gint y = (gint) Inkscape::round(eventy);
333         // The coordinates hasn't changed since the last motion event, abort
334         if (this->curvepoint_event[NR::X] == x &&
335             this->curvepoint_event[NR::Y] == y)
336             return;
338         NR::Point const delta_w(eventx - this->curvepoint_event[NR::X],
339                                 eventy - this->curvepoint_event[NR::Y]);
340         NR::Point const delta_dt(this->desktop->w2d(delta_w));
342         sp_nodepath_curve_drag (this->grab_node, this->grab_t, delta_dt); //!!! FIXME: which nodepath?!!! also uses current!!!
343         this->curvepoint_event[NR::X] = x;
344         this->curvepoint_event[NR::Y] = y;
346     } else if (this->knotholder) {
347         // we do not drag curve in knotholder
348     }
352 void ShapeEditor::finish_drag() {
353     if (this->nodepath && this->hit) {
354         sp_nodepath_update_repr (this->nodepath, _("Drag curve"));
355     }
358 void ShapeEditor::select_rect(NR::Rect const &rect, bool add) {
359     if (this->nodepath) {
360         sp_nodepath_select_rect(this->nodepath, rect, add);
361     }
364 bool ShapeEditor::has_selection() {
365     if (this->nodepath)
366         return this->nodepath->selected;
367     return false; //  so far, knotholder cannot have selection
370 void ShapeEditor::deselect() {
371     if (this->nodepath)
372         sp_nodepath_deselect(this->nodepath);
375 void ShapeEditor::add_node () {
376     sp_node_selected_add_node(this->nodepath);
379 void ShapeEditor::delete_nodes () {
380     sp_node_selected_delete(this->nodepath);
383 void ShapeEditor::delete_nodes_preserving_shape () {
384     if (this->nodepath && this->nodepath->selected) {
385         sp_node_delete_preserve(g_list_copy(this->nodepath->selected));
386     }
389 void ShapeEditor::delete_segment () {
390     sp_node_selected_delete_segment(this->nodepath);
393 void ShapeEditor::set_node_type(int type) {
394     sp_node_selected_set_type(this->nodepath, (Inkscape::NodePath::NodeType) type);
397 void ShapeEditor::break_at_nodes() {
398     sp_node_selected_break(this->nodepath);
401 void ShapeEditor::join_nodes() {
402     sp_node_selected_join(this->nodepath);
405 void ShapeEditor::join_segments() {
406     sp_node_selected_join_segment(this->nodepath);
409 void ShapeEditor::duplicate_nodes() {
410     sp_node_selected_duplicate(this->nodepath);
413 void ShapeEditor::set_type_of_segments(NRPathcode code) {
414     sp_node_selected_set_line_type(this->nodepath, code);
417 void ShapeEditor::move_nodes_screen(gdouble dx, gdouble dy) {
418     sp_node_selected_move_screen(this->nodepath, dx, dy);
420 void ShapeEditor::move_nodes(gdouble dx, gdouble dy) {
421     sp_node_selected_move(this->nodepath, dx, dy);
424 void ShapeEditor::rotate_nodes(gdouble angle, int which, bool screen) {
425     if (this->nodepath)
426         sp_nodepath_selected_nodes_rotate (this->nodepath, angle, which, screen);
429 void ShapeEditor::scale_nodes(gdouble const grow, int const which) {
430     sp_nodepath_selected_nodes_scale(this->nodepath, grow, which);
432 void ShapeEditor::scale_nodes_screen(gdouble const grow, int const which) {
433     sp_nodepath_selected_nodes_scale_screen(this->nodepath, grow, which);
436 void ShapeEditor::select_all (bool invert) {
437     if (this->nodepath) 
438         sp_nodepath_select_all (this->nodepath, invert);
440 void ShapeEditor::select_all_from_subpath (bool invert) {
441     if (this->nodepath) 
442         sp_nodepath_select_all_from_subpath (this->nodepath, invert);
444 void ShapeEditor::select_next () {
445     if (this->nodepath) {
446         sp_nodepath_select_next (this->nodepath);
447         if (this->nodepath->numSelected() >= 1) {
448             this->desktop->scroll_to_point(&(this->nodepath->singleSelectedCoords()), 1.0);
449         }
450     }
452 void ShapeEditor::select_prev () {
453     if (this->nodepath) {
454         sp_nodepath_select_prev (this->nodepath);
455         if (this->nodepath->numSelected() >= 1) {
456             this->desktop->scroll_to_point(&(this->nodepath->singleSelectedCoords()), 1.0);
457         }
458     }
461 void ShapeEditor::show_handles (bool show) {
462     if (this->nodepath) 
463         sp_nodepath_show_handles (this->nodepath, show);
467 void ShapeEditor::flip (NR::Dim2 axis, NR::Maybe<NR::Point> center) {
468     if (this->nodepath) 
469         sp_nodepath_flip (this->nodepath, axis, center);
472 void ShapeEditor::distribute (NR::Dim2 axis) {
473     if (this->nodepath) 
474         sp_nodepath_selected_distribute (this->nodepath, axis);
476 void ShapeEditor::align (NR::Dim2 axis) {
477     if (this->nodepath) 
478         sp_nodepath_selected_align (this->nodepath, axis);
482 /*
483   Local Variables:
484   mode:c++
485   c-file-style:"stroustrup"
486   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
487   indent-tabs-mode:nil
488   fill-column:99
489   End:
490 */
491 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :