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 }
58 ShapeEditor::~ShapeEditor() {
59 unset_item(SH_KNOTHOLDER);
60 unset_item(SH_NODEPATH);
61 }
63 void ShapeEditor::unset_item(SubType type, bool keep_knotholder) {
64 Inkscape::XML::Node *old_repr = NULL;
66 switch (type) {
67 case SH_NODEPATH:
68 if (this->nodepath) {
69 old_repr = this->nodepath->repr;
70 sp_repr_remove_listener_by_data(old_repr, this);
71 Inkscape::GC::release(old_repr);
73 this->grab_node = -1;
74 sp_nodepath_destroy(this->nodepath);
75 this->nodepath = NULL;
76 }
77 break;
78 case SH_KNOTHOLDER:
79 if (this->knotholder) {
80 old_repr = this->knotholder->repr;
81 sp_repr_remove_listener_by_data(old_repr, this);
82 Inkscape::GC::release(old_repr);
84 if (!keep_knotholder) {
85 delete this->knotholder;
86 this->knotholder = NULL;
87 }
88 }
89 break;
90 }
91 }
93 bool ShapeEditor::has_nodepath () {
94 return (this->nodepath != NULL);
95 }
97 bool ShapeEditor::has_knotholder () {
98 return (this->knotholder != NULL);
99 }
101 void ShapeEditor::update_knotholder () {
102 if (this->knotholder)
103 this->knotholder->update_knots();
104 }
106 bool ShapeEditor::has_local_change (SubType type) {
107 switch (type) {
108 case SH_NODEPATH:
109 return (this->nodepath && this->nodepath->local_change);
110 case SH_KNOTHOLDER:
111 return (this->knotholder && this->knotholder->local_change != 0);
112 default:
113 g_assert_not_reached();
114 }
115 }
117 void ShapeEditor::decrement_local_change (SubType type) {
118 switch (type) {
119 case SH_NODEPATH:
120 if (this->nodepath && this->nodepath->local_change > 0) {
121 this->nodepath->local_change--;
122 }
123 break;
124 case SH_KNOTHOLDER:
125 if (this->knotholder) {
126 this->knotholder->local_change = FALSE;
127 }
128 break;
129 default:
130 g_assert_not_reached();
131 }
132 }
134 const SPItem *ShapeEditor::get_item (SubType type) {
135 const SPItem *item = NULL;
136 switch (type) {
137 case SH_NODEPATH:
138 if (this->has_nodepath()) {
139 item = this->nodepath->item;
140 }
141 break;
142 case SH_KNOTHOLDER:
143 if (this->has_knotholder()) {
144 item = this->knotholder->getItem();
145 }
146 break;
147 }
148 return item;
149 }
151 GList *ShapeEditor::save_nodepath_selection () {
152 if (this->nodepath)
153 return ::save_nodepath_selection (this->nodepath);
154 return NULL;
155 }
157 void ShapeEditor::restore_nodepath_selection (GList *saved) {
158 if (this->nodepath && saved)
159 ::restore_nodepath_selection (this->nodepath, saved);
160 }
162 bool ShapeEditor::nodepath_edits_repr_key(gchar const *name) {
163 if (nodepath && name) {
164 return ( !strcmp(name, nodepath->repr_key) || !strcmp(name, nodepath->repr_nodetypes_key) );
165 }
167 return false;
168 }
170 static void shapeeditor_event_attr_changed(Inkscape::XML::Node */*repr*/, gchar const *name,
171 gchar const */*old_value*/, gchar const */*new_value*/,
172 bool /*is_interactive*/, gpointer data)
173 {
174 gboolean changed_np = FALSE;
175 gboolean changed_kh = FALSE;
177 g_assert(data);
178 ShapeEditor *sh = ((ShapeEditor *) data);
180 if (sh->has_nodepath() && sh->nodepath_edits_repr_key(name))
181 {
182 changed_np = !sh->has_local_change(SH_NODEPATH);
183 sh->decrement_local_change(SH_NODEPATH);
185 }
187 if (changed_np) {
188 GList *saved = NULL;
189 if (sh->has_nodepath()) {
190 saved = sh->save_nodepath_selection();
191 }
193 sh->reset_item(SH_NODEPATH);
195 if (sh->has_nodepath() && saved) {
196 sh->restore_nodepath_selection(saved);
197 g_list_free (saved);
198 }
199 }
202 if (sh->has_knotholder())
203 {
204 changed_kh = !sh->has_local_change(SH_KNOTHOLDER);
205 sh->decrement_local_change(SH_KNOTHOLDER);
206 if (changed_kh) {
207 // this can happen if an LPEItem's knotholder handle was dragged, in which case we want
208 // to keep the knotholder; in all other cases (e.g., if the LPE itself changes) we delete it
209 sh->reset_item(SH_KNOTHOLDER, !strcmp(name, "d"));
210 }
211 }
213 sh->update_statusbar(); //TODO: sh->get_container()->update_statusbar();
214 }
216 static Inkscape::XML::NodeEventVector shapeeditor_repr_events = {
217 NULL, /* child_added */
218 NULL, /* child_removed */
219 shapeeditor_event_attr_changed,
220 NULL, /* content_changed */
221 NULL /* order_changed */
222 };
225 void ShapeEditor::set_item(SPItem *item, SubType type, bool keep_knotholder) {
226 // this happens (and should only happen) when for an LPEItem having both knotholder and nodepath the knotholder
227 // is adapted; in this case we don't want to delete the knotholder since this freezes the handles
228 unset_item(type, keep_knotholder);
230 this->grab_node = -1;
232 if (item) {
233 Inkscape::XML::Node *repr;
234 switch(type) {
235 case SH_NODEPATH:
236 if (SP_IS_LPE_ITEM(item)) {
237 this->nodepath = sp_nodepath_new(desktop, item, (prefs_get_int_attribute("tools.nodes", "show_handles", 1) != 0));
238 }
239 if (this->nodepath) {
240 this->nodepath->shape_editor = this;
242 // setting new listener
243 repr = SP_OBJECT_REPR(item);
244 Inkscape::GC::anchor(repr);
245 sp_repr_add_listener(repr, &shapeeditor_repr_events, this);
246 }
247 break;
249 case SH_KNOTHOLDER:
250 if (!this->knotholder) {
251 // only recreate knotholder if none is present
252 this->knotholder = sp_item_knot_holder(item, desktop);
253 }
254 if (this->knotholder) {
255 this->knotholder->update_knots();
256 // setting new listener
257 repr = this->knotholder->repr;
258 Inkscape::GC::anchor(repr);
259 sp_repr_add_listener(repr, &shapeeditor_repr_events, this);
260 }
261 break;
262 }
263 }
264 }
266 /** Please note that this function only works for path parameters.
267 * All other parameters probably will crash Inkscape!
268 */
269 void ShapeEditor::set_item_lpe_path_parameter(SPItem *item, SPObject *lpeobject, const char * key)
270 {
271 unset_item(SH_NODEPATH);
273 this->grab_node = -1;
275 if (lpeobject) {
276 this->nodepath = sp_nodepath_new( desktop, lpeobject,
277 (prefs_get_int_attribute("tools.nodes", "show_handles", 1) != 0),
278 key, item);
279 if (this->nodepath) {
280 this->nodepath->shape_editor = this;
282 // setting new listener
283 Inkscape::XML::Node *repr = SP_OBJECT_REPR(lpeobject);
284 if (repr) {
285 Inkscape::GC::anchor(repr);
286 sp_repr_add_listener(repr, &shapeeditor_repr_events, this);
287 }
288 }
289 }
290 }
292 /**
293 * pass a new knotholder to ShapeEditor to manage (and delete)
294 */
295 void
296 ShapeEditor::set_knotholder(KnotHolder * knot_holder)
297 {
298 unset_item(SH_KNOTHOLDER);
300 this->grab_node = -1;
302 if (knot_holder) {
303 this->knotholder = knot_holder;
304 }
305 }
308 /** FIXME: think about this. Is this thing only called when the item needs to be updated?
309 Why not make a reload function in NodePath and in KnotHolder? */
310 void ShapeEditor::reset_item (SubType type, bool keep_knotholder)
311 {
312 switch (type) {
313 case SH_NODEPATH:
314 if ( (this->nodepath) && (IS_LIVEPATHEFFECT(this->nodepath->object)) ) {
315 SPItem * item = this->nodepath->item;
316 SPObject *obj = this->nodepath->object;
317 char * key = g_strdup(this->nodepath->repr_key);
318 set_item_lpe_path_parameter(item, obj, key); // the above checks for nodepath, so it is indeed a path that we are editing
319 g_free(key);
320 } else {
321 SPItem * item = (SPItem *) get_item(SH_NODEPATH);
322 set_item(item, SH_NODEPATH);
323 }
324 break;
325 case SH_KNOTHOLDER:
326 if (this->knotholder) {
327 SPItem * item = (SPItem *) get_item(SH_KNOTHOLDER);
328 set_item(item, SH_KNOTHOLDER, keep_knotholder);
329 }
330 break;
331 }
332 }
334 void ShapeEditor::nodepath_destroyed () {
335 this->nodepath = NULL;
336 }
338 void ShapeEditor::update_statusbar () {
339 if (this->nodepath)
340 sp_nodepath_update_statusbar(this->nodepath);
341 }
343 bool ShapeEditor::is_over_stroke (NR::Point event_p, bool remember) {
345 if (!this->nodepath)
346 return false; // no stroke in knotholder
348 const SPItem *item = get_item(SH_NODEPATH);
350 //Translate click point into proper coord system
351 this->curvepoint_doc = desktop->w2d(event_p);
352 this->curvepoint_doc *= sp_item_dt2i_affine(item);
354 SPCurve *curve = this->nodepath->curve; // not sure if np->curve is always up to date...
355 Geom::PathVector const &pathv = curve->get_pathvector();
356 Geom::PathVectorPosition pvpos = Geom::nearestPoint(pathv, this->curvepoint_doc);
358 NR::Point nearest = pathv[pvpos.path_nr].pointAt(pvpos.t);
359 NR::Point delta = nearest - this->curvepoint_doc;
361 delta = desktop->d2w(delta);
363 double stroke_tolerance =
364 (( !SP_OBJECT_STYLE(item)->stroke.isNone() ?
365 desktop->current_zoom() *
366 SP_OBJECT_STYLE (item)->stroke_width.computed * 0.5 *
367 NR::expansion(sp_item_i2d_affine(item))
368 : 0.0)
369 + prefs_get_int_attribute_limited("options.dragtolerance", "value", 0, 0, 100)) /NR::expansion(sp_item_i2d_affine(item));
370 bool close = (NR::L2 (delta) < stroke_tolerance);
372 if (remember && close) {
373 // calculate index for nodepath's representation.
374 double int_part;
375 double t = std::modf(pvpos.t, &int_part);
376 unsigned int segment_index = (unsigned int)int_part + 1;
377 for (unsigned int i = 0; i < pvpos.path_nr; ++i) {
378 segment_index += pathv[i].size() + 1;
379 if (pathv[i].closed())
380 segment_index += 1;
381 }
383 this->curvepoint_event[NR::X] = (gint) event_p [NR::X];
384 this->curvepoint_event[NR::Y] = (gint) event_p [NR::Y];
385 this->hit = true;
386 this->grab_t = t;
387 this->grab_node = segment_index;
388 }
390 return close;
391 }
393 void ShapeEditor::add_node_near_point() {
394 if (this->nodepath) {
395 sp_nodepath_add_node_near_point(this->nodepath, this->curvepoint_doc);
396 } else if (this->knotholder) {
397 // we do not add nodes in knotholder... yet
398 }
399 }
401 void ShapeEditor::select_segment_near_point(bool toggle) {
402 if (this->nodepath) {
403 sp_nodepath_select_segment_near_point(this->nodepath, this->curvepoint_doc, toggle);
404 }
405 if (this->knotholder) {
406 // we do not select segments in knotholder... yet?
407 }
408 }
410 void ShapeEditor::cancel_hit() {
411 this->hit = false;
412 }
414 bool ShapeEditor::hits_curve() {
415 return (this->hit);
416 }
419 void ShapeEditor::curve_drag(gdouble eventx, gdouble eventy) {
420 if (this->nodepath && !this->nodepath->straight_path) {
422 if (this->grab_node == -1) // don't know which segment to drag
423 return;
425 // We round off the extra precision in the motion coordinates provided
426 // by some input devices (like tablets). As we'll store the coordinates
427 // as integers in curvepoint_event we need to do this rounding before
428 // comparing them with the last coordinates from curvepoint_event.
429 // See bug #1593499 for details.
431 gint x = (gint) Inkscape::round(eventx);
432 gint y = (gint) Inkscape::round(eventy);
435 // The coordinates hasn't changed since the last motion event, abort
436 if (this->curvepoint_event[NR::X] == x &&
437 this->curvepoint_event[NR::Y] == y)
438 return;
440 NR::Point const delta_w(eventx - this->curvepoint_event[NR::X],
441 eventy - this->curvepoint_event[NR::Y]);
442 NR::Point const delta_dt(this->desktop->w2d(delta_w));
444 sp_nodepath_curve_drag (this->nodepath, this->grab_node, this->grab_t, delta_dt);
445 this->curvepoint_event[NR::X] = x;
446 this->curvepoint_event[NR::Y] = y;
448 }
449 if (this->knotholder) {
450 // we do not drag curve in knotholder
451 }
453 }
455 void ShapeEditor::finish_drag() {
456 if (this->nodepath && this->hit) {
457 sp_nodepath_update_repr (this->nodepath, _("Drag curve"));
458 }
459 }
461 void ShapeEditor::select_rect(NR::Rect const &rect, bool add) {
462 if (this->nodepath) {
463 sp_nodepath_select_rect(this->nodepath, rect, add);
464 }
465 }
467 bool ShapeEditor::has_selection() {
468 if (this->nodepath)
469 return this->nodepath->selected;
470 return false; // so far, knotholder cannot have selection
471 }
473 void ShapeEditor::deselect() {
474 if (this->nodepath)
475 sp_nodepath_deselect(this->nodepath);
476 }
478 void ShapeEditor::add_node () {
479 sp_node_selected_add_node(this->nodepath);
480 }
482 void ShapeEditor::delete_nodes () {
483 sp_node_selected_delete(this->nodepath);
484 }
486 void ShapeEditor::delete_nodes_preserving_shape () {
487 if (this->nodepath && this->nodepath->selected) {
488 sp_node_delete_preserve(g_list_copy(this->nodepath->selected));
489 }
490 }
492 void ShapeEditor::delete_segment () {
493 sp_node_selected_delete_segment(this->nodepath);
494 }
496 void ShapeEditor::set_node_type(int type) {
497 sp_node_selected_set_type(this->nodepath, (Inkscape::NodePath::NodeType) type);
498 }
500 void ShapeEditor::break_at_nodes() {
501 sp_node_selected_break(this->nodepath);
502 }
504 void ShapeEditor::join_nodes() {
505 sp_node_selected_join(this->nodepath);
506 }
508 void ShapeEditor::join_segments() {
509 sp_node_selected_join_segment(this->nodepath);
510 }
512 void ShapeEditor::duplicate_nodes() {
513 sp_node_selected_duplicate(this->nodepath);
514 }
516 void ShapeEditor::set_type_of_segments(NRPathcode code) {
517 sp_node_selected_set_line_type(this->nodepath, code);
518 }
520 void ShapeEditor::move_nodes_screen(SPDesktop *desktop, gdouble dx, gdouble dy) {
521 sp_node_selected_move_screen(desktop, this->nodepath, dx, dy);
522 }
523 void ShapeEditor::move_nodes(gdouble dx, gdouble dy) {
524 sp_node_selected_move(this->nodepath, dx, dy);
525 }
527 void ShapeEditor::rotate_nodes(gdouble angle, int which, bool screen) {
528 if (this->nodepath)
529 sp_nodepath_selected_nodes_rotate (this->nodepath, angle, which, screen);
530 }
532 void ShapeEditor::scale_nodes(gdouble const grow, int const which) {
533 sp_nodepath_selected_nodes_scale(this->nodepath, grow, which);
534 }
535 void ShapeEditor::scale_nodes_screen(gdouble const grow, int const which) {
536 sp_nodepath_selected_nodes_scale_screen(this->nodepath, grow, which);
537 }
539 void ShapeEditor::select_all (bool invert) {
540 if (this->nodepath)
541 sp_nodepath_select_all (this->nodepath, invert);
542 }
543 void ShapeEditor::select_all_from_subpath (bool invert) {
544 if (this->nodepath)
545 sp_nodepath_select_all_from_subpath (this->nodepath, invert);
546 }
547 void ShapeEditor::select_next () {
548 if (this->nodepath) {
549 sp_nodepath_select_next (this->nodepath);
550 if (this->nodepath->numSelected() >= 1) {
551 this->desktop->scroll_to_point(&(this->nodepath->singleSelectedCoords()), 1.0);
552 }
553 }
554 }
555 void ShapeEditor::select_prev () {
556 if (this->nodepath) {
557 sp_nodepath_select_prev (this->nodepath);
558 if (this->nodepath->numSelected() >= 1) {
559 this->desktop->scroll_to_point(&(this->nodepath->singleSelectedCoords()), 1.0);
560 }
561 }
562 }
564 void ShapeEditor::show_handles (bool show) {
565 if (this->nodepath && !this->nodepath->straight_path)
566 sp_nodepath_show_handles (this->nodepath, show);
567 }
569 void ShapeEditor::show_helperpath (bool show) {
570 if (this->nodepath)
571 sp_nodepath_show_helperpath (this->nodepath, show);
572 }
574 void ShapeEditor::flip (NR::Dim2 axis, boost::optional<NR::Point> center) {
575 if (this->nodepath)
576 sp_nodepath_flip (this->nodepath, axis, center);
577 }
579 void ShapeEditor::distribute (NR::Dim2 axis) {
580 if (this->nodepath)
581 sp_nodepath_selected_distribute (this->nodepath, axis);
582 }
583 void ShapeEditor::align (NR::Dim2 axis) {
584 if (this->nodepath)
585 sp_nodepath_selected_align (this->nodepath, axis);
586 }
589 /*
590 Local Variables:
591 mode:c++
592 c-file-style:"stroustrup"
593 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
594 indent-tabs-mode:nil
595 fill-column:99
596 End:
597 */
598 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :