1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (C) Johan Engelen 2007-2008 <j.b.c.engelen@utwente.nl>
4  *   Abhishek Sharma
5  *
6  * Released under GNU GPL v2+, read the file 'COPYING' for more information.
7  */
8 
9 #include "live_effects/lpeobject.h"
10 
11 #include "live_effects/effect.h"
12 
13 #include "xml/repr.h"
14 #include "xml/node-event-vector.h"
15 #include "attributes.h"
16 #include "document.h"
17 
18 #include "object/sp-defs.h"
19 
20 //#define LIVEPATHEFFECT_VERBOSE
21 
22 static void livepatheffect_on_repr_attr_changed (Inkscape::XML::Node * repr, const gchar *key, const gchar *oldval, const gchar *newval, bool is_interactive, void * data);
23 
24 static Inkscape::XML::NodeEventVector const livepatheffect_repr_events = {
25     nullptr, /* child_added */
26     nullptr, /* child_removed */
27     livepatheffect_on_repr_attr_changed,
28     nullptr, /* content_changed */
29     nullptr  /* order_changed */
30 };
31 
32 
LivePathEffectObject()33 LivePathEffectObject::LivePathEffectObject()
34     : SPObject(), effecttype(Inkscape::LivePathEffect::INVALID_LPE), effecttype_set(false),
35       lpe(nullptr)
36 {
37 #ifdef LIVEPATHEFFECT_VERBOSE
38     g_message("Init livepatheffectobject");
39 #endif
40 }
41 
42 LivePathEffectObject::~LivePathEffectObject() = default;
43 
44 /**
45  * Virtual build: set livepatheffect attributes from its associated XML node.
46  */
build(SPDocument * document,Inkscape::XML::Node * repr)47 void LivePathEffectObject::build(SPDocument *document, Inkscape::XML::Node *repr) {
48     g_assert(SP_IS_OBJECT(this));
49 
50     SPObject::build(document, repr);
51 
52     this->readAttr(SPAttr::PATH_EFFECT);
53 
54     if (repr) {
55         repr->addListener (&livepatheffect_repr_events, this);
56     }
57 
58     /* Register ourselves, is this necessary? */
59 //    document->addResource("path-effect", object);
60 }
61 
62 /**
63  * Virtual release of livepatheffect members before destruction.
64  */
release()65 void LivePathEffectObject::release() {
66     this->getRepr()->removeListenerByData(this);
67 
68 /*
69     if (object->document) {
70         // Unregister ourselves
71         sp_document_removeResource(object->document, "livepatheffect", object);
72     }
73 
74     if (gradient->ref) {
75         gradient->modified_connection.disconnect();
76         gradient->ref->detach();
77         delete gradient->ref;
78         gradient->ref = NULL;
79     }
80 
81     gradient->modified_connection.~connection();
82 */
83 
84     if (this->lpe) {
85         delete this->lpe;
86         this->lpe = nullptr;
87     }
88 
89     this->effecttype = Inkscape::LivePathEffect::INVALID_LPE;
90 
91     SPObject::release();
92 }
93 
94 /**
95  * Virtual set: set attribute to value.
96  */
set(SPAttr key,gchar const * value)97 void LivePathEffectObject::set(SPAttr key, gchar const *value) {
98 #ifdef LIVEPATHEFFECT_VERBOSE
99     g_print("Set livepatheffect");
100 #endif
101 
102     switch (key) {
103         case SPAttr::PATH_EFFECT:
104             if (this->lpe) {
105                 delete this->lpe;
106                 this->lpe = nullptr;
107             }
108 
109             if ( value && Inkscape::LivePathEffect::LPETypeConverter.is_valid_key(value) ) {
110                 this->effecttype = Inkscape::LivePathEffect::LPETypeConverter.get_id_from_key(value);
111                 this->lpe = Inkscape::LivePathEffect::Effect::New(this->effecttype, this);
112                 this->effecttype_set = true;
113             } else {
114                 this->effecttype = Inkscape::LivePathEffect::INVALID_LPE;
115                 this->lpe = nullptr;
116                 this->effecttype_set = false;
117             }
118 
119             this->requestModified(SP_OBJECT_MODIFIED_FLAG);
120             break;
121     }
122 
123     SPObject::set(key, value);
124 }
125 
126 /**
127  * Virtual write: write object attributes to repr.
128  */
write(Inkscape::XML::Document * xml_doc,Inkscape::XML::Node * repr,guint flags)129 Inkscape::XML::Node* LivePathEffectObject::write(Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags) {
130     if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
131         repr = xml_doc->createElement("inkscape:path-effect");
132     }
133 
134     if ((flags & SP_OBJECT_WRITE_ALL) || this->lpe) {
135         repr->setAttributeOrRemoveIfEmpty("effect", Inkscape::LivePathEffect::LPETypeConverter.get_key(this->effecttype));
136 
137         this->lpe->writeParamsToSVG();
138     }
139 
140     SPObject::write(xml_doc, repr, flags);
141 
142     return repr;
143 }
144 
145 static void
livepatheffect_on_repr_attr_changed(Inkscape::XML::Node *,const gchar * key,const gchar *,const gchar * newval,bool,void * data)146 livepatheffect_on_repr_attr_changed ( Inkscape::XML::Node * /*repr*/,
147                                       const gchar *key,
148                                       const gchar */*oldval*/,
149                                       const gchar *newval,
150                                       bool /*is_interactive*/,
151                                       void * data )
152 {
153 #ifdef LIVEPATHEFFECT_VERBOSE
154     g_print("livepatheffect_on_repr_attr_changed");
155 #endif
156 
157     if (!data)
158         return;
159 
160     LivePathEffectObject *lpeobj = (LivePathEffectObject*) data;
161     if (!lpeobj->get_lpe())
162         return;
163 
164     lpeobj->get_lpe()->setParameter(key, newval);
165 
166     lpeobj->requestModified(SP_OBJECT_MODIFIED_FLAG);
167 }
168 
169 // Caution using this function, just compare id and same type of
170 // effect, we use on clipboard to do not fork in same doc on pastepatheffect
is_similar(LivePathEffectObject * that)171 bool LivePathEffectObject::is_similar(LivePathEffectObject *that)
172 {
173     if (that) {
174         const char *thisid = this->getId();
175         const char *thatid = that->getId();
176         if (!thisid || !thatid || strcmp(thisid, thatid) != 0) {
177             return false;
178         }
179         Inkscape::LivePathEffect::Effect *thislpe = this->get_lpe();
180         Inkscape::LivePathEffect::Effect *thatlpe = that->get_lpe();
181         if (thatlpe && thislpe && thislpe->getName() != thatlpe->getName()) {
182             return false;
183         }
184     }
185     return true;
186 }
187 
188 /**
189  * If this has other users, create a new private duplicate and return it
190  * returns 'this' when no forking was necessary (and therefore no duplicate was made)
191  * Check out SPLPEItem::forkPathEffectsIfNecessary !
192  */
fork_private_if_necessary(unsigned int nr_of_allowed_users)193 LivePathEffectObject *LivePathEffectObject::fork_private_if_necessary(unsigned int nr_of_allowed_users)
194 {
195     if (hrefcount > nr_of_allowed_users) {
196         SPDocument *doc = this->document;
197         Inkscape::XML::Document *xml_doc = doc->getReprDoc();
198         Inkscape::XML::Node *dup_repr = this->getRepr()->duplicate(xml_doc);
199 
200         doc->getDefs()->getRepr()->addChild(dup_repr, nullptr);
201         LivePathEffectObject *lpeobj_new = dynamic_cast<LivePathEffectObject *>(doc->getObjectByRepr(dup_repr));
202         Inkscape::GC::release(dup_repr);
203         // To regenerate ID
204         sp_object_ref(lpeobj_new, nullptr);
205         gchar *id = sp_object_get_unique_id(this, nullptr);
206         lpeobj_new->setAttribute("id", id);
207         g_free(id);
208         // Load all volatile vars of forked item
209         sp_object_unref(lpeobj_new, nullptr);
210         return lpeobj_new;
211     }
212     return this;
213 }
214 
215 /*
216   Local Variables:
217   mode:c++
218   c-file-style:"stroustrup"
219   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
220   indent-tabs-mode:nil
221   fill-column:99
222   End:
223 */
224 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :
225