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"
24
#include "desktop-handles.h"
25
#include "knotholder.h"
26
#include "live_effects/parameter/point.h"
28
#include "xml/node-event-vector.h"
29
#include "preferences.h"
30
#include "object-edit.h"
32
#include "display/curve.h"
33
#include <2geom/pathvector.h>
35
#include "shape-editor.h"
38
ShapeEditorsCollective::ShapeEditorsCollective(SPDesktop */*dt*/) {
41
ShapeEditorsCollective::~ShapeEditorsCollective() {
45
void ShapeEditorsCollective::update_statusbar() {
47
//!!! move from nodepath: sp_nodepath_update_statusbar but summing for all nodepaths
51
ShapeEditor::ShapeEditor(SPDesktop *dt) {
54
this->nodepath = NULL;
55
this->knotholder = NULL;
57
this->listener_attached_for = NULL;
60
ShapeEditor::~ShapeEditor() {
61
unset_item(SH_KNOTHOLDER);
62
unset_item(SH_NODEPATH);
65
void ShapeEditor::unset_item(SubType type, bool keep_knotholder) {
66
Inkscape::XML::Node *old_repr = NULL;
71
old_repr = this->nodepath->repr;
72
if (old_repr && old_repr == listener_attached_for) {
73
sp_repr_remove_listener_by_data(old_repr, this);
74
Inkscape::GC::release(old_repr);
75
listener_attached_for = NULL;
79
delete this->nodepath;
80
this->nodepath = NULL;
84
if (this->knotholder) {
85
old_repr = this->knotholder->repr;
86
if (old_repr && old_repr == listener_attached_for) {
87
sp_repr_remove_listener_by_data(old_repr, this);
88
Inkscape::GC::release(old_repr);
89
listener_attached_for = NULL;
92
if (!keep_knotholder) {
93
delete this->knotholder;
94
this->knotholder = NULL;
101
bool ShapeEditor::has_nodepath () {
102
return (this->nodepath != NULL);
105
bool ShapeEditor::has_knotholder () {
106
return (this->knotholder != NULL);
109
void ShapeEditor::update_knotholder () {
110
if (this->knotholder)
111
this->knotholder->update_knots();
114
bool ShapeEditor::has_local_change (SubType type) {
117
return (this->nodepath && this->nodepath->local_change);
119
return (this->knotholder && this->knotholder->local_change != 0);
121
g_assert_not_reached();
125
void ShapeEditor::decrement_local_change (SubType type) {
128
if (this->nodepath && this->nodepath->local_change > 0) {
129
this->nodepath->local_change--;
133
if (this->knotholder) {
134
this->knotholder->local_change = FALSE;
138
g_assert_not_reached();
142
const SPItem *ShapeEditor::get_item (SubType type) {
143
const SPItem *item = NULL;
146
if (this->has_nodepath()) {
147
item = this->nodepath->item;
151
if (this->has_knotholder()) {
152
item = this->knotholder->getItem();
159
GList *ShapeEditor::save_nodepath_selection () {
161
return ::save_nodepath_selection (this->nodepath);
165
void ShapeEditor::restore_nodepath_selection (GList *saved) {
166
if (this->nodepath && saved)
167
::restore_nodepath_selection (this->nodepath, saved);
170
bool ShapeEditor::nodepath_edits_repr_key(gchar const *name) {
171
if (nodepath && name) {
172
return ( !strcmp(name, nodepath->repr_key) || !strcmp(name, nodepath->repr_nodetypes_key) );
179
void ShapeEditor::shapeeditor_event_attr_changed(gchar const *name)
181
gboolean changed_np = FALSE;
182
gboolean changed_kh = FALSE;
184
if (has_nodepath() && nodepath_edits_repr_key(name))
186
changed_np = !has_local_change(SH_NODEPATH);
187
decrement_local_change(SH_NODEPATH);
192
if (has_nodepath()) {
193
saved = save_nodepath_selection();
196
reset_item(SH_NODEPATH);
198
if (has_nodepath() && saved) {
199
restore_nodepath_selection(saved);
204
if (has_knotholder())
206
changed_kh = !has_local_change(SH_KNOTHOLDER);
207
decrement_local_change(SH_KNOTHOLDER);
209
// this can happen if an LPEItem's knotholder handle was dragged, in which case we want
210
// to keep the knotholder; in all other cases (e.g., if the LPE itself changes) we delete it
211
reset_item(SH_KNOTHOLDER, !strcmp(name, "d"));
215
update_statusbar(); //TODO: get_container()->update_statusbar();
219
static void shapeeditor_event_attr_changed(Inkscape::XML::Node */*repr*/, gchar const *name,
220
gchar const */*old_value*/, gchar const */*new_value*/,
221
bool /*is_interactive*/, gpointer data)
224
ShapeEditor *sh = ((ShapeEditor *) data);
226
sh->shapeeditor_event_attr_changed(name);
229
static Inkscape::XML::NodeEventVector shapeeditor_repr_events = {
230
NULL, /* child_added */
231
NULL, /* child_removed */
232
shapeeditor_event_attr_changed,
233
NULL, /* content_changed */
234
NULL /* order_changed */
238
void ShapeEditor::set_item(SPItem *item, SubType type, bool keep_knotholder) {
239
// this happens (and should only happen) when for an LPEItem having both knotholder and
240
// nodepath the knotholder is adapted; in this case we don't want to delete the knotholder
241
// since this freezes the handles
242
unset_item(type, keep_knotholder);
244
this->grab_node = -1;
247
Inkscape::XML::Node *repr;
250
if (SP_IS_LPE_ITEM(item)) {
251
Inkscape::Preferences *prefs = Inkscape::Preferences::get();
252
this->nodepath = sp_nodepath_new(desktop, item, (prefs->getBool("/tools/nodes/show_handles", true)));
254
if (this->nodepath) {
255
this->nodepath->shape_editor = this;
257
// setting new listener
258
repr = SP_OBJECT_REPR(item);
259
if (repr != listener_attached_for) {
260
Inkscape::GC::anchor(repr);
261
sp_repr_add_listener(repr, &shapeeditor_repr_events, this);
262
listener_attached_for = repr;
268
if (!this->knotholder) {
269
// only recreate knotholder if none is present
270
this->knotholder = sp_item_knot_holder(item, desktop);
272
if (this->knotholder) {
273
this->knotholder->update_knots();
274
// setting new listener
275
repr = this->knotholder->repr;
276
if (repr != listener_attached_for) {
277
Inkscape::GC::anchor(repr);
278
sp_repr_add_listener(repr, &shapeeditor_repr_events, this);
279
listener_attached_for = repr;
287
/** Please note that this function only works for path parameters.
288
* All other parameters probably will crash Inkscape!
290
void ShapeEditor::set_item_lpe_path_parameter(SPItem *item, LivePathEffectObject *lpeobject, const char * key)
292
unset_item(SH_NODEPATH);
294
this->grab_node = -1;
297
Inkscape::Preferences *prefs = Inkscape::Preferences::get();
298
this->nodepath = sp_nodepath_new( desktop, lpeobject,
299
(prefs->getInt("/tools/nodes/show_handles", true)),
301
if (this->nodepath) {
302
this->nodepath->shape_editor = this;
304
// setting new listener
305
Inkscape::XML::Node *repr = SP_OBJECT_REPR(lpeobject);
306
if (repr && repr != listener_attached_for) {
307
Inkscape::GC::anchor(repr);
308
sp_repr_add_listener(repr, &shapeeditor_repr_events, this);
309
listener_attached_for = repr;
316
/** FIXME: This thing is only called when the item needs to be updated in response to repr change.
317
Why not make a reload function in NodePath and in KnotHolder? */
318
void ShapeEditor::reset_item (SubType type, bool keep_knotholder)
320
/// note that it is not certain that this is an SPItem; it could be a LivePathEffectObject.
321
SPObject *obj = sp_desktop_document(desktop)->getObjectByRepr(listener_attached_for);
325
if ( (nodepath) && (IS_LIVEPATHEFFECT(nodepath->object)) ) {
326
char * key = g_strdup(nodepath->repr_key);
327
set_item_lpe_path_parameter(nodepath->item, LIVEPATHEFFECT(nodepath->object), key); // the above checks for nodepath, so it is indeed a path that we are editing
330
set_item(SP_ITEM(obj), SH_NODEPATH);
334
if (this->knotholder) {
335
set_item(SP_ITEM(obj), SH_KNOTHOLDER, keep_knotholder);
341
void ShapeEditor::nodepath_destroyed () {
342
this->nodepath = NULL;
345
void ShapeEditor::update_statusbar () {
347
sp_nodepath_update_statusbar(this->nodepath);
350
bool ShapeEditor::is_over_stroke (Geom::Point event_p, bool remember) {
352
return false; // no stroke in knotholder
354
const SPItem *item = get_item(SH_NODEPATH);
356
//Translate click point into proper coord system
357
this->curvepoint_doc = desktop->w2d(event_p);
358
this->curvepoint_doc *= sp_item_dt2i_affine(item);
360
SPCurve *curve = this->nodepath->curve; // not sure if np->curve is always up to date...
361
Geom::PathVector const &pathv = curve->get_pathvector();
362
boost::optional<Geom::PathVectorPosition> pvpos = Geom::nearestPoint(pathv, this->curvepoint_doc);
364
g_print("Warning! Possible error?\n");
368
Geom::Point nearest = pathv[pvpos->path_nr].pointAt(pvpos->t);
369
Geom::Point delta = nearest - this->curvepoint_doc;
371
delta = desktop->d2w(delta);
373
Inkscape::Preferences *prefs = Inkscape::Preferences::get();
374
double stroke_tolerance =
375
(( !SP_OBJECT_STYLE(item)->stroke.isNone() ?
376
desktop->current_zoom() *
377
SP_OBJECT_STYLE (item)->stroke_width.computed * 0.5 *
378
to_2geom(sp_item_i2d_affine(item)).descrim()
380
+ prefs->getIntLimited("/options/dragtolerance/value", 0, 0, 100)) / to_2geom(sp_item_i2d_affine(item)).descrim();
381
bool close = (Geom::L2 (delta) < stroke_tolerance);
383
if (remember && close) {
384
// calculate index for nodepath's representation.
386
double t = std::modf(pvpos->t, &int_part);
387
unsigned int segment_index = (unsigned int)int_part + 1;
388
for (unsigned int i = 0; i < pvpos->path_nr; ++i) {
389
segment_index += pathv[i].size() + 1;
390
if (pathv[i].closed())
394
this->curvepoint_event[Geom::X] = (gint) event_p [Geom::X];
395
this->curvepoint_event[Geom::Y] = (gint) event_p [Geom::Y];
398
this->grab_node = segment_index;
404
void ShapeEditor::add_node_near_point() {
405
if (this->nodepath) {
406
sp_nodepath_add_node_near_point(this->nodepath, this->curvepoint_doc);
407
} else if (this->knotholder) {
408
// we do not add nodes in knotholder... yet
412
void ShapeEditor::select_segment_near_point(bool toggle) {
413
if (this->nodepath) {
414
sp_nodepath_select_segment_near_point(this->nodepath, this->curvepoint_doc, toggle);
416
if (this->knotholder) {
417
// we do not select segments in knotholder... yet?
421
void ShapeEditor::cancel_hit() {
425
bool ShapeEditor::hits_curve() {
430
void ShapeEditor::curve_drag(gdouble eventx, gdouble eventy) {
431
if (this->nodepath && !this->nodepath->straight_path) {
433
if (this->grab_node == -1) // don't know which segment to drag
436
// We round off the extra precision in the motion coordinates provided
437
// by some input devices (like tablets). As we'll store the coordinates
438
// as integers in curvepoint_event we need to do this rounding before
439
// comparing them with the last coordinates from curvepoint_event.
440
// See bug #1593499 for details.
442
gint x = (gint) Inkscape::round(eventx);
443
gint y = (gint) Inkscape::round(eventy);
446
// The coordinates hasn't changed since the last motion event, abort
447
if (this->curvepoint_event[Geom::X] == x &&
448
this->curvepoint_event[Geom::Y] == y)
451
Geom::Point const delta_w(eventx - this->curvepoint_event[Geom::X],
452
eventy - this->curvepoint_event[Geom::Y]);
453
Geom::Point const delta_dt(this->desktop->w2d(delta_w));
455
sp_nodepath_curve_drag (this->nodepath, this->grab_node, this->grab_t, delta_dt);
456
this->curvepoint_event[Geom::X] = x;
457
this->curvepoint_event[Geom::Y] = y;
460
if (this->knotholder) {
461
// we do not drag curve in knotholder
466
void ShapeEditor::finish_drag() {
467
if (this->nodepath && this->hit) {
468
sp_nodepath_update_repr (this->nodepath, _("Drag curve"));
472
void ShapeEditor::select_rect(Geom::Rect const &rect, bool add) {
473
if (this->nodepath) {
474
sp_nodepath_select_rect(this->nodepath, rect, add);
478
bool ShapeEditor::has_selection() {
480
return this->nodepath->selected;
481
return false; // so far, knotholder cannot have selection
484
void ShapeEditor::deselect() {
486
sp_nodepath_deselect(this->nodepath);
489
void ShapeEditor::add_node () {
490
sp_node_selected_add_node(this->nodepath);
493
void ShapeEditor::delete_nodes () {
494
sp_node_selected_delete(this->nodepath);
497
void ShapeEditor::delete_nodes_preserving_shape () {
498
if (this->nodepath && this->nodepath->selected) {
499
sp_node_delete_preserve(g_list_copy(this->nodepath->selected));
503
void ShapeEditor::delete_segment () {
504
sp_node_selected_delete_segment(this->nodepath);
507
void ShapeEditor::set_node_type(int type) {
508
sp_node_selected_set_type(this->nodepath, (Inkscape::NodePath::NodeType) type);
511
void ShapeEditor::break_at_nodes() {
512
sp_node_selected_break(this->nodepath);
515
void ShapeEditor::join_nodes() {
516
sp_node_selected_join(this->nodepath);
519
void ShapeEditor::join_segments() {
520
sp_node_selected_join_segment(this->nodepath);
523
void ShapeEditor::duplicate_nodes() {
524
sp_node_selected_duplicate(this->nodepath);
527
void ShapeEditor::set_type_of_segments(NRPathcode code) {
528
sp_node_selected_set_line_type(this->nodepath, code);
531
void ShapeEditor::move_nodes_screen(SPDesktop *desktop, gdouble dx, gdouble dy) {
532
sp_node_selected_move_screen(desktop, this->nodepath, dx, dy);
534
void ShapeEditor::move_nodes(gdouble dx, gdouble dy) {
535
sp_node_selected_move(this->nodepath, dx, dy);
538
void ShapeEditor::rotate_nodes(gdouble angle, int which, bool screen) {
540
sp_nodepath_selected_nodes_rotate (this->nodepath, angle, which, screen);
543
void ShapeEditor::scale_nodes(gdouble const grow, int const which) {
544
sp_nodepath_selected_nodes_scale(this->nodepath, grow, which);
546
void ShapeEditor::scale_nodes_screen(gdouble const grow, int const which) {
547
sp_nodepath_selected_nodes_scale_screen(this->nodepath, grow, which);
550
void ShapeEditor::select_all (bool invert) {
552
sp_nodepath_select_all (this->nodepath, invert);
554
void ShapeEditor::select_all_from_subpath (bool invert) {
556
sp_nodepath_select_all_from_subpath (this->nodepath, invert);
558
void ShapeEditor::select_next () {
559
if (this->nodepath) {
560
sp_nodepath_select_next (this->nodepath);
561
if (this->nodepath->numSelected() >= 1) {
562
this->desktop->scroll_to_point(this->nodepath->singleSelectedCoords(), 1.0);
566
void ShapeEditor::select_prev () {
567
if (this->nodepath) {
568
sp_nodepath_select_prev (this->nodepath);
569
if (this->nodepath->numSelected() >= 1) {
570
this->desktop->scroll_to_point(this->nodepath->singleSelectedCoords(), 1.0);
575
void ShapeEditor::show_handles (bool show) {
576
if (this->nodepath && !this->nodepath->straight_path)
577
sp_nodepath_show_handles (this->nodepath, show);
580
void ShapeEditor::show_helperpath (bool show) {
582
sp_nodepath_show_helperpath (this->nodepath, show);
585
void ShapeEditor::flip (Geom::Dim2 axis, boost::optional<Geom::Point> center) {
587
sp_nodepath_flip (this->nodepath, axis, center);
590
void ShapeEditor::distribute (Geom::Dim2 axis) {
592
sp_nodepath_selected_distribute (this->nodepath, axis);
594
void ShapeEditor::align (Geom::Dim2 axis) {
596
sp_nodepath_selected_align (this->nodepath, axis);
603
c-file-style:"stroustrup"
604
c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
609
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :