1 /***************************************************************************
2 effectslist.cpp - description
4 begin : Sat Aug 10 2002
5 copyright : (C) 2002 by Jason Wood
6 email : jasonwood@blueyonder.co.uk
7 ***************************************************************************/
9 /***************************************************************************
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. *
16 ***************************************************************************/
19 #include "effectslist.h"
25 EffectsList::EffectsList(bool indexRequired) : m_useIndex(indexRequired)
27 m_baseElement = createElement("list");
28 appendChild(m_baseElement);
31 EffectsList::~EffectsList()
35 QDomElement EffectsList::getEffectByName(const QString & name) const
38 QDomNodeList effects = m_baseElement.childNodes();
39 for (int i = 0; i < effects.count(); i++) {
40 QDomElement effect = effects.at(i).toElement();
41 QDomElement namenode = effect.firstChildElement("name");
42 if (!namenode.isNull()) effectName = i18n(namenode.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"));
57 QDomElement EffectsList::getEffectByTag(const QString & tag, const QString & id) const
59 QDomNodeList effects = m_baseElement.childNodes();
60 for (int i = 0; i < effects.count(); i++) {
61 QDomElement effect = effects.at(i).toElement();
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"));
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"));
87 int EffectsList::hasEffect(const QString & tag, const QString & id) const
89 QDomNodeList effects = m_baseElement.childNodes();
90 for (int i = 0; i < effects.count(); i++) {
91 QDomElement effect = effects.at(i).toElement();
93 if (effect.attribute("id") == id) return i;
94 } else if (!tag.isEmpty() && effect.attribute("tag") == tag) return i;
99 QStringList EffectsList::effectIdInfo(const int ix) const
102 QDomElement effect = m_baseElement.childNodes().at(ix).toElement();
103 QDomElement namenode = effect.firstChildElement("name");
104 info << i18n(namenode.text().toUtf8().data()) << effect.attribute("tag") << effect.attribute("id");
108 QStringList EffectsList::effectNames()
111 QDomNodeList effects = m_baseElement.childNodes();
112 for (int i = 0; i < effects.count(); i++) {
113 QDomElement effect = effects.at(i).toElement();
114 QDomElement namenode = effect.firstChildElement("name");
115 if (!namenode.isNull()) list.append(i18n(namenode.text().toUtf8().data()));
120 QString EffectsList::getInfo(const QString & tag, const QString & id) const
123 return getEffectInfo(getEffectByTag(tag, id));
126 QString EffectsList::getInfoFromIndex(const int ix) const
129 return getEffectInfo(m_baseElement.childNodes().at(ix).toElement());
132 QString EffectsList::getEffectInfo(const QDomElement effect) const
135 QDomElement namenode = effect.firstChildElement("description");
136 if (!namenode.isNull())
137 info = i18n(namenode.firstChild().nodeValue().simplified().toUtf8().data());
139 namenode = effect.firstChildElement("author");
140 if (!namenode.isNull())
141 info.append("<br /><strong>" + i18n("Author:") + " </strong>" + i18n(namenode.text().toUtf8().data()));
143 namenode = effect.firstChildElement("version");
144 if (!namenode.isNull())
145 info.append(QString(" (%1)").arg(namenode.text()));
151 bool EffectsList::hasKeyFrames(QDomElement effect)
153 QDomNodeList params = effect.elementsByTagName("parameter");
154 for (int i = 0; i < params.count(); i++) {
155 QDomElement e = params.item(i).toElement();
156 if (e.attribute("type") == "keyframe") return true;
162 bool EffectsList::hasSimpleKeyFrames(QDomElement effect)
164 QDomNodeList params = effect.elementsByTagName("parameter");
165 for (int i = 0; i < params.count(); i++) {
166 QDomElement e = params.item(i).toElement();
167 if (e.attribute("type") == "simplekeyframe") return true;
173 bool EffectsList::hasGeometryKeyFrames(QDomElement effect)
175 QDomNodeList params = effect.elementsByTagName("parameter");
176 for (int i = 0; i < params.count(); ++i) {
177 QDomElement param = params.item(i).toElement();
178 if (param.attribute("type") == "geometry" && !param.hasAttribute("fixed"))
184 void EffectsList::clone(const EffectsList &original)
186 setContent(original.toString());
187 m_baseElement = documentElement();
190 void EffectsList::clearList()
192 while (!m_baseElement.firstChild().isNull())
193 m_baseElement.removeChild(m_baseElement.firstChild());
197 void EffectsList::setParameter(QDomElement effect, const QString &name, const QString &value)
199 QDomNodeList params = effect.elementsByTagName("parameter");
200 for (int i = 0; i < params.count(); i++) {
201 QDomElement e = params.item(i).toElement();
202 if (e.attribute("name") == name) {
203 e.setAttribute("value", value);
210 QString EffectsList::parameter(QDomElement effect, const QString &name)
212 QDomNodeList params = effect.elementsByTagName("parameter");
213 for (int i = 0; i < params.count(); i++) {
214 QDomElement e = params.item(i).toElement();
215 if (e.attribute("name") == name) {
216 return e.attribute("value");
223 void EffectsList::setProperty(QDomElement effect, const QString &name, const QString &value)
225 QDomNodeList params = effect.elementsByTagName("property");
226 // Update property if it already exists
228 for (int i = 0; i < params.count(); i++) {
229 QDomElement e = params.item(i).toElement();
230 if (e.attribute("name") == name) {
231 e.firstChild().setNodeValue(value);
238 QDomDocument doc = effect.ownerDocument();
239 QDomElement e = doc.createElement("property");
240 e.setAttribute("name", name);
241 QDomText val = doc.createTextNode(value);
243 effect.appendChild(e);
248 void EffectsList::renameProperty(QDomElement effect, const QString &oldName, const QString &newName)
250 QDomNodeList params = effect.elementsByTagName("property");
251 // Update property if it already exists
252 for (int i = 0; i < params.count(); i++) {
253 QDomElement e = params.item(i).toElement();
254 if (e.attribute("name") == oldName) {
255 e.setAttribute("name", newName);
262 QString EffectsList::property(QDomElement effect, const QString &name)
264 QDomNodeList params = effect.elementsByTagName("property");
265 for (int i = 0; i < params.count(); i++) {
266 QDomElement e = params.item(i).toElement();
267 if (e.attribute("name") == name) {
268 return e.firstChild().nodeValue();
275 void EffectsList::removeProperty(QDomElement effect, const QString &name)
277 QDomNodeList params = effect.elementsByTagName("property");
278 for (int i = 0; i < params.count(); i++) {
279 QDomElement e = params.item(i).toElement();
280 if (e.attribute("name") == name) {
281 effect.removeChild(params.item(i));
288 void EffectsList::removeMetaProperties(QDomElement producer)
290 QDomNodeList params = producer.elementsByTagName("property");
291 for (int i = 0; i < params.count(); i++) {
292 QDomElement e = params.item(i).toElement();
293 if (e.attribute("name").startsWith("meta")) {
294 producer.removeChild(params.item(i));
300 void EffectsList::append(QDomElement e)
303 m_baseElement.appendChild(importNode(e, true));
304 if (m_useIndex) updateIndexes(m_baseElement.childNodes());
308 int EffectsList::count() const
310 return m_baseElement.childNodes().count();
313 bool EffectsList::isEmpty() const
315 return !m_baseElement.hasChildNodes();
318 const QDomElement EffectsList::at(int ix) const
320 QDomNodeList effects = m_baseElement.childNodes();
321 if (ix < 0 || ix >= effects.count()) return QDomElement();
322 return effects.at(ix).toElement();
325 void EffectsList::removeAt(int ix)
327 QDomNodeList effects = m_baseElement.childNodes();
328 if (ix <= 0 || ix > effects.count()) return;
329 m_baseElement.removeChild(effects.at(ix - 1));
330 if (m_useIndex) updateIndexes(effects);
333 QDomElement EffectsList::itemFromIndex(int ix) const
335 QDomNodeList effects = m_baseElement.childNodes();
336 if (ix <= 0 || ix > effects.count()) return QDomElement();
337 return effects.at(ix - 1).toElement();
340 void EffectsList::insert(QDomElement effect)
342 QDomNodeList effects = m_baseElement.childNodes();
343 int ix = effect.attribute("kdenlive_ix").toInt();
344 if (ix > effects.count()) m_baseElement.appendChild(importNode(effect, true));
346 QDomElement listeffect = effects.at(ix - 1).toElement();
347 m_baseElement.insertBefore(importNode(effect, true), listeffect);
349 if (m_useIndex) updateIndexes(effects);
352 void EffectsList::updateIndexes(QDomNodeList effects)
354 for (int i = 0; i < effects.count(); i++) {
355 QDomElement listeffect = effects.at(i).toElement();
356 listeffect.setAttribute("kdenlive_ix", i + 1);
360 QDomElement EffectsList::effectFromIndex(QDomNodeList effects, int ix)
362 if (ix <= 0 || ix > effects.count()) return QDomElement();
363 return effects.at(ix - 1).toElement();
366 void EffectsList::updateEffect(QDomElement effect)
368 QDomNodeList effects = m_baseElement.childNodes();
369 int ix = effect.attribute("kdenlive_ix").toInt();
370 QDomElement current = effectFromIndex(effects, ix);
371 if (!current.isNull()) {
372 m_baseElement.insertBefore(importNode(effect, true), current);
373 m_baseElement.removeChild(current);
375 else m_baseElement.appendChild(importNode(effect, true));