]> git.sesse.net Git - kdenlive/blob - src/projectlist.cpp
more work on keyframe effects and thumbnail fixes
[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("document-new"), i18n("Add Clip"));
75     connect(addClipButton, SIGNAL(triggered()), this, SLOT(slotAddClip()));
76
77     QAction *addColorClip = addMenu->addAction(KIcon("document-new"), i18n("Add Color Clip"));
78     connect(addColorClip, SIGNAL(triggered()), this, SLOT(slotAddColorClip()));
79
80     QAction *addSlideClip = addMenu->addAction(KIcon("document-new"), i18n("Add Slideshow Clip"));
81     connect(addSlideClip, SIGNAL(triggered()), this, SLOT(slotAddSlideshowClip()));
82
83     QAction *addTitleClip = addMenu->addAction(KIcon("document-new"), 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(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->toXml());
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         if (item == m_selectedItem) {
146             // if user clicked on the active clip item, just focus monitor, don't update it.
147             emit clipSelected(QDomElement());
148             return;
149         }
150         m_selectedItem = item;
151         emit clipSelected(item->toXml());
152     }
153 }
154
155 void ProjectList::slotUpdateClipProperties(int id, QMap <QString, QString> properties) {
156     ProjectItem *item = getItemById(id);
157     if (item) {
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());
161     }
162 }
163
164 void ProjectList::slotUpdateClipProperties(ProjectItem *clip, QMap <QString, QString> properties) {
165     if (!clip) return;
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"));
174         }
175     }
176 }
177
178 void ProjectList::slotItemEdited(QTreeWidgetItem *item, int column) {
179     ProjectItem *clip = static_cast <ProjectItem*>(item);
180     if (column == 2) {
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());
186     }
187 }
188
189 void ProjectList::slotContextMenu(const QPoint &pos, QTreeWidgetItem *item) {
190     bool enable = false;
191     if (item) {
192         enable = true;
193     }
194     m_editAction->setEnabled(enable);
195     m_deleteAction->setEnabled(enable);
196
197     m_menu->popup(pos);
198 }
199
200 void ProjectList::slotRemoveClip() {
201     if (!listView->currentItem()) return;
202     ProjectItem *item = static_cast <ProjectItem *>(listView->currentItem());
203     QList <int> ids;
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();
215         }
216     }
217     if (!ids.isEmpty()) m_doc->deleteProjectClip(ids);
218     if (!folderids.isEmpty()) m_doc->deleteProjectFolder(folderids);
219 }
220
221 void ProjectList::selectItemById(const int clipId) {
222     ProjectItem *item = getItemById(clipId);
223     if (item) listView->setCurrentItem(item);
224 }
225
226
227 void ProjectList::slotDeleteClip(int clipId) {
228     ProjectItem *item = getItemById(clipId);
229     QTreeWidgetItem *p = item->parent();
230     if (p) {
231         kDebug() << "///////  DELETEED CLIP HAS A PARENT... " << p->indexOfChild(item);
232         QTreeWidgetItem *clone = p->takeChild(p->indexOfChild(item));
233     } else if (item) {
234         delete item;
235     }
236 }
237
238 void ProjectList::slotAddFolder() {
239
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);
243 }
244
245 void ProjectList::slotAddFolder(const QString foldername, int clipId, bool remove, bool edit) {
246     if (remove) {
247         ProjectItem *item;
248         QTreeWidgetItemIterator it(listView);
249         while (*it) {
250             item = static_cast <ProjectItem *>(*it);
251             if (item->clipType() == FOLDER && item->clipId() == clipId) {
252                 delete item;
253                 break;
254             }
255             ++it;
256         }
257     } else {
258         if (edit) {
259             disconnect(listView, SIGNAL(itemChanged(QTreeWidgetItem *, int)), this, SLOT(slotUpdateItemDescription(QTreeWidgetItem *, int)));
260             ProjectItem *item;
261             QTreeWidgetItemIterator it(listView);
262             while (*it) {
263                 item = static_cast <ProjectItem *>(*it);
264                 if (item->clipType() == FOLDER && item->clipId() == clipId) {
265                     item->setText(1, foldername);
266                     break;
267                 }
268                 ++it;
269             }
270             connect(listView, SIGNAL(itemChanged(QTreeWidgetItem *, int)), this, SLOT(slotUpdateItemDescription(QTreeWidgetItem *, int)));
271         } else {
272             QStringList text;
273             text << QString() << foldername;
274             (void) new ProjectItem(listView, text, clipId);
275         }
276     }
277 }
278
279 void ProjectList::slotAddClip(DocClipBase *clip) {
280     const int parent = clip->toXML().attribute("groupid").toInt();
281     ProjectItem *item = NULL;
282     if (parent != 0) {
283         ProjectItem *parentitem = getItemById(parent);
284         if (parentitem) item = new ProjectItem(parentitem, clip);
285     }
286     if (item == NULL) item = new ProjectItem(listView, clip);
287
288     KUrl url = clip->fileURL();
289     if (!url.isEmpty()) {
290         // if file has Nepomuk comment, use it
291         Nepomuk::Resource f(url.path());
292         QString annotation;
293         if (f.isValid()) {
294             annotation = f.description();
295             /*
296             Nepomuk::Tag tag("test");
297             f.addTag(tag);*/
298         } else kDebug() << "---  CANNOT CONTACT NEPOMUK";
299         if (!annotation.isEmpty()) item->setText(2, annotation);
300     }
301     emit getFileProperties(clip->toXML(), clip->getId());
302 }
303
304 void ProjectList::slotUpdateClip(int id) {
305     ProjectItem *item = getItemById(id);
306     item->setData(1, UsageRole, QString::number(item->numReferences()));
307 }
308
309 void ProjectList::slotAddClip(QUrl givenUrl, QString group) {
310     if (!m_commandStack) kDebug() << "!!!!!!!!!!!!!!!!  NO CMD STK";
311     KUrl::List list;
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;
317     int groupId = -1;
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;
324             }
325         }
326         if (item && item->clipType() == FOLDER) {
327             group = item->groupName();
328             groupId = item->clipId();
329         }
330     }
331     for (it = list.begin(); it != list.end(); it++) {
332         m_doc->slotAddClipFile(*it, group, groupId);
333     }
334 }
335
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";
346
347         QString group = QString();
348         int groupId = -1;
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;
354             }
355         }
356         if (item && item->clipType() == FOLDER) {
357             group = item->groupName();
358             groupId = item->clipId();
359         }
360
361         m_doc->slotAddColorClipFile(dia_ui->clip_name->text(), color, dia_ui->clip_duration->text(), group, groupId);
362     }
363     delete dia_ui;
364     delete dia;
365 }
366
367
368 void ProjectList::slotAddSlideshowClip() {
369     if (!m_commandStack) kDebug() << "!!!!!!!!!!!!!!!!  NO CMD STK";
370     SlideshowClip *dia = new SlideshowClip(this);
371
372     if (dia->exec() == QDialog::Accepted) {
373
374         QString group = QString();
375         int groupId = -1;
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;
381             }
382         }
383         if (item && item->clipType() == FOLDER) {
384             group = item->groupName();
385             groupId = item->clipId();
386         }
387
388         m_doc->slotAddSlideshowClipFile(dia->clipName(), dia->selectedPath(), dia->imageCount(), dia->clipDuration(), dia->loop(), group, groupId);
389     }
390     delete dia;
391 }
392 void ProjectList::slotAddTitleClip() {
393     QString group = QString();
394     int groupId = -1;
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;
400         }
401     }
402     if (item && item->clipType() == FOLDER) {
403         group = item->groupName();
404         groupId = item->clipId();
405     }
406
407     m_doc->slotCreateTextClip(group, groupId);
408 }
409
410 void ProjectList::setDocument(KdenliveDoc *doc) {
411     listView->clear();
412     QList <DocClipBase*> list = doc->clipManager()->documentClipList();
413     for (int i = 0; i < list.count(); i++) {
414         slotAddClip(list.at(i));
415     }
416
417     m_fps = doc->fps();
418     m_timecode = doc->timecode();
419     m_commandStack = doc->commandStack();
420     m_doc = doc;
421     QTreeWidgetItem *first = listView->topLevelItem(0);
422     if (first) listView->setCurrentItem(first);
423     m_toolbar->setEnabled(true);
424 }
425
426 QDomElement ProjectList::producersList() {
427     QDomDocument doc;
428     QDomElement prods = doc.createElement("producerlist");
429     doc.appendChild(prods);
430     kDebug() << "////////////  PRO LIST BUILD PRDSLIST ";
431     QTreeWidgetItemIterator it(listView);
432     while (*it) {
433         if (!((ProjectItem *)(*it))->isGroup())
434             prods.appendChild(doc.importNode(((ProjectItem *)(*it))->toXml(), true));
435         ++it;
436     }
437     return prods;
438 }
439
440 void ProjectList::slotRefreshClipThumbnail(int clipId) {
441     ProjectItem *item = getItemById(clipId);
442     if (item) slotRefreshClipThumbnail(item);
443 }
444
445 void ProjectList::slotRefreshClipThumbnail(ProjectItem *item) {
446     if (item) {
447         int height = 50;
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);
452     }
453 }
454
455 void ProjectList::slotReplyGetFileProperties(int clipId, const QMap < QString, QString > &properties, const QMap < QString, QString > &metadata) {
456     ProjectItem *item = getItemById(clipId);
457     if (item) {
458         item->setProperties(properties, metadata);
459         listView->setCurrentItem(item);
460         emit receivedClipDuration(clipId, item->clipMaxDuration());
461     }
462 }
463
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);
467 }
468
469 ProjectItem *ProjectList::getItemById(int id) {
470     QTreeWidgetItemIterator it(listView);
471     while (*it) {
472         if (((ProjectItem *)(*it))->clipId() == id)
473             break;
474         ++it;
475     }
476     if (*it) return ((ProjectItem *)(*it));
477     return NULL;
478 }
479
480 #include "projectlist.moc"