]> git.sesse.net Git - kdenlive/blob - src/effectslist.cpp
f3ac8877b8876cf0721374c42df330774d7063d3
[kdenlive] / src / effectslist.cpp
1 /***************************************************************************
2                           effectslist.cpp  -  description
3                              -------------------
4     begin                : Sat Aug 10 2002
5     copyright            : (C) 2002 by Jason Wood
6     email                : jasonwood@blueyonder.co.uk
7  ***************************************************************************/
8
9 /***************************************************************************
10  *                                                                         *
11  *   This program is free software; you can redistribute it and/or modify  *
12  *   it under the terms of the GNU General Public License as published by  *
13  *   the Free Software Foundation; either version 2 of the License, or     *
14  *   (at your option) any later version.                                   *
15  *                                                                         *
16  ***************************************************************************/
17
18
19 #include "effectslist.h"
20
21 #include <KDebug>
22 #include <KLocale>
23
24
25 EffectsList::EffectsList()
26 {
27     m_baseElement = createElement("list");
28     appendChild(m_baseElement);
29 }
30
31 EffectsList::~EffectsList()
32 {
33 }
34
35 QDomElement EffectsList::getEffectByName(const QString & name) const
36 {
37     QString effectName;
38     QDomNodeList effects = m_baseElement.childNodes();
39     for (int i = 0; i < effects.count(); i++) {
40         QDomElement effect =  effects.at(i).toElement();
41         QDomNode namenode = effect.elementsByTagName("name").item(0);
42         if (!namenode.isNull()) effectName = i18n(namenode.toElement().text().toUtf8().data());
43         if (name == effectName) {
44             QDomNodeList params = effect.elementsByTagName("parameter");
45             for (int i = 0; i < params.count(); i++) {
46                 QDomElement e = params.item(i).toElement();
47                 if (!e.hasAttribute("value"))
48                     e.setAttribute("value", e.attribute("default"));
49             }
50             return effect;
51         }
52     }
53
54     return QDomElement();
55 }
56
57 QDomElement EffectsList::getEffectByTag(const QString & tag, const QString & id) const
58 {
59     QDomNodeList effects = m_baseElement.childNodes();
60     for (int i = 0; i < effects.count(); i++) {
61         QDomElement effect =  effects.at(i).toElement();
62         if (!id.isEmpty()) {
63             if (effect.attribute("id") == id) {
64                 QDomNodeList params = effect.elementsByTagName("parameter");
65                 for (int i = 0; i < params.count(); i++) {
66                     QDomElement e = params.item(i).toElement();
67                     if (!e.hasAttribute("value"))
68                         e.setAttribute("value", e.attribute("default"));
69                 }
70                 return effect;
71             }
72         } else if (!tag.isEmpty()) {
73             if (effect.attribute("tag") == tag) {
74                 QDomNodeList params = effect.elementsByTagName("parameter");
75                 for (int i = 0; i < params.count(); i++) {
76                     QDomElement e = params.item(i).toElement();
77                     if (!e.hasAttribute("value"))
78                         e.setAttribute("value", e.attribute("default"));
79                 }
80                 return effect;
81             }
82         }
83     }
84     return QDomElement();
85 }
86
87 int EffectsList::hasEffect(const QString & tag, const QString & id) const
88 {
89     QDomNodeList effects = m_baseElement.childNodes();
90     for (int i = 0; i < effects.count(); i++) {
91         QDomElement effect =  effects.at(i).toElement();
92         if (!id.isEmpty()) {
93             if (effect.attribute("id") == id) return i;
94         } else if (!tag.isEmpty() && effect.attribute("tag") == tag) return i;
95     }
96     return -1;
97 }
98
99 QStringList EffectsList::effectIdInfo(const int ix) const
100 {
101     QStringList info;
102     QDomElement effect = m_baseElement.childNodes().at(ix).toElement();
103     QDomNode namenode = effect.elementsByTagName("name").item(0);
104     info << i18n(namenode.toElement().text().toUtf8().data()) << effect.attribute("tag") << effect.attribute("id");
105     return info;
106 }
107
108 QStringList EffectsList::effectNames()
109 {
110     QStringList list;
111     QDomNodeList effects = m_baseElement.childNodes();
112     for (int i = 0; i < effects.count(); i++) {
113         QDomElement effect =  effects.at(i).toElement();
114         QDomNode namenode = effect.elementsByTagName("name").item(0);
115         if (!namenode.isNull()) list.append(i18n(namenode.toElement().text().toUtf8().data()));
116     }
117     return list;
118 }
119
120 QString EffectsList::getInfo(const QString & tag, const QString & id) const
121 {
122     QString info;
123     QDomElement effect = getEffectByTag(tag, id);
124     QDomNode namenode = effect.elementsByTagName("description").item(0);
125     if (!namenode.isNull())
126         info = i18n(namenode.firstChild().nodeValue().simplified().toUtf8().data());
127
128     namenode = effect.elementsByTagName("author").item(0);
129     if (!namenode.isNull())
130         info.append("<br /><strong>" + i18n("Author:") + " </strong>" + i18n(namenode.toElement().text().toUtf8().data()));
131
132     return info;
133 }
134
135 QString EffectsList::getInfoFromIndex(const int ix) const
136 {
137     QString info;
138     QDomElement effect = m_baseElement.childNodes().at(ix).toElement();
139     QDomNode namenode = effect.elementsByTagName("description").item(0);
140     if (!namenode.isNull()) info = i18n(namenode.toElement().text().toUtf8().data());
141     namenode = effect.elementsByTagName("author").item(0);
142     if (!namenode.isNull()) info.append("<br /><strong>" + i18n("Author:") + " </strong>" + i18n(namenode.toElement().text().toUtf8().data()));
143     return info;
144 }
145
146 // static
147 bool EffectsList::hasKeyFrames(QDomElement effect)
148 {
149     QDomNodeList params = effect.elementsByTagName("parameter");
150     for (int i = 0; i < params.count(); i++) {
151         QDomElement e = params.item(i).toElement();
152         if (e.attribute("type") == "keyframe") return true;
153     }
154     return false;
155 }
156
157 // static
158 bool EffectsList::hasSimpleKeyFrames(QDomElement effect)
159 {
160     QDomNodeList params = effect.elementsByTagName("parameter");
161     for (int i = 0; i < params.count(); i++) {
162         QDomElement e = params.item(i).toElement();
163         if (e.attribute("type") == "simplekeyframe") return true;
164     }
165     return false;
166 }
167
168 // static
169 bool EffectsList::hasGeometryKeyFrames(QDomElement effect)
170 {
171     QDomNodeList params = effect.elementsByTagName("parameter");
172     for (int i = 0; i < params.count(); ++i) {
173         QDomElement param = params.item(i).toElement();
174         if (param.attribute("type") == "geometry" && !param.hasAttribute("fixed"))
175             return true;
176     }
177     return false;
178 }
179
180 void EffectsList::clone(const EffectsList original)
181 {
182     setContent(original.toString());
183     m_baseElement = documentElement();
184 }
185
186 void EffectsList::clearList()
187 {
188     while (!m_baseElement.firstChild().isNull())
189         m_baseElement.removeChild(m_baseElement.firstChild());
190 }
191
192 // static
193 void EffectsList::setParameter(QDomElement effect, const QString &name, const QString &value)
194 {
195     QDomNodeList params = effect.elementsByTagName("parameter");
196     for (int i = 0; i < params.count(); i++) {
197         QDomElement e = params.item(i).toElement();
198         if (e.attribute("name") == name) {
199             e.setAttribute("value", value);
200             break;
201         }
202     }
203 }
204
205 // static
206 QString EffectsList::parameter(QDomElement effect, const QString &name)
207 {
208     QDomNodeList params = effect.elementsByTagName("parameter");
209     for (int i = 0; i < params.count(); i++) {
210         QDomElement e = params.item(i).toElement();
211         if (e.attribute("name") == name) {
212             return e.attribute("value");
213         }
214     }
215     return QString();
216 }
217
218 // static
219 void EffectsList::setProperty(QDomElement effect, const QString &name, const QString &value)
220 {
221     QDomNodeList params = effect.elementsByTagName("property");
222     // Update property if it already exists
223     for (int i = 0; i < params.count(); i++) {
224         QDomElement e = params.item(i).toElement();
225         if (e.attribute("name") == name) {
226             e.firstChild().setNodeValue(value);
227             break;
228         }
229     }
230 }
231
232 // static
233 void EffectsList::renameProperty(QDomElement effect, const QString &oldName, const QString &newName)
234 {
235     QDomNodeList params = effect.elementsByTagName("property");
236     // Update property if it already exists
237     for (int i = 0; i < params.count(); i++) {
238         QDomElement e = params.item(i).toElement();
239         if (e.attribute("name") == oldName) {
240             e.setAttribute("name", newName);
241             break;
242         }
243     }
244 }
245
246 // static
247 QString EffectsList::property(QDomElement effect, const QString &name)
248 {
249     QDomNodeList params = effect.elementsByTagName("property");
250     for (int i = 0; i < params.count(); i++) {
251         QDomElement e = params.item(i).toElement();
252         if (e.attribute("name") == name) {
253             return e.firstChild().nodeValue();
254         }
255     }
256     return QString();
257 }
258
259 // static
260 void EffectsList::removeProperty(QDomElement effect, const QString &name)
261 {
262     QDomNodeList params = effect.elementsByTagName("property");
263     for (int i = 0; i < params.count(); i++) {
264         QDomElement e = params.item(i).toElement();
265         if (e.attribute("name") == name) {
266             effect.removeChild(params.item(i));
267             break;
268         }
269     }
270 }
271
272 void EffectsList::append(QDomElement e)
273 {
274     m_baseElement.appendChild(importNode(e, true));
275 }
276
277 int EffectsList::count() const
278 {
279     return m_baseElement.childNodes().count();
280 }
281
282 bool EffectsList::isEmpty() const
283 {
284     return m_baseElement.childNodes().count() == 0;
285 }
286
287 const QDomElement EffectsList::at(int ix) const
288 {
289     QDomNodeList effects = m_baseElement.childNodes();
290     if (ix < 0 || ix >= effects.count()) return QDomElement();
291     return effects.at(ix).toElement();
292 }
293
294 void EffectsList::removeAt(int ix)
295 {
296     QDomNodeList effects = m_baseElement.childNodes();
297     if (ix < 0 || ix >= effects.count()) return;
298     m_baseElement.removeChild(effects.at(ix));
299 }
300
301 QDomElement EffectsList::item(int ix)
302 {
303     QDomNodeList effects = m_baseElement.childNodes();
304     if (ix < 0 || ix >= effects.count()) return QDomElement();
305     return effects.at(ix).toElement();
306 }
307
308 void EffectsList::insert(int ix, QDomElement effect)
309 {
310     QDomNodeList effects = m_baseElement.childNodes();
311     if (ix < 0) ix = 0;
312     if (ix >= effects.count()) m_baseElement.appendChild(importNode(effect, true));
313     else m_baseElement.insertBefore(importNode(effect, true), effects.at(ix));
314 }
315
316 void EffectsList::replace(int ix, QDomElement effect)
317 {
318     QDomNodeList effects = m_baseElement.childNodes();
319     if (ix < 0) ix = 0;
320     if (ix < effects.count()) m_baseElement.removeChild(effects.at(ix));
321     if (ix == effects.count()) m_baseElement.appendChild(importNode(effect, true));
322     else m_baseElement.insertBefore(importNode(effect, true), effects.at(ix));
323 }