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"
38 ShapeEditor::ShapeEditor(SPDesktop *dt) {
39 this->desktop = dt;
40 this->grab_node = -1;
41 this->nodepath = NULL;
42 this->knotholder = NULL;
43 this->hit = false;
44 this->knotholder_listener_attached_for = NULL;
45 this->nodepath_listener_attached_for = NULL;
46 }
48 ShapeEditor::~ShapeEditor() {
49 unset_item(SH_KNOTHOLDER);
50 unset_item(SH_NODEPATH);
51 }
53 void ShapeEditor::unset_item(SubType type, bool keep_knotholder) {
54 Inkscape::XML::Node *old_repr = NULL;
56 switch (type) {
57 case SH_NODEPATH:
58 if (this->nodepath) {
59 old_repr = this->nodepath->repr;
60 if (old_repr && old_repr == nodepath_listener_attached_for) {
61 sp_repr_remove_listener_by_data(old_repr, this);
62 Inkscape::GC::release(old_repr);
63 nodepath_listener_attached_for = NULL;
64 }
66 this->grab_node = -1;
67 delete this->nodepath;
68 this->nodepath = NULL;
69 }
70 break;
71 case SH_KNOTHOLDER:
72 if (this->knotholder) {
73 old_repr = this->knotholder->repr;
74 if (old_repr && old_repr == knotholder_listener_attached_for) {
75 sp_repr_remove_listener_by_data(old_repr, this);
76 Inkscape::GC::release(old_repr);
77 knotholder_listener_attached_for = NULL;
78 }
80 if (!keep_knotholder) {
81 delete this->knotholder;
82 this->knotholder = NULL;
83 }
84 }
85 break;
86 }
87 }
89 bool ShapeEditor::has_nodepath () {
90 return (this->nodepath != NULL);
91 }
93 bool ShapeEditor::has_knotholder () {
94 return (this->knotholder != NULL);
95 }
97 void ShapeEditor::update_knotholder () {
98 if (this->knotholder)
99 this->knotholder->update_knots();
100 }
102 bool ShapeEditor::has_local_change (SubType type) {
103 switch (type) {
104 case SH_NODEPATH:
105 return (this->nodepath && this->nodepath->local_change);
106 case SH_KNOTHOLDER:
107 return (this->knotholder && this->knotholder->local_change != 0);
108 default:
109 g_assert_not_reached();
110 }
111 }
113 void ShapeEditor::decrement_local_change (SubType type) {
114 switch (type) {
115 case SH_NODEPATH:
116 if (this->nodepath && this->nodepath->local_change > 0) {
117 this->nodepath->local_change--;
118 }
119 break;
120 case SH_KNOTHOLDER:
121 if (this->knotholder) {
122 this->knotholder->local_change = FALSE;
123 }
124 break;
125 default:
126 g_assert_not_reached();
127 }
128 }
130 const SPItem *ShapeEditor::get_item (SubType type) {
131 const SPItem *item = NULL;
132 switch (type) {
133 case SH_NODEPATH:
134 if (this->has_nodepath()) {
135 item = this->nodepath->item;
136 }
137 break;
138 case SH_KNOTHOLDER:
139 if (this->has_knotholder()) {
140 item = this->knotholder->getItem();
141 }
142 break;
143 }
144 return item;
145 }
147 GList *ShapeEditor::save_nodepath_selection () {
148 if (this->nodepath)
149 return ::save_nodepath_selection (this->nodepath);
150 return NULL;
151 }
153 void ShapeEditor::restore_nodepath_selection (GList *saved) {
154 if (this->nodepath && saved)
155 ::restore_nodepath_selection (this->nodepath, saved);
156 }
158 bool ShapeEditor::nodepath_edits_repr_key(gchar const *name) {
159 if (nodepath && name) {
160 return ( !strcmp(name, nodepath->repr_key) || !strcmp(name, nodepath->repr_nodetypes_key) );
161 }
163 return false;
164 }
167 void ShapeEditor::shapeeditor_event_attr_changed(gchar const *name)
168 {
169 gboolean changed_np = FALSE;
170 gboolean changed_kh = FALSE;
172 if (has_nodepath() && nodepath_edits_repr_key(name))
173 {
174 changed_np = !has_local_change(SH_NODEPATH);
175 decrement_local_change(SH_NODEPATH);
176 }
178 if (changed_np) {
179 GList *saved = NULL;
180 if (has_nodepath()) {
181 saved = save_nodepath_selection();
182 }
184 reset_item(SH_NODEPATH);
186 if (has_nodepath() && saved) {
187 restore_nodepath_selection(saved);
188 g_list_free (saved);
189 }
190 }
192 if (has_knotholder())
193 {
194 changed_kh = !has_local_change(SH_KNOTHOLDER);
195 decrement_local_change(SH_KNOTHOLDER);
196 if (changed_kh) {
197 // this can happen if an LPEItem's knotholder handle was dragged, in which case we want
198 // to keep the knotholder; in all other cases (e.g., if the LPE itself changes) we delete it
199 reset_item(SH_KNOTHOLDER, !strcmp(name, "d"));
200 }
201 }
203 update_statusbar(); //TODO: get_container()->update_statusbar();
204 }
207 static void shapeeditor_event_attr_changed(Inkscape::XML::Node */*repr*/, gchar const *name,
208 gchar const */*old_value*/, gchar const */*new_value*/,
209 bool /*is_interactive*/, gpointer data)
210 {
211 g_assert(data);
212 ShapeEditor *sh = ((ShapeEditor *) data);
214 sh->shapeeditor_event_attr_changed(name);
215 }
217 static Inkscape::XML::NodeEventVector shapeeditor_repr_events = {
218 NULL, /* child_added */
219 NULL, /* child_removed */
220 shapeeditor_event_attr_changed,
221 NULL, /* content_changed */
222 NULL /* order_changed */
223 };
226 void ShapeEditor::set_item(SPItem *item, SubType type, bool keep_knotholder) {
227 // this happens (and should only happen) when for an LPEItem having both knotholder and
228 // nodepath the knotholder is adapted; in this case we don't want to delete the knotholder
229 // since this freezes the handles
230 unset_item(type, keep_knotholder);
232 this->grab_node = -1;
234 if (item) {
235 Inkscape::XML::Node *repr;
236 switch(type) {
237 case SH_NODEPATH:
238 if (SP_IS_LPE_ITEM(item)) {
239 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
240 this->nodepath = sp_nodepath_new(desktop, item, (prefs->getBool("/tools/nodes/show_handles", true)));
241 }
242 if (this->nodepath) {
243 this->nodepath->shape_editor = this;
245 // setting new listener
246 repr = SP_OBJECT_REPR(item);
247 if (repr != nodepath_listener_attached_for) {
248 Inkscape::GC::anchor(repr);
249 sp_repr_add_listener(repr, &shapeeditor_repr_events, this);
250 nodepath_listener_attached_for = repr;
251 }
252 }
253 break;
255 case SH_KNOTHOLDER:
256 if (!this->knotholder) {
257 // only recreate knotholder if none is present
258 this->knotholder = sp_item_knot_holder(item, desktop);
259 }
260 if (this->knotholder) {
261 this->knotholder->update_knots();
262 // setting new listener
263 repr = this->knotholder->repr;
264 if (repr != knotholder_listener_attached_for) {
265 Inkscape::GC::anchor(repr);
266 sp_repr_add_listener(repr, &shapeeditor_repr_events, this);
267 knotholder_listener_attached_for = repr;
268 }
269 }
270 break;
271 }
272 }
273 }
275 /** Please note that this function only works for path parameters.
276 * All other parameters probably will crash Inkscape!
277 */
278 void ShapeEditor::set_item_lpe_path_parameter(SPItem *item, LivePathEffectObject *lpeobject, const char * key)
279 {
280 unset_item(SH_NODEPATH);
282 this->grab_node = -1;
284 if (lpeobject) {
285 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
286 this->nodepath = sp_nodepath_new( desktop, lpeobject,
287 (prefs->getInt("/tools/nodes/show_handles", true)),
288 key, item);
289 if (this->nodepath) {
290 this->nodepath->shape_editor = this;
292 // setting new listener
293 Inkscape::XML::Node *repr = SP_OBJECT_REPR(lpeobject);
294 if (repr && repr != nodepath_listener_attached_for) {
295 Inkscape::GC::anchor(repr);
296 sp_repr_add_listener(repr, &shapeeditor_repr_events, this);
297 nodepath_listener_attached_for = repr;
298 }
299 }
300 }
301 }
304 /** FIXME: This thing is only called when the item needs to be updated in response to repr change.
305 Why not make a reload function in NodePath and in KnotHolder? */
306 void ShapeEditor::reset_item (SubType type, bool keep_knotholder)
307 {
308 switch (type) {
309 case SH_NODEPATH:
310 if ( (nodepath) && (IS_LIVEPATHEFFECT(nodepath->object)) ) {
311 char * key = g_strdup(nodepath->repr_key);
312 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
313 g_free(key);
314 } else {
315 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.
316 set_item(SP_ITEM(obj), SH_NODEPATH);
317 }
318 break;
319 case SH_KNOTHOLDER:
320 if ( knotholder ) {
321 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.
322 set_item(SP_ITEM(obj), SH_KNOTHOLDER, keep_knotholder);
323 }
324 break;
325 }
326 }
328 void ShapeEditor::nodepath_destroyed () {
329 this->nodepath = NULL;
330 }
332 void ShapeEditor::update_statusbar () {
333 if (this->nodepath)
334 sp_nodepath_update_statusbar(this->nodepath);
335 }
337 bool ShapeEditor::is_over_stroke (Geom::Point event_p, bool remember) {
338 if (!this->nodepath)
339 return false; // no stroke in knotholder
341 const SPItem *item = get_item(SH_NODEPATH);
343 if (!item || !SP_IS_ITEM(item))
344 return false;
346 //Translate click point into proper coord system
347 this->curvepoint_doc = desktop->w2d(event_p);
348 this->curvepoint_doc *= sp_item_dt2i_affine(item);
350 SPCurve *curve = this->nodepath->curve; // not sure if np->curve is always up to date...
351 Geom::PathVector const &pathv = curve->get_pathvector();
352 boost::optional<Geom::PathVectorPosition> pvpos = Geom::nearestPoint(pathv, this->curvepoint_doc);
353 if (!pvpos) {
354 g_print("Warning! Possible error?\n");
355 return false;
356 }
358 Geom::Point nearest = pathv[pvpos->path_nr].pointAt(pvpos->t);
359 Geom::Point delta = nearest - this->curvepoint_doc;
361 delta = desktop->d2w(delta);
363 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
364 double stroke_tolerance =
365 (( !SP_OBJECT_STYLE(item)->stroke.isNone() ?
366 desktop->current_zoom() *
367 SP_OBJECT_STYLE (item)->stroke_width.computed * 0.5 *
368 to_2geom(sp_item_i2d_affine(item)).descrim()
369 : 0.0)
370 + prefs->getIntLimited("/options/dragtolerance/value", 0, 0, 100)) / to_2geom(sp_item_i2d_affine(item)).descrim();
371 bool close = (Geom::L2 (delta) < stroke_tolerance);
373 if (remember && close) {
374 // calculate index for nodepath's representation.
375 double int_part;
376 double t = std::modf(pvpos->t, &int_part);
377 unsigned int segment_index = (unsigned int)int_part + 1;
378 for (unsigned int i = 0; i < pvpos->path_nr; ++i) {
379 segment_index += pathv[i].size() + 1;
380 if (pathv[i].closed())
381 segment_index += 1;
382 }
384 this->curvepoint_event[Geom::X] = (gint) event_p [Geom::X];
385 this->curvepoint_event[Geom::Y] = (gint) event_p [Geom::Y];
386 this->hit = true;
387 this->grab_t = t;
388 this->grab_node = segment_index;
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[Geom::X] == x &&
438 this->curvepoint_event[Geom::Y] == y)
439 return;
441 Geom::Point const delta_w(eventx - this->curvepoint_event[Geom::X],
442 eventy - this->curvepoint_event[Geom::Y]);
443 Geom::Point const delta_dt(this->desktop->w2d(delta_w));
445 sp_nodepath_curve_drag (this->nodepath, this->grab_node, this->grab_t, delta_dt);
446 this->curvepoint_event[Geom::X] = x;
447 this->curvepoint_event[Geom::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(Geom::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(SPDesktop *desktop, gdouble dx, gdouble dy) {
522 sp_node_selected_move_screen(desktop, 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 (Geom::Dim2 axis, boost::optional<Geom::Point> center) {
576 if (this->nodepath)
577 sp_nodepath_flip (this->nodepath, axis, center);
578 }
580 void ShapeEditor::distribute (Geom::Dim2 axis) {
581 if (this->nodepath)
582 sp_nodepath_selected_distribute (this->nodepath, axis);
583 }
584 void ShapeEditor::align (Geom::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 :