Code

Set up toolbox so that paint bucket defaults can be reset
[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->path);
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::nodepath_destroyed () {
209     this->nodepath = NULL;
212 void ShapeEditor::update_statusbar () {
213     if (this->nodepath)
214         sp_nodepath_update_statusbar(this->nodepath);
217 bool ShapeEditor::is_over_stroke (NR::Point event_p, bool remember) {
219     if (!this->nodepath) 
220         return false; // no stroke in knotholder
222     SPItem *item = get_item();
224     //Translate click point into proper coord system
225     this->curvepoint_doc = desktop->w2d(event_p);
226     this->curvepoint_doc *= sp_item_dt2i_affine(item);
227     this->curvepoint_doc *= sp_item_i2doc_affine(item);
229     sp_nodepath_ensure_livarot_path(this->nodepath);
231     NR::Maybe<Path::cut_position> position = get_nearest_position_on_Path(this->nodepath->livarot_path, this->curvepoint_doc);
232     if (!position) {
233         return false;
234     }
236     NR::Point nearest = get_point_on_Path(this->nodepath->livarot_path, position->piece, position->t);
237     NR::Point delta = nearest - this->curvepoint_doc;
239     delta = desktop->d2w(delta);
241     double stroke_tolerance =
242         (SP_OBJECT_STYLE (item)->stroke.type != SP_PAINT_TYPE_NONE?
243          desktop->current_zoom() *
244          SP_OBJECT_STYLE (item)->stroke_width.computed *
245          sp_item_i2d_affine (item).expansion() * 0.5
246          : 0.0)
247         + prefs_get_int_attribute_limited("options.dragtolerance", "value", 0, 0, 100); //(double) SP_EVENT_CONTEXT(nc)->tolerance;
249     bool close = (NR::L2 (delta) < stroke_tolerance);
251     if (remember && close) {
252         this->curvepoint_event[NR::X] = (gint) event_p [NR::X];
253         this->curvepoint_event[NR::Y] = (gint) event_p [NR::Y];
254         this->hit = true;
255         this->grab_t = position->t;
256         this->grab_node = position->piece;
257     }
259     return close;
262 void ShapeEditor::add_node_near_point() {
263     if (this->nodepath) {
264         sp_nodepath_add_node_near_point(this->nodepath, this->curvepoint_doc);
265     } else if (this->knotholder) {
266         // we do not add nodes in knotholder... yet
267     }
270 void ShapeEditor::select_segment_near_point(bool toggle) {
271     if (this->nodepath) {
272         sp_nodepath_select_segment_near_point(this->nodepath, this->curvepoint_doc, toggle);
273     } else if (this->knotholder) {
274         // we do not select segments in knotholder... yet?
275     }
278 void ShapeEditor::cancel_hit() {
279     this->hit = false;
282 bool ShapeEditor::hits_curve() {
283     return (this->hit);
287 void ShapeEditor::curve_drag(gdouble eventx, gdouble eventy) {
288     if (this->nodepath) {
290         if (this->grab_node == -1) // don't know which segment to drag
291             return;
293         // We round off the extra precision in the motion coordinates provided
294         // by some input devices (like tablets). As we'll store the coordinates
295         // as integers in curvepoint_event we need to do this rounding before
296         // comparing them with the last coordinates from curvepoint_event.
297         // See bug #1593499 for details.
299         gint x = (gint) Inkscape::round(eventx);
300         gint y = (gint) Inkscape::round(eventy);
303         // The coordinates hasn't changed since the last motion event, abort
304         if (this->curvepoint_event[NR::X] == x &&
305             this->curvepoint_event[NR::Y] == y)
306             return;
308         NR::Point const delta_w(eventx - this->curvepoint_event[NR::X],
309                                 eventy - this->curvepoint_event[NR::Y]);
310         NR::Point const delta_dt(this->desktop->w2d(delta_w));
312         sp_nodepath_curve_drag (this->grab_node, this->grab_t, delta_dt); //!!! FIXME: which nodepath?!!! also uses current!!!
313         this->curvepoint_event[NR::X] = x;
314         this->curvepoint_event[NR::Y] = y;
316     } else if (this->knotholder) {
317         // we do not drag curve in knotholder
318     }
322 void ShapeEditor::finish_drag() {
323     if (this->nodepath && this->hit) {
324         sp_nodepath_update_repr (this->nodepath, _("Drag curve"));
325     }
328 void ShapeEditor::select_rect(NR::Rect const &rect, bool add) {
329     if (this->nodepath) {
330         sp_nodepath_select_rect(this->nodepath, rect, add);
331     }
334 bool ShapeEditor::has_selection() {
335     if (this->nodepath)
336         return this->nodepath->selected;
337     return false; //  so far, knotholder cannot have selection
340 void ShapeEditor::deselect() {
341     if (this->nodepath)
342         sp_nodepath_deselect(this->nodepath);
345 void ShapeEditor::add_node () {
346     sp_node_selected_add_node(); // FIXME fix that function by removing nodepath_current lookup, and pass it this->nodepath instead (needs fixing verbs/buttons first)
349 void ShapeEditor::delete_nodes () {
350     sp_node_selected_delete(); // FIXME fix that function by removing nodepath_current lookup, and pass it this->nodepath instead (needs fixing verbs/buttons first)
353 void ShapeEditor::delete_nodes_preserving_shape () {
354     if (this->nodepath && this->nodepath->selected) {
355         sp_node_delete_preserve(g_list_copy(this->nodepath->selected));
356     }
359 void ShapeEditor::set_node_type(int type) {
360     sp_node_selected_set_type((Inkscape::NodePath::NodeType) type); // FIXME fix that function by removing nodepath_current lookup, and pass it this->nodepath instead (needs fixing verbs/buttons first)
363 void ShapeEditor::break_at_nodes() {
364     sp_node_selected_break(); // FIXME fix that function by removing nodepath_current lookup, and pass it this->nodepath instead (needs fixing verbs/buttons first)
367 void ShapeEditor::join_nodes() {
368     sp_node_selected_join(); // FIXME fix that function by removing nodepath_current lookup, and pass it this->nodepath instead (needs fixing verbs/buttons first)
371 void ShapeEditor::duplicate_nodes() {
372     sp_node_selected_duplicate(); // FIXME fix that function by removing nodepath_current lookup, and pass it this->nodepath instead (needs fixing verbs/buttons first)
375 void ShapeEditor::set_type_of_segments(NRPathcode code) {
376     sp_node_selected_set_line_type(code); // FIXME fix that function by removing nodepath_current lookup, and pass it this->nodepath instead (needs fixing verbs/buttons first)
379 void ShapeEditor::move_nodes_screen(gdouble dx, gdouble dy) {
380     sp_node_selected_move_screen(dx, dy); // FIXME fix that function by removing nodepath_current lookup, and pass it this->nodepath instead (needs fixing verbs/buttons first)
382 void ShapeEditor::move_nodes(gdouble dx, gdouble dy) {
383     sp_node_selected_move(dx, dy); // FIXME fix that function by removing nodepath_current lookup, and pass it this->nodepath instead (needs fixing verbs/buttons first)
386 void ShapeEditor::rotate_nodes(gdouble angle, int which, bool screen) {
387     if (this->nodepath)
388         sp_nodepath_selected_nodes_rotate (this->nodepath, angle, which, screen);
391 void ShapeEditor::scale_nodes(gdouble const grow, int const which) {
392     sp_nodepath_selected_nodes_scale(this->nodepath, grow, which);
394 void ShapeEditor::scale_nodes_screen(gdouble const grow, int const which) {
395     sp_nodepath_selected_nodes_scale_screen(this->nodepath, grow, which);
398 void ShapeEditor::select_all (bool invert) {
399     if (this->nodepath) 
400         sp_nodepath_select_all (this->nodepath, invert);
402 void ShapeEditor::select_all_from_subpath (bool invert) {
403     if (this->nodepath) 
404         sp_nodepath_select_all_from_subpath (this->nodepath, invert);
406 void ShapeEditor::select_next () {
407     if (this->nodepath) 
408         sp_nodepath_select_next (this->nodepath);
410 void ShapeEditor::select_prev () {
411     if (this->nodepath) 
412         sp_nodepath_select_prev (this->nodepath);
415 void ShapeEditor::flip (NR::Dim2 axis, NR::Maybe<NR::Point> center) {
416     if (this->nodepath) 
417         sp_nodepath_flip (this->nodepath, axis, center);
420 void ShapeEditor::distribute (NR::Dim2 axis) {
421     if (this->nodepath) 
422         sp_nodepath_selected_distribute (this->nodepath, axis);
424 void ShapeEditor::align (NR::Dim2 axis) {
425     if (this->nodepath) 
426         sp_nodepath_selected_align (this->nodepath, axis);
430 /*
431   Local Variables:
432   mode:c++
433   c-file-style:"stroustrup"
434   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
435   indent-tabs-mode:nil
436   fill-column:99
437   End:
438 */
439 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :