]> git.sesse.net Git - kdenlive/blobdiff - src/projectlist.cpp
Move undo commands to their own subdirectory
[kdenlive] / src / projectlist.cpp
index 2b0999539d55621a67033f08dce024fc68b5d169..eef4da490b172a6017a3add22bd29c281ada6b58 100644 (file)
@@ -19,7 +19,7 @@
 
 #include "projectlist.h"
 #include "projectitem.h"
-#include "addfoldercommand.h"
+#include "commands/addfoldercommand.h"
 #include "kdenlivesettings.h"
 #include "slideshowclip.h"
 #include "ui_colorclip_ui.h"
 #include "projectlistview.h"
 #include "timecodedisplay.h"
 #include "profilesdialog.h"
-#include "editclipcommand.h"
-#include "editclipcutcommand.h"
-#include "editfoldercommand.h"
-#include "addclipcutcommand.h"
+#include "commands/editclipcommand.h"
+#include "commands/editclipcutcommand.h"
+#include "commands/editfoldercommand.h"
+#include "commands/addclipcutcommand.h"
 
 #include "ui_templateclip_ui.h"
 
@@ -115,6 +115,7 @@ ProjectList::ProjectList(QWidget *parent) :
     m_transcodeAction(NULL),
     m_doc(NULL),
     m_refreshed(false),
+    m_allClipsProcessed(false),
     m_thumbnailQueue(),
     m_abortAllProxies(false),
     m_invalidClipDialog(NULL)
@@ -129,10 +130,6 @@ ProjectList::ProjectList(QWidget *parent) :
     QHBoxLayout *box = new QHBoxLayout;
     KTreeWidgetSearchLine *searchView = new KTreeWidgetSearchLine;
 
-    m_refreshMonitorTimer.setSingleShot(true);
-    m_refreshMonitorTimer.setInterval(100);
-    connect(&m_refreshMonitorTimer, SIGNAL(timeout()), this, SLOT(slotRefreshMonitor()));
-
     box->addWidget(searchView);
     //int s = style()->pixelMetric(QStyle::PM_SmallIconSize);
     //m_toolbar->setIconSize(QSize(s, s));
@@ -160,7 +157,7 @@ ProjectList::ProjectList(QWidget *parent) :
     connect(this, SIGNAL(processNextThumbnail()), this, SLOT(slotProcessNextThumbnail()));
     connect(m_listView, SIGNAL(projectModified()), this, SIGNAL(projectModified()));
     connect(m_listView, SIGNAL(itemSelectionChanged()), this, SLOT(slotClipSelected()));
-    connect(m_listView, SIGNAL(focusMonitor()), this, SLOT(slotClipSelected()));
+    connect(m_listView, SIGNAL(focusMonitor()), this, SIGNAL(raiseClipMonitor()));
     connect(m_listView, SIGNAL(pauseMonitor()), this, SLOT(slotPauseMonitor()));
     connect(m_listView, SIGNAL(requestMenu(const QPoint &, QTreeWidgetItem *)), this, SLOT(slotContextMenu(const QPoint &, QTreeWidgetItem *)));
     connect(m_listView, SIGNAL(addClip()), this, SLOT(slotAddClip()));
@@ -257,6 +254,10 @@ void ProjectList::setupGeneratorMenu(QMenu *addMenu, QMenu *transcodeMenu, QMenu
     m_menu->insertSeparator(m_deleteButton->defaultAction());
 }
 
+void ProjectList::clearSelection()
+{
+    m_listView->clearSelection();
+}
 
 QByteArray ProjectList::headerInfo() const
 {
@@ -500,8 +501,8 @@ void ProjectList::slotReloadClip(const QString &id)
         item = static_cast <ProjectItem *>(selected.at(i));
         if (item && !item->isProxyRunning()) {
             DocClipBase *clip = item->referencedClip();
-            if (!clip->isClean()) {
-                // The clip is currently under processing (replacement in timeline), we cannot reload it now.
+            if (!clip || !clip->isClean() || m_render->isProcessing(item->clipId())) {
+                kDebug()<<"//// TRYING TO RELOAD: "<<item->clipId()<<", but it is busy";
                 continue;
             }
             CLIPTYPE t = item->clipType();
@@ -523,9 +524,7 @@ void ProjectList::slotReloadClip(const QString &id)
                     e.setAttribute("length", length);
                 }
             }
-            if (clip) {
-                clip->clearThumbProducer();
-            }
+            resetThumbsProducer(clip);
             m_render->getFileProperties(e, item->clipId(), m_listView->iconSize().height(), true);
         }
     }
@@ -579,7 +578,7 @@ void ProjectList::slotMissingClip(const QString &id)
             }
             item->referencedClip()->setProducer(newProd, true);
             item->slotSetToolTip();
-            emit clipNeedsReload(id, true);
+            emit clipNeedsReload(id);
         }
     }
     update();
@@ -627,7 +626,6 @@ void ProjectList::setRenderer(Render *projectRender)
 
 void ProjectList::slotClipSelected()
 {
-    m_refreshMonitorTimer.stop();
     QTreeWidgetItem *item = m_listView->currentItem();
     ProjectItem *clip = NULL;
     if (item) {
@@ -1087,7 +1085,7 @@ void ProjectList::slotAddClip(DocClipBase *clip, bool getProperties)
         m_listView->processLayout();
         QDomElement e = clip->toXML().cloneNode().toElement();
         e.removeAttribute("file_hash");
-        clip->clearThumbProducer();
+        resetThumbsProducer(clip);
         m_render->getFileProperties(e, clip->getId(), m_listView->iconSize().height(), true);
     }
     else if (item->hasProxy() && !item->isProxyRunning()) {
@@ -1131,7 +1129,7 @@ void ProjectList::slotGotProxy(const QString &proxyPath)
     QTreeWidgetItemIterator it(m_listView);
     ProjectItem *item;
 
-    while (*it) {
+    while (*it && !m_abortAllProxies) {
         if ((*it)->type() == PROJECTCLIPTYPE) {
             item = static_cast <ProjectItem *>(*it);
             if (item->referencedClip()->getProperty("proxy") == proxyPath)
@@ -1145,11 +1143,12 @@ void ProjectList::slotGotProxy(ProjectItem *item)
 {
     if (item == NULL || !m_refreshed) return;
     DocClipBase *clip = item->referencedClip();
-    if (!clip->isClean()) {
-        //WARNING: might result in clip said marked as proxy but not using proxy?
-        // The clip is currently under processing (replacement in timeline), we cannot reload it now.
+    if (!clip || !clip->isClean() || m_render->isProcessing(item->clipId())) {
+        // Clip is being reprocessed, abort
+        kDebug()<<"//// TRYING TO PROXY: "<<item->clipId()<<", but it is busy";
         return;
     }
+    
     // Proxy clip successfully created
     QDomElement e = clip->toXML().cloneNode().toElement();
 
@@ -1161,12 +1160,13 @@ void ProjectList::slotGotProxy(ProjectItem *item)
             e.setAttribute("length", length);
         }
     }
-    clip->clearThumbProducer();
+    resetThumbsProducer(clip);
     m_render->getFileProperties(e, clip->getId(), m_listView->iconSize().height(), true);
 }
 
 void ProjectList::slotResetProjectList()
 {
+    m_listView->blockSignals(true);
     m_abortAllProxies = true;
     m_proxyThreads.waitForFinished();
     m_proxyThreads.clearFutures();
@@ -1174,7 +1174,9 @@ void ProjectList::slotResetProjectList()
     m_listView->clear();
     emit clipSelected(NULL);
     m_refreshed = false;
+    m_allClipsProcessed = false;
     m_abortAllProxies = false;
+    m_listView->blockSignals(false);
 }
 
 void ProjectList::slotUpdateClip(const QString &id)
@@ -1199,13 +1201,35 @@ void ProjectList::getCachedThumbnail(ProjectItem *item)
         }
         else item->setData(0, Qt::DecorationRole, pix);
     }
-    else requestClipThumbnail(item->clipId());
+    else {
+        requestClipThumbnail(item->clipId());
+    }
+}
+
+void ProjectList::getCachedThumbnail(SubProjectItem *item)
+{
+    if (!item) return;
+    ProjectItem *parentItem = static_cast <ProjectItem *>(item->parent());
+    if (!parentItem) return;
+    DocClipBase *clip = parentItem->referencedClip();
+    if (!clip) return;
+    int pos = item->zone().x();
+    QString cachedPixmap = m_doc->projectFolder().path(KUrl::AddTrailingSlash) + "thumbs/" + clip->getClipHash() + "#" + QString::number(pos) + ".png";
+    if (QFile::exists(cachedPixmap)) {
+        QPixmap pix(cachedPixmap);
+        if (pix.isNull()) {
+            KIO::NetAccess::del(KUrl(cachedPixmap), this);
+            requestClipThumbnail(parentItem->clipId() + '#' + QString::number(pos));
+        }
+        else item->setData(0, Qt::DecorationRole, pix);
+    }
+    else requestClipThumbnail(parentItem->clipId() + '#' + QString::number(pos));
 }
 
 void ProjectList::updateAllClips(bool displayRatioChanged, bool fpsChanged)
 {
+    if (!m_allClipsProcessed) m_listView->setEnabled(false);
     m_listView->setSortingEnabled(false);
-
     QTreeWidgetItemIterator it(m_listView);
     DocClipBase *clip;
     ProjectItem *item;
@@ -1218,15 +1242,23 @@ void ProjectList::updateAllClips(bool displayRatioChanged, bool fpsChanged)
     QPainter p(&missingPixmap);
     p.drawPixmap(3, 3, icon.pixmap(width - 6, height - 6));
     p.end();
-    kDebug()<<"//////////////7  UPDATE ALL CLPS";
+    
+    int max = m_doc->clipManager()->clipsCount();
+    max = qMax(1, max);
+    int ct = 0;
+
     while (*it) {
+        emit displayMessage(i18n("Loading thumbnails"), (int)(100 *(max - ct++) / max));
         if ((*it)->type() == PROJECTSUBCLIPTYPE) {
             // subitem
             SubProjectItem *sub = static_cast <SubProjectItem *>(*it);
-            if (displayRatioChanged || sub->data(0, Qt::DecorationRole).isNull()) {
+            if (displayRatioChanged) {
                 item = static_cast <ProjectItem *>((*it)->parent());
                 requestClipThumbnail(item->clipId() + '#' + QString::number(sub->zone().x()));
             }
+            else if (sub->data(0, Qt::DecorationRole).isNull()) {
+                getCachedThumbnail(sub);
+            }
             ++it;
             continue;
         } else if ((*it)->type() == PROJECTFOLDERTYPE) {
@@ -1237,7 +1269,7 @@ void ProjectList::updateAllClips(bool displayRatioChanged, bool fpsChanged)
             item = static_cast <ProjectItem *>(*it);
             clip = item->referencedClip();
             if (item->referencedClip()->getProducer() == NULL) {
-                if (clip->isPlaceHolder() == false) {
+                if (clip->isPlaceHolder() == false && !item->isProxyRunning()) {
                     QDomElement xml = clip->toXML();
                     if (fpsChanged) {
                         xml.removeAttribute("out");
@@ -1245,7 +1277,7 @@ void ProjectList::updateAllClips(bool displayRatioChanged, bool fpsChanged)
                         xml.removeAttribute("proxy_out");
                     }
                     bool replace = xml.attribute("replace") == "1";
-                    if (replace) clip->clearThumbProducer();
+                    if (replace) resetThumbsProducer(clip);
                     m_render->getFileProperties(xml, clip->getId(), m_listView->iconSize().height(), replace);
                 }
                 else {
@@ -1288,6 +1320,7 @@ void ProjectList::updateAllClips(bool displayRatioChanged, bool fpsChanged)
     }
 
     m_listView->setSortingEnabled(true);
+    m_allClipsProcessed = true;
     if (m_render->processingItems() == 0) {
        monitorItemEditing(true);
        slotProcessNextThumbnail();
@@ -1569,6 +1602,7 @@ void ProjectList::setDocument(KdenliveDoc *doc)
     m_listView->setSortingEnabled(false);
     emit clipSelected(NULL);
     m_refreshed = false;
+    m_allClipsProcessed = false;
     m_fps = doc->fps();
     m_timecode = doc->timecode();
     m_commandStack = doc->commandStack();
@@ -1585,7 +1619,11 @@ void ProjectList::setDocument(KdenliveDoc *doc)
     }
 
     QList <DocClipBase*> list = doc->clipManager()->documentClipList();
-    if (list.isEmpty()) m_refreshed = true;
+    if (list.isEmpty()) {
+        // blank document
+        m_refreshed = true;
+        m_allClipsProcessed = true;
+    }
     for (int i = 0; i < list.count(); i++)
         slotAddClip(list.at(i), false);
 
@@ -1610,7 +1648,6 @@ QDomElement ProjectList::producersList()
     QDomDocument doc;
     QDomElement prods = doc.createElement("producerlist");
     doc.appendChild(prods);
-    kDebug() << "////////////  PRO LIST BUILD PRDSLIST ";
     QTreeWidgetItemIterator it(m_listView);
     while (*it) {
         if ((*it)->type() != PROJECTCLIPTYPE) {
@@ -1627,8 +1664,10 @@ QDomElement ProjectList::producersList()
 void ProjectList::slotCheckForEmptyQueue()
 {
     if (m_render->processingItems() == 0 && m_thumbnailQueue.isEmpty()) {
-        if (!m_refreshed) {
-            emit loadingIsOver();
+        if (!m_refreshed && m_allClipsProcessed) {
+            m_listView->setEnabled(true);
+            slotClipSelected();
+            QTimer::singleShot(500, this, SIGNAL(loadingIsOver()));
             emit displayMessage(QString(), -1);
             m_refreshed = true;
         }
@@ -1645,6 +1684,14 @@ void ProjectList::requestClipThumbnail(const QString id)
     slotProcessNextThumbnail();
 }
 
+void ProjectList::resetThumbsProducer(DocClipBase *clip)
+{
+    if (!clip) return;
+    clip->clearThumbProducer();
+    QString id = clip->getId();
+    m_thumbnailQueue.removeAll(id);
+}
+
 void ProjectList::slotProcessNextThumbnail()
 {
     if (m_render->processingItems() > 0) {
@@ -1719,26 +1766,13 @@ void ProjectList::slotRefreshClipThumbnail(QTreeWidgetItem *it, bool update)
     }
 }
 
-void ProjectList::slotRefreshMonitor()
-{
-    if (m_listView->selectedItems().count() == 1 && m_render && m_render->processingItems() == 0) {
-        if (m_listView->currentItem() && m_listView->currentItem()->type() != PROJECTFOLDERTYPE) {
-            ProjectItem *item = static_cast <ProjectItem*>(m_listView->currentItem());
-            DocClipBase *clip = item->referencedClip();
-            if (clip && clip->isClean()) emit clipSelected(clip);
-        }
-    }
-}
 
-void ProjectList::slotReplyGetFileProperties(const QString &clipId, Mlt::Producer *producer, const stringMap &properties, const stringMap &metadata, bool replace, bool refreshThumbnail)
+void ProjectList::slotReplyGetFileProperties(const QString &clipId, Mlt::Producer *producer, const stringMap &properties, const stringMap &metadata, bool replace)
 {
     QString toReload;
     ProjectItem *item = getItemById(clipId);
 
     int queue = m_render->processingItems();
-    if (queue == 0) {
-        m_listView->setEnabled(true);
-    }
     if (item && producer) {
         monitorItemEditing(false);
         DocClipBase *clip = item->referencedClip();
@@ -1749,10 +1783,10 @@ void ProjectList::slotReplyGetFileProperties(const QString &clipId, Mlt::Produce
             }
             item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsDragEnabled | Qt::ItemIsEnabled | Qt::ItemIsEditable | Qt::ItemIsDropEnabled);
         }
-        clip->setProducer(producer, replace);
         item->setProperties(properties, metadata);
+        clip->setProducer(producer, replace);
         clip->askForAudioThumbs();
-        if (refreshThumbnail) getCachedThumbnail(item);
+
         // Proxy stuff
         QString size = properties.value("frame_size");
         if (!useProxy() && clip->getProperty("proxy").isEmpty()) setProxyStatus(item, NOPROXY);
@@ -1780,8 +1814,8 @@ void ProjectList::slotReplyGetFileProperties(const QString &clipId, Mlt::Produce
             }
         }
 
-        if (!replace && item->data(0, Qt::DecorationRole).isNull() && !refreshThumbnail) {
-            requestClipThumbnail(clipId);
+        if (!replace && m_allClipsProcessed && item->data(0, Qt::DecorationRole).isNull()) {
+            getCachedThumbnail(item);
         }
         if (!toReload.isEmpty())
             item->slotSetToolTip();
@@ -1805,21 +1839,17 @@ void ProjectList::slotReplyGetFileProperties(const QString &clipId, Mlt::Produce
             int max = m_doc->clipManager()->clipsCount();
             emit displayMessage(i18n("Loading clips"), (int)(100 *(max - queue) / max));
         }
-        processNextThumbnail();
+        if (m_allClipsProcessed) emit processNextThumbnail();
     }
     if (replace && item) {
-        if (item->numReferences() > 0) toReload = clipId;
-        else item->referencedClip()->cleanupProducers();
-        // update clip in clip monitor
-        if (queue == 0 && item->isSelected() && m_listView->selectedItems().count() == 1)
-            m_refreshMonitorTimer.start();
+        toReload = clipId;
     }
     if (!item) {
         // no item for producer, delete it
         delete producer;
     }
     if (!toReload.isEmpty())
-        emit clipNeedsReload(toReload, true);
+        emit clipNeedsReload(toReload);
 }
 
 bool ProjectList::adjustProjectProfileToItem(ProjectItem *item)
@@ -2368,8 +2398,8 @@ void ProjectList::slotGenerateProxy()
             // Proxy process crashed
             QFile::remove(info.dest);
             setProxyStatus(info.dest, PROXYCRASHED);
-        }   
-
+        }
+        return;
     }
     
     if (info.type == IMAGE) {
@@ -2450,7 +2480,7 @@ void ProjectList::slotGenerateProxy()
             m_abortProxy.removeAll(info.dest);
             m_processingProxy.removeAll(info.dest);
             QFile::remove(info.dest);
-            setProxyStatus(info.dest, NOPROXY);
+            if (!m_abortAllProxies) setProxyStatus(info.dest, NOPROXY);
             result = -2;
             
         }
@@ -2601,12 +2631,19 @@ void ProjectList::slotProxyCurrentItem(bool doProxy)
             CLIPTYPE t = item->clipType();
             if ((t == VIDEO || t == AV || t == UNKNOWN || t == IMAGE || t == PLAYLIST) && item->referencedClip()) {
                 if ((doProxy && item->hasProxy()) || (!doProxy && !item->hasProxy())) continue;
-                oldProps = item->referencedClip()->properties();
+                DocClipBase *clip = item->referencedClip();
+                if (!clip || !clip->isClean() || m_render->isProcessing(item->clipId())) {
+                    kDebug()<<"//// TRYING TO PROXY: "<<item->clipId()<<", but it is busy";
+                    continue;
+                }
+                
+                resetThumbsProducer(clip);
+                oldProps = clip->properties();
                 if (doProxy) {
                     newProps.clear();
-                    QString path = proxydir + item->referencedClip()->getClipHash() + "." + (t == IMAGE ? "png" : m_doc->getDocumentProperty("proxyextension"));
+                    QString path = proxydir + clip->getClipHash() + "." + (t == IMAGE ? "png" : m_doc->getDocumentProperty("proxyextension"));
                     // insert required duration for proxy
-                    newProps.insert("proxy_out", item->referencedClip()->producerProperty("out"));
+                    newProps.insert("proxy_out", clip->producerProperty("out"));
                     newProps.insert("proxy", path);
                     // We need to insert empty proxy so that undo will work
                     oldProps.insert("proxy", QString());
@@ -2653,7 +2690,7 @@ void ProjectList::setProxyStatus(const QString proxyPath, PROXYSTATUS status, in
     if (proxyPath.isEmpty() || m_abortAllProxies) return;
     QTreeWidgetItemIterator it(m_listView);
     ProjectItem *item;
-    while (*it) {
+    while (*it && !m_abortAllProxies) {
         if ((*it)->type() == PROJECTCLIPTYPE) {
             item = static_cast <ProjectItem *>(*it);
             if (item->referencedClip()->getProperty("proxy") == proxyPath) {