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