49d8a2a1442b51297739151851908313f072f7ab
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 "node-context.h"
27 #include "xml/node-event-vector.h"
28 #include "prefs-utils.h"
29 #include "object-edit.h"
30 #include "splivarot.h"
31 #include "style.h"
33 #include "shape-editor.h"
36 ShapeEditorsCollective::ShapeEditorsCollective(SPDesktop */*dt*/) {
37 }
39 ShapeEditorsCollective::~ShapeEditorsCollective() {
40 }
43 void ShapeEditorsCollective::update_statusbar() {
45 //!!! move from nodepath: sp_nodepath_update_statusbar but summing for all nodepaths
47 }
49 ShapeEditor::ShapeEditor(SPDesktop *dt) {
50 this->desktop = dt;
51 this->grab_node = -1;
52 this->nodepath = NULL;
53 this->knotholder = NULL;
54 this->hit = false;
55 }
57 ShapeEditor::~ShapeEditor() {
58 unset_item();
59 }
61 void ShapeEditor::unset_item() {
63 Inkscape::XML::Node *old_repr = NULL;
65 if (this->nodepath) {
66 old_repr = this->nodepath->repr;
67 }
69 if (!old_repr && this->knotholder) {
70 old_repr = this->knotholder->repr;
71 }
73 if (old_repr) { // remove old listener
74 sp_repr_remove_listener_by_data(old_repr, this);
75 Inkscape::GC::release(old_repr);
76 }
78 if (this->nodepath) {
79 this->grab_node = -1;
80 sp_nodepath_destroy(this->nodepath);
81 this->nodepath = NULL;
82 }
84 if (this->knotholder) {
85 sp_knot_holder_destroy(this->knotholder);
86 this->knotholder = NULL;
87 }
88 }
90 bool ShapeEditor::has_nodepath () {
91 return (this->nodepath != NULL);
92 }
94 bool ShapeEditor::has_knotholder () {
95 return (this->knotholder != NULL);
96 }
98 bool ShapeEditor::has_local_change () {
99 if (this->nodepath)
100 return (this->nodepath->local_change != 0);
101 else if (this->knotholder)
102 return (this->knotholder->local_change != 0);
103 else
104 return false;
105 }
107 void ShapeEditor::decrement_local_change () {
108 if (this->nodepath) {
109 if (this->nodepath->local_change > 0)
110 this->nodepath->local_change--;
111 } else if (this->knotholder) {
112 this->knotholder->local_change = FALSE;
113 }
114 }
116 SPItem *ShapeEditor::get_item () {
117 SPItem *item = NULL;
118 if (this->has_nodepath()) {
119 item = this->nodepath->item;
120 } else if (this->has_knotholder()) {
121 item = SP_ITEM(this->knotholder->item);
122 }
123 return item;
124 }
126 GList *ShapeEditor::save_nodepath_selection () {
127 if (this->nodepath)
128 return ::save_nodepath_selection (this->nodepath);
129 return NULL;
130 }
132 void ShapeEditor::restore_nodepath_selection (GList *saved) {
133 if (this->nodepath && saved)
134 ::restore_nodepath_selection (this->nodepath, saved);
135 }
137 bool ShapeEditor::nodepath_edits_repr_key(gchar const *name) {
138 if (nodepath && name) {
139 return ( !strcmp(name, nodepath->repr_key) || !strcmp(name, nodepath->repr_nodetypes_key) );
140 }
142 return false;
143 }
145 static void shapeeditor_event_attr_changed(Inkscape::XML::Node */*repr*/, gchar const *name,
146 gchar const */*old_value*/, gchar const */*new_value*/,
147 bool /*is_interactive*/, gpointer data)
148 {
149 gboolean changed = FALSE;
151 g_assert(data);
152 ShapeEditor *sh = ((ShapeEditor *) data);
154 if ( sh->has_knotholder() || ( sh->has_nodepath() && sh->nodepath_edits_repr_key(name) ) )
155 {
156 changed = !sh->has_local_change();
157 sh->decrement_local_change();
158 }
160 if (changed) {
161 GList *saved = NULL;
162 if (sh->has_nodepath()) {
163 saved = sh->save_nodepath_selection();
164 }
166 sh->reset_item ();
168 if (sh->has_nodepath() && saved) {
169 sh->restore_nodepath_selection(saved);
170 g_list_free (saved);
171 }
172 }
174 sh->update_statusbar(); //TODO: sh->get_container()->update_statusbar();
175 }
177 static Inkscape::XML::NodeEventVector shapeeditor_repr_events = {
178 NULL, /* child_added */
179 NULL, /* child_removed */
180 shapeeditor_event_attr_changed,
181 NULL, /* content_changed */
182 NULL /* order_changed */
183 };
186 void ShapeEditor::set_item(SPItem *item) {
188 unset_item();
190 this->grab_node = -1;
192 if (item) {
193 if (SP_IS_LPE_ITEM(item)) {
194 SPLPEItem *lpeitem = SP_LPE_ITEM(item);
195 if ( !sp_lpe_item_get_current_lpe(lpeitem) || // if returns NULL, the whole expression evaluates to true and C++ will not call the otherwise crashing 2 functions below
196 !sp_lpe_item_get_current_lpe(lpeitem)->isVisible() ||
197 !sp_lpe_item_get_current_lpe(lpeitem)->providesKnotholder() )
198 // only create nodepath if the item either doesn't have an LPE
199 // or the LPE is invisible or it doesn't provide a knotholder itself
200 {
201 this->nodepath =
202 sp_nodepath_new(desktop, item, (prefs_get_int_attribute("tools.nodes", "show_handles", 1) != 0));
203 }
204 } else {
205 // only create nodepath if the item either doesn't have an LPE: no fear the item cannot have an LPE
206 this->nodepath =
207 sp_nodepath_new(desktop, item, (prefs_get_int_attribute("tools.nodes", "show_handles", 1) != 0));
208 }
210 if (this->nodepath) {
211 this->nodepath->shape_editor = this;
212 }
213 this->knotholder = sp_item_knot_holder(item, desktop);
215 if (this->nodepath || this->knotholder) {
216 // setting new listener
217 Inkscape::XML::Node *repr;
218 if (this->knotholder)
219 repr = this->knotholder->repr;
220 else
221 repr = SP_OBJECT_REPR(item);
222 if (repr) {
223 Inkscape::GC::anchor(repr);
224 sp_repr_add_listener(repr, &shapeeditor_repr_events, this);
225 }
226 }
227 }
228 }
230 /** Please note that this function only works for path parameters.
231 * All other parameters probably will crash Inkscape!
232 */
233 void ShapeEditor::set_item_lpe_path_parameter(SPItem *item, SPObject *lpeobject, const char * key)
234 {
235 unset_item();
237 this->grab_node = -1;
239 if (lpeobject) {
240 this->nodepath = sp_nodepath_new( desktop, lpeobject,
241 (prefs_get_int_attribute("tools.nodes", "show_handles", 1) != 0),
242 key, item);
243 if (this->nodepath) {
244 this->nodepath->shape_editor = this;
246 // setting new listener
247 Inkscape::XML::Node *repr = SP_OBJECT_REPR(lpeobject);
248 if (repr) {
249 Inkscape::GC::anchor(repr);
250 sp_repr_add_listener(repr, &shapeeditor_repr_events, this);
251 }
252 }
253 }
254 }
256 /**
257 * pass a new knotholder to ShapeEditor to manage (and delete)
258 */
259 void
260 ShapeEditor::set_knotholder(SPKnotHolder * knot_holder)
261 {
262 unset_item();
264 this->grab_node = -1;
266 if (knot_holder) {
267 this->knotholder = knot_holder;
268 }
269 }
272 /** FIXME: think about this. Is this thing only called when the item needs to be updated?
273 Why not make a reload function in NodePath and in KnotHolder? */
274 void ShapeEditor::reset_item ()
275 {
276 if ( (this->nodepath) && (IS_LIVEPATHEFFECT(this->nodepath->object)) ) {
277 SPItem * item = this->nodepath->item;
278 SPObject *obj = this->nodepath->object;
279 char * key = g_strdup(this->nodepath->repr_key);
280 set_item_lpe_path_parameter(item, obj, key); // the above checks for nodepath, so it is indeed a path that we are editing
281 g_free(key);
282 } else {
283 SPItem * item = get_item();
284 set_item(item);
285 }
286 }
288 void ShapeEditor::nodepath_destroyed () {
289 this->nodepath = NULL;
290 }
292 void ShapeEditor::update_statusbar () {
293 if (this->nodepath)
294 sp_nodepath_update_statusbar(this->nodepath);
295 }
297 bool ShapeEditor::is_over_stroke (NR::Point event_p, bool remember) {
299 if (!this->nodepath)
300 return false; // no stroke in knotholder
302 SPItem *item = get_item();
304 //Translate click point into proper coord system
305 this->curvepoint_doc = desktop->w2d(event_p);
306 this->curvepoint_doc *= sp_item_dt2i_affine(item);
308 sp_nodepath_ensure_livarot_path(this->nodepath);
310 NR::Maybe<Path::cut_position> position = get_nearest_position_on_Path(this->nodepath->livarot_path, this->curvepoint_doc);
311 if (!position) {
312 return false;
313 }
315 NR::Point nearest = get_point_on_Path(this->nodepath->livarot_path, position->piece, position->t);
316 NR::Point delta = nearest - this->curvepoint_doc;
318 delta = desktop->d2w(delta);
320 double stroke_tolerance =
321 (( !SP_OBJECT_STYLE(item)->stroke.isNone() ?
322 desktop->current_zoom() *
323 SP_OBJECT_STYLE (item)->stroke_width.computed * 0.5 *
324 NR::expansion(sp_item_i2d_affine(item))
325 : 0.0)
326 + prefs_get_int_attribute_limited("options.dragtolerance", "value", 0, 0, 100)) /NR::expansion(sp_item_i2d_affine(item));
327 bool close = (NR::L2 (delta) < stroke_tolerance);
329 if (remember && close) {
330 this->curvepoint_event[NR::X] = (gint) event_p [NR::X];
331 this->curvepoint_event[NR::Y] = (gint) event_p [NR::Y];
332 this->hit = true;
333 this->grab_t = position->t;
334 this->grab_node = position->piece;
335 }
337 return close;
338 }
340 void ShapeEditor::add_node_near_point() {
341 if (this->nodepath) {
342 sp_nodepath_add_node_near_point(this->nodepath, this->curvepoint_doc);
343 } else if (this->knotholder) {
344 // we do not add nodes in knotholder... yet
345 }
346 }
348 void ShapeEditor::select_segment_near_point(bool toggle) {
349 if (this->nodepath) {
350 sp_nodepath_select_segment_near_point(this->nodepath, this->curvepoint_doc, toggle);
351 } else if (this->knotholder) {
352 // we do not select segments in knotholder... yet?
353 }
354 }
356 void ShapeEditor::cancel_hit() {
357 this->hit = false;
358 }
360 bool ShapeEditor::hits_curve() {
361 return (this->hit);
362 }
365 void ShapeEditor::curve_drag(gdouble eventx, gdouble eventy) {
366 if (this->nodepath && !this->nodepath->straight_path) {
368 if (this->grab_node == -1) // don't know which segment to drag
369 return;
371 // We round off the extra precision in the motion coordinates provided
372 // by some input devices (like tablets). As we'll store the coordinates
373 // as integers in curvepoint_event we need to do this rounding before
374 // comparing them with the last coordinates from curvepoint_event.
375 // See bug #1593499 for details.
377 gint x = (gint) Inkscape::round(eventx);
378 gint y = (gint) Inkscape::round(eventy);
381 // The coordinates hasn't changed since the last motion event, abort
382 if (this->curvepoint_event[NR::X] == x &&
383 this->curvepoint_event[NR::Y] == y)
384 return;
386 NR::Point const delta_w(eventx - this->curvepoint_event[NR::X],
387 eventy - this->curvepoint_event[NR::Y]);
388 NR::Point const delta_dt(this->desktop->w2d(delta_w));
390 sp_nodepath_curve_drag (this->grab_node, this->grab_t, delta_dt); //!!! FIXME: which nodepath?!!! also uses current!!!
391 this->curvepoint_event[NR::X] = x;
392 this->curvepoint_event[NR::Y] = y;
394 } else if (this->knotholder) {
395 // we do not drag curve in knotholder
396 }
398 }
400 void ShapeEditor::finish_drag() {
401 if (this->nodepath && this->hit) {
402 sp_nodepath_update_repr (this->nodepath, _("Drag curve"));
403 }
404 }
406 void ShapeEditor::select_rect(NR::Rect const &rect, bool add) {
407 if (this->nodepath) {
408 sp_nodepath_select_rect(this->nodepath, rect, add);
409 }
410 }
412 bool ShapeEditor::has_selection() {
413 if (this->nodepath)
414 return this->nodepath->selected;
415 return false; // so far, knotholder cannot have selection
416 }
418 void ShapeEditor::deselect() {
419 if (this->nodepath)
420 sp_nodepath_deselect(this->nodepath);
421 }
423 void ShapeEditor::add_node () {
424 sp_node_selected_add_node(this->nodepath);
425 }
427 void ShapeEditor::delete_nodes () {
428 sp_node_selected_delete(this->nodepath);
429 }
431 void ShapeEditor::delete_nodes_preserving_shape () {
432 if (this->nodepath && this->nodepath->selected) {
433 sp_node_delete_preserve(g_list_copy(this->nodepath->selected));
434 }
435 }
437 void ShapeEditor::delete_segment () {
438 sp_node_selected_delete_segment(this->nodepath);
439 }
441 void ShapeEditor::set_node_type(int type) {
442 sp_node_selected_set_type(this->nodepath, (Inkscape::NodePath::NodeType) type);
443 }
445 void ShapeEditor::break_at_nodes() {
446 sp_node_selected_break(this->nodepath);
447 }
449 void ShapeEditor::join_nodes() {
450 sp_node_selected_join(this->nodepath);
451 }
453 void ShapeEditor::join_segments() {
454 sp_node_selected_join_segment(this->nodepath);
455 }
457 void ShapeEditor::duplicate_nodes() {
458 sp_node_selected_duplicate(this->nodepath);
459 }
461 void ShapeEditor::set_type_of_segments(NRPathcode code) {
462 sp_node_selected_set_line_type(this->nodepath, code);
463 }
465 void ShapeEditor::move_nodes_screen(gdouble dx, gdouble dy) {
466 sp_node_selected_move_screen(this->nodepath, dx, dy);
467 }
468 void ShapeEditor::move_nodes(gdouble dx, gdouble dy) {
469 sp_node_selected_move(this->nodepath, dx, dy);
470 }
472 void ShapeEditor::rotate_nodes(gdouble angle, int which, bool screen) {
473 if (this->nodepath)
474 sp_nodepath_selected_nodes_rotate (this->nodepath, angle, which, screen);
475 }
477 void ShapeEditor::scale_nodes(gdouble const grow, int const which) {
478 sp_nodepath_selected_nodes_scale(this->nodepath, grow, which);
479 }
480 void ShapeEditor::scale_nodes_screen(gdouble const grow, int const which) {
481 sp_nodepath_selected_nodes_scale_screen(this->nodepath, grow, which);
482 }
484 void ShapeEditor::select_all (bool invert) {
485 if (this->nodepath)
486 sp_nodepath_select_all (this->nodepath, invert);
487 }
488 void ShapeEditor::select_all_from_subpath (bool invert) {
489 if (this->nodepath)
490 sp_nodepath_select_all_from_subpath (this->nodepath, invert);
491 }
492 void ShapeEditor::select_next () {
493 if (this->nodepath) {
494 sp_nodepath_select_next (this->nodepath);
495 if (this->nodepath->numSelected() >= 1) {
496 this->desktop->scroll_to_point(&(this->nodepath->singleSelectedCoords()), 1.0);
497 }
498 }
499 }
500 void ShapeEditor::select_prev () {
501 if (this->nodepath) {
502 sp_nodepath_select_prev (this->nodepath);
503 if (this->nodepath->numSelected() >= 1) {
504 this->desktop->scroll_to_point(&(this->nodepath->singleSelectedCoords()), 1.0);
505 }
506 }
507 }
509 void ShapeEditor::show_handles (bool show) {
510 if (this->nodepath && !this->nodepath->straight_path)
511 sp_nodepath_show_handles (this->nodepath, show);
512 }
514 void ShapeEditor::show_helperpath (bool show) {
515 if (this->nodepath)
516 sp_nodepath_show_helperpath (this->nodepath, show);
517 }
519 void ShapeEditor::flip (NR::Dim2 axis, NR::Maybe<NR::Point> center) {
520 if (this->nodepath)
521 sp_nodepath_flip (this->nodepath, axis, center);
522 }
524 void ShapeEditor::distribute (NR::Dim2 axis) {
525 if (this->nodepath)
526 sp_nodepath_selected_distribute (this->nodepath, axis);
527 }
528 void ShapeEditor::align (NR::Dim2 axis) {
529 if (this->nodepath)
530 sp_nodepath_selected_align (this->nodepath, axis);
531 }
534 /*
535 Local Variables:
536 mode:c++
537 c-file-style:"stroustrup"
538 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
539 indent-tabs-mode:nil
540 fill-column:99
541 End:
542 */
543 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :