1
#define __SHAPE_EDITOR_CPP__
4
* Inkscape::ShapeEditor
7
* bulia byak <buliabyak@users.sf.net>
16
#include <glibmm/i18n.h>
18
#include "sp-object.h"
20
#include "live_effects/lpeobject.h"
21
#include "selection.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"
32
#include "shape-editor.h"
35
ShapeEditorsCollective::ShapeEditorsCollective(SPDesktop */*dt*/) {
38
ShapeEditorsCollective::~ShapeEditorsCollective() {
42
void ShapeEditorsCollective::update_statusbar() {
44
//!!! move from nodepath: sp_nodepath_update_statusbar but summing for all nodepaths
48
ShapeEditor::ShapeEditor(SPDesktop *dt) {
51
this->nodepath = NULL;
52
this->knotholder = NULL;
56
ShapeEditor::~ShapeEditor() {
60
void ShapeEditor::unset_item() {
62
Inkscape::XML::Node *old_repr = NULL;
65
old_repr = this->nodepath->repr;
68
if (!old_repr && this->knotholder) {
69
old_repr = this->knotholder->repr;
72
if (old_repr) { // remove old listener
73
sp_repr_remove_listener_by_data(old_repr, this);
74
Inkscape::GC::release(old_repr);
79
sp_nodepath_destroy(this->nodepath);
80
this->nodepath = NULL;
83
if (this->knotholder) {
84
sp_knot_holder_destroy(this->knotholder);
85
this->knotholder = NULL;
89
bool ShapeEditor::has_nodepath () {
90
return (this->nodepath != NULL);
93
bool ShapeEditor::has_knotholder () {
94
return (this->knotholder != NULL);
97
bool ShapeEditor::has_local_change () {
99
return (this->nodepath->local_change != 0);
100
else if (this->knotholder)
101
return (this->knotholder->local_change != 0);
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;
115
SPItem *ShapeEditor::get_item () {
117
if (this->has_nodepath()) {
118
item = this->nodepath->item;
119
} else if (this->has_knotholder()) {
120
item = SP_ITEM(this->knotholder->item);
125
GList *ShapeEditor::save_nodepath_selection () {
127
return ::save_nodepath_selection (this->nodepath);
131
void ShapeEditor::restore_nodepath_selection (GList *saved) {
132
if (this->nodepath && saved)
133
::restore_nodepath_selection (this->nodepath, saved);
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) );
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)
148
gboolean changed = FALSE;
151
ShapeEditor *sh = ((ShapeEditor *) data);
153
if ( sh->has_knotholder() || ( sh->has_nodepath() && sh->nodepath_edits_repr_key(name) ) )
155
changed = !sh->has_local_change();
156
sh->decrement_local_change();
161
if (sh->has_nodepath()) {
162
saved = sh->save_nodepath_selection();
167
if (sh->has_nodepath() && saved) {
168
sh->restore_nodepath_selection(saved);
173
sh->update_statusbar(); //TODO: sh->get_container()->update_statusbar();
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 */
185
void ShapeEditor::set_item(SPItem *item) {
189
this->grab_node = -1;
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;
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;
204
repr = SP_OBJECT_REPR(item);
206
Inkscape::GC::anchor(repr);
207
sp_repr_add_listener(repr, &shapeeditor_repr_events, this);
213
/** Please note that this function only works for path parameters.
214
* All other parameters probably will crash Inkscape!
215
* Fortunately, there are no other on-canvas edittable objects at this moment :)
217
void ShapeEditor::set_item_livepatheffect_parameter(SPItem *item, SPObject *lpeobject, const char * key) {
221
this->grab_node = -1;
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),
228
if (this->nodepath) {
229
this->nodepath->shape_editor = this;
231
// setting new listener
232
Inkscape::XML::Node *repr = SP_OBJECT_REPR(lpeobject);
234
Inkscape::GC::anchor(repr);
235
sp_repr_add_listener(repr, &shapeeditor_repr_events, this);
241
/** FIXME: think about this. Is this thing only called when the item needs to be updated?
242
Why not make a reload function in NodePath and in KnotHolder? */
243
void ShapeEditor::reset_item ()
245
if ( (this->nodepath) && (IS_LIVEPATHEFFECT(this->nodepath->object)) ) {
246
SPItem * item = this->nodepath->item;
247
SPObject *obj = this->nodepath->object;
248
char * key = g_strdup(this->nodepath->repr_key);
249
set_item_livepatheffect_parameter(item, obj, key);
252
SPItem * item = get_item();
257
void ShapeEditor::nodepath_destroyed () {
258
this->nodepath = NULL;
261
void ShapeEditor::update_statusbar () {
263
sp_nodepath_update_statusbar(this->nodepath);
266
bool ShapeEditor::is_over_stroke (NR::Point event_p, bool remember) {
269
return false; // no stroke in knotholder
271
SPItem *item = get_item();
273
//Translate click point into proper coord system
274
this->curvepoint_doc = desktop->w2d(event_p);
275
this->curvepoint_doc *= sp_item_dt2i_affine(item);
277
sp_nodepath_ensure_livarot_path(this->nodepath);
279
NR::Maybe<Path::cut_position> position = get_nearest_position_on_Path(this->nodepath->livarot_path, this->curvepoint_doc);
284
NR::Point nearest = get_point_on_Path(this->nodepath->livarot_path, position->piece, position->t);
285
NR::Point delta = nearest - this->curvepoint_doc;
287
delta = desktop->d2w(delta);
289
double stroke_tolerance =
290
(( !SP_OBJECT_STYLE(item)->stroke.isNone() ?
291
desktop->current_zoom() *
292
SP_OBJECT_STYLE (item)->stroke_width.computed * 0.5 *
293
sp_item_i2d_affine (item).expansion()
295
+ prefs_get_int_attribute_limited("options.dragtolerance", "value", 0, 0, 100)) /sp_item_i2d_affine (item).expansion();
296
bool close = (NR::L2 (delta) < stroke_tolerance);
298
if (remember && close) {
299
this->curvepoint_event[NR::X] = (gint) event_p [NR::X];
300
this->curvepoint_event[NR::Y] = (gint) event_p [NR::Y];
302
this->grab_t = position->t;
303
this->grab_node = position->piece;
309
void ShapeEditor::add_node_near_point() {
310
if (this->nodepath) {
311
sp_nodepath_add_node_near_point(this->nodepath, this->curvepoint_doc);
312
} else if (this->knotholder) {
313
// we do not add nodes in knotholder... yet
317
void ShapeEditor::select_segment_near_point(bool toggle) {
318
if (this->nodepath) {
319
sp_nodepath_select_segment_near_point(this->nodepath, this->curvepoint_doc, toggle);
320
} else if (this->knotholder) {
321
// we do not select segments in knotholder... yet?
325
void ShapeEditor::cancel_hit() {
329
bool ShapeEditor::hits_curve() {
334
void ShapeEditor::curve_drag(gdouble eventx, gdouble eventy) {
335
if (this->nodepath && !this->nodepath->straight_path) {
337
if (this->grab_node == -1) // don't know which segment to drag
340
// We round off the extra precision in the motion coordinates provided
341
// by some input devices (like tablets). As we'll store the coordinates
342
// as integers in curvepoint_event we need to do this rounding before
343
// comparing them with the last coordinates from curvepoint_event.
344
// See bug #1593499 for details.
346
gint x = (gint) Inkscape::round(eventx);
347
gint y = (gint) Inkscape::round(eventy);
350
// The coordinates hasn't changed since the last motion event, abort
351
if (this->curvepoint_event[NR::X] == x &&
352
this->curvepoint_event[NR::Y] == y)
355
NR::Point const delta_w(eventx - this->curvepoint_event[NR::X],
356
eventy - this->curvepoint_event[NR::Y]);
357
NR::Point const delta_dt(this->desktop->w2d(delta_w));
359
sp_nodepath_curve_drag (this->grab_node, this->grab_t, delta_dt); //!!! FIXME: which nodepath?!!! also uses current!!!
360
this->curvepoint_event[NR::X] = x;
361
this->curvepoint_event[NR::Y] = y;
363
} else if (this->knotholder) {
364
// we do not drag curve in knotholder
369
void ShapeEditor::finish_drag() {
370
if (this->nodepath && this->hit) {
371
sp_nodepath_update_repr (this->nodepath, _("Drag curve"));
375
void ShapeEditor::select_rect(NR::Rect const &rect, bool add) {
376
if (this->nodepath) {
377
sp_nodepath_select_rect(this->nodepath, rect, add);
381
bool ShapeEditor::has_selection() {
383
return this->nodepath->selected;
384
return false; // so far, knotholder cannot have selection
387
void ShapeEditor::deselect() {
389
sp_nodepath_deselect(this->nodepath);
392
void ShapeEditor::add_node () {
393
sp_node_selected_add_node(this->nodepath);
396
void ShapeEditor::delete_nodes () {
397
sp_node_selected_delete(this->nodepath);
400
void ShapeEditor::delete_nodes_preserving_shape () {
401
if (this->nodepath && this->nodepath->selected) {
402
sp_node_delete_preserve(g_list_copy(this->nodepath->selected));
406
void ShapeEditor::delete_segment () {
407
sp_node_selected_delete_segment(this->nodepath);
410
void ShapeEditor::set_node_type(int type) {
411
sp_node_selected_set_type(this->nodepath, (Inkscape::NodePath::NodeType) type);
414
void ShapeEditor::break_at_nodes() {
415
sp_node_selected_break(this->nodepath);
418
void ShapeEditor::join_nodes() {
419
sp_node_selected_join(this->nodepath);
422
void ShapeEditor::join_segments() {
423
sp_node_selected_join_segment(this->nodepath);
426
void ShapeEditor::duplicate_nodes() {
427
sp_node_selected_duplicate(this->nodepath);
430
void ShapeEditor::set_type_of_segments(NRPathcode code) {
431
sp_node_selected_set_line_type(this->nodepath, code);
434
void ShapeEditor::move_nodes_screen(gdouble dx, gdouble dy) {
435
sp_node_selected_move_screen(this->nodepath, dx, dy);
437
void ShapeEditor::move_nodes(gdouble dx, gdouble dy) {
438
sp_node_selected_move(this->nodepath, dx, dy);
441
void ShapeEditor::rotate_nodes(gdouble angle, int which, bool screen) {
443
sp_nodepath_selected_nodes_rotate (this->nodepath, angle, which, screen);
446
void ShapeEditor::scale_nodes(gdouble const grow, int const which) {
447
sp_nodepath_selected_nodes_scale(this->nodepath, grow, which);
449
void ShapeEditor::scale_nodes_screen(gdouble const grow, int const which) {
450
sp_nodepath_selected_nodes_scale_screen(this->nodepath, grow, which);
453
void ShapeEditor::select_all (bool invert) {
455
sp_nodepath_select_all (this->nodepath, invert);
457
void ShapeEditor::select_all_from_subpath (bool invert) {
459
sp_nodepath_select_all_from_subpath (this->nodepath, invert);
461
void ShapeEditor::select_next () {
462
if (this->nodepath) {
463
sp_nodepath_select_next (this->nodepath);
464
if (this->nodepath->numSelected() >= 1) {
465
this->desktop->scroll_to_point(&(this->nodepath->singleSelectedCoords()), 1.0);
469
void ShapeEditor::select_prev () {
470
if (this->nodepath) {
471
sp_nodepath_select_prev (this->nodepath);
472
if (this->nodepath->numSelected() >= 1) {
473
this->desktop->scroll_to_point(&(this->nodepath->singleSelectedCoords()), 1.0);
478
void ShapeEditor::show_handles (bool show) {
479
if (this->nodepath && !this->nodepath->straight_path)
480
sp_nodepath_show_handles (this->nodepath, show);
484
void ShapeEditor::flip (NR::Dim2 axis, NR::Maybe<NR::Point> center) {
486
sp_nodepath_flip (this->nodepath, axis, center);
489
void ShapeEditor::distribute (NR::Dim2 axis) {
491
sp_nodepath_selected_distribute (this->nodepath, axis);
493
void ShapeEditor::align (NR::Dim2 axis) {
495
sp_nodepath_selected_align (this->nodepath, axis);
502
c-file-style:"stroustrup"
503
c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
508
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :