]> git.sesse.net Git - kdenlive/blobdiff - src/kdenlivedoc.cpp
correctly load / save text clips
[kdenlive] / src / kdenlivedoc.cpp
index ca12bc7754a6084c767f24ef5ab10ebc02ae1ae5..1fb0f1b5abb1b8c370fc0a00c6840f290d5877ad 100644 (file)
 #include "titlewidget.h"
 #include "mainwindow.h"
 
-KdenliveDoc::KdenliveDoc(const KUrl &url, const KUrl &projectFolder, MltVideoProfile profile, QUndoGroup *undoGroup, MainWindow *parent): QObject(parent), m_render(NULL), m_url(url), m_projectFolder(projectFolder), m_profile(profile), m_fps((double)profile.frame_rate_num / profile.frame_rate_den), m_width(profile.width), m_height(profile.height), m_commandStack(new KUndoStack(undoGroup)), m_modified(false), m_documentLoadingProgress(0), m_documentLoadingStep(0.0), m_startPos(0) {
+
+KdenliveDoc::KdenliveDoc(const KUrl &url, const KUrl &projectFolder, MltVideoProfile profile, QUndoGroup *undoGroup, MainWindow *parent): QObject(parent), m_render(NULL), m_url(url), m_projectFolder(projectFolder), m_profile(profile), m_fps((double)profile.frame_rate_num / profile.frame_rate_den), m_width(profile.width), m_height(profile.height), m_commandStack(new KUndoStack(undoGroup)), m_modified(false), m_documentLoadingProgress(0), m_documentLoadingStep(0.0), m_startPos(0), m_zoom(4) {
     kDebug() << "// init profile, ratnum: " << profile.frame_rate_num << ", " << profile.frame_rate_num << ", width: " << profile.width;
     m_clipManager = new ClipManager(this);
+    KdenliveSettings::setProject_fps(m_fps);
     if (!url.isEmpty()) {
         QString tmpFile;
         if (KIO::NetAccess::download(url.path(), tmpFile, parent)) {
@@ -51,6 +53,7 @@ KdenliveDoc::KdenliveDoc(const KUrl &url, const KUrl &projectFolder, MltVideoPro
                 QDomElement infoXml = infoXmlNode.toElement();
                 QString profilePath = infoXml.attribute("profile");
                 m_startPos = infoXml.attribute("position").toInt();
+                m_zoom = infoXml.attribute("zoom").toInt();
                 if (!profilePath.isEmpty()) setProfilePath(profilePath);
                 double version = infoXml.attribute("version").toDouble();
                 if (version < 0.7) convertDocument(version);
@@ -199,11 +202,52 @@ KdenliveDoc::KdenliveDoc(const KUrl &url, const KUrl &projectFolder, MltVideoPro
     kDebug() << "KDEnnlive document, init timecode: " << m_fps;
     if (m_fps == 30000.0 / 1001.0) m_timecode.setFormat(30, true);
     else m_timecode.setFormat((int) m_fps);
+
+    m_autoSaveTimer = new QTimer(this);
+    m_autoSaveTimer->setSingleShot(true);
+    QString directory = m_url.directory();
+    QString fileName = m_url.fileName();
+    m_recoveryUrl.setDirectory(directory);
+    m_recoveryUrl.setFileName("~" + fileName);
+    connect(m_autoSaveTimer, SIGNAL(timeout()), this, SLOT(slotAutoSave()));
 }
 
 KdenliveDoc::~KdenliveDoc() {
     delete m_commandStack;
     delete m_clipManager;
+    delete m_autoSaveTimer;
+    if (!m_url.isEmpty()) {
+        // remove backup file
+        if (KIO::NetAccess::exists(m_recoveryUrl, KIO::NetAccess::SourceSide, NULL))
+            KIO::NetAccess::del(m_recoveryUrl, NULL);
+    }
+}
+
+void KdenliveDoc::syncGuides(QList <Guide *> guides) {
+    QDomDocument doc;
+    QDomElement e;
+    m_guidesXml.clear();
+    m_guidesXml = doc.createElement("guides");
+    for (int i = 0; i < guides.count(); i++) {
+        e = doc.createElement("guide");
+        e.setAttribute("time", guides.at(i)->position().ms() / 1000);
+        e.setAttribute("comment", guides.at(i)->label());
+        m_guidesXml.appendChild(e);
+    }
+}
+
+void KdenliveDoc::slotAutoSave() {
+    if (m_render)
+        m_render->saveSceneList(m_recoveryUrl.path(), documentInfoXml());
+
+}
+
+void KdenliveDoc::setZoom(int factor) {
+    m_zoom = factor;
+}
+
+int KdenliveDoc::zoom() const {
+    return m_zoom;
 }
 
 void KdenliveDoc::convertDocument(double version) {
@@ -313,7 +357,7 @@ void KdenliveDoc::convertDocument(double version) {
     //kDebug() << "/////////////////  END CONVERTEDĀ DOC:";
 }
 
-QDomElement KdenliveDoc::documentInfoXml(QDomElement timelineInfo) {
+QDomElement KdenliveDoc::documentInfoXml() {
     QDomDocument doc;
     QDomElement e;
     QDomElement addedXml = doc.createElement("kdenlivedoc");
@@ -321,6 +365,7 @@ QDomElement KdenliveDoc::documentInfoXml(QDomElement timelineInfo) {
     addedXml.setAttribute("version", "0.7");
     addedXml.setAttribute("profile", profilePath());
     addedXml.setAttribute("position", m_render->seekPosition().frames(m_fps));
+    addedXml.setAttribute("zoom", m_zoom);
     QList <DocClipBase*> list = m_clipManager->documentClipList();
     for (int i = 0; i < list.count(); i++) {
         e = list.at(i)->toXML();
@@ -336,7 +381,7 @@ QDomElement KdenliveDoc::documentInfoXml(QDomElement timelineInfo) {
         }
     }
     addedXml.appendChild(markers);
-    addedXml.appendChild(doc.importNode(timelineInfo, true));
+    if (!m_guidesXml.isNull()) addedXml.appendChild(doc.importNode(m_guidesXml, true));
     //kDebug() << m_document.toString();
     return addedXml;
 }
@@ -367,6 +412,7 @@ void KdenliveDoc::setProfilePath(QString path) {
     m_fps = (double) m_profile.frame_rate_num / m_profile.frame_rate_den;
     m_width = m_profile.width;
     m_height = m_profile.height;
+    KdenliveSettings::setProject_fps(m_fps);
     kDebug() << "KDEnnlive document, init timecode from path: " << path << ",  " << m_fps;
     if (m_fps == 30000.0 / 1001.0) m_timecode.setFormat(30, true);
     else m_timecode.setFormat((int) m_fps);
@@ -401,6 +447,12 @@ void KdenliveDoc::updateClip(int id) {
     emit updateClipDisplay(id);
 }
 
+void KdenliveDoc::updateAllProjectClips() {
+    QList <DocClipBase*> list = m_clipManager->documentClipList();
+    for (int i = 0; i < list.count(); i++)
+        emit updateClipDisplay(list.at(i)->getId());
+}
+
 int KdenliveDoc::getFramePos(QString duration) {
     return m_timecode.getFrameCount(duration, m_fps);
 }
@@ -488,9 +540,16 @@ KUrl KdenliveDoc::url() const {
 
 void KdenliveDoc::setUrl(KUrl url) {
     m_url = url;
+    QString directory = m_url.directory();
+    QString fileName = m_url.fileName();
+    m_recoveryUrl.setDirectory(directory);
+    m_recoveryUrl.setFileName("~" + fileName);
 }
 
 void KdenliveDoc::setModified(bool mod) {
+    if (!m_url.isEmpty() && mod && KdenliveSettings::crashrecovery()) {
+        m_autoSaveTimer->start(3000);
+    }
     if (mod == m_modified) return;
     m_modified = mod;
     emit docModified(m_modified);
@@ -550,9 +609,9 @@ void KdenliveDoc::slotAddClipFile(const KUrl url, const QString group, const int
     setModified(true);
 }
 
-void KdenliveDoc::slotAddTextClipFile(const QString path, const QString group, const int groupId) {
+void KdenliveDoc::slotAddTextClipFile(const QString path, const QString xml, const QString group, const int groupId) {
     kDebug() << "/////////  DOCUM, ADD TXT CLP: " << path;
-    m_clipManager->slotAddTextClipFile(path, group, groupId);
+    m_clipManager->slotAddTextClipFile(path, xml, group, groupId);
     setModified(true);
 }
 
@@ -606,18 +665,23 @@ void KdenliveDoc::slotCreateTextClip(QString group, int groupId) {
         }
         QPixmap pix = dia_ui->renderedPixmap();
         pix.save(path + ".png");
-        dia_ui->saveTitle(path + ".kdenlivetitle");
-        slotAddTextClipFile(path, QString(), -1);
+        //dia_ui->saveTitle(path + ".kdenlivetitle");
+        slotAddTextClipFile(path, dia_ui->xml().toString(), QString(), -1);
     }
     delete dia_ui;
 }
 
 void KdenliveDoc::editTextClip(QString path, int id) {
-    TitleWidget *dia_ui = new TitleWidget(KUrl(path + ".kdenlivetitle"), path, m_render, kapp->activeWindow());
+    DocClipBase *clip = m_clipManager->getClipById(id);
+    if (!clip) return;
+    TitleWidget *dia_ui = new TitleWidget(KUrl()/*path + ".kdenlivetitle")*/, path, m_render, kapp->activeWindow());
+    QDomDocument doc;
+    doc.setContent(clip->getProperty("xmldata"));
+    dia_ui->setXml(doc);
     if (dia_ui->exec() == QDialog::Accepted) {
         QPixmap pix = dia_ui->renderedPixmap();
         pix.save(path + ".png");
-        dia_ui->saveTitle(path + ".kdenlivetitle");
+        //dia_ui->saveTitle(path + ".kdenlivetitle");
         //slotAddClipFile(KUrl("/tmp/kdenlivetitle.png"), QString(), -1);
         emit refreshClipThumbnail(id);
     }