1 #define __SP_LPE_ITEM_CPP__
3 /** \file
4 * Base class for live path effect items
5 */
6 /*
7 * Authors:
8 * Johan Engelen <j.b.c.engelen@ewi.utwente.nl>
9 * Bastien Bouclet <bgkweb@gmail.com>
10 *
11 * Copyright (C) 2008 authors
12 *
13 * Released under GNU GPL, read the file 'COPYING' for more information
14 */
16 #ifdef HAVE_CONFIG_H
17 # include "config.h"
18 #endif
20 #include "live_effects/effect.h"
21 #include "live_effects/lpeobject.h"
22 #include "live_effects/lpeobject-reference.h"
24 #include "sp-path.h"
25 #include "sp-item-group.h"
26 #include "streq.h"
27 #include "macros.h"
28 #include "attributes.h"
29 #include "sp-lpe-item.h"
30 #include "xml/repr.h"
31 #include "uri.h"
33 /* LPEItem base class */
35 static void sp_lpe_item_class_init(SPLPEItemClass *klass);
36 static void sp_lpe_item_init(SPLPEItem *lpe_item);
37 static void sp_lpe_item_finalize(GObject *object);
39 static void sp_lpe_item_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
40 static void sp_lpe_item_release(SPObject *object);
41 static void sp_lpe_item_set(SPObject *object, unsigned int key, gchar const *value);
42 static void sp_lpe_item_update(SPObject *object, SPCtx *ctx, guint flags);
43 static void sp_lpe_item_modified (SPObject *object, unsigned int flags);
44 static Inkscape::XML::Node *sp_lpe_item_write(SPObject *object, Inkscape::XML::Node *repr, guint flags);
46 static void sp_lpe_item_child_added (SPObject * object, Inkscape::XML::Node * child, Inkscape::XML::Node * ref);
47 static void sp_lpe_item_remove_child (SPObject * object, Inkscape::XML::Node * child);
49 static void lpeobject_ref_changed(SPObject *old_ref, SPObject *ref, SPLPEItem *lpeitem);
50 static void lpeobject_ref_modified(SPObject *href, guint flags, SPLPEItem *lpeitem);
52 static void sp_lpe_item_create_original_path_recursive(SPLPEItem *lpeitem);
53 static void sp_lpe_item_cleanup_original_path_recursive(SPLPEItem *lpeitem);
55 static SPItemClass *parent_class;
57 GType
58 sp_lpe_item_get_type()
59 {
60 static GType lpe_item_type = 0;
62 if (!lpe_item_type) {
63 GTypeInfo lpe_item_info = {
64 sizeof(SPLPEItemClass),
65 NULL, NULL,
66 (GClassInitFunc) sp_lpe_item_class_init,
67 NULL, NULL,
68 sizeof(SPLPEItem),
69 16,
70 (GInstanceInitFunc) sp_lpe_item_init,
71 NULL, /* value_table */
72 };
73 lpe_item_type = g_type_register_static(SP_TYPE_ITEM, "SPLPEItem", &lpe_item_info, (GTypeFlags)0);
74 }
75 return lpe_item_type;
76 }
78 static void
79 sp_lpe_item_class_init(SPLPEItemClass *klass)
80 {
81 GObjectClass *gobject_class;
82 SPObjectClass *sp_object_class;
84 gobject_class = (GObjectClass *) klass;
85 sp_object_class = (SPObjectClass *) klass;
86 parent_class = (SPItemClass *)g_type_class_peek_parent (klass);
88 gobject_class->finalize = sp_lpe_item_finalize;
90 sp_object_class->build = sp_lpe_item_build;
91 sp_object_class->release = sp_lpe_item_release;
92 sp_object_class->set = sp_lpe_item_set;
93 sp_object_class->update = sp_lpe_item_update;
94 sp_object_class->modified = sp_lpe_item_modified;
95 sp_object_class->write = sp_lpe_item_write;
96 sp_object_class->child_added = sp_lpe_item_child_added;
97 sp_object_class->remove_child = sp_lpe_item_remove_child;
99 klass->update_patheffect = NULL;
100 }
102 static void
103 sp_lpe_item_init(SPLPEItem *lpeitem)
104 {
105 lpeitem->path_effect_ref = new Inkscape::LivePathEffect::LPEObjectReference(SP_OBJECT(lpeitem));
106 new (&lpeitem->lpe_modified_connection) sigc::connection();
107 }
109 static void
110 sp_lpe_item_finalize(GObject *object)
111 {
112 if (((GObjectClass *) (parent_class))->finalize) {
113 (* ((GObjectClass *) (parent_class))->finalize)(object);
114 }
115 }
117 /**
118 * Reads the Inkscape::XML::Node, and initializes SPLPEItem variables. For this to get called,
119 * our name must be associated with a repr via "sp_object_type_register". Best done through
120 * sp-object-repr.cpp's repr_name_entries array.
121 */
122 static void
123 sp_lpe_item_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
124 {
125 SP_LPE_ITEM(object)->path_effect_ref->changedSignal().connect(sigc::bind(sigc::ptr_fun(lpeobject_ref_changed), SP_LPE_ITEM(object)));
127 sp_object_read_attr(object, "inkscape:path-effect");
129 if (((SPObjectClass *) parent_class)->build) {
130 ((SPObjectClass *) parent_class)->build(object, document, repr);
131 }
132 }
134 /**
135 * Drops any allocated memory.
136 */
137 static void
138 sp_lpe_item_release(SPObject *object)
139 {
140 SPLPEItem *lpeitem;
141 lpeitem = (SPLPEItem *) object;
143 lpeitem->path_effect_ref->detach();
145 lpeitem->lpe_modified_connection.disconnect();
146 lpeitem->lpe_modified_connection.~connection();
148 if (((SPObjectClass *) parent_class)->release)
149 ((SPObjectClass *) parent_class)->release(object);
150 }
152 /**
153 * Sets a specific value in the SPLPEItem.
154 */
155 static void
156 sp_lpe_item_set(SPObject *object, unsigned int key, gchar const *value)
157 {
158 SPLPEItem *lpeitem = (SPLPEItem *) object;
160 switch (key) {
161 case SP_ATTR_INKSCAPE_PATH_EFFECT:
162 if ( value && lpeitem->path_effect_ref->lpeobject_href
163 && streq(value, lpeitem->path_effect_ref->lpeobject_href) ) {
164 /* No change, do nothing. */
165 } else {
166 if (value) {
167 // Now do the attaching, which emits the changed signal.
168 try {
169 lpeitem->path_effect_ref->link((gchar*)value);
170 } catch (Inkscape::BadURIException &e) {
171 g_warning("%s", e.what());
172 lpeitem->path_effect_ref->detach();
173 }
174 } else {
175 // Detach, which emits the changed signal.
176 lpeitem->path_effect_ref->detach();
177 }
178 }
179 break;
180 default:
181 if (((SPObjectClass *) parent_class)->set) {
182 ((SPObjectClass *) parent_class)->set(object, key, value);
183 }
184 break;
185 }
186 }
188 /**
189 * Receives update notifications.
190 */
191 static void
192 sp_lpe_item_update(SPObject *object, SPCtx *ctx, guint flags)
193 {
194 if (((SPObjectClass *) parent_class)->update) {
195 ((SPObjectClass *) parent_class)->update(object, ctx, flags);
196 }
197 }
199 /**
200 * Sets modified flag for all sub-item views.
201 */
202 static void
203 sp_lpe_item_modified (SPObject *object, unsigned int flags)
204 {
205 if (((SPObjectClass *) (parent_class))->modified) {
206 (* ((SPObjectClass *) (parent_class))->modified) (object, flags);
207 }
208 }
210 /**
211 * Writes its settings to an incoming repr object, if any.
212 */
213 static Inkscape::XML::Node *
214 sp_lpe_item_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
215 {
216 SPLPEItem *lpeitem = (SPLPEItem *) object;
218 if ( sp_lpe_item_has_path_effect(lpeitem) ) {
219 repr->setAttribute("inkscape:path-effect", lpeitem->path_effect_ref->lpeobject_href);
220 } else {
221 repr->setAttribute("inkscape:path-effect", NULL);
222 }
224 if (((SPObjectClass *)(parent_class))->write) {
225 ((SPObjectClass *)(parent_class))->write(object, repr, flags);
226 }
228 return repr;
229 }
232 LivePathEffectObject *
233 sp_lpe_item_get_livepatheffectobject(SPLPEItem *lpeitem) {
234 if (!lpeitem) return NULL;
236 if (sp_lpe_item_has_path_effect(lpeitem)) {
237 return lpeitem->path_effect_ref->lpeobject;
238 } else {
239 return NULL;
240 }
241 }
243 Inkscape::LivePathEffect::Effect *
244 sp_lpe_item_get_livepatheffect(SPLPEItem *lpeitem) {
245 if (!lpeitem) return NULL;
247 LivePathEffectObject * lpeobj = sp_lpe_item_get_livepatheffectobject(lpeitem);
248 if (lpeobj)
249 return lpeobj->lpe;
250 else
251 return NULL;
252 }
254 void sp_lpe_item_perform_path_effect(SPLPEItem *lpeitem, SPCurve *curve) {
255 if (!lpeitem) return;
256 if (!curve) return;
258 if (sp_lpe_item_has_path_effect(lpeitem)) {
259 LivePathEffectObject *lpeobj = sp_lpe_item_get_livepatheffectobject(lpeitem);
260 lpeobj->lpe->doEffect(curve);
261 }
263 SPObject *parent = lpeitem->parent;
264 if (parent && SP_IS_LPE_ITEM(parent))
265 sp_lpe_item_perform_path_effect(SP_LPE_ITEM(parent), curve);
266 }
268 /**
269 * Calls any registered handlers for the update_patheffect action
270 */
271 void
272 sp_lpe_item_update_patheffect (SPLPEItem *lpeitem, bool write)
273 {
274 #ifdef SHAPE_VERBOSE
275 g_message("sp_lpe_item_update_patheffect: %p\n", lpeitem);
276 #endif
277 g_return_if_fail (lpeitem != NULL);
278 g_return_if_fail (SP_IS_LPE_ITEM (lpeitem));
280 if (sp_lpe_item_has_path_effect(lpeitem)) {
281 LivePathEffectObject *lpeobj = sp_lpe_item_get_livepatheffectobject(lpeitem);
282 lpeobj->lpe->doBeforeEffect(lpeitem);
283 }
285 if (SP_LPE_ITEM_CLASS (G_OBJECT_GET_CLASS (lpeitem))->update_patheffect) {
286 SP_LPE_ITEM_CLASS (G_OBJECT_GET_CLASS (lpeitem))->update_patheffect (lpeitem, write);
287 }
288 }
290 /**
291 * Gets called when (re)attached to another lpeobject.
292 */
293 static void
294 lpeobject_ref_changed(SPObject *old_ref, SPObject *ref, SPLPEItem *lpeitem)
295 {
296 if (old_ref) {
297 sp_signal_disconnect_by_data(old_ref, lpeitem);
298 }
299 if ( IS_LIVEPATHEFFECT(ref) && ref != lpeitem )
300 {
301 lpeitem->lpe_modified_connection.disconnect();
302 lpeitem->lpe_modified_connection = ref->connectModified(sigc::bind(sigc::ptr_fun(&lpeobject_ref_modified), lpeitem));
303 lpeobject_ref_modified(ref, 0, lpeitem);
304 }
305 }
307 /**
308 * Gets called when lpeobject repr contents change: i.e. parameter change.
309 */
310 static void
311 lpeobject_ref_modified(SPObject */*href*/, guint /*flags*/, SPLPEItem *lpeitem)
312 {
313 sp_lpe_item_update_patheffect (lpeitem, true);
314 }
316 static void
317 sp_lpe_item_create_original_path_recursive(SPLPEItem *lpeitem)
318 {
319 if (SP_IS_GROUP(lpeitem)) {
320 GSList const *item_list = sp_item_group_item_list(SP_GROUP(lpeitem));
321 for ( GSList const *iter = item_list; iter; iter = iter->next ) {
322 SPObject *subitem = static_cast<SPObject *>(iter->data);
323 if (SP_IS_LPE_ITEM(subitem)) {
324 sp_lpe_item_create_original_path_recursive(SP_LPE_ITEM(subitem));
325 }
326 }
327 }
328 else if (SP_IS_PATH(lpeitem)) {
329 Inkscape::XML::Node *pathrepr = SP_OBJECT_REPR(lpeitem);
330 if ( !pathrepr->attribute("inkscape:original-d") ) {
331 pathrepr->setAttribute("inkscape:original-d", pathrepr->attribute("d"));
332 }
333 }
334 }
336 static void
337 sp_lpe_item_cleanup_original_path_recursive(SPLPEItem *lpeitem)
338 {
339 if (SP_IS_GROUP(lpeitem)) {
340 GSList const *item_list = sp_item_group_item_list(SP_GROUP(lpeitem));
341 for ( GSList const *iter = item_list; iter; iter = iter->next ) {
342 SPObject *subitem = static_cast<SPObject *>(iter->data);
343 if (SP_IS_LPE_ITEM(subitem)) {
344 sp_lpe_item_cleanup_original_path_recursive(SP_LPE_ITEM(subitem));
345 }
346 }
347 }
348 else if (SP_IS_PATH(lpeitem)) {
349 Inkscape::XML::Node *repr = SP_OBJECT_REPR(lpeitem);
350 if (!sp_lpe_item_has_path_effect_recursive(lpeitem)
351 && repr->attribute("inkscape:original-d")) {
352 repr->setAttribute("d", repr->attribute("inkscape:original-d"));
353 repr->setAttribute("inkscape:original-d", NULL);
354 }
355 else {
356 sp_lpe_item_update_patheffect(lpeitem, true);
357 }
358 }
359 }
361 void sp_lpe_item_set_path_effect(SPLPEItem *lpeitem, gchar *value)
362 {
363 if (!value) {
364 sp_lpe_item_remove_path_effect(lpeitem, false);
365 } else {
366 SP_OBJECT_REPR(lpeitem)->setAttribute("inkscape:path-effect", value);
368 // make sure there is an original-d for paths!!!
369 sp_lpe_item_create_original_path_recursive(lpeitem);
370 }
371 }
373 void sp_lpe_item_set_path_effect(SPLPEItem *lpeitem, LivePathEffectObject * new_lpeobj)
374 {
375 const gchar * repr_id = SP_OBJECT_REPR(new_lpeobj)->attribute("id");
376 gchar *hrefstr = g_strdup_printf("#%s", repr_id);
377 sp_lpe_item_set_path_effect(lpeitem, hrefstr);
378 g_free(hrefstr);
379 }
381 void sp_lpe_item_remove_path_effect(SPLPEItem *lpeitem, bool keep_paths)
382 {
383 Inkscape::XML::Node *repr = SP_OBJECT_REPR(lpeitem);
384 repr->setAttribute("inkscape:path-effect", NULL);
386 if (!keep_paths) {
387 sp_lpe_item_cleanup_original_path_recursive(lpeitem);
388 }
389 }
391 bool sp_lpe_item_has_path_effect(SPLPEItem *lpeitem)
392 {
393 return lpeitem->path_effect_ref && lpeitem->path_effect_ref->lpeobject;
394 }
396 bool sp_lpe_item_has_path_effect_recursive(SPLPEItem *lpeitem)
397 {
398 SPObject *parent = lpeitem->parent;
399 if (parent && SP_IS_LPE_ITEM(parent)) {
400 return sp_lpe_item_has_path_effect(lpeitem) || sp_lpe_item_has_path_effect_recursive(SP_LPE_ITEM(parent));
401 }
402 else {
403 return sp_lpe_item_has_path_effect(lpeitem);
404 }
405 }
407 void sp_lpe_item_edit_next_param_oncanvas(SPLPEItem *lpeitem, SPDesktop *dt)
408 {
409 LivePathEffectObject *lpeobj = sp_lpe_item_get_livepatheffectobject(lpeitem);
410 if (lpeobj && lpeobj->lpe) {
411 lpeobj->lpe->editNextParamOncanvas(SP_ITEM(lpeitem), dt);
412 }
413 }
415 static void
416 sp_lpe_item_child_added (SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *ref)
417 {
418 if (((SPObjectClass *) (parent_class))->child_added)
419 (* ((SPObjectClass *) (parent_class))->child_added) (object, child, ref);
421 if (SP_IS_LPE_ITEM(object) && sp_lpe_item_has_path_effect_recursive(SP_LPE_ITEM(object))) {
422 SPObject *ochild = sp_object_get_child_by_repr(object, child);
423 if ( ochild && SP_IS_LPE_ITEM(ochild) ) {
424 sp_lpe_item_create_original_path_recursive(SP_LPE_ITEM(ochild));
425 }
426 }
427 }
429 static void
430 sp_lpe_item_remove_child (SPObject * object, Inkscape::XML::Node * child)
431 {
432 if (SP_IS_LPE_ITEM(object) && sp_lpe_item_has_path_effect_recursive(SP_LPE_ITEM(object))) {
433 SPObject *ochild = sp_object_get_child_by_repr(object, child);
434 if ( ochild && SP_IS_LPE_ITEM(ochild) ) {
435 sp_lpe_item_cleanup_original_path_recursive(SP_LPE_ITEM(ochild));
436 }
437 }
439 if (((SPObjectClass *) (parent_class))->remove_child)
440 (* ((SPObjectClass *) (parent_class))->remove_child) (object, child);
441 }
443 /*
444 Local Variables:
445 mode:c++
446 c-file-style:"stroustrup"
447 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
448 indent-tabs-mode:nil
449 fill-column:99
450 End:
451 */
452 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :