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 "node-context.h"
26 #include "xml/node-event-vector.h"
27 #include "prefs-utils.h"
28 #include "object-edit.h"
29 #include "splivarot.h"
30 #include "style.h"
32 #include "shape-editor.h"
35 ShapeEditorsCollective::ShapeEditorsCollective(SPDesktop */*dt*/) {
36 }
38 ShapeEditorsCollective::~ShapeEditorsCollective() {
39 }
42 void ShapeEditorsCollective::update_statusbar() {
44 //!!! move from nodepath: sp_nodepath_update_statusbar but summing for all nodepaths
46 }
48 ShapeEditor::ShapeEditor(SPDesktop *dt) {
49 this->desktop = dt;
50 this->grab_node = -1;
51 this->nodepath = NULL;
52 this->knotholder = NULL;
53 this->hit = false;
54 }
56 ShapeEditor::~ShapeEditor() {
57 unset_item();
58 }
60 void ShapeEditor::unset_item() {
62 Inkscape::XML::Node *old_repr = NULL;
64 if (this->nodepath) {
65 old_repr = this->nodepath->repr;
66 }
68 if (!old_repr && this->knotholder) {
69 old_repr = this->knotholder->repr;
70 }
72 if (old_repr) { // remove old listener
73 sp_repr_remove_listener_by_data(old_repr, this);
74 Inkscape::GC::release(old_repr);
75 }
77 if (this->nodepath) {
78 this->grab_node = -1;
79 sp_nodepath_destroy(this->nodepath);
80 this->nodepath = NULL;
81 }
83 if (this->knotholder) {
84 sp_knot_holder_destroy(this->knotholder);
85 this->knotholder = NULL;
86 }
87 }
89 bool ShapeEditor::has_nodepath () {
90 return (this->nodepath != NULL);
91 }
93 bool ShapeEditor::has_knotholder () {
94 return (this->knotholder != NULL);
95 }
97 bool ShapeEditor::has_local_change () {
98 if (this->nodepath)
99 return (this->nodepath->local_change != 0);
100 else if (this->knotholder)
101 return (this->knotholder->local_change != 0);
102 else
103 return false;
104 }
106 void ShapeEditor::decrement_local_change () {
107 if (this->nodepath) {
108 if (this->nodepath->local_change > 0)
109 this->nodepath->local_change--;
110 } else if (this->knotholder) {
111 this->knotholder->local_change = FALSE;
112 }
113 }
115 SPItem *ShapeEditor::get_item () {
116 SPItem *item = NULL;
117 if (this->has_nodepath()) {
118 item = this->nodepath->item;
119 } else if (this->has_knotholder()) {
120 item = SP_ITEM(this->knotholder->item);
121 }
122 return item;
123 }
125 GList *ShapeEditor::save_nodepath_selection () {
126 if (this->nodepath)
127 return ::save_nodepath_selection (this->nodepath);
128 return NULL;
129 }
131 void ShapeEditor::restore_nodepath_selection (GList *saved) {
132 if (this->nodepath && saved)
133 ::restore_nodepath_selection (this->nodepath, saved);
134 }
136 bool ShapeEditor::nodepath_edits_repr_key(gchar const *name) {
137 if (nodepath && name) {
138 return ( !strcmp(name, nodepath->repr_key) || !strcmp(name, nodepath->repr_nodetypes_key) );
139 }
141 return false;
142 }
144 static void shapeeditor_event_attr_changed(Inkscape::XML::Node */*repr*/, gchar const *name,
145 gchar const */*old_value*/, gchar const */*new_value*/,
146 bool /*is_interactive*/, gpointer data)
147 {
148 gboolean changed = FALSE;
150 g_assert(data);
151 ShapeEditor *sh = ((ShapeEditor *) data);
153 if ( sh->has_knotholder() || ( sh->has_nodepath() && sh->nodepath_edits_repr_key(name) ) )
154 {
155 changed = !sh->has_local_change();
156 sh->decrement_local_change();
157 }
159 if (changed) {
160 GList *saved = NULL;
161 if (sh->has_nodepath()) {
162 saved = sh->save_nodepath_selection();
163 }
165 sh->reset_item ();
167 if (sh->has_nodepath() && saved) {
168 sh->restore_nodepath_selection(saved);
169 g_list_free (saved);
170 }
171 }
173 sh->update_statusbar(); //TODO: sh->get_container()->update_statusbar();
174 }
176 static Inkscape::XML::NodeEventVector shapeeditor_repr_events = {
177 NULL, /* child_added */
178 NULL, /* child_removed */
179 shapeeditor_event_attr_changed,
180 NULL, /* content_changed */
181 NULL /* order_changed */
182 };
185 void ShapeEditor::set_item(SPItem *item) {
187 unset_item();
189 this->grab_node = -1;
191 if (item) {
192 this->nodepath = sp_nodepath_new(desktop, item, (prefs_get_int_attribute("tools.nodes", "show_handles", 1) != 0));
193 if (this->nodepath) {
194 this->nodepath->shape_editor = this;
195 }
196 this->knotholder = sp_item_knot_holder(item, desktop);
198 if (this->nodepath || this->knotholder) {
199 // setting new listener
200 Inkscape::XML::Node *repr;
201 if (this->knotholder)
202 repr = this->knotholder->repr;
203 else
204 repr = SP_OBJECT_REPR(item);
205 if (repr) {
206 Inkscape::GC::anchor(repr);
207 sp_repr_add_listener(repr, &shapeeditor_repr_events, this);
208 }
209 }
210 }
211 }
213 /** Please note that this function only works for path parameters.
214 * All other parameters probably will crash Inkscape!
215 */
216 void ShapeEditor::set_item_lpe_path_parameter(SPItem *item, SPObject *lpeobject, const char * key) {
218 unset_item();
220 this->grab_node = -1;
222 if (lpeobject) {
223 // FIXME ? just setting knotholder to NULL... not necessary to delete it ?
224 this->knotholder = NULL; // it's a path, no special knotholder needed.
225 this->nodepath = sp_nodepath_new( desktop, lpeobject,
226 (prefs_get_int_attribute("tools.nodes", "show_handles", 1) != 0),
227 key, item);
228 if (this->nodepath) {
229 this->nodepath->shape_editor = this;
231 // setting new listener
232 Inkscape::XML::Node *repr = SP_OBJECT_REPR(lpeobject);
233 if (repr) {
234 Inkscape::GC::anchor(repr);
235 sp_repr_add_listener(repr, &shapeeditor_repr_events, this);
236 }
237 }
238 }
239 }
241 /** Please note that this function only works for point parameters.
242 * All other parameters probably will crash Inkscape!
243 */
244 void ShapeEditor::set_item_lpe_point_parameter(SPItem */*item*/, SPObject */*lpeobject*/, const char * /*key*/) {
245 g_message("ShapeEditor::set_item_lpe_point_parameter has not been implemented yet!");
246 }
249 /** FIXME: think about this. Is this thing only called when the item needs to be updated?
250 Why not make a reload function in NodePath and in KnotHolder? */
251 void ShapeEditor::reset_item ()
252 {
253 if ( (this->nodepath) && (IS_LIVEPATHEFFECT(this->nodepath->object)) ) {
254 SPItem * item = this->nodepath->item;
255 SPObject *obj = this->nodepath->object;
256 char * key = g_strdup(this->nodepath->repr_key);
257 set_item_lpe_path_parameter(item, obj, key); // the above checks for nodepath, so it is indeed a path that we are editing
258 g_free(key);
259 } else {
260 SPItem * item = get_item();
261 set_item(item);
262 }
263 }
265 void ShapeEditor::nodepath_destroyed () {
266 this->nodepath = NULL;
267 }
269 void ShapeEditor::update_statusbar () {
270 if (this->nodepath)
271 sp_nodepath_update_statusbar(this->nodepath);
272 }
274 bool ShapeEditor::is_over_stroke (NR::Point event_p, bool remember) {
276 if (!this->nodepath)
277 return false; // no stroke in knotholder
279 SPItem *item = get_item();
281 //Translate click point into proper coord system
282 this->curvepoint_doc = desktop->w2d(event_p);
283 this->curvepoint_doc *= sp_item_dt2i_affine(item);
285 sp_nodepath_ensure_livarot_path(this->nodepath);
287 NR::Maybe<Path::cut_position> position = get_nearest_position_on_Path(this->nodepath->livarot_path, this->curvepoint_doc);
288 if (!position) {
289 return false;
290 }
292 NR::Point nearest = get_point_on_Path(this->nodepath->livarot_path, position->piece, position->t);
293 NR::Point delta = nearest - this->curvepoint_doc;
295 delta = desktop->d2w(delta);
297 double stroke_tolerance =
298 (( !SP_OBJECT_STYLE(item)->stroke.isNone() ?
299 desktop->current_zoom() *
300 SP_OBJECT_STYLE (item)->stroke_width.computed * 0.5 *
301 sp_item_i2d_affine (item).expansion()
302 : 0.0)
303 + prefs_get_int_attribute_limited("options.dragtolerance", "value", 0, 0, 100)) /sp_item_i2d_affine (item).expansion();
304 bool close = (NR::L2 (delta) < stroke_tolerance);
306 if (remember && close) {
307 this->curvepoint_event[NR::X] = (gint) event_p [NR::X];
308 this->curvepoint_event[NR::Y] = (gint) event_p [NR::Y];
309 this->hit = true;
310 this->grab_t = position->t;
311 this->grab_node = position->piece;
312 }
314 return close;
315 }
317 void ShapeEditor::add_node_near_point() {
318 if (this->nodepath) {
319 sp_nodepath_add_node_near_point(this->nodepath, this->curvepoint_doc);
320 } else if (this->knotholder) {
321 // we do not add nodes in knotholder... yet
322 }
323 }
325 void ShapeEditor::select_segment_near_point(bool toggle) {
326 if (this->nodepath) {
327 sp_nodepath_select_segment_near_point(this->nodepath, this->curvepoint_doc, toggle);
328 } else if (this->knotholder) {
329 // we do not select segments in knotholder... yet?
330 }
331 }
333 void ShapeEditor::cancel_hit() {
334 this->hit = false;
335 }
337 bool ShapeEditor::hits_curve() {
338 return (this->hit);
339 }
342 void ShapeEditor::curve_drag(gdouble eventx, gdouble eventy) {
343 if (this->nodepath && !this->nodepath->straight_path) {
345 if (this->grab_node == -1) // don't know which segment to drag
346 return;
348 // We round off the extra precision in the motion coordinates provided
349 // by some input devices (like tablets). As we'll store the coordinates
350 // as integers in curvepoint_event we need to do this rounding before
351 // comparing them with the last coordinates from curvepoint_event.
352 // See bug #1593499 for details.
354 gint x = (gint) Inkscape::round(eventx);
355 gint y = (gint) Inkscape::round(eventy);
358 // The coordinates hasn't changed since the last motion event, abort
359 if (this->curvepoint_event[NR::X] == x &&
360 this->curvepoint_event[NR::Y] == y)
361 return;
363 NR::Point const delta_w(eventx - this->curvepoint_event[NR::X],
364 eventy - this->curvepoint_event[NR::Y]);
365 NR::Point const delta_dt(this->desktop->w2d(delta_w));
367 sp_nodepath_curve_drag (this->grab_node, this->grab_t, delta_dt); //!!! FIXME: which nodepath?!!! also uses current!!!
368 this->curvepoint_event[NR::X] = x;
369 this->curvepoint_event[NR::Y] = y;
371 } else if (this->knotholder) {
372 // we do not drag curve in knotholder
373 }
375 }
377 void ShapeEditor::finish_drag() {
378 if (this->nodepath && this->hit) {
379 sp_nodepath_update_repr (this->nodepath, _("Drag curve"));
380 }
381 }
383 void ShapeEditor::select_rect(NR::Rect const &rect, bool add) {
384 if (this->nodepath) {
385 sp_nodepath_select_rect(this->nodepath, rect, add);
386 }
387 }
389 bool ShapeEditor::has_selection() {
390 if (this->nodepath)
391 return this->nodepath->selected;
392 return false; // so far, knotholder cannot have selection
393 }
395 void ShapeEditor::deselect() {
396 if (this->nodepath)
397 sp_nodepath_deselect(this->nodepath);
398 }
400 void ShapeEditor::add_node () {
401 sp_node_selected_add_node(this->nodepath);
402 }
404 void ShapeEditor::delete_nodes () {
405 sp_node_selected_delete(this->nodepath);
406 }
408 void ShapeEditor::delete_nodes_preserving_shape () {
409 if (this->nodepath && this->nodepath->selected) {
410 sp_node_delete_preserve(g_list_copy(this->nodepath->selected));
411 }
412 }
414 void ShapeEditor::delete_segment () {
415 sp_node_selected_delete_segment(this->nodepath);
416 }
418 void ShapeEditor::set_node_type(int type) {
419 sp_node_selected_set_type(this->nodepath, (Inkscape::NodePath::NodeType) type);
420 }
422 void ShapeEditor::break_at_nodes() {
423 sp_node_selected_break(this->nodepath);
424 }
426 void ShapeEditor::join_nodes() {
427 sp_node_selected_join(this->nodepath);
428 }
430 void ShapeEditor::join_segments() {
431 sp_node_selected_join_segment(this->nodepath);
432 }
434 void ShapeEditor::duplicate_nodes() {
435 sp_node_selected_duplicate(this->nodepath);
436 }
438 void ShapeEditor::set_type_of_segments(NRPathcode code) {
439 sp_node_selected_set_line_type(this->nodepath, code);
440 }
442 void ShapeEditor::move_nodes_screen(gdouble dx, gdouble dy) {
443 sp_node_selected_move_screen(this->nodepath, dx, dy);
444 }
445 void ShapeEditor::move_nodes(gdouble dx, gdouble dy) {
446 sp_node_selected_move(this->nodepath, dx, dy);
447 }
449 void ShapeEditor::rotate_nodes(gdouble angle, int which, bool screen) {
450 if (this->nodepath)
451 sp_nodepath_selected_nodes_rotate (this->nodepath, angle, which, screen);
452 }
454 void ShapeEditor::scale_nodes(gdouble const grow, int const which) {
455 sp_nodepath_selected_nodes_scale(this->nodepath, grow, which);
456 }
457 void ShapeEditor::scale_nodes_screen(gdouble const grow, int const which) {
458 sp_nodepath_selected_nodes_scale_screen(this->nodepath, grow, which);
459 }
461 void ShapeEditor::select_all (bool invert) {
462 if (this->nodepath)
463 sp_nodepath_select_all (this->nodepath, invert);
464 }
465 void ShapeEditor::select_all_from_subpath (bool invert) {
466 if (this->nodepath)
467 sp_nodepath_select_all_from_subpath (this->nodepath, invert);
468 }
469 void ShapeEditor::select_next () {
470 if (this->nodepath) {
471 sp_nodepath_select_next (this->nodepath);
472 if (this->nodepath->numSelected() >= 1) {
473 this->desktop->scroll_to_point(&(this->nodepath->singleSelectedCoords()), 1.0);
474 }
475 }
476 }
477 void ShapeEditor::select_prev () {
478 if (this->nodepath) {
479 sp_nodepath_select_prev (this->nodepath);
480 if (this->nodepath->numSelected() >= 1) {
481 this->desktop->scroll_to_point(&(this->nodepath->singleSelectedCoords()), 1.0);
482 }
483 }
484 }
486 void ShapeEditor::show_handles (bool show) {
487 if (this->nodepath && !this->nodepath->straight_path)
488 sp_nodepath_show_handles (this->nodepath, show);
489 }
491 void ShapeEditor::show_helperpath (bool show) {
492 if (this->nodepath)
493 sp_nodepath_show_helperpath (this->nodepath, show);
494 }
496 void ShapeEditor::flip (NR::Dim2 axis, NR::Maybe<NR::Point> center) {
497 if (this->nodepath)
498 sp_nodepath_flip (this->nodepath, axis, center);
499 }
501 void ShapeEditor::distribute (NR::Dim2 axis) {
502 if (this->nodepath)
503 sp_nodepath_selected_distribute (this->nodepath, axis);
504 }
505 void ShapeEditor::align (NR::Dim2 axis) {
506 if (this->nodepath)
507 sp_nodepath_selected_align (this->nodepath, axis);
508 }
511 /*
512 Local Variables:
513 mode:c++
514 c-file-style:"stroustrup"
515 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
516 indent-tabs-mode:nil
517 fill-column:99
518 End:
519 */
520 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :