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 delete 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 this->nodepath =
201 sp_nodepath_new(desktop, item, (prefs_get_int_attribute("tools.nodes", "show_handles", 1) != 0));
202 }
203 }
205 if (this->nodepath) {
206 this->nodepath->shape_editor = this;
207 }
208 this->knotholder = sp_item_knot_holder(item, desktop);
210 if (this->nodepath || this->knotholder) {
211 // setting new listener
212 Inkscape::XML::Node *repr;
213 if (this->knotholder)
214 repr = this->knotholder->repr;
215 else
216 repr = SP_OBJECT_REPR(item);
217 if (repr) {
218 Inkscape::GC::anchor(repr);
219 sp_repr_add_listener(repr, &shapeeditor_repr_events, this);
220 }
221 }
222 }
223 }
225 /** Please note that this function only works for path parameters.
226 * All other parameters probably will crash Inkscape!
227 */
228 void ShapeEditor::set_item_lpe_path_parameter(SPItem *item, SPObject *lpeobject, const char * key)
229 {
230 unset_item();
232 this->grab_node = -1;
234 if (lpeobject) {
235 this->nodepath = sp_nodepath_new( desktop, lpeobject,
236 (prefs_get_int_attribute("tools.nodes", "show_handles", 1) != 0),
237 key, item);
238 if (this->nodepath) {
239 this->nodepath->shape_editor = this;
241 // setting new listener
242 Inkscape::XML::Node *repr = SP_OBJECT_REPR(lpeobject);
243 if (repr) {
244 Inkscape::GC::anchor(repr);
245 sp_repr_add_listener(repr, &shapeeditor_repr_events, this);
246 }
247 }
248 }
249 }
251 /**
252 * pass a new knotholder to ShapeEditor to manage (and delete)
253 */
254 void
255 ShapeEditor::set_knotholder(KnotHolder * knot_holder)
256 {
257 unset_item();
259 this->grab_node = -1;
261 if (knot_holder) {
262 this->knotholder = knot_holder;
263 }
264 }
267 /** FIXME: think about this. Is this thing only called when the item needs to be updated?
268 Why not make a reload function in NodePath and in KnotHolder? */
269 void ShapeEditor::reset_item ()
270 {
271 if ( (this->nodepath) && (IS_LIVEPATHEFFECT(this->nodepath->object)) ) {
272 SPItem * item = this->nodepath->item;
273 SPObject *obj = this->nodepath->object;
274 char * key = g_strdup(this->nodepath->repr_key);
275 set_item_lpe_path_parameter(item, obj, key); // the above checks for nodepath, so it is indeed a path that we are editing
276 g_free(key);
277 } else {
278 SPItem * item = get_item();
279 set_item(item);
280 }
281 }
283 void ShapeEditor::nodepath_destroyed () {
284 this->nodepath = NULL;
285 }
287 void ShapeEditor::update_statusbar () {
288 if (this->nodepath)
289 sp_nodepath_update_statusbar(this->nodepath);
290 }
292 bool ShapeEditor::is_over_stroke (NR::Point event_p, bool remember) {
294 if (!this->nodepath)
295 return false; // no stroke in knotholder
297 SPItem *item = get_item();
299 //Translate click point into proper coord system
300 this->curvepoint_doc = desktop->w2d(event_p);
301 this->curvepoint_doc *= sp_item_dt2i_affine(item);
303 sp_nodepath_ensure_livarot_path(this->nodepath);
305 NR::Maybe<Path::cut_position> position = get_nearest_position_on_Path(this->nodepath->livarot_path, this->curvepoint_doc);
306 if (!position) {
307 return false;
308 }
310 NR::Point nearest = get_point_on_Path(this->nodepath->livarot_path, position->piece, position->t);
311 NR::Point delta = nearest - this->curvepoint_doc;
313 delta = desktop->d2w(delta);
315 double stroke_tolerance =
316 (( !SP_OBJECT_STYLE(item)->stroke.isNone() ?
317 desktop->current_zoom() *
318 SP_OBJECT_STYLE (item)->stroke_width.computed * 0.5 *
319 NR::expansion(sp_item_i2d_affine(item))
320 : 0.0)
321 + prefs_get_int_attribute_limited("options.dragtolerance", "value", 0, 0, 100)) /NR::expansion(sp_item_i2d_affine(item));
322 bool close = (NR::L2 (delta) < stroke_tolerance);
324 if (remember && close) {
325 this->curvepoint_event[NR::X] = (gint) event_p [NR::X];
326 this->curvepoint_event[NR::Y] = (gint) event_p [NR::Y];
327 this->hit = true;
328 this->grab_t = position->t;
329 this->grab_node = position->piece;
330 }
332 return close;
333 }
335 void ShapeEditor::add_node_near_point() {
336 if (this->nodepath) {
337 sp_nodepath_add_node_near_point(this->nodepath, this->curvepoint_doc);
338 } else if (this->knotholder) {
339 // we do not add nodes in knotholder... yet
340 }
341 }
343 void ShapeEditor::select_segment_near_point(bool toggle) {
344 if (this->nodepath) {
345 sp_nodepath_select_segment_near_point(this->nodepath, this->curvepoint_doc, toggle);
346 } else if (this->knotholder) {
347 // we do not select segments in knotholder... yet?
348 }
349 }
351 void ShapeEditor::cancel_hit() {
352 this->hit = false;
353 }
355 bool ShapeEditor::hits_curve() {
356 return (this->hit);
357 }
360 void ShapeEditor::curve_drag(gdouble eventx, gdouble eventy) {
361 if (this->nodepath && !this->nodepath->straight_path) {
363 if (this->grab_node == -1) // don't know which segment to drag
364 return;
366 // We round off the extra precision in the motion coordinates provided
367 // by some input devices (like tablets). As we'll store the coordinates
368 // as integers in curvepoint_event we need to do this rounding before
369 // comparing them with the last coordinates from curvepoint_event.
370 // See bug #1593499 for details.
372 gint x = (gint) Inkscape::round(eventx);
373 gint y = (gint) Inkscape::round(eventy);
376 // The coordinates hasn't changed since the last motion event, abort
377 if (this->curvepoint_event[NR::X] == x &&
378 this->curvepoint_event[NR::Y] == y)
379 return;
381 NR::Point const delta_w(eventx - this->curvepoint_event[NR::X],
382 eventy - this->curvepoint_event[NR::Y]);
383 NR::Point const delta_dt(this->desktop->w2d(delta_w));
385 sp_nodepath_curve_drag (this->grab_node, this->grab_t, delta_dt); //!!! FIXME: which nodepath?!!! also uses current!!!
386 this->curvepoint_event[NR::X] = x;
387 this->curvepoint_event[NR::Y] = y;
389 } else if (this->knotholder) {
390 // we do not drag curve in knotholder
391 }
393 }
395 void ShapeEditor::finish_drag() {
396 if (this->nodepath && this->hit) {
397 sp_nodepath_update_repr (this->nodepath, _("Drag curve"));
398 }
399 }
401 void ShapeEditor::select_rect(NR::Rect const &rect, bool add) {
402 if (this->nodepath) {
403 sp_nodepath_select_rect(this->nodepath, rect, add);
404 }
405 }
407 bool ShapeEditor::has_selection() {
408 if (this->nodepath)
409 return this->nodepath->selected;
410 return false; // so far, knotholder cannot have selection
411 }
413 void ShapeEditor::deselect() {
414 if (this->nodepath)
415 sp_nodepath_deselect(this->nodepath);
416 }
418 void ShapeEditor::add_node () {
419 sp_node_selected_add_node(this->nodepath);
420 }
422 void ShapeEditor::delete_nodes () {
423 sp_node_selected_delete(this->nodepath);
424 }
426 void ShapeEditor::delete_nodes_preserving_shape () {
427 if (this->nodepath && this->nodepath->selected) {
428 sp_node_delete_preserve(g_list_copy(this->nodepath->selected));
429 }
430 }
432 void ShapeEditor::delete_segment () {
433 sp_node_selected_delete_segment(this->nodepath);
434 }
436 void ShapeEditor::set_node_type(int type) {
437 sp_node_selected_set_type(this->nodepath, (Inkscape::NodePath::NodeType) type);
438 }
440 void ShapeEditor::break_at_nodes() {
441 sp_node_selected_break(this->nodepath);
442 }
444 void ShapeEditor::join_nodes() {
445 sp_node_selected_join(this->nodepath);
446 }
448 void ShapeEditor::join_segments() {
449 sp_node_selected_join_segment(this->nodepath);
450 }
452 void ShapeEditor::duplicate_nodes() {
453 sp_node_selected_duplicate(this->nodepath);
454 }
456 void ShapeEditor::set_type_of_segments(NRPathcode code) {
457 sp_node_selected_set_line_type(this->nodepath, code);
458 }
460 void ShapeEditor::move_nodes_screen(gdouble dx, gdouble dy) {
461 sp_node_selected_move_screen(this->nodepath, dx, dy);
462 }
463 void ShapeEditor::move_nodes(gdouble dx, gdouble dy) {
464 sp_node_selected_move(this->nodepath, dx, dy);
465 }
467 void ShapeEditor::rotate_nodes(gdouble angle, int which, bool screen) {
468 if (this->nodepath)
469 sp_nodepath_selected_nodes_rotate (this->nodepath, angle, which, screen);
470 }
472 void ShapeEditor::scale_nodes(gdouble const grow, int const which) {
473 sp_nodepath_selected_nodes_scale(this->nodepath, grow, which);
474 }
475 void ShapeEditor::scale_nodes_screen(gdouble const grow, int const which) {
476 sp_nodepath_selected_nodes_scale_screen(this->nodepath, grow, which);
477 }
479 void ShapeEditor::select_all (bool invert) {
480 if (this->nodepath)
481 sp_nodepath_select_all (this->nodepath, invert);
482 }
483 void ShapeEditor::select_all_from_subpath (bool invert) {
484 if (this->nodepath)
485 sp_nodepath_select_all_from_subpath (this->nodepath, invert);
486 }
487 void ShapeEditor::select_next () {
488 if (this->nodepath) {
489 sp_nodepath_select_next (this->nodepath);
490 if (this->nodepath->numSelected() >= 1) {
491 this->desktop->scroll_to_point(&(this->nodepath->singleSelectedCoords()), 1.0);
492 }
493 }
494 }
495 void ShapeEditor::select_prev () {
496 if (this->nodepath) {
497 sp_nodepath_select_prev (this->nodepath);
498 if (this->nodepath->numSelected() >= 1) {
499 this->desktop->scroll_to_point(&(this->nodepath->singleSelectedCoords()), 1.0);
500 }
501 }
502 }
504 void ShapeEditor::show_handles (bool show) {
505 if (this->nodepath && !this->nodepath->straight_path)
506 sp_nodepath_show_handles (this->nodepath, show);
507 }
509 void ShapeEditor::show_helperpath (bool show) {
510 if (this->nodepath)
511 sp_nodepath_show_helperpath (this->nodepath, show);
512 }
514 void ShapeEditor::flip (NR::Dim2 axis, NR::Maybe<NR::Point> center) {
515 if (this->nodepath)
516 sp_nodepath_flip (this->nodepath, axis, center);
517 }
519 void ShapeEditor::distribute (NR::Dim2 axis) {
520 if (this->nodepath)
521 sp_nodepath_selected_distribute (this->nodepath, axis);
522 }
523 void ShapeEditor::align (NR::Dim2 axis) {
524 if (this->nodepath)
525 sp_nodepath_selected_align (this->nodepath, axis);
526 }
529 /*
530 Local Variables:
531 mode:c++
532 c-file-style:"stroustrup"
533 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
534 indent-tabs-mode:nil
535 fill-column:99
536 End:
537 */
538 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :