]> git.sesse.net Git - kdenlive/blob - src/projectlist.cpp
slideshow update
[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 <kio/netaccess.h>
34 #include <KMessageBox>
35
36 #include <nepomuk/global.h>
37 #include <nepomuk/resource.h>
38 #include <nepomuk/tag.h>
39
40 #include "projectlist.h"
41 #include "projectitem.h"
42 #include "kdenlivesettings.h"
43 #include "slideshowclip.h"
44 #include "ui_colorclip_ui.h"
45
46
47 #include "definitions.h"
48 #include "clipmanager.h"
49 #include "docclipbase.h"
50 #include "kdenlivedoc.h"
51 #include "renderer.h"
52 #include "kthumb.h"
53 #include "projectlistview.h"
54
55 ProjectList::ProjectList(QWidget *parent)
56         : QWidget(parent), m_render(NULL), m_fps(-1), m_commandStack(NULL), m_selectedItem(NULL) {
57
58     QWidget *vbox = new QWidget;
59     listView = new ProjectListView(this);;
60     QVBoxLayout *layout = new QVBoxLayout;
61     m_clipIdCounter = 0;
62
63     // setup toolbar
64     searchView = new KTreeWidgetSearchLine(this);
65     m_toolbar = new QToolBar("projectToolBar", this);
66     m_toolbar->addWidget(searchView);
67
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);
73
74     QAction *addClipButton = addMenu->addAction(KIcon("kdenlive-add-clip"), i18n("Add Clip"));
75     connect(addClipButton, SIGNAL(triggered()), this, SLOT(slotAddClip()));
76
77     QAction *addColorClip = addMenu->addAction(KIcon("kdenlive-add-color-clip"), i18n("Add Color Clip"));
78     connect(addColorClip, SIGNAL(triggered()), this, SLOT(slotAddColorClip()));
79
80     QAction *addSlideClip = addMenu->addAction(KIcon("kdenlive-add-slide-clip"), i18n("Add Slideshow Clip"));
81     connect(addSlideClip, SIGNAL(triggered()), this, SLOT(slotAddSlideshowClip()));
82
83     QAction *addTitleClip = addMenu->addAction(KIcon("kdenlive-add-text-clip"), i18n("Add Title Clip"));
84     connect(addTitleClip, SIGNAL(triggered()), this, SLOT(slotAddTitleClip()));
85
86     m_deleteAction = m_toolbar->addAction(KIcon("edit-delete"), i18n("Delete Clip"));
87     connect(m_deleteAction, SIGNAL(triggered()), this, SLOT(slotRemoveClip()));
88
89     m_editAction = m_toolbar->addAction(KIcon("document-properties"), i18n("Edit Clip"));
90     connect(m_editAction, SIGNAL(triggered()), this, SLOT(slotEditClip()));
91
92     QAction *addFolderButton = addMenu->addAction(KIcon("folder-new"), i18n("Create Folder"));
93     connect(addFolderButton, SIGNAL(triggered()), this, SLOT(slotAddFolder()));
94
95     addButton->setDefaultAction(addClipButton);
96
97     layout->addWidget(m_toolbar);
98     layout->addWidget(listView);
99     setLayout(layout);
100     //m_toolbar->setEnabled(false);
101
102     searchView->setTreeWidget(listView);
103
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);
113
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 *)));
121
122     m_listViewDelegate = new ItemDelegate(listView);
123     listView->setItemDelegate(m_listViewDelegate);
124 }
125
126 ProjectList::~ProjectList() {
127     delete m_menu;
128     delete m_toolbar;
129 }
130
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());
135 }
136
137
138
139 void ProjectList::setRenderer(Render *projectRender) {
140     m_render = projectRender;
141 }
142
143 void ProjectList::slotClipSelected() {
144     ProjectItem *item = static_cast <ProjectItem*>(listView->currentItem());
145     if (item && !item->isGroup()) {
146         if (item == m_selectedItem) {
147             // if user clicked on the active clip item, just focus monitor, don't update it.
148             emit clipSelected(NULL);
149             return;
150         }
151         m_selectedItem = item;
152         emit clipSelected(item->referencedClip());
153     }
154 }
155
156 void ProjectList::slotUpdateClipProperties(int id, QMap <QString, QString> properties) {
157     ProjectItem *item = getItemById(id);
158     if (item) {
159         slotUpdateClipProperties(item, properties);
160         if (properties.contains("colour") || properties.contains("resource")) slotRefreshClipThumbnail(item);
161         if (properties.contains("out")) item->changeDuration(properties.value("out").toInt());
162     }
163 }
164
165 void ProjectList::slotUpdateClipProperties(ProjectItem *clip, QMap <QString, QString> properties) {
166     if (!clip) return;
167     clip->setProperties(properties);
168     if (properties.contains("description")) {
169         CLIPTYPE type = clip->clipType();
170         clip->setText(2, properties.value("description"));
171         if (type == AUDIO || type == VIDEO || type == AV || type == IMAGE || type == PLAYLIST) {
172             // Use Nepomuk system to store clip description
173             Nepomuk::Resource f(clip->clipUrl().path());
174             if (f.isValid()) f.setDescription(properties.value("description"));
175         }
176     }
177 }
178
179 void ProjectList::slotItemEdited(QTreeWidgetItem *item, int column) {
180     ProjectItem *clip = static_cast <ProjectItem*>(item);
181     if (column == 2) {
182         QMap <QString, QString> props;
183         props["description"] = item->text(2);
184         slotUpdateClipProperties(clip, props);
185     } else if (column == 1 && clip->clipType() == FOLDER) {
186         m_doc->slotEditFolder(item->text(1), clip->groupName(), clip->clipId());
187     }
188 }
189
190 void ProjectList::slotContextMenu(const QPoint &pos, QTreeWidgetItem *item) {
191     bool enable = false;
192     if (item) {
193         enable = true;
194     }
195     m_editAction->setEnabled(enable);
196     m_deleteAction->setEnabled(enable);
197
198     m_menu->popup(pos);
199 }
200
201 void ProjectList::slotRemoveClip() {
202     if (!listView->currentItem()) return;
203     ProjectItem *item = static_cast <ProjectItem *>(listView->currentItem());
204     QList <int> ids;
205     QMap <QString, int> folderids;
206     if (item->clipType() == FOLDER) folderids[item->groupName()] = item->clipId();
207     else ids << item->clipId();
208     if (item->numReferences() > 0) {
209         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;
210     } else if (item->clipType() == FOLDER && item->childCount() > 0) {
211         int children = item->childCount();
212         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;
213         for (int i = 0; i < children; ++i) {
214             ProjectItem *child = static_cast <ProjectItem *>(item->child(i));
215             ids << child->clipId();
216         }
217     }
218     if (!ids.isEmpty()) m_doc->deleteProjectClip(ids);
219     if (!folderids.isEmpty()) m_doc->deleteProjectFolder(folderids);
220 }
221
222 void ProjectList::selectItemById(const int clipId) {
223     ProjectItem *item = getItemById(clipId);
224     if (item) listView->setCurrentItem(item);
225 }
226
227
228 void ProjectList::slotDeleteClip(int clipId) {
229     ProjectItem *item = getItemById(clipId);
230     QTreeWidgetItem *p = item->parent();
231     if (p) {
232         kDebug() << "///////  DELETEED CLIP HAS A PARENT... " << p->indexOfChild(item);
233         QTreeWidgetItem *clone = p->takeChild(p->indexOfChild(item));
234     } else if (item) {
235         delete item;
236     }
237 }
238
239 void ProjectList::slotAddFolder() {
240
241     // QString folderName = KInputDialog::getText(i18n("New Folder"), i18n("Enter new folder name: "));
242     // if (folderName.isEmpty()) return;
243     m_doc->slotAddFolder(i18n("Folder")); //folderName);
244 }
245
246 void ProjectList::slotAddFolder(const QString foldername, int clipId, bool remove, bool edit) {
247     if (remove) {
248         ProjectItem *item;
249         QTreeWidgetItemIterator it(listView);
250         while (*it) {
251             item = static_cast <ProjectItem *>(*it);
252             if (item->clipType() == FOLDER && item->clipId() == clipId) {
253                 delete item;
254                 break;
255             }
256             ++it;
257         }
258     } else {
259         if (edit) {
260             disconnect(listView, SIGNAL(itemChanged(QTreeWidgetItem *, int)), this, SLOT(slotUpdateItemDescription(QTreeWidgetItem *, int)));
261             ProjectItem *item;
262             QTreeWidgetItemIterator it(listView);
263             while (*it) {
264                 item = static_cast <ProjectItem *>(*it);
265                 if (item->clipType() == FOLDER && item->clipId() == clipId) {
266                     item->setText(1, foldername);
267                     break;
268                 }
269                 ++it;
270             }
271             connect(listView, SIGNAL(itemChanged(QTreeWidgetItem *, int)), this, SLOT(slotUpdateItemDescription(QTreeWidgetItem *, int)));
272         } else {
273             QStringList text;
274             text << QString() << foldername;
275             (void) new ProjectItem(listView, text, clipId);
276         }
277     }
278 }
279
280 void ProjectList::slotAddClip(DocClipBase *clip) {
281     const int parent = clip->toXML().attribute("groupid").toInt();
282     ProjectItem *item = NULL;
283     if (parent != 0) {
284         ProjectItem *parentitem = getItemById(parent);
285         if (parentitem) item = new ProjectItem(parentitem, clip);
286     }
287     if (item == NULL) item = new ProjectItem(listView, clip);
288
289     KUrl url = clip->fileURL();
290     if (!url.isEmpty()) {
291         // if file has Nepomuk comment, use it
292         Nepomuk::Resource f(url.path());
293         QString annotation;
294         if (f.isValid()) {
295             annotation = f.description();
296             /*
297             Nepomuk::Tag tag("test");
298             f.addTag(tag);*/
299         } else kDebug() << "---  CANNOT CONTACT NEPOMUK";
300         if (!annotation.isEmpty()) item->setText(2, annotation);
301     }
302     emit getFileProperties(clip->toXML(), clip->getId());
303 }
304
305 void ProjectList::slotUpdateClip(int id) {
306     ProjectItem *item = getItemById(id);
307     item->setData(1, UsageRole, QString::number(item->numReferences()));
308 }
309
310 void ProjectList::slotAddClip(QUrl givenUrl, QString group) {
311     if (!m_commandStack) kDebug() << "!!!!!!!!!!!!!!!!  NO CMD STK";
312     KUrl::List list;
313     if (givenUrl.isEmpty()) {
314         list = KFileDialog::getOpenUrls(KUrl("kfiledialog:///clipfolder"), "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 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\n*.m2t *.mts|HDV video\n*.dv|DV video");
315     } else list.append(givenUrl);
316     if (list.isEmpty()) return;
317     KUrl::List::Iterator it;
318     int groupId = -1;
319     if (group.isEmpty()) {
320         ProjectItem *item = static_cast <ProjectItem*>(listView->currentItem());
321         if (item && item->clipType() != FOLDER) {
322             while (item->parent()) {
323                 item = static_cast <ProjectItem*>(item->parent());
324                 if (item->clipType() == FOLDER) break;
325             }
326         }
327         if (item && item->clipType() == FOLDER) {
328             group = item->groupName();
329             groupId = item->clipId();
330         }
331     }
332     for (it = list.begin(); it != list.end(); it++) {
333         m_doc->slotAddClipFile(*it, group, groupId);
334     }
335 }
336
337 void ProjectList::slotAddColorClip() {
338     if (!m_commandStack) kDebug() << "!!!!!!!!!!!!!!!!  NO CMD STK";
339     QDialog *dia = new QDialog(this);
340     Ui::ColorClip_UI *dia_ui = new Ui::ColorClip_UI();
341     dia_ui->setupUi(dia);
342     dia_ui->clip_name->setText(i18n("Color Clip"));
343     dia_ui->clip_duration->setText(KdenliveSettings::color_duration());
344     if (dia->exec() == QDialog::Accepted) {
345         QString color = dia_ui->clip_color->color().name();
346         color = color.replace(0, 1, "0x") + "ff";
347
348         QString group = QString();
349         int groupId = -1;
350         ProjectItem *item = static_cast <ProjectItem*>(listView->currentItem());
351         if (item && item->clipType() != FOLDER) {
352             while (item->parent()) {
353                 item = static_cast <ProjectItem*>(item->parent());
354                 if (item->clipType() == FOLDER) break;
355             }
356         }
357         if (item && item->clipType() == FOLDER) {
358             group = item->groupName();
359             groupId = item->clipId();
360         }
361
362         m_doc->slotAddColorClipFile(dia_ui->clip_name->text(), color, dia_ui->clip_duration->text(), group, groupId);
363     }
364     delete dia_ui;
365     delete dia;
366 }
367
368
369 void ProjectList::slotAddSlideshowClip() {
370     if (!m_commandStack) kDebug() << "!!!!!!!!!!!!!!!!  NO CMD STK";
371     SlideshowClip *dia = new SlideshowClip(this);
372
373     if (dia->exec() == QDialog::Accepted) {
374
375         QString group = QString();
376         int groupId = -1;
377         ProjectItem *item = static_cast <ProjectItem*>(listView->currentItem());
378         if (item && item->clipType() != FOLDER) {
379             while (item->parent()) {
380                 item = static_cast <ProjectItem*>(item->parent());
381                 if (item->clipType() == FOLDER) break;
382             }
383         }
384         if (item && item->clipType() == FOLDER) {
385             group = item->groupName();
386             groupId = item->clipId();
387         }
388
389         m_doc->slotAddSlideshowClipFile(dia->clipName(), dia->selectedPath(), dia->imageCount(), dia->clipDuration(), dia->loop(), dia->fade(), dia->lumaDuration(), dia->lumaFile(), dia->softness(),group, groupId);
390     }
391     delete dia;
392 }
393
394 void ProjectList::slotAddTitleClip() {
395     QString group = QString();
396     int groupId = -1;
397     ProjectItem *item = static_cast <ProjectItem*>(listView->currentItem());
398     if (item && item->clipType() != FOLDER) {
399         while (item->parent()) {
400             item = static_cast <ProjectItem*>(item->parent());
401             if (item->clipType() == FOLDER) break;
402         }
403     }
404     if (item && item->clipType() == FOLDER) {
405         group = item->groupName();
406         groupId = item->clipId();
407     }
408
409     m_doc->slotCreateTextClip(group, groupId);
410 }
411
412 void ProjectList::setDocument(KdenliveDoc *doc) {
413     listView->clear();
414     QList <DocClipBase*> list = doc->clipManager()->documentClipList();
415     for (int i = 0; i < list.count(); i++) {
416         slotAddClip(list.at(i));
417     }
418
419     m_fps = doc->fps();
420     m_timecode = doc->timecode();
421     m_commandStack = doc->commandStack();
422     m_doc = doc;
423     QTreeWidgetItem *first = listView->topLevelItem(0);
424     if (first) listView->setCurrentItem(first);
425     m_toolbar->setEnabled(true);
426 }
427
428 QDomElement ProjectList::producersList() {
429     QDomDocument doc;
430     QDomElement prods = doc.createElement("producerlist");
431     doc.appendChild(prods);
432     kDebug() << "////////////  PRO LIST BUILD PRDSLIST ";
433     QTreeWidgetItemIterator it(listView);
434     while (*it) {
435         if (!((ProjectItem *)(*it))->isGroup())
436             prods.appendChild(doc.importNode(((ProjectItem *)(*it))->toXml(), true));
437         ++it;
438     }
439     return prods;
440 }
441
442 void ProjectList::slotRefreshClipThumbnail(int clipId) {
443     ProjectItem *item = getItemById(clipId);
444     if (item) slotRefreshClipThumbnail(item);
445 }
446
447 void ProjectList::slotRefreshClipThumbnail(ProjectItem *item) {
448     if (item) {
449         int height = 50;
450         int width = (int)(height  * m_render->dar());
451         QPixmap pix = KThumb::getImage(item->toXml(), item->referencedClip()->getClipThumbFrame(), width, height);
452         item->setIcon(0, pix);
453     }
454 }
455
456 void ProjectList::slotReplyGetFileProperties(int clipId, Mlt::Producer *producer, const QMap < QString, QString > &properties, const QMap < QString, QString > &metadata) {
457     ProjectItem *item = getItemById(clipId);
458     if (item) {
459         item->setProperties(properties, metadata);
460         item->referencedClip()->setProducer(producer);
461         listView->setCurrentItem(item);
462         emit receivedClipDuration(clipId, item->clipMaxDuration());
463     } else kDebug() << "////////  COULD NOT FIND CLIP TO UPDATE PRPS...";
464 }
465
466 void ProjectList::slotReplyGetImage(int clipId, int pos, const QPixmap &pix, int w, int h) {
467     ProjectItem *item = getItemById(clipId);
468     if (item) item->setIcon(0, pix);
469 }
470
471 ProjectItem *ProjectList::getItemById(int id) {
472     QTreeWidgetItemIterator it(listView);
473     while (*it) {
474         if (((ProjectItem *)(*it))->clipId() == id)
475             break;
476         ++it;
477     }
478     if (*it) return ((ProjectItem *)(*it));
479     return NULL;
480 }
481
482 #include "projectlist.moc"