]> git.sesse.net Git - kdenlive/blobdiff - src/projectlist.cpp
Start caching of project tree thumbnails for faster project opening
[kdenlive] / src / projectlist.cpp
index 95716709500699a613ca137711ac3e7408b0b20e..0bdbba1b97a9808fcb8ae7501808fd57da1e2ecd 100644 (file)
 #include "projectlistview.h"
 
 ProjectList::ProjectList(QWidget *parent)
-        : QWidget(parent), m_render(NULL), m_fps(-1), m_commandStack(NULL), m_selectedItem(NULL), m_infoQueue(QMap <QString, QDomElement> ()), m_thumbnailQueue(QList <QString> ()) {
+        : 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);
@@ -112,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 &)));
@@ -127,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()) {
@@ -156,6 +166,10 @@ void ProjectList::slotClipSelected() {
     }
 }
 
+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) {
@@ -251,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;
@@ -306,17 +320,21 @@ void ProjectList::slotAddFolder(const QString foldername, const QString &clipId,
 
 void ProjectList::slotAddClip(DocClipBase *clip, bool getProperties) {
     const QString parent = clip->getProperty("groupid");
+    //kDebug() << "Adding clip with groupid: " << parent;
     ProjectItem *item = NULL;
     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);
     }
@@ -342,6 +360,7 @@ void ProjectList::slotAddClip(DocClipBase *clip, bool getProperties) {
 }
 
 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()));
@@ -386,8 +405,11 @@ void ProjectList::updateAllClips() {
                 }
                 requestClipInfo(clip->toXML(), clip->getId());
             } else {
-                requestClipThumbnail(item->clipId());
-                //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()));
@@ -395,13 +417,14 @@ 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;
 
@@ -425,18 +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);
@@ -468,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) {
@@ -511,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);
@@ -523,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);
 }
 
@@ -540,6 +567,12 @@ QDomElement ProjectList::producersList() {
     return prods;
 }
 
+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);
@@ -547,7 +580,9 @@ void ProjectList::requestClipThumbnail(const QString &id) {
 }
 
 void ProjectList::slotProcessNextThumbnail() {
-    if (m_thumbnailQueue.isEmpty()) return;
+    if (m_thumbnailQueue.isEmpty()) {
+        return;
+    }
     slotRefreshClipThumbnail(m_thumbnailQueue.takeFirst(), false);
 }
 
@@ -559,10 +594,16 @@ void ProjectList::slotRefreshClipThumbnail(const QString &clipId, bool update) {
 
 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()));
     }
@@ -580,11 +621,21 @@ void ProjectList::slotReplyGetFileProperties(const QString &clipId, Mlt::Produce
 
 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;
@@ -592,6 +643,7 @@ ProjectItem *ProjectList::getItemById(const QString &id) {
     }
     if (*it) return ((ProjectItem *)(*it));
     return NULL;
+#endif
 }
 
 void ProjectList::slotSelectClip(const QString &ix) {