4fe1130bcd48347c66afe8cffcf501f33bfb61c4
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 "nodepath.h"
27 #include "xml/node-event-vector.h"
28 #include "prefs-utils.h"
29 #include "object-edit.h"
30 #include "style.h"
31 #include "display/curve.h"
32 #include <2geom/pathvector.h>
34 #include "shape-editor.h"
37 ShapeEditorsCollective::ShapeEditorsCollective(SPDesktop */*dt*/) {
38 }
40 ShapeEditorsCollective::~ShapeEditorsCollective() {
41 }
44 void ShapeEditorsCollective::update_statusbar() {
46 //!!! move from nodepath: sp_nodepath_update_statusbar but summing for all nodepaths
48 }
50 ShapeEditor::ShapeEditor(SPDesktop *dt) {
51 this->desktop = dt;
52 this->grab_node = -1;
53 this->nodepath = NULL;
54 this->knotholder = NULL;
55 this->hit = false;
56 this->listener_attached_for = NULL;
57 }
59 ShapeEditor::~ShapeEditor() {
60 unset_item(SH_KNOTHOLDER);
61 unset_item(SH_NODEPATH);
62 }
64 void ShapeEditor::unset_item(SubType type, bool keep_knotholder) {
65 Inkscape::XML::Node *old_repr = NULL;
67 switch (type) {
68 case SH_NODEPATH:
69 if (this->nodepath) {
70 old_repr = this->nodepath->repr;
71 if (old_repr && old_repr == listener_attached_for) {
72 sp_repr_remove_listener_by_data(old_repr, this);
73 Inkscape::GC::release(old_repr);
74 listener_attached_for = NULL;
75 }
77 this->grab_node = -1;
78 sp_nodepath_destroy(this->nodepath);
79 this->nodepath = NULL;
80 }
81 break;
82 case SH_KNOTHOLDER:
83 if (this->knotholder) {
84 old_repr = this->knotholder->repr;
85 if (old_repr && old_repr == listener_attached_for) {
86 sp_repr_remove_listener_by_data(old_repr, this);
87 Inkscape::GC::release(old_repr);
88 listener_attached_for = NULL;
89 }
91 if (!keep_knotholder) {
92 delete this->knotholder;
93 this->knotholder = NULL;
94 }
95 }
96 break;
97 }
98 }
100 bool ShapeEditor::has_nodepath () {
101 return (this->nodepath != NULL);
102 }
104 bool ShapeEditor::has_knotholder () {
105 return (this->knotholder != NULL);
106 }
108 void ShapeEditor::update_knotholder () {
109 if (this->knotholder)
110 this->knotholder->update_knots();
111 }
113 bool ShapeEditor::has_local_change (SubType type) {
114 switch (type) {
115 case SH_NODEPATH:
116 return (this->nodepath && this->nodepath->local_change);
117 case SH_KNOTHOLDER:
118 return (this->knotholder && this->knotholder->local_change != 0);
119 default:
120 g_assert_not_reached();
121 }
122 }
124 void ShapeEditor::decrement_local_change (SubType type) {
125 switch (type) {
126 case SH_NODEPATH:
127 if (this->nodepath && this->nodepath->local_change > 0) {
128 this->nodepath->local_change--;
129 }
130 break;
131 case SH_KNOTHOLDER:
132 if (this->knotholder) {
133 this->knotholder->local_change = FALSE;
134 }
135 break;
136 default:
137 g_assert_not_reached();
138 }
139 }
141 const SPItem *ShapeEditor::get_item (SubType type) {
142 const SPItem *item = NULL;
143 switch (type) {
144 case SH_NODEPATH:
145 if (this->has_nodepath()) {
146 item = this->nodepath->item;
147 }
148 break;
149 case SH_KNOTHOLDER:
150 if (this->has_knotholder()) {
151 item = this->knotholder->getItem();
152 }
153 break;
154 }
155 return item;
156 }
158 GList *ShapeEditor::save_nodepath_selection () {
159 if (this->nodepath)
160 return ::save_nodepath_selection (this->nodepath);
161 return NULL;
162 }
164 void ShapeEditor::restore_nodepath_selection (GList *saved) {
165 if (this->nodepath && saved)
166 ::restore_nodepath_selection (this->nodepath, saved);
167 }
169 bool ShapeEditor::nodepath_edits_repr_key(gchar const *name) {
170 if (nodepath && name) {
171 return ( !strcmp(name, nodepath->repr_key) || !strcmp(name, nodepath->repr_nodetypes_key) );
172 }
174 return false;
175 }
177 static void shapeeditor_event_attr_changed(Inkscape::XML::Node */*repr*/, gchar const *name,
178 gchar const */*old_value*/, gchar const */*new_value*/,
179 bool /*is_interactive*/, gpointer data)
180 {
181 gboolean changed_np = FALSE;
182 gboolean changed_kh = FALSE;
184 g_assert(data);
185 ShapeEditor *sh = ((ShapeEditor *) data);
187 if (sh->has_nodepath() && sh->nodepath_edits_repr_key(name))
188 {
189 changed_np = !sh->has_local_change(SH_NODEPATH);
190 sh->decrement_local_change(SH_NODEPATH);
192 }
194 if (changed_np) {
195 GList *saved = NULL;
196 if (sh->has_nodepath()) {
197 saved = sh->save_nodepath_selection();
198 }
200 sh->reset_item(SH_NODEPATH);
202 if (sh->has_nodepath() && saved) {
203 sh->restore_nodepath_selection(saved);
204 g_list_free (saved);
205 }
206 }
209 if (sh->has_knotholder())
210 {
211 changed_kh = !sh->has_local_change(SH_KNOTHOLDER);
212 sh->decrement_local_change(SH_KNOTHOLDER);
213 if (changed_kh) {
214 // this can happen if an LPEItem's knotholder handle was dragged, in which case we want
215 // to keep the knotholder; in all other cases (e.g., if the LPE itself changes) we delete it
216 sh->reset_item(SH_KNOTHOLDER, !strcmp(name, "d"));
217 }
218 }
220 sh->update_statusbar(); //TODO: sh->get_container()->update_statusbar();
221 }
223 static Inkscape::XML::NodeEventVector shapeeditor_repr_events = {
224 NULL, /* child_added */
225 NULL, /* child_removed */
226 shapeeditor_event_attr_changed,
227 NULL, /* content_changed */
228 NULL /* order_changed */
229 };
232 void ShapeEditor::set_item(SPItem *item, SubType type, bool keep_knotholder) {
233 // this happens (and should only happen) when for an LPEItem having both knotholder and nodepath the knotholder
234 // is adapted; in this case we don't want to delete the knotholder since this freezes the handles
235 unset_item(type, keep_knotholder);
237 this->grab_node = -1;
239 if (item) {
240 Inkscape::XML::Node *repr;
241 switch(type) {
242 case SH_NODEPATH:
243 if (SP_IS_LPE_ITEM(item)) {
244 this->nodepath = sp_nodepath_new(desktop, item, (prefs_get_int_attribute("tools.nodes", "show_handles", 1) != 0));
245 }
246 if (this->nodepath) {
247 this->nodepath->shape_editor = this;
249 // setting new listener
250 repr = SP_OBJECT_REPR(item);
251 if (repr != listener_attached_for) {
252 Inkscape::GC::anchor(repr);
253 sp_repr_add_listener(repr, &shapeeditor_repr_events, this);
254 listener_attached_for = repr;
255 }
256 }
257 break;
259 case SH_KNOTHOLDER:
260 if (!this->knotholder) {
261 // only recreate knotholder if none is present
262 this->knotholder = sp_item_knot_holder(item, desktop);
263 }
264 if (this->knotholder) {
265 this->knotholder->update_knots();
266 // setting new listener
267 repr = this->knotholder->repr;
268 if (repr != listener_attached_for) {
269 Inkscape::GC::anchor(repr);
270 sp_repr_add_listener(repr, &shapeeditor_repr_events, this);
271 listener_attached_for = repr;
272 }
273 }
274 break;
275 }
276 }
277 }
279 /** Please note that this function only works for path parameters.
280 * All other parameters probably will crash Inkscape!
281 */
282 void ShapeEditor::set_item_lpe_path_parameter(SPItem *item, SPObject *lpeobject, const char * key)
283 {
284 unset_item(SH_NODEPATH);
286 this->grab_node = -1;
288 if (lpeobject) {
289 this->nodepath = sp_nodepath_new( desktop, lpeobject,
290 (prefs_get_int_attribute("tools.nodes", "show_handles", 1) != 0),
291 key, item);
292 if (this->nodepath) {
293 this->nodepath->shape_editor = this;
295 // setting new listener
296 Inkscape::XML::Node *repr = SP_OBJECT_REPR(lpeobject);
297 if (repr && repr != listener_attached_for) {
298 Inkscape::GC::anchor(repr);
299 sp_repr_add_listener(repr, &shapeeditor_repr_events, this);
300 listener_attached_for = repr;
301 }
302 }
303 }
304 }
306 /**
307 * pass a new knotholder to ShapeEditor to manage (and delete)
308 */
309 void
310 ShapeEditor::set_knotholder(KnotHolder * knot_holder)
311 {
312 unset_item(SH_KNOTHOLDER);
314 this->grab_node = -1;
316 if (knot_holder) {
317 this->knotholder = knot_holder;
318 }
319 }
322 /** FIXME: think about this. Is this thing only called when the item needs to be updated?
323 Why not make a reload function in NodePath and in KnotHolder? */
324 void ShapeEditor::reset_item (SubType type, bool keep_knotholder)
325 {
326 switch (type) {
327 case SH_NODEPATH:
328 if ( (this->nodepath) && (IS_LIVEPATHEFFECT(this->nodepath->object)) ) {
329 SPItem * item = this->nodepath->item;
330 SPObject *obj = this->nodepath->object;
331 char * key = g_strdup(this->nodepath->repr_key);
332 set_item_lpe_path_parameter(item, obj, key); // the above checks for nodepath, so it is indeed a path that we are editing
333 g_free(key);
334 } else {
335 SPItem * item = (SPItem *) get_item(SH_NODEPATH);
336 set_item(item, SH_NODEPATH);
337 }
338 break;
339 case SH_KNOTHOLDER:
340 if (this->knotholder) {
341 SPItem * item = (SPItem *) get_item(SH_KNOTHOLDER);
342 set_item(item, SH_KNOTHOLDER, keep_knotholder);
343 }
344 break;
345 }
346 }
348 void ShapeEditor::nodepath_destroyed () {
349 this->nodepath = NULL;
350 }
352 void ShapeEditor::update_statusbar () {
353 if (this->nodepath)
354 sp_nodepath_update_statusbar(this->nodepath);
355 }
357 bool ShapeEditor::is_over_stroke (Geom::Point event_p, bool remember) {
358 if (!this->nodepath)
359 return false; // no stroke in knotholder
361 const SPItem *item = get_item(SH_NODEPATH);
363 //Translate click point into proper coord system
364 this->curvepoint_doc = desktop->w2d(event_p);
365 this->curvepoint_doc *= sp_item_dt2i_affine(item);
367 SPCurve *curve = this->nodepath->curve; // not sure if np->curve is always up to date...
368 Geom::PathVector const &pathv = curve->get_pathvector();
369 boost::optional<Geom::PathVectorPosition> pvpos = Geom::nearestPoint(pathv, this->curvepoint_doc);
370 if (!pvpos) {
371 g_print("Warning! Possible error?\n");
372 return false;
373 }
375 Geom::Point nearest = pathv[pvpos->path_nr].pointAt(pvpos->t);
376 Geom::Point delta = nearest - this->curvepoint_doc;
378 delta = desktop->d2w(delta);
380 double stroke_tolerance =
381 (( !SP_OBJECT_STYLE(item)->stroke.isNone() ?
382 desktop->current_zoom() *
383 SP_OBJECT_STYLE (item)->stroke_width.computed * 0.5 *
384 to_2geom(sp_item_i2d_affine(item)).descrim()
385 : 0.0)
386 + prefs_get_int_attribute_limited("options.dragtolerance", "value", 0, 0, 100)) / to_2geom(sp_item_i2d_affine(item)).descrim();
387 bool close = (Geom::L2 (delta) < stroke_tolerance);
389 if (remember && close) {
390 // calculate index for nodepath's representation.
391 double int_part;
392 double t = std::modf(pvpos->t, &int_part);
393 unsigned int segment_index = (unsigned int)int_part + 1;
394 for (unsigned int i = 0; i < pvpos->path_nr; ++i) {
395 segment_index += pathv[i].size() + 1;
396 if (pathv[i].closed())
397 segment_index += 1;
398 }
400 this->curvepoint_event[Geom::X] = (gint) event_p [Geom::X];
401 this->curvepoint_event[Geom::Y] = (gint) event_p [Geom::Y];
402 this->hit = true;
403 this->grab_t = t;
404 this->grab_node = segment_index;
405 }
407 return close;
408 }
410 void ShapeEditor::add_node_near_point() {
411 if (this->nodepath) {
412 sp_nodepath_add_node_near_point(this->nodepath, this->curvepoint_doc);
413 } else if (this->knotholder) {
414 // we do not add nodes in knotholder... yet
415 }
416 }
418 void ShapeEditor::select_segment_near_point(bool toggle) {
419 if (this->nodepath) {
420 sp_nodepath_select_segment_near_point(this->nodepath, this->curvepoint_doc, toggle);
421 }
422 if (this->knotholder) {
423 // we do not select segments in knotholder... yet?
424 }
425 }
427 void ShapeEditor::cancel_hit() {
428 this->hit = false;
429 }
431 bool ShapeEditor::hits_curve() {
432 return (this->hit);
433 }
436 void ShapeEditor::curve_drag(gdouble eventx, gdouble eventy) {
437 if (this->nodepath && !this->nodepath->straight_path) {
439 if (this->grab_node == -1) // don't know which segment to drag
440 return;
442 // We round off the extra precision in the motion coordinates provided
443 // by some input devices (like tablets). As we'll store the coordinates
444 // as integers in curvepoint_event we need to do this rounding before
445 // comparing them with the last coordinates from curvepoint_event.
446 // See bug #1593499 for details.
448 gint x = (gint) Inkscape::round(eventx);
449 gint y = (gint) Inkscape::round(eventy);
452 // The coordinates hasn't changed since the last motion event, abort
453 if (this->curvepoint_event[Geom::X] == x &&
454 this->curvepoint_event[Geom::Y] == y)
455 return;
457 Geom::Point const delta_w(eventx - this->curvepoint_event[Geom::X],
458 eventy - this->curvepoint_event[Geom::Y]);
459 Geom::Point const delta_dt(this->desktop->w2d(delta_w));
461 sp_nodepath_curve_drag (this->nodepath, this->grab_node, this->grab_t, delta_dt);
462 this->curvepoint_event[Geom::X] = x;
463 this->curvepoint_event[Geom::Y] = y;
465 }
466 if (this->knotholder) {
467 // we do not drag curve in knotholder
468 }
470 }
472 void ShapeEditor::finish_drag() {
473 if (this->nodepath && this->hit) {
474 sp_nodepath_update_repr (this->nodepath, _("Drag curve"));
475 }
476 }
478 void ShapeEditor::select_rect(Geom::Rect const &rect, bool add) {
479 if (this->nodepath) {
480 sp_nodepath_select_rect(this->nodepath, rect, add);
481 }
482 }
484 bool ShapeEditor::has_selection() {
485 if (this->nodepath)
486 return this->nodepath->selected;
487 return false; // so far, knotholder cannot have selection
488 }
490 void ShapeEditor::deselect() {
491 if (this->nodepath)
492 sp_nodepath_deselect(this->nodepath);
493 }
495 void ShapeEditor::add_node () {
496 sp_node_selected_add_node(this->nodepath);
497 }
499 void ShapeEditor::delete_nodes () {
500 sp_node_selected_delete(this->nodepath);
501 }
503 void ShapeEditor::delete_nodes_preserving_shape () {
504 if (this->nodepath && this->nodepath->selected) {
505 sp_node_delete_preserve(g_list_copy(this->nodepath->selected));
506 }
507 }
509 void ShapeEditor::delete_segment () {
510 sp_node_selected_delete_segment(this->nodepath);
511 }
513 void ShapeEditor::set_node_type(int type) {
514 sp_node_selected_set_type(this->nodepath, (Inkscape::NodePath::NodeType) type);
515 }
517 void ShapeEditor::break_at_nodes() {
518 sp_node_selected_break(this->nodepath);
519 }
521 void ShapeEditor::join_nodes() {
522 sp_node_selected_join(this->nodepath);
523 }
525 void ShapeEditor::join_segments() {
526 sp_node_selected_join_segment(this->nodepath);
527 }
529 void ShapeEditor::duplicate_nodes() {
530 sp_node_selected_duplicate(this->nodepath);
531 }
533 void ShapeEditor::set_type_of_segments(NRPathcode code) {
534 sp_node_selected_set_line_type(this->nodepath, code);
535 }
537 void ShapeEditor::move_nodes_screen(SPDesktop *desktop, gdouble dx, gdouble dy) {
538 sp_node_selected_move_screen(desktop, this->nodepath, dx, dy);
539 }
540 void ShapeEditor::move_nodes(gdouble dx, gdouble dy) {
541 sp_node_selected_move(this->nodepath, dx, dy);
542 }
544 void ShapeEditor::rotate_nodes(gdouble angle, int which, bool screen) {
545 if (this->nodepath)
546 sp_nodepath_selected_nodes_rotate (this->nodepath, angle, which, screen);
547 }
549 void ShapeEditor::scale_nodes(gdouble const grow, int const which) {
550 sp_nodepath_selected_nodes_scale(this->nodepath, grow, which);
551 }
552 void ShapeEditor::scale_nodes_screen(gdouble const grow, int const which) {
553 sp_nodepath_selected_nodes_scale_screen(this->nodepath, grow, which);
554 }
556 void ShapeEditor::select_all (bool invert) {
557 if (this->nodepath)
558 sp_nodepath_select_all (this->nodepath, invert);
559 }
560 void ShapeEditor::select_all_from_subpath (bool invert) {
561 if (this->nodepath)
562 sp_nodepath_select_all_from_subpath (this->nodepath, invert);
563 }
564 void ShapeEditor::select_next () {
565 if (this->nodepath) {
566 sp_nodepath_select_next (this->nodepath);
567 if (this->nodepath->numSelected() >= 1) {
568 this->desktop->scroll_to_point(this->nodepath->singleSelectedCoords(), 1.0);
569 }
570 }
571 }
572 void ShapeEditor::select_prev () {
573 if (this->nodepath) {
574 sp_nodepath_select_prev (this->nodepath);
575 if (this->nodepath->numSelected() >= 1) {
576 this->desktop->scroll_to_point(this->nodepath->singleSelectedCoords(), 1.0);
577 }
578 }
579 }
581 void ShapeEditor::show_handles (bool show) {
582 if (this->nodepath && !this->nodepath->straight_path)
583 sp_nodepath_show_handles (this->nodepath, show);
584 }
586 void ShapeEditor::show_helperpath (bool show) {
587 if (this->nodepath)
588 sp_nodepath_show_helperpath (this->nodepath, show);
589 }
591 void ShapeEditor::flip (Geom::Dim2 axis, boost::optional<Geom::Point> center) {
592 if (this->nodepath)
593 sp_nodepath_flip (this->nodepath, axis, center);
594 }
596 void ShapeEditor::distribute (Geom::Dim2 axis) {
597 if (this->nodepath)
598 sp_nodepath_selected_distribute (this->nodepath, axis);
599 }
600 void ShapeEditor::align (Geom::Dim2 axis) {
601 if (this->nodepath)
602 sp_nodepath_selected_align (this->nodepath, axis);
603 }
606 /*
607 Local Variables:
608 mode:c++
609 c-file-style:"stroustrup"
610 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
611 indent-tabs-mode:nil
612 fill-column:99
613 End:
614 */
615 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :