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) {
58 QWidget *vbox = new QWidget;
59 listView = new ProjectListView(this);;
60 QVBoxLayout *layout = new QVBoxLayout;
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(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 void ProjectList::slotEditClip() {
132 ProjectItem *item = static_cast <ProjectItem*>(listView->currentItem());
133 if (item && !item->isGroup()) {
134 emit clipSelected(item->referencedClip());
135 emit showClipProperties(item->referencedClip());
141 void ProjectList::setRenderer(Render *projectRender) {
142 m_render = projectRender;
145 void ProjectList::slotClipSelected() {
146 if (listView->currentItem()) {
147 ProjectItem *clip = static_cast <ProjectItem*>(listView->currentItem());
148 if (!clip->isGroup()) {
149 m_selectedItem = clip;
150 emit clipSelected(clip->referencedClip());
152 m_editAction->setEnabled(true);
153 m_deleteAction->setEnabled(true);
155 m_editAction->setEnabled(false);
156 m_deleteAction->setEnabled(false);
160 void ProjectList::slotUpdateClipProperties(const QString &id, QMap <QString, QString> properties) {
161 ProjectItem *item = getItemById(id);
163 slotUpdateClipProperties(item, properties);
164 if (properties.contains("colour") || properties.contains("resource") || properties.contains("xmldata")) slotRefreshClipThumbnail(item);
165 if (properties.contains("out")) item->changeDuration(properties.value("out").toInt());
169 void ProjectList::slotUpdateClipProperties(ProjectItem *clip, QMap <QString, QString> properties) {
171 if (!clip->isGroup()) clip->setProperties(properties);
172 if (properties.contains("description")) {
173 CLIPTYPE type = clip->clipType();
174 clip->setText(2, properties.value("description"));
175 if (KdenliveSettings::activate_nepomuk() && (type == AUDIO || type == VIDEO || type == AV || type == IMAGE || type == PLAYLIST)) {
176 // Use Nepomuk system to store clip description
177 Nepomuk::Resource f(clip->clipUrl().path());
179 f.setDescription(properties.value("description"));
181 KMessageBox::sorry(this, i18n("Cannot access Desktop Search info for %1.\nDisabling Desktop Search integration.", clip->clipUrl().path()));
182 KdenliveSettings::setActivate_nepomuk(false);
185 emit projectModified();
189 void ProjectList::slotItemEdited(QTreeWidgetItem *item, int column) {
190 ProjectItem *clip = static_cast <ProjectItem*>(item);
192 QMap <QString, QString> props;
193 props["description"] = item->text(2);
194 slotUpdateClipProperties(clip, props);
195 } else if (column == 1 && clip->isGroup()) {
196 m_doc->slotEditFolder(item->text(1), clip->groupName(), clip->clipId());
197 const int children = item->childCount();
198 for (int i = 0; i < children; i++) {
199 ProjectItem *child = static_cast <ProjectItem *>(item->child(i));
200 child->setProperty("groupname", item->text(1));
205 void ProjectList::slotContextMenu(const QPoint &pos, QTreeWidgetItem *item) {
210 m_editAction->setEnabled(enable);
211 m_deleteAction->setEnabled(enable);
215 void ProjectList::slotRemoveClip() {
216 if (!listView->currentItem()) return;
218 QMap <QString, QString> folderids;
219 QList<QTreeWidgetItem *> selected = listView->selectedItems();
221 for (int i = 0; i < selected.count(); i++) {
222 item = static_cast <ProjectItem *>(selected.at(i));
223 if (item->isGroup()) folderids[item->groupName()] = item->clipId();
224 else ids << item->clipId();
225 if (item->numReferences() > 0) {
226 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;
227 } else if (item->isGroup() && item->childCount() > 0) {
228 int children = item->childCount();
229 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;
230 for (int i = 0; i < children; ++i) {
231 ProjectItem *child = static_cast <ProjectItem *>(item->child(i));
232 ids << child->clipId();
236 if (!ids.isEmpty()) m_doc->deleteProjectClip(ids);
237 if (!folderids.isEmpty()) m_doc->deleteProjectFolder(folderids);
238 if (listView->topLevelItemCount() == 0) {
239 m_editAction->setEnabled(false);
240 m_deleteAction->setEnabled(false);
244 void ProjectList::selectItemById(const QString &clipId) {
245 ProjectItem *item = getItemById(clipId);
246 if (item) listView->setCurrentItem(item);
250 void ProjectList::slotDeleteClip(const QString &clipId) {
251 ProjectItem *item = getItemById(clipId);
252 QTreeWidgetItem *p = item->parent();
254 kDebug() << "/////// DELETEED CLIP HAS A PARENT... " << p->indexOfChild(item);
255 QTreeWidgetItem *clone = p->takeChild(p->indexOfChild(item));
261 void ProjectList::slotAddFolder() {
263 // QString folderName = KInputDialog::getText(i18n("New Folder"), i18n("Enter new folder name: "));
264 // if (folderName.isEmpty()) return;
265 m_doc->slotAddFolder(i18n("Folder")); //folderName);
268 void ProjectList::slotAddFolder(const QString foldername, const QString &clipId, bool remove, bool edit) {
271 QTreeWidgetItemIterator it(listView);
273 item = static_cast <ProjectItem *>(*it);
274 if (item->isGroup() && item->clipId() == clipId) {
282 disconnect(listView, SIGNAL(itemChanged(QTreeWidgetItem *, int)), this, SLOT(slotUpdateItemDescription(QTreeWidgetItem *, int)));
284 QTreeWidgetItemIterator it(listView);
286 item = static_cast <ProjectItem *>(*it);
287 if (item->isGroup() && item->clipId() == clipId) {
288 item->setText(1, foldername);
289 const int children = item->childCount();
290 for (int i = 0; i < children; i++) {
291 ProjectItem *child = static_cast <ProjectItem *>(item->child(i));
292 child->setProperty("groupname", foldername);
298 connect(listView, SIGNAL(itemChanged(QTreeWidgetItem *, int)), this, SLOT(slotUpdateItemDescription(QTreeWidgetItem *, int)));
301 text << QString() << foldername;
302 (void) new ProjectItem(listView, text, clipId);
307 void ProjectList::slotAddClip(DocClipBase *clip, bool getProperties) {
308 const QString parent = clip->getProperty("groupid");
309 ProjectItem *item = NULL;
310 if (!parent.isEmpty()) {
311 ProjectItem *parentitem = getItemById(parent);
314 QString groupName = clip->getProperty("groupname");
315 if (groupName.isEmpty()) groupName = i18n("Folder");
316 text << QString() << groupName;
317 listView->blockSignals(true);
318 parentitem = new ProjectItem(listView, text, parent);
319 listView->blockSignals(false);
321 if (parentitem) item = new ProjectItem(parentitem, clip);
323 if (item == NULL) item = new ProjectItem(listView, clip);
325 KUrl url = clip->fileURL();
326 if (!url.isEmpty() && KdenliveSettings::activate_nepomuk()) {
327 // if file has Nepomuk comment, use it
328 Nepomuk::Resource f(url.path());
331 annotation = f.description();
333 Nepomuk::Tag tag("test");
336 KMessageBox::sorry(this, i18n("Cannot access Desktop Search info for %1.\nDisabling Desktop Search integration.", url.path()));
337 KdenliveSettings::setActivate_nepomuk(false);
339 if (!annotation.isEmpty()) item->setText(2, annotation);
341 if (getProperties) emit getFileProperties(clip->toXML(), clip->getId());
344 void ProjectList::slotUpdateClip(const QString &id) {
345 ProjectItem *item = getItemById(id);
346 item->setData(1, UsageRole, QString::number(item->numReferences()));
349 void ProjectList::updateAllClips() {
350 QTreeWidgetItemIterator it(listView);
352 ProjectItem *item = static_cast <ProjectItem *>(*it);
353 if (!item->isGroup()) {
354 if (item->referencedClip()->producer() == NULL) {
355 DocClipBase *clip = item->referencedClip();
356 if (clip->clipType() == TEXT && !QFile::exists(clip->fileURL().path())) {
357 // regenerate text clip image if required
358 TitleWidget *dia_ui = new TitleWidget(KUrl(), QString(), m_render, this);
360 doc.setContent(clip->getProperty("xmldata"));
362 QPixmap pix = dia_ui->renderedPixmap();
363 pix.save(clip->fileURL().path());
366 emit getFileProperties(clip->toXML(), clip->getId());
368 slotRefreshClipThumbnail(item, false);
369 item->changeDuration(item->referencedClip()->producer()->get_playtime());
371 item->setData(1, UsageRole, QString::number(item->numReferences()));
372 qApp->processEvents();
378 void ProjectList::slotAddClip(QUrl givenUrl, QString group) {
379 if (!m_commandStack) kDebug() << "!!!!!!!!!!!!!!!! NO CMD STK";
381 if (givenUrl.isEmpty()) {
382 list = KFileDialog::getOpenUrls(KUrl("kfiledialog:///clipfolder"), "application/x-kdenlive application/x-flash-video 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);
383 } else list.append(givenUrl);
384 if (list.isEmpty()) return;
386 QString groupId = QString();
387 if (group.isEmpty()) {
388 ProjectItem *item = static_cast <ProjectItem*>(listView->currentItem());
389 if (item && item->clipType() != FOLDER) {
390 while (item->parent()) {
391 item = static_cast <ProjectItem*>(item->parent());
392 if (item->clipType() == FOLDER) break;
395 if (item && item->clipType() == FOLDER) {
396 group = item->groupName();
397 groupId = item->clipId();
400 m_doc->slotAddClipList(list, group, groupId);
403 void ProjectList::slotRemoveInvalidClip(const QString &id) {
404 ProjectItem *item = getItemById(id);
406 QString path = item->referencedClip()->fileURL().path();
407 if (!path.isEmpty()) KMessageBox::sorry(this, i18n("<qt>Clip <b>%1</b><br>is invalid, will be removed from project.", path));
412 m_doc->deleteProjectClip(ids);
415 void ProjectList::slotAddColorClip() {
416 if (!m_commandStack) kDebug() << "!!!!!!!!!!!!!!!! NO CMD STK";
417 QDialog *dia = new QDialog(this);
418 Ui::ColorClip_UI *dia_ui = new Ui::ColorClip_UI();
419 dia_ui->setupUi(dia);
420 dia_ui->clip_name->setText(i18n("Color Clip"));
421 dia_ui->clip_duration->setText(KdenliveSettings::color_duration());
422 if (dia->exec() == QDialog::Accepted) {
423 QString color = dia_ui->clip_color->color().name();
424 color = color.replace(0, 1, "0x") + "ff";
426 QString group = QString();
427 QString groupId = QString();
428 ProjectItem *item = static_cast <ProjectItem*>(listView->currentItem());
429 if (item && item->clipType() != FOLDER) {
430 while (item->parent()) {
431 item = static_cast <ProjectItem*>(item->parent());
432 if (item->clipType() == FOLDER) break;
435 if (item && item->clipType() == FOLDER) {
436 group = item->groupName();
437 groupId = item->clipId();
440 m_doc->slotAddColorClipFile(dia_ui->clip_name->text(), color, dia_ui->clip_duration->text(), group, groupId);
447 void ProjectList::slotAddSlideshowClip() {
448 if (!m_commandStack) kDebug() << "!!!!!!!!!!!!!!!! NO CMD STK";
449 SlideshowClip *dia = new SlideshowClip(this);
451 if (dia->exec() == QDialog::Accepted) {
453 QString group = QString();
454 QString groupId = QString();
455 ProjectItem *item = static_cast <ProjectItem*>(listView->currentItem());
456 if (item && item->clipType() != FOLDER) {
457 while (item->parent()) {
458 item = static_cast <ProjectItem*>(item->parent());
459 if (item->clipType() == FOLDER) break;
462 if (item && item->clipType() == FOLDER) {
463 group = item->groupName();
464 groupId = item->clipId();
467 m_doc->slotAddSlideshowClipFile(dia->clipName(), dia->selectedPath(), dia->imageCount(), dia->clipDuration(), dia->loop(), dia->fade(), dia->lumaDuration(), dia->lumaFile(), dia->softness(), group, groupId);
472 void ProjectList::slotAddTitleClip() {
473 QString group = QString();
474 QString groupId = QString();
475 ProjectItem *item = static_cast <ProjectItem*>(listView->currentItem());
476 if (item && item->clipType() != FOLDER) {
477 while (item->parent()) {
478 item = static_cast <ProjectItem*>(item->parent());
479 if (item->clipType() == FOLDER) break;
482 if (item && item->clipType() == FOLDER) {
483 group = item->groupName();
484 groupId = item->clipId();
487 m_doc->slotCreateTextClip(group, groupId);
490 void ProjectList::setDocument(KdenliveDoc *doc) {
492 QList <DocClipBase*> list = doc->clipManager()->documentClipList();
493 for (int i = 0; i < list.count(); i++) {
494 slotAddClip(list.at(i), false);
498 m_timecode = doc->timecode();
499 m_commandStack = doc->commandStack();
501 QTreeWidgetItem *first = listView->topLevelItem(0);
502 if (first) listView->setCurrentItem(first);
503 m_toolbar->setEnabled(true);
506 QDomElement ProjectList::producersList() {
508 QDomElement prods = doc.createElement("producerlist");
509 doc.appendChild(prods);
510 kDebug() << "//////////// PRO LIST BUILD PRDSLIST ";
511 QTreeWidgetItemIterator it(listView);
513 if (!((ProjectItem *)(*it))->isGroup())
514 prods.appendChild(doc.importNode(((ProjectItem *)(*it))->toXml(), true));
520 void ProjectList::slotRefreshClipThumbnail(const QString &clipId) {
521 ProjectItem *item = getItemById(clipId);
522 if (item) slotRefreshClipThumbnail(item);
525 void ProjectList::slotRefreshClipThumbnail(ProjectItem *item, bool update) {
528 int width = (int)(height * m_render->dar());
529 QPixmap pix = item->referencedClip()->thumbProducer()->extractImage(item->referencedClip()->getClipThumbFrame(), width, height);
530 item->setIcon(0, pix);
531 if (update) emit projectModified();
535 void ProjectList::slotReplyGetFileProperties(const QString &clipId, Mlt::Producer *producer, const QMap < QString, QString > &properties, const QMap < QString, QString > &metadata) {
536 ProjectItem *item = getItemById(clipId);
538 item->setProperties(properties, metadata);
539 item->referencedClip()->setProducer(producer);
540 emit receivedClipDuration(clipId, item->clipMaxDuration());
541 } else kDebug() << "//////// COULD NOT FIND CLIP TO UPDATE PRPS...";
544 void ProjectList::slotReplyGetImage(const QString &clipId, int pos, const QPixmap &pix, int w, int h) {
545 ProjectItem *item = getItemById(clipId);
546 if (item) item->setIcon(0, pix);
549 ProjectItem *ProjectList::getItemById(const QString &id) {
550 QTreeWidgetItemIterator it(listView);
552 if (((ProjectItem *)(*it))->clipId() == id)
556 if (*it) return ((ProjectItem *)(*it));
560 void ProjectList::slotSelectClip(const QString &ix) {
561 ProjectItem *p = getItemById(ix);
563 listView->setCurrentItem(p);
564 listView->scrollToItem(p);
565 m_editAction->setEnabled(true);
566 m_deleteAction->setEnabled(true);
570 #include "projectlist.moc"