]> git.sesse.net Git - kdenlive/blobdiff - src/kdenlivedoc.cpp
Introducing template based title clips
[kdenlive] / src / kdenlivedoc.cpp
index 79ad86cbd049916ef4323e5d1fa532dad8a4905e..549807b4155def56fa87b3f24b1d89cc4040088e 100644 (file)
 
 #include <QCryptographicHash>
 #include <QFile>
+#include <QInputDialog>
 
 #include <mlt++/Mlt.h>
 
 const double DOCUMENTVERSION = 0.83;
 
-KdenliveDoc::KdenliveDoc(const KUrl &url, const KUrl &projectFolder, QUndoGroup *undoGroup, const QString &profileName, const QPoint tracks, Render *render, MainWindow *parent) :
+KdenliveDoc::KdenliveDoc(const KUrl &url, const KUrl &projectFolder, QUndoGroup *undoGroup, QString profileName, const QPoint tracks, Render *render, MainWindow *parent) :
         QObject(parent),
         m_autosave(NULL),
         m_url(url),
@@ -93,11 +94,10 @@ KdenliveDoc::KdenliveDoc(const KUrl &url, const KUrl &projectFolder, QUndoGroup
                     success = validator.validate(DOCUMENTVERSION);
                     if (success) { // Let the validator handle error messages
                         setModified(validator.isModified());
-                        QDomNode infoXmlNode = m_document.elementsByTagName("kdenlivedoc").at(0);
-                        QDomElement infoXml = infoXmlNode.toElement();
-                        QDomNode mlt = m_document.elementsByTagName("mlt").at(0);
+                        QDomElement mlt = m_document.firstChildElement("mlt");
+                        QDomElement infoXml = mlt.firstChildElement("kdenlivedoc");
 
-                        QString profilePath = infoXml.attribute("profile");
+                        profileName = infoXml.attribute("profile");
                         m_projectFolder = infoXml.attribute("projectfolder");
 
                         m_startPos = infoXml.attribute("position").toInt();
@@ -107,7 +107,7 @@ KdenliveDoc::KdenliveDoc(const KUrl &url, const KUrl &projectFolder, QUndoGroup
 
                         // Build tracks
                         QDomElement e;
-                        QDomNode tracksinfo = m_document.elementsByTagName("tracksinfo").at(0);
+                        QDomElement tracksinfo = infoXml.firstChildElement("tracksinfo");
                         TrackInfo projectTrack;
                         if (!tracksinfo.isNull()) {
                             QDomNodeList trackslist = tracksinfo.childNodes();
@@ -178,7 +178,7 @@ KdenliveDoc::KdenliveDoc(const KUrl &url, const KUrl &projectFolder, QUndoGroup
                             setProfilePath(KdenliveSettings::default_profile());
                             m_clipManager->clear();
                         } else {
-                            QDomNode markers = m_document.elementsByTagName("markers").at(0);
+                            QDomElement markers = infoXml.firstChildElement("markers");
                             if (!markers.isNull()) {
                                 QDomNodeList markerslist = markers.childNodes();
                                 int maxchild = markerslist.count();
@@ -188,16 +188,16 @@ KdenliveDoc::KdenliveDoc(const KUrl &url, const KUrl &projectFolder, QUndoGroup
                                         m_clipManager->getClipById(e.attribute("id"))->addSnapMarker(GenTime(e.attribute("time").toDouble()), e.attribute("comment"));
                                     }
                                 }
-                                mlt.removeChild(markers);
+                                infoXml.removeChild(markers);
                             }
-                            m_document.removeChild(infoXmlNode);
+                            setProfilePath(profileName);
                             kDebug() << "Reading file: " << url.path() << ", found clips: " << producers.count();
                         }
                     }
                 }
             }
         }
-    }
+    } else setProfilePath(profileName);
 
     // Something went wrong, or a new file was requested: create a new project
     if (!success) {
@@ -206,7 +206,6 @@ KdenliveDoc::KdenliveDoc(const KUrl &url, const KUrl &projectFolder, QUndoGroup
     }
 
     // Set the video profile (empty == default)
-    setProfilePath(profileName);
 
     // Make sure the project folder is usable
     if (m_projectFolder.isEmpty() || !KIO::NetAccess::exists(m_projectFolder.path(), KIO::NetAccess::DestinationSide, parent)) {
@@ -411,7 +410,7 @@ bool KdenliveDoc::saveSceneList(const QString &path, const QString &scene)
 {
     QDomDocument sceneList;
     sceneList.setContent(scene, true);
-    QDomNode mlt = sceneList.elementsByTagName("mlt").at(0);
+    QDomElement mlt = sceneList.firstChildElement("mlt");
     QDomElement addedXml = sceneList.createElement("kdenlivedoc");
     mlt.appendChild(addedXml);
 
@@ -425,6 +424,20 @@ bool KdenliveDoc::saveSceneList(const QString &path, const QString &scene)
     addedXml.setAttribute("projectfolder", m_projectFolder.path());
     addedXml.setAttribute("zoom", m_zoom);
 
+    // Add profile info
+    QDomElement profileinfo = sceneList.createElement("profileinfo");
+    profileinfo.setAttribute("description", m_profile.description);
+    profileinfo.setAttribute("frame_rate_num", m_profile.frame_rate_num);
+    profileinfo.setAttribute("frame_rate_den", m_profile.frame_rate_den);
+    profileinfo.setAttribute("width", m_profile.width);
+    profileinfo.setAttribute("height", m_profile.height);
+    profileinfo.setAttribute("progressive", m_profile.progressive);
+    profileinfo.setAttribute("sample_aspect_num", m_profile.sample_aspect_num);
+    profileinfo.setAttribute("sample_aspect_den", m_profile.sample_aspect_den);
+    profileinfo.setAttribute("display_aspect_num", m_profile.display_aspect_num);
+    profileinfo.setAttribute("display_aspect_den", m_profile.display_aspect_den);
+    addedXml.appendChild(profileinfo);
+
     // tracks info
     QDomElement tracksinfo = sceneList.createElement("tracksinfo");
     foreach(const TrackInfo &info, m_tracksList) {
@@ -558,6 +571,50 @@ void KdenliveDoc::setProfilePath(QString path)
     if (path.isEmpty()) path = KdenliveSettings::default_profile();
     if (path.isEmpty()) path = "dv_pal";
     m_profile = ProfilesDialog::getVideoProfile(path);
+    if (m_profile.path.isEmpty()) {
+        // Profile not found, use embedded profile
+        QDomElement profileInfo = m_document.elementsByTagName("profileinfo").at(0).toElement();
+        if (profileInfo.isNull()) {
+            KMessageBox::information(kapp->activeWindow(), i18n("Project profile was not found, using default profile."), i18n("Missing Profile"));
+            m_profile = ProfilesDialog::getVideoProfile(KdenliveSettings::default_profile());
+        } else {
+            m_profile.description = profileInfo.attribute("description");
+            m_profile.frame_rate_num = profileInfo.attribute("frame_rate_num").toInt();
+            m_profile.frame_rate_den = profileInfo.attribute("frame_rate_den").toInt();
+            m_profile.width = profileInfo.attribute("width").toInt();
+            m_profile.height = profileInfo.attribute("height").toInt();
+            m_profile.progressive = profileInfo.attribute("progressive").toInt();
+            m_profile.sample_aspect_num = profileInfo.attribute("sample_aspect_num").toInt();
+            m_profile.sample_aspect_den = profileInfo.attribute("sample_aspect_den").toInt();
+            m_profile.display_aspect_num = profileInfo.attribute("display_aspect_num").toInt();
+            m_profile.display_aspect_den = profileInfo.attribute("display_aspect_den").toInt();
+            QString existing = ProfilesDialog::existingProfile(m_profile);
+            if (!existing.isEmpty()) {
+                m_profile = ProfilesDialog::getVideoProfile(existing);
+                KMessageBox::information(kapp->activeWindow(), i18n("Project profile not found, replacing with existing one: %1", m_profile.description), i18n("Missing Profile"));
+            } else {
+                QString newDesc = m_profile.description;
+                bool ok = true;
+                while (ok && (newDesc.isEmpty() || ProfilesDialog::existingProfileDescription(newDesc))) {
+                    newDesc = QInputDialog::getText(kapp->activeWindow(), i18n("Existing Profile"), i18n("Your project uses an unknown profile.\nIt uses an existing profile name: %1.\nPlease choose a new name to save it", newDesc), QLineEdit::Normal, newDesc, &ok);
+                }
+                if (ok == false) {
+                    // User canceled, use default profile
+                    m_profile = ProfilesDialog::getVideoProfile(KdenliveSettings::default_profile());
+                } else {
+                    if (newDesc != m_profile.description) {
+                        // Profile description existed, was replaced by new one
+                        m_profile.description = newDesc;
+                    } else {
+                        KMessageBox::information(kapp->activeWindow(), i18n("Project profile was not found, it will be added to your system now."), i18n("Missing Profile"));
+                    }
+                    ProfilesDialog::saveProfile(m_profile);
+                }
+            }
+            setModified(true);
+        }
+    }
+
     KdenliveSettings::setProject_display_ratio((double) m_profile.display_aspect_num / m_profile.display_aspect_den);
     m_fps = (double) m_profile.frame_rate_num / m_profile.frame_rate_den;
     KdenliveSettings::setProject_fps(m_fps);
@@ -989,7 +1046,7 @@ DocClipBase *KdenliveDoc::getBaseClip(const QString &clipId)
     return m_clipManager->getClipById(clipId);
 }
 
-void KdenliveDoc::slotCreateTextClip(QString /*group*/, const QString &/*groupId*/)
+void KdenliveDoc::slotCreateTextClip(QString group, const QString &groupId)
 {
     QString titlesFolder = projectFolder().path() + "/titles/";
     KStandardDirs::makeDir(titlesFolder);
@@ -999,12 +1056,28 @@ void KdenliveDoc::slotCreateTextClip(QString /*group*/, const QString &/*groupId
         QImage pix = dia_ui->renderedPixmap();
         pix.save(titleInfo.at(1));
         //dia_ui->saveTitle(path + ".kdenlivetitle");
-        m_clipManager->slotAddTextClipFile(titleInfo.at(0), titleInfo.at(1), dia_ui->xml().toString(), QString(), QString());
+        m_clipManager->slotAddTextClipFile(titleInfo.at(0), titleInfo.at(1), dia_ui->xml().toString(), group, groupId);
         setModified(true);
     }
     delete dia_ui;
 }
 
+void KdenliveDoc::slotCreateTextTemplateClip(QString group, const QString &groupId)
+{
+    KUrl titlesFolder = KUrl(projectFolder().path() + "/titles/");
+    KUrl path = KFileDialog::getOpenUrl(titlesFolder, "*.kdenlivetitle", kapp->activeWindow(), i18n("Enter Template Path"));
+    if (path.isEmpty()) return;
+
+    QStringList titleInfo = TitleWidget::getFreeTitleInfo(projectFolder());
+
+    TitleWidget *dia_ui = new TitleWidget(path, titlesFolder.path(), m_render, kapp->activeWindow());
+    QImage pix = dia_ui->renderedPixmap();
+    pix.save(titleInfo.at(1));
+    delete dia_ui;
+    m_clipManager->slotAddTextTemplateClip(titleInfo.at(0), titleInfo.at(1), path, group, groupId);
+    setModified(true);
+}
+
 int KdenliveDoc::tracksCount() const
 {
     return m_tracksList.count();