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 "selection.h"
20 #include "desktop.h"
21 #include "desktop-handles.h"
22 #include "knotholder.h"
23 #include "node-context.h"
24 #include "xml/node-event-vector.h"
25 #include "prefs-utils.h"
26 #include "object-edit.h"
27 #include "splivarot.h"
28 #include "style.h"
30 #include "shape-editor.h"
33 ShapeEditorsCollective::ShapeEditorsCollective(SPDesktop *dt) {
34 }
36 ShapeEditorsCollective::~ShapeEditorsCollective() {
37 }
40 void ShapeEditorsCollective::update_statusbar() {
42 //!!! move from nodepath: sp_nodepath_update_statusbar but summing for all nodepaths
44 }
46 ShapeEditor::ShapeEditor(SPDesktop *dt) {
47 this->desktop = dt;
48 this->grab_node = -1;
49 this->nodepath = NULL;
50 this->knotholder = NULL;
51 this->hit = false;
52 }
54 ShapeEditor::~ShapeEditor() {
55 unset_item();
56 }
58 void ShapeEditor::unset_item() {
60 Inkscape::XML::Node *old_repr = NULL;
62 if (this->nodepath) {
63 old_repr = this->nodepath->repr;
64 }
66 if (!old_repr && this->knotholder) {
67 old_repr = this->knotholder->repr;
68 }
70 if (old_repr) { // remove old listener
71 sp_repr_remove_listener_by_data(old_repr, this);
72 Inkscape::GC::release(old_repr);
73 }
75 if (this->nodepath) {
76 this->grab_node = -1;
77 sp_nodepath_destroy(this->nodepath);
78 this->nodepath = NULL;
79 }
81 if (this->knotholder) {
82 sp_knot_holder_destroy(this->knotholder);
83 this->knotholder = NULL;
84 }
85 }
87 bool ShapeEditor::has_nodepath () {
88 return (this->nodepath != NULL);
89 }
91 bool ShapeEditor::has_knotholder () {
92 return (this->knotholder != NULL);
93 }
95 bool ShapeEditor::has_local_change () {
96 if (this->nodepath)
97 return (this->nodepath->local_change != 0);
98 else if (this->knotholder)
99 return (this->knotholder->local_change != 0);
100 else
101 return false;
102 }
104 void ShapeEditor::decrement_local_change () {
105 if (this->nodepath) {
106 if (this->nodepath->local_change > 0)
107 this->nodepath->local_change--;
108 } else if (this->knotholder) {
109 this->knotholder->local_change = FALSE;
110 }
111 }
113 SPItem *ShapeEditor::get_item () {
114 SPItem *item = NULL;
115 if (this->has_nodepath()) {
116 item = SP_ITEM(this->nodepath->object);
117 } else if (this->has_knotholder()) {
118 item = SP_ITEM(this->knotholder->item);
119 }
120 return item;
121 }
123 GList *ShapeEditor::save_nodepath_selection () {
124 if (this->nodepath)
125 return ::save_nodepath_selection (this->nodepath);
126 return NULL;
127 }
129 void ShapeEditor::restore_nodepath_selection (GList *saved) {
130 if (this->nodepath && saved)
131 ::restore_nodepath_selection (this->nodepath, saved);
132 }
135 static void shapeeditor_event_attr_changed(Inkscape::XML::Node *repr, gchar const *name,
136 gchar const *old_value, gchar const *new_value,
137 bool is_interactive, gpointer data)
138 {
139 SPItem *item = NULL;
140 gboolean changed = FALSE;
142 g_assert(data);
143 ShapeEditor *sh = ((ShapeEditor *) data);
145 item = sh->get_item();
147 if ( ( (sh->has_nodepath())
148 && ( !strcmp(name, "d") // With paths, we only need to act if one of the path-affecting attributes has changed.
149 || !strcmp(name, "sodipodi:nodetypes")
150 || !strcmp(name, "inkscape:original-d") ) )
151 || sh->has_knotholder() )
152 {
153 changed = !sh->has_local_change();
154 sh->decrement_local_change();
155 }
157 if (changed) {
158 GList *saved = NULL;
159 if (sh->has_nodepath()) {
160 saved = sh->save_nodepath_selection();
161 }
163 sh->set_item (item);
165 if (sh->has_nodepath() && saved) {
166 sh->restore_nodepath_selection(saved);
167 g_list_free (saved);
168 }
169 }
171 sh->update_statusbar(); //TODO: sh->get_container()->update_statusbar();
172 }
174 static Inkscape::XML::NodeEventVector shapeeditor_repr_events = {
175 NULL, /* child_added */
176 NULL, /* child_removed */
177 shapeeditor_event_attr_changed,
178 NULL, /* content_changed */
179 NULL /* order_changed */
180 };
183 void ShapeEditor::set_item(SPItem *item) {
185 unset_item();
187 this->grab_node = -1;
189 if (item) {
190 this->nodepath = sp_nodepath_new(desktop, item, (prefs_get_int_attribute("tools.nodes", "show_handles", 1) != 0));
191 if (this->nodepath) {
192 this->nodepath->shape_editor = this;
193 }
194 this->knotholder = sp_item_knot_holder(item, desktop);
196 if (this->nodepath || this->knotholder) {
197 // setting new listener
198 Inkscape::XML::Node *repr;
199 if (this->knotholder)
200 repr = this->knotholder->repr;
201 else
202 repr = SP_OBJECT_REPR(item);
203 if (repr) {
204 Inkscape::GC::anchor(repr);
205 sp_repr_add_listener(repr, &shapeeditor_repr_events, this);
206 }
207 }
208 }
209 }
211 void ShapeEditor::set_livepatheffect_parameter(SPObject *lpeobject, const char * key) {
213 unset_item();
215 this->grab_node = -1;
217 if (lpeobject) {
218 this->nodepath = sp_nodepath_new( desktop, lpeobject,
219 (prefs_get_int_attribute("tools.nodes", "show_handles", 1) != 0),
220 key);
221 if (this->nodepath) {
222 this->nodepath->shape_editor = this;
223 }
224 //this->knotholder = sp_item_knot_holder(item, desktop);
225 g_message("create knotholder?");
227 if (this->nodepath || this->knotholder) {
228 // setting new listener
229 Inkscape::XML::Node *repr;
230 if (this->knotholder)
231 repr = this->knotholder->repr;
232 else
233 repr = SP_OBJECT_REPR(lpeobject);
234 if (repr) {
235 Inkscape::GC::anchor(repr);
236 sp_repr_add_listener(repr, &shapeeditor_repr_events, this);
237 }
238 }
239 }
240 }
242 void ShapeEditor::nodepath_destroyed () {
243 this->nodepath = NULL;
244 }
246 void ShapeEditor::update_statusbar () {
247 if (this->nodepath)
248 sp_nodepath_update_statusbar(this->nodepath);
249 }
251 bool ShapeEditor::is_over_stroke (NR::Point event_p, bool remember) {
253 if (!this->nodepath)
254 return false; // no stroke in knotholder
256 SPItem *item = get_item();
258 //Translate click point into proper coord system
259 this->curvepoint_doc = desktop->w2d(event_p);
260 this->curvepoint_doc *= sp_item_dt2i_affine(item);
262 sp_nodepath_ensure_livarot_path(this->nodepath);
264 NR::Maybe<Path::cut_position> position = get_nearest_position_on_Path(this->nodepath->livarot_path, this->curvepoint_doc);
265 if (!position) {
266 return false;
267 }
269 NR::Point nearest = get_point_on_Path(this->nodepath->livarot_path, position->piece, position->t);
270 NR::Point delta = nearest - this->curvepoint_doc;
272 delta = desktop->d2w(delta);
274 double stroke_tolerance =
275 ( !SP_OBJECT_STYLE(item)->stroke.isNone() ?
276 desktop->current_zoom() *
277 SP_OBJECT_STYLE (item)->stroke_width.computed *
278 sp_item_i2d_affine (item).expansion() * 0.5
279 : 0.0)
280 + prefs_get_int_attribute_limited("options.dragtolerance", "value", 0, 0, 100); //(double) SP_EVENT_CONTEXT(nc)->tolerance;
282 bool close = (NR::L2 (delta) < stroke_tolerance);
284 if (remember && close) {
285 this->curvepoint_event[NR::X] = (gint) event_p [NR::X];
286 this->curvepoint_event[NR::Y] = (gint) event_p [NR::Y];
287 this->hit = true;
288 this->grab_t = position->t;
289 this->grab_node = position->piece;
290 }
292 return close;
293 }
295 void ShapeEditor::add_node_near_point() {
296 if (this->nodepath) {
297 sp_nodepath_add_node_near_point(this->nodepath, this->curvepoint_doc);
298 } else if (this->knotholder) {
299 // we do not add nodes in knotholder... yet
300 }
301 }
303 void ShapeEditor::select_segment_near_point(bool toggle) {
304 if (this->nodepath) {
305 sp_nodepath_select_segment_near_point(this->nodepath, this->curvepoint_doc, toggle);
306 } else if (this->knotholder) {
307 // we do not select segments in knotholder... yet?
308 }
309 }
311 void ShapeEditor::cancel_hit() {
312 this->hit = false;
313 }
315 bool ShapeEditor::hits_curve() {
316 return (this->hit);
317 }
320 void ShapeEditor::curve_drag(gdouble eventx, gdouble eventy) {
321 if (this->nodepath && !this->nodepath->straight_path) {
323 if (this->grab_node == -1) // don't know which segment to drag
324 return;
326 // We round off the extra precision in the motion coordinates provided
327 // by some input devices (like tablets). As we'll store the coordinates
328 // as integers in curvepoint_event we need to do this rounding before
329 // comparing them with the last coordinates from curvepoint_event.
330 // See bug #1593499 for details.
332 gint x = (gint) Inkscape::round(eventx);
333 gint y = (gint) Inkscape::round(eventy);
336 // The coordinates hasn't changed since the last motion event, abort
337 if (this->curvepoint_event[NR::X] == x &&
338 this->curvepoint_event[NR::Y] == y)
339 return;
341 NR::Point const delta_w(eventx - this->curvepoint_event[NR::X],
342 eventy - this->curvepoint_event[NR::Y]);
343 NR::Point const delta_dt(this->desktop->w2d(delta_w));
345 sp_nodepath_curve_drag (this->grab_node, this->grab_t, delta_dt); //!!! FIXME: which nodepath?!!! also uses current!!!
346 this->curvepoint_event[NR::X] = x;
347 this->curvepoint_event[NR::Y] = y;
349 } else if (this->knotholder) {
350 // we do not drag curve in knotholder
351 }
353 }
355 void ShapeEditor::finish_drag() {
356 if (this->nodepath && this->hit) {
357 sp_nodepath_update_repr (this->nodepath, _("Drag curve"));
358 }
359 }
361 void ShapeEditor::select_rect(NR::Rect const &rect, bool add) {
362 if (this->nodepath) {
363 sp_nodepath_select_rect(this->nodepath, rect, add);
364 }
365 }
367 bool ShapeEditor::has_selection() {
368 if (this->nodepath)
369 return this->nodepath->selected;
370 return false; // so far, knotholder cannot have selection
371 }
373 void ShapeEditor::deselect() {
374 if (this->nodepath)
375 sp_nodepath_deselect(this->nodepath);
376 }
378 void ShapeEditor::add_node () {
379 sp_node_selected_add_node(this->nodepath);
380 }
382 void ShapeEditor::delete_nodes () {
383 sp_node_selected_delete(this->nodepath);
384 }
386 void ShapeEditor::delete_nodes_preserving_shape () {
387 if (this->nodepath && this->nodepath->selected) {
388 sp_node_delete_preserve(g_list_copy(this->nodepath->selected));
389 }
390 }
392 void ShapeEditor::delete_segment () {
393 sp_node_selected_delete_segment(this->nodepath);
394 }
396 void ShapeEditor::set_node_type(int type) {
397 sp_node_selected_set_type(this->nodepath, (Inkscape::NodePath::NodeType) type);
398 }
400 void ShapeEditor::break_at_nodes() {
401 sp_node_selected_break(this->nodepath);
402 }
404 void ShapeEditor::join_nodes() {
405 sp_node_selected_join(this->nodepath);
406 }
408 void ShapeEditor::join_segments() {
409 sp_node_selected_join_segment(this->nodepath);
410 }
412 void ShapeEditor::duplicate_nodes() {
413 sp_node_selected_duplicate(this->nodepath);
414 }
416 void ShapeEditor::set_type_of_segments(NRPathcode code) {
417 sp_node_selected_set_line_type(this->nodepath, code);
418 }
420 void ShapeEditor::move_nodes_screen(gdouble dx, gdouble dy) {
421 sp_node_selected_move_screen(this->nodepath, dx, dy);
422 }
423 void ShapeEditor::move_nodes(gdouble dx, gdouble dy) {
424 sp_node_selected_move(this->nodepath, dx, dy);
425 }
427 void ShapeEditor::rotate_nodes(gdouble angle, int which, bool screen) {
428 if (this->nodepath)
429 sp_nodepath_selected_nodes_rotate (this->nodepath, angle, which, screen);
430 }
432 void ShapeEditor::scale_nodes(gdouble const grow, int const which) {
433 sp_nodepath_selected_nodes_scale(this->nodepath, grow, which);
434 }
435 void ShapeEditor::scale_nodes_screen(gdouble const grow, int const which) {
436 sp_nodepath_selected_nodes_scale_screen(this->nodepath, grow, which);
437 }
439 void ShapeEditor::select_all (bool invert) {
440 if (this->nodepath)
441 sp_nodepath_select_all (this->nodepath, invert);
442 }
443 void ShapeEditor::select_all_from_subpath (bool invert) {
444 if (this->nodepath)
445 sp_nodepath_select_all_from_subpath (this->nodepath, invert);
446 }
447 void ShapeEditor::select_next () {
448 if (this->nodepath) {
449 sp_nodepath_select_next (this->nodepath);
450 if (this->nodepath->numSelected() >= 1) {
451 this->desktop->scroll_to_point(&(this->nodepath->singleSelectedCoords()), 1.0);
452 }
453 }
454 }
455 void ShapeEditor::select_prev () {
456 if (this->nodepath) {
457 sp_nodepath_select_prev (this->nodepath);
458 if (this->nodepath->numSelected() >= 1) {
459 this->desktop->scroll_to_point(&(this->nodepath->singleSelectedCoords()), 1.0);
460 }
461 }
462 }
464 void ShapeEditor::show_handles (bool show) {
465 if (this->nodepath)
466 sp_nodepath_show_handles (this->nodepath, show);
467 }
470 void ShapeEditor::flip (NR::Dim2 axis, NR::Maybe<NR::Point> center) {
471 if (this->nodepath)
472 sp_nodepath_flip (this->nodepath, axis, center);
473 }
475 void ShapeEditor::distribute (NR::Dim2 axis) {
476 if (this->nodepath)
477 sp_nodepath_selected_distribute (this->nodepath, axis);
478 }
479 void ShapeEditor::align (NR::Dim2 axis) {
480 if (this->nodepath)
481 sp_nodepath_selected_align (this->nodepath, axis);
482 }
485 /*
486 Local Variables:
487 mode:c++
488 c-file-style:"stroustrup"
489 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
490 indent-tabs-mode:nil
491 fill-column:99
492 End:
493 */
494 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :