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 <kio/netaccess.h>
34 #include <KMessageBox>
36 #include <nepomuk/global.h>
37 #include <nepomuk/resource.h>
38 #include <nepomuk/tag.h>
40 #include "projectlist.h"
41 #include "projectitem.h"
42 #include "kdenlivesettings.h"
43 #include "slideshowclip.h"
44 #include "ui_colorclip_ui.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()) emit clipSelected(item->referencedClip());
134 emit showClipProperties(item->referencedClip());
139 void ProjectList::setRenderer(Render *projectRender) {
140 m_render = projectRender;
143 void ProjectList::slotClipSelected() {
144 ProjectItem *item = static_cast <ProjectItem*>(listView->currentItem());
145 if (item && !item->isGroup()) {
146 m_selectedItem = item;
147 emit clipSelected(item->referencedClip());
151 void ProjectList::slotUpdateClipProperties(int id, QMap <QString, QString> properties) {
152 ProjectItem *item = getItemById(id);
154 slotUpdateClipProperties(item, properties);
155 if (properties.contains("colour") || properties.contains("resource")) slotRefreshClipThumbnail(item);
156 if (properties.contains("out")) item->changeDuration(properties.value("out").toInt());
160 void ProjectList::slotUpdateClipProperties(ProjectItem *clip, QMap <QString, QString> properties) {
162 clip->setProperties(properties);
163 if (properties.contains("description")) {
164 CLIPTYPE type = clip->clipType();
165 clip->setText(2, properties.value("description"));
166 if (type == AUDIO || type == VIDEO || type == AV || type == IMAGE || type == PLAYLIST) {
167 // Use Nepomuk system to store clip description
168 Nepomuk::Resource f(clip->clipUrl().path());
169 if (f.isValid()) f.setDescription(properties.value("description"));
174 void ProjectList::slotItemEdited(QTreeWidgetItem *item, int column) {
175 ProjectItem *clip = static_cast <ProjectItem*>(item);
177 QMap <QString, QString> props;
178 props["description"] = item->text(2);
179 slotUpdateClipProperties(clip, props);
180 } else if (column == 1 && clip->clipType() == FOLDER) {
181 m_doc->slotEditFolder(item->text(1), clip->groupName(), clip->clipId());
185 void ProjectList::slotContextMenu(const QPoint &pos, QTreeWidgetItem *item) {
190 m_editAction->setEnabled(enable);
191 m_deleteAction->setEnabled(enable);
196 void ProjectList::slotRemoveClip() {
197 if (!listView->currentItem()) return;
198 ProjectItem *item = static_cast <ProjectItem *>(listView->currentItem());
200 QMap <QString, int> folderids;
201 if (item->clipType() == FOLDER) folderids[item->groupName()] = item->clipId();
202 else ids << item->clipId();
203 if (item->numReferences() > 0) {
204 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;
205 } else if (item->clipType() == FOLDER && item->childCount() > 0) {
206 int children = item->childCount();
207 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;
208 for (int i = 0; i < children; ++i) {
209 ProjectItem *child = static_cast <ProjectItem *>(item->child(i));
210 ids << child->clipId();
213 if (!ids.isEmpty()) m_doc->deleteProjectClip(ids);
214 if (!folderids.isEmpty()) m_doc->deleteProjectFolder(folderids);
217 void ProjectList::selectItemById(const int clipId) {
218 ProjectItem *item = getItemById(clipId);
219 if (item) listView->setCurrentItem(item);
223 void ProjectList::slotDeleteClip(int clipId) {
224 ProjectItem *item = getItemById(clipId);
225 QTreeWidgetItem *p = item->parent();
227 kDebug() << "/////// DELETEED CLIP HAS A PARENT... " << p->indexOfChild(item);
228 QTreeWidgetItem *clone = p->takeChild(p->indexOfChild(item));
234 void ProjectList::slotAddFolder() {
236 // QString folderName = KInputDialog::getText(i18n("New Folder"), i18n("Enter new folder name: "));
237 // if (folderName.isEmpty()) return;
238 m_doc->slotAddFolder(i18n("Folder")); //folderName);
241 void ProjectList::slotAddFolder(const QString foldername, int clipId, bool remove, bool edit) {
244 QTreeWidgetItemIterator it(listView);
246 item = static_cast <ProjectItem *>(*it);
247 if (item->clipType() == FOLDER && item->clipId() == clipId) {
255 disconnect(listView, SIGNAL(itemChanged(QTreeWidgetItem *, int)), this, SLOT(slotUpdateItemDescription(QTreeWidgetItem *, int)));
257 QTreeWidgetItemIterator it(listView);
259 item = static_cast <ProjectItem *>(*it);
260 if (item->clipType() == FOLDER && item->clipId() == clipId) {
261 item->setText(1, foldername);
266 connect(listView, SIGNAL(itemChanged(QTreeWidgetItem *, int)), this, SLOT(slotUpdateItemDescription(QTreeWidgetItem *, int)));
269 text << QString() << foldername;
270 (void) new ProjectItem(listView, text, clipId);
275 void ProjectList::slotAddClip(DocClipBase *clip) {
276 const int parent = clip->toXML().attribute("groupid").toInt();
277 ProjectItem *item = NULL;
279 ProjectItem *parentitem = getItemById(parent);
280 if (parentitem) item = new ProjectItem(parentitem, clip);
282 if (item == NULL) item = new ProjectItem(listView, clip);
284 KUrl url = clip->fileURL();
285 if (!url.isEmpty()) {
286 // if file has Nepomuk comment, use it
287 Nepomuk::Resource f(url.path());
290 annotation = f.description();
292 Nepomuk::Tag tag("test");
294 } else kDebug() << "--- CANNOT CONTACT NEPOMUK";
295 if (!annotation.isEmpty()) item->setText(2, annotation);
297 emit getFileProperties(clip->toXML(), clip->getId());
300 void ProjectList::slotUpdateClip(int id) {
301 ProjectItem *item = getItemById(id);
302 item->setData(1, UsageRole, QString::number(item->numReferences()));
305 void ProjectList::slotAddClip(QUrl givenUrl, QString group) {
306 if (!m_commandStack) kDebug() << "!!!!!!!!!!!!!!!! NO CMD STK";
308 if (givenUrl.isEmpty()) {
309 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", this);
310 } else list.append(givenUrl);
311 if (list.isEmpty()) return;
314 if (group.isEmpty()) {
315 ProjectItem *item = static_cast <ProjectItem*>(listView->currentItem());
316 if (item && item->clipType() != FOLDER) {
317 while (item->parent()) {
318 item = static_cast <ProjectItem*>(item->parent());
319 if (item->clipType() == FOLDER) break;
322 if (item && item->clipType() == FOLDER) {
323 group = item->groupName();
324 groupId = item->clipId();
327 foreach(const KUrl file, list) {
328 if (KIO::NetAccess::exists(file, KIO::NetAccess::SourceSide, NULL))
329 m_doc->slotAddClipFile(file, group, groupId);
333 void ProjectList::slotRemoveInvalidClip(int id) {
334 ProjectItem *item = getItemById(id);
336 QString path = item->referencedClip()->fileURL().path();
337 if (!path.isEmpty()) KMessageBox::sorry(this, i18n("<qt>Clip <b>%1</b><br>is invalid, will be removed from project.", path));
342 m_doc->deleteProjectClip(ids);
345 void ProjectList::slotAddColorClip() {
346 if (!m_commandStack) kDebug() << "!!!!!!!!!!!!!!!! NO CMD STK";
347 QDialog *dia = new QDialog(this);
348 Ui::ColorClip_UI *dia_ui = new Ui::ColorClip_UI();
349 dia_ui->setupUi(dia);
350 dia_ui->clip_name->setText(i18n("Color Clip"));
351 dia_ui->clip_duration->setText(KdenliveSettings::color_duration());
352 if (dia->exec() == QDialog::Accepted) {
353 QString color = dia_ui->clip_color->color().name();
354 color = color.replace(0, 1, "0x") + "ff";
356 QString group = QString();
358 ProjectItem *item = static_cast <ProjectItem*>(listView->currentItem());
359 if (item && item->clipType() != FOLDER) {
360 while (item->parent()) {
361 item = static_cast <ProjectItem*>(item->parent());
362 if (item->clipType() == FOLDER) break;
365 if (item && item->clipType() == FOLDER) {
366 group = item->groupName();
367 groupId = item->clipId();
370 m_doc->slotAddColorClipFile(dia_ui->clip_name->text(), color, dia_ui->clip_duration->text(), group, groupId);
377 void ProjectList::slotAddSlideshowClip() {
378 if (!m_commandStack) kDebug() << "!!!!!!!!!!!!!!!! NO CMD STK";
379 SlideshowClip *dia = new SlideshowClip(this);
381 if (dia->exec() == QDialog::Accepted) {
383 QString group = QString();
385 ProjectItem *item = static_cast <ProjectItem*>(listView->currentItem());
386 if (item && item->clipType() != FOLDER) {
387 while (item->parent()) {
388 item = static_cast <ProjectItem*>(item->parent());
389 if (item->clipType() == FOLDER) break;
392 if (item && item->clipType() == FOLDER) {
393 group = item->groupName();
394 groupId = item->clipId();
397 m_doc->slotAddSlideshowClipFile(dia->clipName(), dia->selectedPath(), dia->imageCount(), dia->clipDuration(), dia->loop(), dia->fade(), dia->lumaDuration(), dia->lumaFile(), dia->softness(), group, groupId);
402 void ProjectList::slotAddTitleClip() {
403 QString group = QString();
405 ProjectItem *item = static_cast <ProjectItem*>(listView->currentItem());
406 if (item && item->clipType() != FOLDER) {
407 while (item->parent()) {
408 item = static_cast <ProjectItem*>(item->parent());
409 if (item->clipType() == FOLDER) break;
412 if (item && item->clipType() == FOLDER) {
413 group = item->groupName();
414 groupId = item->clipId();
417 m_doc->slotCreateTextClip(group, groupId);
420 void ProjectList::setDocument(KdenliveDoc *doc) {
422 QList <DocClipBase*> list = doc->clipManager()->documentClipList();
423 for (int i = 0; i < list.count(); i++) {
424 slotAddClip(list.at(i));
428 m_timecode = doc->timecode();
429 m_commandStack = doc->commandStack();
431 QTreeWidgetItem *first = listView->topLevelItem(0);
432 if (first) listView->setCurrentItem(first);
433 m_toolbar->setEnabled(true);
436 QDomElement ProjectList::producersList() {
438 QDomElement prods = doc.createElement("producerlist");
439 doc.appendChild(prods);
440 kDebug() << "//////////// PRO LIST BUILD PRDSLIST ";
441 QTreeWidgetItemIterator it(listView);
443 if (!((ProjectItem *)(*it))->isGroup())
444 prods.appendChild(doc.importNode(((ProjectItem *)(*it))->toXml(), true));
450 void ProjectList::slotRefreshClipThumbnail(int clipId) {
451 ProjectItem *item = getItemById(clipId);
452 if (item) slotRefreshClipThumbnail(item);
455 void ProjectList::slotRefreshClipThumbnail(ProjectItem *item) {
458 int width = (int)(height * m_render->dar());
459 QPixmap pix = item->referencedClip()->thumbProducer()->extractImage(item->referencedClip()->getClipThumbFrame(), width, height);
460 item->setIcon(0, pix);
464 void ProjectList::slotReplyGetFileProperties(int clipId, Mlt::Producer *producer, const QMap < QString, QString > &properties, const QMap < QString, QString > &metadata) {
465 ProjectItem *item = getItemById(clipId);
467 item->setProperties(properties, metadata);
468 item->referencedClip()->setProducer(producer);
469 listView->setCurrentItem(item);
470 emit receivedClipDuration(clipId, item->clipMaxDuration());
471 } else kDebug() << "//////// COULD NOT FIND CLIP TO UPDATE PRPS...";
474 void ProjectList::slotReplyGetImage(int clipId, int pos, const QPixmap &pix, int w, int h) {
475 ProjectItem *item = getItemById(clipId);
476 if (item) item->setIcon(0, pix);
479 ProjectItem *ProjectList::getItemById(int id) {
480 QTreeWidgetItemIterator it(listView);
482 if (((ProjectItem *)(*it))->clipId() == id)
486 if (*it) return ((ProjectItem *)(*it));
490 #include "projectlist.moc"