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 ***************************************************************************/
21 #include <QMouseEvent>
22 #include <QStylePainter>
31 #include <KFileDialog>
32 #include <KInputDialog>
33 #include <KMessageBox>
35 #include <nepomuk/global.h>
36 #include <nepomuk/resource.h>
37 #include <nepomuk/tag.h>
39 #include "projectlist.h"
40 #include "projectitem.h"
41 #include "kdenlivesettings.h"
42 #include "slideshowclip.h"
43 #include "ui_colorclip_ui.h"
44 #include "titlewidget.h"
47 #include "definitions.h"
48 #include "clipmanager.h"
49 #include "docclipbase.h"
50 #include "kdenlivedoc.h"
53 #include "projectlistview.h"
55 ProjectList::ProjectList(QWidget *parent)
56 : QWidget(parent), m_render(NULL), m_fps(-1), m_commandStack(NULL), m_selectedItem(NULL), m_infoQueue(QMap <QString, QDomElement> ()), m_thumbnailQueue(QList <QString> ()), m_refreshed(false) {
58 QWidget *vbox = new QWidget;
59 listView = new ProjectListView(this);;
60 QVBoxLayout *layout = new QVBoxLayout;
63 searchView = new KTreeWidgetSearchLine(this);
64 m_toolbar = new QToolBar("projectToolBar", this);
65 m_toolbar->addWidget(searchView);
67 QToolButton *addButton = new QToolButton(m_toolbar);
68 QMenu *addMenu = new QMenu(this);
69 addButton->setMenu(addMenu);
70 addButton->setPopupMode(QToolButton::MenuButtonPopup);
71 m_toolbar->addWidget(addButton);
73 QAction *addClipButton = addMenu->addAction(KIcon("kdenlive-add-clip"), i18n("Add Clip"));
74 connect(addClipButton, SIGNAL(triggered()), this, SLOT(slotAddClip()));
76 QAction *addColorClip = addMenu->addAction(KIcon("kdenlive-add-color-clip"), i18n("Add Color Clip"));
77 connect(addColorClip, SIGNAL(triggered()), this, SLOT(slotAddColorClip()));
79 QAction *addSlideClip = addMenu->addAction(KIcon("kdenlive-add-slide-clip"), i18n("Add Slideshow Clip"));
80 connect(addSlideClip, SIGNAL(triggered()), this, SLOT(slotAddSlideshowClip()));
82 QAction *addTitleClip = addMenu->addAction(KIcon("kdenlive-add-text-clip"), i18n("Add Title Clip"));
83 connect(addTitleClip, SIGNAL(triggered()), this, SLOT(slotAddTitleClip()));
85 m_deleteAction = m_toolbar->addAction(KIcon("edit-delete"), i18n("Delete Clip"));
86 connect(m_deleteAction, SIGNAL(triggered()), this, SLOT(slotRemoveClip()));
88 m_editAction = m_toolbar->addAction(KIcon("document-properties"), i18n("Edit Clip"));
89 connect(m_editAction, SIGNAL(triggered()), this, SLOT(slotEditClip()));
91 QAction *addFolderButton = addMenu->addAction(KIcon("folder-new"), i18n("Create Folder"));
92 connect(addFolderButton, SIGNAL(triggered()), this, SLOT(slotAddFolder()));
94 addButton->setDefaultAction(addClipButton);
96 layout->addWidget(m_toolbar);
97 layout->addWidget(listView);
99 //m_toolbar->setEnabled(false);
101 searchView->setTreeWidget(listView);
103 m_menu = new QMenu();
104 m_menu->addAction(addClipButton);
105 m_menu->addAction(addColorClip);
106 m_menu->addAction(addSlideClip);
107 m_menu->addAction(addTitleClip);
108 m_menu->addAction(m_editAction);
109 m_menu->addAction(m_deleteAction);
110 m_menu->addAction(addFolderButton);
111 m_menu->insertSeparator(m_deleteAction);
113 connect(listView, SIGNAL(itemSelectionChanged()), this, SLOT(slotClipSelected()));
114 connect(listView, SIGNAL(focusMonitor()), this, SLOT(slotClipSelected()));
115 connect(listView, SIGNAL(pauseMonitor()), this, SLOT(slotPauseMonitor()));
116 connect(listView, SIGNAL(requestMenu(const QPoint &, QTreeWidgetItem *)), this, SLOT(slotContextMenu(const QPoint &, QTreeWidgetItem *)));
117 connect(listView, SIGNAL(addClip()), this, SLOT(slotAddClip()));
118 connect(listView, SIGNAL(addClip(QUrl, const QString &)), this, SLOT(slotAddClip(QUrl, const QString &)));
119 connect(listView, SIGNAL(itemChanged(QTreeWidgetItem *, int)), this, SLOT(slotItemEdited(QTreeWidgetItem *, int)));
120 connect(listView, SIGNAL(showProperties(DocClipBase *)), this, SIGNAL(showClipProperties(DocClipBase *)));
122 m_listViewDelegate = new ItemDelegate(listView);
123 listView->setItemDelegate(m_listViewDelegate);
126 ProjectList::~ProjectList() {
131 QByteArray ProjectList::headerInfo() {
132 return listView->header()->saveState();
135 void ProjectList::setHeaderInfo(const QByteArray &state) {
136 listView->header()->restoreState(state);
139 void ProjectList::slotEditClip() {
140 ProjectItem *item = static_cast <ProjectItem*>(listView->currentItem());
141 if (item && !item->isGroup()) {
142 emit clipSelected(item->referencedClip());
143 emit showClipProperties(item->referencedClip());
149 void ProjectList::setRenderer(Render *projectRender) {
150 m_render = projectRender;
153 void ProjectList::slotClipSelected() {
154 if (listView->currentItem()) {
155 ProjectItem *clip = static_cast <ProjectItem*>(listView->currentItem());
156 if (!clip->isGroup()) {
157 m_selectedItem = clip;
158 emit clipSelected(clip->referencedClip());
160 m_editAction->setEnabled(true);
161 m_deleteAction->setEnabled(true);
163 m_editAction->setEnabled(false);
164 m_deleteAction->setEnabled(false);
168 void ProjectList::slotPauseMonitor() {
169 if (m_render) m_render->pause();
172 void ProjectList::slotUpdateClipProperties(const QString &id, QMap <QString, QString> properties) {
173 ProjectItem *item = getItemById(id);
175 slotUpdateClipProperties(item, properties);
176 if (properties.contains("colour") || properties.contains("resource") || properties.contains("xmldata")) slotRefreshClipThumbnail(item);
177 if (properties.contains("out")) item->changeDuration(properties.value("out").toInt());
181 void ProjectList::slotUpdateClipProperties(ProjectItem *clip, QMap <QString, QString> properties) {
183 if (!clip->isGroup()) clip->setProperties(properties);
184 if (properties.contains("description")) {
185 CLIPTYPE type = clip->clipType();
186 clip->setText(2, properties.value("description"));
187 if (KdenliveSettings::activate_nepomuk() && (type == AUDIO || type == VIDEO || type == AV || type == IMAGE || type == PLAYLIST)) {
188 // Use Nepomuk system to store clip description
189 Nepomuk::Resource f(clip->clipUrl().path());
191 f.setDescription(properties.value("description"));
193 KMessageBox::sorry(this, i18n("Cannot access Desktop Search info for %1.\nDisabling Desktop Search integration.", clip->clipUrl().path()));
194 KdenliveSettings::setActivate_nepomuk(false);
197 emit projectModified();
201 void ProjectList::slotItemEdited(QTreeWidgetItem *item, int column) {
202 ProjectItem *clip = static_cast <ProjectItem*>(item);
204 QMap <QString, QString> props;
205 props["description"] = item->text(2);
206 slotUpdateClipProperties(clip, props);
207 } else if (column == 1 && clip->isGroup()) {
208 m_doc->slotEditFolder(item->text(1), clip->groupName(), clip->clipId());
209 clip->setGroupName(item->text(1));
210 const int children = item->childCount();
211 for (int i = 0; i < children; i++) {
212 ProjectItem *child = static_cast <ProjectItem *>(item->child(i));
213 child->setProperty("groupname", item->text(1));
218 void ProjectList::slotContextMenu(const QPoint &pos, QTreeWidgetItem *item) {
223 m_editAction->setEnabled(enable);
224 m_deleteAction->setEnabled(enable);
228 void ProjectList::slotRemoveClip() {
229 if (!listView->currentItem()) return;
231 QMap <QString, QString> folderids;
232 QList<QTreeWidgetItem *> selected = listView->selectedItems();
234 for (int i = 0; i < selected.count(); i++) {
235 item = static_cast <ProjectItem *>(selected.at(i));
236 if (item->isGroup()) folderids[item->groupName()] = item->clipId();
237 else ids << item->clipId();
238 if (item->numReferences() > 0) {
239 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;
240 } else if (item->isGroup() && item->childCount() > 0) {
241 int children = item->childCount();
242 if (KMessageBox::questionYesNo(this, i18n("Delete folder <b>%2</b> ?<br>This will also remove the %1 clips in that folder", children, item->names().at(1)), i18n("Delete Folder")) != KMessageBox::Yes) return;
243 for (int i = 0; i < children; ++i) {
244 ProjectItem *child = static_cast <ProjectItem *>(item->child(i));
245 ids << child->clipId();
249 if (!ids.isEmpty()) m_doc->deleteProjectClip(ids);
250 if (!folderids.isEmpty()) m_doc->deleteProjectFolder(folderids);
251 if (listView->topLevelItemCount() == 0) {
252 m_editAction->setEnabled(false);
253 m_deleteAction->setEnabled(false);
257 void ProjectList::selectItemById(const QString &clipId) {
258 ProjectItem *item = getItemById(clipId);
259 if (item) listView->setCurrentItem(item);
263 void ProjectList::slotDeleteClip(const QString &clipId) {
264 ProjectItem *item = getItemById(clipId);
265 QTreeWidgetItem *p = item->parent();
267 kDebug() << "/////// DELETED CLIP HAS A PARENT... " << p->indexOfChild(item);
268 QTreeWidgetItem *clone = p->takeChild(p->indexOfChild(item));
274 void ProjectList::slotAddFolder() {
276 // QString folderName = KInputDialog::getText(i18n("New Folder"), i18n("Enter new folder name: "));
277 // if (folderName.isEmpty()) return;
278 m_doc->slotAddFolder(i18n("Folder")); //folderName);
281 void ProjectList::slotAddFolder(const QString foldername, const QString &clipId, bool remove, bool edit) {
284 QTreeWidgetItemIterator it(listView);
286 item = static_cast <ProjectItem *>(*it);
287 if (item->isGroup() && item->clipId() == clipId) {
295 listView->blockSignals(true);
297 QTreeWidgetItemIterator it(listView);
299 item = static_cast <ProjectItem *>(*it);
300 if (item->isGroup() && item->clipId() == clipId) {
301 item->setGroupName(foldername);
302 const int children = item->childCount();
303 for (int i = 0; i < children; i++) {
304 ProjectItem *child = static_cast <ProjectItem *>(item->child(i));
305 child->setProperty("groupname", foldername);
311 listView->blockSignals(false);
314 text << QString() << foldername;
315 (void) new ProjectItem(listView, text, clipId);
320 void ProjectList::slotAddClip(DocClipBase *clip, bool getProperties) {
321 const QString parent = clip->getProperty("groupid");
322 //kDebug() << "Adding clip with groupid: " << parent;
323 ProjectItem *item = NULL;
324 if (!parent.isEmpty()) {
325 ProjectItem *parentitem = getItemById(parent);
328 QString groupName = clip->getProperty("groupname");
329 //kDebug() << "Adding clip to new group: " << groupName;
330 if (groupName.isEmpty()) groupName = i18n("Folder");
331 text << QString() << groupName;
332 listView->blockSignals(true);
333 parentitem = new ProjectItem(listView, text, parent);
334 listView->blockSignals(false);
336 //kDebug() << "Adding clip to existing group: " << parentitem->groupName();
338 if (parentitem) item = new ProjectItem(parentitem, clip);
340 if (item == NULL) item = new ProjectItem(listView, clip);
342 KUrl url = clip->fileURL();
343 if (!url.isEmpty() && KdenliveSettings::activate_nepomuk()) {
344 // if file has Nepomuk comment, use it
345 Nepomuk::Resource f(url.path());
348 annotation = f.description();
350 Nepomuk::Tag tag("test");
353 KMessageBox::sorry(this, i18n("Cannot access Desktop Search info for %1.\nDisabling Desktop Search integration.", url.path()));
354 KdenliveSettings::setActivate_nepomuk(false);
356 if (!annotation.isEmpty()) item->setText(2, annotation);
358 if (getProperties) requestClipInfo(clip->toXML(), clip->getId());
361 void ProjectList::requestClipInfo(const QDomElement xml, const QString id) {
362 m_infoQueue.insert(id, xml);
363 listView->setEnabled(false);
364 if (m_infoQueue.count() == 1) QTimer::singleShot(300, this, SLOT(slotProcessNextClipInQueue()));
367 void ProjectList::slotProcessNextClipInQueue() {
368 if (m_infoQueue.isEmpty()) {
369 listView->setEnabled(true);
372 QMap<QString, QDomElement>::const_iterator i = m_infoQueue.constBegin();
373 if (i != m_infoQueue.constEnd()) {
374 const QDomElement dom = i.value();
375 const QString id = i.key();
376 m_infoQueue.remove(i.key());
377 emit getFileProperties(dom, id);
379 if (m_infoQueue.isEmpty()) listView->setEnabled(true);
382 void ProjectList::slotUpdateClip(const QString &id) {
383 ProjectItem *item = getItemById(id);
384 item->setData(1, UsageRole, QString::number(item->numReferences()));
387 void ProjectList::updateAllClips() {
388 QTreeWidgetItemIterator it(listView);
390 ProjectItem *item = static_cast <ProjectItem *>(*it);
391 if (!item->isGroup()) {
392 if (item->referencedClip()->producer() == NULL) {
393 DocClipBase *clip = item->referencedClip();
394 if (clip->clipType() == TEXT && !QFile::exists(clip->fileURL().path())) {
395 // regenerate text clip image if required
396 TitleWidget *dia_ui = new TitleWidget(KUrl(), QString(), m_render, this);
398 doc.setContent(clip->getProperty("xmldata"));
400 QPixmap pix = dia_ui->renderedPixmap();
401 pix.save(clip->fileURL().path());
404 requestClipInfo(clip->toXML(), clip->getId());
406 requestClipThumbnail(item->clipId());
407 item->changeDuration(item->referencedClip()->producer()->get_playtime());
409 item->setData(1, UsageRole, QString::number(item->numReferences()));
410 qApp->processEvents();
414 QTimer::singleShot(500, this, SLOT(slotCheckForEmptyQueue()));
417 void ProjectList::slotAddClip(QUrl givenUrl, QString group) {
418 if (!m_commandStack) kDebug() << "!!!!!!!!!!!!!!!! NO CMD STK";
420 if (givenUrl.isEmpty()) {
421 list = KFileDialog::getOpenUrls(KUrl("kfiledialog:///clipfolder"), "application/x-kdenlive video/x-flv application/vnd.rn-realmedia video/x-dv video/dv video/x-msvideo video/mpeg video/x-ms-wmv audio/mpeg audio/x-mp3 audio/x-wav application/ogg video/mp4 video/quicktime image/gif image/jpeg image/png image/x-bmp image/svg+xml image/tiff image/x-xcf-gimp image/x-vnd.adobe.photoshop image/x-pcx image/x-exr video/mlt-playlist audio/x-flac audio/mp4", this);
422 } else list.append(givenUrl);
423 if (list.isEmpty()) return;
425 QString groupId = QString();
426 if (group.isEmpty()) {
427 ProjectItem *item = static_cast <ProjectItem*>(listView->currentItem());
428 if (item && !item->isGroup()) {
429 while (item->parent()) {
430 item = static_cast <ProjectItem*>(item->parent());
431 if (item->isGroup()) break;
434 if (item && item->isGroup()) {
435 group = item->groupName();
436 groupId = item->clipId();
439 m_doc->slotAddClipList(list, group, groupId);
442 void ProjectList::slotRemoveInvalidClip(const QString &id) {
443 ProjectItem *item = getItemById(id);
445 const QString path = item->referencedClip()->fileURL().path();
446 if (!path.isEmpty()) KMessageBox::sorry(this, i18n("<qt>Clip <b>%1</b><br>is invalid, will be removed from project.", path));
449 m_doc->deleteProjectClip(ids);
451 if (!m_infoQueue.isEmpty()) QTimer::singleShot(300, this, SLOT(slotProcessNextClipInQueue()));
454 void ProjectList::slotAddColorClip() {
455 if (!m_commandStack) kDebug() << "!!!!!!!!!!!!!!!! NO CMD STK";
456 QDialog *dia = new QDialog(this);
457 Ui::ColorClip_UI *dia_ui = new Ui::ColorClip_UI();
458 dia_ui->setupUi(dia);
459 dia_ui->clip_name->setText(i18n("Color Clip"));
460 dia_ui->clip_duration->setText(KdenliveSettings::color_duration());
461 if (dia->exec() == QDialog::Accepted) {
462 QString color = dia_ui->clip_color->color().name();
463 color = color.replace(0, 1, "0x") + "ff";
465 QString group = QString();
466 QString groupId = QString();
467 ProjectItem *item = static_cast <ProjectItem*>(listView->currentItem());
468 if (item && !item->isGroup()) {
469 while (item->parent()) {
470 item = static_cast <ProjectItem*>(item->parent());
471 if (item->isGroup()) break;
474 if (item && item->isGroup()) {
475 group = item->groupName();
476 groupId = item->clipId();
479 m_doc->slotAddColorClipFile(dia_ui->clip_name->text(), color, dia_ui->clip_duration->text(), group, groupId);
486 void ProjectList::slotAddSlideshowClip() {
487 if (!m_commandStack) kDebug() << "!!!!!!!!!!!!!!!! NO CMD STK";
488 SlideshowClip *dia = new SlideshowClip(this);
490 if (dia->exec() == QDialog::Accepted) {
492 QString group = QString();
493 QString groupId = QString();
494 ProjectItem *item = static_cast <ProjectItem*>(listView->currentItem());
495 if (item && !item->isGroup()) {
496 while (item->parent()) {
497 item = static_cast <ProjectItem*>(item->parent());
498 if (item->isGroup()) break;
501 if (item && item->isGroup()) {
502 group = item->groupName();
503 groupId = item->clipId();
506 m_doc->slotAddSlideshowClipFile(dia->clipName(), dia->selectedPath(), dia->imageCount(), dia->clipDuration(), dia->loop(), dia->fade(), dia->lumaDuration(), dia->lumaFile(), dia->softness(), group, groupId);
511 void ProjectList::slotAddTitleClip() {
512 QString group = QString();
513 QString groupId = QString();
514 ProjectItem *item = static_cast <ProjectItem*>(listView->currentItem());
515 if (item && !item->isGroup()) {
516 while (item->parent()) {
517 item = static_cast <ProjectItem*>(item->parent());
518 if (item->isGroup()) break;
521 if (item && item->isGroup()) {
522 group = item->groupName();
523 groupId = item->clipId();
526 m_doc->slotCreateTextClip(group, groupId);
529 void ProjectList::setDocument(KdenliveDoc *doc) {
530 listView->blockSignals(true);
532 m_thumbnailQueue.clear();
535 QList <DocClipBase*> list = doc->clipManager()->documentClipList();
536 for (int i = 0; i < list.count(); i++) {
537 slotAddClip(list.at(i), false);
541 m_timecode = doc->timecode();
542 m_commandStack = doc->commandStack();
544 QTreeWidgetItem *first = listView->topLevelItem(0);
545 if (first) listView->setCurrentItem(first);
546 listView->blockSignals(false);
547 m_toolbar->setEnabled(true);
550 QDomElement ProjectList::producersList() {
552 QDomElement prods = doc.createElement("producerlist");
553 doc.appendChild(prods);
554 kDebug() << "//////////// PRO LISTĀ BUILD PRDSLIST ";
555 QTreeWidgetItemIterator it(listView);
557 if (!((ProjectItem *)(*it))->isGroup())
558 prods.appendChild(doc.importNode(((ProjectItem *)(*it))->toXml(), true));
564 void ProjectList::slotCheckForEmptyQueue() {
565 if (!m_refreshed && m_thumbnailQueue.isEmpty() && m_infoQueue.isEmpty()) {
567 emit loadingIsOver();
568 } else QTimer::singleShot(500, this, SLOT(slotCheckForEmptyQueue()));
571 void ProjectList::requestClipThumbnail(const QString &id) {
572 m_thumbnailQueue.append(id);
573 if (m_thumbnailQueue.count() == 1) QTimer::singleShot(300, this, SLOT(slotProcessNextThumbnail()));
576 void ProjectList::slotProcessNextThumbnail() {
577 if (m_thumbnailQueue.isEmpty()) {
580 slotRefreshClipThumbnail(m_thumbnailQueue.takeFirst(), false);
583 void ProjectList::slotRefreshClipThumbnail(const QString &clipId, bool update) {
584 ProjectItem *item = getItemById(clipId);
585 if (item) slotRefreshClipThumbnail(item, update);
586 else slotProcessNextThumbnail();
589 void ProjectList::slotRefreshClipThumbnail(ProjectItem *item, bool update) {
591 if (!item->referencedClip()) return;
593 int width = (int)(height * m_render->dar());
594 DocClipBase *clip = item->referencedClip();
595 if (!clip) slotProcessNextThumbnail();
597 if (clip->clipType() == AUDIO) pix = KIcon("audio-x-generic").pixmap(QSize(width, height));
598 else pix = item->referencedClip()->thumbProducer()->extractImage(item->referencedClip()->getClipThumbFrame(), width, height);
599 item->setIcon(0, pix);
600 if (update) emit projectModified();
601 if (!m_thumbnailQueue.isEmpty()) QTimer::singleShot(300, this, SLOT(slotProcessNextThumbnail()));
605 void ProjectList::slotReplyGetFileProperties(const QString &clipId, Mlt::Producer *producer, const QMap < QString, QString > &properties, const QMap < QString, QString > &metadata) {
606 ProjectItem *item = getItemById(clipId);
607 if (item && producer) {
608 item->setProperties(properties, metadata);
609 item->referencedClip()->setProducer(producer);
610 emit receivedClipDuration(clipId, item->clipMaxDuration());
611 } else kDebug() << "//////// COULD NOT FIND CLIP TO UPDATE PRPS...";
612 if (!m_infoQueue.isEmpty()) QTimer::singleShot(300, this, SLOT(slotProcessNextClipInQueue()));
615 void ProjectList::slotReplyGetImage(const QString &clipId, int pos, const QPixmap &pix, int w, int h) {
616 ProjectItem *item = getItemById(clipId);
617 if (item) item->setIcon(0, pix);
620 ProjectItem *ProjectList::getItemById(const QString &id) {
621 QTreeWidgetItemIterator it(listView);
623 if (((ProjectItem *)(*it))->clipId() == id)
624 return static_cast<ProjectItem *>(*it);
628 #ifdef USED_TO_BE_THIS
630 if (((ProjectItem *)(*it))->clipId() == id)
634 if (*it) return ((ProjectItem *)(*it));
639 void ProjectList::slotSelectClip(const QString &ix) {
640 ProjectItem *p = getItemById(ix);
642 listView->setCurrentItem(p);
643 listView->scrollToItem(p);
644 m_editAction->setEnabled(true);
645 m_deleteAction->setEnabled(true);
649 #include "projectlist.moc"