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;
61 layout->setContentsMargins(0, 0, 0, 0);
64 searchView = new KTreeWidgetSearchLine(this);
65 m_toolbar = new QToolBar("projectToolBar", this);
66 m_toolbar->addWidget(searchView);
68 QToolButton *addButton = new QToolButton(m_toolbar);
69 QMenu *addMenu = new QMenu(this);
70 addButton->setMenu(addMenu);
71 addButton->setPopupMode(QToolButton::MenuButtonPopup);
72 m_toolbar->addWidget(addButton);
74 QAction *addClipButton = addMenu->addAction(KIcon("kdenlive-add-clip"), i18n("Add Clip"));
75 connect(addClipButton, SIGNAL(triggered()), this, SLOT(slotAddClip()));
77 QAction *addColorClip = addMenu->addAction(KIcon("kdenlive-add-color-clip"), i18n("Add Color Clip"));
78 connect(addColorClip, SIGNAL(triggered()), this, SLOT(slotAddColorClip()));
80 QAction *addSlideClip = addMenu->addAction(KIcon("kdenlive-add-slide-clip"), i18n("Add Slideshow Clip"));
81 connect(addSlideClip, SIGNAL(triggered()), this, SLOT(slotAddSlideshowClip()));
83 QAction *addTitleClip = addMenu->addAction(KIcon("kdenlive-add-text-clip"), i18n("Add Title Clip"));
84 connect(addTitleClip, SIGNAL(triggered()), this, SLOT(slotAddTitleClip()));
86 m_deleteAction = m_toolbar->addAction(KIcon("edit-delete"), i18n("Delete Clip"));
87 connect(m_deleteAction, SIGNAL(triggered()), this, SLOT(slotRemoveClip()));
89 m_editAction = m_toolbar->addAction(KIcon("document-properties"), i18n("Edit Clip"));
90 connect(m_editAction, SIGNAL(triggered()), this, SLOT(slotEditClip()));
92 QAction *addFolderButton = addMenu->addAction(KIcon("folder-new"), i18n("Create Folder"));
93 connect(addFolderButton, SIGNAL(triggered()), this, SLOT(slotAddFolder()));
95 addButton->setDefaultAction(addClipButton);
97 layout->addWidget(m_toolbar);
98 layout->addWidget(listView);
100 //m_toolbar->setEnabled(false);
102 searchView->setTreeWidget(listView);
104 m_menu = new QMenu();
105 m_menu->addAction(addClipButton);
106 m_menu->addAction(addColorClip);
107 m_menu->addAction(addSlideClip);
108 m_menu->addAction(addTitleClip);
109 m_menu->addAction(m_editAction);
110 m_menu->addAction(m_deleteAction);
111 m_menu->addAction(addFolderButton);
112 m_menu->insertSeparator(m_deleteAction);
114 connect(listView, SIGNAL(itemSelectionChanged()), this, SLOT(slotClipSelected()));
115 connect(listView, SIGNAL(focusMonitor()), this, SLOT(slotClipSelected()));
116 connect(listView, SIGNAL(pauseMonitor()), this, SLOT(slotPauseMonitor()));
117 connect(listView, SIGNAL(requestMenu(const QPoint &, QTreeWidgetItem *)), this, SLOT(slotContextMenu(const QPoint &, QTreeWidgetItem *)));
118 connect(listView, SIGNAL(addClip()), this, SLOT(slotAddClip()));
119 connect(listView, SIGNAL(addClip(QUrl, const QString &)), this, SLOT(slotAddClip(QUrl, const QString &)));
120 connect(listView, SIGNAL(itemChanged(QTreeWidgetItem *, int)), this, SLOT(slotItemEdited(QTreeWidgetItem *, int)));
121 connect(listView, SIGNAL(showProperties(DocClipBase *)), this, SIGNAL(showClipProperties(DocClipBase *)));
123 m_listViewDelegate = new ItemDelegate(listView);
124 listView->setItemDelegate(m_listViewDelegate);
127 ProjectList::~ProjectList() {
132 QByteArray ProjectList::headerInfo() {
133 return listView->header()->saveState();
136 void ProjectList::setHeaderInfo(const QByteArray &state) {
137 listView->header()->restoreState(state);
140 void ProjectList::slotEditClip() {
141 ProjectItem *item = static_cast <ProjectItem*>(listView->currentItem());
142 if (item && !item->isGroup()) {
143 emit clipSelected(item->referencedClip());
144 emit showClipProperties(item->referencedClip());
150 void ProjectList::setRenderer(Render *projectRender) {
151 m_render = projectRender;
154 void ProjectList::slotClipSelected() {
155 if (listView->currentItem()) {
156 ProjectItem *clip = static_cast <ProjectItem*>(listView->currentItem());
157 if (!clip->isGroup()) {
158 m_selectedItem = clip;
159 emit clipSelected(clip->referencedClip());
161 m_editAction->setEnabled(true);
162 m_deleteAction->setEnabled(true);
164 m_editAction->setEnabled(false);
165 m_deleteAction->setEnabled(false);
169 void ProjectList::slotPauseMonitor() {
170 if (m_render) m_render->pause();
173 void ProjectList::slotUpdateClipProperties(const QString &id, QMap <QString, QString> properties) {
174 ProjectItem *item = getItemById(id);
176 slotUpdateClipProperties(item, properties);
177 if (properties.contains("colour") || properties.contains("resource") || properties.contains("xmldata")) slotRefreshClipThumbnail(item);
178 if (properties.contains("out")) item->changeDuration(properties.value("out").toInt());
182 void ProjectList::slotUpdateClipProperties(ProjectItem *clip, QMap <QString, QString> properties) {
184 if (!clip->isGroup()) clip->setProperties(properties);
185 if (properties.contains("description")) {
186 CLIPTYPE type = clip->clipType();
187 clip->setText(2, properties.value("description"));
188 if (KdenliveSettings::activate_nepomuk() && (type == AUDIO || type == VIDEO || type == AV || type == IMAGE || type == PLAYLIST)) {
189 // Use Nepomuk system to store clip description
190 Nepomuk::Resource f(clip->clipUrl().path());
192 f.setDescription(properties.value("description"));
194 KMessageBox::sorry(this, i18n("Cannot access Desktop Search info for %1.\nDisabling Desktop Search integration.", clip->clipUrl().path()));
195 KdenliveSettings::setActivate_nepomuk(false);
198 emit projectModified();
202 void ProjectList::slotItemEdited(QTreeWidgetItem *item, int column) {
203 ProjectItem *clip = static_cast <ProjectItem*>(item);
205 QMap <QString, QString> props;
206 props["description"] = item->text(2);
207 slotUpdateClipProperties(clip, props);
208 } else if (column == 1 && clip->isGroup()) {
209 m_doc->slotEditFolder(item->text(1), clip->groupName(), clip->clipId());
210 clip->setGroupName(item->text(1));
211 const int children = item->childCount();
212 for (int i = 0; i < children; i++) {
213 ProjectItem *child = static_cast <ProjectItem *>(item->child(i));
214 child->setProperty("groupname", item->text(1));
219 void ProjectList::slotContextMenu(const QPoint &pos, QTreeWidgetItem *item) {
224 m_editAction->setEnabled(enable);
225 m_deleteAction->setEnabled(enable);
229 void ProjectList::slotRemoveClip() {
230 if (!listView->currentItem()) return;
232 QMap <QString, QString> folderids;
233 QList<QTreeWidgetItem *> selected = listView->selectedItems();
235 for (int i = 0; i < selected.count(); i++) {
236 item = static_cast <ProjectItem *>(selected.at(i));
237 if (item->isGroup()) folderids[item->groupName()] = item->clipId();
238 else ids << item->clipId();
239 if (item->numReferences() > 0) {
240 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;
241 } else if (item->isGroup() && item->childCount() > 0) {
242 int children = item->childCount();
243 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;
244 for (int i = 0; i < children; ++i) {
245 ProjectItem *child = static_cast <ProjectItem *>(item->child(i));
246 ids << child->clipId();
250 if (!ids.isEmpty()) m_doc->deleteProjectClip(ids);
251 if (!folderids.isEmpty()) m_doc->deleteProjectFolder(folderids);
252 if (listView->topLevelItemCount() == 0) {
253 m_editAction->setEnabled(false);
254 m_deleteAction->setEnabled(false);
258 void ProjectList::selectItemById(const QString &clipId) {
259 ProjectItem *item = getItemById(clipId);
260 if (item) listView->setCurrentItem(item);
264 void ProjectList::slotDeleteClip(const QString &clipId) {
265 ProjectItem *item = getItemById(clipId);
266 QTreeWidgetItem *p = item->parent();
268 kDebug() << "/////// DELETED CLIP HAS A PARENT... " << p->indexOfChild(item);
269 QTreeWidgetItem *clone = p->takeChild(p->indexOfChild(item));
275 void ProjectList::slotAddFolder() {
277 // QString folderName = KInputDialog::getText(i18n("New Folder"), i18n("Enter new folder name: "));
278 // if (folderName.isEmpty()) return;
279 m_doc->slotAddFolder(i18n("Folder")); //folderName);
282 void ProjectList::slotAddFolder(const QString foldername, const QString &clipId, bool remove, bool edit) {
285 QTreeWidgetItemIterator it(listView);
287 item = static_cast <ProjectItem *>(*it);
288 if (item->isGroup() && item->clipId() == clipId) {
296 listView->blockSignals(true);
298 QTreeWidgetItemIterator it(listView);
300 item = static_cast <ProjectItem *>(*it);
301 if (item->isGroup() && item->clipId() == clipId) {
302 item->setGroupName(foldername);
303 const int children = item->childCount();
304 for (int i = 0; i < children; i++) {
305 ProjectItem *child = static_cast <ProjectItem *>(item->child(i));
306 child->setProperty("groupname", foldername);
312 listView->blockSignals(false);
315 text << QString() << foldername;
316 (void) new ProjectItem(listView, text, clipId);
321 void ProjectList::slotAddClip(DocClipBase *clip, bool getProperties) {
322 const QString parent = clip->getProperty("groupid");
323 //kDebug() << "Adding clip with groupid: " << parent;
324 ProjectItem *item = NULL;
325 if (!parent.isEmpty()) {
326 ProjectItem *parentitem = getItemById(parent);
329 QString groupName = clip->getProperty("groupname");
330 //kDebug() << "Adding clip to new group: " << groupName;
331 if (groupName.isEmpty()) groupName = i18n("Folder");
332 text << QString() << groupName;
333 listView->blockSignals(true);
334 parentitem = new ProjectItem(listView, text, parent);
335 listView->blockSignals(false);
337 //kDebug() << "Adding clip to existing group: " << parentitem->groupName();
339 if (parentitem) item = new ProjectItem(parentitem, clip);
341 if (item == NULL) item = new ProjectItem(listView, clip);
343 KUrl url = clip->fileURL();
344 if (!url.isEmpty() && KdenliveSettings::activate_nepomuk()) {
345 // if file has Nepomuk comment, use it
346 Nepomuk::Resource f(url.path());
349 annotation = f.description();
351 Nepomuk::Tag tag("test");
354 KMessageBox::sorry(this, i18n("Cannot access Desktop Search info for %1.\nDisabling Desktop Search integration.", url.path()));
355 KdenliveSettings::setActivate_nepomuk(false);
357 if (!annotation.isEmpty()) item->setText(2, annotation);
359 if (getProperties) requestClipInfo(clip->toXML(), clip->getId());
362 void ProjectList::requestClipInfo(const QDomElement xml, const QString id) {
363 m_infoQueue.insert(id, xml);
364 listView->setEnabled(false);
365 if (m_infoQueue.count() == 1) QTimer::singleShot(300, this, SLOT(slotProcessNextClipInQueue()));
368 void ProjectList::slotProcessNextClipInQueue() {
369 if (m_infoQueue.isEmpty()) {
370 listView->setEnabled(true);
373 QMap<QString, QDomElement>::const_iterator i = m_infoQueue.constBegin();
374 if (i != m_infoQueue.constEnd()) {
375 const QDomElement dom = i.value();
376 const QString id = i.key();
377 m_infoQueue.remove(i.key());
378 emit getFileProperties(dom, id);
380 if (m_infoQueue.isEmpty()) listView->setEnabled(true);
383 void ProjectList::slotUpdateClip(const QString &id) {
384 ProjectItem *item = getItemById(id);
385 item->setData(1, UsageRole, QString::number(item->numReferences()));
388 void ProjectList::updateAllClips() {
389 QTreeWidgetItemIterator it(listView);
391 ProjectItem *item = static_cast <ProjectItem *>(*it);
392 if (!item->isGroup()) {
393 if (item->referencedClip()->producer() == NULL) {
394 DocClipBase *clip = item->referencedClip();
395 if (clip->clipType() == TEXT && !QFile::exists(clip->fileURL().path())) {
396 // regenerate text clip image if required
397 TitleWidget *dia_ui = new TitleWidget(KUrl(), QString(), m_render, this);
399 doc.setContent(clip->getProperty("xmldata"));
401 QPixmap pix = dia_ui->renderedPixmap();
402 pix.save(clip->fileURL().path());
405 requestClipInfo(clip->toXML(), clip->getId());
407 requestClipThumbnail(item->clipId());
408 item->changeDuration(item->referencedClip()->producer()->get_playtime());
410 item->setData(1, UsageRole, QString::number(item->numReferences()));
411 qApp->processEvents();
415 QTimer::singleShot(500, this, SLOT(slotCheckForEmptyQueue()));
418 void ProjectList::slotAddClip(QUrl givenUrl, QString group) {
419 if (!m_commandStack) kDebug() << "!!!!!!!!!!!!!!!! NO CMD STK";
421 if (givenUrl.isEmpty()) {
422 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);
423 } else list.append(givenUrl);
424 if (list.isEmpty()) return;
426 QString groupId = QString();
427 if (group.isEmpty()) {
428 ProjectItem *item = static_cast <ProjectItem*>(listView->currentItem());
429 if (item && !item->isGroup()) {
430 while (item->parent()) {
431 item = static_cast <ProjectItem*>(item->parent());
432 if (item->isGroup()) break;
435 if (item && item->isGroup()) {
436 group = item->groupName();
437 groupId = item->clipId();
440 m_doc->slotAddClipList(list, group, groupId);
443 void ProjectList::slotRemoveInvalidClip(const QString &id) {
444 ProjectItem *item = getItemById(id);
446 const QString path = item->referencedClip()->fileURL().path();
447 if (!path.isEmpty()) KMessageBox::sorry(this, i18n("<qt>Clip <b>%1</b><br>is invalid, will be removed from project.", path));
450 m_doc->deleteProjectClip(ids);
452 if (!m_infoQueue.isEmpty()) QTimer::singleShot(300, this, SLOT(slotProcessNextClipInQueue()));
455 void ProjectList::slotAddColorClip() {
456 if (!m_commandStack) kDebug() << "!!!!!!!!!!!!!!!! NO CMD STK";
457 QDialog *dia = new QDialog(this);
458 Ui::ColorClip_UI *dia_ui = new Ui::ColorClip_UI();
459 dia_ui->setupUi(dia);
460 dia_ui->clip_name->setText(i18n("Color Clip"));
461 dia_ui->clip_duration->setText(KdenliveSettings::color_duration());
462 if (dia->exec() == QDialog::Accepted) {
463 QString color = dia_ui->clip_color->color().name();
464 color = color.replace(0, 1, "0x") + "ff";
466 QString group = QString();
467 QString groupId = QString();
468 ProjectItem *item = static_cast <ProjectItem*>(listView->currentItem());
469 if (item && !item->isGroup()) {
470 while (item->parent()) {
471 item = static_cast <ProjectItem*>(item->parent());
472 if (item->isGroup()) break;
475 if (item && item->isGroup()) {
476 group = item->groupName();
477 groupId = item->clipId();
480 m_doc->slotAddColorClipFile(dia_ui->clip_name->text(), color, dia_ui->clip_duration->text(), group, groupId);
487 void ProjectList::slotAddSlideshowClip() {
488 if (!m_commandStack) kDebug() << "!!!!!!!!!!!!!!!! NO CMD STK";
489 SlideshowClip *dia = new SlideshowClip(this);
491 if (dia->exec() == QDialog::Accepted) {
493 QString group = QString();
494 QString groupId = QString();
495 ProjectItem *item = static_cast <ProjectItem*>(listView->currentItem());
496 if (item && !item->isGroup()) {
497 while (item->parent()) {
498 item = static_cast <ProjectItem*>(item->parent());
499 if (item->isGroup()) break;
502 if (item && item->isGroup()) {
503 group = item->groupName();
504 groupId = item->clipId();
507 m_doc->slotAddSlideshowClipFile(dia->clipName(), dia->selectedPath(), dia->imageCount(), dia->clipDuration(), dia->loop(), dia->fade(), dia->lumaDuration(), dia->lumaFile(), dia->softness(), group, groupId);
512 void ProjectList::slotAddTitleClip() {
513 QString group = QString();
514 QString groupId = QString();
515 ProjectItem *item = static_cast <ProjectItem*>(listView->currentItem());
516 if (item && !item->isGroup()) {
517 while (item->parent()) {
518 item = static_cast <ProjectItem*>(item->parent());
519 if (item->isGroup()) break;
522 if (item && item->isGroup()) {
523 group = item->groupName();
524 groupId = item->clipId();
527 m_doc->slotCreateTextClip(group, groupId);
530 void ProjectList::setDocument(KdenliveDoc *doc) {
531 listView->blockSignals(true);
533 m_thumbnailQueue.clear();
536 QList <DocClipBase*> list = doc->clipManager()->documentClipList();
537 for (int i = 0; i < list.count(); i++) {
538 slotAddClip(list.at(i), false);
542 m_timecode = doc->timecode();
543 m_commandStack = doc->commandStack();
545 QTreeWidgetItem *first = listView->topLevelItem(0);
546 if (first) listView->setCurrentItem(first);
547 listView->blockSignals(false);
548 m_toolbar->setEnabled(true);
551 QDomElement ProjectList::producersList() {
553 QDomElement prods = doc.createElement("producerlist");
554 doc.appendChild(prods);
555 kDebug() << "//////////// PRO LISTĀ BUILD PRDSLIST ";
556 QTreeWidgetItemIterator it(listView);
558 if (!((ProjectItem *)(*it))->isGroup())
559 prods.appendChild(doc.importNode(((ProjectItem *)(*it))->toXml(), true));
565 void ProjectList::slotCheckForEmptyQueue() {
566 if (!m_refreshed && m_thumbnailQueue.isEmpty() && m_infoQueue.isEmpty()) {
568 emit loadingIsOver();
569 } else QTimer::singleShot(500, this, SLOT(slotCheckForEmptyQueue()));
572 void ProjectList::requestClipThumbnail(const QString &id) {
573 m_thumbnailQueue.append(id);
574 if (m_thumbnailQueue.count() == 1) QTimer::singleShot(300, this, SLOT(slotProcessNextThumbnail()));
577 void ProjectList::slotProcessNextThumbnail() {
578 if (m_thumbnailQueue.isEmpty()) {
581 slotRefreshClipThumbnail(m_thumbnailQueue.takeFirst(), false);
584 void ProjectList::slotRefreshClipThumbnail(const QString &clipId, bool update) {
585 ProjectItem *item = getItemById(clipId);
586 if (item) slotRefreshClipThumbnail(item, update);
587 else slotProcessNextThumbnail();
590 void ProjectList::slotRefreshClipThumbnail(ProjectItem *item, bool update) {
592 if (!item->referencedClip()) return;
594 int width = (int)(height * m_render->dar());
595 DocClipBase *clip = item->referencedClip();
596 if (!clip) slotProcessNextThumbnail();
598 if (clip->clipType() == AUDIO) pix = KIcon("audio-x-generic").pixmap(QSize(width, height));
599 else pix = item->referencedClip()->thumbProducer()->extractImage(item->referencedClip()->getClipThumbFrame(), width, height);
600 item->setIcon(0, pix);
601 if (update) emit projectModified();
602 if (!m_thumbnailQueue.isEmpty()) QTimer::singleShot(300, this, SLOT(slotProcessNextThumbnail()));
606 void ProjectList::slotReplyGetFileProperties(const QString &clipId, Mlt::Producer *producer, const QMap < QString, QString > &properties, const QMap < QString, QString > &metadata) {
607 ProjectItem *item = getItemById(clipId);
608 if (item && producer) {
609 item->setProperties(properties, metadata);
610 item->referencedClip()->setProducer(producer);
611 emit receivedClipDuration(clipId, item->clipMaxDuration());
612 } else kDebug() << "//////// COULD NOT FIND CLIP TO UPDATE PRPS...";
613 if (!m_infoQueue.isEmpty()) QTimer::singleShot(300, this, SLOT(slotProcessNextClipInQueue()));
616 void ProjectList::slotReplyGetImage(const QString &clipId, int pos, const QPixmap &pix, int w, int h) {
617 ProjectItem *item = getItemById(clipId);
618 if (item) item->setIcon(0, pix);
621 ProjectItem *ProjectList::getItemById(const QString &id) {
622 QTreeWidgetItemIterator it(listView);
624 if (((ProjectItem *)(*it))->clipId() == id)
625 return static_cast<ProjectItem *>(*it);
629 #ifdef USED_TO_BE_THIS
631 if (((ProjectItem *)(*it))->clipId() == id)
635 if (*it) return ((ProjectItem *)(*it));
640 void ProjectList::slotSelectClip(const QString &ix) {
641 ProjectItem *p = getItemById(ix);
643 listView->setCurrentItem(p);
644 listView->scrollToItem(p);
645 m_editAction->setEnabled(true);
646 m_deleteAction->setEnabled(true);
650 #include "projectlist.moc"