]> git.sesse.net Git - kdenlive/blobdiff - src/projectlist.cpp
Start caching of project tree thumbnails for faster project opening
[kdenlive] / src / projectlist.cpp
index af83769ab34b13ee820c9372e84af187b18ee0c5..0bdbba1b97a9808fcb8ae7501808fd57da1e2ecd 100644 (file)
 #include "projectlistview.h"
 
 ProjectList::ProjectList(QWidget *parent)
-        : QWidget(parent), m_render(NULL), m_fps(-1), m_commandStack(NULL), m_selectedItem(NULL) {
+        : QWidget(parent), m_render(NULL), m_fps(-1), m_commandStack(NULL), m_selectedItem(NULL), m_infoQueue(QMap <QString, QDomElement> ()), m_thumbnailQueue(QList <QString> ()), m_refreshed(false) {
 
     QWidget *vbox = new QWidget;
     listView = new ProjectListView(this);;
     QVBoxLayout *layout = new QVBoxLayout;
+    layout->setContentsMargins(0, 0, 0, 0);
     m_clipIdCounter = 0;
-
     // setup toolbar
     searchView = new KTreeWidgetSearchLine(this);
     m_toolbar = new QToolBar("projectToolBar", this);
@@ -113,6 +113,7 @@ ProjectList::ProjectList(QWidget *parent)
 
     connect(listView, SIGNAL(itemSelectionChanged()), this, SLOT(slotClipSelected()));
     connect(listView, SIGNAL(focusMonitor()), this, SLOT(slotClipSelected()));
+    connect(listView, SIGNAL(pauseMonitor()), this, SLOT(slotPauseMonitor()));
     connect(listView, SIGNAL(requestMenu(const QPoint &, QTreeWidgetItem *)), this, SLOT(slotContextMenu(const QPoint &, QTreeWidgetItem *)));
     connect(listView, SIGNAL(addClip()), this, SLOT(slotAddClip()));
     connect(listView, SIGNAL(addClip(QUrl, const QString &)), this, SLOT(slotAddClip(QUrl, const QString &)));
@@ -128,6 +129,14 @@ ProjectList::~ProjectList() {
     delete m_toolbar;
 }
 
+QByteArray ProjectList::headerInfo() {
+    return listView->header()->saveState();
+}
+
+void ProjectList::setHeaderInfo(const QByteArray &state) {
+    listView->header()->restoreState(state);
+}
+
 void ProjectList::slotEditClip() {
     ProjectItem *item = static_cast <ProjectItem*>(listView->currentItem());
     if (item && !item->isGroup()) {
@@ -143,13 +152,24 @@ void ProjectList::setRenderer(Render *projectRender) {
 }
 
 void ProjectList::slotClipSelected() {
-    ProjectItem *item = static_cast <ProjectItem*>(listView->currentItem());
-    if (item && !item->isGroup()) {
-        m_selectedItem = item;
-        emit clipSelected(item->referencedClip());
+    if (listView->currentItem()) {
+        ProjectItem *clip = static_cast <ProjectItem*>(listView->currentItem());
+        if (!clip->isGroup()) {
+            m_selectedItem = clip;
+            emit clipSelected(clip->referencedClip());
+        }
+        m_editAction->setEnabled(true);
+        m_deleteAction->setEnabled(true);
+    } else {
+        m_editAction->setEnabled(false);
+        m_deleteAction->setEnabled(false);
     }
 }
 
+void ProjectList::slotPauseMonitor() {
+    if (m_render) m_render->pause();
+}
+
 void ProjectList::slotUpdateClipProperties(const QString &id, QMap <QString, QString> properties) {
     ProjectItem *item = getItemById(id);
     if (item) {
@@ -187,6 +207,12 @@ void ProjectList::slotItemEdited(QTreeWidgetItem *item, int column) {
         slotUpdateClipProperties(clip, props);
     } else if (column == 1 && clip->isGroup()) {
         m_doc->slotEditFolder(item->text(1), clip->groupName(), clip->clipId());
+        clip->setGroupName(item->text(1));
+        const int children = item->childCount();
+        for (int i = 0; i < children; i++) {
+            ProjectItem *child = static_cast <ProjectItem *>(item->child(i));
+            child->setProperty("groupname", item->text(1));
+        }
     }
 }
 
@@ -197,29 +223,36 @@ void ProjectList::slotContextMenu(const QPoint &pos, QTreeWidgetItem *item) {
     }
     m_editAction->setEnabled(enable);
     m_deleteAction->setEnabled(enable);
-
     m_menu->popup(pos);
 }
 
 void ProjectList::slotRemoveClip() {
     if (!listView->currentItem()) return;
-    ProjectItem *item = static_cast <ProjectItem *>(listView->currentItem());
     QList <QString> ids;
     QMap <QString, QString> folderids;
-    if (item->clipType() == FOLDER) folderids[item->groupName()] = item->clipId();
-    else ids << item->clipId();
-    if (item->numReferences() > 0) {
-        if (KMessageBox::questionYesNo(this, i18np("Delete clip <b>%2</b> ?<br>This will also remove the clip in timeline", "Delete clip <b>%2</b> ?<br>This will also remove its %1 clips in timeline", item->numReferences(), item->names().at(1)), i18n("Delete Clip")) != KMessageBox::Yes) return;
-    } else if (item->clipType() == FOLDER && item->childCount() > 0) {
-        int children = item->childCount();
-        if (KMessageBox::questionYesNo(this, i18n("Delete folder <b>%2</b> ?<br>This will also remove the %1 clips in that folder", children, item->names().at(1)), i18n("Delete Folder")) != KMessageBox::Yes) return;
-        for (int i = 0; i < children; ++i) {
-            ProjectItem *child = static_cast <ProjectItem *>(item->child(i));
-            ids << child->clipId();
+    QList<QTreeWidgetItem *> selected = listView->selectedItems();
+    ProjectItem *item;
+    for (int i = 0; i < selected.count(); i++) {
+        item = static_cast <ProjectItem *>(selected.at(i));
+        if (item->isGroup()) folderids[item->groupName()] = item->clipId();
+        else ids << item->clipId();
+        if (item->numReferences() > 0) {
+            if (KMessageBox::questionYesNo(this, i18np("Delete clip <b>%2</b> ?<br>This will also remove the clip in timeline", "Delete clip <b>%2</b> ?<br>This will also remove its %1 clips in timeline", item->numReferences(), item->names().at(1)), i18n("Delete Clip")) != KMessageBox::Yes) return;
+        } else if (item->isGroup() && item->childCount() > 0) {
+            int children = item->childCount();
+            if (KMessageBox::questionYesNo(this, i18n("Delete folder <b>%2</b> ?<br>This will also remove the %1 clips in that folder", children, item->names().at(1)), i18n("Delete Folder")) != KMessageBox::Yes) return;
+            for (int i = 0; i < children; ++i) {
+                ProjectItem *child = static_cast <ProjectItem *>(item->child(i));
+                ids << child->clipId();
+            }
         }
     }
     if (!ids.isEmpty()) m_doc->deleteProjectClip(ids);
     if (!folderids.isEmpty()) m_doc->deleteProjectFolder(folderids);
+    if (listView->topLevelItemCount() == 0) {
+        m_editAction->setEnabled(false);
+        m_deleteAction->setEnabled(false);
+    }
 }
 
 void ProjectList::selectItemById(const QString &clipId) {
@@ -232,7 +265,7 @@ void ProjectList::slotDeleteClip(const QString &clipId) {
     ProjectItem *item = getItemById(clipId);
     QTreeWidgetItem *p = item->parent();
     if (p) {
-        kDebug() << "///////  DELETEED CLIP HAS A PARENT... " << p->indexOfChild(item);
+        kDebug() << "///////  DELETED CLIP HAS A PARENT... " << p->indexOfChild(item);
         QTreeWidgetItem *clone = p->takeChild(p->indexOfChild(item));
     } else if (item) {
         delete item;
@@ -252,7 +285,7 @@ void ProjectList::slotAddFolder(const QString foldername, const QString &clipId,
         QTreeWidgetItemIterator it(listView);
         while (*it) {
             item = static_cast <ProjectItem *>(*it);
-            if (item->clipType() == FOLDER && item->clipId() == clipId) {
+            if (item->isGroup() && item->clipId() == clipId) {
                 delete item;
                 break;
             }
@@ -260,18 +293,23 @@ void ProjectList::slotAddFolder(const QString foldername, const QString &clipId,
         }
     } else {
         if (edit) {
-            disconnect(listView, SIGNAL(itemChanged(QTreeWidgetItem *, int)), this, SLOT(slotUpdateItemDescription(QTreeWidgetItem *, int)));
+            listView->blockSignals(true);
             ProjectItem *item;
             QTreeWidgetItemIterator it(listView);
             while (*it) {
                 item = static_cast <ProjectItem *>(*it);
-                if (item->clipType() == FOLDER && item->clipId() == clipId) {
-                    item->setText(1, foldername);
+                if (item->isGroup() && item->clipId() == clipId) {
+                    item->setGroupName(foldername);
+                    const int children = item->childCount();
+                    for (int i = 0; i < children; i++) {
+                        ProjectItem *child = static_cast <ProjectItem *>(item->child(i));
+                        child->setProperty("groupname", foldername);
+                    }
                     break;
                 }
                 ++it;
             }
-            connect(listView, SIGNAL(itemChanged(QTreeWidgetItem *, int)), this, SLOT(slotUpdateItemDescription(QTreeWidgetItem *, int)));
+            listView->blockSignals(false);
         } else {
             QStringList text;
             text << QString() << foldername;
@@ -281,10 +319,23 @@ void ProjectList::slotAddFolder(const QString foldername, const QString &clipId,
 }
 
 void ProjectList::slotAddClip(DocClipBase *clip, bool getProperties) {
-    const QString parent = clip->toXML().attribute("groupid");
+    const QString parent = clip->getProperty("groupid");
+    //kDebug() << "Adding clip with groupid: " << parent;
     ProjectItem *item = NULL;
-    if (parent != 0) {
+    if (!parent.isEmpty()) {
         ProjectItem *parentitem = getItemById(parent);
+        if (!parentitem) {
+            QStringList text;
+            QString groupName = clip->getProperty("groupname");
+            //kDebug() << "Adding clip to new group: " << groupName;
+            if (groupName.isEmpty()) groupName = i18n("Folder");
+            text << QString() << groupName;
+            listView->blockSignals(true);
+            parentitem = new ProjectItem(listView, text, parent);
+            listView->blockSignals(false);
+        } else {
+            //kDebug() << "Adding clip to existing group: " << parentitem->groupName();
+        }
         if (parentitem) item = new ProjectItem(parentitem, clip);
     }
     if (item == NULL) item = new ProjectItem(listView, clip);
@@ -305,7 +356,29 @@ void ProjectList::slotAddClip(DocClipBase *clip, bool getProperties) {
         }
         if (!annotation.isEmpty()) item->setText(2, annotation);
     }
-    if (getProperties) emit getFileProperties(clip->toXML(), clip->getId());
+    if (getProperties) requestClipInfo(clip->toXML(), clip->getId());
+}
+
+void ProjectList::requestClipInfo(const QDomElement xml, const QString id) {
+    kDebug() << " PRG LIST REQUEST CLP INFO: " << id;
+    m_infoQueue.insert(id, xml);
+    listView->setEnabled(false);
+    if (m_infoQueue.count() == 1) QTimer::singleShot(300, this, SLOT(slotProcessNextClipInQueue()));
+}
+
+void ProjectList::slotProcessNextClipInQueue() {
+    if (m_infoQueue.isEmpty()) {
+        listView->setEnabled(true);
+        return;
+    }
+    QMap<QString, QDomElement>::const_iterator i = m_infoQueue.constBegin();
+    if (i != m_infoQueue.constEnd()) {
+        const QDomElement dom = i.value();
+        const QString id = i.key();
+        m_infoQueue.remove(i.key());
+        emit getFileProperties(dom, id);
+    }
+    if (m_infoQueue.isEmpty()) listView->setEnabled(true);
 }
 
 void ProjectList::slotUpdateClip(const QString &id) {
@@ -330,9 +403,13 @@ void ProjectList::updateAllClips() {
                     pix.save(clip->fileURL().path());
                     delete dia_ui;
                 }
-                emit getFileProperties(clip->toXML(), clip->getId());
+                requestClipInfo(clip->toXML(), clip->getId());
             } else {
-                slotRefreshClipThumbnail(item, false);
+                QString cachedPixmap = m_doc->projectFolder().path() + "/thumbs/" + item->getClipHash() + ".png";
+                if (QFile::exists(cachedPixmap)) {
+                    //kDebug()<<"// USING CACHED PIX: "<<cachedPixmap;
+                    item->setIcon(0, QPixmap(cachedPixmap));
+                } else requestClipThumbnail(item->clipId());
                 item->changeDuration(item->referencedClip()->producer()->get_playtime());
             }
             item->setData(1, UsageRole, QString::number(item->numReferences()));
@@ -340,26 +417,27 @@ void ProjectList::updateAllClips() {
         }
         ++it;
     }
+    QTimer::singleShot(500, this, SLOT(slotCheckForEmptyQueue()));
 }
 
 void ProjectList::slotAddClip(QUrl givenUrl, QString group) {
-    if (!m_commandStack) kDebug() << "!!!!!!!!!!!!!!!!  NO CMD STK";
+    if (!m_commandStack) kDebug() << "!!!!!!!!!!!!!!!! NO CMD STK";
     KUrl::List list;
     if (givenUrl.isEmpty()) {
-        list = KFileDialog::getOpenUrls(KUrl("kfiledialog:///clipfolder"), "application/x-kdenlive application/x-flash-video application/vnd.rn-realmedia video/x-dv video/dv video/x-msvideo video/mpeg video/x-ms-wmv audio/mpeg audio/x-mp3 audio/x-wav application/ogg video/mp4 video/quicktime image/gif image/jpeg image/png image/x-bmp image/svg+xml image/tiff image/x-xcf-gimp image/x-vnd.adobe.photoshop image/x-pcx image/x-exr video/mlt-playlist audio/x-flac audio/mp4", this);
+        list = KFileDialog::getOpenUrls(KUrl("kfiledialog:///clipfolder"), "application/x-kdenlive video/x-flv application/vnd.rn-realmedia video/x-dv video/dv video/x-msvideo video/mpeg video/x-ms-wmv audio/mpeg audio/x-mp3 audio/x-wav application/ogg video/mp4 video/quicktime image/gif image/jpeg image/png image/x-bmp image/svg+xml image/tiff image/x-xcf-gimp image/x-vnd.adobe.photoshop image/x-pcx image/x-exr video/mlt-playlist audio/x-flac audio/mp4", this);
     } else list.append(givenUrl);
     if (list.isEmpty()) return;
 
     QString groupId = QString();
     if (group.isEmpty()) {
         ProjectItem *item = static_cast <ProjectItem*>(listView->currentItem());
-        if (item && item->clipType() != FOLDER) {
+        if (item && !item->isGroup()) {
             while (item->parent()) {
                 item = static_cast <ProjectItem*>(item->parent());
-                if (item->clipType() == FOLDER) break;
+                if (item->isGroup()) break;
             }
         }
-        if (item && item->clipType() == FOLDER) {
+        if (item && item->isGroup()) {
             group = item->groupName();
             groupId = item->clipId();
         }
@@ -370,17 +448,17 @@ void ProjectList::slotAddClip(QUrl givenUrl, QString group) {
 void ProjectList::slotRemoveInvalidClip(const QString &id) {
     ProjectItem *item = getItemById(id);
     if (item) {
-        QString path = item->referencedClip()->fileURL().path();
+        const QString path = item->referencedClip()->fileURL().path();
         if (!path.isEmpty()) KMessageBox::sorry(this, i18n("<qt>Clip <b>%1</b><br>is invalid, will be removed from project.", path));
-
+        QList <QString> ids;
+        ids << id;
+        m_doc->deleteProjectClip(ids);
     }
-    QList <QString> ids;
-    ids << id;
-    m_doc->deleteProjectClip(ids);
+    if (!m_infoQueue.isEmpty()) QTimer::singleShot(300, this, SLOT(slotProcessNextClipInQueue()));
 }
 
 void ProjectList::slotAddColorClip() {
-    if (!m_commandStack) kDebug() << "!!!!!!!!!!!!!!!!  NO CMD STK";
+    if (!m_commandStack) kDebug() << "!!!!!!!!!!!!!!!! NO CMD STK";
     QDialog *dia = new QDialog(this);
     Ui::ColorClip_UI *dia_ui = new Ui::ColorClip_UI();
     dia_ui->setupUi(dia);
@@ -393,13 +471,13 @@ void ProjectList::slotAddColorClip() {
         QString group = QString();
         QString groupId = QString();
         ProjectItem *item = static_cast <ProjectItem*>(listView->currentItem());
-        if (item && item->clipType() != FOLDER) {
+        if (item && !item->isGroup()) {
             while (item->parent()) {
                 item = static_cast <ProjectItem*>(item->parent());
-                if (item->clipType() == FOLDER) break;
+                if (item->isGroup()) break;
             }
         }
-        if (item && item->clipType() == FOLDER) {
+        if (item && item->isGroup()) {
             group = item->groupName();
             groupId = item->clipId();
         }
@@ -412,7 +490,7 @@ void ProjectList::slotAddColorClip() {
 
 
 void ProjectList::slotAddSlideshowClip() {
-    if (!m_commandStack) kDebug() << "!!!!!!!!!!!!!!!!  NO CMD STK";
+    if (!m_commandStack) kDebug() << "!!!!!!!!!!!!!!!! NO CMD STK";
     SlideshowClip *dia = new SlideshowClip(this);
 
     if (dia->exec() == QDialog::Accepted) {
@@ -420,13 +498,13 @@ void ProjectList::slotAddSlideshowClip() {
         QString group = QString();
         QString groupId = QString();
         ProjectItem *item = static_cast <ProjectItem*>(listView->currentItem());
-        if (item && item->clipType() != FOLDER) {
+        if (item && !item->isGroup()) {
             while (item->parent()) {
                 item = static_cast <ProjectItem*>(item->parent());
-                if (item->clipType() == FOLDER) break;
+                if (item->isGroup()) break;
             }
         }
-        if (item && item->clipType() == FOLDER) {
+        if (item && item->isGroup()) {
             group = item->groupName();
             groupId = item->clipId();
         }
@@ -440,13 +518,13 @@ void ProjectList::slotAddTitleClip() {
     QString group = QString();
     QString groupId = QString();
     ProjectItem *item = static_cast <ProjectItem*>(listView->currentItem());
-    if (item && item->clipType() != FOLDER) {
+    if (item && !item->isGroup()) {
         while (item->parent()) {
             item = static_cast <ProjectItem*>(item->parent());
-            if (item->clipType() == FOLDER) break;
+            if (item->isGroup()) break;
         }
     }
-    if (item && item->clipType() == FOLDER) {
+    if (item && item->isGroup()) {
         group = item->groupName();
         groupId = item->clipId();
     }
@@ -455,7 +533,11 @@ void ProjectList::slotAddTitleClip() {
 }
 
 void ProjectList::setDocument(KdenliveDoc *doc) {
+    listView->blockSignals(true);
     listView->clear();
+    m_thumbnailQueue.clear();
+    m_infoQueue.clear();
+    m_refreshed = false;
     QList <DocClipBase*> list = doc->clipManager()->documentClipList();
     for (int i = 0; i < list.count(); i++) {
         slotAddClip(list.at(i), false);
@@ -467,6 +549,7 @@ void ProjectList::setDocument(KdenliveDoc *doc) {
     m_doc = doc;
     QTreeWidgetItem *first = listView->topLevelItem(0);
     if (first) listView->setCurrentItem(first);
+    listView->blockSignals(false);
     m_toolbar->setEnabled(true);
 }
 
@@ -484,37 +567,75 @@ QDomElement ProjectList::producersList() {
     return prods;
 }
 
-void ProjectList::slotRefreshClipThumbnail(const QString &clipId) {
+void ProjectList::slotCheckForEmptyQueue() {
+    if (!m_refreshed && m_thumbnailQueue.isEmpty() && m_infoQueue.isEmpty()) {
+        m_refreshed = true;
+        emit loadingIsOver();
+    } else QTimer::singleShot(500, this, SLOT(slotCheckForEmptyQueue()));
+}
+
+void ProjectList::requestClipThumbnail(const QString &id) {
+    m_thumbnailQueue.append(id);
+    if (m_thumbnailQueue.count() == 1) QTimer::singleShot(300, this, SLOT(slotProcessNextThumbnail()));
+}
+
+void ProjectList::slotProcessNextThumbnail() {
+    if (m_thumbnailQueue.isEmpty()) {
+        return;
+    }
+    slotRefreshClipThumbnail(m_thumbnailQueue.takeFirst(), false);
+}
+
+void ProjectList::slotRefreshClipThumbnail(const QString &clipId, bool update) {
     ProjectItem *item = getItemById(clipId);
-    if (item) slotRefreshClipThumbnail(item);
+    if (item) slotRefreshClipThumbnail(item, update);
+    else slotProcessNextThumbnail();
 }
 
 void ProjectList::slotRefreshClipThumbnail(ProjectItem *item, bool update) {
     if (item) {
+        if (!item->referencedClip()) return;
         int height = 50;
         int width = (int)(height  * m_render->dar());
-        QPixmap pix = item->referencedClip()->thumbProducer()->extractImage(item->referencedClip()->getClipThumbFrame(), width, height);
+        DocClipBase *clip = item->referencedClip();
+        if (!clip) slotProcessNextThumbnail();
+        QPixmap pix;
+        if (clip->clipType() == AUDIO) pix = KIcon("audio-x-generic").pixmap(QSize(width, height));
+        else pix = item->referencedClip()->thumbProducer()->extractImage(item->referencedClip()->getClipThumbFrame(), width, height);
         item->setIcon(0, pix);
+        m_doc->cachePixmap(item->getClipHash(), pix);
         if (update) emit projectModified();
+        if (!m_thumbnailQueue.isEmpty()) QTimer::singleShot(300, this, SLOT(slotProcessNextThumbnail()));
     }
 }
 
 void ProjectList::slotReplyGetFileProperties(const QString &clipId, Mlt::Producer *producer, const QMap < QString, QString > &properties, const QMap < QString, QString > &metadata) {
     ProjectItem *item = getItemById(clipId);
-    if (item) {
+    if (item && producer) {
         item->setProperties(properties, metadata);
         item->referencedClip()->setProducer(producer);
         emit receivedClipDuration(clipId, item->clipMaxDuration());
     } else kDebug() << "////////  COULD NOT FIND CLIP TO UPDATE PRPS...";
+    if (!m_infoQueue.isEmpty()) QTimer::singleShot(300, this, SLOT(slotProcessNextClipInQueue()));
 }
 
 void ProjectList::slotReplyGetImage(const QString &clipId, int pos, const QPixmap &pix, int w, int h) {
     ProjectItem *item = getItemById(clipId);
-    if (item) item->setIcon(0, pix);
+    if (item) {
+        item->setIcon(0, pix);
+        m_doc->cachePixmap(item->getClipHash(), pix);
+    }
 }
 
 ProjectItem *ProjectList::getItemById(const QString &id) {
     QTreeWidgetItemIterator it(listView);
+    while (*it) {
+        if (((ProjectItem *)(*it))->clipId() == id)
+            return static_cast<ProjectItem *>(*it);
+        ++it;
+    }
+    return NULL;
+#ifdef USED_TO_BE_THIS
     while (*it) {
         if (((ProjectItem *)(*it))->clipId() == id)
             break;
@@ -522,6 +643,7 @@ ProjectItem *ProjectList::getItemById(const QString &id) {
     }
     if (*it) return ((ProjectItem *)(*it));
     return NULL;
+#endif
 }
 
 void ProjectList::slotSelectClip(const QString &ix) {
@@ -529,6 +651,8 @@ void ProjectList::slotSelectClip(const QString &ix) {
     if (p) {
         listView->setCurrentItem(p);
         listView->scrollToItem(p);
+        m_editAction->setEnabled(true);
+        m_deleteAction->setEnabled(true);
     }
 }