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(SH_KNOTHOLDER);
59 unset_item(SH_NODEPATH);
60 }
62 void ShapeEditor::unset_item(SubType type, bool keep_knotholder) {
63 Inkscape::XML::Node *old_repr = NULL;
65 switch (type) {
66 case SH_NODEPATH:
67 if (this->nodepath) {
68 old_repr = this->nodepath->repr;
69 sp_repr_remove_listener_by_data(old_repr, this);
70 Inkscape::GC::release(old_repr);
72 this->grab_node = -1;
73 sp_nodepath_destroy(this->nodepath);
74 this->nodepath = NULL;
75 }
76 break;
77 case SH_KNOTHOLDER:
78 if (this->knotholder) {
79 old_repr = this->knotholder->repr;
80 sp_repr_remove_listener_by_data(old_repr, this);
81 Inkscape::GC::release(old_repr);
83 if (!keep_knotholder) {
84 delete this->knotholder;
85 this->knotholder = NULL;
86 }
87 }
88 break;
89 }
90 }
92 bool ShapeEditor::has_nodepath () {
93 return (this->nodepath != NULL);
94 }
96 bool ShapeEditor::has_knotholder () {
97 return (this->knotholder != NULL);
98 }
100 void ShapeEditor::update_knotholder () {
101 if (this->knotholder)
102 this->knotholder->update_knots();
103 }
105 bool ShapeEditor::has_local_change (SubType type) {
106 switch (type) {
107 case SH_NODEPATH:
108 return (this->nodepath && this->nodepath->local_change);
109 case SH_KNOTHOLDER:
110 return (this->knotholder && this->knotholder->local_change != 0);
111 default:
112 g_assert_not_reached();
113 }
114 }
116 void ShapeEditor::decrement_local_change (SubType type) {
117 switch (type) {
118 case SH_NODEPATH:
119 if (this->nodepath && this->nodepath->local_change > 0) {
120 this->nodepath->local_change--;
121 }
122 break;
123 case SH_KNOTHOLDER:
124 if (this->knotholder) {
125 this->knotholder->local_change = FALSE;
126 }
127 break;
128 default:
129 g_assert_not_reached();
130 }
131 }
133 const SPItem *ShapeEditor::get_item (SubType type) {
134 const SPItem *item = NULL;
135 switch (type) {
136 case SH_NODEPATH:
137 if (this->has_nodepath()) {
138 item = this->nodepath->item;
139 }
140 break;
141 case SH_KNOTHOLDER:
142 if (this->has_knotholder()) {
143 item = this->knotholder->getItem();
144 }
145 break;
146 }
147 return item;
148 }
150 GList *ShapeEditor::save_nodepath_selection () {
151 if (this->nodepath)
152 return ::save_nodepath_selection (this->nodepath);
153 return NULL;
154 }
156 void ShapeEditor::restore_nodepath_selection (GList *saved) {
157 if (this->nodepath && saved)
158 ::restore_nodepath_selection (this->nodepath, saved);
159 }
161 bool ShapeEditor::nodepath_edits_repr_key(gchar const *name) {
162 if (nodepath && name) {
163 return ( !strcmp(name, nodepath->repr_key) || !strcmp(name, nodepath->repr_nodetypes_key) );
164 }
166 return false;
167 }
169 static void shapeeditor_event_attr_changed(Inkscape::XML::Node */*repr*/, gchar const *name,
170 gchar const */*old_value*/, gchar const */*new_value*/,
171 bool /*is_interactive*/, gpointer data)
172 {
173 gboolean changed_np = FALSE;
174 gboolean changed_kh = FALSE;
176 g_assert(data);
177 ShapeEditor *sh = ((ShapeEditor *) data);
179 if (sh->has_nodepath() && sh->nodepath_edits_repr_key(name))
180 {
181 changed_np = !sh->has_local_change(SH_NODEPATH);
182 sh->decrement_local_change(SH_NODEPATH);
184 }
186 if (changed_np) {
187 GList *saved = NULL;
188 if (sh->has_nodepath()) {
189 saved = sh->save_nodepath_selection();
190 }
192 sh->reset_item(SH_NODEPATH);
194 if (sh->has_nodepath() && saved) {
195 sh->restore_nodepath_selection(saved);
196 g_list_free (saved);
197 }
198 }
201 if (sh->has_knotholder())
202 {
203 changed_kh = !sh->has_local_change(SH_KNOTHOLDER);
204 sh->decrement_local_change(SH_KNOTHOLDER);
205 if (changed_kh) {
206 // this can happen if an LPEItem's knotholder handle was dragged, in which case we want
207 // to keep the knotholder; in all other cases (e.g., if the LPE itself changes) we delete it
208 sh->reset_item(SH_KNOTHOLDER, !strcmp(name, "d"));
209 }
210 }
212 sh->update_statusbar(); //TODO: sh->get_container()->update_statusbar();
213 }
215 static Inkscape::XML::NodeEventVector shapeeditor_repr_events = {
216 NULL, /* child_added */
217 NULL, /* child_removed */
218 shapeeditor_event_attr_changed,
219 NULL, /* content_changed */
220 NULL /* order_changed */
221 };
224 void ShapeEditor::set_item(SPItem *item, SubType type, bool keep_knotholder) {
225 // this happens (and should only happen) when for an LPEItem having both knotholder and nodepath the knotholder
226 // is adapted; in this case we don't want to delete the knotholder since this freezes the handles
227 unset_item(type, keep_knotholder);
229 this->grab_node = -1;
231 if (item) {
232 Inkscape::XML::Node *repr;
233 switch(type) {
234 case SH_NODEPATH:
235 if (SP_IS_LPE_ITEM(item)) {
236 //SPLPEItem *lpeitem = SP_LPE_ITEM(item);
237 //Inkscape::LivePathEffect::Effect *lpe = sp_lpe_item_get_current_lpe(lpeitem);
238 //if (!(lpe && lpe->isVisible() && lpe->providesKnotholder())) {
239 // only create nodepath if the item either doesn't have an LPE
240 // or the LPE is invisible or it doesn't provide a knotholder itself
241 this->nodepath = sp_nodepath_new(desktop, item,
242 (prefs_get_int_attribute("tools.nodes", "show_handles", 1) != 0));
243 //} else if (lpe && lpe->isVisible()) {
244 // sp_lpe_item_add_temporary_canvasitems(lpeitem, desktop);
245 //}
246 }
247 if (this->nodepath) {
248 this->nodepath->shape_editor = this;
250 // setting new listener
251 repr = SP_OBJECT_REPR(item);
252 Inkscape::GC::anchor(repr);
253 sp_repr_add_listener(repr, &shapeeditor_repr_events, this);
254 }
255 break;
257 case SH_KNOTHOLDER:
258 if (!this->knotholder) {
259 // only recreate knotholder if none is present
260 this->knotholder = sp_item_knot_holder(item, desktop);
261 }
262 if (this->knotholder) {
263 this->knotholder->update_knots();
264 // setting new listener
265 repr = this->knotholder->repr;
266 Inkscape::GC::anchor(repr);
267 sp_repr_add_listener(repr, &shapeeditor_repr_events, this);
268 }
269 break;
270 }
271 }
272 }
274 /** Please note that this function only works for path parameters.
275 * All other parameters probably will crash Inkscape!
276 */
277 void ShapeEditor::set_item_lpe_path_parameter(SPItem *item, SPObject *lpeobject, const char * key)
278 {
279 unset_item(SH_NODEPATH);
281 this->grab_node = -1;
283 if (lpeobject) {
284 this->nodepath = sp_nodepath_new( desktop, lpeobject,
285 (prefs_get_int_attribute("tools.nodes", "show_handles", 1) != 0),
286 key, item);
287 if (this->nodepath) {
288 this->nodepath->shape_editor = this;
290 // setting new listener
291 Inkscape::XML::Node *repr = SP_OBJECT_REPR(lpeobject);
292 if (repr) {
293 Inkscape::GC::anchor(repr);
294 sp_repr_add_listener(repr, &shapeeditor_repr_events, this);
295 }
296 }
297 }
298 }
300 /**
301 * pass a new knotholder to ShapeEditor to manage (and delete)
302 */
303 void
304 ShapeEditor::set_knotholder(KnotHolder * knot_holder)
305 {
306 unset_item(SH_KNOTHOLDER);
308 this->grab_node = -1;
310 if (knot_holder) {
311 this->knotholder = knot_holder;
312 }
313 }
316 /** FIXME: think about this. Is this thing only called when the item needs to be updated?
317 Why not make a reload function in NodePath and in KnotHolder? */
318 void ShapeEditor::reset_item (SubType type, bool keep_knotholder)
319 {
320 switch (type) {
321 case SH_NODEPATH:
322 if ( (this->nodepath) && (IS_LIVEPATHEFFECT(this->nodepath->object)) ) {
323 SPItem * item = this->nodepath->item;
324 SPObject *obj = this->nodepath->object;
325 char * key = g_strdup(this->nodepath->repr_key);
326 set_item_lpe_path_parameter(item, obj, key); // the above checks for nodepath, so it is indeed a path that we are editing
327 g_free(key);
328 } else {
329 SPItem * item = (SPItem *) get_item(SH_NODEPATH);
330 set_item(item, SH_NODEPATH);
331 }
332 break;
333 case SH_KNOTHOLDER:
334 if (this->knotholder) {
335 SPItem * item = (SPItem *) get_item(SH_KNOTHOLDER);
336 set_item(item, SH_KNOTHOLDER, keep_knotholder);
337 }
338 break;
339 }
340 }
342 void ShapeEditor::nodepath_destroyed () {
343 this->nodepath = NULL;
344 }
346 void ShapeEditor::update_statusbar () {
347 if (this->nodepath)
348 sp_nodepath_update_statusbar(this->nodepath);
349 }
351 bool ShapeEditor::is_over_stroke (NR::Point event_p, bool remember) {
353 if (!this->nodepath)
354 return false; // no stroke in knotholder
356 const SPItem *item = get_item(SH_NODEPATH);
358 //Translate click point into proper coord system
359 this->curvepoint_doc = desktop->w2d(event_p);
360 this->curvepoint_doc *= from_2geom(sp_item_dt2i_affine(item));
362 sp_nodepath_ensure_livarot_path(this->nodepath);
364 NR::Maybe<Path::cut_position> position = get_nearest_position_on_Path(this->nodepath->livarot_path, this->curvepoint_doc);
365 if (!position) {
366 return false;
367 }
369 NR::Point nearest = get_point_on_Path(this->nodepath->livarot_path, position->piece, position->t);
370 NR::Point delta = nearest - this->curvepoint_doc;
372 delta = desktop->d2w(delta);
374 double stroke_tolerance =
375 (( !SP_OBJECT_STYLE(item)->stroke.isNone() ?
376 desktop->current_zoom() *
377 SP_OBJECT_STYLE (item)->stroke_width.computed * 0.5 *
378 NR::expansion(from_2geom(sp_item_i2d_affine(item)))
379 : 0.0)
380 + prefs_get_int_attribute_limited("options.dragtolerance", "value", 0, 0, 100)) /NR::expansion(from_2geom(sp_item_i2d_affine(item)));
381 bool close = (NR::L2 (delta) < stroke_tolerance);
383 if (remember && close) {
384 this->curvepoint_event[NR::X] = (gint) event_p [NR::X];
385 this->curvepoint_event[NR::Y] = (gint) event_p [NR::Y];
386 this->hit = true;
387 this->grab_t = position->t;
388 this->grab_node = position->piece;
389 }
391 return close;
392 }
394 void ShapeEditor::add_node_near_point() {
395 if (this->nodepath) {
396 sp_nodepath_add_node_near_point(this->nodepath, this->curvepoint_doc);
397 } else if (this->knotholder) {
398 // we do not add nodes in knotholder... yet
399 }
400 }
402 void ShapeEditor::select_segment_near_point(bool toggle) {
403 if (this->nodepath) {
404 sp_nodepath_select_segment_near_point(this->nodepath, this->curvepoint_doc, toggle);
405 }
406 if (this->knotholder) {
407 // we do not select segments in knotholder... yet?
408 }
409 }
411 void ShapeEditor::cancel_hit() {
412 this->hit = false;
413 }
415 bool ShapeEditor::hits_curve() {
416 return (this->hit);
417 }
420 void ShapeEditor::curve_drag(gdouble eventx, gdouble eventy) {
421 if (this->nodepath && !this->nodepath->straight_path) {
423 if (this->grab_node == -1) // don't know which segment to drag
424 return;
426 // We round off the extra precision in the motion coordinates provided
427 // by some input devices (like tablets). As we'll store the coordinates
428 // as integers in curvepoint_event we need to do this rounding before
429 // comparing them with the last coordinates from curvepoint_event.
430 // See bug #1593499 for details.
432 gint x = (gint) Inkscape::round(eventx);
433 gint y = (gint) Inkscape::round(eventy);
436 // The coordinates hasn't changed since the last motion event, abort
437 if (this->curvepoint_event[NR::X] == x &&
438 this->curvepoint_event[NR::Y] == y)
439 return;
441 NR::Point const delta_w(eventx - this->curvepoint_event[NR::X],
442 eventy - this->curvepoint_event[NR::Y]);
443 NR::Point const delta_dt(this->desktop->w2d(delta_w));
445 sp_nodepath_curve_drag (this->grab_node, this->grab_t, delta_dt); //!!! FIXME: which nodepath?!!! also uses current!!!
446 this->curvepoint_event[NR::X] = x;
447 this->curvepoint_event[NR::Y] = y;
449 }
450 if (this->knotholder) {
451 // we do not drag curve in knotholder
452 }
454 }
456 void ShapeEditor::finish_drag() {
457 if (this->nodepath && this->hit) {
458 sp_nodepath_update_repr (this->nodepath, _("Drag curve"));
459 }
460 }
462 void ShapeEditor::select_rect(NR::Rect const &rect, bool add) {
463 if (this->nodepath) {
464 sp_nodepath_select_rect(this->nodepath, rect, add);
465 }
466 }
468 bool ShapeEditor::has_selection() {
469 if (this->nodepath)
470 return this->nodepath->selected;
471 return false; // so far, knotholder cannot have selection
472 }
474 void ShapeEditor::deselect() {
475 if (this->nodepath)
476 sp_nodepath_deselect(this->nodepath);
477 }
479 void ShapeEditor::add_node () {
480 sp_node_selected_add_node(this->nodepath);
481 }
483 void ShapeEditor::delete_nodes () {
484 sp_node_selected_delete(this->nodepath);
485 }
487 void ShapeEditor::delete_nodes_preserving_shape () {
488 if (this->nodepath && this->nodepath->selected) {
489 sp_node_delete_preserve(g_list_copy(this->nodepath->selected));
490 }
491 }
493 void ShapeEditor::delete_segment () {
494 sp_node_selected_delete_segment(this->nodepath);
495 }
497 void ShapeEditor::set_node_type(int type) {
498 sp_node_selected_set_type(this->nodepath, (Inkscape::NodePath::NodeType) type);
499 }
501 void ShapeEditor::break_at_nodes() {
502 sp_node_selected_break(this->nodepath);
503 }
505 void ShapeEditor::join_nodes() {
506 sp_node_selected_join(this->nodepath);
507 }
509 void ShapeEditor::join_segments() {
510 sp_node_selected_join_segment(this->nodepath);
511 }
513 void ShapeEditor::duplicate_nodes() {
514 sp_node_selected_duplicate(this->nodepath);
515 }
517 void ShapeEditor::set_type_of_segments(NRPathcode code) {
518 sp_node_selected_set_line_type(this->nodepath, code);
519 }
521 void ShapeEditor::move_nodes_screen(gdouble dx, gdouble dy) {
522 sp_node_selected_move_screen(this->nodepath, dx, dy);
523 }
524 void ShapeEditor::move_nodes(gdouble dx, gdouble dy) {
525 sp_node_selected_move(this->nodepath, dx, dy);
526 }
528 void ShapeEditor::rotate_nodes(gdouble angle, int which, bool screen) {
529 if (this->nodepath)
530 sp_nodepath_selected_nodes_rotate (this->nodepath, angle, which, screen);
531 }
533 void ShapeEditor::scale_nodes(gdouble const grow, int const which) {
534 sp_nodepath_selected_nodes_scale(this->nodepath, grow, which);
535 }
536 void ShapeEditor::scale_nodes_screen(gdouble const grow, int const which) {
537 sp_nodepath_selected_nodes_scale_screen(this->nodepath, grow, which);
538 }
540 void ShapeEditor::select_all (bool invert) {
541 if (this->nodepath)
542 sp_nodepath_select_all (this->nodepath, invert);
543 }
544 void ShapeEditor::select_all_from_subpath (bool invert) {
545 if (this->nodepath)
546 sp_nodepath_select_all_from_subpath (this->nodepath, invert);
547 }
548 void ShapeEditor::select_next () {
549 if (this->nodepath) {
550 sp_nodepath_select_next (this->nodepath);
551 if (this->nodepath->numSelected() >= 1) {
552 this->desktop->scroll_to_point(&(this->nodepath->singleSelectedCoords()), 1.0);
553 }
554 }
555 }
556 void ShapeEditor::select_prev () {
557 if (this->nodepath) {
558 sp_nodepath_select_prev (this->nodepath);
559 if (this->nodepath->numSelected() >= 1) {
560 this->desktop->scroll_to_point(&(this->nodepath->singleSelectedCoords()), 1.0);
561 }
562 }
563 }
565 void ShapeEditor::show_handles (bool show) {
566 if (this->nodepath && !this->nodepath->straight_path)
567 sp_nodepath_show_handles (this->nodepath, show);
568 }
570 void ShapeEditor::show_helperpath (bool show) {
571 if (this->nodepath)
572 sp_nodepath_show_helperpath (this->nodepath, show);
573 }
575 void ShapeEditor::flip (NR::Dim2 axis, NR::Maybe<NR::Point> center) {
576 if (this->nodepath)
577 sp_nodepath_flip (this->nodepath, axis, center);
578 }
580 void ShapeEditor::distribute (NR::Dim2 axis) {
581 if (this->nodepath)
582 sp_nodepath_selected_distribute (this->nodepath, axis);
583 }
584 void ShapeEditor::align (NR::Dim2 axis) {
585 if (this->nodepath)
586 sp_nodepath_selected_align (this->nodepath, axis);
587 }
590 /*
591 Local Variables:
592 mode:c++
593 c-file-style:"stroustrup"
594 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
595 indent-tabs-mode:nil
596 fill-column:99
597 End:
598 */
599 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :