a2a9a86ebb38952f289c40dea12371aace0d548f
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 <glibmm/i18n.h>
17 #include "sp-object.h"
18 #include "sp-item.h"
19 #include "live_effects/lpeobject.h"
20 #include "selection.h"
21 #include "desktop.h"
22 #include "desktop-handles.h"
23 #include "knotholder.h"
24 #include "node-context.h"
25 #include "xml/node-event-vector.h"
26 #include "prefs-utils.h"
27 #include "object-edit.h"
28 #include "splivarot.h"
29 #include "style.h"
31 #include "shape-editor.h"
34 ShapeEditorsCollective::ShapeEditorsCollective(SPDesktop */*dt*/) {
35 }
37 ShapeEditorsCollective::~ShapeEditorsCollective() {
38 }
41 void ShapeEditorsCollective::update_statusbar() {
43 //!!! move from nodepath: sp_nodepath_update_statusbar but summing for all nodepaths
45 }
47 ShapeEditor::ShapeEditor(SPDesktop *dt) {
48 this->desktop = dt;
49 this->grab_node = -1;
50 this->nodepath = NULL;
51 this->knotholder = NULL;
52 this->hit = false;
53 }
55 ShapeEditor::~ShapeEditor() {
56 unset_item();
57 }
59 void ShapeEditor::unset_item() {
61 Inkscape::XML::Node *old_repr = NULL;
63 if (this->nodepath) {
64 old_repr = this->nodepath->repr;
65 }
67 if (!old_repr && this->knotholder) {
68 old_repr = this->knotholder->repr;
69 }
71 if (old_repr) { // remove old listener
72 sp_repr_remove_listener_by_data(old_repr, this);
73 Inkscape::GC::release(old_repr);
74 }
76 if (this->nodepath) {
77 this->grab_node = -1;
78 sp_nodepath_destroy(this->nodepath);
79 this->nodepath = NULL;
80 }
82 if (this->knotholder) {
83 sp_knot_holder_destroy(this->knotholder);
84 this->knotholder = NULL;
85 }
86 }
88 bool ShapeEditor::has_nodepath () {
89 return (this->nodepath != NULL);
90 }
92 bool ShapeEditor::has_knotholder () {
93 return (this->knotholder != NULL);
94 }
96 bool ShapeEditor::has_local_change () {
97 if (this->nodepath)
98 return (this->nodepath->local_change != 0);
99 else if (this->knotholder)
100 return (this->knotholder->local_change != 0);
101 else
102 return false;
103 }
105 void ShapeEditor::decrement_local_change () {
106 if (this->nodepath) {
107 if (this->nodepath->local_change > 0)
108 this->nodepath->local_change--;
109 } else if (this->knotholder) {
110 this->knotholder->local_change = FALSE;
111 }
112 }
114 SPItem *ShapeEditor::get_item () {
115 SPItem *item = NULL;
116 if (this->has_nodepath()) {
117 item = this->nodepath->item;
118 } else if (this->has_knotholder()) {
119 item = SP_ITEM(this->knotholder->item);
120 }
121 return item;
122 }
124 GList *ShapeEditor::save_nodepath_selection () {
125 if (this->nodepath)
126 return ::save_nodepath_selection (this->nodepath);
127 return NULL;
128 }
130 void ShapeEditor::restore_nodepath_selection (GList *saved) {
131 if (this->nodepath && saved)
132 ::restore_nodepath_selection (this->nodepath, saved);
133 }
135 bool ShapeEditor::nodepath_edits_repr_key(gchar const *name) {
136 if (nodepath && name) {
137 return ( !strcmp(name, nodepath->repr_key) || !strcmp(name, nodepath->repr_nodetypes_key) );
138 }
140 return false;
141 }
143 static void shapeeditor_event_attr_changed(Inkscape::XML::Node */*repr*/, gchar const *name,
144 gchar const */*old_value*/, gchar const */*new_value*/,
145 bool /*is_interactive*/, gpointer data)
146 {
147 gboolean changed = FALSE;
149 g_assert(data);
150 ShapeEditor *sh = ((ShapeEditor *) data);
152 if ( sh->has_knotholder() || ( sh->has_nodepath() && sh->nodepath_edits_repr_key(name) ) )
153 {
154 changed = !sh->has_local_change();
155 sh->decrement_local_change();
156 }
158 if (changed) {
159 GList *saved = NULL;
160 if (sh->has_nodepath()) {
161 saved = sh->save_nodepath_selection();
162 }
164 sh->reset_item ();
166 if (sh->has_nodepath() && saved) {
167 sh->restore_nodepath_selection(saved);
168 g_list_free (saved);
169 }
170 }
172 sh->update_statusbar(); //TODO: sh->get_container()->update_statusbar();
173 }
175 static Inkscape::XML::NodeEventVector shapeeditor_repr_events = {
176 NULL, /* child_added */
177 NULL, /* child_removed */
178 shapeeditor_event_attr_changed,
179 NULL, /* content_changed */
180 NULL /* order_changed */
181 };
184 void ShapeEditor::set_item(SPItem *item) {
186 unset_item();
188 this->grab_node = -1;
190 if (item) {
191 this->nodepath = sp_nodepath_new(desktop, item, (prefs_get_int_attribute("tools.nodes", "show_handles", 1) != 0));
192 if (this->nodepath) {
193 this->nodepath->shape_editor = this;
194 }
195 this->knotholder = sp_item_knot_holder(item, desktop);
197 if (this->nodepath || this->knotholder) {
198 // setting new listener
199 Inkscape::XML::Node *repr;
200 if (this->knotholder)
201 repr = this->knotholder->repr;
202 else
203 repr = SP_OBJECT_REPR(item);
204 if (repr) {
205 Inkscape::GC::anchor(repr);
206 sp_repr_add_listener(repr, &shapeeditor_repr_events, this);
207 }
208 }
209 }
210 }
212 /** Please note that this function only works for path parameters.
213 * All other parameters probably will crash Inkscape!
214 * Fortunately, there are no other on-canvas edittable objects at this moment :)
215 */
216 void ShapeEditor::set_item_livepatheffect_parameter(SPItem *item, SPObject *lpeobject, const char * key) {
218 unset_item();
220 this->grab_node = -1;
222 if (lpeobject) {
223 this->knotholder = NULL; // it's a path, no special knotholder needed.
224 this->nodepath = sp_nodepath_new( desktop, lpeobject,
225 (prefs_get_int_attribute("tools.nodes", "show_handles", 1) != 0),
226 key, item);
227 if (this->nodepath) {
228 this->nodepath->shape_editor = this;
230 // setting new listener
231 Inkscape::XML::Node *repr = SP_OBJECT_REPR(lpeobject);
232 if (repr) {
233 Inkscape::GC::anchor(repr);
234 sp_repr_add_listener(repr, &shapeeditor_repr_events, this);
235 }
236 }
237 }
238 }
240 /** FIXME: think about this. Is this thing only called when the item needs to be updated?
241 Why not make a reload function in NodePath and in KnotHolder? */
242 void ShapeEditor::reset_item ()
243 {
244 if ( (this->nodepath) && (IS_LIVEPATHEFFECT(this->nodepath->object)) ) {
245 SPItem * item = this->nodepath->item;
246 SPObject *obj = this->nodepath->object;
247 char * key = g_strdup(this->nodepath->repr_key);
248 set_item_livepatheffect_parameter(item, obj, key);
249 g_free(key);
250 } else {
251 SPItem * item = get_item();
252 set_item(item);
253 }
254 }
256 void ShapeEditor::nodepath_destroyed () {
257 this->nodepath = NULL;
258 }
260 void ShapeEditor::update_statusbar () {
261 if (this->nodepath)
262 sp_nodepath_update_statusbar(this->nodepath);
263 }
265 bool ShapeEditor::is_over_stroke (NR::Point event_p, bool remember) {
267 if (!this->nodepath)
268 return false; // no stroke in knotholder
270 SPItem *item = get_item();
272 //Translate click point into proper coord system
273 this->curvepoint_doc = desktop->w2d(event_p);
274 this->curvepoint_doc *= sp_item_dt2i_affine(item);
276 sp_nodepath_ensure_livarot_path(this->nodepath);
278 NR::Maybe<Path::cut_position> position = get_nearest_position_on_Path(this->nodepath->livarot_path, this->curvepoint_doc);
279 if (!position) {
280 return false;
281 }
283 NR::Point nearest = get_point_on_Path(this->nodepath->livarot_path, position->piece, position->t);
284 NR::Point delta = nearest - this->curvepoint_doc;
286 delta = desktop->d2w(delta);
288 double stroke_tolerance =
289 (( !SP_OBJECT_STYLE(item)->stroke.isNone() ?
290 desktop->current_zoom() *
291 SP_OBJECT_STYLE (item)->stroke_width.computed * 0.5 *
292 sp_item_i2d_affine (item).expansion()
293 : 0.0)
294 + prefs_get_int_attribute_limited("options.dragtolerance", "value", 0, 0, 100)) /sp_item_i2d_affine (item).expansion();
295 bool close = (NR::L2 (delta) < stroke_tolerance);
297 if (remember && close) {
298 this->curvepoint_event[NR::X] = (gint) event_p [NR::X];
299 this->curvepoint_event[NR::Y] = (gint) event_p [NR::Y];
300 this->hit = true;
301 this->grab_t = position->t;
302 this->grab_node = position->piece;
303 }
305 return close;
306 }
308 void ShapeEditor::add_node_near_point() {
309 if (this->nodepath) {
310 sp_nodepath_add_node_near_point(this->nodepath, this->curvepoint_doc);
311 } else if (this->knotholder) {
312 // we do not add nodes in knotholder... yet
313 }
314 }
316 void ShapeEditor::select_segment_near_point(bool toggle) {
317 if (this->nodepath) {
318 sp_nodepath_select_segment_near_point(this->nodepath, this->curvepoint_doc, toggle);
319 } else if (this->knotholder) {
320 // we do not select segments in knotholder... yet?
321 }
322 }
324 void ShapeEditor::cancel_hit() {
325 this->hit = false;
326 }
328 bool ShapeEditor::hits_curve() {
329 return (this->hit);
330 }
333 void ShapeEditor::curve_drag(gdouble eventx, gdouble eventy) {
334 if (this->nodepath && !this->nodepath->straight_path) {
336 if (this->grab_node == -1) // don't know which segment to drag
337 return;
339 // We round off the extra precision in the motion coordinates provided
340 // by some input devices (like tablets). As we'll store the coordinates
341 // as integers in curvepoint_event we need to do this rounding before
342 // comparing them with the last coordinates from curvepoint_event.
343 // See bug #1593499 for details.
345 gint x = (gint) Inkscape::round(eventx);
346 gint y = (gint) Inkscape::round(eventy);
349 // The coordinates hasn't changed since the last motion event, abort
350 if (this->curvepoint_event[NR::X] == x &&
351 this->curvepoint_event[NR::Y] == y)
352 return;
354 NR::Point const delta_w(eventx - this->curvepoint_event[NR::X],
355 eventy - this->curvepoint_event[NR::Y]);
356 NR::Point const delta_dt(this->desktop->w2d(delta_w));
358 sp_nodepath_curve_drag (this->grab_node, this->grab_t, delta_dt); //!!! FIXME: which nodepath?!!! also uses current!!!
359 this->curvepoint_event[NR::X] = x;
360 this->curvepoint_event[NR::Y] = y;
362 } else if (this->knotholder) {
363 // we do not drag curve in knotholder
364 }
366 }
368 void ShapeEditor::finish_drag() {
369 if (this->nodepath && this->hit) {
370 sp_nodepath_update_repr (this->nodepath, _("Drag curve"));
371 }
372 }
374 void ShapeEditor::select_rect(NR::Rect const &rect, bool add) {
375 if (this->nodepath) {
376 sp_nodepath_select_rect(this->nodepath, rect, add);
377 }
378 }
380 bool ShapeEditor::has_selection() {
381 if (this->nodepath)
382 return this->nodepath->selected;
383 return false; // so far, knotholder cannot have selection
384 }
386 void ShapeEditor::deselect() {
387 if (this->nodepath)
388 sp_nodepath_deselect(this->nodepath);
389 }
391 void ShapeEditor::add_node () {
392 sp_node_selected_add_node(this->nodepath);
393 }
395 void ShapeEditor::delete_nodes () {
396 sp_node_selected_delete(this->nodepath);
397 }
399 void ShapeEditor::delete_nodes_preserving_shape () {
400 if (this->nodepath && this->nodepath->selected) {
401 sp_node_delete_preserve(g_list_copy(this->nodepath->selected));
402 }
403 }
405 void ShapeEditor::delete_segment () {
406 sp_node_selected_delete_segment(this->nodepath);
407 }
409 void ShapeEditor::set_node_type(int type) {
410 sp_node_selected_set_type(this->nodepath, (Inkscape::NodePath::NodeType) type);
411 }
413 void ShapeEditor::break_at_nodes() {
414 sp_node_selected_break(this->nodepath);
415 }
417 void ShapeEditor::join_nodes() {
418 sp_node_selected_join(this->nodepath);
419 }
421 void ShapeEditor::join_segments() {
422 sp_node_selected_join_segment(this->nodepath);
423 }
425 void ShapeEditor::duplicate_nodes() {
426 sp_node_selected_duplicate(this->nodepath);
427 }
429 void ShapeEditor::set_type_of_segments(NRPathcode code) {
430 sp_node_selected_set_line_type(this->nodepath, code);
431 }
433 void ShapeEditor::move_nodes_screen(gdouble dx, gdouble dy) {
434 sp_node_selected_move_screen(this->nodepath, dx, dy);
435 }
436 void ShapeEditor::move_nodes(gdouble dx, gdouble dy) {
437 sp_node_selected_move(this->nodepath, dx, dy);
438 }
440 void ShapeEditor::rotate_nodes(gdouble angle, int which, bool screen) {
441 if (this->nodepath)
442 sp_nodepath_selected_nodes_rotate (this->nodepath, angle, which, screen);
443 }
445 void ShapeEditor::scale_nodes(gdouble const grow, int const which) {
446 sp_nodepath_selected_nodes_scale(this->nodepath, grow, which);
447 }
448 void ShapeEditor::scale_nodes_screen(gdouble const grow, int const which) {
449 sp_nodepath_selected_nodes_scale_screen(this->nodepath, grow, which);
450 }
452 void ShapeEditor::select_all (bool invert) {
453 if (this->nodepath)
454 sp_nodepath_select_all (this->nodepath, invert);
455 }
456 void ShapeEditor::select_all_from_subpath (bool invert) {
457 if (this->nodepath)
458 sp_nodepath_select_all_from_subpath (this->nodepath, invert);
459 }
460 void ShapeEditor::select_next () {
461 if (this->nodepath) {
462 sp_nodepath_select_next (this->nodepath);
463 if (this->nodepath->numSelected() >= 1) {
464 this->desktop->scroll_to_point(&(this->nodepath->singleSelectedCoords()), 1.0);
465 }
466 }
467 }
468 void ShapeEditor::select_prev () {
469 if (this->nodepath) {
470 sp_nodepath_select_prev (this->nodepath);
471 if (this->nodepath->numSelected() >= 1) {
472 this->desktop->scroll_to_point(&(this->nodepath->singleSelectedCoords()), 1.0);
473 }
474 }
475 }
477 void ShapeEditor::show_handles (bool show) {
478 if (this->nodepath && !this->nodepath->straight_path)
479 sp_nodepath_show_handles (this->nodepath, show);
480 }
483 void ShapeEditor::flip (NR::Dim2 axis, NR::Maybe<NR::Point> center) {
484 if (this->nodepath)
485 sp_nodepath_flip (this->nodepath, axis, center);
486 }
488 void ShapeEditor::distribute (NR::Dim2 axis) {
489 if (this->nodepath)
490 sp_nodepath_selected_distribute (this->nodepath, axis);
491 }
492 void ShapeEditor::align (NR::Dim2 axis) {
493 if (this->nodepath)
494 sp_nodepath_selected_align (this->nodepath, axis);
495 }
498 /*
499 Local Variables:
500 mode:c++
501 c-file-style:"stroustrup"
502 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
503 indent-tabs-mode:nil
504 fill-column:99
505 End:
506 */
507 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :