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 "sp-lpe-item.h"
21 #include "live_effects/lpeobject.h"
22 #include "selection.h"
23 #include "desktop.h"
24 #include "document.h"
25 #include "desktop-handles.h"
26 #include "knotholder.h"
27 #include "live_effects/parameter/point.h"
28 #include "nodepath.h"
29 #include "xml/node-event-vector.h"
30 #include "preferences.h"
31 #include "object-edit.h"
32 #include "style.h"
33 #include "display/curve.h"
34 #include <2geom/pathvector.h>
36 #include "shape-editor.h"
39 ShapeEditorsCollective::ShapeEditorsCollective(SPDesktop */*dt*/) {
40 }
42 ShapeEditorsCollective::~ShapeEditorsCollective() {
43 }
46 void ShapeEditorsCollective::update_statusbar() {
48 //!!! move from nodepath: sp_nodepath_update_statusbar but summing for all nodepaths
50 }
52 ShapeEditor::ShapeEditor(SPDesktop *dt) {
53 this->desktop = dt;
54 this->grab_node = -1;
55 this->nodepath = NULL;
56 this->knotholder = NULL;
57 this->hit = false;
58 this->knotholder_listener_attached_for = NULL;
59 this->nodepath_listener_attached_for = NULL;
60 }
62 ShapeEditor::~ShapeEditor() {
63 unset_item(SH_KNOTHOLDER);
64 unset_item(SH_NODEPATH);
65 }
67 void ShapeEditor::unset_item(SubType type, bool keep_knotholder) {
68 Inkscape::XML::Node *old_repr = NULL;
70 switch (type) {
71 case SH_NODEPATH:
72 if (this->nodepath) {
73 old_repr = this->nodepath->repr;
74 if (old_repr && old_repr == nodepath_listener_attached_for) {
75 sp_repr_remove_listener_by_data(old_repr, this);
76 Inkscape::GC::release(old_repr);
77 nodepath_listener_attached_for = NULL;
78 }
80 this->grab_node = -1;
81 delete this->nodepath;
82 this->nodepath = NULL;
83 }
84 break;
85 case SH_KNOTHOLDER:
86 if (this->knotholder) {
87 old_repr = this->knotholder->repr;
88 if (old_repr && old_repr == knotholder_listener_attached_for) {
89 sp_repr_remove_listener_by_data(old_repr, this);
90 Inkscape::GC::release(old_repr);
91 knotholder_listener_attached_for = NULL;
92 }
94 if (!keep_knotholder) {
95 delete this->knotholder;
96 this->knotholder = NULL;
97 }
98 }
99 break;
100 }
101 }
103 bool ShapeEditor::has_nodepath () {
104 return (this->nodepath != NULL);
105 }
107 bool ShapeEditor::has_knotholder () {
108 return (this->knotholder != NULL);
109 }
111 void ShapeEditor::update_knotholder () {
112 if (this->knotholder)
113 this->knotholder->update_knots();
114 }
116 bool ShapeEditor::has_local_change (SubType type) {
117 switch (type) {
118 case SH_NODEPATH:
119 return (this->nodepath && this->nodepath->local_change);
120 case SH_KNOTHOLDER:
121 return (this->knotholder && this->knotholder->local_change != 0);
122 default:
123 g_assert_not_reached();
124 }
125 }
127 void ShapeEditor::decrement_local_change (SubType type) {
128 switch (type) {
129 case SH_NODEPATH:
130 if (this->nodepath && this->nodepath->local_change > 0) {
131 this->nodepath->local_change--;
132 }
133 break;
134 case SH_KNOTHOLDER:
135 if (this->knotholder) {
136 this->knotholder->local_change = FALSE;
137 }
138 break;
139 default:
140 g_assert_not_reached();
141 }
142 }
144 const SPItem *ShapeEditor::get_item (SubType type) {
145 const SPItem *item = NULL;
146 switch (type) {
147 case SH_NODEPATH:
148 if (this->has_nodepath()) {
149 item = this->nodepath->item;
150 }
151 break;
152 case SH_KNOTHOLDER:
153 if (this->has_knotholder()) {
154 item = this->knotholder->getItem();
155 }
156 break;
157 }
158 return item;
159 }
161 GList *ShapeEditor::save_nodepath_selection () {
162 if (this->nodepath)
163 return ::save_nodepath_selection (this->nodepath);
164 return NULL;
165 }
167 void ShapeEditor::restore_nodepath_selection (GList *saved) {
168 if (this->nodepath && saved)
169 ::restore_nodepath_selection (this->nodepath, saved);
170 }
172 bool ShapeEditor::nodepath_edits_repr_key(gchar const *name) {
173 if (nodepath && name) {
174 return ( !strcmp(name, nodepath->repr_key) || !strcmp(name, nodepath->repr_nodetypes_key) );
175 }
177 return false;
178 }
181 void ShapeEditor::shapeeditor_event_attr_changed(gchar const *name)
182 {
183 gboolean changed_np = FALSE;
184 gboolean changed_kh = FALSE;
186 if (has_nodepath() && nodepath_edits_repr_key(name))
187 {
188 changed_np = !has_local_change(SH_NODEPATH);
189 decrement_local_change(SH_NODEPATH);
190 }
192 if (changed_np) {
193 GList *saved = NULL;
194 if (has_nodepath()) {
195 saved = save_nodepath_selection();
196 }
198 reset_item(SH_NODEPATH);
200 if (has_nodepath() && saved) {
201 restore_nodepath_selection(saved);
202 g_list_free (saved);
203 }
204 }
206 if (has_knotholder())
207 {
208 changed_kh = !has_local_change(SH_KNOTHOLDER);
209 decrement_local_change(SH_KNOTHOLDER);
210 if (changed_kh) {
211 // this can happen if an LPEItem's knotholder handle was dragged, in which case we want
212 // to keep the knotholder; in all other cases (e.g., if the LPE itself changes) we delete it
213 reset_item(SH_KNOTHOLDER, !strcmp(name, "d"));
214 }
215 }
217 update_statusbar(); //TODO: get_container()->update_statusbar();
218 }
221 static void shapeeditor_event_attr_changed(Inkscape::XML::Node */*repr*/, gchar const *name,
222 gchar const */*old_value*/, gchar const */*new_value*/,
223 bool /*is_interactive*/, gpointer data)
224 {
225 g_assert(data);
226 ShapeEditor *sh = ((ShapeEditor *) data);
228 sh->shapeeditor_event_attr_changed(name);
229 }
231 static Inkscape::XML::NodeEventVector shapeeditor_repr_events = {
232 NULL, /* child_added */
233 NULL, /* child_removed */
234 shapeeditor_event_attr_changed,
235 NULL, /* content_changed */
236 NULL /* order_changed */
237 };
240 void ShapeEditor::set_item(SPItem *item, SubType type, bool keep_knotholder) {
241 // this happens (and should only happen) when for an LPEItem having both knotholder and
242 // nodepath the knotholder is adapted; in this case we don't want to delete the knotholder
243 // since this freezes the handles
244 unset_item(type, keep_knotholder);
246 this->grab_node = -1;
248 if (item) {
249 Inkscape::XML::Node *repr;
250 switch(type) {
251 case SH_NODEPATH:
252 if (SP_IS_LPE_ITEM(item)) {
253 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
254 this->nodepath = sp_nodepath_new(desktop, item, (prefs->getBool("/tools/nodes/show_handles", true)));
255 }
256 if (this->nodepath) {
257 this->nodepath->shape_editor = this;
259 // setting new listener
260 repr = SP_OBJECT_REPR(item);
261 if (repr != nodepath_listener_attached_for) {
262 Inkscape::GC::anchor(repr);
263 sp_repr_add_listener(repr, &shapeeditor_repr_events, this);
264 nodepath_listener_attached_for = repr;
265 }
266 }
267 break;
269 case SH_KNOTHOLDER:
270 if (!this->knotholder) {
271 // only recreate knotholder if none is present
272 this->knotholder = sp_item_knot_holder(item, desktop);
273 }
274 if (this->knotholder) {
275 this->knotholder->update_knots();
276 // setting new listener
277 repr = this->knotholder->repr;
278 if (repr != knotholder_listener_attached_for) {
279 Inkscape::GC::anchor(repr);
280 sp_repr_add_listener(repr, &shapeeditor_repr_events, this);
281 knotholder_listener_attached_for = repr;
282 }
283 }
284 break;
285 }
286 }
287 }
289 /** Please note that this function only works for path parameters.
290 * All other parameters probably will crash Inkscape!
291 */
292 void ShapeEditor::set_item_lpe_path_parameter(SPItem *item, LivePathEffectObject *lpeobject, const char * key)
293 {
294 unset_item(SH_NODEPATH);
296 this->grab_node = -1;
298 if (lpeobject) {
299 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
300 this->nodepath = sp_nodepath_new( desktop, lpeobject,
301 (prefs->getInt("/tools/nodes/show_handles", true)),
302 key, item);
303 if (this->nodepath) {
304 this->nodepath->shape_editor = this;
306 // setting new listener
307 Inkscape::XML::Node *repr = SP_OBJECT_REPR(lpeobject);
308 if (repr && repr != nodepath_listener_attached_for) {
309 Inkscape::GC::anchor(repr);
310 sp_repr_add_listener(repr, &shapeeditor_repr_events, this);
311 nodepath_listener_attached_for = repr;
312 }
313 }
314 }
315 }
318 /** FIXME: This thing is only called when the item needs to be updated in response to repr change.
319 Why not make a reload function in NodePath and in KnotHolder? */
320 void ShapeEditor::reset_item (SubType type, bool keep_knotholder)
321 {
322 switch (type) {
323 case SH_NODEPATH:
324 if ( (nodepath) && (IS_LIVEPATHEFFECT(nodepath->object)) ) {
325 char * key = g_strdup(nodepath->repr_key);
326 set_item_lpe_path_parameter(nodepath->item, LIVEPATHEFFECT(nodepath->object), key); // the above checks for nodepath, so it is indeed a path that we are editing
327 g_free(key);
328 } else {
329 SPObject *obj = sp_desktop_document(desktop)->getObjectByRepr(nodepath_listener_attached_for); /// note that it is not certain that this is an SPItem; it could be a LivePathEffectObject.
330 set_item(SP_ITEM(obj), SH_NODEPATH);
331 }
332 break;
333 case SH_KNOTHOLDER:
334 if ( knotholder ) {
335 SPObject *obj = sp_desktop_document(desktop)->getObjectByRepr(knotholder_listener_attached_for); /// note that it is not certain that this is an SPItem; it could be a LivePathEffectObject.
336 set_item(SP_ITEM(obj), 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 (Geom::Point event_p, bool remember) {
352 if (!this->nodepath)
353 return false; // no stroke in knotholder
355 const SPItem *item = get_item(SH_NODEPATH);
357 if (!item || !SP_IS_ITEM(item))
358 return false;
360 //Translate click point into proper coord system
361 this->curvepoint_doc = desktop->w2d(event_p);
362 this->curvepoint_doc *= sp_item_dt2i_affine(item);
364 SPCurve *curve = this->nodepath->curve; // not sure if np->curve is always up to date...
365 Geom::PathVector const &pathv = curve->get_pathvector();
366 boost::optional<Geom::PathVectorPosition> pvpos = Geom::nearestPoint(pathv, this->curvepoint_doc);
367 if (!pvpos) {
368 g_print("Warning! Possible error?\n");
369 return false;
370 }
372 Geom::Point nearest = pathv[pvpos->path_nr].pointAt(pvpos->t);
373 Geom::Point delta = nearest - this->curvepoint_doc;
375 delta = desktop->d2w(delta);
377 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
378 double stroke_tolerance =
379 (( !SP_OBJECT_STYLE(item)->stroke.isNone() ?
380 desktop->current_zoom() *
381 SP_OBJECT_STYLE (item)->stroke_width.computed * 0.5 *
382 to_2geom(sp_item_i2d_affine(item)).descrim()
383 : 0.0)
384 + prefs->getIntLimited("/options/dragtolerance/value", 0, 0, 100)) / to_2geom(sp_item_i2d_affine(item)).descrim();
385 bool close = (Geom::L2 (delta) < stroke_tolerance);
387 if (remember && close) {
388 // calculate index for nodepath's representation.
389 double int_part;
390 double t = std::modf(pvpos->t, &int_part);
391 unsigned int segment_index = (unsigned int)int_part + 1;
392 for (unsigned int i = 0; i < pvpos->path_nr; ++i) {
393 segment_index += pathv[i].size() + 1;
394 if (pathv[i].closed())
395 segment_index += 1;
396 }
398 this->curvepoint_event[Geom::X] = (gint) event_p [Geom::X];
399 this->curvepoint_event[Geom::Y] = (gint) event_p [Geom::Y];
400 this->hit = true;
401 this->grab_t = t;
402 this->grab_node = segment_index;
403 }
405 return close;
406 }
408 void ShapeEditor::add_node_near_point() {
409 if (this->nodepath) {
410 sp_nodepath_add_node_near_point(this->nodepath, this->curvepoint_doc);
411 } else if (this->knotholder) {
412 // we do not add nodes in knotholder... yet
413 }
414 }
416 void ShapeEditor::select_segment_near_point(bool toggle) {
417 if (this->nodepath) {
418 sp_nodepath_select_segment_near_point(this->nodepath, this->curvepoint_doc, toggle);
419 }
420 if (this->knotholder) {
421 // we do not select segments in knotholder... yet?
422 }
423 }
425 void ShapeEditor::cancel_hit() {
426 this->hit = false;
427 }
429 bool ShapeEditor::hits_curve() {
430 return (this->hit);
431 }
434 void ShapeEditor::curve_drag(gdouble eventx, gdouble eventy) {
435 if (this->nodepath && !this->nodepath->straight_path) {
437 if (this->grab_node == -1) // don't know which segment to drag
438 return;
440 // We round off the extra precision in the motion coordinates provided
441 // by some input devices (like tablets). As we'll store the coordinates
442 // as integers in curvepoint_event we need to do this rounding before
443 // comparing them with the last coordinates from curvepoint_event.
444 // See bug #1593499 for details.
446 gint x = (gint) Inkscape::round(eventx);
447 gint y = (gint) Inkscape::round(eventy);
450 // The coordinates hasn't changed since the last motion event, abort
451 if (this->curvepoint_event[Geom::X] == x &&
452 this->curvepoint_event[Geom::Y] == y)
453 return;
455 Geom::Point const delta_w(eventx - this->curvepoint_event[Geom::X],
456 eventy - this->curvepoint_event[Geom::Y]);
457 Geom::Point const delta_dt(this->desktop->w2d(delta_w));
459 sp_nodepath_curve_drag (this->nodepath, this->grab_node, this->grab_t, delta_dt);
460 this->curvepoint_event[Geom::X] = x;
461 this->curvepoint_event[Geom::Y] = y;
463 }
464 if (this->knotholder) {
465 // we do not drag curve in knotholder
466 }
468 }
470 void ShapeEditor::finish_drag() {
471 if (this->nodepath && this->hit) {
472 sp_nodepath_update_repr (this->nodepath, _("Drag curve"));
473 }
474 }
476 void ShapeEditor::select_rect(Geom::Rect const &rect, bool add) {
477 if (this->nodepath) {
478 sp_nodepath_select_rect(this->nodepath, rect, add);
479 }
480 }
482 bool ShapeEditor::has_selection() {
483 if (this->nodepath)
484 return this->nodepath->selected;
485 return false; // so far, knotholder cannot have selection
486 }
488 void ShapeEditor::deselect() {
489 if (this->nodepath)
490 sp_nodepath_deselect(this->nodepath);
491 }
493 void ShapeEditor::add_node () {
494 sp_node_selected_add_node(this->nodepath);
495 }
497 void ShapeEditor::delete_nodes () {
498 sp_node_selected_delete(this->nodepath);
499 }
501 void ShapeEditor::delete_nodes_preserving_shape () {
502 if (this->nodepath && this->nodepath->selected) {
503 sp_node_delete_preserve(g_list_copy(this->nodepath->selected));
504 }
505 }
507 void ShapeEditor::delete_segment () {
508 sp_node_selected_delete_segment(this->nodepath);
509 }
511 void ShapeEditor::set_node_type(int type) {
512 sp_node_selected_set_type(this->nodepath, (Inkscape::NodePath::NodeType) type);
513 }
515 void ShapeEditor::break_at_nodes() {
516 sp_node_selected_break(this->nodepath);
517 }
519 void ShapeEditor::join_nodes() {
520 sp_node_selected_join(this->nodepath);
521 }
523 void ShapeEditor::join_segments() {
524 sp_node_selected_join_segment(this->nodepath);
525 }
527 void ShapeEditor::duplicate_nodes() {
528 sp_node_selected_duplicate(this->nodepath);
529 }
531 void ShapeEditor::set_type_of_segments(NRPathcode code) {
532 sp_node_selected_set_line_type(this->nodepath, code);
533 }
535 void ShapeEditor::move_nodes_screen(SPDesktop *desktop, gdouble dx, gdouble dy) {
536 sp_node_selected_move_screen(desktop, this->nodepath, dx, dy);
537 }
538 void ShapeEditor::move_nodes(gdouble dx, gdouble dy) {
539 sp_node_selected_move(this->nodepath, dx, dy);
540 }
542 void ShapeEditor::rotate_nodes(gdouble angle, int which, bool screen) {
543 if (this->nodepath)
544 sp_nodepath_selected_nodes_rotate (this->nodepath, angle, which, screen);
545 }
547 void ShapeEditor::scale_nodes(gdouble const grow, int const which) {
548 sp_nodepath_selected_nodes_scale(this->nodepath, grow, which);
549 }
550 void ShapeEditor::scale_nodes_screen(gdouble const grow, int const which) {
551 sp_nodepath_selected_nodes_scale_screen(this->nodepath, grow, which);
552 }
554 void ShapeEditor::select_all (bool invert) {
555 if (this->nodepath)
556 sp_nodepath_select_all (this->nodepath, invert);
557 }
558 void ShapeEditor::select_all_from_subpath (bool invert) {
559 if (this->nodepath)
560 sp_nodepath_select_all_from_subpath (this->nodepath, invert);
561 }
562 void ShapeEditor::select_next () {
563 if (this->nodepath) {
564 sp_nodepath_select_next (this->nodepath);
565 if (this->nodepath->numSelected() >= 1) {
566 this->desktop->scroll_to_point(this->nodepath->singleSelectedCoords(), 1.0);
567 }
568 }
569 }
570 void ShapeEditor::select_prev () {
571 if (this->nodepath) {
572 sp_nodepath_select_prev (this->nodepath);
573 if (this->nodepath->numSelected() >= 1) {
574 this->desktop->scroll_to_point(this->nodepath->singleSelectedCoords(), 1.0);
575 }
576 }
577 }
579 void ShapeEditor::show_handles (bool show) {
580 if (this->nodepath && !this->nodepath->straight_path)
581 sp_nodepath_show_handles (this->nodepath, show);
582 }
584 void ShapeEditor::show_helperpath (bool show) {
585 if (this->nodepath)
586 sp_nodepath_show_helperpath (this->nodepath, show);
587 }
589 void ShapeEditor::flip (Geom::Dim2 axis, boost::optional<Geom::Point> center) {
590 if (this->nodepath)
591 sp_nodepath_flip (this->nodepath, axis, center);
592 }
594 void ShapeEditor::distribute (Geom::Dim2 axis) {
595 if (this->nodepath)
596 sp_nodepath_selected_distribute (this->nodepath, axis);
597 }
598 void ShapeEditor::align (Geom::Dim2 axis) {
599 if (this->nodepath)
600 sp_nodepath_selected_align (this->nodepath, axis);
601 }
604 /*
605 Local Variables:
606 mode:c++
607 c-file-style:"stroustrup"
608 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
609 indent-tabs-mode:nil
610 fill-column:99
611 End:
612 */
613 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :