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"));
58 void EffectsList::initEffect(QDomElement effect) const
60 QDomNodeList params = effect.elementsByTagName("parameter");
61 for (int i = 0; i < params.count(); i++) {
62 QDomElement e = params.item(i).toElement();
63 if (!e.hasAttribute("value"))
64 e.setAttribute("value", e.attribute("default"));
68 QDomElement EffectsList::getEffectByTag(const QString & tag, const QString & id) const
70 QDomNodeList effects = m_baseElement.childNodes();
71 for (int i = 0; i < effects.count(); i++) {
72 QDomElement effect = effects.at(i).toElement();
74 if (effect.attribute("id") == id) {
75 if (effect.tagName() == "effectgroup") {
77 QDomNodeList subeffects = effect.elementsByTagName("effect");
78 for (int j = 0; j < subeffects.count(); j++) {
79 QDomElement sub = subeffects.at(j).toElement();
83 else initEffect(effect);
86 } else if (!tag.isEmpty()) {
87 if (effect.attribute("tag") == tag) {
96 int EffectsList::hasEffect(const QString & tag, const QString & id) const
98 QDomNodeList effects = m_baseElement.childNodes();
99 for (int i = 0; i < effects.count(); i++) {
100 QDomElement effect = effects.at(i).toElement();
102 if (effect.attribute("id") == id) return effect.attribute("kdenlive_ix").toInt();
103 } else if (!tag.isEmpty() && effect.attribute("tag") == tag) return effect.attribute("kdenlive_ix").toInt();
108 QStringList EffectsList::effectIdInfo(const int ix) const
111 QDomElement effect = m_baseElement.childNodes().at(ix).toElement();
112 if (effect.tagName() == "effectgroup") {
113 QString groupName = effect.attribute("name");
114 info << groupName << groupName << effect.attribute("id") << QString::number(Kdenlive::groupEffect);
117 QDomElement namenode = effect.firstChildElement("name");
118 info << i18n(namenode.text().toUtf8().data()) << effect.attribute("tag") << effect.attribute("id");
123 QStringList EffectsList::effectNames()
126 QDomNodeList effects = m_baseElement.childNodes();
127 for (int i = 0; i < effects.count(); i++) {
128 QDomElement effect = effects.at(i).toElement();
129 QDomElement namenode = effect.firstChildElement("name");
130 if (!namenode.isNull()) list.append(i18n(namenode.text().toUtf8().data()));
135 QString EffectsList::getInfo(const QString & tag, const QString & id) const
138 return getEffectInfo(getEffectByTag(tag, id));
141 QString EffectsList::getInfoFromIndex(const int ix) const
144 return getEffectInfo(m_baseElement.childNodes().at(ix).toElement());
147 QString EffectsList::getEffectInfo(const QDomElement effect) const
150 QDomElement namenode = effect.firstChildElement("description");
151 if (!namenode.isNull())
152 info = i18n(namenode.firstChild().nodeValue().simplified().toUtf8().data());
154 namenode = effect.firstChildElement("author");
155 if (!namenode.isNull())
156 info.append("<br /><strong>" + i18n("Author:") + " </strong>" + i18n(namenode.text().toUtf8().data()));
158 namenode = effect.firstChildElement("version");
159 if (!namenode.isNull())
160 info.append(QString(" (%1)").arg(namenode.text()));
166 bool EffectsList::hasKeyFrames(QDomElement effect)
168 QDomNodeList params = effect.elementsByTagName("parameter");
169 for (int i = 0; i < params.count(); i++) {
170 QDomElement e = params.item(i).toElement();
171 if (e.attribute("type") == "keyframe") return true;
177 bool EffectsList::hasSimpleKeyFrames(QDomElement effect)
179 QDomNodeList params = effect.elementsByTagName("parameter");
180 for (int i = 0; i < params.count(); i++) {
181 QDomElement e = params.item(i).toElement();
182 if (e.attribute("type") == "simplekeyframe") return true;
188 bool EffectsList::hasGeometryKeyFrames(QDomElement effect)
190 QDomNodeList params = effect.elementsByTagName("parameter");
191 for (int i = 0; i < params.count(); ++i) {
192 QDomElement param = params.item(i).toElement();
193 if (param.attribute("type") == "geometry" && !param.hasAttribute("fixed"))
199 void EffectsList::clone(const EffectsList &original)
201 setContent(original.toString());
202 m_baseElement = documentElement();
205 void EffectsList::clearList()
207 while (!m_baseElement.firstChild().isNull())
208 m_baseElement.removeChild(m_baseElement.firstChild());
212 void EffectsList::setParameter(QDomElement effect, const QString &name, const QString &value)
214 QDomNodeList params = effect.elementsByTagName("parameter");
216 for (int i = 0; i < params.count(); i++) {
217 QDomElement e = params.item(i).toElement();
218 if (e.attribute("name") == name) {
219 e.setAttribute("value", value);
226 QDomDocument doc = effect.ownerDocument();
227 QDomElement e = doc.createElement("parameter");
228 e.setAttribute("name", name);
229 QDomText val = doc.createTextNode(value);
231 effect.appendChild(e);
236 QString EffectsList::parameter(QDomElement effect, const QString &name)
238 QDomNodeList params = effect.elementsByTagName("parameter");
239 for (int i = 0; i < params.count(); i++) {
240 QDomElement e = params.item(i).toElement();
241 if (e.attribute("name") == name) {
242 return e.attribute("value");
249 void EffectsList::setProperty(QDomElement effect, const QString &name, const QString &value)
251 QDomNodeList params = effect.elementsByTagName("property");
252 // Update property if it already exists
254 for (int i = 0; i < params.count(); i++) {
255 QDomElement e = params.item(i).toElement();
256 if (e.attribute("name") == name) {
257 e.firstChild().setNodeValue(value);
264 QDomDocument doc = effect.ownerDocument();
265 QDomElement e = doc.createElement("property");
266 e.setAttribute("name", name);
267 QDomText val = doc.createTextNode(value);
269 effect.appendChild(e);
274 void EffectsList::renameProperty(QDomElement effect, const QString &oldName, const QString &newName)
276 QDomNodeList params = effect.elementsByTagName("property");
277 // Update property if it already exists
278 for (int i = 0; i < params.count(); i++) {
279 QDomElement e = params.item(i).toElement();
280 if (e.attribute("name") == oldName) {
281 e.setAttribute("name", newName);
288 QString EffectsList::property(QDomElement effect, const QString &name)
290 QDomNodeList params = effect.elementsByTagName("property");
291 for (int i = 0; i < params.count(); i++) {
292 QDomElement e = params.item(i).toElement();
293 if (e.attribute("name") == name) {
294 return e.firstChild().nodeValue();
301 void EffectsList::removeProperty(QDomElement effect, const QString &name)
303 QDomNodeList params = effect.elementsByTagName("property");
304 for (int i = 0; i < params.count(); i++) {
305 QDomElement e = params.item(i).toElement();
306 if (e.attribute("name") == name) {
307 effect.removeChild(params.item(i));
314 void EffectsList::removeMetaProperties(QDomElement producer)
316 QDomNodeList params = producer.elementsByTagName("property");
317 for (int i = 0; i < params.count(); i++) {
318 QDomElement e = params.item(i).toElement();
319 if (e.attribute("name").startsWith("meta")) {
320 producer.removeChild(params.item(i));
326 QDomElement EffectsList::append(QDomElement e)
330 result = m_baseElement.appendChild(importNode(e, true)).toElement();
332 updateIndexes(m_baseElement.childNodes(), m_baseElement.childNodes().count() - 1);
338 int EffectsList::count() const
340 return m_baseElement.childNodes().count();
343 bool EffectsList::isEmpty() const
345 return !m_baseElement.hasChildNodes();
348 const QDomElement EffectsList::at(int ix) const
350 QDomNodeList effects = m_baseElement.childNodes();
351 if (ix < 0 || ix >= effects.count()) return QDomElement();
352 return effects.at(ix).toElement();
355 void EffectsList::removeAt(int ix)
357 QDomNodeList effects = m_baseElement.childNodes();
358 if (ix <= 0 || ix > effects.count()) return;
359 m_baseElement.removeChild(effects.at(ix - 1));
360 if (m_useIndex) updateIndexes(effects, ix - 1);
363 QDomElement EffectsList::itemFromIndex(int ix) const
365 QDomNodeList effects = m_baseElement.childNodes();
366 if (ix <= 0 || ix > effects.count()) return QDomElement();
367 return effects.at(ix - 1).toElement();
370 QDomElement EffectsList::insert(QDomElement effect)
372 QDomNodeList effects = m_baseElement.childNodes();
373 int ix = effect.attribute("kdenlive_ix").toInt();
375 if (ix <= 0 || ix > effects.count()) {
376 ix = effects.count();
377 result = m_baseElement.appendChild(importNode(effect, true)).toElement();
380 QDomElement listeffect = effects.at(ix - 1).toElement();
381 result = m_baseElement.insertBefore(importNode(effect, true), listeffect).toElement();
383 if (m_useIndex && ix > 0)
384 updateIndexes(effects, ix - 1);
388 void EffectsList::updateIndexes(QDomNodeList effects, int startIndex)
390 for (int i = startIndex; i < effects.count(); i++) {
391 QDomElement listeffect = effects.at(i).toElement();
392 listeffect.setAttribute("kdenlive_ix", i + 1);
396 void EffectsList::enableEffects(QList <int> indexes, bool disable)
398 QDomNodeList effects = m_baseElement.childNodes();
400 for (int i = 0; i < indexes.count(); i++) {
401 effect = effectFromIndex(effects, indexes.at(i));
402 effect.setAttribute("disable", (int) disable);
406 QDomElement EffectsList::effectFromIndex(QDomNodeList effects, int ix)
408 if (ix <= 0 || ix > effects.count()) return QDomElement();
409 return effects.at(ix - 1).toElement();
412 void EffectsList::updateEffect(QDomElement effect)
414 QDomNodeList effects = m_baseElement.childNodes();
415 int ix = effect.attribute("kdenlive_ix").toInt();
416 QDomElement current = effectFromIndex(effects, ix);
417 if (!current.isNull()) {
418 m_baseElement.insertBefore(importNode(effect, true), current);
419 m_baseElement.removeChild(current);
421 else m_baseElement.appendChild(importNode(effect, true));