+QMap <QString, QString> ProjectList::getProxies()
+{
+ QMap <QString, QString> list;
+ ProjectItem *item;
+ QTreeWidgetItemIterator it(m_listView);
+ while (*it) {
+ if ((*it)->type() != PROJECTCLIPTYPE) {
+ ++it;
+ continue;
+ }
+ item = static_cast<ProjectItem *>(*it);
+ if (item && item->referencedClip() != NULL) {
+ if (item->hasProxy()) {
+ QString proxy = item->referencedClip()->getProperty("proxy");
+ list.insert(proxy, item->clipUrl().path());
+ }
+ }
+ ++it;
+ }
+ return list;
+}
+
+void ProjectList::slotCreateProxy(const QString id)
+{
+ ProjectItem *item = getItemById(id);
+ if (!item || item->isProxyRunning() || item->referencedClip()->isPlaceHolder()) return;
+ QString path = item->referencedClip()->getProperty("proxy");
+ if (path.isEmpty()) {
+ setProxyStatus(item, JOBCRASHED);
+ return;
+ }
+ setProxyStatus(item, JOBWAITING);
+ if (m_abortProxy.contains(path)) m_abortProxy.removeAll(path);
+ if (m_processingProxy.contains(path)) {
+ // Proxy is already being generated
+ return;
+ }
+ if (QFileInfo(path).size() > 0) {
+ // Proxy already created
+ setProxyStatus(item, JOBDONE);
+ slotGotProxy(path);
+ return;
+ }
+ m_processingProxy.append(path);
+
+ /*PROXYINFO info;
+ info.dest = path;
+ info.src = item->clipUrl().path();
+ info.type = item->clipType();
+ info.exif = QString(item->referencedClip()->producerProperty("_exif_orientation")).toInt();*/
+ ProxyJob *job = new ProxyJob(PROXYJOB, item->clipType(), item->clipId(), QStringList() << path << item->clipUrl().path() << item->referencedClip()->producerProperty("_exif_orientation") << m_doc->getDocumentProperty("proxyparams").simplified() << QString::number(m_render->frameRenderWidth()) << QString::number(m_render->renderHeight()));
+ m_jobList.append(job);
+ int ct = 0;
+ if (!m_proxyThreads.futures().isEmpty()) {
+ // Remove inactive threads
+ QList <QFuture<void> > futures = m_proxyThreads.futures();
+ m_proxyThreads.clearFutures();
+ for (int i = 0; i < futures.count(); i++)
+ if (!futures.at(i).isFinished()) {
+ m_proxyThreads.addFuture(futures.at(i));
+ ct++;
+ }
+ }
+ emit jobCount(ct + m_jobList.count());
+
+ if (m_proxyThreads.futures().isEmpty() || m_proxyThreads.futures().count() < KdenliveSettings::proxythreads()) m_proxyThreads.addFuture(QtConcurrent::run(this, &ProjectList::slotGenerateProxy));
+}
+
+void ProjectList::slotAbortProxy(const QString id, const QString path)
+{
+ QTreeWidgetItemIterator it(m_listView);
+ ProjectItem *item = getItemById(id);
+ if (!item) return;
+ if (!path.isEmpty() && m_processingProxy.contains(path)) {
+ m_abortProxy << path;
+ }
+ else {
+ // Should only be done if we were already using a proxy producer
+ if (!item->isProxyRunning()) slotGotProxy(item);
+ }
+}
+
+void ProjectList::slotGenerateProxy()
+{
+ while (!m_jobList.isEmpty() && !m_abortAllJobs) {
+ emit projectModified();
+ AbstractClipJob *job = m_jobList.takeFirst();
+ // Get jobs count
+ int ct = 0;
+ QList <QFuture<void> > futures = m_proxyThreads.futures();
+ for (int i = 0; i < futures.count(); i++)
+ if (futures.at(i).isRunning()) {
+ ct++;
+ }
+ emit jobCount(ct + m_jobList.count());
+
+ if (job->jobType == PROXYJOB) {
+ //ProxyJob *pjob = static_cast<ProxyJob *> (job);
+ kDebug()<<"// STARTING JOB: "<<job->destination();
+ if (m_abortProxy.contains(job->destination())) {
+ m_abortProxy.removeAll(job->destination());
+ m_processingProxy.removeAll(job->destination());
+ emit cancelRunningJob(job->clipId(), job->cancelProperties());
+ delete job;
+ continue;
+ }
+ }
+
+ // Get the list of clips that will need to get progress info
+ QTreeWidgetItemIterator it(m_listView);
+ QList <ProjectItem *> processingItems;
+ ProjectItem *item = getItemById(job->clipId());
+ processingItems.append(item);
+
+ /*while (*it && !m_abortAllJobs) {
+ if ((*it)->type() == PROJECTCLIPTYPE) {
+ ProjectItem *item = static_cast <ProjectItem *>(*it);
+ if (item->referencedClip()->getProperty("proxy") == job->destination()) {
+ processingItems.append(item);
+ }
+ }
+ ++it;
+ }*/
+
+ // Make sure proxy path is writable
+ QFile file(job->destination());
+ if (!file.open(QIODevice::WriteOnly)) {
+ for (int i = 0; i < processingItems.count(); i++)
+ setProxyStatus(processingItems.at(i), JOBCRASHED);
+ m_processingProxy.removeAll(job->destination());
+ delete job;
+ continue;
+ }
+ file.close();
+ QFile::remove(job->destination());
+
+ for (int i = 0; i < processingItems.count(); i++)
+ setProxyStatus(processingItems.at(i), CREATINGJOB, 0); //, job->description);
+ bool success;
+ QProcess *jobProcess = job->startJob(&success);
+
+ int result = -1;
+ if (jobProcess == NULL) {
+ // job is finished
+ for (int i = 0; i < processingItems.count(); i++)
+ setProxyStatus(processingItems.at(i), success ? JOBDONE : JOBCRASHED);
+ if (success) {
+ slotGotProxy(job->destination());
+ }
+ else {
+ QFile::remove(job->destination());
+ }
+ m_abortProxy.removeAll(job->destination());
+ m_processingProxy.removeAll(job->destination());
+ delete job;
+ continue;
+ }
+ else while (jobProcess->state() != QProcess::NotRunning) {
+ // building proxy file
+ if (m_abortProxy.contains(job->destination()) || m_abortAllJobs) {
+ jobProcess->close();
+ jobProcess->waitForFinished();
+ QFile::remove(job->destination());
+ m_abortProxy.removeAll(job->destination());
+ m_processingProxy.removeAll(job->destination());
+ if (!m_closing) {
+ emit cancelRunningJob(job->clipId(), job->cancelProperties());
+ /*for (int i = 0; i < processingItems.count(); i++)
+ setProxyStatus(processingItems.at(i), NOJOB);*/
+ }
+ else continue;
+ result = -2;
+ }
+ else {
+ int progress = job->processLogInfo();
+ if (progress > -1) processLogInfo(processingItems, progress);
+ }
+ jobProcess->waitForFinished(500);
+ }
+ jobProcess->waitForFinished();
+ m_processingProxy.removeAll(job->destination());
+ if (result == -1) result = jobProcess->exitStatus();
+
+ if (result != -2 && QFileInfo(job->destination()).size() == 0) {
+ result = QProcess::CrashExit;
+ }
+
+ if (result == QProcess::NormalExit) {
+ // proxy successfully created
+ for (int i = 0; i < processingItems.count(); i++)
+ setProxyStatus(processingItems.at(i), JOBDONE);
+ slotGotProxy(job->destination());
+ }
+ else if (result == QProcess::CrashExit) {
+ // Proxy process crashed
+ QFile::remove(job->destination());
+ for (int i = 0; i < processingItems.count(); i++)
+ setProxyStatus(processingItems.at(i), JOBCRASHED);
+ }
+ delete job;
+ continue;
+ }
+ // Thread finished, update count
+ int ct = -1; // -1 because we don't want to count this terminating thread
+ QList <QFuture<void> > futures = m_proxyThreads.futures();
+ for (int i = 0; i < futures.count(); i++)
+ if (futures.at(i).isRunning()) {
+ ct++;
+ }
+ emit jobCount(ct + m_jobList.count());
+}
+
+
+void ProjectList::processLogInfo(QList <ProjectItem *>items, int progress)
+{
+ for (int i = 0; i < items.count(); i++)
+ setProxyStatus(items.at(i), CREATINGJOB, progress);
+}
+
+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;
+ continue;
+ }
+ item = static_cast<ProjectItem *>(*it);
+ if (item == NULL) {
+ ++it;
+ continue;
+ }
+ CLIPTYPE t = item->clipType();
+ if ((t == VIDEO || t == AV || t == UNKNOWN) && item->referencedClip() != NULL) {
+ 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()->currentProperties(newProps), newProps, true, command);
+ }
+ }
+ else if (t == IMAGE && item->referencedClip() != NULL) {
+ if (generateImageProxy() && useProxy()) {
+ DocClipBase *clip = item->referencedClip();
+ 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()) {
+ // remove proxy
+ 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;
+}
+
+void ProjectList::slotProxyCurrentItem(bool doProxy, ProjectItem *itemToProxy)
+{
+ QList<QTreeWidgetItem *> list;
+ if (itemToProxy == NULL) list = m_listView->selectedItems();
+ else list << itemToProxy;
+
+ // expand list (folders, subclips) to get real clips
+ QTreeWidgetItem *listItem;
+ QList<ProjectItem *> clipList;
+ for (int i = 0; i < list.count(); i++) {
+ listItem = list.at(i);
+ if (listItem->type() == PROJECTFOLDERTYPE) {
+ for (int j = 0; j < listItem->childCount(); j++) {
+ QTreeWidgetItem *sub = listItem->child(j);
+ if (sub->type() == PROJECTCLIPTYPE) {
+ ProjectItem *item = static_cast <ProjectItem*>(sub);
+ if (!clipList.contains(item)) clipList.append(item);
+ }
+ }
+ }
+ else if (listItem->type() == PROJECTSUBCLIPTYPE) {
+ QTreeWidgetItem *sub = listItem->parent();
+ ProjectItem *item = static_cast <ProjectItem*>(sub);
+ if (!clipList.contains(item)) clipList.append(item);
+ }
+ else if (listItem->type() == PROJECTCLIPTYPE) {
+ ProjectItem *item = static_cast <ProjectItem*>(listItem);
+ if (!clipList.contains(item)) clipList.append(item);
+ }
+ }
+
+ QUndoCommand *command = new QUndoCommand();
+ if (doProxy) command->setText(i18np("Add proxy clip", "Add proxy clips", clipList.count()));
+ else command->setText(i18np("Remove proxy clip", "Remove proxy clips", clipList.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 < clipList.count(); i++) {
+ ProjectItem *item = clipList.at(i);
+ CLIPTYPE t = item->clipType();
+ if ((t == VIDEO || t == AV || t == UNKNOWN || t == IMAGE || t == PLAYLIST) && item->referencedClip()) {
+ if ((doProxy && item->hasProxy()) || (!doProxy && !item->hasProxy() && item->referencedClip()->getProducer() != NULL)) continue;
+ DocClipBase *clip = item->referencedClip();
+ if (!clip || !clip->isClean() || m_render->isProcessing(item->clipId())) {
+ kDebug()<<"//// TRYING TO PROXY: "<<item->clipId()<<", but it is busy";
+ continue;
+ }
+
+ //oldProps = clip->properties();
+ if (doProxy) {
+ newProps.clear();
+ QString path = proxydir + clip->getClipHash() + "." + (t == IMAGE ? "png" : m_doc->getDocumentProperty("proxyextension"));
+ // insert required duration for proxy
+ 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());
+ }
+ else if (item->referencedClip()->getProducer() == NULL) {
+ // Force clip reload
+ kDebug()<<"// CLIP HAD NULL PROD------------";
+ newProps.insert("resource", item->referencedClip()->getProperty("resource"));
+ }
+ // We need to insert empty proxy so that undo will work
+ oldProps = clip->currentProperties(newProps);
+ if (doProxy) oldProps.insert("proxy", "-");
+ new EditClipCommand(this, item->clipId(), oldProps, newProps, true, command);
+ }
+ }
+ if (command->childCount() > 0) {
+ m_doc->commandStack()->push(command);
+ }
+ else delete command;
+}
+
+
+void ProjectList::slotDeleteProxy(const QString proxyPath)
+{
+ if (proxyPath.isEmpty()) return;
+ QUndoCommand *proxyCommand = new QUndoCommand();
+ proxyCommand->setText(i18n("Remove Proxy"));
+ QTreeWidgetItemIterator it(m_listView);
+ ProjectItem *item;
+ while (*it) {
+ if ((*it)->type() == PROJECTCLIPTYPE) {
+ item = static_cast <ProjectItem *>(*it);
+ if (item->referencedClip()->getProperty("proxy") == proxyPath) {
+ QMap <QString, QString> props;
+ props.insert("proxy", QString());
+ new EditClipCommand(this, item->clipId(), item->referencedClip()->currentProperties(props), props, true, proxyCommand);
+
+ }
+ }
+ ++it;
+ }
+ if (proxyCommand->childCount() == 0)
+ delete proxyCommand;
+ else
+ m_commandStack->push(proxyCommand);
+ QFile::remove(proxyPath);
+}
+
+void ProjectList::setProxyStatus(ProjectItem *item, CLIPJOBSTATUS status, int progress)
+{
+ if (item == NULL || m_abortAllJobs) return;
+ monitorItemEditing(false);
+ item->setProxyStatus(status, progress);
+ if (status == JOBCRASHED) {
+ DocClipBase *clip = item->referencedClip();
+ if (!clip) {
+ kDebug()<<"// PROXY CRASHED";
+ }
+ else if (clip->getProducer() == NULL && !clip->isPlaceHolder()) {
+ // disable proxy and fetch real clip
+ clip->setProperty("proxy", "-");
+ QDomElement xml = clip->toXML();
+ m_render->getFileProperties(xml, clip->getId(), m_listView->iconSize().height(), true);
+ }
+ else {
+ // Disable proxy for this clip
+ clip->setProperty("proxy", "-");
+ }
+ }
+ monitorItemEditing(true);
+}
+
+void ProjectList::monitorItemEditing(bool enable)
+{
+ if (enable) connect(m_listView, SIGNAL(itemChanged(QTreeWidgetItem *, int)), this, SLOT(slotItemEdited(QTreeWidgetItem *, int)));
+ 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;
+}
+
+void ProjectList::processThumbOverlays(ProjectItem *item, QPixmap &pix)
+{
+ if (item->hasProxy()) {
+ QPainter p(&pix);
+ QColor c = QPalette().base().color();
+ c.setAlpha(160);
+ QBrush br(c);
+ p.setBrush(br);
+ p.setPen(Qt::NoPen);
+ QRect r(1, 1, 10, 10);
+ p.drawRect(r);
+ p.setPen(QPalette().text().color());
+ p.drawText(r, Qt::AlignCenter, i18nc("The first letter of Proxy, used as abbreviation", "P"));
+ }
+}
+
+void ProjectList::slotCancelJobs()
+{
+ m_abortAllJobs = true;
+ m_proxyThreads.waitForFinished();
+ m_proxyThreads.clearFutures();
+ QUndoCommand *command = new QUndoCommand();
+ command->setText(i18np("Cancel job", "Cancel jobs", m_jobList.count()));
+ for (int i = 0; i < m_jobList.count(); i++) {
+ ProjectItem *item = getItemById(m_jobList.at(i)->clipId());
+ if (!item || !item->referencedClip()) continue;
+ QMap <QString, QString> newProps = m_jobList.at(i)->cancelProperties();
+ if (newProps.isEmpty()) continue;
+ QMap <QString, QString> oldProps = item->referencedClip()->currentProperties(newProps);
+ new EditClipCommand(this, m_jobList.at(i)->clipId(), oldProps, newProps, true, command);
+ }
+ if (command->childCount() > 0) {
+ m_doc->commandStack()->push(command);
+ }
+ else delete command;
+ if (!m_jobList.isEmpty()) qDeleteAll(m_jobList);
+ m_processingProxy.clear();
+ m_jobList.clear();
+ m_abortAllJobs = false;
+ m_infoLabel->slotSetJobCount(0);
+}
+
+void ProjectList::slotCancelRunningJob(const QString id, stringMap newProps)
+{
+ if (newProps.isEmpty()) return;
+ ProjectItem *item = getItemById(id);
+ if (!item || !item->referencedClip()) return;
+ QMap <QString, QString> oldProps = item->referencedClip()->currentProperties(newProps);
+ if (newProps == oldProps) return;
+ QMapIterator<QString, QString> i(oldProps);
+ EditClipCommand *command = new EditClipCommand(this, id, oldProps, newProps, true);
+ m_commandStack->push(command);
+}
+