1 /***************************************************************************
2 * Copyright (C) 2007 by Jean-Baptiste Mardelle (jb@kdenlive.org) *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
18 ***************************************************************************/
20 #include "projectlist.h"
21 #include "projectitem.h"
22 #include "subprojectitem.h"
23 #include "addfoldercommand.h"
24 #include "kdenlivesettings.h"
25 #include "slideshowclip.h"
26 #include "ui_colorclip_ui.h"
27 #include "titlewidget.h"
28 #include "definitions.h"
29 #include "clipmanager.h"
30 #include "docclipbase.h"
31 #include "kdenlivedoc.h"
34 #include "projectlistview.h"
35 #include "editclipcommand.h"
36 #include "editfoldercommand.h"
37 #include "addclipcutcommand.h"
39 #include "ui_templateclip_ui.h"
44 #include <KFileDialog>
45 #include <KInputDialog>
46 #include <KMessageBox>
47 #include <KIO/NetAccess>
50 #include <nepomuk/global.h>
51 #include <nepomuk/resourcemanager.h>
52 //#include <nepomuk/tag.h>
54 #include <QMouseEvent>
55 #include <QStylePainter>
60 #include <QHeaderView>
62 ProjectList::ProjectList(QWidget *parent) :
71 m_transcodeAction(NULL),
78 m_listView = new ProjectListView(this);;
79 QVBoxLayout *layout = new QVBoxLayout;
80 layout->setContentsMargins(0, 0, 0, 0);
83 KTreeWidgetSearchLine *searchView = new KTreeWidgetSearchLine(this);
84 m_toolbar = new QToolBar("projectToolBar", this);
85 m_toolbar->addWidget(searchView);
86 int s = style()->pixelMetric(QStyle::PM_SmallIconSize);
87 m_toolbar->setIconSize(QSize(s, s));
88 searchView->setTreeWidget(m_listView);
90 m_addButton = new QToolButton(m_toolbar);
91 m_addButton->setPopupMode(QToolButton::MenuButtonPopup);
92 m_toolbar->addWidget(m_addButton);
94 layout->addWidget(m_toolbar);
95 layout->addWidget(m_listView);
98 m_queueTimer.setInterval(100);
99 connect(&m_queueTimer, SIGNAL(timeout()), this, SLOT(slotProcessNextClipInQueue()));
100 m_queueTimer.setSingleShot(true);
104 connect(m_listView, SIGNAL(itemSelectionChanged()), this, SLOT(slotClipSelected()));
105 connect(m_listView, SIGNAL(focusMonitor()), this, SLOT(slotClipSelected()));
106 connect(m_listView, SIGNAL(pauseMonitor()), this, SLOT(slotPauseMonitor()));
107 connect(m_listView, SIGNAL(requestMenu(const QPoint &, QTreeWidgetItem *)), this, SLOT(slotContextMenu(const QPoint &, QTreeWidgetItem *)));
108 connect(m_listView, SIGNAL(addClip()), this, SLOT(slotAddClip()));
109 connect(m_listView, SIGNAL(addClip(const QList <QUrl>, const QString &, const QString &)), this, SLOT(slotAddClip(const QList <QUrl>, const QString &, const QString &)));
110 connect(m_listView, SIGNAL(addClipCut(const QString &, int, int)), this, SLOT(slotAddClipCut(const QString &, int, int)));
111 connect(m_listView, SIGNAL(itemChanged(QTreeWidgetItem *, int)), this, SLOT(slotItemEdited(QTreeWidgetItem *, int)));
112 connect(m_listView, SIGNAL(showProperties(DocClipBase *)), this, SIGNAL(showClipProperties(DocClipBase *)));
114 m_listViewDelegate = new ItemDelegate(m_listView);
115 m_listView->setItemDelegate(m_listViewDelegate);
117 if (KdenliveSettings::activate_nepomuk()) {
118 Nepomuk::ResourceManager::instance()->init();
119 if (!Nepomuk::ResourceManager::instance()->initialized()) {
120 kDebug() << "Cannot communicate with Nepomuk, DISABLING it";
121 KdenliveSettings::setActivate_nepomuk(false);
126 ProjectList::~ProjectList()
130 m_listView->blockSignals(true);
132 delete m_listViewDelegate;
135 void ProjectList::focusTree() const
137 m_listView->setFocus();
140 void ProjectList::setupMenu(QMenu *addMenu, QAction *defaultAction)
142 QList <QAction *> actions = addMenu->actions();
143 for (int i = 0; i < actions.count(); i++) {
144 if (actions.at(i)->data().toString() == "clip_properties") {
145 m_editAction = actions.at(i);
146 m_toolbar->addAction(m_editAction);
149 } else if (actions.at(i)->data().toString() == "delete_clip") {
150 m_deleteAction = actions.at(i);
151 m_toolbar->addAction(m_deleteAction);
154 } else if (actions.at(i)->data().toString() == "edit_clip") {
155 m_openAction = actions.at(i);
158 } else if (actions.at(i)->data().toString() == "reload_clip") {
159 m_reloadAction = actions.at(i);
165 QMenu *m = new QMenu();
166 m->addActions(actions);
167 m_addButton->setMenu(m);
168 m_addButton->setDefaultAction(defaultAction);
169 m_menu = new QMenu();
170 m_menu->addActions(addMenu->actions());
173 void ProjectList::setupGeneratorMenu(QMenu *addMenu, QMenu *transcodeMenu)
175 if (!addMenu) return;
176 QMenu *menu = m_addButton->menu();
177 menu->addMenu(addMenu);
178 m_addButton->setMenu(menu);
180 m_menu->addMenu(addMenu);
181 if (addMenu->isEmpty()) addMenu->setEnabled(false);
182 m_menu->addMenu(transcodeMenu);
183 if (transcodeMenu->isEmpty()) transcodeMenu->setEnabled(false);
184 m_transcodeAction = transcodeMenu;
185 m_menu->addAction(m_reloadAction);
186 m_menu->addAction(m_editAction);
187 m_menu->addAction(m_openAction);
188 m_menu->addAction(m_deleteAction);
189 m_menu->insertSeparator(m_deleteAction);
193 QByteArray ProjectList::headerInfo() const
195 return m_listView->header()->saveState();
198 void ProjectList::setHeaderInfo(const QByteArray &state)
200 m_listView->header()->restoreState(state);
203 void ProjectList::slotEditClip()
206 if (!m_listView->currentItem() || m_listView->currentItem()->type() == PROJECTFOLDERTYPE) return;
207 if (m_listView->currentItem()->type() == PROJECTSUBCLIPTYPE) {
208 item = static_cast <ProjectItem*>(m_listView->currentItem()->parent());
209 } else item = static_cast <ProjectItem*>(m_listView->currentItem());
210 if (!(item->flags() & Qt::ItemIsDragEnabled)) return;
212 emit clipSelected(item->referencedClip());
213 emit showClipProperties(item->referencedClip());
217 void ProjectList::slotOpenClip()
220 if (!m_listView->currentItem() || m_listView->currentItem()->type() == PROJECTFOLDERTYPE) return;
221 if (m_listView->currentItem()->type() == QTreeWidgetItem::UserType + 1) {
222 item = static_cast <ProjectItem*>(m_listView->currentItem()->parent());
223 } else item = static_cast <ProjectItem*>(m_listView->currentItem());
225 if (item->clipType() == IMAGE) {
226 if (KdenliveSettings::defaultimageapp().isEmpty()) KMessageBox::sorry(this, i18n("Please set a default application to open images in the Settings dialog"));
227 else QProcess::startDetached(KdenliveSettings::defaultimageapp(), QStringList() << item->clipUrl().path());
229 if (item->clipType() == AUDIO) {
230 if (KdenliveSettings::defaultaudioapp().isEmpty()) KMessageBox::sorry(this, i18n("Please set a default application to open audio files in the Settings dialog"));
231 else QProcess::startDetached(KdenliveSettings::defaultaudioapp(), QStringList() << item->clipUrl().path());
236 void ProjectList::cleanup()
238 m_listView->clearSelection();
239 QTreeWidgetItemIterator it(m_listView);
242 if ((*it)->type() != PROJECTCLIPTYPE) {
246 item = static_cast <ProjectItem *>(*it);
247 if (item->numReferences() == 0) item->setSelected(true);
253 void ProjectList::trashUnusedClips()
255 QTreeWidgetItemIterator it(m_listView);
260 if ((*it)->type() != PROJECTCLIPTYPE) {
264 item = static_cast <ProjectItem *>(*it);
265 if (item->numReferences() == 0) {
266 ids << item->clipId();
267 KUrl url = item->clipUrl();
268 if (!url.isEmpty() && !urls.contains(url.path())) urls << url.path();
273 // Check that we don't use the URL in another clip
274 QTreeWidgetItemIterator it2(m_listView);
276 if ((*it2)->type() != PROJECTCLIPTYPE) {
280 item = static_cast <ProjectItem *>(*it2);
281 if (item->numReferences() > 0) {
282 KUrl url = item->clipUrl();
283 if (!url.isEmpty() && urls.contains(url.path())) urls.removeAll(url.path());
288 m_doc->deleteProjectClip(ids);
289 for (int i = 0; i < urls.count(); i++) {
290 KIO::NetAccess::del(KUrl(urls.at(i)), this);
294 void ProjectList::slotReloadClip(const QString &id)
296 QList<QTreeWidgetItem *> selected;
297 if (id.isEmpty()) selected = m_listView->selectedItems();
298 else selected.append(getItemById(id));
300 for (int i = 0; i < selected.count(); i++) {
301 if (selected.at(i)->type() != PROJECTCLIPTYPE) continue;
302 item = static_cast <ProjectItem *>(selected.at(i));
304 if (item->clipType() == IMAGE) {
305 item->referencedClip()->producer()->set("force_reload", 1);
306 } else if (item->clipType() == TEXT) {
307 if (!item->referencedClip()->getProperty("xmltemplate").isEmpty()) regenerateTemplate(item);
309 //requestClipInfo(item->toXml(), item->clipId(), true);
310 // Clear the file_hash value, which will cause a complete reload of the clip
311 emit getFileProperties(item->toXml(), item->clipId(), true);
316 void ProjectList::setRenderer(Render *projectRender)
318 m_render = projectRender;
319 m_listView->setIconSize(QSize((ProjectItem::itemDefaultHeight() - 2) * m_render->dar(), ProjectItem::itemDefaultHeight() - 2));
322 void ProjectList::slotClipSelected()
324 if (m_listView->currentItem()) {
325 if (m_listView->currentItem()->type() == PROJECTFOLDERTYPE) {
326 emit clipSelected(NULL);
327 m_editAction->setEnabled(false);
328 m_deleteAction->setEnabled(true);
329 m_openAction->setEnabled(false);
330 m_reloadAction->setEnabled(false);
331 m_transcodeAction->setEnabled(false);
334 if (m_listView->currentItem()->type() == PROJECTSUBCLIPTYPE) {
335 // this is a sub item, use base clip
336 clip = static_cast <ProjectItem*>(m_listView->currentItem()->parent());
337 if (clip == NULL) kDebug() << "-----------ERROR";
338 SubProjectItem *sub = static_cast <SubProjectItem*>(m_listView->currentItem());
339 emit clipSelected(clip->referencedClip(), sub->zone());
342 clip = static_cast <ProjectItem*>(m_listView->currentItem());
343 emit clipSelected(clip->referencedClip());
344 m_editAction->setEnabled(true);
345 m_deleteAction->setEnabled(true);
346 m_reloadAction->setEnabled(true);
347 m_transcodeAction->setEnabled(true);
348 if (clip->clipType() == IMAGE && !KdenliveSettings::defaultimageapp().isEmpty()) {
349 m_openAction->setIcon(KIcon(KdenliveSettings::defaultimageapp()));
350 m_openAction->setEnabled(true);
351 } else if (clip->clipType() == AUDIO && !KdenliveSettings::defaultaudioapp().isEmpty()) {
352 m_openAction->setIcon(KIcon(KdenliveSettings::defaultaudioapp()));
353 m_openAction->setEnabled(true);
354 } else m_openAction->setEnabled(false);
357 emit clipSelected(NULL);
358 m_editAction->setEnabled(false);
359 m_deleteAction->setEnabled(false);
360 m_openAction->setEnabled(false);
361 m_reloadAction->setEnabled(false);
362 m_transcodeAction->setEnabled(false);
366 void ProjectList::slotPauseMonitor()
368 if (m_render) m_render->pause();
371 void ProjectList::slotUpdateClipProperties(const QString &id, QMap <QString, QString> properties)
373 ProjectItem *item = getItemById(id);
375 slotUpdateClipProperties(item, properties);
376 if (properties.contains("out")) {
378 } else if (properties.contains("colour") || properties.contains("resource") || properties.contains("xmldata") || properties.contains("force_aspect_ratio") || properties.contains("templatetext")) {
379 slotRefreshClipThumbnail(item);
385 void ProjectList::slotUpdateClipProperties(ProjectItem *clip, QMap <QString, QString> properties)
388 clip->setProperties(properties);
389 if (properties.contains("name")) {
390 m_listView->blockSignals(true);
391 clip->setText(0, properties.value("name"));
392 m_listView->blockSignals(false);
393 emit clipNameChanged(clip->clipId(), properties.value("name"));
395 if (properties.contains("description")) {
396 CLIPTYPE type = clip->clipType();
397 m_listView->blockSignals(true);
398 clip->setText(1, properties.value("description"));
399 m_listView->blockSignals(false);
400 if (KdenliveSettings::activate_nepomuk() && (type == AUDIO || type == VIDEO || type == AV || type == IMAGE || type == PLAYLIST)) {
401 // Use Nepomuk system to store clip description
402 Nepomuk::Resource f(clip->clipUrl().path());
403 f.setDescription(properties.value("description"));
405 emit projectModified();
409 void ProjectList::slotItemEdited(QTreeWidgetItem *item, int column)
411 if (item->type() == PROJECTSUBCLIPTYPE) {
412 // this is a sub-item
415 if (item->type() == PROJECTFOLDERTYPE) {
416 if (column != 0) return;
417 FolderProjectItem *folder = static_cast <FolderProjectItem*>(item);
418 editFolder(item->text(0), folder->groupName(), folder->clipId());
419 folder->setGroupName(item->text(0));
420 m_doc->clipManager()->addFolder(folder->clipId(), item->text(0));
421 const int children = item->childCount();
422 for (int i = 0; i < children; i++) {
423 ProjectItem *child = static_cast <ProjectItem *>(item->child(i));
424 child->setProperty("groupname", item->text(0));
429 ProjectItem *clip = static_cast <ProjectItem*>(item);
431 if (clip->referencedClip()) {
432 QMap <QString, QString> oldprops;
433 QMap <QString, QString> newprops;
434 oldprops["description"] = clip->referencedClip()->getProperty("description");
435 newprops["description"] = item->text(1);
437 if (clip->clipType() == TEXT) {
438 // This is a text template clip, update the image
439 /*oldprops.insert("xmldata", clip->referencedClip()->getProperty("xmldata"));
440 newprops.insert("xmldata", generateTemplateXml(clip->referencedClip()->getProperty("xmltemplate"), item->text(2)).toString());*/
441 oldprops.insert("templatetext", clip->referencedClip()->getProperty("templatetext"));
442 newprops.insert("templatetext", item->text(1));
444 slotUpdateClipProperties(clip->clipId(), newprops);
445 EditClipCommand *command = new EditClipCommand(this, clip->clipId(), oldprops, newprops, false);
446 m_commandStack->push(command);
448 } else if (column == 0) {
449 if (clip->referencedClip()) {
450 QMap <QString, QString> oldprops;
451 QMap <QString, QString> newprops;
452 oldprops["name"] = clip->referencedClip()->getProperty("name");
453 newprops["name"] = item->text(0);
454 slotUpdateClipProperties(clip, newprops);
455 emit projectModified();
456 EditClipCommand *command = new EditClipCommand(this, clip->clipId(), oldprops, newprops, false);
457 m_commandStack->push(command);
462 void ProjectList::slotContextMenu(const QPoint &pos, QTreeWidgetItem *item)
468 m_editAction->setEnabled(enable);
469 m_deleteAction->setEnabled(enable);
470 m_reloadAction->setEnabled(enable);
471 m_transcodeAction->setEnabled(enable);
473 ProjectItem *clip = NULL;
474 if (m_listView->currentItem()->type() == PROJECTSUBCLIPTYPE) {
475 clip = static_cast <ProjectItem*>(item->parent());
476 } else if (m_listView->currentItem()->type() == PROJECTCLIPTYPE) clip = static_cast <ProjectItem*>(item);
477 if (clip && clip->clipType() == IMAGE && !KdenliveSettings::defaultimageapp().isEmpty()) {
478 m_openAction->setIcon(KIcon(KdenliveSettings::defaultimageapp()));
479 m_openAction->setEnabled(true);
480 } else if (clip && clip->clipType() == AUDIO && !KdenliveSettings::defaultaudioapp().isEmpty()) {
481 m_openAction->setIcon(KIcon(KdenliveSettings::defaultaudioapp()));
482 m_openAction->setEnabled(true);
483 } else m_openAction->setEnabled(false);
484 } else m_openAction->setEnabled(false);
488 void ProjectList::slotRemoveClip()
490 if (!m_listView->currentItem()) return;
492 QMap <QString, QString> folderids;
493 QList<QTreeWidgetItem *> selected = m_listView->selectedItems();
495 QUndoCommand *delCommand = new QUndoCommand();
496 delCommand->setText(i18n("Delete Clip Zone"));
498 for (int i = 0; i < selected.count(); i++) {
499 if (selected.at(i)->type() == PROJECTSUBCLIPTYPE) {
501 SubProjectItem *sub = static_cast <SubProjectItem *>(selected.at(i));
502 ProjectItem *item = static_cast <ProjectItem *>(sub->parent());
503 new AddClipCutCommand(this, item->clipId(), sub->zone().x(), sub->zone().y(), true, delCommand);
507 if (selected.at(i)->type() == PROJECTFOLDERTYPE) {
509 FolderProjectItem *folder = static_cast <FolderProjectItem *>(selected.at(i));
510 folderids[folder->groupName()] = folder->clipId();
511 int children = folder->childCount();
513 if (children > 0 && 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, folder->text(1)), i18n("Delete Folder")) != KMessageBox::Yes) return;
514 for (int i = 0; i < children; ++i) {
515 ProjectItem *child = static_cast <ProjectItem *>(folder->child(i));
516 ids << child->clipId();
521 ProjectItem *item = static_cast <ProjectItem *>(selected.at(i));
522 ids << item->clipId();
523 if (item->numReferences() > 0) {
524 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;
527 if (delCommand->childCount() == 0) delete delCommand;
528 else m_commandStack->push(delCommand);
529 if (!ids.isEmpty()) m_doc->deleteProjectClip(ids);
530 if (!folderids.isEmpty()) deleteProjectFolder(folderids);
531 if (m_listView->topLevelItemCount() == 0) {
532 m_editAction->setEnabled(false);
533 m_deleteAction->setEnabled(false);
534 m_openAction->setEnabled(false);
535 m_reloadAction->setEnabled(false);
536 m_transcodeAction->setEnabled(false);
540 void ProjectList::selectItemById(const QString &clipId)
542 ProjectItem *item = getItemById(clipId);
543 if (item) m_listView->setCurrentItem(item);
547 void ProjectList::slotDeleteClip(const QString &clipId)
549 ProjectItem *item = getItemById(clipId);
551 kDebug() << "/// Cannot find clip to delete";
554 m_listView->blockSignals(true);
556 m_doc->clipManager()->deleteClip(clipId);
557 m_listView->blockSignals(false);
562 void ProjectList::editFolder(const QString folderName, const QString oldfolderName, const QString &clipId)
564 EditFolderCommand *command = new EditFolderCommand(this, folderName, oldfolderName, clipId, false);
565 m_commandStack->push(command);
566 m_doc->setModified(true);
569 void ProjectList::slotAddFolder()
571 AddFolderCommand *command = new AddFolderCommand(this, i18n("Folder"), QString::number(m_doc->clipManager()->getFreeFolderId()), true);
572 m_commandStack->push(command);
575 void ProjectList::slotAddFolder(const QString foldername, const QString &clipId, bool remove, bool edit)
578 FolderProjectItem *item = getFolderItemById(clipId);
580 m_doc->clipManager()->deleteFolder(clipId);
585 FolderProjectItem *item = getFolderItemById(clipId);
587 m_listView->blockSignals(true);
588 item->setGroupName(foldername);
589 m_listView->blockSignals(false);
590 m_doc->clipManager()->addFolder(clipId, foldername);
591 const int children = item->childCount();
592 for (int i = 0; i < children; i++) {
593 ProjectItem *child = static_cast <ProjectItem *>(item->child(i));
594 child->setProperty("groupname", foldername);
600 m_listView->blockSignals(true);
601 m_listView->setCurrentItem(new FolderProjectItem(m_listView, text, clipId));
602 m_doc->clipManager()->addFolder(clipId, foldername);
603 m_listView->blockSignals(false);
606 m_doc->setModified(true);
611 void ProjectList::deleteProjectFolder(QMap <QString, QString> map)
613 QMapIterator<QString, QString> i(map);
614 QUndoCommand *delCommand = new QUndoCommand();
615 delCommand->setText(i18n("Delete Folder"));
616 while (i.hasNext()) {
618 new AddFolderCommand(this, i.key(), i.value(), false, delCommand);
620 m_commandStack->push(delCommand);
623 void ProjectList::slotAddClip(DocClipBase *clip, bool getProperties)
625 m_listView->setEnabled(false);
627 m_listView->blockSignals(true);
629 // remove file_hash so that we load all properties for the clip
630 QDomElement e = clip->toXML().cloneNode().toElement();
631 e.removeAttribute("file_hash");
632 m_infoQueue.insert(clip->getId(), e);
633 //m_render->getFileProperties(clip->toXML(), clip->getId(), true);
635 const QString parent = clip->getProperty("groupid");
636 ProjectItem *item = NULL;
637 if (!parent.isEmpty()) {
638 FolderProjectItem *parentitem = getFolderItemById(parent);
641 QString groupName = clip->getProperty("groupname");
642 //kDebug() << "Adding clip to new group: " << groupName;
643 if (groupName.isEmpty()) groupName = i18n("Folder");
645 parentitem = new FolderProjectItem(m_listView, text, parent);
647 //kDebug() << "Adding clip to existing group: " << parentitem->groupName();
649 if (parentitem) item = new ProjectItem(parentitem, clip);
651 if (item == NULL) item = new ProjectItem(m_listView, clip);
652 KUrl url = clip->fileURL();
654 if (getProperties == false && !clip->getClipHash().isEmpty()) {
655 QString cachedPixmap = m_doc->projectFolder().path(KUrl::AddTrailingSlash) + "thumbs/" + clip->getClipHash() + ".png";
656 if (QFile::exists(cachedPixmap)) {
657 item->setIcon(0, QPixmap(cachedPixmap));
661 if (!url.isEmpty() && KdenliveSettings::activate_nepomuk()) {
662 // if file has Nepomuk comment, use it
663 Nepomuk::Resource f(url.path());
664 QString annotation = f.description();
665 if (!annotation.isEmpty()) item->setText(1, annotation);
666 item->setText(2, QString::number(f.rating()));
670 QList <QPoint> cuts = clip->cutZones();
671 if (!cuts.isEmpty()) {
672 for (int i = 0; i < cuts.count(); i++) {
673 SubProjectItem *sub = new SubProjectItem(item, cuts.at(i).x(), cuts.at(i).y());
674 if (!clip->getClipHash().isEmpty()) {
675 QString cachedPixmap = m_doc->projectFolder().path(KUrl::AddTrailingSlash) + "thumbs/" + clip->getClipHash() + '#' + QString::number(cuts.at(i).x()) + ".png";
676 if (QFile::exists(cachedPixmap)) {
677 sub->setIcon(0, QPixmap(cachedPixmap));
683 if (getProperties && m_listView->isEnabled()) m_listView->blockSignals(false);
684 if (getProperties && !m_queueTimer.isActive()) m_queueTimer.start();
687 void ProjectList::slotResetProjectList()
690 emit clipSelected(NULL);
691 m_thumbnailQueue.clear();
696 void ProjectList::requestClipInfo(const QDomElement xml, const QString id)
699 m_infoQueue.insert(id, xml);
700 //if (m_infoQueue.count() == 1 || ) QTimer::singleShot(300, this, SLOT(slotProcessNextClipInQueue()));
703 void ProjectList::slotProcessNextClipInQueue()
705 if (m_infoQueue.isEmpty()) {
706 slotProcessNextThumbnail();
710 QMap<QString, QDomElement>::const_iterator j = m_infoQueue.constBegin();
711 if (j != m_infoQueue.constEnd()) {
712 const QDomElement dom = j.value();
713 const QString id = j.key();
714 m_infoQueue.remove(j.key());
715 emit getFileProperties(dom, id, false);
719 void ProjectList::slotUpdateClip(const QString &id)
721 ProjectItem *item = getItemById(id);
722 m_listView->blockSignals(true);
723 if (item) item->setData(1, UsageRole, QString::number(item->numReferences()));
724 m_listView->blockSignals(false);
727 void ProjectList::updateAllClips()
729 m_listView->setSortingEnabled(false);
731 QTreeWidgetItemIterator it(m_listView);
734 m_listView->blockSignals(true);
736 if ((*it)->type() == PROJECTSUBCLIPTYPE) {
738 SubProjectItem *sub = static_cast <SubProjectItem *>(*it);
739 if (sub->icon(0).isNull()) {
740 item = static_cast <ProjectItem *>((*it)->parent());
741 requestClipThumbnail(item->clipId() + '#' + QString::number(sub->zone().x()));
745 } else if ((*it)->type() == PROJECTFOLDERTYPE) {
750 item = static_cast <ProjectItem *>(*it);
751 clip = item->referencedClip();
752 if (item->referencedClip()->producer() == NULL) {
753 if (clip->isPlaceHolder() == false) {
754 requestClipInfo(clip->toXML(), clip->getId());
755 } else item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
757 if (item->icon(0).isNull()) {
758 requestClipThumbnail(clip->getId());
760 if (item->data(0, DurationRole).toString().isEmpty()) {
761 item->changeDuration(item->referencedClip()->producer()->get_playtime());
764 item->setData(1, UsageRole, QString::number(item->numReferences()));
765 //qApp->processEvents();
768 qApp->processEvents();
769 if (!m_queueTimer.isActive()) m_queueTimer.start();
771 if (m_listView->isEnabled()) m_listView->blockSignals(false);
772 m_listView->setSortingEnabled(true);
773 if (m_infoQueue.isEmpty()) slotProcessNextThumbnail();
776 void ProjectList::slotAddClip(const QList <QUrl> givenList, const QString &groupName, const QString &groupId)
778 if (!m_commandStack) {
779 kDebug() << "!!!!!!!!!!!!!!!! NO CMD STK";
782 if (givenList.isEmpty()) {
783 // Build list of mime types
784 QStringList mimeTypes = QStringList() << "application/x-kdenlive" << "application/x-kdenlivetitle" << "video/x-flv" << "application/vnd.rn-realmedia" << "video/x-dv" << "video/dv" << "video/x-msvideo" << "video/x-matroska" << "video/mlt-playlist" << "video/mpeg" << "video/ogg" << "video/x-ms-wmv" << "audio/x-flac" << "audio/x-matroska" << "audio/mp4" << "audio/mpeg" << "audio/x-mp3" << "audio/ogg" << "audio/x-wav" << "application/ogg" << "video/mp4" << "video/quicktime" << "image/gif" << "image/jpeg" << "image/png" << "image/x-tga" << "image/x-bmp" << "image/svg+xml" << "image/tiff" << "image/x-xcf-gimp" << "image/x-vnd.adobe.photoshop" << "image/x-pcx" << "image/x-exr";
786 QString allExtensions;
787 foreach(const QString& mimeType, mimeTypes) {
788 KMimeType::Ptr mime(KMimeType::mimeType(mimeType));
790 allExtensions.append(mime->patterns().join(" "));
791 allExtensions.append(' ');
794 const QString dialogFilter = allExtensions.simplified() + ' ' + QLatin1Char('|') + i18n("All Supported Files") + "\n* " + QLatin1Char('|') + i18n("All Files");
795 list = KFileDialog::getOpenUrls(KUrl("kfiledialog:///clipfolder"), dialogFilter, this);
798 for (int i = 0; i < givenList.count(); i++)
799 list << givenList.at(i);
801 if (list.isEmpty()) return;
803 if (givenList.isEmpty()) {
804 QStringList groupInfo = getGroup();
805 m_doc->slotAddClipList(list, groupInfo.at(0), groupInfo.at(1));
806 } else m_doc->slotAddClipList(list, groupName, groupId);
809 void ProjectList::slotRemoveInvalidClip(const QString &id, bool replace)
811 ProjectItem *item = getItemById(id);
812 QTimer::singleShot(300, this, SLOT(slotProcessNextClipInQueue()));
814 const QString path = item->referencedClip()->fileURL().path();
815 if (!path.isEmpty()) {
816 if (replace) KMessageBox::sorry(this, i18n("Clip <b>%1</b><br>is invalid, will be removed from project.", path));
818 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;
823 if (replace) m_doc->deleteProjectClip(ids);
827 void ProjectList::slotAddColorClip()
829 if (!m_commandStack) {
830 kDebug() << "!!!!!!!!!!!!!!!! NO CMD STK";
832 QDialog *dia = new QDialog(this);
833 Ui::ColorClip_UI dia_ui;
835 dia_ui.clip_name->setText(i18n("Color Clip"));
836 dia_ui.clip_duration->setText(KdenliveSettings::color_duration());
837 if (dia->exec() == QDialog::Accepted) {
838 QString color = dia_ui.clip_color->color().name();
839 color = color.replace(0, 1, "0x") + "ff";
840 QStringList groupInfo = getGroup();
841 m_doc->slotCreateColorClip(dia_ui.clip_name->text(), color, dia_ui.clip_duration->text(), groupInfo.at(0), groupInfo.at(1));
847 void ProjectList::slotAddSlideshowClip()
849 if (!m_commandStack) {
850 kDebug() << "!!!!!!!!!!!!!!!! NO CMD STK";
852 SlideshowClip *dia = new SlideshowClip(m_timecode, this);
854 if (dia->exec() == QDialog::Accepted) {
855 QStringList groupInfo = getGroup();
856 m_doc->slotCreateSlideshowClipFile(dia->clipName(), dia->selectedPath(), dia->imageCount(), dia->clipDuration(), dia->loop(), dia->fade(), dia->lumaDuration(), dia->lumaFile(), dia->softness(), groupInfo.at(0), groupInfo.at(1));
861 void ProjectList::slotAddTitleClip()
863 QStringList groupInfo = getGroup();
864 m_doc->slotCreateTextClip(groupInfo.at(0), groupInfo.at(1));
867 void ProjectList::slotAddTitleTemplateClip()
869 QStringList groupInfo = getGroup();
870 if (!m_commandStack) {
871 kDebug() << "!!!!!!!!!!!!!!!! NO CMD STK";
874 // Get the list of existing templates
876 filter << "*.kdenlivetitle";
877 const QString path = m_doc->projectFolder().path(KUrl::AddTrailingSlash) + "titles/";
878 QStringList templateFiles = QDir(path).entryList(filter, QDir::Files);
880 QDialog *dia = new QDialog(this);
881 Ui::TemplateClip_UI dia_ui;
883 for (int i = 0; i < templateFiles.size(); ++i) {
884 dia_ui.template_list->comboBox()->addItem(templateFiles.at(i), path + templateFiles.at(i));
886 dia_ui.template_list->fileDialog()->setFilter("*.kdenlivetitle");
887 //warning: setting base directory doesn't work??
889 dia_ui.template_list->fileDialog()->setUrl(startDir);
890 dia_ui.text_box->setHidden(true);
891 if (dia->exec() == QDialog::Accepted) {
892 QString textTemplate = dia_ui.template_list->comboBox()->itemData(dia_ui.template_list->comboBox()->currentIndex()).toString();
893 if (textTemplate.isEmpty()) textTemplate = dia_ui.template_list->comboBox()->currentText();
894 // Create a cloned template clip
895 m_doc->slotCreateTextTemplateClip(groupInfo.at(0), groupInfo.at(1), KUrl(textTemplate));
900 QStringList ProjectList::getGroup() const
903 QTreeWidgetItem *item = m_listView->currentItem();
904 while (item && item->type() != PROJECTFOLDERTYPE) {
905 item = item->parent();
909 FolderProjectItem *folder = static_cast <FolderProjectItem *>(item);
910 result << folder->groupName();
911 result << folder->clipId();
912 } else result << QString() << QString();
916 void ProjectList::setDocument(KdenliveDoc *doc)
918 m_listView->blockSignals(true);
920 m_listView->setSortingEnabled(false);
921 emit clipSelected(NULL);
922 m_thumbnailQueue.clear();
926 m_timecode = doc->timecode();
927 m_commandStack = doc->commandStack();
930 QMap <QString, QString> flist = doc->clipManager()->documentFolderList();
931 QMapIterator<QString, QString> f(flist);
932 while (f.hasNext()) {
934 (void) new FolderProjectItem(m_listView, QStringList() << f.value(), f.key());
937 QList <DocClipBase*> list = doc->clipManager()->documentClipList();
938 for (int i = 0; i < list.count(); i++) {
939 slotAddClip(list.at(i), false);
942 m_listView->blockSignals(false);
943 m_toolbar->setEnabled(true);
944 connect(m_doc->clipManager(), SIGNAL(reloadClip(const QString &)), this, SLOT(slotReloadClip(const QString &)));
945 connect(m_doc->clipManager(), SIGNAL(checkAllClips()), this, SLOT(updateAllClips()));
948 QList <DocClipBase*> ProjectList::documentClipList() const
950 if (m_doc == NULL) return QList <DocClipBase*> ();
951 return m_doc->clipManager()->documentClipList();
954 QDomElement ProjectList::producersList()
957 QDomElement prods = doc.createElement("producerlist");
958 doc.appendChild(prods);
959 kDebug() << "//////////// PRO LISTÂ BUILD PRDSLIST ";
960 QTreeWidgetItemIterator it(m_listView);
962 if ((*it)->type() != PROJECTCLIPTYPE) {
967 prods.appendChild(doc.importNode(((ProjectItem *)(*it))->toXml(), true));
973 void ProjectList::slotCheckForEmptyQueue()
975 if (!m_refreshed && m_thumbnailQueue.isEmpty() && m_infoQueue.isEmpty()) {
977 emit loadingIsOver();
978 emit displayMessage(QString(), -1);
979 m_listView->blockSignals(false);
980 m_listView->setEnabled(true);
981 } else if (!m_refreshed) QTimer::singleShot(300, this, SLOT(slotCheckForEmptyQueue()));
984 void ProjectList::reloadClipThumbnails()
986 kDebug() << "////////////// RELOAD CLIPS THUMBNAILS!!!";
987 m_thumbnailQueue.clear();
988 QTreeWidgetItemIterator it(m_listView);
990 if ((*it)->type() != PROJECTCLIPTYPE) {
995 m_thumbnailQueue << ((ProjectItem *)(*it))->clipId();
998 QTimer::singleShot(300, this, SLOT(slotProcessNextThumbnail()));
1001 void ProjectList::requestClipThumbnail(const QString id)
1003 if (!m_thumbnailQueue.contains(id)) m_thumbnailQueue.append(id);
1006 void ProjectList::slotProcessNextThumbnail()
1008 if (m_thumbnailQueue.isEmpty() && m_infoQueue.isEmpty()) {
1009 slotCheckForEmptyQueue();
1012 if (!m_infoQueue.isEmpty()) {
1013 //QTimer::singleShot(300, this, SLOT(slotProcessNextThumbnail()));
1016 if (m_thumbnailQueue.count() > 1) {
1017 int max = m_doc->clipManager()->clipsCount();
1018 emit displayMessage(i18n("Loading thumbnails"), (int)(100 *(max - m_thumbnailQueue.count()) / max));
1020 slotRefreshClipThumbnail(m_thumbnailQueue.takeFirst(), false);
1023 void ProjectList::slotRefreshClipThumbnail(const QString &clipId, bool update)
1025 QTreeWidgetItem *item = getAnyItemById(clipId);
1026 if (item) slotRefreshClipThumbnail(item, update);
1027 else slotProcessNextThumbnail();
1030 void ProjectList::slotRefreshClipThumbnail(QTreeWidgetItem *it, bool update)
1032 if (it == NULL) return;
1033 ProjectItem *item = NULL;
1034 bool isSubItem = false;
1036 if (it->type() == PROJECTFOLDERTYPE) return;
1037 if (it->type() == PROJECTSUBCLIPTYPE) {
1038 item = static_cast <ProjectItem *>(it->parent());
1039 frame = static_cast <SubProjectItem *>(it)->zone().x();
1042 item = static_cast <ProjectItem *>(it);
1043 frame = item->referencedClip()->getClipThumbFrame();
1047 DocClipBase *clip = item->referencedClip();
1049 slotProcessNextThumbnail();
1053 int height = it->sizeHint(0).height();
1054 int width = (int)(height * m_render->dar());
1055 if (clip->clipType() == AUDIO) pix = KIcon("audio-x-generic").pixmap(QSize(width, height));
1056 else if (clip->clipType() == IMAGE) pix = QPixmap::fromImage(KThumb::getFrame(item->referencedClip()->producer(), 0, width, height));
1057 else pix = item->referencedClip()->thumbProducer()->extractImage(frame, width, height);
1059 if (!pix.isNull()) {
1060 m_listView->blockSignals(true);
1061 it->setIcon(0, pix);
1062 if (m_listView->isEnabled()) m_listView->blockSignals(false);
1063 if (!isSubItem) m_doc->cachePixmap(item->getClipHash(), pix);
1064 else m_doc->cachePixmap(item->getClipHash() + '#' + QString::number(frame), pix);
1066 if (update) emit projectModified();
1067 QTimer::singleShot(30, this, SLOT(slotProcessNextThumbnail()));
1071 void ProjectList::slotReplyGetFileProperties(const QString &clipId, Mlt::Producer *producer, const QMap < QString, QString > &properties, const QMap < QString, QString > &metadata, bool replace)
1073 ProjectItem *item = getItemById(clipId);
1074 if (item && producer) {
1075 m_listView->blockSignals(true);
1076 item->setProperties(properties, metadata);
1077 //Q_ASSERT_X(item->referencedClip(), "void ProjectList::slotReplyGetFileProperties", QString("Item with groupName %1 does not have a clip associated").arg(item->groupName()).toLatin1());
1078 item->referencedClip()->setProducer(producer, replace);
1079 //emit receivedClipDuration(clipId);
1080 if (m_listView->isEnabled() && replace) {
1081 // update clip in clip monitor
1082 emit clipSelected(NULL);
1083 emit clipSelected(item->referencedClip());
1086 // Check if duration changed.
1087 emit receivedClipDuration(clipId);
1090 if (m_listView->isEnabled()) m_listView->blockSignals(false);
1091 if (item->icon(0).isNull()) {
1092 requestClipThumbnail(clipId);
1094 } else kDebug() << "//////// COULD NOT FIND CLIP TO UPDATE PRPS...";
1095 int max = m_doc->clipManager()->clipsCount();
1096 emit displayMessage(i18n("Loading clips"), (int)(100 *(max - m_infoQueue.count()) / max));
1097 // small delay so that the app can display the progress info
1098 if (item && m_infoQueue.isEmpty() && m_thumbnailQueue.isEmpty()) {
1099 m_listView->setCurrentItem(item);
1100 emit clipSelected(item->referencedClip());
1102 QTimer::singleShot(30, this, SLOT(slotProcessNextClipInQueue()));
1105 void ProjectList::slotReplyGetImage(const QString &clipId, const QPixmap &pix)
1107 ProjectItem *item = getItemById(clipId);
1108 if (item && !pix.isNull()) {
1109 m_listView->blockSignals(true);
1110 item->setIcon(0, pix);
1111 m_doc->cachePixmap(item->getClipHash(), pix);
1112 if (m_listView->isEnabled()) m_listView->blockSignals(false);
1116 QTreeWidgetItem *ProjectList::getAnyItemById(const QString &id)
1118 QTreeWidgetItemIterator it(m_listView);
1119 QString lookId = id;
1120 if (id.contains('#')) {
1121 lookId = id.section('#', 0, 0);
1124 ProjectItem *result = NULL;
1126 if ((*it)->type() != PROJECTCLIPTYPE) {
1131 ProjectItem *item = static_cast<ProjectItem *>(*it);
1132 if (item->clipId() == lookId) {
1138 if (result == NULL || !id.contains('#')) return result;
1139 else for (int i = 0; i < result->childCount(); i++) {
1140 SubProjectItem *sub = static_cast <SubProjectItem *>(result->child(i));
1141 if (sub && sub->zone().x() == id.section('#', 1, 1).toInt()) {
1150 ProjectItem *ProjectList::getItemById(const QString &id)
1153 QTreeWidgetItemIterator it(m_listView);
1155 if ((*it)->type() != PROJECTCLIPTYPE) {
1160 item = static_cast<ProjectItem *>(*it);
1161 if (item->clipId() == id)
1168 FolderProjectItem *ProjectList::getFolderItemById(const QString &id)
1170 FolderProjectItem *item;
1171 QTreeWidgetItemIterator it(m_listView);
1173 if ((*it)->type() == PROJECTFOLDERTYPE) {
1174 item = static_cast<FolderProjectItem *>(*it);
1175 if (item->clipId() == id) return item;
1182 void ProjectList::slotSelectClip(const QString &ix)
1184 ProjectItem *clip = getItemById(ix);
1186 m_listView->setCurrentItem(clip);
1187 m_listView->scrollToItem(clip);
1188 m_editAction->setEnabled(true);
1189 m_deleteAction->setEnabled(true);
1190 m_reloadAction->setEnabled(true);
1191 m_transcodeAction->setEnabled(true);
1192 if (clip->clipType() == IMAGE && !KdenliveSettings::defaultimageapp().isEmpty()) {
1193 m_openAction->setIcon(KIcon(KdenliveSettings::defaultimageapp()));
1194 m_openAction->setEnabled(true);
1195 } else if (clip->clipType() == AUDIO && !KdenliveSettings::defaultaudioapp().isEmpty()) {
1196 m_openAction->setIcon(KIcon(KdenliveSettings::defaultaudioapp()));
1197 m_openAction->setEnabled(true);
1198 } else m_openAction->setEnabled(false);
1202 QString ProjectList::currentClipUrl() const
1205 if (!m_listView->currentItem() || m_listView->currentItem()->type() == PROJECTFOLDERTYPE) return QString();
1206 if (m_listView->currentItem()->type() == PROJECTSUBCLIPTYPE) {
1208 item = static_cast <ProjectItem*>(m_listView->currentItem()->parent());
1209 } else item = static_cast <ProjectItem*>(m_listView->currentItem());
1210 if (item == NULL) return QString();
1211 return item->clipUrl().path();
1214 void ProjectList::regenerateTemplate(const QString &id)
1216 ProjectItem *clip = getItemById(id);
1217 if (clip) regenerateTemplate(clip);
1220 void ProjectList::regenerateTemplate(ProjectItem *clip)
1222 //TODO: remove this unused method, only force_reload is necessary
1223 clip->referencedClip()->producer()->set("force_reload", 1);
1226 QDomDocument ProjectList::generateTemplateXml(QString path, const QString &replaceString)
1230 if (!file.open(QIODevice::ReadOnly)) {
1231 kWarning() << "ERROR, CANNOT READ: " << path;
1234 if (!doc.setContent(&file)) {
1235 kWarning() << "ERROR, CANNOT READ: " << path;
1240 QDomNodeList texts = doc.elementsByTagName("content");
1241 for (int i = 0; i < texts.count(); i++) {
1242 QString data = texts.item(i).firstChild().nodeValue();
1243 data.replace("%s", replaceString);
1244 texts.item(i).firstChild().setNodeValue(data);
1250 void ProjectList::slotAddClipCut(const QString &id, int in, int out)
1252 ProjectItem *clip = getItemById(id);
1253 if (clip == NULL) return;
1254 if (clip->referencedClip()->hasCutZone(QPoint(in, out))) return;
1255 AddClipCutCommand *command = new AddClipCutCommand(this, id, in, out, false);
1256 m_commandStack->push(command);
1259 void ProjectList::addClipCut(const QString &id, int in, int out)
1261 ProjectItem *clip = getItemById(id);
1263 DocClipBase *base = clip->referencedClip();
1264 base->addCutZone(in, out);
1265 m_listView->blockSignals(true);
1266 SubProjectItem *sub = new SubProjectItem(clip, in, out);
1268 QPixmap p = clip->referencedClip()->thumbProducer()->extractImage(in, (int)(sub->sizeHint(0).height() * m_render->dar()), sub->sizeHint(0).height() - 2);
1270 m_doc->cachePixmap(clip->getClipHash() + '#' + QString::number(in), p);
1271 m_listView->blockSignals(false);
1275 void ProjectList::removeClipCut(const QString &id, int in, int out)
1277 ProjectItem *clip = getItemById(id);
1279 DocClipBase *base = clip->referencedClip();
1280 base->removeCutZone(in, out);
1281 for (int i = 0; i < clip->childCount(); i++) {
1282 QTreeWidgetItem *it = clip->child(i);
1283 if (it->type() != PROJECTSUBCLIPTYPE) continue;
1284 SubProjectItem *sub = static_cast <SubProjectItem*>(it);
1285 if (sub->zone() == QPoint(in, out)) {
1286 m_listView->blockSignals(true);
1288 m_listView->blockSignals(false);
1295 #include "projectlist.moc"