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("document-new"), i18n("Add Clip"));
75 connect(addClipButton, SIGNAL(triggered()), this, SLOT(slotAddClip()));
77 QAction *addColorClip = addMenu->addAction(KIcon("document-new"), i18n("Add Color Clip"));
78 connect(addColorClip, SIGNAL(triggered()), this, SLOT(slotAddColorClip()));
80 QAction *addSlideClip = addMenu->addAction(KIcon("document-new"), i18n("Add Slideshow Clip"));
81 connect(addSlideClip, SIGNAL(triggered()), this, SLOT(slotAddSlideshowClip()));
83 QAction *addTitleClip = addMenu->addAction(KIcon("document-new"), 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(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->toXml());
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 if (item == m_selectedItem) {
146 // if user clicked on the active clip item, just focus monitor, don't update it.
147 emit clipSelected(QDomElement());
150 m_selectedItem = item;
151 emit clipSelected(item->toXml());
155 void ProjectList::slotUpdateClipProperties(int id, QMap <QString, QString> properties) {
156 ProjectItem *item = getItemById(id);
158 slotUpdateClipProperties(item, properties);
159 if (properties.contains("colour") || properties.contains("resource")) slotRefreshClipThumbnail(item);
160 if (properties.contains("out")) item->changeDuration(properties.value("out").toInt());
164 void ProjectList::slotUpdateClipProperties(ProjectItem *clip, QMap <QString, QString> properties) {
166 clip->setProperties(properties);
167 if (properties.contains("description")) {
168 CLIPTYPE type = clip->clipType();
169 clip->setText(2, properties.value("description"));
170 if (type == AUDIO || type == VIDEO || type == AV || type == IMAGE || type == PLAYLIST) {
171 // Use Nepomuk system to store clip description
172 Nepomuk::Resource f(clip->clipUrl().path());
173 if (f.isValid()) f.setDescription(properties.value("description"));
178 void ProjectList::slotItemEdited(QTreeWidgetItem *item, int column) {
179 ProjectItem *clip = static_cast <ProjectItem*>(item);
181 QMap <QString, QString> props;
182 props["description"] = item->text(2);
183 slotUpdateClipProperties(clip, props);
184 } else if (column == 1 && clip->clipType() == FOLDER) {
185 m_doc->slotEditFolder(item->text(1), clip->groupName(), clip->clipId());
189 void ProjectList::slotContextMenu(const QPoint &pos, QTreeWidgetItem *item) {
194 m_editAction->setEnabled(enable);
195 m_deleteAction->setEnabled(enable);
200 void ProjectList::slotRemoveClip() {
201 if (!listView->currentItem()) return;
202 ProjectItem *item = static_cast <ProjectItem *>(listView->currentItem());
204 QMap <QString, int> folderids;
205 if (item->clipType() == FOLDER) folderids[item->groupName()] = item->clipId();
206 else ids << item->clipId();
207 if (item->numReferences() > 0) {
208 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;
209 } else if (item->clipType() == FOLDER && item->childCount() > 0) {
210 int children = item->childCount();
211 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;
212 for (int i = 0; i < children; ++i) {
213 ProjectItem *child = static_cast <ProjectItem *>(item->child(i));
214 ids << child->clipId();
217 if (!ids.isEmpty()) m_doc->deleteProjectClip(ids);
218 if (!folderids.isEmpty()) m_doc->deleteProjectFolder(folderids);
221 void ProjectList::selectItemById(const int clipId) {
222 ProjectItem *item = getItemById(clipId);
223 if (item) listView->setCurrentItem(item);
227 void ProjectList::slotDeleteClip(int clipId) {
228 ProjectItem *item = getItemById(clipId);
229 QTreeWidgetItem *p = item->parent();
231 kDebug() << "/////// DELETEED CLIP HAS A PARENT... " << p->indexOfChild(item);
232 QTreeWidgetItem *clone = p->takeChild(p->indexOfChild(item));
238 void ProjectList::slotAddFolder() {
240 // QString folderName = KInputDialog::getText(i18n("New Folder"), i18n("Enter new folder name: "));
241 // if (folderName.isEmpty()) return;
242 m_doc->slotAddFolder(i18n("Folder")); //folderName);
245 void ProjectList::slotAddFolder(const QString foldername, int clipId, bool remove, bool edit) {
248 QTreeWidgetItemIterator it(listView);
250 item = static_cast <ProjectItem *>(*it);
251 if (item->clipType() == FOLDER && item->clipId() == clipId) {
259 disconnect(listView, SIGNAL(itemChanged(QTreeWidgetItem *, int)), this, SLOT(slotUpdateItemDescription(QTreeWidgetItem *, int)));
261 QTreeWidgetItemIterator it(listView);
263 item = static_cast <ProjectItem *>(*it);
264 if (item->clipType() == FOLDER && item->clipId() == clipId) {
265 item->setText(1, foldername);
270 connect(listView, SIGNAL(itemChanged(QTreeWidgetItem *, int)), this, SLOT(slotUpdateItemDescription(QTreeWidgetItem *, int)));
273 text << QString() << foldername;
274 (void) new ProjectItem(listView, text, clipId);
279 void ProjectList::slotAddClip(DocClipBase *clip) {
280 const int parent = clip->toXML().attribute("groupid").toInt();
281 ProjectItem *item = NULL;
283 ProjectItem *parentitem = getItemById(parent);
284 if (parentitem) item = new ProjectItem(parentitem, clip);
286 if (item == NULL) item = new ProjectItem(listView, clip);
288 KUrl url = clip->fileURL();
289 if (!url.isEmpty()) {
290 // if file has Nepomuk comment, use it
291 Nepomuk::Resource f(url.path());
294 annotation = f.description();
296 Nepomuk::Tag tag("test");
298 } else kDebug() << "--- CANNOT CONTACT NEPOMUK";
299 if (!annotation.isEmpty()) item->setText(2, annotation);
301 emit getFileProperties(clip->toXML(), clip->getId());
304 void ProjectList::slotUpdateClip(int id) {
305 ProjectItem *item = getItemById(id);
306 item->setData(1, UsageRole, QString::number(item->numReferences()));
309 void ProjectList::slotAddClip(QUrl givenUrl, QString group) {
310 if (!m_commandStack) kDebug() << "!!!!!!!!!!!!!!!! NO CMD STK";
312 if (givenUrl.isEmpty())
313 list = KFileDialog::getOpenUrls(KUrl(), "application/vnd.kde.kdenlive application/vnd.westley.scenelist application/flv application/vnd.rn-realmedia video/x-dv video/x-msvideo video/mpeg video/x-ms-wmv audio/mpeg audio/x-mp3 audio/x-wav application/ogg *.m2t *.dv 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");
314 else list.append(givenUrl);
315 if (list.isEmpty()) return;
316 KUrl::List::Iterator it;
318 if (group.isEmpty()) {
319 ProjectItem *item = static_cast <ProjectItem*>(listView->currentItem());
320 if (item && item->clipType() != FOLDER) {
321 while (item->parent()) {
322 item = static_cast <ProjectItem*>(item->parent());
323 if (item->clipType() == FOLDER) break;
326 if (item && item->clipType() == FOLDER) {
327 group = item->groupName();
328 groupId = item->clipId();
331 for (it = list.begin(); it != list.end(); it++) {
332 m_doc->slotAddClipFile(*it, group, groupId);
336 void ProjectList::slotAddColorClip() {
337 if (!m_commandStack) kDebug() << "!!!!!!!!!!!!!!!! NO CMD STK";
338 QDialog *dia = new QDialog(this);
339 Ui::ColorClip_UI *dia_ui = new Ui::ColorClip_UI();
340 dia_ui->setupUi(dia);
341 dia_ui->clip_name->setText(i18n("Color Clip"));
342 dia_ui->clip_duration->setText(KdenliveSettings::color_duration());
343 if (dia->exec() == QDialog::Accepted) {
344 QString color = dia_ui->clip_color->color().name();
345 color = color.replace(0, 1, "0x") + "ff";
347 QString group = QString();
349 ProjectItem *item = static_cast <ProjectItem*>(listView->currentItem());
350 if (item && item->clipType() != FOLDER) {
351 while (item->parent()) {
352 item = static_cast <ProjectItem*>(item->parent());
353 if (item->clipType() == FOLDER) break;
356 if (item && item->clipType() == FOLDER) {
357 group = item->groupName();
358 groupId = item->clipId();
361 m_doc->slotAddColorClipFile(dia_ui->clip_name->text(), color, dia_ui->clip_duration->text(), group, groupId);
368 void ProjectList::slotAddSlideshowClip() {
369 if (!m_commandStack) kDebug() << "!!!!!!!!!!!!!!!! NO CMD STK";
370 SlideshowClip *dia = new SlideshowClip(this);
372 if (dia->exec() == QDialog::Accepted) {
374 QString group = QString();
376 ProjectItem *item = static_cast <ProjectItem*>(listView->currentItem());
377 if (item && item->clipType() != FOLDER) {
378 while (item->parent()) {
379 item = static_cast <ProjectItem*>(item->parent());
380 if (item->clipType() == FOLDER) break;
383 if (item && item->clipType() == FOLDER) {
384 group = item->groupName();
385 groupId = item->clipId();
388 m_doc->slotAddSlideshowClipFile(dia->clipName(), dia->selectedPath(), dia->imageCount(), dia->clipDuration(), dia->loop(), group, groupId);
392 void ProjectList::slotAddTitleClip() {
393 QString group = QString();
395 ProjectItem *item = static_cast <ProjectItem*>(listView->currentItem());
396 if (item && item->clipType() != FOLDER) {
397 while (item->parent()) {
398 item = static_cast <ProjectItem*>(item->parent());
399 if (item->clipType() == FOLDER) break;
402 if (item && item->clipType() == FOLDER) {
403 group = item->groupName();
404 groupId = item->clipId();
407 m_doc->slotCreateTextClip(group, groupId);
410 void ProjectList::setDocument(KdenliveDoc *doc) {
412 QList <DocClipBase*> list = doc->clipManager()->documentClipList();
413 for (int i = 0; i < list.count(); i++) {
414 slotAddClip(list.at(i));
418 m_timecode = doc->timecode();
419 m_commandStack = doc->commandStack();
421 QTreeWidgetItem *first = listView->topLevelItem(0);
422 if (first) listView->setCurrentItem(first);
423 m_toolbar->setEnabled(true);
426 QDomElement ProjectList::producersList() {
428 QDomElement prods = doc.createElement("producerlist");
429 doc.appendChild(prods);
430 kDebug() << "//////////// PRO LIST BUILD PRDSLIST ";
431 QTreeWidgetItemIterator it(listView);
433 if (!((ProjectItem *)(*it))->isGroup())
434 prods.appendChild(doc.importNode(((ProjectItem *)(*it))->toXml(), true));
440 void ProjectList::slotRefreshClipThumbnail(int clipId) {
441 ProjectItem *item = getItemById(clipId);
442 if (item) slotRefreshClipThumbnail(item);
445 void ProjectList::slotRefreshClipThumbnail(ProjectItem *item) {
448 int width = (int)(height * m_render->dar());
449 QPixmap pix = KThumb::getImage(item->toXml(), item->referencedClip()->getProjectThumbFrame(), width, height);
450 //QPixmap pix = KThumb::getFrame(item->toXml()), 0, width, height);
451 item->setIcon(0, pix);
455 void ProjectList::slotReplyGetFileProperties(int clipId, const QMap < QString, QString > &properties, const QMap < QString, QString > &metadata) {
456 ProjectItem *item = getItemById(clipId);
458 item->setProperties(properties, metadata);
459 listView->setCurrentItem(item);
460 emit receivedClipDuration(clipId, item->clipMaxDuration());
464 void ProjectList::slotReplyGetImage(int clipId, int pos, const QPixmap &pix, int w, int h) {
465 ProjectItem *item = getItemById(clipId);
466 if (item) item->setIcon(0, pix);
469 ProjectItem *ProjectList::getItemById(int id) {
470 QTreeWidgetItemIterator it(listView);
472 if (((ProjectItem *)(*it))->clipId() == id)
476 if (*it) return ((ProjectItem *)(*it));
480 #include "projectlist.moc"