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>
33 #include "sp-shape.h"
35 #include "shape-editor.h"
38 ShapeEditorsCollective::ShapeEditorsCollective(SPDesktop */*dt*/) {
39 }
41 ShapeEditorsCollective::~ShapeEditorsCollective() {
42 }
45 void ShapeEditorsCollective::update_statusbar() {
47 //!!! move from nodepath: sp_nodepath_update_statusbar but summing for all nodepaths
49 }
51 ShapeEditor::ShapeEditor(SPDesktop *dt) {
52 this->desktop = dt;
53 this->grab_node = -1;
54 this->nodepath = NULL;
55 this->knotholder = NULL;
56 this->hit = false;
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 sp_repr_remove_listener_by_data(old_repr, this);
72 Inkscape::GC::release(old_repr);
74 this->grab_node = -1;
75 sp_nodepath_destroy(this->nodepath);
76 this->nodepath = NULL;
77 }
78 break;
79 case SH_KNOTHOLDER:
80 if (this->knotholder) {
81 old_repr = this->knotholder->repr;
82 sp_repr_remove_listener_by_data(old_repr, this);
83 Inkscape::GC::release(old_repr);
85 if (!keep_knotholder) {
86 delete this->knotholder;
87 this->knotholder = NULL;
88 }
89 }
90 break;
91 }
92 }
94 bool ShapeEditor::has_nodepath () {
95 return (this->nodepath != NULL);
96 }
98 bool ShapeEditor::has_knotholder () {
99 return (this->knotholder != NULL);
100 }
102 void ShapeEditor::update_knotholder () {
103 if (this->knotholder)
104 this->knotholder->update_knots();
105 }
107 bool ShapeEditor::has_local_change (SubType type) {
108 switch (type) {
109 case SH_NODEPATH:
110 return (this->nodepath && this->nodepath->local_change);
111 case SH_KNOTHOLDER:
112 return (this->knotholder && this->knotholder->local_change != 0);
113 default:
114 g_assert_not_reached();
115 }
116 }
118 void ShapeEditor::decrement_local_change (SubType type) {
119 switch (type) {
120 case SH_NODEPATH:
121 if (this->nodepath && this->nodepath->local_change > 0) {
122 this->nodepath->local_change--;
123 }
124 break;
125 case SH_KNOTHOLDER:
126 if (this->knotholder) {
127 this->knotholder->local_change = FALSE;
128 }
129 break;
130 default:
131 g_assert_not_reached();
132 }
133 }
135 const SPItem *ShapeEditor::get_item (SubType type) {
136 const SPItem *item = NULL;
137 switch (type) {
138 case SH_NODEPATH:
139 if (this->has_nodepath()) {
140 item = this->nodepath->item;
141 }
142 break;
143 case SH_KNOTHOLDER:
144 if (this->has_knotholder()) {
145 item = this->knotholder->getItem();
146 }
147 break;
148 }
149 return item;
150 }
152 GList *ShapeEditor::save_nodepath_selection () {
153 if (this->nodepath)
154 return ::save_nodepath_selection (this->nodepath);
155 return NULL;
156 }
158 void ShapeEditor::restore_nodepath_selection (GList *saved) {
159 if (this->nodepath && saved)
160 ::restore_nodepath_selection (this->nodepath, saved);
161 }
163 bool ShapeEditor::nodepath_edits_repr_key(gchar const *name) {
164 if (nodepath && name) {
165 return ( !strcmp(name, nodepath->repr_key) || !strcmp(name, nodepath->repr_nodetypes_key) );
166 }
168 return false;
169 }
171 static void shapeeditor_event_attr_changed(Inkscape::XML::Node */*repr*/, gchar const *name,
172 gchar const */*old_value*/, gchar const */*new_value*/,
173 bool /*is_interactive*/, gpointer data)
174 {
175 gboolean changed_np = FALSE;
176 gboolean changed_kh = FALSE;
178 g_assert(data);
179 ShapeEditor *sh = ((ShapeEditor *) data);
181 if (sh->has_nodepath() && sh->nodepath_edits_repr_key(name))
182 {
183 changed_np = !sh->has_local_change(SH_NODEPATH);
184 sh->decrement_local_change(SH_NODEPATH);
186 }
188 if (changed_np) {
189 GList *saved = NULL;
190 if (sh->has_nodepath()) {
191 saved = sh->save_nodepath_selection();
192 }
194 sh->reset_item(SH_NODEPATH);
196 if (sh->has_nodepath() && saved) {
197 sh->restore_nodepath_selection(saved);
198 g_list_free (saved);
199 }
200 }
203 if (sh->has_knotholder())
204 {
205 changed_kh = !sh->has_local_change(SH_KNOTHOLDER);
206 sh->decrement_local_change(SH_KNOTHOLDER);
207 if (changed_kh) {
208 // this can happen if an LPEItem's knotholder handle was dragged, in which case we want
209 // to keep the knotholder; in all other cases (e.g., if the LPE itself changes) we delete it
210 sh->reset_item(SH_KNOTHOLDER, !strcmp(name, "d"));
211 }
212 }
214 sh->update_statusbar(); //TODO: sh->get_container()->update_statusbar();
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 nodepath the knotholder
228 // is adapted; in this case we don't want to delete the knotholder since this freezes the handles
229 unset_item(type, keep_knotholder);
231 this->grab_node = -1;
233 if (item) {
234 Inkscape::XML::Node *repr;
235 switch(type) {
236 case SH_NODEPATH:
237 if (SP_IS_LPE_ITEM(item)) {
238 this->nodepath = sp_nodepath_new(desktop, item, (prefs_get_int_attribute("tools.nodes", "show_handles", 1) != 0));
239 }
240 if (this->nodepath) {
241 this->nodepath->shape_editor = this;
243 // setting new listener
244 repr = SP_OBJECT_REPR(item);
245 Inkscape::GC::anchor(repr);
246 sp_repr_add_listener(repr, &shapeeditor_repr_events, this);
247 }
248 break;
250 case SH_KNOTHOLDER:
251 if (!this->knotholder) {
252 // only recreate knotholder if none is present
253 this->knotholder = sp_item_knot_holder(item, desktop);
254 }
255 if (this->knotholder) {
256 this->knotholder->update_knots();
257 // setting new listener
258 repr = this->knotholder->repr;
259 Inkscape::GC::anchor(repr);
260 sp_repr_add_listener(repr, &shapeeditor_repr_events, this);
261 }
262 break;
263 }
264 }
265 }
267 /** Please note that this function only works for path parameters.
268 * All other parameters probably will crash Inkscape!
269 */
270 void ShapeEditor::set_item_lpe_path_parameter(SPItem *item, SPObject *lpeobject, const char * key)
271 {
272 unset_item(SH_NODEPATH);
274 this->grab_node = -1;
276 if (lpeobject) {
277 this->nodepath = sp_nodepath_new( desktop, lpeobject,
278 (prefs_get_int_attribute("tools.nodes", "show_handles", 1) != 0),
279 key, item);
280 if (this->nodepath) {
281 this->nodepath->shape_editor = this;
283 // setting new listener
284 Inkscape::XML::Node *repr = SP_OBJECT_REPR(lpeobject);
285 if (repr) {
286 Inkscape::GC::anchor(repr);
287 sp_repr_add_listener(repr, &shapeeditor_repr_events, this);
288 }
289 }
290 }
291 }
293 /**
294 * pass a new knotholder to ShapeEditor to manage (and delete)
295 */
296 void
297 ShapeEditor::set_knotholder(KnotHolder * knot_holder)
298 {
299 unset_item(SH_KNOTHOLDER);
301 this->grab_node = -1;
303 if (knot_holder) {
304 this->knotholder = knot_holder;
305 }
306 }
309 /** FIXME: think about this. Is this thing only called when the item needs to be updated?
310 Why not make a reload function in NodePath and in KnotHolder? */
311 void ShapeEditor::reset_item (SubType type, bool keep_knotholder)
312 {
313 switch (type) {
314 case SH_NODEPATH:
315 if ( (this->nodepath) && (IS_LIVEPATHEFFECT(this->nodepath->object)) ) {
316 SPItem * item = this->nodepath->item;
317 SPObject *obj = this->nodepath->object;
318 char * key = g_strdup(this->nodepath->repr_key);
319 set_item_lpe_path_parameter(item, obj, key); // the above checks for nodepath, so it is indeed a path that we are editing
320 g_free(key);
321 } else {
322 SPItem * item = (SPItem *) get_item(SH_NODEPATH);
323 set_item(item, SH_NODEPATH);
324 }
325 break;
326 case SH_KNOTHOLDER:
327 if (this->knotholder) {
328 SPItem * item = (SPItem *) get_item(SH_KNOTHOLDER);
329 set_item(item, SH_KNOTHOLDER, keep_knotholder);
330 }
331 break;
332 }
333 }
335 void ShapeEditor::nodepath_destroyed () {
336 this->nodepath = NULL;
337 }
339 void ShapeEditor::update_statusbar () {
340 if (this->nodepath)
341 sp_nodepath_update_statusbar(this->nodepath);
342 }
344 bool ShapeEditor::is_over_stroke (NR::Point event_p, bool remember) {
346 if (!this->nodepath)
347 return false; // no stroke in knotholder
349 const SPItem *item = get_item(SH_NODEPATH);
351 //Translate click point into proper coord system
352 this->curvepoint_doc = desktop->w2d(event_p);
353 this->curvepoint_doc *= sp_item_dt2i_affine(item);
355 SPCurve *curve;
356 if (SP_IS_SHAPE(item)) {
357 curve = sp_shape_get_curve(SP_SHAPE(item));
358 } else {
359 this->nodepath->curve; // not sure if np->curve is always up to date...
360 }
361 Geom::PathVector const &pathv = curve->get_pathvector();
362 Geom::PathVectorPosition pvpos = Geom::nearestPoint(pathv, this->curvepoint_doc);
364 NR::Point nearest = pathv[pvpos.path_nr].pointAt(pvpos.t);
365 NR::Point delta = nearest - this->curvepoint_doc;
367 delta = desktop->d2w(delta);
369 double stroke_tolerance =
370 (( !SP_OBJECT_STYLE(item)->stroke.isNone() ?
371 desktop->current_zoom() *
372 SP_OBJECT_STYLE (item)->stroke_width.computed * 0.5 *
373 NR::expansion(sp_item_i2d_affine(item))
374 : 0.0)
375 + prefs_get_int_attribute_limited("options.dragtolerance", "value", 0, 0, 100)) /NR::expansion(sp_item_i2d_affine(item));
376 bool close = (NR::L2 (delta) < stroke_tolerance);
378 if (remember && close) {
379 // calculate index for nodepath's representation.
380 double int_part;
381 double t = std::modf(pvpos.t, &int_part);
382 unsigned int segment_index = (unsigned int)int_part + 1;
383 for (unsigned int i = 0; i < pvpos.path_nr; ++i) {
384 segment_index += pathv[i].size() + 1;
385 if (pathv[i].closed())
386 segment_index += 1;
387 }
389 this->curvepoint_event[NR::X] = (gint) event_p [NR::X];
390 this->curvepoint_event[NR::Y] = (gint) event_p [NR::Y];
391 this->hit = true;
392 this->grab_t = t;
393 this->grab_node = segment_index;
394 }
396 return close;
397 }
399 void ShapeEditor::add_node_near_point() {
400 if (this->nodepath) {
401 sp_nodepath_add_node_near_point(this->nodepath, this->curvepoint_doc);
402 } else if (this->knotholder) {
403 // we do not add nodes in knotholder... yet
404 }
405 }
407 void ShapeEditor::select_segment_near_point(bool toggle) {
408 if (this->nodepath) {
409 sp_nodepath_select_segment_near_point(this->nodepath, this->curvepoint_doc, toggle);
410 }
411 if (this->knotholder) {
412 // we do not select segments in knotholder... yet?
413 }
414 }
416 void ShapeEditor::cancel_hit() {
417 this->hit = false;
418 }
420 bool ShapeEditor::hits_curve() {
421 return (this->hit);
422 }
425 void ShapeEditor::curve_drag(gdouble eventx, gdouble eventy) {
426 if (this->nodepath && !this->nodepath->straight_path) {
428 if (this->grab_node == -1) // don't know which segment to drag
429 return;
431 // We round off the extra precision in the motion coordinates provided
432 // by some input devices (like tablets). As we'll store the coordinates
433 // as integers in curvepoint_event we need to do this rounding before
434 // comparing them with the last coordinates from curvepoint_event.
435 // See bug #1593499 for details.
437 gint x = (gint) Inkscape::round(eventx);
438 gint y = (gint) Inkscape::round(eventy);
441 // The coordinates hasn't changed since the last motion event, abort
442 if (this->curvepoint_event[NR::X] == x &&
443 this->curvepoint_event[NR::Y] == y)
444 return;
446 NR::Point const delta_w(eventx - this->curvepoint_event[NR::X],
447 eventy - this->curvepoint_event[NR::Y]);
448 NR::Point const delta_dt(this->desktop->w2d(delta_w));
450 sp_nodepath_curve_drag (this->nodepath, this->grab_node, this->grab_t, delta_dt);
451 this->curvepoint_event[NR::X] = x;
452 this->curvepoint_event[NR::Y] = y;
454 }
455 if (this->knotholder) {
456 // we do not drag curve in knotholder
457 }
459 }
461 void ShapeEditor::finish_drag() {
462 if (this->nodepath && this->hit) {
463 sp_nodepath_update_repr (this->nodepath, _("Drag curve"));
464 }
465 }
467 void ShapeEditor::select_rect(NR::Rect const &rect, bool add) {
468 if (this->nodepath) {
469 sp_nodepath_select_rect(this->nodepath, rect, add);
470 }
471 }
473 bool ShapeEditor::has_selection() {
474 if (this->nodepath)
475 return this->nodepath->selected;
476 return false; // so far, knotholder cannot have selection
477 }
479 void ShapeEditor::deselect() {
480 if (this->nodepath)
481 sp_nodepath_deselect(this->nodepath);
482 }
484 void ShapeEditor::add_node () {
485 sp_node_selected_add_node(this->nodepath);
486 }
488 void ShapeEditor::delete_nodes () {
489 sp_node_selected_delete(this->nodepath);
490 }
492 void ShapeEditor::delete_nodes_preserving_shape () {
493 if (this->nodepath && this->nodepath->selected) {
494 sp_node_delete_preserve(g_list_copy(this->nodepath->selected));
495 }
496 }
498 void ShapeEditor::delete_segment () {
499 sp_node_selected_delete_segment(this->nodepath);
500 }
502 void ShapeEditor::set_node_type(int type) {
503 sp_node_selected_set_type(this->nodepath, (Inkscape::NodePath::NodeType) type);
504 }
506 void ShapeEditor::break_at_nodes() {
507 sp_node_selected_break(this->nodepath);
508 }
510 void ShapeEditor::join_nodes() {
511 sp_node_selected_join(this->nodepath);
512 }
514 void ShapeEditor::join_segments() {
515 sp_node_selected_join_segment(this->nodepath);
516 }
518 void ShapeEditor::duplicate_nodes() {
519 sp_node_selected_duplicate(this->nodepath);
520 }
522 void ShapeEditor::set_type_of_segments(NRPathcode code) {
523 sp_node_selected_set_line_type(this->nodepath, code);
524 }
526 void ShapeEditor::move_nodes_screen(SPDesktop *desktop, gdouble dx, gdouble dy) {
527 sp_node_selected_move_screen(desktop, this->nodepath, dx, dy);
528 }
529 void ShapeEditor::move_nodes(gdouble dx, gdouble dy) {
530 sp_node_selected_move(this->nodepath, dx, dy);
531 }
533 void ShapeEditor::rotate_nodes(gdouble angle, int which, bool screen) {
534 if (this->nodepath)
535 sp_nodepath_selected_nodes_rotate (this->nodepath, angle, which, screen);
536 }
538 void ShapeEditor::scale_nodes(gdouble const grow, int const which) {
539 sp_nodepath_selected_nodes_scale(this->nodepath, grow, which);
540 }
541 void ShapeEditor::scale_nodes_screen(gdouble const grow, int const which) {
542 sp_nodepath_selected_nodes_scale_screen(this->nodepath, grow, which);
543 }
545 void ShapeEditor::select_all (bool invert) {
546 if (this->nodepath)
547 sp_nodepath_select_all (this->nodepath, invert);
548 }
549 void ShapeEditor::select_all_from_subpath (bool invert) {
550 if (this->nodepath)
551 sp_nodepath_select_all_from_subpath (this->nodepath, invert);
552 }
553 void ShapeEditor::select_next () {
554 if (this->nodepath) {
555 sp_nodepath_select_next (this->nodepath);
556 if (this->nodepath->numSelected() >= 1) {
557 this->desktop->scroll_to_point(&(this->nodepath->singleSelectedCoords()), 1.0);
558 }
559 }
560 }
561 void ShapeEditor::select_prev () {
562 if (this->nodepath) {
563 sp_nodepath_select_prev (this->nodepath);
564 if (this->nodepath->numSelected() >= 1) {
565 this->desktop->scroll_to_point(&(this->nodepath->singleSelectedCoords()), 1.0);
566 }
567 }
568 }
570 void ShapeEditor::show_handles (bool show) {
571 if (this->nodepath && !this->nodepath->straight_path)
572 sp_nodepath_show_handles (this->nodepath, show);
573 }
575 void ShapeEditor::show_helperpath (bool show) {
576 if (this->nodepath)
577 sp_nodepath_show_helperpath (this->nodepath, show);
578 }
580 void ShapeEditor::flip (NR::Dim2 axis, boost::optional<NR::Point> center) {
581 if (this->nodepath)
582 sp_nodepath_flip (this->nodepath, axis, center);
583 }
585 void ShapeEditor::distribute (NR::Dim2 axis) {
586 if (this->nodepath)
587 sp_nodepath_selected_distribute (this->nodepath, axis);
588 }
589 void ShapeEditor::align (NR::Dim2 axis) {
590 if (this->nodepath)
591 sp_nodepath_selected_align (this->nodepath, axis);
592 }
595 /*
596 Local Variables:
597 mode:c++
598 c-file-style:"stroustrup"
599 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
600 indent-tabs-mode:nil
601 fill-column:99
602 End:
603 */
604 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :