00e7c61b1dc223a13db66fb02ecab01cc4fafddf
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 }
136 static void shapeeditor_event_attr_changed(Inkscape::XML::Node *repr, gchar const *name,
137 gchar const *old_value, gchar const *new_value,
138 bool is_interactive, gpointer data)
139 {
140 gboolean changed = FALSE;
142 g_assert(data);
143 ShapeEditor *sh = ((ShapeEditor *) data);
145 if ( sh->has_nodepath() || sh->has_knotholder() )
146 {
147 changed = !sh->has_local_change();
148 sh->decrement_local_change();
149 }
151 if (changed) {
152 GList *saved = NULL;
153 if (sh->has_nodepath()) {
154 saved = sh->save_nodepath_selection();
155 }
157 sh->reset_item ();
159 if (sh->has_nodepath() && saved) {
160 sh->restore_nodepath_selection(saved);
161 g_list_free (saved);
162 }
163 }
165 sh->update_statusbar(); //TODO: sh->get_container()->update_statusbar();
166 }
168 static Inkscape::XML::NodeEventVector shapeeditor_repr_events = {
169 NULL, /* child_added */
170 NULL, /* child_removed */
171 shapeeditor_event_attr_changed,
172 NULL, /* content_changed */
173 NULL /* order_changed */
174 };
177 void ShapeEditor::set_item(SPItem *item) {
179 unset_item();
181 this->grab_node = -1;
183 if (item) {
184 this->nodepath = sp_nodepath_new(desktop, item, (prefs_get_int_attribute("tools.nodes", "show_handles", 1) != 0));
185 if (this->nodepath) {
186 this->nodepath->shape_editor = this;
187 }
188 this->knotholder = sp_item_knot_holder(item, desktop);
190 if (this->nodepath || this->knotholder) {
191 // setting new listener
192 Inkscape::XML::Node *repr;
193 if (this->knotholder)
194 repr = this->knotholder->repr;
195 else
196 repr = SP_OBJECT_REPR(item);
197 if (repr) {
198 Inkscape::GC::anchor(repr);
199 sp_repr_add_listener(repr, &shapeeditor_repr_events, this);
200 }
201 }
202 }
203 }
205 /** Please note that this function only works for path parameters.
206 * All other parameters probably will crash Inkscape!
207 * Fortunately, there are no other on-canvas edittable objects at this moment :)
208 */
209 void ShapeEditor::set_item_livepatheffect_parameter(SPItem *item, SPObject *lpeobject, const char * key) {
211 unset_item();
213 this->grab_node = -1;
215 if (lpeobject) {
216 this->knotholder = NULL; // it's a path, no special knotholder needed.
217 this->nodepath = sp_nodepath_new( desktop, lpeobject,
218 (prefs_get_int_attribute("tools.nodes", "show_handles", 1) != 0),
219 key, item);
220 if (this->nodepath) {
221 this->nodepath->shape_editor = this;
223 // setting new listener
224 Inkscape::XML::Node *repr = SP_OBJECT_REPR(lpeobject);
225 if (repr) {
226 Inkscape::GC::anchor(repr);
227 sp_repr_add_listener(repr, &shapeeditor_repr_events, this);
228 }
229 }
230 }
231 }
233 /** FIXME: think about this. Is this thing only called when the item needs to be updated?
234 Why not make a reload function in NodePath and in KnotHolder? */
235 void ShapeEditor::reset_item ()
236 {
237 if ( (this->nodepath) && (IS_LIVEPATHEFFECT(this->nodepath->object)) ) {
238 SPItem * item = this->nodepath->item;
239 SPObject *obj = this->nodepath->object;
240 char * key = g_strdup(this->nodepath->repr_key);
241 set_item_livepatheffect_parameter(item, obj, key);
242 g_free(key);
243 } else {
244 SPItem * item = get_item();
245 set_item(item);
246 }
247 }
249 void ShapeEditor::nodepath_destroyed () {
250 this->nodepath = NULL;
251 }
253 void ShapeEditor::update_statusbar () {
254 if (this->nodepath)
255 sp_nodepath_update_statusbar(this->nodepath);
256 }
258 bool ShapeEditor::is_over_stroke (NR::Point event_p, bool remember) {
260 if (!this->nodepath)
261 return false; // no stroke in knotholder
263 SPItem *item = get_item();
265 //Translate click point into proper coord system
266 this->curvepoint_doc = desktop->w2d(event_p);
267 this->curvepoint_doc *= sp_item_dt2i_affine(item);
269 sp_nodepath_ensure_livarot_path(this->nodepath);
271 NR::Maybe<Path::cut_position> position = get_nearest_position_on_Path(this->nodepath->livarot_path, this->curvepoint_doc);
272 if (!position) {
273 return false;
274 }
276 NR::Point nearest = get_point_on_Path(this->nodepath->livarot_path, position->piece, position->t);
277 NR::Point delta = nearest - this->curvepoint_doc;
279 delta = desktop->d2w(delta);
281 double stroke_tolerance =
282 ( !SP_OBJECT_STYLE(item)->stroke.isNone() ?
283 desktop->current_zoom() *
284 SP_OBJECT_STYLE (item)->stroke_width.computed *
285 sp_item_i2d_affine (item).expansion() * 0.5
286 : 0.0)
287 + prefs_get_int_attribute_limited("options.dragtolerance", "value", 0, 0, 100); //(double) SP_EVENT_CONTEXT(nc)->tolerance;
289 bool close = (NR::L2 (delta) < stroke_tolerance);
291 if (remember && close) {
292 this->curvepoint_event[NR::X] = (gint) event_p [NR::X];
293 this->curvepoint_event[NR::Y] = (gint) event_p [NR::Y];
294 this->hit = true;
295 this->grab_t = position->t;
296 this->grab_node = position->piece;
297 }
299 return close;
300 }
302 void ShapeEditor::add_node_near_point() {
303 if (this->nodepath) {
304 sp_nodepath_add_node_near_point(this->nodepath, this->curvepoint_doc);
305 } else if (this->knotholder) {
306 // we do not add nodes in knotholder... yet
307 }
308 }
310 void ShapeEditor::select_segment_near_point(bool toggle) {
311 if (this->nodepath) {
312 sp_nodepath_select_segment_near_point(this->nodepath, this->curvepoint_doc, toggle);
313 } else if (this->knotholder) {
314 // we do not select segments in knotholder... yet?
315 }
316 }
318 void ShapeEditor::cancel_hit() {
319 this->hit = false;
320 }
322 bool ShapeEditor::hits_curve() {
323 return (this->hit);
324 }
327 void ShapeEditor::curve_drag(gdouble eventx, gdouble eventy) {
328 if (this->nodepath && !this->nodepath->straight_path) {
330 if (this->grab_node == -1) // don't know which segment to drag
331 return;
333 // We round off the extra precision in the motion coordinates provided
334 // by some input devices (like tablets). As we'll store the coordinates
335 // as integers in curvepoint_event we need to do this rounding before
336 // comparing them with the last coordinates from curvepoint_event.
337 // See bug #1593499 for details.
339 gint x = (gint) Inkscape::round(eventx);
340 gint y = (gint) Inkscape::round(eventy);
343 // The coordinates hasn't changed since the last motion event, abort
344 if (this->curvepoint_event[NR::X] == x &&
345 this->curvepoint_event[NR::Y] == y)
346 return;
348 NR::Point const delta_w(eventx - this->curvepoint_event[NR::X],
349 eventy - this->curvepoint_event[NR::Y]);
350 NR::Point const delta_dt(this->desktop->w2d(delta_w));
352 sp_nodepath_curve_drag (this->grab_node, this->grab_t, delta_dt); //!!! FIXME: which nodepath?!!! also uses current!!!
353 this->curvepoint_event[NR::X] = x;
354 this->curvepoint_event[NR::Y] = y;
356 } else if (this->knotholder) {
357 // we do not drag curve in knotholder
358 }
360 }
362 void ShapeEditor::finish_drag() {
363 if (this->nodepath && this->hit) {
364 sp_nodepath_update_repr (this->nodepath, _("Drag curve"));
365 }
366 }
368 void ShapeEditor::select_rect(NR::Rect const &rect, bool add) {
369 if (this->nodepath) {
370 sp_nodepath_select_rect(this->nodepath, rect, add);
371 }
372 }
374 bool ShapeEditor::has_selection() {
375 if (this->nodepath)
376 return this->nodepath->selected;
377 return false; // so far, knotholder cannot have selection
378 }
380 void ShapeEditor::deselect() {
381 if (this->nodepath)
382 sp_nodepath_deselect(this->nodepath);
383 }
385 void ShapeEditor::add_node () {
386 sp_node_selected_add_node(this->nodepath);
387 }
389 void ShapeEditor::delete_nodes () {
390 sp_node_selected_delete(this->nodepath);
391 }
393 void ShapeEditor::delete_nodes_preserving_shape () {
394 if (this->nodepath && this->nodepath->selected) {
395 sp_node_delete_preserve(g_list_copy(this->nodepath->selected));
396 }
397 }
399 void ShapeEditor::delete_segment () {
400 sp_node_selected_delete_segment(this->nodepath);
401 }
403 void ShapeEditor::set_node_type(int type) {
404 sp_node_selected_set_type(this->nodepath, (Inkscape::NodePath::NodeType) type);
405 }
407 void ShapeEditor::break_at_nodes() {
408 sp_node_selected_break(this->nodepath);
409 }
411 void ShapeEditor::join_nodes() {
412 sp_node_selected_join(this->nodepath);
413 }
415 void ShapeEditor::join_segments() {
416 sp_node_selected_join_segment(this->nodepath);
417 }
419 void ShapeEditor::duplicate_nodes() {
420 sp_node_selected_duplicate(this->nodepath);
421 }
423 void ShapeEditor::set_type_of_segments(NRPathcode code) {
424 sp_node_selected_set_line_type(this->nodepath, code);
425 }
427 void ShapeEditor::move_nodes_screen(gdouble dx, gdouble dy) {
428 sp_node_selected_move_screen(this->nodepath, dx, dy);
429 }
430 void ShapeEditor::move_nodes(gdouble dx, gdouble dy) {
431 sp_node_selected_move(this->nodepath, dx, dy);
432 }
434 void ShapeEditor::rotate_nodes(gdouble angle, int which, bool screen) {
435 if (this->nodepath)
436 sp_nodepath_selected_nodes_rotate (this->nodepath, angle, which, screen);
437 }
439 void ShapeEditor::scale_nodes(gdouble const grow, int const which) {
440 sp_nodepath_selected_nodes_scale(this->nodepath, grow, which);
441 }
442 void ShapeEditor::scale_nodes_screen(gdouble const grow, int const which) {
443 sp_nodepath_selected_nodes_scale_screen(this->nodepath, grow, which);
444 }
446 void ShapeEditor::select_all (bool invert) {
447 if (this->nodepath)
448 sp_nodepath_select_all (this->nodepath, invert);
449 }
450 void ShapeEditor::select_all_from_subpath (bool invert) {
451 if (this->nodepath)
452 sp_nodepath_select_all_from_subpath (this->nodepath, invert);
453 }
454 void ShapeEditor::select_next () {
455 if (this->nodepath) {
456 sp_nodepath_select_next (this->nodepath);
457 if (this->nodepath->numSelected() >= 1) {
458 this->desktop->scroll_to_point(&(this->nodepath->singleSelectedCoords()), 1.0);
459 }
460 }
461 }
462 void ShapeEditor::select_prev () {
463 if (this->nodepath) {
464 sp_nodepath_select_prev (this->nodepath);
465 if (this->nodepath->numSelected() >= 1) {
466 this->desktop->scroll_to_point(&(this->nodepath->singleSelectedCoords()), 1.0);
467 }
468 }
469 }
471 void ShapeEditor::show_handles (bool show) {
472 if (this->nodepath)
473 sp_nodepath_show_handles (this->nodepath, show);
474 }
477 void ShapeEditor::flip (NR::Dim2 axis, NR::Maybe<NR::Point> center) {
478 if (this->nodepath)
479 sp_nodepath_flip (this->nodepath, axis, center);
480 }
482 void ShapeEditor::distribute (NR::Dim2 axis) {
483 if (this->nodepath)
484 sp_nodepath_selected_distribute (this->nodepath, axis);
485 }
486 void ShapeEditor::align (NR::Dim2 axis) {
487 if (this->nodepath)
488 sp_nodepath_selected_align (this->nodepath, axis);
489 }
492 /*
493 Local Variables:
494 mode:c++
495 c-file-style:"stroustrup"
496 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
497 indent-tabs-mode:nil
498 fill-column:99
499 End:
500 */
501 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :