Code

Fix for 419577 by Johan
[inkscape.git] / src / sp-lpe-item.cpp
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/lpe-path_length.h"
22 #include "live_effects/lpeobject.h"
23 #include "live_effects/lpeobject-reference.h"
25 #include "sp-path.h"
26 #include "sp-item-group.h"
27 #include "streq.h"
28 #include "macros.h"
29 #include "attributes.h"
30 #include "sp-lpe-item.h"
31 #include "xml/repr.h"
32 #include "uri.h"
33 #include "message-stack.h"
34 #include "inkscape.h"
35 #include "desktop.h"
36 #include "node-context.h"
37 #include "shape-editor.h"
39 #include <algorithm>
41 /* LPEItem base class */
43 static void sp_lpe_item_class_init(SPLPEItemClass *klass);
44 static void sp_lpe_item_init(SPLPEItem *lpe_item);
45 static void sp_lpe_item_finalize(GObject *object);
47 static void sp_lpe_item_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
48 static void sp_lpe_item_release(SPObject *object);
49 static void sp_lpe_item_set(SPObject *object, unsigned int key, gchar const *value);
50 static void sp_lpe_item_update(SPObject *object, SPCtx *ctx, guint flags);
51 static void sp_lpe_item_modified (SPObject *object, unsigned int flags);
52 static Inkscape::XML::Node *sp_lpe_item_write(SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags);
54 static void sp_lpe_item_child_added (SPObject * object, Inkscape::XML::Node * child, Inkscape::XML::Node * ref);
55 static void sp_lpe_item_remove_child (SPObject * object, Inkscape::XML::Node * child);
57 static void sp_lpe_item_enable_path_effects(SPLPEItem *lpeitem, bool enable);
59 static void lpeobject_ref_changed(SPObject *old_ref, SPObject *ref, SPLPEItem *lpeitem);
60 static void lpeobject_ref_modified(SPObject *href, guint flags, SPLPEItem *lpeitem);
62 static void sp_lpe_item_create_original_path_recursive(SPLPEItem *lpeitem);
63 static void sp_lpe_item_cleanup_original_path_recursive(SPLPEItem *lpeitem);
64 typedef std::list<std::string> HRefList;
65 static std::string patheffectlist_write_svg(PathEffectList const & list);
66 static std::string hreflist_write_svg(HRefList const & list);
68 static SPItemClass *parent_class;
70 GType
71 sp_lpe_item_get_type()
72 {
73     static GType lpe_item_type = 0;
75     if (!lpe_item_type) {
76         GTypeInfo lpe_item_info = {
77             sizeof(SPLPEItemClass),
78             NULL, NULL,
79             (GClassInitFunc) sp_lpe_item_class_init,
80             NULL, NULL,
81             sizeof(SPLPEItem),
82             16,
83             (GInstanceInitFunc) sp_lpe_item_init,
84             NULL,    /* value_table */
85         };
86         lpe_item_type = g_type_register_static(SP_TYPE_ITEM, "SPLPEItem", &lpe_item_info, (GTypeFlags)0);
87     }
88     return lpe_item_type;
89 }
91 static void
92 sp_lpe_item_class_init(SPLPEItemClass *klass)
93 {
94     GObjectClass *gobject_class;
95     SPObjectClass *sp_object_class;
97     gobject_class = (GObjectClass *) klass;
98     sp_object_class = (SPObjectClass *) klass;
99     parent_class = (SPItemClass *)g_type_class_peek_parent (klass);
101     gobject_class->finalize = sp_lpe_item_finalize;
103     sp_object_class->build = sp_lpe_item_build;
104     sp_object_class->release = sp_lpe_item_release;
105     sp_object_class->set = sp_lpe_item_set;
106     sp_object_class->update = sp_lpe_item_update;
107     sp_object_class->modified = sp_lpe_item_modified;
108     sp_object_class->write = sp_lpe_item_write;
109     sp_object_class->child_added = sp_lpe_item_child_added;
110     sp_object_class->remove_child = sp_lpe_item_remove_child;
112     klass->update_patheffect = NULL;
115 static void
116 sp_lpe_item_init(SPLPEItem *lpeitem)
118     lpeitem->path_effects_enabled = 1;
120     lpeitem->path_effect_list = new PathEffectList();
121     lpeitem->current_path_effect = NULL;
123     new (&lpeitem->lpe_modified_connection) sigc::connection();
126 static void
127 sp_lpe_item_finalize(GObject *object)
129     if (((GObjectClass *) (parent_class))->finalize) {
130         (* ((GObjectClass *) (parent_class))->finalize)(object);
131     }
134 /**
135  * Reads the Inkscape::XML::Node, and initializes SPLPEItem variables.  For this to get called,
136  * our name must be associated with a repr via "sp_object_type_register".  Best done through
137  * sp-object-repr.cpp's repr_name_entries array.
138  */
139 static void
140 sp_lpe_item_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
142     sp_object_read_attr(object, "inkscape:path-effect");
144     if (((SPObjectClass *) parent_class)->build) {
145         ((SPObjectClass *) parent_class)->build(object, document, repr);
146     }
149 /**
150  * Drops any allocated memory.
151  */
152 static void
153 sp_lpe_item_release(SPObject *object)
155     SPLPEItem *lpeitem = (SPLPEItem *) object;
157     lpeitem->lpe_modified_connection.disconnect();
158     lpeitem->lpe_modified_connection.~connection();
160     PathEffectList::iterator it = lpeitem->path_effect_list->begin();
161     while ( it != lpeitem->path_effect_list->end() ) {
162         // unlink and delete all references in the list
163         (*it)->unlink();
164         delete *it;
165         it = lpeitem->path_effect_list->erase(it);
166     }
167     // delete the list itself
168     delete SP_LPE_ITEM(object)->path_effect_list;
170     if (((SPObjectClass *) parent_class)->release)
171         ((SPObjectClass *) parent_class)->release(object);
174 /**
175  * Sets a specific value in the SPLPEItem.
176  */
177 static void
178 sp_lpe_item_set(SPObject *object, unsigned int key, gchar const *value)
180     SPLPEItem *lpeitem = (SPLPEItem *) object;
182     switch (key) {
183         case SP_ATTR_INKSCAPE_PATH_EFFECT:
184             {
185                 lpeitem->current_path_effect = NULL;
187                 // Disable the path effects while populating the LPE list
188                  sp_lpe_item_enable_path_effects(lpeitem, false);
190                 // Clear the path effect list
191                 PathEffectList::iterator it = lpeitem->path_effect_list->begin();
192                 while ( it != lpeitem->path_effect_list->end() )
193                 {
194                     (*it)->unlink();
195                     delete *it;
196                     it = lpeitem->path_effect_list->erase(it);
197                 }
199                 // Parse the contents of "value" to rebuild the path effect reference list
200                 if ( value ) {
201                     std::istringstream iss(value);
202                     std::string href;
203                     while (std::getline(iss, href, ';'))
204                     {
205                         Inkscape::LivePathEffect::LPEObjectReference *path_effect_ref = new Inkscape::LivePathEffect::LPEObjectReference(SP_OBJECT(lpeitem));
206                         path_effect_ref->changedSignal().connect(sigc::bind(sigc::ptr_fun(lpeobject_ref_changed), SP_LPE_ITEM(object)));
207                         // Now do the attaching, which emits the changed signal.
208                         // Fixme, it should not do this changed signal and updating before all effects are added to the path_effect_list
209                         try {
210                             path_effect_ref->link(href.c_str());
211                         } catch (Inkscape::BadURIException e) {
212                             g_warning("BadURIException when trying to find LPE: %s", e.what());
213                             path_effect_ref->unlink();
214                             delete path_effect_ref;
215                             path_effect_ref = NULL;
216                         }
218                         lpeitem->path_effect_list->push_back(path_effect_ref);
219                         if ( !(path_effect_ref->lpeobject && path_effect_ref->lpeobject->get_lpe()) ) {
220                             // something has gone wrong in finding the right patheffect.
221                             g_warning("Unknown LPE type specified, LPE stack effectively disabled");
222                             // keep the effect in the lpestack, so the whole stack is effectively disabled but maintained
223                         }
224                     }
225                 }
227                 sp_lpe_item_enable_path_effects(lpeitem, true);
228             }
229             break;
230         default:
231             if (((SPObjectClass *) parent_class)->set) {
232                 ((SPObjectClass *) parent_class)->set(object, key, value);
233             }
234             break;
235     }
238 /**
239  * Receives update notifications.
240  */
241 static void
242 sp_lpe_item_update(SPObject *object, SPCtx *ctx, guint flags)
244     if (((SPObjectClass *) parent_class)->update) {
245         ((SPObjectClass *) parent_class)->update(object, ctx, flags);
246     }
248     // update the helperpaths of all LPEs applied to the item
249     // TODO: is there a more canonical place for this, since we don't have instant access to the item's nodepath?
250     // FIXME: this is called multiple (at least 3) times; how can we avoid this?
252     // FIXME: ditch inkscape_active_event_context()
253     SPEventContext *ec = inkscape_active_event_context();
254     if (!SP_IS_NODE_CONTEXT(ec)) return;
255     ShapeEditor *sh = ec->shape_editor;
256     g_assert(sh);
257     if (!sh->has_nodepath()) return;
259     Inkscape::NodePath::Path *np = sh->get_nodepath();
260     sp_nodepath_update_helperpaths(np);
263 /**
264  * Sets modified flag for all sub-item views.
265  */
266 static void
267 sp_lpe_item_modified (SPObject *object, unsigned int flags)
269     if (SP_IS_GROUP(object) && (flags & SP_OBJECT_MODIFIED_FLAG) && (flags & SP_OBJECT_USER_MODIFIED_FLAG_B)) {
270         sp_lpe_item_update_patheffect(SP_LPE_ITEM(object), true, true);
271     }
273     if (((SPObjectClass *) (parent_class))->modified) {
274         (* ((SPObjectClass *) (parent_class))->modified) (object, flags);
275     }
278 /**
279  * Writes its settings to an incoming repr object, if any.
280  */
281 static Inkscape::XML::Node *
282 sp_lpe_item_write(SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
284     SPLPEItem *lpeitem = (SPLPEItem *) object;
286     if (flags & SP_OBJECT_WRITE_EXT) {
287         if ( sp_lpe_item_has_path_effect(lpeitem) ) {
288             std::string href = patheffectlist_write_svg(*lpeitem->path_effect_list);
289             repr->setAttribute("inkscape:path-effect", href.c_str());
290         } else {
291             repr->setAttribute("inkscape:path-effect", NULL);
292         }
293     }
295     if (((SPObjectClass *)(parent_class))->write) {
296         ((SPObjectClass *)(parent_class))->write(object, xml_doc, repr, flags);
297     }
299     return repr;
302 /**
303  * returns true when LPE was successful.
304  */
305 bool sp_lpe_item_perform_path_effect(SPLPEItem *lpeitem, SPCurve *curve) {
306     if (!lpeitem) return false;
307     if (!curve) return false;
309     if (sp_lpe_item_has_path_effect(lpeitem) && sp_lpe_item_path_effects_enabled(lpeitem)) {
310         for (PathEffectList::iterator it = lpeitem->path_effect_list->begin(); it != lpeitem->path_effect_list->end(); ++it)
311         {
312             LivePathEffectObject *lpeobj = (*it)->lpeobject;
313             if (!lpeobj) {
314                 /** \todo Investigate the cause of this.
315                  * For example, this happens when copy pasting an object with LPE applied. Probably because the object is pasted while the effect is not yet pasted to defs, and cannot be found.
316                  */
317                 g_warning("sp_lpe_item_perform_path_effect - NULL lpeobj in list!");
318                 return false;
319             }
320             Inkscape::LivePathEffect::Effect *lpe = lpeobj->get_lpe();
321             if (!lpe) {
322                 /** \todo Investigate the cause of this.
323                  * Not sure, but I think this can happen when an unknown effect type is specified...
324                  */
325                 g_warning("sp_lpe_item_perform_path_effect - lpeobj with invalid lpe in the stack!");
326                 return false;
327             }
329             if (lpe->isVisible()) {
330                 if (lpe->acceptsNumClicks() > 0 && !lpe->isReady()) {
331                     // if the effect expects mouse input before being applied and the input is not finished
332                     // yet, we don't alter the path
333                     return false;
334                 }
336                 // Groups have their doBeforeEffect called elsewhere
337                 if (!SP_IS_GROUP(lpeitem)) {
338                     lpe->doBeforeEffect(lpeitem);
339                 }
341                 try {
342                     lpe->doEffect(curve);
343                 }
344                 catch (std::exception & e) {
345                     g_warning("Exception during LPE %s execution. \n %s", lpe->getName().c_str(), e.what());
346                     if (SP_ACTIVE_DESKTOP && SP_ACTIVE_DESKTOP->messageStack()) {
347                         SP_ACTIVE_DESKTOP->messageStack()->flash( Inkscape::WARNING_MESSAGE,
348                                         _("An exception occurred during execution of the Path Effect.") );
349                     }
350                     return false;
351                 }
352             }
353         }
354     }
356     return true;
359 /**
360  * Calls any registered handlers for the update_patheffect action
361  */
362 void
363 sp_lpe_item_update_patheffect (SPLPEItem *lpeitem, bool wholetree, bool write)
365 #ifdef SHAPE_VERBOSE
366     g_message("sp_lpe_item_update_patheffect: %p\n", lpeitem);
367 #endif
368     g_return_if_fail (lpeitem != NULL);
369     g_return_if_fail (SP_IS_LPE_ITEM (lpeitem));
371     if (!sp_lpe_item_path_effects_enabled(lpeitem))
372         return;
374     // TODO: hack! this will be removed when path length measuring is reimplemented in a better way
375     PathEffectList lpelist = sp_lpe_item_get_effect_list(lpeitem);
376     std::list<Inkscape::LivePathEffect::LPEObjectReference *>::iterator i;
377     for (i = lpelist.begin(); i != lpelist.end(); ++i) {
378         if ((*i)->lpeobject) {
379             Inkscape::LivePathEffect::Effect *lpe = (*i)->lpeobject->get_lpe();
380             if (dynamic_cast<Inkscape::LivePathEffect::LPEPathLength *>(lpe)) {
381                 if (!lpe->isVisible()) {
382                     // we manually disable text for LPEPathLength
383                     dynamic_cast<Inkscape::LivePathEffect::LPEPathLength *>(lpe)->hideCanvasText();
384                 }
385             }
386         }
387     }
389     SPLPEItem *top;
391     if (wholetree) {
392         SPObject *prev_parent = lpeitem;
393         SPObject *parent = prev_parent->parent;
394         while (parent && SP_IS_LPE_ITEM(parent) && sp_lpe_item_has_path_effect_recursive(SP_LPE_ITEM(parent))) {
395             prev_parent = parent;
396             parent = prev_parent->parent;
397         }
398         top = SP_LPE_ITEM(prev_parent);
399     }
400     else {
401         top = lpeitem;
402     }
404     if (SP_LPE_ITEM_CLASS (G_OBJECT_GET_CLASS (top))->update_patheffect) {
405         SP_LPE_ITEM_CLASS (G_OBJECT_GET_CLASS (top))->update_patheffect (top, write);
406     }
409 /**
410  * Gets called when (re)attached to another lpeobject.
411  */
412 static void
413 lpeobject_ref_changed(SPObject *old_ref, SPObject *ref, SPLPEItem *lpeitem)
415     if (old_ref) {
416         sp_signal_disconnect_by_data(old_ref, lpeitem);
417     }
418     if ( IS_LIVEPATHEFFECT(ref) && ref != lpeitem )
419     {
420         lpeitem->lpe_modified_connection.disconnect();
421         lpeitem->lpe_modified_connection = ref->connectModified(sigc::bind(sigc::ptr_fun(&lpeobject_ref_modified), lpeitem));
422         lpeobject_ref_modified(ref, 0, lpeitem);
423     }
426 /**
427  * Gets called when lpeobject repr contents change: i.e. parameter change.
428  */
429 static void
430 lpeobject_ref_modified(SPObject */*href*/, guint /*flags*/, SPLPEItem *lpeitem)
432     sp_lpe_item_update_patheffect (lpeitem, true, true);
435 static void
436 sp_lpe_item_create_original_path_recursive(SPLPEItem *lpeitem)
438     if (SP_IS_GROUP(lpeitem)) {
439         GSList const *item_list = sp_item_group_item_list(SP_GROUP(lpeitem));
440         for ( GSList const *iter = item_list; iter; iter = iter->next ) {
441             SPObject *subitem = static_cast<SPObject *>(iter->data);
442             if (SP_IS_LPE_ITEM(subitem)) {
443                 sp_lpe_item_create_original_path_recursive(SP_LPE_ITEM(subitem));
444             }
445         }
446     }
447     else if (SP_IS_PATH(lpeitem)) {
448         Inkscape::XML::Node *pathrepr = SP_OBJECT_REPR(lpeitem);
449         if ( !pathrepr->attribute("inkscape:original-d") ) {
450             pathrepr->setAttribute("inkscape:original-d", pathrepr->attribute("d"));
451         }
452     }
455 static void
456 sp_lpe_item_cleanup_original_path_recursive(SPLPEItem *lpeitem)
458     if (SP_IS_GROUP(lpeitem)) {
459         GSList const *item_list = sp_item_group_item_list(SP_GROUP(lpeitem));
460         for ( GSList const *iter = item_list; iter; iter = iter->next ) {
461             SPObject *subitem = static_cast<SPObject *>(iter->data);
462             if (SP_IS_LPE_ITEM(subitem)) {
463                 sp_lpe_item_cleanup_original_path_recursive(SP_LPE_ITEM(subitem));
464             }
465         }
466     }
467     else if (SP_IS_PATH(lpeitem)) {
468         Inkscape::XML::Node *repr = SP_OBJECT_REPR(lpeitem);
469         if (!sp_lpe_item_has_path_effect_recursive(lpeitem)
470                 && repr->attribute("inkscape:original-d")) {
471             repr->setAttribute("d", repr->attribute("inkscape:original-d"));
472             repr->setAttribute("inkscape:original-d", NULL);
473         }
474         else {
475             sp_lpe_item_update_patheffect(lpeitem, true, true);
476         }
477     }
480 void sp_lpe_item_add_path_effect(SPLPEItem *lpeitem, gchar *value, bool reset)
482     if (value) {
483         // Apply the path effects here because in the casse of a group, lpe->resetDefaults
484         // needs that all the subitems have their effects applied
485         sp_lpe_item_update_patheffect(lpeitem, false, true);
487         // Disable the path effects while preparing the new lpe
488         sp_lpe_item_enable_path_effects(lpeitem, false);
490         // Add the new reference to the list of LPE references
491         HRefList hreflist;
492         for (PathEffectList::const_iterator it = lpeitem->path_effect_list->begin(); it != lpeitem->path_effect_list->end(); ++it)
493         {
494             hreflist.push_back( std::string((*it)->lpeobject_href) );
495         }
496         hreflist.push_back( std::string(value) );
497         std::string hrefs = hreflist_write_svg(hreflist);
499         SP_OBJECT_REPR(lpeitem)->setAttribute("inkscape:path-effect", hrefs.c_str());
501         // make sure there is an original-d for paths!!!
502         sp_lpe_item_create_original_path_recursive(lpeitem);
504         LivePathEffectObject *lpeobj = lpeitem->path_effect_list->back()->lpeobject;
505         if (lpeobj && lpeobj->get_lpe()) {
506             Inkscape::LivePathEffect::Effect *lpe = lpeobj->get_lpe();
507             // Ask the path effect to reset itself if it doesn't have parameters yet
508             if (reset) {
509                 // has to be called when all the subitems have their lpes applied
510                 lpe->resetDefaults(lpeitem);
511             }
513             // perform this once when the effect is applied
514             lpe->doOnApply(SP_LPE_ITEM(lpeitem));
516             // indicate that all necessary preparations are done and the effect can be performed
517             lpe->setReady();
518         }
520         //Enable the path effects now that everything is ready to apply the new path effect
521         sp_lpe_item_enable_path_effects(lpeitem, true);
523         // Apply the path effect
524         sp_lpe_item_update_patheffect(lpeitem, true, true);
525     }
528 void sp_lpe_item_add_path_effect(SPLPEItem *lpeitem, LivePathEffectObject * new_lpeobj)
530     const gchar * repr_id = SP_OBJECT_REPR(new_lpeobj)->attribute("id");
531     gchar *hrefstr = g_strdup_printf("#%s", repr_id);
532     sp_lpe_item_add_path_effect(lpeitem, hrefstr, false);
533     g_free(hrefstr);
536 void sp_lpe_item_remove_current_path_effect(SPLPEItem *lpeitem, bool keep_paths)
538     Inkscape::LivePathEffect::LPEObjectReference* lperef = sp_lpe_item_get_current_lpereference(lpeitem);
539     if (!lperef)
540         return;
542     PathEffectList new_list = *lpeitem->path_effect_list;
543     new_list.remove(lperef); //current lpe ref is always our 'own' pointer from the path_effect_list
544     std::string r = patheffectlist_write_svg(new_list);
546     if (!r.empty()) {
547         SP_OBJECT_REPR(lpeitem)->setAttribute("inkscape:path-effect", r.c_str());
548     } else {
549         SP_OBJECT_REPR(lpeitem)->setAttribute("inkscape:path-effect", NULL);
550     }
552     if (!keep_paths) {
553         sp_lpe_item_cleanup_original_path_recursive(lpeitem);
554     }
557 void sp_lpe_item_remove_all_path_effects(SPLPEItem *lpeitem, bool keep_paths)
559     SP_OBJECT_REPR(lpeitem)->setAttribute("inkscape:path-effect", NULL);
561     if (!keep_paths) {
562         sp_lpe_item_cleanup_original_path_recursive(lpeitem);
563     }
566 void sp_lpe_item_down_current_path_effect(SPLPEItem *lpeitem)
568     Inkscape::LivePathEffect::LPEObjectReference* lperef = sp_lpe_item_get_current_lpereference(lpeitem);
569     if (!lperef)
570         return;
572     PathEffectList new_list = *lpeitem->path_effect_list;
573     PathEffectList::iterator cur_it = find( new_list.begin(), new_list.end(), lperef );
574     if (cur_it != new_list.end()) {
575         PathEffectList::iterator down_it = cur_it;
576         down_it++;
577         if (down_it != new_list.end()) { // perhaps current effect is already last effect
578             std::iter_swap(cur_it, down_it);
579         }
580     }
581     std::string r = patheffectlist_write_svg(new_list);
582     SP_OBJECT_REPR(lpeitem)->setAttribute("inkscape:path-effect", r.c_str());
584     sp_lpe_item_cleanup_original_path_recursive(lpeitem);
587 void sp_lpe_item_up_current_path_effect(SPLPEItem *lpeitem)
589     Inkscape::LivePathEffect::LPEObjectReference* lperef = sp_lpe_item_get_current_lpereference(lpeitem);
590     if (!lperef)
591         return;
593     PathEffectList new_list = *lpeitem->path_effect_list;
594     PathEffectList::iterator cur_it = find( new_list.begin(), new_list.end(), lperef );
595     if (cur_it != new_list.end() && cur_it != new_list.begin()) {
596         PathEffectList::iterator up_it = cur_it;
597         up_it--;
598         std::iter_swap(cur_it, up_it);
599     }
600     std::string r = patheffectlist_write_svg(new_list);
602     SP_OBJECT_REPR(lpeitem)->setAttribute("inkscape:path-effect", r.c_str());
604     sp_lpe_item_cleanup_original_path_recursive(lpeitem);
607 /** used for shapes so they can see if they should also disable shape calculation and read from d= */
608 bool sp_lpe_item_has_broken_path_effect(SPLPEItem *lpeitem)
610     if (lpeitem->path_effect_list->empty())
611         return false;
613     // go through the list; if some are unknown or invalid, return true
614     PathEffectList effect_list =  sp_lpe_item_get_effect_list(lpeitem);
615     for (PathEffectList::iterator it = effect_list.begin(); it != effect_list.end(); it++)
616     {
617         LivePathEffectObject *lpeobj = (*it)->lpeobject;
618         if (!lpeobj || !lpeobj->get_lpe())
619             return true;
620     }
622     return false;
626 bool sp_lpe_item_has_path_effect(SPLPEItem *lpeitem)
628     if (lpeitem->path_effect_list->empty())
629         return false;
631     // go through the list; if some are unknown or invalid, we are not an LPE item!
632     PathEffectList effect_list =  sp_lpe_item_get_effect_list(lpeitem);
633     for (PathEffectList::iterator it = effect_list.begin(); it != effect_list.end(); it++)
634     {
635         LivePathEffectObject *lpeobj = (*it)->lpeobject;
636         if (!lpeobj || !lpeobj->get_lpe())
637             return false;
638     }
640     return true;
643 bool sp_lpe_item_has_path_effect_recursive(SPLPEItem *lpeitem)
645     SPObject *parent = lpeitem->parent;
646     if (parent && SP_IS_LPE_ITEM(parent)) {
647         return sp_lpe_item_has_path_effect(lpeitem) || sp_lpe_item_has_path_effect_recursive(SP_LPE_ITEM(parent));
648     }
649     else {
650         return sp_lpe_item_has_path_effect(lpeitem);
651     }
654 Inkscape::LivePathEffect::Effect*
655 sp_lpe_item_has_path_effect_of_type(SPLPEItem *lpeitem, int type)
657     std::list<Inkscape::LivePathEffect::LPEObjectReference *>::iterator i;
658     for (i = lpeitem->path_effect_list->begin(); i != lpeitem->path_effect_list->end(); ++i) {
659         Inkscape::LivePathEffect::Effect* lpe = (*i)->lpeobject->get_lpe();
660         if (lpe && (lpe->effectType() == type)) {
661             return lpe;
662         }
663     }
664     return NULL;
667 /* Return false if the item is not a path or already has a shape applied */
668 bool sp_lpe_item_can_accept_freehand_shape(SPLPEItem *lpeitem)
670     if (!SP_IS_PATH(lpeitem))
671         return false;
673     if (sp_lpe_item_has_path_effect_of_type(lpeitem, Inkscape::LivePathEffect::FREEHAND_SHAPE))
674         return false;
676     return true;
679 void sp_lpe_item_edit_next_param_oncanvas(SPLPEItem *lpeitem, SPDesktop *dt)
681     Inkscape::LivePathEffect::LPEObjectReference *lperef = sp_lpe_item_get_current_lpereference(lpeitem);
682     if (lperef && lperef->lpeobject && lperef->lpeobject->get_lpe()) {
683         lperef->lpeobject->get_lpe()->editNextParamOncanvas(SP_ITEM(lpeitem), dt);
684     }
687 static void
688 sp_lpe_item_child_added (SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *ref)
690     if (((SPObjectClass *) (parent_class))->child_added)
691         (* ((SPObjectClass *) (parent_class))->child_added) (object, child, ref);
693     if (SP_IS_LPE_ITEM(object) && sp_lpe_item_has_path_effect_recursive(SP_LPE_ITEM(object))) {
694         SPObject *ochild = sp_object_get_child_by_repr(object, child);
695         if ( ochild && SP_IS_LPE_ITEM(ochild) ) {
696             sp_lpe_item_create_original_path_recursive(SP_LPE_ITEM(ochild));
697         }
698     }
701 static void
702 sp_lpe_item_remove_child (SPObject * object, Inkscape::XML::Node * child)
704     if (SP_IS_LPE_ITEM(object) && sp_lpe_item_has_path_effect_recursive(SP_LPE_ITEM(object))) {
705         SPObject *ochild = sp_object_get_child_by_repr(object, child);
706         if ( ochild && SP_IS_LPE_ITEM(ochild) ) {
707             sp_lpe_item_cleanup_original_path_recursive(SP_LPE_ITEM(ochild));
708         }
709     }
711     if (((SPObjectClass *) (parent_class))->remove_child)
712         (* ((SPObjectClass *) (parent_class))->remove_child) (object, child);
715 static std::string patheffectlist_write_svg(PathEffectList const & list)
717     HRefList hreflist;
718     for (PathEffectList::const_iterator it = list.begin(); it != list.end(); ++it)
719     {
720         hreflist.push_back( std::string((*it)->lpeobject_href) );
721     }
722     return hreflist_write_svg(hreflist);
725 /**
726  *  THE function that should be used to generate any patheffectlist string.
727  * one of the methods to change the effect list:
728  *  - create temporary href list
729  *  - populate the templist with the effects from the old list that you want to have and their order
730  *  - call this function with temp list as param
731  */
732 static std::string hreflist_write_svg(HRefList const & list)
734     std::string r;
735     bool semicolon_first = false;
736     for (HRefList::const_iterator it = list.begin(); it != list.end(); ++it)
737     {
738         if (semicolon_first) {
739             r += ';';
740         }
741         semicolon_first = true;
743         r += (*it);
744     }
745     return r;
748 // Return a copy of the effect list
749 PathEffectList sp_lpe_item_get_effect_list(SPLPEItem *lpeitem)
751     return *lpeitem->path_effect_list;
754 Inkscape::LivePathEffect::LPEObjectReference* sp_lpe_item_get_current_lpereference(SPLPEItem *lpeitem)
756     if (!lpeitem->current_path_effect && !lpeitem->path_effect_list->empty())
757         sp_lpe_item_set_current_path_effect(lpeitem, lpeitem->path_effect_list->back());
759     return lpeitem->current_path_effect;
762 Inkscape::LivePathEffect::Effect* sp_lpe_item_get_current_lpe(SPLPEItem *lpeitem)
764     Inkscape::LivePathEffect::LPEObjectReference* lperef = sp_lpe_item_get_current_lpereference(lpeitem);
766     if (lperef && lperef->lpeobject)
767         return lperef->lpeobject->get_lpe();
768     else
769         return NULL;
772 bool sp_lpe_item_set_current_path_effect(SPLPEItem *lpeitem, Inkscape::LivePathEffect::LPEObjectReference* lperef)
774     for (PathEffectList::iterator it = lpeitem->path_effect_list->begin(); it != lpeitem->path_effect_list->end(); it++) {
775         if ((*it)->lpeobject_repr == lperef->lpeobject_repr) {
776             lpeobject_ref_changed(NULL, (*it)->lpeobject, SP_LPE_ITEM(lpeitem)); // FIXME: explain why this is here?
777             lpeitem->current_path_effect = (*it);  // current_path_effect should always be a pointer from the path_effect_list !
778             return true;
779         }
780     }
782     return false;
785 void sp_lpe_item_replace_path_effect(SPLPEItem *lpeitem, LivePathEffectObject * old_lpeobj,
786                                         LivePathEffectObject * new_lpeobj)
788     HRefList hreflist;
789     for (PathEffectList::const_iterator it = lpeitem->path_effect_list->begin(); it != lpeitem->path_effect_list->end(); ++it)
790     {
791         if ((*it)->lpeobject == old_lpeobj) {
792             const gchar * repr_id = SP_OBJECT_REPR(new_lpeobj)->attribute("id");
793             gchar *hrefstr = g_strdup_printf("#%s", repr_id);
794             hreflist.push_back( std::string(hrefstr) );
795             g_free(hrefstr);
796         }
797         else {
798             hreflist.push_back( std::string((*it)->lpeobject_href) );
799         }
800     }
801     std::string r = hreflist_write_svg(hreflist);
802     SP_OBJECT_REPR(lpeitem)->setAttribute("inkscape:path-effect", r.c_str());
805 // Enable or disable the path effects of the item.
806 // The counter allows nested calls
807 static void sp_lpe_item_enable_path_effects(SPLPEItem *lpeitem, bool enable)
809     if (enable) {
810         lpeitem->path_effects_enabled++;
811     }
812     else {
813         lpeitem->path_effects_enabled--;
814     }
817 // Are the path effects enabled on this item ?
818 bool sp_lpe_item_path_effects_enabled(SPLPEItem *lpeitem)
820     return lpeitem->path_effects_enabled > 0;
823 /*
824   Local Variables:
825   mode:c++
826   c-file-style:"stroustrup"
827   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
828   indent-tabs-mode:nil
829   fill-column:99
830   End:
831 */
832 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :