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(requestMenu(const QPoint &, QTreeWidgetItem *)), this, SLOT(slotContextMenu(const QPoint &, QTreeWidgetItem *)));
116 connect(listView, SIGNAL(addClip()), this, SLOT(slotAddClip()));
117 connect(listView, SIGNAL(addClip(QUrl, const QString &)), this, SLOT(slotAddClip(QUrl, const QString &)));
118 connect(listView, SIGNAL(itemChanged(QTreeWidgetItem *, int)), this, SLOT(slotItemEdited(QTreeWidgetItem *, int)));
119 connect(listView, SIGNAL(showProperties(DocClipBase *)), this, SIGNAL(showClipProperties(DocClipBase *)));
121 m_listViewDelegate = new ItemDelegate(listView);
122 listView->setItemDelegate(m_listViewDelegate);
125 ProjectList::~ProjectList() {
130 QByteArray ProjectList::headerInfo() {
131 return listView->header()->saveState();
134 void ProjectList::setHeaderInfo(const QByteArray &state) {
135 listView->header()->restoreState(state);
138 void ProjectList::slotEditClip() {
139 ProjectItem *item = static_cast <ProjectItem*>(listView->currentItem());
140 if (item && !item->isGroup()) {
141 emit clipSelected(item->referencedClip());
142 emit showClipProperties(item->referencedClip());
148 void ProjectList::setRenderer(Render *projectRender) {
149 m_render = projectRender;
152 void ProjectList::slotClipSelected() {
153 if (listView->currentItem()) {
154 ProjectItem *clip = static_cast <ProjectItem*>(listView->currentItem());
155 if (!clip->isGroup()) {
156 m_selectedItem = clip;
157 emit clipSelected(clip->referencedClip());
159 m_editAction->setEnabled(true);
160 m_deleteAction->setEnabled(true);
162 m_editAction->setEnabled(false);
163 m_deleteAction->setEnabled(false);
167 void ProjectList::slotUpdateClipProperties(const QString &id, QMap <QString, QString> properties) {
168 ProjectItem *item = getItemById(id);
170 slotUpdateClipProperties(item, properties);
171 if (properties.contains("colour") || properties.contains("resource") || properties.contains("xmldata")) slotRefreshClipThumbnail(item);
172 if (properties.contains("out")) item->changeDuration(properties.value("out").toInt());
176 void ProjectList::slotUpdateClipProperties(ProjectItem *clip, QMap <QString, QString> properties) {
178 if (!clip->isGroup()) clip->setProperties(properties);
179 if (properties.contains("description")) {
180 CLIPTYPE type = clip->clipType();
181 clip->setText(2, properties.value("description"));
182 if (KdenliveSettings::activate_nepomuk() && (type == AUDIO || type == VIDEO || type == AV || type == IMAGE || type == PLAYLIST)) {
183 // Use Nepomuk system to store clip description
184 Nepomuk::Resource f(clip->clipUrl().path());
186 f.setDescription(properties.value("description"));
188 KMessageBox::sorry(this, i18n("Cannot access Desktop Search info for %1.\nDisabling Desktop Search integration.", clip->clipUrl().path()));
189 KdenliveSettings::setActivate_nepomuk(false);
192 emit projectModified();
196 void ProjectList::slotItemEdited(QTreeWidgetItem *item, int column) {
197 ProjectItem *clip = static_cast <ProjectItem*>(item);
199 QMap <QString, QString> props;
200 props["description"] = item->text(2);
201 slotUpdateClipProperties(clip, props);
202 } else if (column == 1 && clip->isGroup()) {
203 m_doc->slotEditFolder(item->text(1), clip->groupName(), clip->clipId());
204 clip->setGroupName(item->text(1));
205 const int children = item->childCount();
206 for (int i = 0; i < children; i++) {
207 ProjectItem *child = static_cast <ProjectItem *>(item->child(i));
208 child->setProperty("groupname", item->text(1));
213 void ProjectList::slotContextMenu(const QPoint &pos, QTreeWidgetItem *item) {
218 m_editAction->setEnabled(enable);
219 m_deleteAction->setEnabled(enable);
223 void ProjectList::slotRemoveClip() {
224 if (!listView->currentItem()) return;
226 QMap <QString, QString> folderids;
227 QList<QTreeWidgetItem *> selected = listView->selectedItems();
229 for (int i = 0; i < selected.count(); i++) {
230 item = static_cast <ProjectItem *>(selected.at(i));
231 if (item->isGroup()) folderids[item->groupName()] = item->clipId();
232 else ids << item->clipId();
233 if (item->numReferences() > 0) {
234 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;
235 } else if (item->isGroup() && item->childCount() > 0) {
236 int children = item->childCount();
237 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;
238 for (int i = 0; i < children; ++i) {
239 ProjectItem *child = static_cast <ProjectItem *>(item->child(i));
240 ids << child->clipId();
244 if (!ids.isEmpty()) m_doc->deleteProjectClip(ids);
245 if (!folderids.isEmpty()) m_doc->deleteProjectFolder(folderids);
246 if (listView->topLevelItemCount() == 0) {
247 m_editAction->setEnabled(false);
248 m_deleteAction->setEnabled(false);
252 void ProjectList::selectItemById(const QString &clipId) {
253 ProjectItem *item = getItemById(clipId);
254 if (item) listView->setCurrentItem(item);
258 void ProjectList::slotDeleteClip(const QString &clipId) {
259 ProjectItem *item = getItemById(clipId);
260 QTreeWidgetItem *p = item->parent();
262 kDebug() << "/////// DELETED CLIP HAS A PARENT... " << p->indexOfChild(item);
263 QTreeWidgetItem *clone = p->takeChild(p->indexOfChild(item));
269 void ProjectList::slotAddFolder() {
271 // QString folderName = KInputDialog::getText(i18n("New Folder"), i18n("Enter new folder name: "));
272 // if (folderName.isEmpty()) return;
273 m_doc->slotAddFolder(i18n("Folder")); //folderName);
276 void ProjectList::slotAddFolder(const QString foldername, const QString &clipId, bool remove, bool edit) {
279 QTreeWidgetItemIterator it(listView);
281 item = static_cast <ProjectItem *>(*it);
282 if (item->isGroup() && item->clipId() == clipId) {
290 listView->blockSignals(true);
292 QTreeWidgetItemIterator it(listView);
294 item = static_cast <ProjectItem *>(*it);
295 if (item->isGroup() && item->clipId() == clipId) {
296 item->setGroupName(foldername);
297 const int children = item->childCount();
298 for (int i = 0; i < children; i++) {
299 ProjectItem *child = static_cast <ProjectItem *>(item->child(i));
300 child->setProperty("groupname", foldername);
306 listView->blockSignals(false);
309 text << QString() << foldername;
310 (void) new ProjectItem(listView, text, clipId);
315 void ProjectList::slotAddClip(DocClipBase *clip, bool getProperties) {
316 const QString parent = clip->getProperty("groupid");
317 //kDebug() << "Adding clip with groupid: " << parent;
318 ProjectItem *item = NULL;
319 if (!parent.isEmpty()) {
320 ProjectItem *parentitem = getItemById(parent);
323 QString groupName = clip->getProperty("groupname");
324 //kDebug() << "Adding clip to new group: " << groupName;
325 if (groupName.isEmpty()) groupName = i18n("Folder");
326 text << QString() << groupName;
327 listView->blockSignals(true);
328 parentitem = new ProjectItem(listView, text, parent);
329 listView->blockSignals(false);
331 //kDebug() << "Adding clip to existing group: " << parentitem->groupName();
333 if (parentitem) item = new ProjectItem(parentitem, clip);
335 if (item == NULL) item = new ProjectItem(listView, clip);
337 KUrl url = clip->fileURL();
338 if (!url.isEmpty() && KdenliveSettings::activate_nepomuk()) {
339 // if file has Nepomuk comment, use it
340 Nepomuk::Resource f(url.path());
343 annotation = f.description();
345 Nepomuk::Tag tag("test");
348 KMessageBox::sorry(this, i18n("Cannot access Desktop Search info for %1.\nDisabling Desktop Search integration.", url.path()));
349 KdenliveSettings::setActivate_nepomuk(false);
351 if (!annotation.isEmpty()) item->setText(2, annotation);
353 if (getProperties) requestClipInfo(clip->toXML(), clip->getId());
356 void ProjectList::requestClipInfo(const QDomElement xml, const QString id) {
357 m_infoQueue.insert(id, xml);
358 listView->setEnabled(false);
359 if (m_infoQueue.count() == 1) QTimer::singleShot(300, this, SLOT(slotProcessNextClipInQueue()));
362 void ProjectList::slotProcessNextClipInQueue() {
363 if (m_infoQueue.isEmpty()) {
364 listView->setEnabled(true);
367 QMap<QString, QDomElement>::const_iterator i = m_infoQueue.constBegin();
368 if (i != m_infoQueue.constEnd()) {
369 const QDomElement dom = i.value();
370 const QString id = i.key();
371 m_infoQueue.remove(i.key());
372 emit getFileProperties(dom, id);
374 if (m_infoQueue.isEmpty()) listView->setEnabled(true);
377 void ProjectList::slotUpdateClip(const QString &id) {
378 ProjectItem *item = getItemById(id);
379 item->setData(1, UsageRole, QString::number(item->numReferences()));
382 void ProjectList::updateAllClips() {
383 QTreeWidgetItemIterator it(listView);
385 ProjectItem *item = static_cast <ProjectItem *>(*it);
386 if (!item->isGroup()) {
387 if (item->referencedClip()->producer() == NULL) {
388 DocClipBase *clip = item->referencedClip();
389 if (clip->clipType() == TEXT && !QFile::exists(clip->fileURL().path())) {
390 // regenerate text clip image if required
391 TitleWidget *dia_ui = new TitleWidget(KUrl(), QString(), m_render, this);
393 doc.setContent(clip->getProperty("xmldata"));
395 QPixmap pix = dia_ui->renderedPixmap();
396 pix.save(clip->fileURL().path());
399 requestClipInfo(clip->toXML(), clip->getId());
401 requestClipThumbnail(item->clipId());
402 item->changeDuration(item->referencedClip()->producer()->get_playtime());
404 item->setData(1, UsageRole, QString::number(item->numReferences()));
405 qApp->processEvents();
409 QTimer::singleShot(500, this, SLOT(slotCheckForEmptyQueue()));
412 void ProjectList::slotAddClip(QUrl givenUrl, QString group) {
413 if (!m_commandStack) kDebug() << "!!!!!!!!!!!!!!!! NO CMD STK";
415 if (givenUrl.isEmpty()) {
416 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);
417 } else list.append(givenUrl);
418 if (list.isEmpty()) return;
420 QString groupId = QString();
421 if (group.isEmpty()) {
422 ProjectItem *item = static_cast <ProjectItem*>(listView->currentItem());
423 if (item && !item->isGroup()) {
424 while (item->parent()) {
425 item = static_cast <ProjectItem*>(item->parent());
426 if (item->isGroup()) break;
429 if (item && item->isGroup()) {
430 group = item->groupName();
431 groupId = item->clipId();
434 m_doc->slotAddClipList(list, group, groupId);
437 void ProjectList::slotRemoveInvalidClip(const QString &id) {
438 ProjectItem *item = getItemById(id);
440 QString path = item->referencedClip()->fileURL().path();
441 if (!path.isEmpty()) KMessageBox::sorry(this, i18n("<qt>Clip <b>%1</b><br>is invalid, will be removed from project.", path));
446 m_doc->deleteProjectClip(ids);
447 if (!m_infoQueue.isEmpty()) QTimer::singleShot(300, this, SLOT(slotProcessNextClipInQueue()));
450 void ProjectList::slotAddColorClip() {
451 if (!m_commandStack) kDebug() << "!!!!!!!!!!!!!!!! NO CMD STK";
452 QDialog *dia = new QDialog(this);
453 Ui::ColorClip_UI *dia_ui = new Ui::ColorClip_UI();
454 dia_ui->setupUi(dia);
455 dia_ui->clip_name->setText(i18n("Color Clip"));
456 dia_ui->clip_duration->setText(KdenliveSettings::color_duration());
457 if (dia->exec() == QDialog::Accepted) {
458 QString color = dia_ui->clip_color->color().name();
459 color = color.replace(0, 1, "0x") + "ff";
461 QString group = QString();
462 QString groupId = QString();
463 ProjectItem *item = static_cast <ProjectItem*>(listView->currentItem());
464 if (item && !item->isGroup()) {
465 while (item->parent()) {
466 item = static_cast <ProjectItem*>(item->parent());
467 if (item->isGroup()) break;
470 if (item && item->isGroup()) {
471 group = item->groupName();
472 groupId = item->clipId();
475 m_doc->slotAddColorClipFile(dia_ui->clip_name->text(), color, dia_ui->clip_duration->text(), group, groupId);
482 void ProjectList::slotAddSlideshowClip() {
483 if (!m_commandStack) kDebug() << "!!!!!!!!!!!!!!!! NO CMD STK";
484 SlideshowClip *dia = new SlideshowClip(this);
486 if (dia->exec() == QDialog::Accepted) {
488 QString group = QString();
489 QString groupId = QString();
490 ProjectItem *item = static_cast <ProjectItem*>(listView->currentItem());
491 if (item && !item->isGroup()) {
492 while (item->parent()) {
493 item = static_cast <ProjectItem*>(item->parent());
494 if (item->isGroup()) break;
497 if (item && item->isGroup()) {
498 group = item->groupName();
499 groupId = item->clipId();
502 m_doc->slotAddSlideshowClipFile(dia->clipName(), dia->selectedPath(), dia->imageCount(), dia->clipDuration(), dia->loop(), dia->fade(), dia->lumaDuration(), dia->lumaFile(), dia->softness(), group, groupId);
507 void ProjectList::slotAddTitleClip() {
508 QString group = QString();
509 QString groupId = QString();
510 ProjectItem *item = static_cast <ProjectItem*>(listView->currentItem());
511 if (item && !item->isGroup()) {
512 while (item->parent()) {
513 item = static_cast <ProjectItem*>(item->parent());
514 if (item->isGroup()) break;
517 if (item && item->isGroup()) {
518 group = item->groupName();
519 groupId = item->clipId();
522 m_doc->slotCreateTextClip(group, groupId);
525 void ProjectList::setDocument(KdenliveDoc *doc) {
526 listView->blockSignals(true);
528 m_thumbnailQueue.clear();
531 QList <DocClipBase*> list = doc->clipManager()->documentClipList();
532 for (int i = 0; i < list.count(); i++) {
533 slotAddClip(list.at(i), false);
537 m_timecode = doc->timecode();
538 m_commandStack = doc->commandStack();
540 QTreeWidgetItem *first = listView->topLevelItem(0);
541 if (first) listView->setCurrentItem(first);
542 listView->blockSignals(false);
543 m_toolbar->setEnabled(true);
546 QDomElement ProjectList::producersList() {
548 QDomElement prods = doc.createElement("producerlist");
549 doc.appendChild(prods);
550 kDebug() << "//////////// PRO LISTĀ BUILD PRDSLIST ";
551 QTreeWidgetItemIterator it(listView);
553 if (!((ProjectItem *)(*it))->isGroup())
554 prods.appendChild(doc.importNode(((ProjectItem *)(*it))->toXml(), true));
560 void ProjectList::slotCheckForEmptyQueue() {
561 if (!m_refreshed && m_thumbnailQueue.isEmpty() && m_infoQueue.isEmpty()) {
563 emit loadingIsOver();
564 } else QTimer::singleShot(500, this, SLOT(slotCheckForEmptyQueue()));
567 void ProjectList::requestClipThumbnail(const QString &id) {
568 m_thumbnailQueue.append(id);
569 if (m_thumbnailQueue.count() == 1) QTimer::singleShot(300, this, SLOT(slotProcessNextThumbnail()));
572 void ProjectList::slotProcessNextThumbnail() {
573 if (m_thumbnailQueue.isEmpty()) {
576 slotRefreshClipThumbnail(m_thumbnailQueue.takeFirst(), false);
579 void ProjectList::slotRefreshClipThumbnail(const QString &clipId, bool update) {
580 ProjectItem *item = getItemById(clipId);
581 if (item) slotRefreshClipThumbnail(item, update);
582 else slotProcessNextThumbnail();
585 void ProjectList::slotRefreshClipThumbnail(ProjectItem *item, bool update) {
587 if (!item->referencedClip()) return;
589 int width = (int)(height * m_render->dar());
590 DocClipBase *clip = item->referencedClip();
591 if (!clip) slotProcessNextThumbnail();
593 if (clip->clipType() == AUDIO) pix = KIcon("audio-x-generic").pixmap(QSize(width, height));
594 else pix = item->referencedClip()->thumbProducer()->extractImage(item->referencedClip()->getClipThumbFrame(), width, height);
595 item->setIcon(0, pix);
596 if (update) emit projectModified();
597 if (!m_thumbnailQueue.isEmpty()) QTimer::singleShot(300, this, SLOT(slotProcessNextThumbnail()));
601 void ProjectList::slotReplyGetFileProperties(const QString &clipId, Mlt::Producer *producer, const QMap < QString, QString > &properties, const QMap < QString, QString > &metadata) {
602 ProjectItem *item = getItemById(clipId);
603 if (item && producer) {
604 item->setProperties(properties, metadata);
605 item->referencedClip()->setProducer(producer);
606 emit receivedClipDuration(clipId, item->clipMaxDuration());
607 } else kDebug() << "//////// COULD NOT FIND CLIP TO UPDATE PRPS...";
608 if (!m_infoQueue.isEmpty()) QTimer::singleShot(300, this, SLOT(slotProcessNextClipInQueue()));
611 void ProjectList::slotReplyGetImage(const QString &clipId, int pos, const QPixmap &pix, int w, int h) {
612 ProjectItem *item = getItemById(clipId);
613 if (item) item->setIcon(0, pix);
616 ProjectItem *ProjectList::getItemById(const QString &id) {
617 QTreeWidgetItemIterator it(listView);
619 if (((ProjectItem *)(*it))->clipId() == id)
620 return static_cast<ProjectItem *>(*it);
624 #ifdef USED_TO_BE_THIS
626 if (((ProjectItem *)(*it))->clipId() == id)
630 if (*it) return ((ProjectItem *)(*it));
635 void ProjectList::slotSelectClip(const QString &ix) {
636 ProjectItem *p = getItemById(ix);
638 listView->setCurrentItem(p);
639 listView->scrollToItem(p);
640 m_editAction->setEnabled(true);
641 m_deleteAction->setEnabled(true);
645 #include "projectlist.moc"