]> git.sesse.net Git - kdenlive/blobdiff - src/projectlist.cpp
Correctly update monitor when changing a title clip duration
[kdenlive] / src / projectlist.cpp
index 52e4287113b88ce571d6eeb87d17693543c68600..af531d2565490be6ca812177ee7e0d9988c8f3ed 100644 (file)
@@ -41,6 +41,8 @@
 #include <KFileDialog>
 #include <KInputDialog>
 #include <KMessageBox>
+#include <KIO/NetAccess>
+#include <KFileItem>
 
 #include <nepomuk/global.h>
 #include <nepomuk/resourcemanager.h>
@@ -212,6 +214,40 @@ void ProjectList::slotOpenClip()
     }
 }
 
+void ProjectList::cleanup()
+{
+    m_listView->clearSelection();
+    QTreeWidgetItemIterator it(m_listView);
+    ProjectItem *item;
+    while (*it) {
+        item = static_cast <ProjectItem *>(*it);
+        if (item->numReferences() == 0) item->setSelected(true);
+        it++;
+    }
+    slotRemoveClip();
+}
+
+void ProjectList::trashUnusedClips()
+{
+    QTreeWidgetItemIterator it(m_listView);
+    ProjectItem *item;
+    QStringList ids;
+    KUrl::List urls;
+    while (*it) {
+        item = static_cast <ProjectItem *>(*it);
+        if (item->numReferences() == 0) {
+            ids << item->clipId();
+            KUrl url = item->clipUrl();
+            if (!url.isEmpty()) urls << url;
+        }
+        it++;
+    }
+    m_doc->deleteProjectClip(ids);
+    for (int i = 0; i < urls.count(); i++) {
+        KIO::NetAccess::del(urls.at(i), this);
+    }
+}
+
 void ProjectList::slotReloadClip(const QString &id)
 {
     QList<QTreeWidgetItem *> selected;
@@ -278,11 +314,14 @@ void ProjectList::slotUpdateClipProperties(const QString &id, QMap <QString, QSt
     ProjectItem *item = getItemById(id);
     if (item) {
         slotUpdateClipProperties(item, properties);
-        if (properties.contains("colour") || properties.contains("resource") || properties.contains("xmldata") || properties.contains("force_aspect_ratio") || properties.contains("templatetext")) {
+        if (properties.contains("out")) {
+            slotReloadClip(id);
+            //item->changeDuration(properties.value("out").toInt());
+        }
+        else if (properties.contains("colour") || properties.contains("resource") || properties.contains("xmldata") || properties.contains("force_aspect_ratio") || properties.contains("templatetext")) {
             slotRefreshClipThumbnail(item);
             emit refreshClip();
         }
-        if (properties.contains("out")) item->changeDuration(properties.value("out").toInt());
     }
 }
 
@@ -290,7 +329,6 @@ void ProjectList::slotUpdateClipProperties(ProjectItem *clip, QMap <QString, QSt
 {
     if (!clip) return;
     if (!clip->isGroup()) clip->setProperties(properties);
-    //if (properties.contains("xmldata")) regenerateTemplateImage(clip);
     if (properties.contains("name")) {
         m_listView->blockSignals(true);
         clip->setText(1, properties.value("name"));
@@ -321,14 +359,13 @@ void ProjectList::slotItemEdited(QTreeWidgetItem *item, int column)
             oldprops["description"] = clip->referencedClip()->getProperty("description");
             newprops["description"] = item->text(2);
 
-            if (clip->clipType() == TEXT && !clip->referencedClip()->getProperty("xmldata").isEmpty()) {
+            if (clip->clipType() == TEXT) {
                 // This is a text template clip, update the image
                 /*oldprops.insert("xmldata", clip->referencedClip()->getProperty("xmldata"));
                 newprops.insert("xmldata", generateTemplateXml(clip->referencedClip()->getProperty("xmltemplate"), item->text(2)).toString());*/
                 oldprops.insert("templatetext", clip->referencedClip()->getProperty("templatetext"));
                 newprops.insert("templatetext", item->text(2));
             }
-
             slotUpdateClipProperties(clip->clipId(), newprops);
             EditClipCommand *command = new EditClipCommand(this, clip->clipId(), oldprops, newprops, false);
             m_commandStack->push(command);
@@ -384,7 +421,7 @@ void ProjectList::slotContextMenu(const QPoint &pos, QTreeWidgetItem *item)
 void ProjectList::slotRemoveClip()
 {
     if (!m_listView->currentItem()) return;
-    QList <QString> ids;
+    QStringList ids;
     QMap <QString, QString> folderids;
     QList<QTreeWidgetItem *> selected = m_listView->selectedItems();
     ProjectItem *item;
@@ -396,7 +433,7 @@ void ProjectList::slotRemoveClip()
             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;
+            if (KMessageBox::questionYesNo(this, i18np("Delete folder <b>%2</b>?<br>This will also remove the clip in that folder", "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();
@@ -499,12 +536,17 @@ void ProjectList::deleteProjectFolder(QMap <QString, QString> map)
 
 void ProjectList::slotAddClip(DocClipBase *clip, bool getProperties)
 {
+    m_listView->setEnabled(false);
     if (getProperties) {
-        m_listView->setEnabled(false);
         m_listView->blockSignals(true);
+        m_refreshed = false;
+        // 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);
     }
     const QString parent = clip->getProperty("groupid");
-    kDebug() << "Adding clip with groupid: " << parent;
     ProjectItem *item = NULL;
     if (!parent.isEmpty()) {
         ProjectItem *parentitem = getFolderItemById(parent);
@@ -537,7 +579,8 @@ void ProjectList::slotAddClip(DocClipBase *clip, bool getProperties)
         if (!annotation.isEmpty()) item->setText(2, annotation);
         item->setText(3, QString::number(f.rating()));
     }
-    if (getProperties) m_listView->blockSignals(false);
+    if (getProperties && m_listView->isEnabled()) m_listView->blockSignals(false);
+    if (getProperties && !m_queueTimer.isActive()) m_queueTimer.start();
 }
 
 void ProjectList::slotResetProjectList()
@@ -551,9 +594,8 @@ void ProjectList::slotResetProjectList()
 
 void ProjectList::requestClipInfo(const QDomElement xml, const QString id)
 {
+    m_refreshed = false;
     m_infoQueue.insert(id, xml);
-    m_listView->setEnabled(false);
-    if (!m_queueTimer.isActive()) m_queueTimer.start();
     //if (m_infoQueue.count() == 1 || ) QTimer::singleShot(300, this, SLOT(slotProcessNextClipInQueue()));
 }
 
@@ -571,7 +613,7 @@ void ProjectList::slotProcessNextClipInQueue()
         m_infoQueue.remove(j.key());
         emit getFileProperties(dom, id, false);
     }
-    if (!m_infoQueue.isEmpty()) m_queueTimer.start();
+    //if (!m_infoQueue.isEmpty() && !m_queueTimer.isActive()) m_queueTimer.start();
 }
 
 void ProjectList::slotUpdateClip(const QString &id)
@@ -594,16 +636,6 @@ void ProjectList::updateAllClips()
         item = static_cast <ProjectItem *>(*it);
         if (!item->isGroup()) {
             clip = item->referencedClip();
-            /*if (clip->clipType() == TEXT && !QFile::exists(clip->fileURL().path())) {
-                // regenerate text clip image if required
-                TitleWidget *dia_ui = new TitleWidget(KUrl(), QString(), m_render, this);
-                QDomDocument doc;
-                doc.setContent(clip->getProperty("xmldata"));
-                dia_ui->setXml(doc);
-                QImage pix = dia_ui->renderedPixmap();
-                pix.save(clip->fileURL().path());
-                delete dia_ui;
-            }*/
             if (item->referencedClip()->producer() == NULL) {
                 if (clip->isPlaceHolder() == false) {
                     requestClipInfo(clip->toXML(), clip->getId());
@@ -617,12 +649,14 @@ void ProjectList::updateAllClips()
                 }
             }
             item->setData(1, UsageRole, QString::number(item->numReferences()));
-            qApp->processEvents();
+            //qApp->processEvents();
         }
         ++it;
     }
+    qApp->processEvents();
+    if (!m_queueTimer.isActive()) m_queueTimer.start();
 
-    m_listView->blockSignals(false);
+    if (m_listView->isEnabled()) m_listView->blockSignals(false);
     m_listView->setSortingEnabled(true);
     if (m_infoQueue.isEmpty()) slotProcessNextThumbnail();
 }
@@ -672,7 +706,7 @@ void ProjectList::slotRemoveInvalidClip(const QString &id, bool replace)
                 if (KMessageBox::questionYesNo(this, i18n("Clip <b>%1</b><br>is missing or invalid. Remove it from project?", path), i18n("Invalid clip")) == KMessageBox::Yes) replace = true;
             }
         }
-        QList <QString> ids;
+        QStringList ids;
         ids << id;
         if (replace) m_doc->deleteProjectClip(ids);
     }
@@ -741,17 +775,12 @@ void ProjectList::slotAddTitleTemplateClip()
     //warning: setting base directory doesn't work??
     KUrl startDir(path);
     dia_ui.template_list->fileDialog()->setUrl(startDir);
-    dia_ui.description->setHidden(true);
+    dia_ui.text_box->setHidden(true);
     if (dia->exec() == QDialog::Accepted) {
         QString textTemplate = dia_ui.template_list->comboBox()->itemData(dia_ui.template_list->comboBox()->currentIndex()).toString();
         if (textTemplate.isEmpty()) textTemplate = dia_ui.template_list->comboBox()->currentText();
-        if (dia_ui.normal_clip->isChecked()) {
-            // Create a normal title clip
-            m_doc->slotCreateTextClip(groupInfo.at(0), groupInfo.at(1), textTemplate);
-        } else {
-            // Create a cloned template clip
-            m_doc->slotCreateTextTemplateClip(groupInfo.at(0), groupInfo.at(1), KUrl(textTemplate));
-        }
+        // Create a cloned template clip
+        m_doc->slotCreateTextTemplateClip(groupInfo.at(0), groupInfo.at(1), KUrl(textTemplate));
     }
     delete dia;
 }
@@ -825,11 +854,15 @@ void ProjectList::slotCheckForEmptyQueue()
     if (!m_refreshed && m_thumbnailQueue.isEmpty() && m_infoQueue.isEmpty()) {
         m_refreshed = true;
         emit loadingIsOver();
-    } else QTimer::singleShot(300, this, SLOT(slotCheckForEmptyQueue()));
+        emit displayMessage(QString(), -1);
+        m_listView->blockSignals(false);
+        m_listView->setEnabled(true);
+    } else if (!m_refreshed) QTimer::singleShot(300, this, SLOT(slotCheckForEmptyQueue()));
 }
 
 void ProjectList::reloadClipThumbnails()
 {
+    kDebug() << "//////////////  RELOAD CLIPS THUMBNAILS!!!";
     m_thumbnailQueue.clear();
     QTreeWidgetItemIterator it(m_listView);
     while (*it) {
@@ -842,13 +875,12 @@ void ProjectList::reloadClipThumbnails()
 
 void ProjectList::requestClipThumbnail(const QString id)
 {
-    m_thumbnailQueue.append(id);
+    if (!m_thumbnailQueue.contains(id)) m_thumbnailQueue.append(id);
 }
 
 void ProjectList::slotProcessNextThumbnail()
 {
     if (m_thumbnailQueue.isEmpty() && m_infoQueue.isEmpty()) {
-        m_listView->setEnabled(true);
         slotCheckForEmptyQueue();
         return;
     }
@@ -856,6 +888,10 @@ void ProjectList::slotProcessNextThumbnail()
         //QTimer::singleShot(300, this, SLOT(slotProcessNextThumbnail()));
         return;
     }
+    if (m_thumbnailQueue.count() > 1) {
+        int max = m_doc->clipManager()->clipsCount();
+        emit displayMessage(i18n("Loading thumbnails"), (int)(100 * (max - m_thumbnailQueue.count()) / max));
+    }
     slotRefreshClipThumbnail(m_thumbnailQueue.takeFirst(), false);
 }
 
@@ -878,16 +914,16 @@ void ProjectList::slotRefreshClipThumbnail(ProjectItem *item, bool update)
         int height = 50;
         int width = (int)(height  * m_render->dar());
         if (clip->clipType() == AUDIO) pix = KIcon("audio-x-generic").pixmap(QSize(width, height));
-        else if (clip->clipType() == IMAGE) pix = KThumb::getFrame(item->referencedClip()->producer(), 0, width, height);
+        else if (clip->clipType() == IMAGE) pix = QPixmap::fromImage(KThumb::getFrame(item->referencedClip()->producer(), 0, width, height));
         else pix = item->referencedClip()->thumbProducer()->extractImage(item->referencedClip()->getClipThumbFrame(), width, height);
         if (!pix.isNull()) {
             m_listView->blockSignals(true);
             item->setIcon(0, pix);
-            m_listView->blockSignals(false);
+            if (m_listView->isEnabled()) m_listView->blockSignals(false);
             m_doc->cachePixmap(item->getClipHash(), pix);
         }
         if (update) emit projectModified();
-        QTimer::singleShot(100, this, SLOT(slotProcessNextThumbnail()));
+        QTimer::singleShot(30, this, SLOT(slotProcessNextThumbnail()));
     }
 }
 
@@ -899,8 +935,8 @@ void ProjectList::slotReplyGetFileProperties(const QString &clipId, Mlt::Produce
         item->setProperties(properties, metadata);
         Q_ASSERT_X(item->referencedClip(), "void ProjectList::slotReplyGetFileProperties", QString("Item with groupName %1 does not have a clip associated").arg(item->groupName()).toLatin1());
         item->referencedClip()->setProducer(producer, replace);
-        emit receivedClipDuration(clipId);
-        if (replace) {
+        //emit receivedClipDuration(clipId);
+        if (m_listView->isEnabled() && replace) {
             // update clip in clip monitor
             emit clipSelected(NULL);
             emit clipSelected(item->referencedClip());
@@ -910,13 +946,15 @@ void ProjectList::slotReplyGetFileProperties(const QString &clipId, Mlt::Produce
             emit receivedClipDuration(clipId);
             delete producer;
         }*/
-        m_listView->blockSignals(false);
+        if (m_listView->isEnabled()) m_listView->blockSignals(false);
         if (item->icon(0).isNull()) {
             requestClipThumbnail(clipId);
         }
     } else kDebug() << "////////  COULD NOT FIND CLIP TO UPDATE PRPS...";
-
-    slotProcessNextClipInQueue();
+    int max = m_doc->clipManager()->clipsCount();
+    emit displayMessage(i18n("Loading clips"), (int)(100 * (max - m_infoQueue.count()) / max));
+    // small delay so that the app can display the progress info
+    QTimer::singleShot(30, this, SLOT(slotProcessNextClipInQueue()));
 }
 
 void ProjectList::slotReplyGetImage(const QString &clipId, const QPixmap &pix)
@@ -926,7 +964,7 @@ void ProjectList::slotReplyGetImage(const QString &clipId, const QPixmap &pix)
         m_listView->blockSignals(true);
         item->setIcon(0, pix);
         m_doc->cachePixmap(item->getClipHash(), pix);
-        m_listView->blockSignals(false);
+        if (m_listView->isEnabled()) m_listView->blockSignals(false);
     }
 }
 
@@ -992,31 +1030,9 @@ void ProjectList::regenerateTemplate(const QString &id)
 void ProjectList::regenerateTemplate(ProjectItem *clip)
 {
     //TODO: remove this unused method, only force_reload is necessary
-    // Generate image for template clip
-    /*const QString comment = clip->referencedClip()->getProperty("description");
-    const QString path = clip->referencedClip()->getProperty("resource");
-    QDomDocument doc = generateTemplateXml(path, comment);
-    TitleWidget *dia_ui = new TitleWidget(KUrl(), QString(), m_render, this);
-    dia_ui->setXml(doc);
-    QImage pix = dia_ui->renderedPixmap();
-    pix.save(clip->clipUrl().path());
-    delete dia_ui;*/
     clip->referencedClip()->producer()->set("force_reload", 1);
 }
 
-void ProjectList::regenerateTemplateImage(ProjectItem *clip)
-{
-    //TODO: remove this unused method
-    // Generate image for template clip
-    /*TitleWidget *dia_ui = new TitleWidget(KUrl(), QString(), m_render, this);
-    QDomDocument doc;
-    doc.setContent(clip->referencedClip()->getProperty("xmldata"));
-    dia_ui->setXml(doc);
-    QImage pix = dia_ui->renderedPixmap();
-    pix.save(clip->clipUrl().path());
-    delete dia_ui;*/
-}
-
 QDomDocument ProjectList::generateTemplateXml(QString path, const QString &replaceString)
 {
     QDomDocument doc;