ed946d95f32cd92c4827616e5f526cc66cff3493
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 "live_effects/lpeobject.h"
20 #include "selection.h"
21 #include "desktop.h"
22 #include "desktop-handles.h"
23 #include "knotholder.h"
24 #include "node-context.h"
25 #include "xml/node-event-vector.h"
26 #include "prefs-utils.h"
27 #include "object-edit.h"
28 #include "splivarot.h"
29 #include "style.h"
31 #include "shape-editor.h"
34 ShapeEditorsCollective::ShapeEditorsCollective(SPDesktop *dt) {
35 }
37 ShapeEditorsCollective::~ShapeEditorsCollective() {
38 }
41 void ShapeEditorsCollective::update_statusbar() {
43 //!!! move from nodepath: sp_nodepath_update_statusbar but summing for all nodepaths
45 }
47 ShapeEditor::ShapeEditor(SPDesktop *dt) {
48 this->desktop = dt;
49 this->grab_node = -1;
50 this->nodepath = NULL;
51 this->knotholder = NULL;
52 this->hit = false;
53 }
55 ShapeEditor::~ShapeEditor() {
56 unset_item();
57 }
59 void ShapeEditor::unset_item() {
61 Inkscape::XML::Node *old_repr = NULL;
63 if (this->nodepath) {
64 old_repr = this->nodepath->repr;
65 }
67 if (!old_repr && this->knotholder) {
68 old_repr = this->knotholder->repr;
69 }
71 if (old_repr) { // remove old listener
72 sp_repr_remove_listener_by_data(old_repr, this);
73 Inkscape::GC::release(old_repr);
74 }
76 if (this->nodepath) {
77 this->grab_node = -1;
78 sp_nodepath_destroy(this->nodepath);
79 this->nodepath = NULL;
80 }
82 if (this->knotholder) {
83 sp_knot_holder_destroy(this->knotholder);
84 this->knotholder = NULL;
85 }
86 }
88 bool ShapeEditor::has_nodepath () {
89 return (this->nodepath != NULL);
90 }
92 bool ShapeEditor::has_knotholder () {
93 return (this->knotholder != NULL);
94 }
96 bool ShapeEditor::has_local_change () {
97 if (this->nodepath)
98 return (this->nodepath->local_change != 0);
99 else if (this->knotholder)
100 return (this->knotholder->local_change != 0);
101 else
102 return false;
103 }
105 void ShapeEditor::decrement_local_change () {
106 if (this->nodepath) {
107 if (this->nodepath->local_change > 0)
108 this->nodepath->local_change--;
109 } else if (this->knotholder) {
110 this->knotholder->local_change = FALSE;
111 }
112 }
114 SPItem *ShapeEditor::get_item () {
115 SPItem *item = NULL;
116 if (this->has_nodepath()) {
117 item = this->nodepath->item;
118 } else if (this->has_knotholder()) {
119 item = SP_ITEM(this->knotholder->item);
120 }
121 return item;
122 }
124 GList *ShapeEditor::save_nodepath_selection () {
125 if (this->nodepath)
126 return ::save_nodepath_selection (this->nodepath);
127 return NULL;
128 }
130 void ShapeEditor::restore_nodepath_selection (GList *saved) {
131 if (this->nodepath && saved)
132 ::restore_nodepath_selection (this->nodepath, saved);
133 }
136 static void shapeeditor_event_attr_changed(Inkscape::XML::Node *repr, gchar const *name,
137 gchar const *old_value, gchar const *new_value,
138 bool is_interactive, gpointer data)
139 {
140 gboolean changed = FALSE;
142 g_assert(data);
143 ShapeEditor *sh = ((ShapeEditor *) data);
145 if ( ((sh->has_nodepath())
146 && (!strcmp(name, "d") || !strcmp(name, "sodipodi:nodetypes"))) // With paths, we only need to act if one of the path-affecting attributes has changed.
147 || sh->has_knotholder())
148 {
149 changed = !sh->has_local_change();
150 sh->decrement_local_change();
151 }
153 if (changed) {
154 GList *saved = NULL;
155 if (sh->has_nodepath()) {
156 saved = sh->save_nodepath_selection();
157 }
159 sh->reset_item ();
161 if (sh->has_nodepath() && saved) {
162 sh->restore_nodepath_selection(saved);
163 g_list_free (saved);
164 }
165 }
167 sh->update_statusbar(); //TODO: sh->get_container()->update_statusbar();
168 }
170 static Inkscape::XML::NodeEventVector shapeeditor_repr_events = {
171 NULL, /* child_added */
172 NULL, /* child_removed */
173 shapeeditor_event_attr_changed,
174 NULL, /* content_changed */
175 NULL /* order_changed */
176 };
179 void ShapeEditor::set_item(SPItem *item) {
181 unset_item();
183 this->grab_node = -1;
185 if (item) {
186 this->nodepath = sp_nodepath_new(desktop, item, (prefs_get_int_attribute("tools.nodes", "show_handles", 1) != 0));
187 if (this->nodepath) {
188 this->nodepath->shape_editor = this;
189 }
190 this->knotholder = sp_item_knot_holder(item, desktop);
192 if (this->nodepath || this->knotholder) {
193 // setting new listener
194 Inkscape::XML::Node *repr;
195 if (this->knotholder)
196 repr = this->knotholder->repr;
197 else
198 repr = SP_OBJECT_REPR(item);
199 if (repr) {
200 Inkscape::GC::anchor(repr);
201 sp_repr_add_listener(repr, &shapeeditor_repr_events, this);
202 }
203 }
204 }
205 }
207 /** Please note that this function only works for path parameters.
208 * All other parameters probably will crash Inkscape!
209 * Fortunately, there are no other on-canvas edittable objects at this moment :)
210 */
211 void ShapeEditor::set_item_livepatheffect_parameter(SPItem *item, SPObject *lpeobject, const char * key) {
213 unset_item();
215 this->grab_node = -1;
217 if (lpeobject) {
218 this->knotholder = NULL; // it's a path, no special knotholder needed.
219 this->nodepath = sp_nodepath_new( desktop, lpeobject,
220 (prefs_get_int_attribute("tools.nodes", "show_handles", 1) != 0),
221 key, item);
222 if (this->nodepath) {
223 this->nodepath->shape_editor = this;
225 // setting new listener
226 Inkscape::XML::Node *repr = SP_OBJECT_REPR(lpeobject);
227 if (repr) {
228 Inkscape::GC::anchor(repr);
229 sp_repr_add_listener(repr, &shapeeditor_repr_events, this);
230 }
231 }
232 }
233 }
235 /** FIXME: think about this. Is this thing only called when the item needs to be updated?
236 Why not make a reload function in NodePath and in KnotHolder? */
237 void ShapeEditor::reset_item ()
238 {
239 if ( (this->nodepath) && (IS_LIVEPATHEFFECT(this->nodepath->object)) ) {
240 SPItem * item = this->nodepath->item;
241 SPObject *obj = this->nodepath->object;
242 char * key = g_strdup(this->nodepath->repr_key);
243 set_item_livepatheffect_parameter(item, obj, key);
244 g_free(key);
245 } else {
246 SPItem * item = get_item();
247 set_item(item);
248 }
249 }
251 void ShapeEditor::nodepath_destroyed () {
252 this->nodepath = NULL;
253 }
255 void ShapeEditor::update_statusbar () {
256 if (this->nodepath)
257 sp_nodepath_update_statusbar(this->nodepath);
258 }
260 bool ShapeEditor::is_over_stroke (NR::Point event_p, bool remember) {
262 if (!this->nodepath)
263 return false; // no stroke in knotholder
265 SPItem *item = get_item();
267 //Translate click point into proper coord system
268 this->curvepoint_doc = desktop->w2d(event_p);
269 this->curvepoint_doc *= sp_item_dt2i_affine(item);
271 sp_nodepath_ensure_livarot_path(this->nodepath);
273 NR::Maybe<Path::cut_position> position = get_nearest_position_on_Path(this->nodepath->livarot_path, this->curvepoint_doc);
274 if (!position) {
275 return false;
276 }
278 NR::Point nearest = get_point_on_Path(this->nodepath->livarot_path, position->piece, position->t);
279 NR::Point delta = nearest - this->curvepoint_doc;
281 delta = desktop->d2w(delta);
283 double stroke_tolerance =
284 ( !SP_OBJECT_STYLE(item)->stroke.isNone() ?
285 desktop->current_zoom() *
286 SP_OBJECT_STYLE (item)->stroke_width.computed *
287 sp_item_i2d_affine (item).expansion() * 0.5
288 : 0.0)
289 + prefs_get_int_attribute_limited("options.dragtolerance", "value", 0, 0, 100); //(double) SP_EVENT_CONTEXT(nc)->tolerance;
291 bool close = (NR::L2 (delta) < stroke_tolerance);
293 if (remember && close) {
294 this->curvepoint_event[NR::X] = (gint) event_p [NR::X];
295 this->curvepoint_event[NR::Y] = (gint) event_p [NR::Y];
296 this->hit = true;
297 this->grab_t = position->t;
298 this->grab_node = position->piece;
299 }
301 return close;
302 }
304 void ShapeEditor::add_node_near_point() {
305 if (this->nodepath) {
306 sp_nodepath_add_node_near_point(this->nodepath, this->curvepoint_doc);
307 } else if (this->knotholder) {
308 // we do not add nodes in knotholder... yet
309 }
310 }
312 void ShapeEditor::select_segment_near_point(bool toggle) {
313 if (this->nodepath) {
314 sp_nodepath_select_segment_near_point(this->nodepath, this->curvepoint_doc, toggle);
315 } else if (this->knotholder) {
316 // we do not select segments in knotholder... yet?
317 }
318 }
320 void ShapeEditor::cancel_hit() {
321 this->hit = false;
322 }
324 bool ShapeEditor::hits_curve() {
325 return (this->hit);
326 }
329 void ShapeEditor::curve_drag(gdouble eventx, gdouble eventy) {
330 if (this->nodepath && !this->nodepath->straight_path) {
332 if (this->grab_node == -1) // don't know which segment to drag
333 return;
335 // We round off the extra precision in the motion coordinates provided
336 // by some input devices (like tablets). As we'll store the coordinates
337 // as integers in curvepoint_event we need to do this rounding before
338 // comparing them with the last coordinates from curvepoint_event.
339 // See bug #1593499 for details.
341 gint x = (gint) Inkscape::round(eventx);
342 gint y = (gint) Inkscape::round(eventy);
345 // The coordinates hasn't changed since the last motion event, abort
346 if (this->curvepoint_event[NR::X] == x &&
347 this->curvepoint_event[NR::Y] == y)
348 return;
350 NR::Point const delta_w(eventx - this->curvepoint_event[NR::X],
351 eventy - this->curvepoint_event[NR::Y]);
352 NR::Point const delta_dt(this->desktop->w2d(delta_w));
354 sp_nodepath_curve_drag (this->grab_node, this->grab_t, delta_dt); //!!! FIXME: which nodepath?!!! also uses current!!!
355 this->curvepoint_event[NR::X] = x;
356 this->curvepoint_event[NR::Y] = y;
358 } else if (this->knotholder) {
359 // we do not drag curve in knotholder
360 }
362 }
364 void ShapeEditor::finish_drag() {
365 if (this->nodepath && this->hit) {
366 sp_nodepath_update_repr (this->nodepath, _("Drag curve"));
367 }
368 }
370 void ShapeEditor::select_rect(NR::Rect const &rect, bool add) {
371 if (this->nodepath) {
372 sp_nodepath_select_rect(this->nodepath, rect, add);
373 }
374 }
376 bool ShapeEditor::has_selection() {
377 if (this->nodepath)
378 return this->nodepath->selected;
379 return false; // so far, knotholder cannot have selection
380 }
382 void ShapeEditor::deselect() {
383 if (this->nodepath)
384 sp_nodepath_deselect(this->nodepath);
385 }
387 void ShapeEditor::add_node () {
388 sp_node_selected_add_node(this->nodepath);
389 }
391 void ShapeEditor::delete_nodes () {
392 sp_node_selected_delete(this->nodepath);
393 }
395 void ShapeEditor::delete_nodes_preserving_shape () {
396 if (this->nodepath && this->nodepath->selected) {
397 sp_node_delete_preserve(g_list_copy(this->nodepath->selected));
398 }
399 }
401 void ShapeEditor::delete_segment () {
402 sp_node_selected_delete_segment(this->nodepath);
403 }
405 void ShapeEditor::set_node_type(int type) {
406 sp_node_selected_set_type(this->nodepath, (Inkscape::NodePath::NodeType) type);
407 }
409 void ShapeEditor::break_at_nodes() {
410 sp_node_selected_break(this->nodepath);
411 }
413 void ShapeEditor::join_nodes() {
414 sp_node_selected_join(this->nodepath);
415 }
417 void ShapeEditor::join_segments() {
418 sp_node_selected_join_segment(this->nodepath);
419 }
421 void ShapeEditor::duplicate_nodes() {
422 sp_node_selected_duplicate(this->nodepath);
423 }
425 void ShapeEditor::set_type_of_segments(NRPathcode code) {
426 sp_node_selected_set_line_type(this->nodepath, code);
427 }
429 void ShapeEditor::move_nodes_screen(gdouble dx, gdouble dy) {
430 sp_node_selected_move_screen(this->nodepath, dx, dy);
431 }
432 void ShapeEditor::move_nodes(gdouble dx, gdouble dy) {
433 sp_node_selected_move(this->nodepath, dx, dy);
434 }
436 void ShapeEditor::rotate_nodes(gdouble angle, int which, bool screen) {
437 if (this->nodepath)
438 sp_nodepath_selected_nodes_rotate (this->nodepath, angle, which, screen);
439 }
441 void ShapeEditor::scale_nodes(gdouble const grow, int const which) {
442 sp_nodepath_selected_nodes_scale(this->nodepath, grow, which);
443 }
444 void ShapeEditor::scale_nodes_screen(gdouble const grow, int const which) {
445 sp_nodepath_selected_nodes_scale_screen(this->nodepath, grow, which);
446 }
448 void ShapeEditor::select_all (bool invert) {
449 if (this->nodepath)
450 sp_nodepath_select_all (this->nodepath, invert);
451 }
452 void ShapeEditor::select_all_from_subpath (bool invert) {
453 if (this->nodepath)
454 sp_nodepath_select_all_from_subpath (this->nodepath, invert);
455 }
456 void ShapeEditor::select_next () {
457 if (this->nodepath) {
458 sp_nodepath_select_next (this->nodepath);
459 if (this->nodepath->numSelected() >= 1) {
460 this->desktop->scroll_to_point(&(this->nodepath->singleSelectedCoords()), 1.0);
461 }
462 }
463 }
464 void ShapeEditor::select_prev () {
465 if (this->nodepath) {
466 sp_nodepath_select_prev (this->nodepath);
467 if (this->nodepath->numSelected() >= 1) {
468 this->desktop->scroll_to_point(&(this->nodepath->singleSelectedCoords()), 1.0);
469 }
470 }
471 }
473 void ShapeEditor::show_handles (bool show) {
474 if (this->nodepath)
475 sp_nodepath_show_handles (this->nodepath, show);
476 }
479 void ShapeEditor::flip (NR::Dim2 axis, NR::Maybe<NR::Point> center) {
480 if (this->nodepath)
481 sp_nodepath_flip (this->nodepath, axis, center);
482 }
484 void ShapeEditor::distribute (NR::Dim2 axis) {
485 if (this->nodepath)
486 sp_nodepath_selected_distribute (this->nodepath, axis);
487 }
488 void ShapeEditor::align (NR::Dim2 axis) {
489 if (this->nodepath)
490 sp_nodepath_selected_align (this->nodepath, axis);
491 }
494 /*
495 Local Variables:
496 mode:c++
497 c-file-style:"stroustrup"
498 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
499 indent-tabs-mode:nil
500 fill-column:99
501 End:
502 */
503 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :