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"
46 #include "definitions.h"
47 #include "clipmanager.h"
48 #include "docclipbase.h"
49 #include "kdenlivedoc.h"
52 #include "projectlistview.h"
54 ProjectList::ProjectList(QWidget *parent)
55 : QWidget(parent), m_render(NULL), m_fps(-1), m_commandStack(NULL), m_selectedItem(NULL) {
57 QWidget *vbox = new QWidget;
58 listView = new ProjectListView(this);;
59 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 void ProjectList::slotEditClip() {
131 ProjectItem *item = static_cast <ProjectItem*>(listView->currentItem());
132 if (item && !item->isGroup()) emit clipSelected(item->referencedClip());
133 emit showClipProperties(item->referencedClip());
138 void ProjectList::setRenderer(Render *projectRender) {
139 m_render = projectRender;
142 void ProjectList::slotClipSelected() {
143 ProjectItem *item = static_cast <ProjectItem*>(listView->currentItem());
144 if (item && !item->isGroup()) {
145 m_selectedItem = item;
146 emit clipSelected(item->referencedClip());
150 void ProjectList::slotUpdateClipProperties(const QString &id, QMap <QString, QString> properties) {
151 ProjectItem *item = getItemById(id);
153 slotUpdateClipProperties(item, properties);
154 if (properties.contains("colour") || properties.contains("resource")) slotRefreshClipThumbnail(item);
155 if (properties.contains("out")) item->changeDuration(properties.value("out").toInt());
159 void ProjectList::slotUpdateClipProperties(ProjectItem *clip, QMap <QString, QString> properties) {
161 clip->setProperties(properties);
162 if (properties.contains("description")) {
163 CLIPTYPE type = clip->clipType();
164 clip->setText(2, properties.value("description"));
165 if (type == AUDIO || type == VIDEO || type == AV || type == IMAGE || type == PLAYLIST) {
166 // Use Nepomuk system to store clip description
167 Nepomuk::Resource f(clip->clipUrl().path());
168 if (f.isValid()) f.setDescription(properties.value("description"));
173 void ProjectList::slotItemEdited(QTreeWidgetItem *item, int column) {
174 ProjectItem *clip = static_cast <ProjectItem*>(item);
176 QMap <QString, QString> props;
177 props["description"] = item->text(2);
178 slotUpdateClipProperties(clip, props);
179 } else if (column == 1 && clip->clipType() == FOLDER) {
180 m_doc->slotEditFolder(item->text(1), clip->groupName(), clip->clipId());
184 void ProjectList::slotContextMenu(const QPoint &pos, QTreeWidgetItem *item) {
189 m_editAction->setEnabled(enable);
190 m_deleteAction->setEnabled(enable);
195 void ProjectList::slotRemoveClip() {
196 if (!listView->currentItem()) return;
197 ProjectItem *item = static_cast <ProjectItem *>(listView->currentItem());
199 QMap <QString, QString> folderids;
200 if (item->clipType() == FOLDER) folderids[item->groupName()] = item->clipId();
201 else ids << item->clipId();
202 if (item->numReferences() > 0) {
203 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;
204 } else if (item->clipType() == FOLDER && item->childCount() > 0) {
205 int children = item->childCount();
206 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;
207 for (int i = 0; i < children; ++i) {
208 ProjectItem *child = static_cast <ProjectItem *>(item->child(i));
209 ids << child->clipId();
212 if (!ids.isEmpty()) m_doc->deleteProjectClip(ids);
213 if (!folderids.isEmpty()) m_doc->deleteProjectFolder(folderids);
216 void ProjectList::selectItemById(const QString &clipId) {
217 ProjectItem *item = getItemById(clipId);
218 if (item) listView->setCurrentItem(item);
222 void ProjectList::slotDeleteClip(const QString &clipId) {
223 ProjectItem *item = getItemById(clipId);
224 QTreeWidgetItem *p = item->parent();
226 kDebug() << "/////// DELETEED CLIP HAS A PARENT... " << p->indexOfChild(item);
227 QTreeWidgetItem *clone = p->takeChild(p->indexOfChild(item));
233 void ProjectList::slotAddFolder() {
235 // QString folderName = KInputDialog::getText(i18n("New Folder"), i18n("Enter new folder name: "));
236 // if (folderName.isEmpty()) return;
237 m_doc->slotAddFolder(i18n("Folder")); //folderName);
240 void ProjectList::slotAddFolder(const QString foldername, const QString &clipId, bool remove, bool edit) {
243 QTreeWidgetItemIterator it(listView);
245 item = static_cast <ProjectItem *>(*it);
246 if (item->clipType() == FOLDER && item->clipId() == clipId) {
254 disconnect(listView, SIGNAL(itemChanged(QTreeWidgetItem *, int)), this, SLOT(slotUpdateItemDescription(QTreeWidgetItem *, int)));
256 QTreeWidgetItemIterator it(listView);
258 item = static_cast <ProjectItem *>(*it);
259 if (item->clipType() == FOLDER && item->clipId() == clipId) {
260 item->setText(1, foldername);
265 connect(listView, SIGNAL(itemChanged(QTreeWidgetItem *, int)), this, SLOT(slotUpdateItemDescription(QTreeWidgetItem *, int)));
268 text << QString() << foldername;
269 (void) new ProjectItem(listView, text, clipId);
274 void ProjectList::slotAddClip(DocClipBase *clip) {
275 const QString parent = clip->toXML().attribute("groupid");
276 ProjectItem *item = NULL;
278 ProjectItem *parentitem = getItemById(parent);
279 if (parentitem) item = new ProjectItem(parentitem, clip);
281 if (item == NULL) item = new ProjectItem(listView, clip);
283 KUrl url = clip->fileURL();
284 if (!url.isEmpty()) {
285 // if file has Nepomuk comment, use it
286 Nepomuk::Resource f(url.path());
289 annotation = f.description();
291 Nepomuk::Tag tag("test");
293 } else kDebug() << "--- CANNOT CONTACT NEPOMUK";
294 if (!annotation.isEmpty()) item->setText(2, annotation);
296 emit getFileProperties(clip->toXML(), clip->getId());
299 void ProjectList::slotUpdateClip(const QString &id) {
300 ProjectItem *item = getItemById(id);
301 item->setData(1, UsageRole, QString::number(item->numReferences()));
304 void ProjectList::slotAddClip(QUrl givenUrl, QString group) {
305 if (!m_commandStack) kDebug() << "!!!!!!!!!!!!!!!! NO CMD STK";
307 if (givenUrl.isEmpty()) {
308 list = KFileDialog::getOpenUrls(KUrl("kfiledialog:///clipfolder"), "application/vnd.kde.kdenlive application/vnd.westley.scenelist application/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", this);
309 } else list.append(givenUrl);
310 if (list.isEmpty()) return;
312 QString groupId = QString();
313 if (group.isEmpty()) {
314 ProjectItem *item = static_cast <ProjectItem*>(listView->currentItem());
315 if (item && item->clipType() != FOLDER) {
316 while (item->parent()) {
317 item = static_cast <ProjectItem*>(item->parent());
318 if (item->clipType() == FOLDER) break;
321 if (item && item->clipType() == FOLDER) {
322 group = item->groupName();
323 groupId = item->clipId();
326 m_doc->slotAddClipList(list, group, groupId);
329 void ProjectList::slotRemoveInvalidClip(const QString &id) {
330 ProjectItem *item = getItemById(id);
332 QString path = item->referencedClip()->fileURL().path();
333 if (!path.isEmpty()) KMessageBox::sorry(this, i18n("<qt>Clip <b>%1</b><br>is invalid, will be removed from project.", path));
338 m_doc->deleteProjectClip(ids);
341 void ProjectList::slotAddColorClip() {
342 if (!m_commandStack) kDebug() << "!!!!!!!!!!!!!!!! NO CMD STK";
343 QDialog *dia = new QDialog(this);
344 Ui::ColorClip_UI *dia_ui = new Ui::ColorClip_UI();
345 dia_ui->setupUi(dia);
346 dia_ui->clip_name->setText(i18n("Color Clip"));
347 dia_ui->clip_duration->setText(KdenliveSettings::color_duration());
348 if (dia->exec() == QDialog::Accepted) {
349 QString color = dia_ui->clip_color->color().name();
350 color = color.replace(0, 1, "0x") + "ff";
352 QString group = QString();
353 QString groupId = QString();
354 ProjectItem *item = static_cast <ProjectItem*>(listView->currentItem());
355 if (item && item->clipType() != FOLDER) {
356 while (item->parent()) {
357 item = static_cast <ProjectItem*>(item->parent());
358 if (item->clipType() == FOLDER) break;
361 if (item && item->clipType() == FOLDER) {
362 group = item->groupName();
363 groupId = item->clipId();
366 m_doc->slotAddColorClipFile(dia_ui->clip_name->text(), color, dia_ui->clip_duration->text(), group, groupId);
373 void ProjectList::slotAddSlideshowClip() {
374 if (!m_commandStack) kDebug() << "!!!!!!!!!!!!!!!! NO CMD STK";
375 SlideshowClip *dia = new SlideshowClip(this);
377 if (dia->exec() == QDialog::Accepted) {
379 QString group = QString();
380 QString groupId = QString();
381 ProjectItem *item = static_cast <ProjectItem*>(listView->currentItem());
382 if (item && item->clipType() != FOLDER) {
383 while (item->parent()) {
384 item = static_cast <ProjectItem*>(item->parent());
385 if (item->clipType() == FOLDER) break;
388 if (item && item->clipType() == FOLDER) {
389 group = item->groupName();
390 groupId = item->clipId();
393 m_doc->slotAddSlideshowClipFile(dia->clipName(), dia->selectedPath(), dia->imageCount(), dia->clipDuration(), dia->loop(), dia->fade(), dia->lumaDuration(), dia->lumaFile(), dia->softness(), group, groupId);
398 void ProjectList::slotAddTitleClip() {
399 QString group = QString();
400 QString groupId = QString();
401 ProjectItem *item = static_cast <ProjectItem*>(listView->currentItem());
402 if (item && item->clipType() != FOLDER) {
403 while (item->parent()) {
404 item = static_cast <ProjectItem*>(item->parent());
405 if (item->clipType() == FOLDER) break;
408 if (item && item->clipType() == FOLDER) {
409 group = item->groupName();
410 groupId = item->clipId();
413 m_doc->slotCreateTextClip(group, groupId);
416 void ProjectList::setDocument(KdenliveDoc *doc) {
418 QList <DocClipBase*> list = doc->clipManager()->documentClipList();
419 for (int i = 0; i < list.count(); i++) {
420 slotAddClip(list.at(i));
424 m_timecode = doc->timecode();
425 m_commandStack = doc->commandStack();
427 QTreeWidgetItem *first = listView->topLevelItem(0);
428 if (first) listView->setCurrentItem(first);
429 m_toolbar->setEnabled(true);
432 QDomElement ProjectList::producersList() {
434 QDomElement prods = doc.createElement("producerlist");
435 doc.appendChild(prods);
436 kDebug() << "//////////// PRO LIST BUILD PRDSLIST ";
437 QTreeWidgetItemIterator it(listView);
439 if (!((ProjectItem *)(*it))->isGroup())
440 prods.appendChild(doc.importNode(((ProjectItem *)(*it))->toXml(), true));
446 void ProjectList::slotRefreshClipThumbnail(const QString &clipId) {
447 ProjectItem *item = getItemById(clipId);
448 if (item) slotRefreshClipThumbnail(item);
451 void ProjectList::slotRefreshClipThumbnail(ProjectItem *item) {
454 int width = (int)(height * m_render->dar());
455 QPixmap pix = item->referencedClip()->thumbProducer()->extractImage(item->referencedClip()->getClipThumbFrame(), width, height);
456 item->setIcon(0, pix);
460 void ProjectList::slotReplyGetFileProperties(const QString &clipId, Mlt::Producer *producer, const QMap < QString, QString > &properties, const QMap < QString, QString > &metadata) {
461 ProjectItem *item = getItemById(clipId);
463 item->setProperties(properties, metadata);
464 item->referencedClip()->setProducer(producer);
465 emit receivedClipDuration(clipId, item->clipMaxDuration());
466 } else kDebug() << "//////// COULD NOT FIND CLIP TO UPDATE PRPS...";
469 void ProjectList::slotReplyGetImage(const QString &clipId, int pos, const QPixmap &pix, int w, int h) {
470 ProjectItem *item = getItemById(clipId);
471 if (item) item->setIcon(0, pix);
474 ProjectItem *ProjectList::getItemById(const QString &id) {
475 QTreeWidgetItemIterator it(listView);
477 if (((ProjectItem *)(*it))->clipId() == id)
481 if (*it) return ((ProjectItem *)(*it));
485 void ProjectList::slotSelectClip(const QString &ix) {
486 ProjectItem *p = getItemById(ix);
488 listView->setCurrentItem(p);
489 listView->scrollToItem(p);
493 #include "projectlist.moc"