+
+void ProjectList::slotAddClipCut(const QString &id, int in, int out)
+{
+ ProjectItem *clip = getItemById(id);
+ if (clip == NULL || clip->referencedClip()->hasCutZone(QPoint(in, out)))
+ return;
+ AddClipCutCommand *command = new AddClipCutCommand(this, id, in, out, QString(), true, false);
+ m_commandStack->push(command);
+}
+
+void ProjectList::addClipCut(const QString &id, int in, int out, const QString desc, bool newItem)
+{
+ ProjectItem *clip = getItemById(id);
+ if (clip) {
+ DocClipBase *base = clip->referencedClip();
+ base->addCutZone(in, out);
+ monitorItemEditing(false);
+ SubProjectItem *sub = new SubProjectItem(clip, in, out, desc);
+ if (newItem && desc.isEmpty() && !m_listView->isColumnHidden(1)) {
+ if (!clip->isExpanded())
+ clip->setExpanded(true);
+ m_listView->scrollToItem(sub);
+ m_listView->editItem(sub, 1);
+ }
+ QPixmap p = clip->referencedClip()->extractImage(in, (int)(sub->sizeHint(0).height() * m_render->dar()), sub->sizeHint(0).height() - 2);
+ sub->setData(0, Qt::DecorationRole, p);
+ m_doc->cachePixmap(clip->getClipHash() + '#' + QString::number(in), p);
+ monitorItemEditing(true);
+ }
+ emit projectModified();
+}
+
+void ProjectList::removeClipCut(const QString &id, int in, int out)
+{
+ ProjectItem *clip = getItemById(id);
+ if (clip) {
+ DocClipBase *base = clip->referencedClip();
+ base->removeCutZone(in, out);
+ SubProjectItem *sub = getSubItem(clip, QPoint(in, out));
+ if (sub) {
+ monitorItemEditing(false);
+ delete sub;
+ monitorItemEditing(true);
+ }
+ }
+ emit projectModified();
+}
+
+SubProjectItem *ProjectList::getSubItem(ProjectItem *clip, QPoint zone)
+{
+ SubProjectItem *sub = NULL;
+ if (clip) {
+ for (int i = 0; i < clip->childCount(); i++) {
+ QTreeWidgetItem *it = clip->child(i);
+ if (it->type() == PROJECTSUBCLIPTYPE) {
+ sub = static_cast <SubProjectItem*>(it);
+ if (sub->zone() == zone)
+ break;
+ else
+ sub = NULL;
+ }
+ }
+ }
+ return sub;
+}
+
+void ProjectList::slotUpdateClipCut(QPoint p)
+{
+ if (!m_listView->currentItem() || m_listView->currentItem()->type() != PROJECTSUBCLIPTYPE)
+ return;
+ SubProjectItem *sub = static_cast <SubProjectItem*>(m_listView->currentItem());
+ ProjectItem *item = static_cast <ProjectItem *>(sub->parent());
+ EditClipCutCommand *command = new EditClipCutCommand(this, item->clipId(), sub->zone(), p, sub->text(1), sub->text(1), true);
+ m_commandStack->push(command);
+}
+
+void ProjectList::doUpdateClipCut(const QString &id, const QPoint oldzone, const QPoint zone, const QString &comment)
+{
+ ProjectItem *clip = getItemById(id);
+ SubProjectItem *sub = getSubItem(clip, oldzone);
+ if (sub == NULL || clip == NULL)
+ return;
+ DocClipBase *base = clip->referencedClip();
+ base->updateCutZone(oldzone.x(), oldzone.y(), zone.x(), zone.y(), comment);
+ monitorItemEditing(false);
+ sub->setZone(zone);
+ sub->setDescription(comment);
+ monitorItemEditing(true);
+ emit projectModified();
+}
+
+void ProjectList::slotForceProcessing(const QString &id)
+{
+ while (m_infoQueue.contains(id)) {
+ slotProcessNextClipInQueue();
+ }
+}
+
+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(path, PROXYCRASHED);
+ return;
+ }
+ setProxyStatus(path, PROXYWAITING);
+ if (m_abortProxy.contains(path)) m_abortProxy.removeAll(path);
+ if (m_processingProxy.contains(path)) {
+ // Proxy is already being generated
+ return;
+ }
+ if (QFile::exists(path)) {
+ // Proxy already created
+ setProxyStatus(path, PROXYDONE);
+ slotGotProxy(path);
+ return;
+ }
+ m_processingProxy.append(path);
+ QtConcurrent::run(this, &ProjectList::slotGenerateProxy, path, item->clipUrl().path(), item->clipType(), QString(item->referencedClip()->producerProperty("_exif_orientation")).toInt());
+}
+
+void ProjectList::slotAbortProxy(const QString id, const QString path)
+{
+ QTreeWidgetItemIterator it(m_listView);
+ ProjectItem *item = getItemById(id);
+ setProxyStatus(item, NOPROXY);
+ slotGotProxy(item);
+ if (!path.isEmpty() && m_processingProxy.contains(path)) {
+ m_abortProxy << path;
+ setProxyStatus(path, NOPROXY);
+ }
+}
+
+void ProjectList::slotGenerateProxy(const QString destPath, const QString sourcePath, int clipType, int exif_orientation)
+{
+ emit projectModified();
+ // Make sure proxy path is writable
+ QFile file(destPath);
+ if (!file.open(QIODevice::WriteOnly)) {
+ setProxyStatus(destPath, PROXYCRASHED);
+ m_processingProxy.removeAll(destPath);
+ return;
+ }
+ file.close();
+ QFile::remove(destPath);
+
+ setProxyStatus(destPath, CREATINGPROXY);
+
+ // Special case: playlist clips (.mlt or .kdenlive project files)
+ if (clipType == PLAYLIST) {
+ // change FFmpeg params to MLT format
+ QStringList parameters;
+ parameters << sourcePath;
+ parameters << "-consumer" << "avformat:" + destPath;
+ 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(sourcePath);
+ parameters << "aspect=" + QString::number(display_ratio);
+
+ //kDebug()<<"TRANSCOD: "<<parameters;
+ QProcess myProcess;
+ myProcess.start(KdenliveSettings::rendererpath(), parameters);
+ myProcess.waitForStarted();
+ int result = -1;
+ while (myProcess.state() != QProcess::NotRunning) {
+ // building proxy file
+ if (m_abortProxy.contains(destPath)) {
+ myProcess.close();
+ myProcess.waitForFinished();
+ m_abortProxy.removeAll(destPath);
+ m_processingProxy.removeAll(destPath);
+ QFile::remove(destPath);
+ setProxyStatus(destPath, NOPROXY);
+ result = -2;
+
+ }
+ myProcess.waitForFinished(500);
+ }
+ myProcess.waitForFinished();
+ m_processingProxy.removeAll(destPath);
+ if (result == -1) result = myProcess.exitStatus();
+ if (result == 0) {
+ // proxy successfully created
+ setProxyStatus(destPath, PROXYDONE);
+ slotGotProxy(destPath);
+ }
+ else if (result == 1) {
+ // Proxy process crashed
+ QFile::remove(destPath);
+ setProxyStatus(destPath, PROXYCRASHED);
+ }
+
+ }
+
+ if (clipType == IMAGE) {
+ // Image proxy
+ QImage i(sourcePath);
+ if (i.isNull()) {
+ // Cannot load image
+ setProxyStatus(destPath, 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());
+ 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(destPath);
+ }
+ else proxy.save(destPath);
+ setProxyStatus(destPath, PROXYDONE);
+ slotGotProxy(destPath);
+ m_abortProxy.removeAll(destPath);
+ m_processingProxy.removeAll(destPath);
+ return;
+ }
+
+ QStringList parameters;
+ parameters << "-i" << sourcePath;
+ 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 << destPath;
+ kDebug()<<"// STARTING PROXY GEN: "<<parameters;
+ QProcess myProcess;
+ myProcess.start("ffmpeg", parameters);
+ myProcess.waitForStarted();
+ int result = -1;
+ while (myProcess.state() != QProcess::NotRunning) {
+ // building proxy file
+ if (m_abortProxy.contains(destPath)) {
+ myProcess.close();
+ myProcess.waitForFinished();
+ m_abortProxy.removeAll(destPath);
+ m_processingProxy.removeAll(destPath);
+ QFile::remove(destPath);
+ setProxyStatus(destPath, NOPROXY);
+ result = -2;
+
+ }
+ myProcess.waitForFinished(500);
+ }
+ myProcess.waitForFinished();
+ if (result == -1) result = myProcess.exitStatus();
+ if (result == 0) {
+ // proxy successfully created
+ setProxyStatus(destPath, PROXYDONE);
+ slotGotProxy(destPath);
+ }
+ else if (result == 1) {
+ // Proxy process crashed
+ QFile::remove(destPath);
+ setProxyStatus(destPath, PROXYCRASHED);
+ }
+ m_abortProxy.removeAll(destPath);
+ m_processingProxy.removeAll(destPath);
+}
+
+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;
+ //if (!m_infoQueue.isEmpty() && !m_queueRunner.isRunning() && m_processingClips.isEmpty()) m_queueRunner = QtConcurrent::run(this, &ProjectList::slotProcessNextClipInQueue);
+ QtConcurrent::run(this, &ProjectList::slotProcessNextClipInQueue);
+}
+
+void ProjectList::slotProxyCurrentItem(bool doProxy)
+{
+ QList<QTreeWidgetItem *> list = m_listView->selectedItems();
+ QTreeWidgetItem *listItem;
+ QUndoCommand *command = new QUndoCommand();
+ 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);
+ if (listItem->type() == PROJECTFOLDERTYPE) {
+ for (int j = 0; j < listItem->childCount(); j++) {
+ QTreeWidgetItem *sub = listItem->child(j);
+ if (!list.contains(sub)) list.append(sub);
+ }
+ }
+ if (listItem->type() == PROJECTCLIPTYPE) {
+ ProjectItem *item = static_cast <ProjectItem*>(listItem);
+ CLIPTYPE t = item->clipType();
+ if ((t == VIDEO || t == AV || t == UNKNOWN || t == IMAGE || t == PLAYLIST) && item->referencedClip()) {
+ oldProps = item->referencedClip()->properties();
+ if (doProxy) {
+ newProps.clear();
+ QString path = proxydir + item->referencedClip()->getClipHash() + "." + (t == IMAGE ? "png" : m_doc->getDocumentProperty("proxyextension"));
+ // insert required duration for proxy
+ newProps.insert("proxy_out", item->referencedClip()->producerProperty("out"));
+ newProps.insert("proxy", path);
+ // We need to insert empty proxy so that undo will work
+ oldProps.insert("proxy", QString());
+ }
+ new EditClipCommand(this, item->clipId(), oldProps, newProps, true, 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);
+ if (!m_infoQueue.isEmpty()) QtConcurrent::run(this, &ProjectList::slotProcessNextClipInQueue);
+}
+
+
+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(const QString proxyPath, PROXYSTATUS status)
+{
+ if (proxyPath.isEmpty()) return;
+ QTreeWidgetItemIterator it(m_listView);
+ ProjectItem *item;
+ while (*it) {
+ if ((*it)->type() == PROJECTCLIPTYPE) {
+ item = static_cast <ProjectItem *>(*it);
+ if (item->referencedClip()->getProperty("proxy") == proxyPath) {
+ setProxyStatus(item, status);
+ }
+ }
+ ++it;
+ }
+}
+
+void ProjectList::setProxyStatus(ProjectItem *item, PROXYSTATUS status)
+{
+ if (item == NULL) return;
+ monitorItemEditing(false);
+ item->setProxyStatus(status);
+ 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;
+}
+