+void ProjectList::slotForceProcessing(const QString &id)
+{
+ m_render->forceProcessing(id);
+}
+
+void ProjectList::slotAddOrUpdateSequence(const QString frameName)
+{
+ QString fileName = KUrl(frameName).fileName().section('_', 0, -2);
+ QStringList list;
+ QString pattern = SlideshowClip::selectedPath(frameName, false, QString(), &list);
+ int count = list.count();
+ if (count > 1) {
+ const QList <DocClipBase *> existing = m_doc->clipManager()->getClipByResource(pattern);
+ if (!existing.isEmpty()) {
+ // Sequence already exists, update
+ QString id = existing.at(0)->getId();
+ //ProjectItem *item = getItemById(id);
+ QMap <QString, QString> oldprops;
+ QMap <QString, QString> newprops;
+ int ttl = existing.at(0)->getProperty("ttl").toInt();
+ oldprops["out"] = existing.at(0)->getProperty("out");
+ newprops["out"] = QString::number(ttl * count - 1);
+ slotUpdateClipProperties(id, newprops);
+ EditClipCommand *command = new EditClipCommand(this, id, oldprops, newprops, false);
+ m_commandStack->push(command);
+ } else {
+ // Create sequence
+ QStringList groupInfo = getGroup();
+ m_doc->slotCreateSlideshowClipFile(fileName, pattern, count, m_timecode.reformatSeparators(KdenliveSettings::sequence_duration()),
+ false, false, false,
+ m_timecode.getTimecodeFromFrames(int(ceil(m_timecode.fps()))), QString(), 0,
+ QString(), groupInfo.at(0), groupInfo.at(1));
+ }
+ } else emit displayMessage(i18n("Sequence not found"), -2);
+}
+
+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, PROXYCRASHED);
+ return;
+ }
+ setProxyStatus(item, PROXYWAITING);
+ 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, PROXYDONE);
+ 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();
+ m_proxyList.append(info);
+ 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));
+ }
+ }
+ 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 (!path.isEmpty() && m_processingProxy.contains(path)) {
+ m_abortProxy << path;
+ setProxyStatus(item, NOPROXY);
+ }
+ else {
+ setProxyStatus(item, NOPROXY);
+ slotGotProxy(item);
+ }
+}
+
+void ProjectList::slotGenerateProxy()
+{
+ while (!m_proxyList.isEmpty() && !m_abortAllProxies) {
+ emit projectModified();
+ PROXYINFO info = m_proxyList.takeFirst();
+ if (m_abortProxy.contains(info.dest)) {
+ m_abortProxy.removeAll(info.dest);
+ continue;
+ }
+ // Get the list of clips that will need to get progress info
+ QTreeWidgetItemIterator it(m_listView);
+ QList <ProjectItem *> processingItems;
+ while (*it && !m_abortAllProxies) {
+ if ((*it)->type() == PROJECTCLIPTYPE) {
+ ProjectItem *item = static_cast <ProjectItem *>(*it);
+ if (item->referencedClip()->getProperty("proxy") == info.dest) {
+ processingItems.append(item);
+ }
+ }
+ ++it;
+ }
+
+ // Make sure proxy path is writable
+ QFile file(info.dest);
+ if (!file.open(QIODevice::WriteOnly)) {
+ for (int i = 0; i < processingItems.count(); i++)
+ setProxyStatus(processingItems.at(i), PROXYCRASHED);
+ m_processingProxy.removeAll(info.dest);
+ continue;
+ }
+ file.close();
+ QFile::remove(info.dest);
+
+ for (int i = 0; i < processingItems.count(); i++)
+ setProxyStatus(processingItems.at(i), CREATINGPROXY);
+
+ // Special case: playlist clips (.mlt or .kdenlive project files)
+ if (info.type == PLAYLIST) {
+ // change FFmpeg params to MLT format
+ QStringList parameters;
+ parameters << info.src;
+ parameters << "-consumer" << "avformat:" + info.dest;
+ QStringList params = m_doc->getDocumentProperty("proxyparams").simplified().split('-', QString::SkipEmptyParts);
+
+ foreach(QString s, params) {
+ s = s.simplified();
+ if (s.count(' ') == 0) {
+ s.append("=1");
+ }
+ else s.replace(' ', '=');
+ parameters << s;
+ }
+
+ parameters.append(QString("real_time=-%1").arg(KdenliveSettings::mltthreads()));
+
+ //TODO: currently, when rendering an xml file through melt, the display ration is lost, so we enforce it manualy
+ double display_ratio = KdenliveDoc::getDisplayRatio(info.src);
+ parameters << "aspect=" + QString::number(display_ratio);
+
+ //kDebug()<<"TRANSCOD: "<<parameters;
+ QProcess myProcess;
+ myProcess.setProcessChannelMode(QProcess::MergedChannels);
+ myProcess.start(KdenliveSettings::rendererpath(), parameters);
+ myProcess.waitForStarted();
+ int result = -1;
+ int duration = 0;
+ while (myProcess.state() != QProcess::NotRunning) {
+ // building proxy file
+ if (m_abortProxy.contains(info.dest) || m_abortAllProxies) {
+ myProcess.close();
+ myProcess.waitForFinished();
+ QFile::remove(info.dest);
+ m_abortProxy.removeAll(info.dest);
+ m_processingProxy.removeAll(info.dest);
+ for (int i = 0; i < processingItems.count(); i++)
+ setProxyStatus(processingItems.at(i), NOPROXY);
+ result = -2;
+ }
+ else {
+ QString log = QString(myProcess.readAll());
+ processLogInfo(processingItems, &duration, log);
+ }
+ myProcess.waitForFinished(500);
+ }
+ myProcess.waitForFinished();
+ m_processingProxy.removeAll(info.dest);
+ if (result == -1) result = myProcess.exitStatus();
+ if (result == 0) {
+ // proxy successfully created
+ for (int i = 0; i < processingItems.count(); i++)
+ setProxyStatus(processingItems.at(i), PROXYDONE);
+ slotGotProxy(info.dest);
+ }
+ else if (result == 1) {
+ // Proxy process crashed
+ QFile::remove(info.dest);
+ for (int i = 0; i < processingItems.count(); i++)
+ setProxyStatus(processingItems.at(i), PROXYCRASHED);
+ }
+ continue;
+ }
+
+ if (info.type == IMAGE) {
+ // Image proxy
+ QImage i(info.src);
+ if (i.isNull()) {
+ // Cannot load image
+ for (int i = 0; i < processingItems.count(); i++)
+ setProxyStatus(processingItems.at(i), PROXYCRASHED);
+ continue;
+ }
+ 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());
+ if (info.exif > 1) {
+ // Rotate image according to exif data
+ QImage processed;
+ QMatrix matrix;
+
+ switch ( info.exif ) {
+ 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(info.dest);
+ }
+ else proxy.save(info.dest);
+ for (int i = 0; i < processingItems.count(); i++)
+ setProxyStatus(processingItems.at(i), PROXYDONE);
+ slotGotProxy(info.dest);
+ m_abortProxy.removeAll(info.dest);
+ m_processingProxy.removeAll(info.dest);
+ continue;
+ }
+
+ QStringList parameters;
+ parameters << "-i" << info.src;
+ QString params = m_doc->getDocumentProperty("proxyparams").simplified();
+ foreach(QString s, params.split(' '))
+ parameters << s;
+
+ // Make sure we don't block when proxy file already exists
+ parameters << "-y";
+ parameters << info.dest;
+ QProcess myProcess;
+ myProcess.setProcessChannelMode(QProcess::MergedChannels);
+ myProcess.start("ffmpeg", parameters);
+ myProcess.waitForStarted();
+ int result = -1;
+ int duration = 0;
+
+ while (myProcess.state() != QProcess::NotRunning) {
+ // building proxy file
+ if (m_abortProxy.contains(info.dest) || m_abortAllProxies) {
+ myProcess.close();
+ myProcess.waitForFinished();
+ m_abortProxy.removeAll(info.dest);
+ m_processingProxy.removeAll(info.dest);
+ QFile::remove(info.dest);
+ if (!m_abortAllProxies) {
+ for (int i = 0; i < processingItems.count(); i++)
+ setProxyStatus(processingItems.at(i), NOPROXY);
+ }
+ else continue;
+ result = -2;
+ }
+ else {
+ QString log = QString(myProcess.readAll());
+ processLogInfo(processingItems, &duration, log);
+ }
+ myProcess.waitForFinished(500);
+ }
+ myProcess.waitForFinished();
+ m_abortProxy.removeAll(info.dest);
+ m_processingProxy.removeAll(info.dest);
+ if (result == -1) {
+ result = myProcess.exitStatus();
+ }
+
+ // FFmpeg process terminated normally, but make sure proxy clip exists
+ if (result != -2 && QFileInfo(info.dest).size() == 0) {
+ result = QProcess::CrashExit;
+ }
+
+ if (result == QProcess::NormalExit) {
+ // proxy successfully created
+ for (int i = 0; i < processingItems.count(); i++)
+ setProxyStatus(processingItems.at(i), PROXYDONE);
+ slotGotProxy(info.dest);
+ }
+ else if (result == QProcess::CrashExit) {
+ // Proxy process crashed
+ QFile::remove(info.dest);
+ for (int i = 0; i < processingItems.count(); i++)
+ setProxyStatus(processingItems.at(i), PROXYCRASHED);
+ }
+ }
+}
+
+
+void ProjectList::processLogInfo(QList <ProjectItem *>items, int *duration, const QString &log)
+{
+ int progress;
+ if (*duration == 0) {
+ if (log.contains("Duration:")) {
+ QString data = log.section("Duration:", 1, 1).section(',', 0, 0).simplified();
+ QStringList numbers = data.split(':');
+ *duration = (int) (numbers.at(0).toInt() * 3600 + numbers.at(1).toInt() * 60 + numbers.at(2).toDouble());
+ }
+ }
+ else if (log.contains("time=")) {
+ QString time = log.section("time=", 1, 1).simplified().section(' ', 0, 0);
+ if (time.contains(':')) {
+ QStringList numbers = time.split(':');
+ progress = numbers.at(0).toInt() * 3600 + numbers.at(1).toInt() * 60 + numbers.at(2).toDouble();
+ }
+ else progress = (int) time.toDouble();
+ for (int i = 0; i < items.count(); i++)
+ setProxyStatus(items.at(i), CREATINGPROXY, (int) (100.0 * progress / (*duration)));
+ }
+}
+
+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()->properties(), 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"));
+ }
+ 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()->properties(), props, true, proxyCommand);
+
+ }
+ }
+ ++it;
+ }
+ if (proxyCommand->childCount() == 0)
+ delete proxyCommand;
+ else
+ m_commandStack->push(proxyCommand);
+ QFile::remove(proxyPath);
+}
+
+void ProjectList::setProxyStatus(ProjectItem *item, PROXYSTATUS status, int progress)
+{
+ if (item == NULL || m_abortAllProxies) return;
+ monitorItemEditing(false);
+ item->setProxyStatus(status, progress);
+ if (status == PROXYCRASHED) {
+ 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;
+}
+