]> git.sesse.net Git - kdenlive/blob - src/projectlist.cpp
* internal rework: switch clip id's from integer to string
[kdenlive] / src / projectlist.cpp
1 /***************************************************************************
2  *   Copyright (C) 2007 by Jean-Baptiste Mardelle (jb@kdenlive.org)        *
3  *                                                                         *
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.                                   *
8  *                                                                         *
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.                          *
13  *                                                                         *
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  ***************************************************************************/
19
20
21 #include <QMouseEvent>
22 #include <QStylePainter>
23 #include <QPixmap>
24 #include <QIcon>
25 #include <QDialog>
26 #include <QtGui>
27
28 #include <KDebug>
29 #include <KAction>
30 #include <KLocale>
31 #include <KFileDialog>
32 #include <KInputDialog>
33 #include <KMessageBox>
34
35 #include <nepomuk/global.h>
36 #include <nepomuk/resource.h>
37 #include <nepomuk/tag.h>
38
39 #include "projectlist.h"
40 #include "projectitem.h"
41 #include "kdenlivesettings.h"
42 #include "slideshowclip.h"
43 #include "ui_colorclip_ui.h"
44
45
46 #include "definitions.h"
47 #include "clipmanager.h"
48 #include "docclipbase.h"
49 #include "kdenlivedoc.h"
50 #include "renderer.h"
51 #include "kthumb.h"
52 #include "projectlistview.h"
53
54 ProjectList::ProjectList(QWidget *parent)
55         : QWidget(parent), m_render(NULL), m_fps(-1), m_commandStack(NULL), m_selectedItem(NULL) {
56
57     QWidget *vbox = new QWidget;
58     listView = new ProjectListView(this);;
59     QVBoxLayout *layout = new QVBoxLayout;
60     m_clipIdCounter = 0;
61
62     // setup toolbar
63     searchView = new KTreeWidgetSearchLine(this);
64     m_toolbar = new QToolBar("projectToolBar", this);
65     m_toolbar->addWidget(searchView);
66
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);
72
73     QAction *addClipButton = addMenu->addAction(KIcon("kdenlive-add-clip"), i18n("Add Clip"));
74     connect(addClipButton, SIGNAL(triggered()), this, SLOT(slotAddClip()));
75
76     QAction *addColorClip = addMenu->addAction(KIcon("kdenlive-add-color-clip"), i18n("Add Color Clip"));
77     connect(addColorClip, SIGNAL(triggered()), this, SLOT(slotAddColorClip()));
78
79     QAction *addSlideClip = addMenu->addAction(KIcon("kdenlive-add-slide-clip"), i18n("Add Slideshow Clip"));
80     connect(addSlideClip, SIGNAL(triggered()), this, SLOT(slotAddSlideshowClip()));
81
82     QAction *addTitleClip = addMenu->addAction(KIcon("kdenlive-add-text-clip"), i18n("Add Title Clip"));
83     connect(addTitleClip, SIGNAL(triggered()), this, SLOT(slotAddTitleClip()));
84
85     m_deleteAction = m_toolbar->addAction(KIcon("edit-delete"), i18n("Delete Clip"));
86     connect(m_deleteAction, SIGNAL(triggered()), this, SLOT(slotRemoveClip()));
87
88     m_editAction = m_toolbar->addAction(KIcon("document-properties"), i18n("Edit Clip"));
89     connect(m_editAction, SIGNAL(triggered()), this, SLOT(slotEditClip()));
90
91     QAction *addFolderButton = addMenu->addAction(KIcon("folder-new"), i18n("Create Folder"));
92     connect(addFolderButton, SIGNAL(triggered()), this, SLOT(slotAddFolder()));
93
94     addButton->setDefaultAction(addClipButton);
95
96     layout->addWidget(m_toolbar);
97     layout->addWidget(listView);
98     setLayout(layout);
99     //m_toolbar->setEnabled(false);
100
101     searchView->setTreeWidget(listView);
102
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);
112
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 *)));
120
121     m_listViewDelegate = new ItemDelegate(listView);
122     listView->setItemDelegate(m_listViewDelegate);
123 }
124
125 ProjectList::~ProjectList() {
126     delete m_menu;
127     delete m_toolbar;
128 }
129
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());
134 }
135
136
137
138 void ProjectList::setRenderer(Render *projectRender) {
139     m_render = projectRender;
140 }
141
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());
147     }
148 }
149
150 void ProjectList::slotUpdateClipProperties(const QString &id, QMap <QString, QString> properties) {
151     ProjectItem *item = getItemById(id);
152     if (item) {
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());
156     }
157 }
158
159 void ProjectList::slotUpdateClipProperties(ProjectItem *clip, QMap <QString, QString> properties) {
160     if (!clip) return;
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"));
169         }
170     }
171 }
172
173 void ProjectList::slotItemEdited(QTreeWidgetItem *item, int column) {
174     ProjectItem *clip = static_cast <ProjectItem*>(item);
175     if (column == 2) {
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());
181     }
182 }
183
184 void ProjectList::slotContextMenu(const QPoint &pos, QTreeWidgetItem *item) {
185     bool enable = false;
186     if (item) {
187         enable = true;
188     }
189     m_editAction->setEnabled(enable);
190     m_deleteAction->setEnabled(enable);
191
192     m_menu->popup(pos);
193 }
194
195 void ProjectList::slotRemoveClip() {
196     if (!listView->currentItem()) return;
197     ProjectItem *item = static_cast <ProjectItem *>(listView->currentItem());
198     QList <QString> ids;
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();
210         }
211     }
212     if (!ids.isEmpty()) m_doc->deleteProjectClip(ids);
213     if (!folderids.isEmpty()) m_doc->deleteProjectFolder(folderids);
214 }
215
216 void ProjectList::selectItemById(const QString &clipId) {
217     ProjectItem *item = getItemById(clipId);
218     if (item) listView->setCurrentItem(item);
219 }
220
221
222 void ProjectList::slotDeleteClip(const QString &clipId) {
223     ProjectItem *item = getItemById(clipId);
224     QTreeWidgetItem *p = item->parent();
225     if (p) {
226         kDebug() << "///////  DELETEED CLIP HAS A PARENT... " << p->indexOfChild(item);
227         QTreeWidgetItem *clone = p->takeChild(p->indexOfChild(item));
228     } else if (item) {
229         delete item;
230     }
231 }
232
233 void ProjectList::slotAddFolder() {
234
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);
238 }
239
240 void ProjectList::slotAddFolder(const QString foldername, const QString &clipId, bool remove, bool edit) {
241     if (remove) {
242         ProjectItem *item;
243         QTreeWidgetItemIterator it(listView);
244         while (*it) {
245             item = static_cast <ProjectItem *>(*it);
246             if (item->clipType() == FOLDER && item->clipId() == clipId) {
247                 delete item;
248                 break;
249             }
250             ++it;
251         }
252     } else {
253         if (edit) {
254             disconnect(listView, SIGNAL(itemChanged(QTreeWidgetItem *, int)), this, SLOT(slotUpdateItemDescription(QTreeWidgetItem *, int)));
255             ProjectItem *item;
256             QTreeWidgetItemIterator it(listView);
257             while (*it) {
258                 item = static_cast <ProjectItem *>(*it);
259                 if (item->clipType() == FOLDER && item->clipId() == clipId) {
260                     item->setText(1, foldername);
261                     break;
262                 }
263                 ++it;
264             }
265             connect(listView, SIGNAL(itemChanged(QTreeWidgetItem *, int)), this, SLOT(slotUpdateItemDescription(QTreeWidgetItem *, int)));
266         } else {
267             QStringList text;
268             text << QString() << foldername;
269             (void) new ProjectItem(listView, text, clipId);
270         }
271     }
272 }
273
274 void ProjectList::slotAddClip(DocClipBase *clip) {
275     const QString parent = clip->toXML().attribute("groupid");
276     ProjectItem *item = NULL;
277     if (parent != 0) {
278         ProjectItem *parentitem = getItemById(parent);
279         if (parentitem) item = new ProjectItem(parentitem, clip);
280     }
281     if (item == NULL) item = new ProjectItem(listView, clip);
282
283     KUrl url = clip->fileURL();
284     if (!url.isEmpty()) {
285         // if file has Nepomuk comment, use it
286         Nepomuk::Resource f(url.path());
287         QString annotation;
288         if (f.isValid()) {
289             annotation = f.description();
290             /*
291             Nepomuk::Tag tag("test");
292             f.addTag(tag);*/
293         } else kDebug() << "---  CANNOT CONTACT NEPOMUK";
294         if (!annotation.isEmpty()) item->setText(2, annotation);
295     }
296     emit getFileProperties(clip->toXML(), clip->getId());
297 }
298
299 void ProjectList::slotUpdateClip(const QString &id) {
300     ProjectItem *item = getItemById(id);
301     item->setData(1, UsageRole, QString::number(item->numReferences()));
302 }
303
304 void ProjectList::slotAddClip(QUrl givenUrl, QString group) {
305     if (!m_commandStack) kDebug() << "!!!!!!!!!!!!!!!!  NO CMD STK";
306     KUrl::List list;
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;
311
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;
319             }
320         }
321         if (item && item->clipType() == FOLDER) {
322             group = item->groupName();
323             groupId = item->clipId();
324         }
325     }
326     m_doc->slotAddClipList(list, group, groupId);
327 }
328
329 void ProjectList::slotRemoveInvalidClip(const QString &id) {
330     ProjectItem *item = getItemById(id);
331     if (item) {
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));
334
335     }
336     QList <QString> ids;
337     ids << id;
338     m_doc->deleteProjectClip(ids);
339 }
340
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";
351
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;
359             }
360         }
361         if (item && item->clipType() == FOLDER) {
362             group = item->groupName();
363             groupId = item->clipId();
364         }
365
366         m_doc->slotAddColorClipFile(dia_ui->clip_name->text(), color, dia_ui->clip_duration->text(), group, groupId);
367     }
368     delete dia_ui;
369     delete dia;
370 }
371
372
373 void ProjectList::slotAddSlideshowClip() {
374     if (!m_commandStack) kDebug() << "!!!!!!!!!!!!!!!!  NO CMD STK";
375     SlideshowClip *dia = new SlideshowClip(this);
376
377     if (dia->exec() == QDialog::Accepted) {
378
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;
386             }
387         }
388         if (item && item->clipType() == FOLDER) {
389             group = item->groupName();
390             groupId = item->clipId();
391         }
392
393         m_doc->slotAddSlideshowClipFile(dia->clipName(), dia->selectedPath(), dia->imageCount(), dia->clipDuration(), dia->loop(), dia->fade(), dia->lumaDuration(), dia->lumaFile(), dia->softness(), group, groupId);
394     }
395     delete dia;
396 }
397
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;
406         }
407     }
408     if (item && item->clipType() == FOLDER) {
409         group = item->groupName();
410         groupId = item->clipId();
411     }
412
413     m_doc->slotCreateTextClip(group, groupId);
414 }
415
416 void ProjectList::setDocument(KdenliveDoc *doc) {
417     listView->clear();
418     QList <DocClipBase*> list = doc->clipManager()->documentClipList();
419     for (int i = 0; i < list.count(); i++) {
420         slotAddClip(list.at(i));
421     }
422
423     m_fps = doc->fps();
424     m_timecode = doc->timecode();
425     m_commandStack = doc->commandStack();
426     m_doc = doc;
427     QTreeWidgetItem *first = listView->topLevelItem(0);
428     if (first) listView->setCurrentItem(first);
429     m_toolbar->setEnabled(true);
430 }
431
432 QDomElement ProjectList::producersList() {
433     QDomDocument doc;
434     QDomElement prods = doc.createElement("producerlist");
435     doc.appendChild(prods);
436     kDebug() << "////////////  PRO LIST BUILD PRDSLIST ";
437     QTreeWidgetItemIterator it(listView);
438     while (*it) {
439         if (!((ProjectItem *)(*it))->isGroup())
440             prods.appendChild(doc.importNode(((ProjectItem *)(*it))->toXml(), true));
441         ++it;
442     }
443     return prods;
444 }
445
446 void ProjectList::slotRefreshClipThumbnail(const QString &clipId) {
447     ProjectItem *item = getItemById(clipId);
448     if (item) slotRefreshClipThumbnail(item);
449 }
450
451 void ProjectList::slotRefreshClipThumbnail(ProjectItem *item) {
452     if (item) {
453         int height = 50;
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);
457     }
458 }
459
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);
462     if (item) {
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...";
467 }
468
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);
472 }
473
474 ProjectItem *ProjectList::getItemById(const QString &id) {
475     QTreeWidgetItemIterator it(listView);
476     while (*it) {
477         if (((ProjectItem *)(*it))->clipId() == id)
478             break;
479         ++it;
480     }
481     if (*it) return ((ProjectItem *)(*it));
482     return NULL;
483 }
484
485 void ProjectList::slotSelectClip(const QString &ix) {
486     ProjectItem *p = getItemById(ix);
487     if (p) {
488         listView->setCurrentItem(p);
489         listView->scrollToItem(p);
490     }
491 }
492
493 #include "projectlist.moc"