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
|
#define __SP_LPE_ITEM_CPP__
/** \file
* Base class for live path effect items
*/
/*
* Authors:
* Johan Engelen <j.b.c.engelen@ewi.utwente.nl>
* Bastien Bouclet <bgkweb@gmail.com>
*
* Copyright (C) 2008 authors
*
* Released under GNU GPL, read the file 'COPYING' for more information
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include "live_effects/effect.h"
#include "live_effects/lpeobject.h"
#include "live_effects/lpeobject-reference.h"
#include "sp-path.h"
#include "sp-item-group.h"
#include "streq.h"
#include "macros.h"
#include "attributes.h"
#include "sp-lpe-item.h"
#include "xml/repr.h"
#include "uri.h"
/* LPEItem base class */
static void sp_lpe_item_class_init(SPLPEItemClass *klass);
static void sp_lpe_item_init(SPLPEItem *lpe_item);
static void sp_lpe_item_finalize(GObject *object);
static void sp_lpe_item_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
static void sp_lpe_item_release(SPObject *object);
static void sp_lpe_item_set(SPObject *object, unsigned int key, gchar const *value);
static void sp_lpe_item_update(SPObject *object, SPCtx *ctx, guint flags);
static void sp_lpe_item_modified (SPObject *object, unsigned int flags);
static Inkscape::XML::Node *sp_lpe_item_write(SPObject *object, Inkscape::XML::Node *repr, guint flags);
static void sp_lpe_item_child_added (SPObject * object, Inkscape::XML::Node * child, Inkscape::XML::Node * ref);
static void sp_lpe_item_remove_child (SPObject * object, Inkscape::XML::Node * child);
static void lpeobject_ref_changed(SPObject *old_ref, SPObject *ref, SPLPEItem *lpeitem);
static void lpeobject_ref_modified(SPObject *href, guint flags, SPLPEItem *lpeitem);
static void sp_lpe_item_create_original_path_recursive(SPLPEItem *lpeitem);
static void sp_lpe_item_cleanup_original_path_recursive(SPLPEItem *lpeitem);
static SPItemClass *parent_class;
GType
sp_lpe_item_get_type()
{
static GType lpe_item_type = 0;
if (!lpe_item_type) {
GTypeInfo lpe_item_info = {
sizeof(SPLPEItemClass),
NULL, NULL,
(GClassInitFunc) sp_lpe_item_class_init,
NULL, NULL,
sizeof(SPLPEItem),
16,
(GInstanceInitFunc) sp_lpe_item_init,
NULL, /* value_table */
};
lpe_item_type = g_type_register_static(SP_TYPE_ITEM, "SPLPEItem", &lpe_item_info, (GTypeFlags)0);
}
return lpe_item_type;
}
static void
sp_lpe_item_class_init(SPLPEItemClass *klass)
{
GObjectClass *gobject_class;
SPObjectClass *sp_object_class;
gobject_class = (GObjectClass *) klass;
sp_object_class = (SPObjectClass *) klass;
parent_class = (SPItemClass *)g_type_class_peek_parent (klass);
gobject_class->finalize = sp_lpe_item_finalize;
sp_object_class->build = sp_lpe_item_build;
sp_object_class->release = sp_lpe_item_release;
sp_object_class->set = sp_lpe_item_set;
sp_object_class->update = sp_lpe_item_update;
sp_object_class->modified = sp_lpe_item_modified;
sp_object_class->write = sp_lpe_item_write;
sp_object_class->child_added = sp_lpe_item_child_added;
sp_object_class->remove_child = sp_lpe_item_remove_child;
klass->update_patheffect = NULL;
}
static void
sp_lpe_item_init(SPLPEItem *lpeitem)
{
lpeitem->path_effect_ref = new Inkscape::LivePathEffect::LPEObjectReference(SP_OBJECT(lpeitem));
new (&lpeitem->lpe_modified_connection) sigc::connection();
}
static void
sp_lpe_item_finalize(GObject *object)
{
if (((GObjectClass *) (parent_class))->finalize) {
(* ((GObjectClass *) (parent_class))->finalize)(object);
}
}
/**
* Reads the Inkscape::XML::Node, and initializes SPLPEItem variables. For this to get called,
* our name must be associated with a repr via "sp_object_type_register". Best done through
* sp-object-repr.cpp's repr_name_entries array.
*/
static void
sp_lpe_item_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
{
SP_LPE_ITEM(object)->path_effect_ref->changedSignal().connect(sigc::bind(sigc::ptr_fun(lpeobject_ref_changed), SP_LPE_ITEM(object)));
sp_object_read_attr(object, "inkscape:path-effect");
if (((SPObjectClass *) parent_class)->build) {
((SPObjectClass *) parent_class)->build(object, document, repr);
}
}
/**
* Drops any allocated memory.
*/
static void
sp_lpe_item_release(SPObject *object)
{
SPLPEItem *lpeitem;
lpeitem = (SPLPEItem *) object;
lpeitem->path_effect_ref->detach();
lpeitem->lpe_modified_connection.disconnect();
lpeitem->lpe_modified_connection.~connection();
if (((SPObjectClass *) parent_class)->release)
((SPObjectClass *) parent_class)->release(object);
}
/**
* Sets a specific value in the SPLPEItem.
*/
static void
sp_lpe_item_set(SPObject *object, unsigned int key, gchar const *value)
{
SPLPEItem *lpeitem = (SPLPEItem *) object;
switch (key) {
case SP_ATTR_INKSCAPE_PATH_EFFECT:
if ( value && lpeitem->path_effect_ref->lpeobject_href
&& streq(value, lpeitem->path_effect_ref->lpeobject_href) ) {
/* No change, do nothing. */
} else {
if (value) {
// Now do the attaching, which emits the changed signal.
try {
lpeitem->path_effect_ref->link((gchar*)value);
} catch (Inkscape::BadURIException &e) {
g_warning("%s", e.what());
lpeitem->path_effect_ref->detach();
}
} else {
// Detach, which emits the changed signal.
lpeitem->path_effect_ref->detach();
}
}
break;
default:
if (((SPObjectClass *) parent_class)->set) {
((SPObjectClass *) parent_class)->set(object, key, value);
}
break;
}
}
/**
* Receives update notifications.
*/
static void
sp_lpe_item_update(SPObject *object, SPCtx *ctx, guint flags)
{
if (((SPObjectClass *) parent_class)->update) {
((SPObjectClass *) parent_class)->update(object, ctx, flags);
}
}
/**
* Sets modified flag for all sub-item views.
*/
static void
sp_lpe_item_modified (SPObject *object, unsigned int flags)
{
if (((SPObjectClass *) (parent_class))->modified) {
(* ((SPObjectClass *) (parent_class))->modified) (object, flags);
}
}
/**
* Writes its settings to an incoming repr object, if any.
*/
static Inkscape::XML::Node *
sp_lpe_item_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
{
SPLPEItem *lpeitem = (SPLPEItem *) object;
if ( sp_lpe_item_has_path_effect(lpeitem) ) {
repr->setAttribute("inkscape:path-effect", lpeitem->path_effect_ref->lpeobject_href);
} else {
repr->setAttribute("inkscape:path-effect", NULL);
}
if (((SPObjectClass *)(parent_class))->write) {
((SPObjectClass *)(parent_class))->write(object, repr, flags);
}
return repr;
}
LivePathEffectObject *
sp_lpe_item_get_livepatheffectobject(SPLPEItem *lpeitem) {
if (!lpeitem) return NULL;
if (sp_lpe_item_has_path_effect(lpeitem)) {
return lpeitem->path_effect_ref->lpeobject;
} else {
return NULL;
}
}
Inkscape::LivePathEffect::Effect *
sp_lpe_item_get_livepatheffect(SPLPEItem *lpeitem) {
if (!lpeitem) return NULL;
LivePathEffectObject * lpeobj = sp_lpe_item_get_livepatheffectobject(lpeitem);
if (lpeobj)
return lpeobj->lpe;
else
return NULL;
}
void sp_lpe_item_perform_path_effect(SPLPEItem *lpeitem, SPCurve *curve) {
if (!lpeitem) return;
if (!curve) return;
if (sp_lpe_item_has_path_effect(lpeitem)) {
LivePathEffectObject *lpeobj = sp_lpe_item_get_livepatheffectobject(lpeitem);
lpeobj->lpe->doBeforeEffect(lpeitem);
lpeobj->lpe->doEffect(curve);
}
SPObject *parent = lpeitem->parent;
if (parent && SP_IS_LPE_ITEM(parent))
sp_lpe_item_perform_path_effect(SP_LPE_ITEM(parent), curve);
}
/**
* Calls any registered handlers for the update_patheffect action
*/
void
sp_lpe_item_update_patheffect (SPLPEItem *lpeitem, bool write)
{
#ifdef SHAPE_VERBOSE
g_message("sp_lpe_item_update_patheffect: %p\n", lpeitem);
#endif
g_return_if_fail (lpeitem != NULL);
g_return_if_fail (SP_IS_LPE_ITEM (lpeitem));
if (SP_LPE_ITEM_CLASS (G_OBJECT_GET_CLASS (lpeitem))->update_patheffect) {
SP_LPE_ITEM_CLASS (G_OBJECT_GET_CLASS (lpeitem))->update_patheffect (lpeitem, write);
}
}
/**
* Gets called when (re)attached to another lpeobject.
*/
static void
lpeobject_ref_changed(SPObject *old_ref, SPObject *ref, SPLPEItem *lpeitem)
{
if (old_ref) {
sp_signal_disconnect_by_data(old_ref, lpeitem);
}
if ( IS_LIVEPATHEFFECT(ref) && ref != lpeitem )
{
lpeitem->lpe_modified_connection.disconnect();
lpeitem->lpe_modified_connection = ref->connectModified(sigc::bind(sigc::ptr_fun(&lpeobject_ref_modified), lpeitem));
lpeobject_ref_modified(ref, 0, lpeitem);
}
}
/**
* Gets called when lpeobject repr contents change: i.e. parameter change.
*/
static void
lpeobject_ref_modified(SPObject */*href*/, guint /*flags*/, SPLPEItem *lpeitem)
{
sp_lpe_item_update_patheffect (lpeitem, true);
}
static void
sp_lpe_item_create_original_path_recursive(SPLPEItem *lpeitem)
{
if (SP_IS_GROUP(lpeitem)) {
GSList const *item_list = sp_item_group_item_list(SP_GROUP(lpeitem));
for ( GSList const *iter = item_list; iter; iter = iter->next ) {
SPObject *subitem = static_cast<SPObject *>(iter->data);
if (SP_IS_LPE_ITEM(subitem)) {
sp_lpe_item_create_original_path_recursive(SP_LPE_ITEM(subitem));
}
}
}
else if (SP_IS_PATH(lpeitem)) {
Inkscape::XML::Node *pathrepr = SP_OBJECT_REPR(lpeitem);
if ( !pathrepr->attribute("inkscape:original-d") ) {
pathrepr->setAttribute("inkscape:original-d", pathrepr->attribute("d"));
}
}
}
static void
sp_lpe_item_cleanup_original_path_recursive(SPLPEItem *lpeitem)
{
if (SP_IS_GROUP(lpeitem)) {
GSList const *item_list = sp_item_group_item_list(SP_GROUP(lpeitem));
for ( GSList const *iter = item_list; iter; iter = iter->next ) {
SPObject *subitem = static_cast<SPObject *>(iter->data);
if (SP_IS_LPE_ITEM(subitem)) {
sp_lpe_item_cleanup_original_path_recursive(SP_LPE_ITEM(subitem));
}
}
}
else if (SP_IS_PATH(lpeitem)) {
Inkscape::XML::Node *repr = SP_OBJECT_REPR(lpeitem);
if (!sp_lpe_item_has_path_effect_recursive(lpeitem)
&& repr->attribute("inkscape:original-d")) {
repr->setAttribute("d", repr->attribute("inkscape:original-d"));
repr->setAttribute("inkscape:original-d", NULL);
}
else {
sp_lpe_item_update_patheffect(lpeitem, true);
}
}
}
void sp_lpe_item_set_path_effect(SPLPEItem *lpeitem, gchar *value, bool reset)
{
if (!value) {
sp_lpe_item_remove_path_effect(lpeitem, false);
} else {
SP_OBJECT_REPR(lpeitem)->setAttribute("inkscape:path-effect", value);
// Ask the path effect to reset itself if it doesn't have parameters yet
if (lpeitem->path_effect_ref && reset) {
LivePathEffectObject *lpeobj = lpeitem->path_effect_ref->lpeobject;
if (lpeobj && lpeobj->lpe) {
// has to be called when all the subitems have their lpes applied
lpeobj->lpe->resetDefaults(lpeitem);
}
}
// make sure there is an original-d for paths!!!
sp_lpe_item_create_original_path_recursive(lpeitem);
}
}
void sp_lpe_item_set_path_effect(SPLPEItem *lpeitem, LivePathEffectObject * new_lpeobj)
{
const gchar * repr_id = SP_OBJECT_REPR(new_lpeobj)->attribute("id");
gchar *hrefstr = g_strdup_printf("#%s", repr_id);
sp_lpe_item_set_path_effect(lpeitem, hrefstr, false);
g_free(hrefstr);
}
void sp_lpe_item_remove_path_effect(SPLPEItem *lpeitem, bool keep_paths)
{
Inkscape::XML::Node *repr = SP_OBJECT_REPR(lpeitem);
repr->setAttribute("inkscape:path-effect", NULL);
if (!keep_paths) {
sp_lpe_item_cleanup_original_path_recursive(lpeitem);
}
}
bool sp_lpe_item_has_path_effect(SPLPEItem *lpeitem)
{
return lpeitem->path_effect_ref && lpeitem->path_effect_ref->lpeobject;
}
bool sp_lpe_item_has_path_effect_recursive(SPLPEItem *lpeitem)
{
SPObject *parent = lpeitem->parent;
if (parent && SP_IS_LPE_ITEM(parent)) {
return sp_lpe_item_has_path_effect(lpeitem) || sp_lpe_item_has_path_effect_recursive(SP_LPE_ITEM(parent));
}
else {
return sp_lpe_item_has_path_effect(lpeitem);
}
}
void sp_lpe_item_edit_next_param_oncanvas(SPLPEItem *lpeitem, SPDesktop *dt)
{
LivePathEffectObject *lpeobj = sp_lpe_item_get_livepatheffectobject(lpeitem);
if (lpeobj && lpeobj->lpe) {
lpeobj->lpe->editNextParamOncanvas(SP_ITEM(lpeitem), dt);
}
}
static void
sp_lpe_item_child_added (SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *ref)
{
if (((SPObjectClass *) (parent_class))->child_added)
(* ((SPObjectClass *) (parent_class))->child_added) (object, child, ref);
if (SP_IS_LPE_ITEM(object) && sp_lpe_item_has_path_effect_recursive(SP_LPE_ITEM(object))) {
SPObject *ochild = sp_object_get_child_by_repr(object, child);
if ( ochild && SP_IS_LPE_ITEM(ochild) ) {
sp_lpe_item_create_original_path_recursive(SP_LPE_ITEM(ochild));
}
}
}
static void
sp_lpe_item_remove_child (SPObject * object, Inkscape::XML::Node * child)
{
if (SP_IS_LPE_ITEM(object) && sp_lpe_item_has_path_effect_recursive(SP_LPE_ITEM(object))) {
SPObject *ochild = sp_object_get_child_by_repr(object, child);
if ( ochild && SP_IS_LPE_ITEM(ochild) ) {
sp_lpe_item_cleanup_original_path_recursive(SP_LPE_ITEM(ochild));
}
}
if (((SPObjectClass *) (parent_class))->remove_child)
(* ((SPObjectClass *) (parent_class))->remove_child) (object, child);
}
/*
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:encoding=utf-8:textwidth=99 :
|