]> git.sesse.net Git - kdenlive/blobdiff - src/projectlist.cpp
Fix encoding problem in renderer:
[kdenlive] / src / projectlist.cpp
index 87870c64d4d12efe0ac31550812afaba20f26e7b..375a1c2c488f5261a0325fe2089f70946920eb52 100644 (file)
@@ -966,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)
@@ -997,13 +998,12 @@ void ProjectList::slotAddClip(DocClipBase *clip, bool getProperties)
     connect(clip, SIGNAL(abortProxy(const QString)), this, SLOT(slotAbortProxy(const QString)));
     if (getProperties) {
         m_listView->processLayout();
-        m_refreshed = false;
         QDomElement e = clip->toXML().cloneNode().toElement();
         e.removeAttribute("file_hash");
         m_infoQueue.insert(clip->getId(), e);
     }
     else if (item->hasProxy() && !item->isProxyRunning()) {
-        slotCreateProxy(clip->getId());
+        slotCreateProxy(clip->getId(), false);
     }
     clip->askForAudioThumbs();
     
@@ -1058,6 +1058,15 @@ void ProjectList::slotGotProxy(const QString &id)
         // Proxy clip successfully created
         QDomElement e = item->referencedClip()->toXML().cloneNode().toElement();  
         //e.removeAttribute("file_hash");
+
+        // Make sure we get the correct producer length if it was adjusted in timeline
+        CLIPTYPE t = item->clipType();
+        if (t == COLOR || t == IMAGE || t == SLIDESHOW || t == TEXT) {
+            int length = QString(item->referencedClip()->producerProperty("length")).toInt();
+            if (length > 0 && !e.hasAttribute("length")) {
+                e.setAttribute("length", length);
+            }
+        }
         e.setAttribute("replace", 1);
         m_infoQueue.insert(id, e);
         if (!m_queueRunner.isRunning() && m_processingClips.isEmpty()) m_queueRunner = QtConcurrent::run(this, &ProjectList::slotProcessNextClipInQueue);
@@ -1075,7 +1084,6 @@ void ProjectList::slotResetProjectList()
 
 void ProjectList::requestClipInfo(const QDomElement xml, const QString id)
 {
-    m_refreshed = false;
     m_infoQueue.insert(id, xml);
     //if (m_infoQueue.count() == 1 || ) QTimer::singleShot(300, this, SLOT(slotProcessNextClipInQueue()));
 }
@@ -1148,8 +1156,9 @@ void ProjectList::updateAllClips()
             } else {
                 if (item->data(0, Qt::DecorationRole).isNull())
                     requestClipThumbnail(clip->getId());
-                if (item->data(0, DurationRole).toString().isEmpty())
+                if (item->data(0, DurationRole).toString().isEmpty()) {
                     item->changeDuration(item->referencedClip()->producer()->get_playtime());
+                }
             }
             item->setData(0, UsageRole, QString::number(item->numReferences()));
         }
@@ -1286,10 +1295,14 @@ void ProjectList::slotRemoveInvalidClip(const QString &id, bool replace)
     }
 }
 
-void ProjectList::slotRemoveInvalidProxy(const QString &id)
+void ProjectList::slotRemoveInvalidProxy(const QString &id, bool durationError)
 {
     ProjectItem *item = getItemById(id);
     if (item) {
+        if (durationError) {
+            kDebug() << "Proxy duration is wrong, try changing transcoding parameters.";
+            emit displayMessage(i18n("Proxy clip unusable (duration is different from original)."), -2);
+        }
         item->setProxyStatus(PROXYCRASHED);
         QString path = item->referencedClip()->getProperty("proxy");
         KUrl proxyFolder(m_doc->projectFolder().path( KUrl::AddTrailingSlash) + "proxy/");
@@ -1423,10 +1436,12 @@ void ProjectList::setDocument(KdenliveDoc *doc)
     m_proxyList.clear();
 
     QMap <QString, QString> flist = doc->clipManager()->documentFolderList();
+    QStringList openedFolders = doc->getExpandedFolders();
     QMapIterator<QString, QString> f(flist);
     while (f.hasNext()) {
         f.next();
-        (void) new FolderProjectItem(m_listView, QStringList() << f.value(), f.key());
+        FolderProjectItem *folder = new FolderProjectItem(m_listView, QStringList() << f.value(), f.key());
+        folder->setExpanded(openedFolders.contains(f.key()));
     }
 
     QList <DocClipBase*> list = doc->clipManager()->documentClipList();
@@ -1470,7 +1485,7 @@ QDomElement ProjectList::producersList()
 
 void ProjectList::slotCheckForEmptyQueue()
 {
-    if (!m_refreshed && m_thumbnailQueue.isEmpty() && m_infoQueue.isEmpty()) {
+    if (!m_refreshed && m_processingClips.isEmpty() && m_thumbnailQueue.isEmpty() && m_infoQueue.isEmpty()) {
         m_refreshed = true;
         emit loadingIsOver();
         emit displayMessage(QString(), -1);
@@ -1582,27 +1597,46 @@ void ProjectList::slotReplyGetFileProperties(const QString &clipId, Mlt::Produce
 {
     QString toReload;
     ProjectItem *item = getItemById(clipId);
+    if (!m_refreshed) {
+        // we are still finishing to load the document
+        selectClip = false;
+    }
     m_processingClips.removeAll(clipId);
     if (m_infoQueue.isEmpty() && m_processingClips.isEmpty()) m_listView->setEnabled(true);
     if (item && producer) {
         //m_listView->blockSignals(true);
         monitorItemEditing(false);
+        DocClipBase *clip = item->referencedClip();
         item->setProperties(properties, metadata);
-        if (item->referencedClip()->isPlaceHolder() && producer->is_valid()) {
-            item->referencedClip()->setValid();
+        if (clip->isPlaceHolder() && producer->is_valid()) {
+            clip->setValid();
             item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsDragEnabled | Qt::ItemIsEnabled | Qt::ItemIsEditable | Qt::ItemIsDropEnabled);
             toReload = clipId;
         }
-        
+
         // Proxy stuff
         QString size = properties.value("frame_size");
-        DocClipBase *clip = item->referencedClip();
-        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()) {
-                QString proxydir = m_doc->projectFolder().path( KUrl::AddTrailingSlash) + "proxy/";
-                clip->setProperty("proxy", proxydir + item->referencedClip()->getClipHash() + ".avi");
+        if (!useProxy() && clip->getProperty("proxy").isEmpty()) setProxyStatus(item, NOPROXY);
+        if (useProxy() && generateProxy() && clip->getProperty("proxy") == "-") setProxyStatus(item, NOPROXY);
+        else if (useProxy() && !item->isProxyRunning()) {
+            // proxy video and image clips
+            int maxSize;
+            CLIPTYPE t = item->clipType();
+            if (t == IMAGE) maxSize = m_doc->getDocumentProperty("proxyimageminsize").toInt();
+            else maxSize = m_doc->getDocumentProperty("proxyminsize").toInt();
+            if (((t == AV || t == VIDEO) && generateProxy() && size.section('x', 0, 0).toInt() > maxSize) || (t == IMAGE && generateImageProxy() && (size.section('x', 0, 0).toInt() > maxSize || size.section('x', 1, 1).toInt() > maxSize))) {
+                if (clip->getProperty("proxy").isEmpty()) {
+                    QString proxydir = m_doc->projectFolder().path( KUrl::AddTrailingSlash) + "proxy/";
+                    QMap <QString, QString> newProps;
+                    QString path = proxydir + clip->getClipHash() + "." + (t == IMAGE ? "png" : m_doc->getDocumentProperty("proxyextension"));
+                    newProps.insert("proxy", path);
+                    // insert required duration for proxy
+                    if (t != IMAGE) newProps.insert("proxy_out", clip->producerProperty("out"));
+                    QMap <QString, QString> oldProps = clip->properties();
+                    oldProps.insert("proxy", QString());
+                    EditClipCommand *command = new EditClipCommand(this, clipId, oldProps, newProps, true);
+                    m_doc->commandStack()->push(command);
+                }
             }
         }
         
@@ -1713,6 +1747,11 @@ bool ProjectList::adjustProjectProfileToItem(ProjectItem *item)
     return profileUpdated;
 }
 
+QString ProjectList::getDocumentProperty(const QString &key) const
+{
+    return m_doc->getDocumentProperty(key);
+}
+
 bool ProjectList::useProxy() const
 {
     return m_doc->getDocumentProperty("enableproxy").toInt();
@@ -1723,14 +1762,9 @@ bool ProjectList::generateProxy() const
     return m_doc->getDocumentProperty("generateproxy").toInt();
 }
 
-int ProjectList::proxyMinSize() const
-{
-    return m_doc->getDocumentProperty("proxyminsize").toInt();
-}
-
-QString ProjectList::proxyParams() const
+bool ProjectList::generateImageProxy() const
 {
-    return m_doc->getDocumentProperty("proxyparams").simplified();
+    return m_doc->getDocumentProperty("generateimageproxy").toInt();
 }
 
 void ProjectList::slotReplyGetImage(const QString &clipId, const QPixmap &pix)
@@ -2067,12 +2101,19 @@ QMap <QString, QString> ProjectList::getProxies()
     return list;
 }
 
-void ProjectList::slotCreateProxy(const QString id)
+void ProjectList::slotCreateProxy(const QString id, bool createProducer)
 {
     ProjectItem *item = getItemById(id);
     if (!item || item->isProxyRunning()) return;
+    
+    // If proxy producer already exists, skip creation
+    if (!createProducer) {
+        setProxyStatus(id, PROXYDONE);
+        return;
+    }
     setProxyStatus(id, PROXYWAITING);
     if (m_abortProxyId.contains(id)) m_abortProxyId.removeAll(id);
+    emit projectModified();
     QtConcurrent::run(this, &ProjectList::slotGenerateProxy, id);
 }
 
@@ -2081,6 +2122,7 @@ 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);
@@ -2115,10 +2157,62 @@ void ProjectList::slotGenerateProxy(const QString id)
         file.close();
         QFile::remove(path);
     }
+    if (item->clipType() == IMAGE) {
+        // Image proxy
+        QImage i(url);
+        if (i.isNull()) {
+            // Cannot load image
+            setProxyStatus(id, PROXYCRASHED);
+            return;
+        }
+        QImage proxy;
+        // Images are scaled to profile size. 
+        //TODO: Make it be configurable?
+        if (i.width() > i.height()) proxy = i.scaledToWidth(m_render->frameRenderWidth());
+        else proxy = i.scaledToHeight(m_render->renderHeight());
+        int exif_orientation = QString(item->referencedClip()->producerProperty("_exif_orientation")).toInt();
+        if (exif_orientation > 1) {
+            // Rotate image according to exif data
+            QImage processed;
+            QMatrix matrix;
+
+            switch ( exif_orientation ) {
+                case 2:
+                  matrix.scale( -1, 1 );
+                  break;
+                case 3:
+                  matrix.rotate( 180 );
+                  break;
+                case 4:
+                  matrix.scale( 1, -1 );
+                  break;
+                case 5:
+                  matrix.rotate( 270 );
+                  matrix.scale( -1, 1 );
+                  break;
+                case 6:
+                  matrix.rotate( 90 );
+                  break;
+                case 7:
+                  matrix.rotate( 90 );
+                  matrix.scale( -1, 1 );
+                  break;
+                case 8:
+                  matrix.rotate( 270 );
+                  break;
+              }
+              processed = proxy.transformed( matrix );
+              processed.save(path);
+        }
+        else proxy.save(path);
+        setProxyStatus(id, PROXYDONE);
+        slotGotProxy(id);
+        return;
+    }
 
     QStringList parameters;
     parameters << "-i" << url;
-    QString params = proxyParams();
+    QString params = m_doc->getDocumentProperty("proxyparams").simplified();
     foreach(QString s, params.split(' '))
     parameters << s;
 
@@ -2160,6 +2254,9 @@ 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;
@@ -2172,25 +2269,56 @@ 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() > m_doc->getDocumentProperty("proxyminsize").toInt()) {
+                    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() + "." + m_doc->getDocumentProperty("proxyextension"));
+                        new EditClipCommand(this, clip->getId(), oldProps, newProps, true, command);
+                    }
+                }
+            }
+            else if (item->hasProxy()) {
+                // remove proxy
+                QMap <QString, QString> newProps;
+                newProps.insert("proxy", QString());
+                newProps.insert("replace", "1");
+                // insert required duration for proxy
+                newProps.insert("proxy_out", item->referencedClip()->producerProperty("out"));
+                new EditClipCommand(this, item->clipId(), item->referencedClip()->properties(), newProps, true, command);
+            }
+        }
+        else if (t == IMAGE && item->referencedClip() != NULL) {
+            if  (generateImageProxy() && useProxy()) {
                 DocClipBase *clip = item->referencedClip();
-                if (clip->getProperty("frame_size").section('x', 0, 0).toInt() > proxyMinSize()) {
-                    kDebug()<<"// UPDATE ST PROXY";
-                    slotCreateProxy(clip->getId());
-                    setProxyStatus(item, PROXYWAITING);
+                int maxImageSize = m_doc->getDocumentProperty("proxyimageminsize").toInt();
+                if (clip->getProperty("frame_size").section('x', 0, 0).toInt() > maxImageSize || clip->getProperty("frame_size").section('x', 1, 1).toInt() > maxImageSize) {
+                    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() + ".png");
+                        new EditClipCommand(this, clip->getId(), oldProps, newProps, true, command);
+                    }
                 }
             }
-            else if (!item->hasProxy()) {
+            else if (item->hasProxy()) {
                 // remove proxy
-                item->referencedClip()->clearProperty("proxy");
-                QDomElement e = item->toXml().cloneNode().toElement();
-                e.removeAttribute("file_hash");
-                e.setAttribute("replace", 1);
-                m_infoQueue.insert(item->clipId(), e);
+                QMap <QString, QString> newProps;
+                newProps.insert("proxy", QString());
+                newProps.insert("replace", "1");
+                new EditClipCommand(this, item->clipId(), item->referencedClip()->properties(), newProps, true, command);
             }
         }
         ++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);
 }
 
@@ -2199,13 +2327,15 @@ void ProjectList::slotProxyCurrentItem(bool doProxy)
     QList<QTreeWidgetItem *> list = m_listView->selectedItems();
     QTreeWidgetItem *listItem;
     QUndoCommand *command = new QUndoCommand();
-    command->setText(i18np("Edit clip", "Edit clips", list.count()));
+    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);
@@ -2218,17 +2348,25 @@ void ProjectList::slotProxyCurrentItem(bool doProxy)
         if (listItem->type() == PROJECTCLIPTYPE) {
             ProjectItem *item = static_cast <ProjectItem*>(listItem);
             CLIPTYPE t = item->clipType();
-            if ((t == VIDEO || t == AV || t == UNKNOWN) && item->referencedClip()) {
+            if ((t == VIDEO || t == AV || t == UNKNOWN || t == IMAGE) && item->referencedClip()) {
+                oldProps = item->referencedClip()->properties();
                 if (doProxy) {
                     newProps.clear();
-                    QString path = proxydir + item->referencedClip()->getClipHash() + ".avi";                
+                    QString path = proxydir + item->referencedClip()->getClipHash() + "." + (t == IMAGE ? "png" : m_doc->getDocumentProperty("proxyextension"));
                     newProps.insert("proxy", path);
+                    // insert required duration for proxy
+                    newProps.insert("proxy_out", item->referencedClip()->producerProperty("out"));
+                    // We need to insert empty proxy so that undo will work
+                    oldProps.insert("proxy", QString());
                 }
-                new EditClipCommand(this, item->clipId(), item->referencedClip()->properties(), newProps, true, command);
+                new EditClipCommand(this, item->clipId(), oldProps, newProps, true, command);
             }
         }
     }
-    m_doc->commandStack()->push(command);
+    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);
 }
 
@@ -2252,4 +2390,23 @@ void ProjectList::monitorItemEditing(bool enable)
     else disconnect(m_listView, SIGNAL(itemChanged(QTreeWidgetItem *, int)), this, SLOT(slotItemEdited(QTreeWidgetItem *, int)));     
 }
 
+QStringList ProjectList::expandedFolders() const
+{
+    QStringList result;
+    FolderProjectItem *item;
+    QTreeWidgetItemIterator it(m_listView);
+    while (*it) {
+        if ((*it)->type() != PROJECTFOLDERTYPE) {
+            ++it;
+            continue;
+        }
+        if ((*it)->isExpanded()) {
+            item = static_cast<FolderProjectItem *>(*it);
+            result.append(item->clipId());
+        }
+        ++it;
+    }
+    return result;
+}
+
 #include "projectlist.moc"