]> git.sesse.net Git - kdenlive/commitdiff
Add an easier way to handle simple parameter updates.
authorTill Theato <root@ttill.de>
Thu, 8 Sep 2011 15:18:40 +0000 (15:18 +0000)
committerTill Theato <root@ttill.de>
Thu, 8 Sep 2011 15:18:40 +0000 (15:18 +0000)
(see frei0r.levels.js)

svn path=/trunk/kdenlive/; revision=5864

effects/update/frei0r.levels.js
src/documentvalidator.cpp
src/documentvalidator.h

index 279ae2fa4c0541e5cb9eb91e19c9303276692c76..36d5d052ca362e92d7c77041ede1c4326548a9c7 100644 (file)
@@ -1,23 +1,12 @@
 
-function update(serviceVersion, effectVersion, effectString) {
-    var locale = new QLocale();
-    var doc = new QDomDocument();
-    doc.setContent(effectString);
-    for (var node = doc.documentElement().firstChild(); !node.isNull(); node = node.nextSibling()) {
-        var effectparam = node.toElement();
-        if (effectparam.attribute("name") == "Channel" || effectparam.attribute("name") == "Histogram position") {
-            if (serviceVersion < effectVersion) {
-                // downgrade
-                if (effectVersion > 0.1) {
-                    effectparam.firstChild().toText().setData(locale.toString(effectparam.text() * 10));
-                }
-            } else {
-                // upgrade
-                if (effectVersion < 0.2) { 
-                    effectparam.firstChild().toText().setData(locale.toString(effectparam.text() / 10.));
-                }
-            }
-        }
-    }
-    return doc.toString();
+var update = new Object();
+
+update["Channel"] = new Array(new Array(0.2, function(v, d) { return this.upd1(v, d); }));
+update["Histogram position"] = update["Channel"];
+
+function upd1(value, isDowngrade) {
+    if (isDowngrade)
+        return value * 10;
+    else
+        return value / 10.;
 }
index 52bb70ca1a1b45d7fe1b1456fccb8852d57ebba0..3722a0d929c822efec099ff51b03410c100ebd5b 100644 (file)
@@ -1031,7 +1031,7 @@ bool DocumentValidator::isModified() const
 
 void DocumentValidator::updateEffects()
 {
-    // WARNING: order by findDirs will determine which js file to use (in case multiple for the same filter exist)
+    // WARNING: order by findDirs will determine which js file to use (in case multiple scripts for the same filter exist)
     QMap <QString, KUrl> paths;
 #if QT_VERSION >= 0x040700
     QMap <QString, QScriptProgram> scripts;
@@ -1087,25 +1087,61 @@ void DocumentValidator::updateEffects()
                     scripts.insert(effectId, scriptProgram);
                 }
 
-                QDomDocument scriptDoc;
-                scriptDoc.appendChild(scriptDoc.importNode(effect, true));
-
                 QScriptEngine scriptEngine;
                 scriptEngine.importExtension("qt.core");
                 scriptEngine.importExtension("qt.xml");
                 scriptEngine.evaluate(scripts.value(effectId));
-                QString effectString = scriptEngine.globalObject().property("update").call(QScriptValue(), QScriptValueList()  << serviceVersion << effectVersion << scriptDoc.toString()).toString();
-
-                if (!effectString.isEmpty()) {
-                    scriptDoc.setContent(effectString);
-                    QDomNode updatedEffect = effect.ownerDocument().importNode(scriptDoc.documentElement(), true);
-                    effect.parentNode().replaceChild(updatedEffect, effect);
-                    // TODO: set version to avoid dependency on latest MLT
-                    m_modified = true;
+                QScriptValue updateRules = scriptEngine.globalObject().property("update");
+                if (!updateRules.isValid())
+                    continue;
+                if (updateRules.isFunction()) {
+                    QDomDocument scriptDoc;
+                    scriptDoc.appendChild(scriptDoc.importNode(effect, true));
+
+                    QString effectString = updateRules.call(QScriptValue(), QScriptValueList()  << serviceVersion << effectVersion << scriptDoc.toString()).toString();
+
+                    if (!effectString.isEmpty()) {
+                        scriptDoc.setContent(effectString);
+                        QDomNode updatedEffect = effect.ownerDocument().importNode(scriptDoc.documentElement(), true);
+                        effect.parentNode().replaceChild(updatedEffect, effect);
+                        m_modified = true;
+                    }
+                } else {
+                    m_modified = updateEffectParameters(effect.childNodes(), &updateRules, serviceVersion, effectVersion);
                 }
             }
         }
     }
 }
 
-
+bool DocumentValidator::updateEffectParameters(QDomNodeList parameters, const QScriptValue* updateRules, const double serviceVersion, const double effectVersion)
+{
+    bool updated = false;
+    bool isDowngrade = serviceVersion < effectVersion;
+    for (int i = 0; i < parameters.count(); ++i) {
+        QDomElement parameter = parameters.at(i).toElement();
+        QScriptValue rules = updateRules->property(parameter.attribute("name"));
+        if (rules.isValid() && rules.isArray()) {
+            int rulesCount = rules.property("length").toInt32();
+            if (isDowngrade) {
+                // start with the highest version and downgrade step by step
+                for (int j = rulesCount - 1; j >= 0; --j) {
+                    double version = rules.property(j).property(0).toNumber();
+                    if (version <= effectVersion && version > serviceVersion) {
+                        parameter.firstChild().setNodeValue(rules.property(j).property(1).call(QScriptValue(), QScriptValueList() << parameter.text() << isDowngrade).toString());
+                        updated = true;
+                    }
+                }
+            } else {
+                for (int j = 0; j < rulesCount; ++j) {
+                    double version = rules.property(j).property(0).toNumber();
+                    if (version > effectVersion && version <= serviceVersion) {
+                        parameter.firstChild().setNodeValue(rules.property(j).property(1).call(QScriptValue(), QScriptValueList() << parameter.text() << isDowngrade).toString());
+                        updated = true;
+                    }
+                }
+            }
+        }
+    }
+    return updated;
+}
index dd15c1e35a547b5679e54e4bffddf5d75bf03d4e..5708f5fcae38ec466b7fbbe538aac399ec8620c1 100644 (file)
@@ -24,6 +24,9 @@
 #include <QDomDocument>
 #include <QColor>
 
+class QScriptValue;
+
+
 class DocumentValidator
 {
 
@@ -39,7 +42,11 @@ private:
     bool upgrade(double version, const double currentVersion);
     QStringList getInfoFromEffectName(const QString oldName);
     QString colorToString(const QColor& c);
+    /** @brief Updates effects that were created using a different version of the underlaying filter than the one installed. */
     void updateEffects();
+    /** @brief Updates the parameters according to the updateRules.
+     * @see the related in README in effects/update */
+    bool updateEffectParameters(QDomNodeList parameters, const QScriptValue *updateRules, const double serviceVersion, const double effectVersion);
 };
 
 #endif