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/point.h"
26 #include "nodepath.h"
27 #include "xml/node-event-vector.h"
28 #include "preferences.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 delete 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 }
178 void ShapeEditor::shapeeditor_event_attr_changed(gchar const *name)
179 {
180 gboolean changed_np = FALSE;
181 gboolean changed_kh = FALSE;
183 if (has_nodepath() && nodepath_edits_repr_key(name))
184 {
185 changed_np = !has_local_change(SH_NODEPATH);
186 decrement_local_change(SH_NODEPATH);
187 }
189 if (changed_np) {
190 GList *saved = NULL;
191 if (has_nodepath()) {
192 saved = save_nodepath_selection();
193 }
195 reset_item(SH_NODEPATH);
197 if (has_nodepath() && saved) {
198 restore_nodepath_selection(saved);
199 g_list_free (saved);
200 }
201 }
203 if (has_knotholder())
204 {
205 changed_kh = !has_local_change(SH_KNOTHOLDER);
206 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 reset_item(SH_KNOTHOLDER, !strcmp(name, "d"));
211 }
212 }
214 update_statusbar(); //TODO: get_container()->update_statusbar();
215 }
218 static void shapeeditor_event_attr_changed(Inkscape::XML::Node */*repr*/, gchar const *name,
219 gchar const */*old_value*/, gchar const */*new_value*/,
220 bool /*is_interactive*/, gpointer data)
221 {
222 g_assert(data);
223 ShapeEditor *sh = ((ShapeEditor *) data);
225 sh->shapeeditor_event_attr_changed(name);
226 }
228 static Inkscape::XML::NodeEventVector shapeeditor_repr_events = {
229 NULL, /* child_added */
230 NULL, /* child_removed */
231 shapeeditor_event_attr_changed,
232 NULL, /* content_changed */
233 NULL /* order_changed */
234 };
237 void ShapeEditor::set_item(SPItem *item, SubType type, bool keep_knotholder) {
238 // this happens (and should only happen) when for an LPEItem having both knotholder and
239 // nodepath the knotholder is adapted; in this case we don't want to delete the knotholder
240 // since this freezes the handles
241 unset_item(type, keep_knotholder);
243 this->grab_node = -1;
245 if (item) {
246 Inkscape::XML::Node *repr;
247 switch(type) {
248 case SH_NODEPATH:
249 if (SP_IS_LPE_ITEM(item)) {
250 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
251 this->nodepath = sp_nodepath_new(desktop, item, (prefs->getBool("/tools/nodes/show_handles", true)));
252 }
253 if (this->nodepath) {
254 this->nodepath->shape_editor = this;
256 // setting new listener
257 repr = SP_OBJECT_REPR(item);
258 if (repr != listener_attached_for) {
259 Inkscape::GC::anchor(repr);
260 sp_repr_add_listener(repr, &shapeeditor_repr_events, this);
261 listener_attached_for = repr;
262 }
263 }
264 break;
266 case SH_KNOTHOLDER:
267 if (!this->knotholder) {
268 // only recreate knotholder if none is present
269 this->knotholder = sp_item_knot_holder(item, desktop);
270 }
271 if (this->knotholder) {
272 this->knotholder->update_knots();
273 // setting new listener
274 repr = this->knotholder->repr;
275 if (repr != listener_attached_for) {
276 Inkscape::GC::anchor(repr);
277 sp_repr_add_listener(repr, &shapeeditor_repr_events, this);
278 listener_attached_for = repr;
279 }
280 }
281 break;
282 }
283 }
284 }
286 /** Please note that this function only works for path parameters.
287 * All other parameters probably will crash Inkscape!
288 */
289 void ShapeEditor::set_item_lpe_path_parameter(SPItem *item, SPObject *lpeobject, const char * key)
290 {
291 unset_item(SH_NODEPATH);
293 this->grab_node = -1;
295 if (lpeobject) {
296 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
297 this->nodepath = sp_nodepath_new( desktop, lpeobject,
298 (prefs->getInt("/tools/nodes/show_handles", true)),
299 key, item);
300 if (this->nodepath) {
301 this->nodepath->shape_editor = this;
303 // setting new listener
304 Inkscape::XML::Node *repr = SP_OBJECT_REPR(lpeobject);
305 if (repr && repr != listener_attached_for) {
306 Inkscape::GC::anchor(repr);
307 sp_repr_add_listener(repr, &shapeeditor_repr_events, this);
308 listener_attached_for = repr;
309 }
310 }
311 }
312 }
315 /** FIXME: This thing is only called when the item needs to be updated in response to repr change.
316 Why not make a reload function in NodePath and in KnotHolder? */
317 void ShapeEditor::reset_item (SubType type, bool keep_knotholder)
318 {
319 switch (type) {
320 case SH_NODEPATH:
321 if ( (this->nodepath) && (IS_LIVEPATHEFFECT(this->nodepath->object)) ) {
322 SPItem * item = this->nodepath->item;
323 SPObject *obj = this->nodepath->object;
324 char * key = g_strdup(this->nodepath->repr_key);
325 set_item_lpe_path_parameter(item, obj, key); // the above checks for nodepath, so it is indeed a path that we are editing
326 g_free(key);
327 } else {
328 SPItem * item = (SPItem *) get_item(SH_NODEPATH);
329 set_item(item, SH_NODEPATH);
330 }
331 break;
332 case SH_KNOTHOLDER:
333 if (this->knotholder) {
334 SPItem * item = (SPItem *) get_item(SH_KNOTHOLDER);
335 set_item(item, SH_KNOTHOLDER, keep_knotholder);
336 }
337 break;
338 }
339 }
341 void ShapeEditor::nodepath_destroyed () {
342 this->nodepath = NULL;
343 }
345 void ShapeEditor::update_statusbar () {
346 if (this->nodepath)
347 sp_nodepath_update_statusbar(this->nodepath);
348 }
350 bool ShapeEditor::is_over_stroke (Geom::Point event_p, bool remember) {
351 if (!this->nodepath)
352 return false; // no stroke in knotholder
354 const SPItem *item = get_item(SH_NODEPATH);
356 //Translate click point into proper coord system
357 this->curvepoint_doc = desktop->w2d(event_p);
358 this->curvepoint_doc *= sp_item_dt2i_affine(item);
360 SPCurve *curve = this->nodepath->curve; // not sure if np->curve is always up to date...
361 Geom::PathVector const &pathv = curve->get_pathvector();
362 boost::optional<Geom::PathVectorPosition> pvpos = Geom::nearestPoint(pathv, this->curvepoint_doc);
363 if (!pvpos) {
364 g_print("Warning! Possible error?\n");
365 return false;
366 }
368 Geom::Point nearest = pathv[pvpos->path_nr].pointAt(pvpos->t);
369 Geom::Point delta = nearest - this->curvepoint_doc;
371 delta = desktop->d2w(delta);
373 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
374 double stroke_tolerance =
375 (( !SP_OBJECT_STYLE(item)->stroke.isNone() ?
376 desktop->current_zoom() *
377 SP_OBJECT_STYLE (item)->stroke_width.computed * 0.5 *
378 to_2geom(sp_item_i2d_affine(item)).descrim()
379 : 0.0)
380 + prefs->getIntLimited("/options/dragtolerance/value", 0, 0, 100)) / to_2geom(sp_item_i2d_affine(item)).descrim();
381 bool close = (Geom::L2 (delta) < stroke_tolerance);
383 if (remember && close) {
384 // calculate index for nodepath's representation.
385 double int_part;
386 double t = std::modf(pvpos->t, &int_part);
387 unsigned int segment_index = (unsigned int)int_part + 1;
388 for (unsigned int i = 0; i < pvpos->path_nr; ++i) {
389 segment_index += pathv[i].size() + 1;
390 if (pathv[i].closed())
391 segment_index += 1;
392 }
394 this->curvepoint_event[Geom::X] = (gint) event_p [Geom::X];
395 this->curvepoint_event[Geom::Y] = (gint) event_p [Geom::Y];
396 this->hit = true;
397 this->grab_t = t;
398 this->grab_node = segment_index;
399 }
401 return close;
402 }
404 void ShapeEditor::add_node_near_point() {
405 if (this->nodepath) {
406 sp_nodepath_add_node_near_point(this->nodepath, this->curvepoint_doc);
407 } else if (this->knotholder) {
408 // we do not add nodes in knotholder... yet
409 }
410 }
412 void ShapeEditor::select_segment_near_point(bool toggle) {
413 if (this->nodepath) {
414 sp_nodepath_select_segment_near_point(this->nodepath, this->curvepoint_doc, toggle);
415 }
416 if (this->knotholder) {
417 // we do not select segments in knotholder... yet?
418 }
419 }
421 void ShapeEditor::cancel_hit() {
422 this->hit = false;
423 }
425 bool ShapeEditor::hits_curve() {
426 return (this->hit);
427 }
430 void ShapeEditor::curve_drag(gdouble eventx, gdouble eventy) {
431 if (this->nodepath && !this->nodepath->straight_path) {
433 if (this->grab_node == -1) // don't know which segment to drag
434 return;
436 // We round off the extra precision in the motion coordinates provided
437 // by some input devices (like tablets). As we'll store the coordinates
438 // as integers in curvepoint_event we need to do this rounding before
439 // comparing them with the last coordinates from curvepoint_event.
440 // See bug #1593499 for details.
442 gint x = (gint) Inkscape::round(eventx);
443 gint y = (gint) Inkscape::round(eventy);
446 // The coordinates hasn't changed since the last motion event, abort
447 if (this->curvepoint_event[Geom::X] == x &&
448 this->curvepoint_event[Geom::Y] == y)
449 return;
451 Geom::Point const delta_w(eventx - this->curvepoint_event[Geom::X],
452 eventy - this->curvepoint_event[Geom::Y]);
453 Geom::Point const delta_dt(this->desktop->w2d(delta_w));
455 sp_nodepath_curve_drag (this->nodepath, this->grab_node, this->grab_t, delta_dt);
456 this->curvepoint_event[Geom::X] = x;
457 this->curvepoint_event[Geom::Y] = y;
459 }
460 if (this->knotholder) {
461 // we do not drag curve in knotholder
462 }
464 }
466 void ShapeEditor::finish_drag() {
467 if (this->nodepath && this->hit) {
468 sp_nodepath_update_repr (this->nodepath, _("Drag curve"));
469 }
470 }
472 void ShapeEditor::select_rect(Geom::Rect const &rect, bool add) {
473 if (this->nodepath) {
474 sp_nodepath_select_rect(this->nodepath, rect, add);
475 }
476 }
478 bool ShapeEditor::has_selection() {
479 if (this->nodepath)
480 return this->nodepath->selected;
481 return false; // so far, knotholder cannot have selection
482 }
484 void ShapeEditor::deselect() {
485 if (this->nodepath)
486 sp_nodepath_deselect(this->nodepath);
487 }
489 void ShapeEditor::add_node () {
490 sp_node_selected_add_node(this->nodepath);
491 }
493 void ShapeEditor::delete_nodes () {
494 sp_node_selected_delete(this->nodepath);
495 }
497 void ShapeEditor::delete_nodes_preserving_shape () {
498 if (this->nodepath && this->nodepath->selected) {
499 sp_node_delete_preserve(g_list_copy(this->nodepath->selected));
500 }
501 }
503 void ShapeEditor::delete_segment () {
504 sp_node_selected_delete_segment(this->nodepath);
505 }
507 void ShapeEditor::set_node_type(int type) {
508 sp_node_selected_set_type(this->nodepath, (Inkscape::NodePath::NodeType) type);
509 }
511 void ShapeEditor::break_at_nodes() {
512 sp_node_selected_break(this->nodepath);
513 }
515 void ShapeEditor::join_nodes() {
516 sp_node_selected_join(this->nodepath);
517 }
519 void ShapeEditor::join_segments() {
520 sp_node_selected_join_segment(this->nodepath);
521 }
523 void ShapeEditor::duplicate_nodes() {
524 sp_node_selected_duplicate(this->nodepath);
525 }
527 void ShapeEditor::set_type_of_segments(NRPathcode code) {
528 sp_node_selected_set_line_type(this->nodepath, code);
529 }
531 void ShapeEditor::move_nodes_screen(SPDesktop *desktop, gdouble dx, gdouble dy) {
532 sp_node_selected_move_screen(desktop, this->nodepath, dx, dy);
533 }
534 void ShapeEditor::move_nodes(gdouble dx, gdouble dy) {
535 sp_node_selected_move(this->nodepath, dx, dy);
536 }
538 void ShapeEditor::rotate_nodes(gdouble angle, int which, bool screen) {
539 if (this->nodepath)
540 sp_nodepath_selected_nodes_rotate (this->nodepath, angle, which, screen);
541 }
543 void ShapeEditor::scale_nodes(gdouble const grow, int const which) {
544 sp_nodepath_selected_nodes_scale(this->nodepath, grow, which);
545 }
546 void ShapeEditor::scale_nodes_screen(gdouble const grow, int const which) {
547 sp_nodepath_selected_nodes_scale_screen(this->nodepath, grow, which);
548 }
550 void ShapeEditor::select_all (bool invert) {
551 if (this->nodepath)
552 sp_nodepath_select_all (this->nodepath, invert);
553 }
554 void ShapeEditor::select_all_from_subpath (bool invert) {
555 if (this->nodepath)
556 sp_nodepath_select_all_from_subpath (this->nodepath, invert);
557 }
558 void ShapeEditor::select_next () {
559 if (this->nodepath) {
560 sp_nodepath_select_next (this->nodepath);
561 if (this->nodepath->numSelected() >= 1) {
562 this->desktop->scroll_to_point(this->nodepath->singleSelectedCoords(), 1.0);
563 }
564 }
565 }
566 void ShapeEditor::select_prev () {
567 if (this->nodepath) {
568 sp_nodepath_select_prev (this->nodepath);
569 if (this->nodepath->numSelected() >= 1) {
570 this->desktop->scroll_to_point(this->nodepath->singleSelectedCoords(), 1.0);
571 }
572 }
573 }
575 void ShapeEditor::show_handles (bool show) {
576 if (this->nodepath && !this->nodepath->straight_path)
577 sp_nodepath_show_handles (this->nodepath, show);
578 }
580 void ShapeEditor::show_helperpath (bool show) {
581 if (this->nodepath)
582 sp_nodepath_show_helperpath (this->nodepath, show);
583 }
585 void ShapeEditor::flip (Geom::Dim2 axis, boost::optional<Geom::Point> center) {
586 if (this->nodepath)
587 sp_nodepath_flip (this->nodepath, axis, center);
588 }
590 void ShapeEditor::distribute (Geom::Dim2 axis) {
591 if (this->nodepath)
592 sp_nodepath_selected_distribute (this->nodepath, axis);
593 }
594 void ShapeEditor::align (Geom::Dim2 axis) {
595 if (this->nodepath)
596 sp_nodepath_selected_align (this->nodepath, axis);
597 }
600 /*
601 Local Variables:
602 mode:c++
603 c-file-style:"stroustrup"
604 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
605 indent-tabs-mode:nil
606 fill-column:99
607 End:
608 */
609 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :