]> git.sesse.net Git - kdenlive/blobdiff - src/projectlist.cpp
rotoscoping: Fix keyframes with crop from start > 0
[kdenlive] / src / projectlist.cpp
index 94286501a2b344fe40f9df0ed62957f95902ad38..e6eda4e7823d21f18b23b456a91fbef7461c4b34 100644 (file)
@@ -49,6 +49,8 @@
 #include <KIO/NetAccess>
 #include <KFileItem>
 #include <KApplication>
+#include <KStandardDirs>
+
 #ifdef NEPOMUK
 #include <nepomuk/global.h>
 #include <nepomuk/resourcemanager.h>
@@ -262,9 +264,11 @@ void ProjectList::editClipSelection(QList<QTreeWidgetItem *> list)
     commonproperties.insert("audio_index", "-");
     commonproperties.insert("force_colorspace", "-");
     commonproperties.insert("full_luma", "-");
+    QString transparency = "-";
 
     bool allowDurationChange = true;
     int commonDuration = -1;
+    bool hasImages = false;;
     ProjectItem *item;
     for (int i = 0; i < list.count(); i++) {
         item = NULL;
@@ -286,6 +290,29 @@ void ProjectList::editClipSelection(QList<QTreeWidgetItem *> list)
             // check properties
             DocClipBase *clip = item->referencedClip();
             if (clipList.contains(clip)) continue;
+            if (clip->clipType() == IMAGE) {
+                hasImages = true;
+                if (clip->getProperty("transparency").isEmpty() || clip->getProperty("transparency").toInt() == 0) {
+                    if (transparency == "-") {
+                        // first non transparent image
+                        transparency = "0";
+                    }
+                    else if (transparency == "1") {
+                        // we have transparent and non transparent clips
+                        transparency = "-1";
+                    }
+                }
+                else {
+                    if (transparency == "-") {
+                        // first transparent image
+                        transparency = "1";
+                    }
+                    else if (transparency == "0") {
+                        // we have transparent and non transparent clips
+                        transparency = "-1";
+                    }
+                }
+            }
             if (clip->clipType() != COLOR && clip->clipType() != IMAGE && clip->clipType() != TEXT)
                 allowDurationChange = false;
             if (allowDurationChange && commonDuration != 0) {
@@ -313,6 +340,8 @@ void ProjectList::editClipSelection(QList<QTreeWidgetItem *> list)
     }
     if (allowDurationChange)
         commonproperties.insert("out", QString::number(commonDuration));
+    if (hasImages)
+        commonproperties.insert("transparency", transparency);
     /*QMapIterator<QString, QString> p(commonproperties);
     while (p.hasNext()) {
         p.next();
@@ -442,10 +471,8 @@ void ProjectList::slotReloadClip(const QString &id)
                 int length = QString(item->referencedClip()->producerProperty("length")).toInt();
                 if (length > 0 && !e.hasAttribute("length")) {
                     e.setAttribute("length", length);
-                    e.setAttribute("out", length - 1);
                 }
-            }
-            
+            }            
             emit getFileProperties(e, item->clipId(), m_listView->iconSize().height(), true, false);
         }
     }
@@ -720,11 +747,13 @@ void ProjectList::slotItemEdited(QTreeWidgetItem *item, int column)
             QMap <QString, QString> oldprops;
             QMap <QString, QString> newprops;
             oldprops["name"] = clip->referencedClip()->getProperty("name");
-            newprops["name"] = item->text(0);
-            slotUpdateClipProperties(clip, newprops);
-            emit projectModified();
-            EditClipCommand *command = new EditClipCommand(this, clip->clipId(), oldprops, newprops, false);
-            m_commandStack->push(command);
+            if (oldprops.value("name") != item->text(0)) {
+                newprops["name"] = item->text(0);
+                slotUpdateClipProperties(clip, newprops);
+                emit projectModified();
+                EditClipCommand *command = new EditClipCommand(this, clip->clipId(), oldprops, newprops, false);
+                m_commandStack->push(command);
+            }
         }
     }
 }
@@ -937,7 +966,8 @@ void ProjectList::deleteProjectFolder(QMap <QString, QString> map)
         i.next();
         new AddFolderCommand(this, i.key(), i.value(), false, delCommand);
     }
-    m_commandStack->push(delCommand);
+    if (delCommand->childCount() > 0) m_commandStack->push(delCommand);
+    else delete delCommand;
 }
 
 void ProjectList::slotAddClip(DocClipBase *clip, bool getProperties)
@@ -964,39 +994,17 @@ void ProjectList::slotAddClip(DocClipBase *clip, bool getProperties)
         item = new ProjectItem(m_listView, clip);
     }
     if (item->data(0, DurationRole).isNull()) item->setData(0, DurationRole, i18n("Loading"));
+    connect(clip, SIGNAL(createProxy(const QString)), this, SLOT(slotCreateProxy(const QString)));
+    connect(clip, SIGNAL(abortProxy(const QString)), this, SLOT(slotAbortProxy(const QString)));
     if (getProperties) {
         m_listView->processLayout();
         m_refreshed = false;
-        // Proxy clips
-        CLIPTYPE t = clip->clipType();
-        if ((t == VIDEO || t == AV || t == UNKNOWN) && useProxy()) {
-            if (clip->getProperty("proxy").isEmpty()) {
-                
-                //connect(clip, SIGNAL(proxyReady(const QString&, bool)), this, SLOT(slotGotProxy(const QString&, bool)));
-                //setProxyStatus(item, 1);
-                //clip->generateProxy(m_doc->projectFolder(), proxyParams());
-            }
-            else {
-                // Proxy clip already created
-                setProxyStatus(item, 2);
-                QDomElement e = clip->toXML().cloneNode().toElement();
-                e.removeAttribute("file_hash");
-                m_infoQueue.insert(clip->getId(), e);
-            }
-        }
-        //else {
-            // We don't use proxies
-            // remove file_hash so that we load all properties for the clip
-            QDomElement e = clip->toXML().cloneNode().toElement();
-            e.removeAttribute("file_hash");
-            m_infoQueue.insert(clip->getId(), e);
-        //}
-        //m_render->getFileProperties(clip->toXML(), clip->getId(), true);
+        QDomElement e = clip->toXML().cloneNode().toElement();
+        e.removeAttribute("file_hash");
+        m_infoQueue.insert(clip->getId(), e);
     }
-    else if (!clip->getProperty("proxy").isEmpty()) {
-        connect(clip, SIGNAL(proxyReady(const QString&, bool)), this, SLOT(slotGotProxy(const QString&, bool)));
-        setProxyStatus(item, 1);
-        clip->generateProxy(m_doc->projectFolder(), proxyParams());
+    else if (item->hasProxy() && !item->isProxyRunning()) {
+        slotCreateProxy(clip->getId());
     }
     clip->askForAudioThumbs();
     
@@ -1044,21 +1052,16 @@ void ProjectList::slotAddClip(DocClipBase *clip, bool getProperties)
         m_queueRunner = QtConcurrent::run(this, &ProjectList::slotProcessNextClipInQueue);
 }
 
-void ProjectList::slotGotProxy(const QString &id, bool success)
+void ProjectList::slotGotProxy(const QString &id)
 {
     ProjectItem *item = getItemById(id);
     if (item) {
-        if (success) {
-            // Proxy clip successfully created
-            setProxyStatus(item, 2);
-            QDomElement e = item->referencedClip()->toXML().cloneNode().toElement();  
-            e.removeAttribute("file_hash");
-            e.setAttribute("replace", 1);
-            m_infoQueue.insert(id, e);
-            if (!m_queueRunner.isRunning() && m_processingClips.isEmpty()) m_queueRunner = QtConcurrent::run(this, &ProjectList::slotProcessNextClipInQueue);
-        }
-        else setProxyStatus(item, 0);
-        update();
+        // Proxy clip successfully created
+        QDomElement e = item->referencedClip()->toXML().cloneNode().toElement();  
+        //e.removeAttribute("file_hash");
+        e.setAttribute("replace", 1);
+        m_infoQueue.insert(id, e);
+        if (!m_queueRunner.isRunning() && m_processingClips.isEmpty()) m_queueRunner = QtConcurrent::run(this, &ProjectList::slotProcessNextClipInQueue);
     }
 }
 
@@ -1284,6 +1287,23 @@ void ProjectList::slotRemoveInvalidClip(const QString &id, bool replace)
     }
 }
 
+void ProjectList::slotRemoveInvalidProxy(const QString &id)
+{
+    ProjectItem *item = getItemById(id);
+    if (item) {
+        item->setProxyStatus(PROXYCRASHED);
+        QString path = item->referencedClip()->getProperty("proxy");
+        KUrl proxyFolder(m_doc->projectFolder().path( KUrl::AddTrailingSlash) + "proxy/");
+
+        //Security check: make sure the invalid proxy file is in the proxy folder
+        if (proxyFolder.isParentOf(KUrl(path))) {
+            QFile::remove(path);
+        }
+    }
+    m_processingClips.removeAll(id);
+    if (!m_queueRunner.isRunning() && m_processingClips.isEmpty()) m_queueRunner = QtConcurrent::run(this, &ProjectList::slotProcessNextClipInQueue);
+}
+
 void ProjectList::slotAddColorClip()
 {
     if (!m_commandStack)
@@ -1401,6 +1421,7 @@ void ProjectList::setDocument(KdenliveDoc *doc)
     m_timecode = doc->timecode();
     m_commandStack = doc->commandStack();
     m_doc = doc;
+    m_proxyList.clear();
 
     QMap <QString, QString> flist = doc->clipManager()->documentFolderList();
     QMapIterator<QString, QString> f(flist);
@@ -1573,16 +1594,24 @@ void ProjectList::slotReplyGetFileProperties(const QString &clipId, Mlt::Produce
             item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsDragEnabled | Qt::ItemIsEnabled | Qt::ItemIsEditable | Qt::ItemIsDropEnabled);
             toReload = clipId;
         }
-        if (!useProxy() && item->referencedClip()->getProperty("proxy").isEmpty()) setProxyStatus(item, 0);
+        
+        // Proxy stuff
         QString size = properties.value("frame_size");
         DocClipBase *clip = item->referencedClip();
-        if (useProxy() && (item->clipType() == AV || item->clipType() == VIDEO) && generateProxy() && size.section('x', 0, 0).toInt() > proxyMinSize()) {
+        if (!useProxy() && item->referencedClip()->getProperty("proxy").isEmpty()) setProxyStatus(item, NOPROXY);
+        if (useProxy() && generateProxy() && item->referencedClip()->getProperty("proxy") == "-") setProxyStatus(item, NOPROXY);
+        else if (useProxy() && !item->isProxyRunning() && (item->clipType() == AV || item->clipType() == VIDEO) && generateProxy() && size.section('x', 0, 0).toInt() > proxyMinSize()) {
             if (clip->getProperty("proxy").isEmpty()) {
-                connect(clip, SIGNAL(proxyReady(const QString&, bool)), this, SLOT(slotGotProxy(const QString&, bool)));
-                setProxyStatus(item, 1);
-                clip->generateProxy(m_doc->projectFolder(), proxyParams());
+                QString proxydir = m_doc->projectFolder().path( KUrl::AddTrailingSlash) + "proxy/";
+                QMap <QString, QString> newProps;
+                newProps.insert("proxy", proxydir + item->referencedClip()->getClipHash() + ".avi");
+                QMap <QString, QString> oldProps = clip->properties();
+                oldProps.insert("proxy", QString());
+                EditClipCommand *command = new EditClipCommand(this, clipId, oldProps, newProps, true);
+                m_doc->commandStack()->push(command);
             }
         }
+        
         clip->setProducer(producer, replace);
         clip->askForAudioThumbs();
         if (!replace && item->data(0, Qt::DecorationRole).isNull())
@@ -2034,18 +2063,114 @@ QMap <QString, QString> ProjectList::getProxies()
         }
         item = static_cast<ProjectItem *>(*it);
         if (item && item->referencedClip() != NULL) {
-            QString proxy = item->referencedClip()->getProperty("proxy");
-            if (!proxy.isEmpty()) list.insert(proxy, item->clipUrl().path());
+            if (item->hasProxy()) {
+                QString proxy = item->referencedClip()->getProperty("proxy");
+                list.insert(proxy, item->clipUrl().path());
+            }
         }
         ++it;
     }
     return list;
 }
 
+void ProjectList::slotCreateProxy(const QString id)
+{
+    ProjectItem *item = getItemById(id);
+    if (!item || item->isProxyRunning()) return;
+    setProxyStatus(id, PROXYWAITING);
+    if (m_abortProxyId.contains(id)) m_abortProxyId.removeAll(id);
+    emit projectModified();
+    QtConcurrent::run(this, &ProjectList::slotGenerateProxy, id);
+}
+
+void ProjectList::slotAbortProxy(const QString id)
+{
+    if (m_proxyList.contains(id)) m_proxyList.removeAll(id);
+    ProjectItem *item = getItemById(id);
+    if (item) {
+      emit projectModified();
+      if (item->isProxyReady()) slotGotProxy(id);
+      else if (item->isProxyRunning()) m_abortProxyId << id;
+      setProxyStatus(id, NOPROXY);
+    }
+}
+
+void ProjectList::slotGenerateProxy(const QString id)
+{
+    setProxyStatus(id, CREATINGPROXY);
+    ProjectItem *item = getItemById(id);
+    if (item == NULL) return;
+    QString path = item->referencedClip()->getProperty("proxy");
+    if (path.isEmpty()) {
+        setProxyStatus(id, PROXYCRASHED);
+        return;
+    }
+
+    QString url = item->clipUrl().path();
+    
+    if (QFile::exists(path)) {
+        setProxyStatus(id, PROXYDONE);
+        slotGotProxy(id);
+        return;
+    }
+    else {
+        // Make sure proxy path is writable
+        QFile file(path);
+        if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
+            setProxyStatus(id, PROXYCRASHED);
+            return;
+        }
+        file.close();
+        QFile::remove(path);
+    }
+
+    QStringList parameters;
+    parameters << "-i" << url;
+    QString params = proxyParams();
+    foreach(QString s, params.split(' '))
+    parameters << s;
+
+    // Make sure we don't block when proxy file already exists
+    parameters << "-y";
+    parameters << path;
+    QProcess myProcess;
+    myProcess.start("ffmpeg", parameters);
+    myProcess.waitForStarted();
+    int result = -1;
+    while (myProcess.state() != QProcess::NotRunning) {
+        // building proxy file
+        if (m_abortProxyId.contains(id)) {
+            myProcess.close();
+            myProcess.waitForFinished();
+            m_abortProxyId.removeAll(id);
+            QFile::remove(path);
+            setProxyStatus(id, NOPROXY);
+            result = -2;
+            
+        }
+        myProcess.waitForFinished(500);
+    }
+    myProcess.waitForFinished();
+    if (result == -1) result = myProcess.exitStatus();
+    if (result == 0) {
+        // proxy successfully created
+        setProxyStatus(id, PROXYDONE);
+        slotGotProxy(id);
+    }
+    else if (result == 1) {
+        // Proxy process crashed
+        QFile::remove(path);
+        setProxyStatus(id, PROXYCRASHED);
+    }
+}
+
 void ProjectList::updateProxyConfig()
 {
     ProjectItem *item;
     QTreeWidgetItemIterator it(m_listView);
+    QUndoCommand *command = new QUndoCommand();
+    command->setText(i18n("Update proxy settings"));
+    QString proxydir = m_doc->projectFolder().path( KUrl::AddTrailingSlash) + "proxy/";
     while (*it) {
         if ((*it)->type() != PROJECTCLIPTYPE) {
             ++it;
@@ -2058,25 +2183,36 @@ void ProjectList::updateProxyConfig()
         }
         CLIPTYPE t = item->clipType();
         if ((t == VIDEO || t == AV || t == UNKNOWN) && item->referencedClip() != NULL) {
-            if  (generateProxy() && useProxy()) {
+            if  (generateProxy() && useProxy() && !item->isProxyRunning()) {
                 DocClipBase *clip = item->referencedClip();
                 if (clip->getProperty("frame_size").section('x', 0, 0).toInt() > proxyMinSize()) {
-                    connect(clip, SIGNAL(proxyReady(const QString &, bool)), this, SLOT(slotGotProxy(const QString &, bool)));
-                    setProxyStatus(item, 1);
-                    clip->generateProxy(m_doc->projectFolder(), proxyParams());
+                    if (clip->getProperty("proxy").isEmpty()) {
+                        // We need to insert empty proxy in old properties so that undo will work
+                        QMap <QString, QString> oldProps = clip->properties();
+                        oldProps.insert("proxy", QString());
+                        QMap <QString, QString> newProps;
+                        newProps.insert("proxy", proxydir + item->referencedClip()->getClipHash() + ".avi");
+                        new EditClipCommand(this, clip->getId(), oldProps, newProps, true, command);
+                    }
                 }
             }
-            else if (!item->referencedClip()->getProperty("proxy").isEmpty()) {
+            else if (item->hasProxy()) {
                 // remove proxy
-                item->referencedClip()->clearProperty("proxy");
+                QMap <QString, QString> newProps;
+                newProps.insert("proxy", QString());
+                newProps.insert("replace", "1");
+                new EditClipCommand(this, item->clipId(), item->referencedClip()->properties(), newProps, true, command);
+                /*item->referencedClip()->clearProperty("proxy");
                 QDomElement e = item->toXml().cloneNode().toElement();
                 e.removeAttribute("file_hash");
                 e.setAttribute("replace", 1);
-                m_infoQueue.insert(item->clipId(), e);
+                m_infoQueue.insert(item->clipId(), e);*/
             }
         }
         ++it;
     }
+    if (command->childCount() > 0) m_doc->commandStack()->push(command);
+    else delete command;
     if (!m_infoQueue.isEmpty() && !m_queueRunner.isRunning() && m_processingClips.isEmpty()) m_queueRunner = QtConcurrent::run(this, &ProjectList::slotProcessNextClipInQueue);
 }
 
@@ -2084,6 +2220,17 @@ void ProjectList::slotProxyCurrentItem(bool doProxy)
 {
     QList<QTreeWidgetItem *> list = m_listView->selectedItems();
     QTreeWidgetItem *listItem;
+    QUndoCommand *command = new QUndoCommand();
+    if (doProxy) command->setText(i18np("Add proxy clip", "Add proxy clips", list.count()));
+    else command->setText(i18np("Remove proxy clip", "Remove proxy clips", list.count()));
+    
+    // Make sure the proxy folder exists
+    QString proxydir = m_doc->projectFolder().path( KUrl::AddTrailingSlash) + "proxy/";
+    KStandardDirs::makeDir(proxydir);
+                
+    QMap <QString, QString> newProps;
+    QMap <QString, QString> oldProps;
+    if (!doProxy) newProps.insert("proxy", "-");
     for (int i = 0; i < list.count(); i++) {
         listItem = list.at(i);
         if (listItem->type() == PROJECTFOLDERTYPE) {
@@ -2096,31 +2243,34 @@ void ProjectList::slotProxyCurrentItem(bool doProxy)
             ProjectItem *item = static_cast <ProjectItem*>(listItem);
             CLIPTYPE t = item->clipType();
             if ((t == VIDEO || t == AV || t == UNKNOWN) && item->referencedClip()) {
+                oldProps = item->referencedClip()->properties();
                 if (doProxy) {
-                    DocClipBase *clip = item->referencedClip();
-                    connect(clip, SIGNAL(proxyReady(const QString&, bool)), this, SLOT(slotGotProxy(const QString&, bool)));
-                    setProxyStatus(item, 1);
-                    clip->generateProxy(m_doc->projectFolder(), proxyParams());
-                }
-                else if (!item->referencedClip()->getProperty("proxy").isEmpty()) {
-                    // remove proxy
-                    if (!item->isProxyRunning()) {
-                        setProxyStatus(item, 0);
-                        QDomElement e = item->toXml().cloneNode().toElement();
-                        e.removeAttribute("file_hash");
-                        e.setAttribute("replace", 1);
-                        m_infoQueue.insert(item->clipId(), e);
-                    }
-                    else setProxyStatus(item, 0);
+                    newProps.clear();
+                    QString path = proxydir + item->referencedClip()->getClipHash() + ".avi";                
+                    newProps.insert("proxy", path);
+                    // We need to insert empty proxy so that undo will work
+                    oldProps.insert("proxy", QString());
                 }
+                new EditClipCommand(this, item->clipId(), oldProps, newProps, true, command);
             }
         }
     }
-    if (!m_infoQueue.isEmpty() && !m_queueRunner.isRunning() && m_processingClips.isEmpty()) m_queueRunner = QtConcurrent::run(this, &ProjectList::slotProcessNextClipInQueue);
+    if (command->childCount() > 0) {
+        m_doc->commandStack()->push(command);
+    }
+    else delete command;
+    //if (!m_infoQueue.isEmpty() && !m_queueRunner.isRunning() && m_processingClips.isEmpty()) m_queueRunner = QtConcurrent::run(this, &ProjectList::slotProcessNextClipInQueue);
+}
+
+void ProjectList::setProxyStatus(const QString id, PROXYSTATUS status)
+{
+    ProjectItem *item = getItemById(id);
+    setProxyStatus(item, status);
 }
 
-void ProjectList::setProxyStatus(ProjectItem *item, int status)
+void ProjectList::setProxyStatus(ProjectItem *item, PROXYSTATUS status)
 {
+    if (item == NULL) return;
     monitorItemEditing(false);
     item->setProxyStatus(status);
     monitorItemEditing(true);