]> git.sesse.net Git - kdenlive/blobdiff - src/kdenlivedoc.cpp
Correctly focus clips / folders when they are created:
[kdenlive] / src / kdenlivedoc.cpp
index 79ad86cbd049916ef4323e5d1fa532dad8a4905e..ab517a6cdcdb3b1e0c8b8b1ca6f4a3ad0d9dcc1a 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),
-        m_zoom(7),
         m_startPos(0),
         m_render(render),
         m_commandStack(new QUndoStack(undoGroup)),
@@ -58,14 +58,18 @@ KdenliveDoc::KdenliveDoc(const KUrl &url, const KUrl &projectFolder, QUndoGroup
         m_projectFolder(projectFolder),
         m_documentLoadingStep(0.0),
         m_documentLoadingProgress(0),
-        m_abortLoading(false),
-        m_zoneStart(0),
-        m_zoneEnd(100)
+        m_abortLoading(false)
 {
     m_clipManager = new ClipManager(this);
     m_autoSaveTimer = new QTimer(this);
     m_autoSaveTimer->setSingleShot(true);
     bool success = false;
+
+    // init default document properties
+    m_documentProperties["zoom"] = "7";
+    m_documentProperties["zonein"] = "0";
+    m_documentProperties["zoneout"] = "100";
+
     if (!url.isEmpty()) {
         QString tmpFile;
         success = KIO::NetAccess::download(url.path(), tmpFile, parent);
@@ -93,21 +97,22 @@ 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();
-                        m_zoom = infoXml.attribute("zoom", "7").toInt();
-                        m_zoneStart = infoXml.attribute("zonein", "0").toInt();
-                        m_zoneEnd = infoXml.attribute("zoneout", "100").toInt();
+
+                        QDomElement docproperties = infoXml.firstChildElement("documentproperties");
+                        QDomNamedNodeMap props = docproperties.attributes();
+                        for (int i = 0; i < props.count(); i++) {
+                            m_documentProperties.insert(props.item(i).nodeName(), props.item(i).nodeValue());
+                        }
 
                         // 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 +183,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 +193,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 +211,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)) {
@@ -388,30 +392,30 @@ void KdenliveDoc::slotAutoSave()
 
 void KdenliveDoc::setZoom(int factor)
 {
-    m_zoom = factor;
+    m_documentProperties["zoom"] = QString::number(factor);
 }
 
 int KdenliveDoc::zoom() const
 {
-    return m_zoom;
+    return m_documentProperties.value("zoom").toInt();
 }
 
 void KdenliveDoc::setZone(int start, int end)
 {
-    m_zoneStart = start;
-    m_zoneEnd = end;
+    m_documentProperties["zonein"] = QString::number(start);
+    m_documentProperties["zoneout"] = QString::number(end);
 }
 
 QPoint KdenliveDoc::zone() const
 {
-    return QPoint(m_zoneStart, m_zoneEnd);
+    return QPoint(m_documentProperties.value("zonein").toInt(), m_documentProperties.value("zoneout").toInt());
 }
 
 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);
 
@@ -420,10 +424,29 @@ bool KdenliveDoc::saveSceneList(const QString &path, const QString &scene)
     addedXml.setAttribute("kdenliveversion", VERSION);
     addedXml.setAttribute("profile", profilePath());
     addedXml.setAttribute("position", m_render->seekPosition().frames(m_fps));
-    addedXml.setAttribute("zonein", m_zoneStart);
-    addedXml.setAttribute("zoneout", m_zoneEnd);
     addedXml.setAttribute("projectfolder", m_projectFolder.path());
-    addedXml.setAttribute("zoom", m_zoom);
+
+    QDomElement docproperties = sceneList.createElement("documentproperties");
+    QMapIterator<QString, QString> i(m_documentProperties);
+    while (i.hasNext()) {
+        i.next();
+        docproperties.setAttribute(i.key(), i.value());
+    }
+    addedXml.appendChild(docproperties);
+
+    // 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");
@@ -558,6 +581,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);
@@ -851,7 +918,7 @@ void KdenliveDoc::addClip(QDomElement elem, QString clipId, bool createClipItem)
     if (createClipItem) {
         emit addProjectClip(clip);
         qApp->processEvents();
-        m_render->getFileProperties(clip->toXML(), clip->getId());
+        m_render->getFileProperties(clip->toXML(), clip->getId(), false);
     }
 }
 
@@ -973,7 +1040,6 @@ void KdenliveDoc::slotAddClipList(const KUrl::List urls, const QString group, co
 
 void KdenliveDoc::slotAddClipFile(const KUrl url, const QString group, const QString &groupId)
 {
-    //kDebug() << "/////////  DOCUM, ADD CLP: " << url;
     m_clipManager->slotAddClipFile(url, group, groupId);
     emit selectLastAddedClip(QString::number(m_clipManager->lastClipId()));
     setModified(true);
@@ -989,22 +1055,57 @@ DocClipBase *KdenliveDoc::getBaseClip(const QString &clipId)
     return m_clipManager->getClipById(clipId);
 }
 
-void KdenliveDoc::slotCreateTextClip(QString /*group*/, const QString &/*groupId*/)
+void KdenliveDoc::slotCreateColorClip(const QString &name, const QString &color, const QString &duration, QString group, const QString &groupId)
+{
+    m_clipManager->slotAddColorClipFile(name, color, duration, group, groupId);
+    setModified(true);
+    emit selectLastAddedClip(QString::number(m_clipManager->lastClipId()));
+}
+
+void KdenliveDoc::slotCreateSlideshowClipFile(const QString name, const QString path, int count, const QString duration, const bool loop, const bool fade, const QString &luma_duration, const QString &luma_file, const int softness, QString group, const QString &groupId)
+{
+    m_clipManager->slotAddSlideshowClipFile(name, path, count, duration, loop, fade, luma_duration, luma_file, softness, group, groupId);
+    setModified(true);
+    emit selectLastAddedClip(QString::number(m_clipManager->lastClipId()));
+}
+
+void KdenliveDoc::slotCreateTextClip(QString group, const QString &groupId, const QString &templatePath)
 {
     QString titlesFolder = projectFolder().path() + "/titles/";
     KStandardDirs::makeDir(titlesFolder);
-    TitleWidget *dia_ui = new TitleWidget(KUrl(), titlesFolder, m_render, kapp->activeWindow());
+    TitleWidget *dia_ui = new TitleWidget(templatePath, titlesFolder, m_render, kapp->activeWindow());
     if (dia_ui->exec() == QDialog::Accepted) {
         QStringList titleInfo = TitleWidget::getFreeTitleInfo(projectFolder());
         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);
+        emit selectLastAddedClip(QString::number(m_clipManager->lastClipId()));
     }
     delete dia_ui;
 }
 
+void KdenliveDoc::slotCreateTextTemplateClip(QString group, const QString &groupId, KUrl path)
+{
+    QString titlesFolder = projectFolder().path() + "/titles/";
+    if (path.isEmpty()) {
+        path = KFileDialog::getOpenUrl(KUrl(titlesFolder), "*.kdenlivetitle", kapp->activeWindow(), i18n("Enter Template Path"));
+    }
+
+    if (path.isEmpty()) return;
+
+    QStringList titleInfo = TitleWidget::getFreeTitleInfo(projectFolder());
+
+    TitleWidget *dia_ui = new TitleWidget(path, titlesFolder, 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);
+    emit selectLastAddedClip(QString::number(m_clipManager->lastClipId()));
+}
+
 int KdenliveDoc::tracksCount() const
 {
     return m_tracksList.count();
@@ -1110,6 +1211,15 @@ bool KdenliveDoc::checkDocumentClips(QDomNodeList infoproducers)
     return (d.exec() == QDialog::Accepted);
 }
 
+void KdenliveDoc::setDocumentProperty(const QString &name, const QString &value)
+{
+    m_documentProperties[name] = value;
+}
+
+const QString KdenliveDoc::getDocumentProperty(const QString &name) const
+{
+    return m_documentProperties.value(name);
+}
 
 #include "kdenlivedoc.moc"