]> git.sesse.net Git - kdenlive/blobdiff - src/documentvalidator.cpp
also move position from <kdenlivedoc /> to <documentproperties /> when loading an...
[kdenlive] / src / documentvalidator.cpp
index fd10011e6db9ecbb8adc1f13c9f6489ed25b7145..e28d3d1c72511e1bc9c403be1046798d5a324423 100644 (file)
@@ -26,6 +26,7 @@
 #include <KApplication>
 #include <KLocale>
 
+#include <QFile>
 #include <QColor>
 
 DocumentValidator::DocumentValidator(QDomDocument doc):
@@ -45,7 +46,7 @@ bool DocumentValidator::validate(const double currentVersion)
     if (!upgrade(kdenlivedocElm.attribute("version").toDouble(), currentVersion))
         return false;
 
-       /*
+    /*
      * Check the syntax (this will be replaced by XSD validation with Qt 4.6)
      * and correct some errors
      */
@@ -79,8 +80,8 @@ bool DocumentValidator::validate(const double currentVersion)
         tracksMax = qMax(tracksinfo.count(), tracksMax);
         tracksMax = qMax(1, tracksMax); // Force existance of one track
         if (playlists.count() - 1 < tracksMax ||
-            tracks.count() - 1 < tracksMax ||
-            tracksinfo.count() < tracksMax) {
+                tracks.count() - 1 < tracksMax ||
+                tracksinfo.count() < tracksMax) {
             m_modified = true;
             int difference;
             if (playlists.count() - 1 < tracksMax) {
@@ -126,8 +127,10 @@ bool DocumentValidator::upgrade(double version, const double currentVersion)
     kDebug() << "Opening a document with version " << version;
 
     // No conversion needed
-    if (version == currentVersion)
-        return true;
+    if (version == currentVersion) {
+        // TODO: uncomment when currentVersion == 0.84
+        //return true;
+    }
 
     // The document is too new
     if (version > currentVersion) {
@@ -136,10 +139,6 @@ bool DocumentValidator::upgrade(double version, const double currentVersion)
         return false;
     }
 
-    // <kdenlivedoc />
-    QDomNode infoXmlNode = m_doc.elementsByTagName("kdenlivedoc").at(0);
-    QDomElement infoXml = infoXmlNode.toElement();
-
     // Unsupported document versions
     if (version == 0.5 || version == 0.7) {
         kDebug() << "Unable to open document with version " << version;
@@ -147,6 +146,10 @@ bool DocumentValidator::upgrade(double version, const double currentVersion)
         return false;
     }
 
+    // <kdenlivedoc />
+    QDomNode infoXmlNode = m_doc.elementsByTagName("kdenlivedoc").at(0);
+    QDomElement infoXml = infoXmlNode.toElement();
+
     if (version <= 0.6) {
         QDomElement infoXml_old = infoXmlNode.cloneNode(true).toElement(); // Needed for folders
         QDomNode westley = m_doc.elementsByTagName("westley").at(1);
@@ -191,61 +194,61 @@ bool DocumentValidator::upgrade(double version, const double currentVersion)
                 }
             }
         } else for (int i = 0; i < max; i++) {
-            QDomNode n = playlists.at(i);
-            westley.insertBefore(n, QDomNode());
-            QDomElement pl = n.toElement();
-            QDomElement track = m_doc.createElement("track");
-            QString trackType = pl.attribute("hide");
-            if (!trackType.isEmpty())
-                track.setAttribute("hide", trackType);
-            QString playlist_id =  pl.attribute("id");
-            if (playlist_id.isEmpty()) {
-                playlist_id = "black_track";
-                pl.setAttribute("id", playlist_id);
-            }
-            track.setAttribute("producer", playlist_id);
-            //tractor.appendChild(track);
+                QDomNode n = playlists.at(i);
+                westley.insertBefore(n, QDomNode());
+                QDomElement pl = n.toElement();
+                QDomElement track = m_doc.createElement("track");
+                QString trackType = pl.attribute("hide");
+                if (!trackType.isEmpty())
+                    track.setAttribute("hide", trackType);
+                QString playlist_id =  pl.attribute("id");
+                if (playlist_id.isEmpty()) {
+                    playlist_id = "black_track";
+                    pl.setAttribute("id", playlist_id);
+                }
+                track.setAttribute("producer", playlist_id);
+                //tractor.appendChild(track);
 #define KEEP_TRACK_ORDER 1
 #ifdef KEEP_TRACK_ORDER
-            tractor.insertAfter(track, QDomNode());
+                tractor.insertAfter(track, QDomNode());
 #else
-            // Insert the new track in an order that hopefully matches the 3 video, then 2 audio tracks of Kdenlive 0.7.0
-            // insertion sort - O( tracks*tracks )
-            // Note, this breaks _all_ transitions - but you can move them up and down afterwards.
-            QDomElement tractor_elem = tractor.toElement();
-            if (! tractor_elem.isNull()) {
-                QDomNodeList tracks = tractor_elem.elementsByTagName("track");
-                int size = tracks.size();
-                if (size == 0) {
-                    tractor.insertAfter(track, QDomNode());
-                } else {
-                    bool inserted = false;
-                    for (int i = 0; i < size; ++i) {
-                        QDomElement track_elem = tracks.at(i).toElement();
-                        if (track_elem.isNull()) {
-                            tractor.insertAfter(track, QDomNode());
-                            inserted = true;
-                            break;
-                        } else {
-                            kDebug() << "playlist_id: " << playlist_id << " producer:" << track_elem.attribute("producer");
-                            if (playlist_id < track_elem.attribute("producer")) {
-                                tractor.insertBefore(track, track_elem);
+                // Insert the new track in an order that hopefully matches the 3 video, then 2 audio tracks of Kdenlive 0.7.0
+                // insertion sort - O( tracks*tracks )
+                // Note, this breaks _all_ transitions - but you can move them up and down afterwards.
+                QDomElement tractor_elem = tractor.toElement();
+                if (! tractor_elem.isNull()) {
+                    QDomNodeList tracks = tractor_elem.elementsByTagName("track");
+                    int size = tracks.size();
+                    if (size == 0) {
+                        tractor.insertAfter(track, QDomNode());
+                    } else {
+                        bool inserted = false;
+                        for (int i = 0; i < size; ++i) {
+                            QDomElement track_elem = tracks.at(i).toElement();
+                            if (track_elem.isNull()) {
+                                tractor.insertAfter(track, QDomNode());
                                 inserted = true;
                                 break;
+                            } else {
+                                kDebug() << "playlist_id: " << playlist_id << " producer:" << track_elem.attribute("producer");
+                                if (playlist_id < track_elem.attribute("producer")) {
+                                    tractor.insertBefore(track, track_elem);
+                                    inserted = true;
+                                    break;
+                                }
                             }
                         }
+                        // Reach here, no insertion, insert last
+                        if (!inserted) {
+                            tractor.insertAfter(track, QDomNode());
+                        }
                     }
-                    // Reach here, no insertion, insert last
-                    if (!inserted) {
-                        tractor.insertAfter(track, QDomNode());
-                    }
+                } else {
+                    kWarning() << "tractor was not a QDomElement";
+                    tractor.insertAfter(track, QDomNode());
                 }
-            } else {
-                kWarning() << "tractor was not a QDomElement";
-                tractor.insertAfter(track, QDomNode());
-            }
 #endif
-        }
+            }
         tractor.removeChild(multitrack);
 
         // audio track mixing transitions should not be added to track view, so add required attribute
@@ -669,6 +672,56 @@ bool DocumentValidator::upgrade(double version, const double currentVersion)
         }
     }
 
+    if (version <= 0.83) {
+        // Replace point size with pixel size in text titles
+        if (m_doc.toString().contains("font-size")) {
+            KMessageBox::ButtonCode convert;
+            QDomNodeList kproducerNodes = m_doc.elementsByTagName("kdenlive_producer");
+            for (int i = 0; i < kproducerNodes.count() && convert != KMessageBox::No; ++i) {
+                QDomElement kproducer = kproducerNodes.at(i).toElement();
+                if (kproducer.attribute("type").toInt() == TEXT) {
+                    QDomDocument data;
+                    data.setContent(kproducer.attribute("xmldata"));
+                    QDomNodeList items = data.firstChild().childNodes();
+                    for (int j = 0; j < items.count() && convert != KMessageBox::No; ++j) {
+                        if (items.at(j).attributes().namedItem("type").nodeValue() == "QGraphicsTextItem") {
+                            QDomNamedNodeMap textProperties = items.at(j).namedItem("content").attributes();
+                            if (textProperties.namedItem("font-pixel-size").isNull() && !textProperties.namedItem("font-size").isNull()) {
+                                // Ask the user if he wants to convert
+                                if (convert != KMessageBox::Yes && convert != KMessageBox::No)
+                                    convert = (KMessageBox::ButtonCode)KMessageBox::warningYesNo(kapp->activeWindow(), i18n("Some of your text clips were saved with size in points, which means different sizes on different displays. Do you want to convert them to pixel size, making them portable? It is recommended you do this on the computer they were first created on, or you could have to adjust their size."), i18n("Update Text Clips"));
+                                if (convert == KMessageBox::Yes) {
+                                    QFont font;
+                                    font.setPointSize(textProperties.namedItem("font-size").nodeValue().toInt());
+                                    QDomElement content = items.at(j).namedItem("content").toElement();
+                                    content.setAttribute("font-pixel-size", QFontInfo(font).pixelSize());
+                                    content.removeAttribute("font-size");
+                                    kproducer.setAttribute("xmldata", data.toString());
+                                    /*
+                                     * You may be tempted to delete the preview file
+                                     * to force its recreation: bad idea (see
+                                     * http://www.kdenlive.org/mantis/view.php?id=749)
+                                     */
+                                }
+                            }
+                        }
+                    }
+                }
+            }
+        }
+
+        // Fill the <documentproperties /> element
+        QDomElement docProperties = infoXml.firstChildElement("documentproperties");
+        if (docProperties.isNull()) {
+            docProperties = m_doc.createElement("documentproperties");
+            docProperties.setAttribute("zonein", infoXml.attribute("zonein"));
+            docProperties.setAttribute("zoneout", infoXml.attribute("zoneout"));
+            docProperties.setAttribute("zoom", infoXml.attribute("zoom"));
+            docProperties.setAttribute("position", infoXml.attribute("position"));
+            infoXml.appendChild(docProperties);
+        }
+    }
+
     // The document has been converted: mark it as modified
     infoXml.setAttribute("version", currentVersion);
     m_modified = true;