]> git.sesse.net Git - kdenlive/blob - src/projectlist.cpp
982318a36c3c587953e0a9fb4817bf56fe330c77
[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         m_selectedItem = item;
147         emit clipSelected(item->referencedClip());
148     }
149 }
150
151 void ProjectList::slotUpdateClipProperties(int id, QMap <QString, QString> properties) {
152     ProjectItem *item = getItemById(id);
153     if (item) {
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());
157     }
158 }
159
160 void ProjectList::slotUpdateClipProperties(ProjectItem *clip, QMap <QString, QString> properties) {
161     if (!clip) return;
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"));
170         }
171     }
172 }
173
174 void ProjectList::slotItemEdited(QTreeWidgetItem *item, int column) {
175     ProjectItem *clip = static_cast <ProjectItem*>(item);
176     if (column == 2) {
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());
182     }
183 }
184
185 void ProjectList::slotContextMenu(const QPoint &pos, QTreeWidgetItem *item) {
186     bool enable = false;
187     if (item) {
188         enable = true;
189     }
190     m_editAction->setEnabled(enable);
191     m_deleteAction->setEnabled(enable);
192
193     m_menu->popup(pos);
194 }
195
196 void ProjectList::slotRemoveClip() {
197     if (!listView->currentItem()) return;
198     ProjectItem *item = static_cast <ProjectItem *>(listView->currentItem());
199     QList <int> ids;
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();
211         }
212     }
213     if (!ids.isEmpty()) m_doc->deleteProjectClip(ids);
214     if (!folderids.isEmpty()) m_doc->deleteProjectFolder(folderids);
215 }
216
217 void ProjectList::selectItemById(const int clipId) {
218     ProjectItem *item = getItemById(clipId);
219     if (item) listView->setCurrentItem(item);
220 }
221
222
223 void ProjectList::slotDeleteClip(int clipId) {
224     ProjectItem *item = getItemById(clipId);
225     QTreeWidgetItem *p = item->parent();
226     if (p) {
227         kDebug() << "///////  DELETEED CLIP HAS A PARENT... " << p->indexOfChild(item);
228         QTreeWidgetItem *clone = p->takeChild(p->indexOfChild(item));
229     } else if (item) {
230         delete item;
231     }
232 }
233
234 void ProjectList::slotAddFolder() {
235
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);
239 }
240
241 void ProjectList::slotAddFolder(const QString foldername, int clipId, bool remove, bool edit) {
242     if (remove) {
243         ProjectItem *item;
244         QTreeWidgetItemIterator it(listView);
245         while (*it) {
246             item = static_cast <ProjectItem *>(*it);
247             if (item->clipType() == FOLDER && item->clipId() == clipId) {
248                 delete item;
249                 break;
250             }
251             ++it;
252         }
253     } else {
254         if (edit) {
255             disconnect(listView, SIGNAL(itemChanged(QTreeWidgetItem *, int)), this, SLOT(slotUpdateItemDescription(QTreeWidgetItem *, int)));
256             ProjectItem *item;
257             QTreeWidgetItemIterator it(listView);
258             while (*it) {
259                 item = static_cast <ProjectItem *>(*it);
260                 if (item->clipType() == FOLDER && item->clipId() == clipId) {
261                     item->setText(1, foldername);
262                     break;
263                 }
264                 ++it;
265             }
266             connect(listView, SIGNAL(itemChanged(QTreeWidgetItem *, int)), this, SLOT(slotUpdateItemDescription(QTreeWidgetItem *, int)));
267         } else {
268             QStringList text;
269             text << QString() << foldername;
270             (void) new ProjectItem(listView, text, clipId);
271         }
272     }
273 }
274
275 void ProjectList::slotAddClip(DocClipBase *clip) {
276     const int parent = clip->toXML().attribute("groupid").toInt();
277     ProjectItem *item = NULL;
278     if (parent != 0) {
279         ProjectItem *parentitem = getItemById(parent);
280         if (parentitem) item = new ProjectItem(parentitem, clip);
281     }
282     if (item == NULL) item = new ProjectItem(listView, clip);
283
284     KUrl url = clip->fileURL();
285     if (!url.isEmpty()) {
286         // if file has Nepomuk comment, use it
287         Nepomuk::Resource f(url.path());
288         QString annotation;
289         if (f.isValid()) {
290             annotation = f.description();
291             /*
292             Nepomuk::Tag tag("test");
293             f.addTag(tag);*/
294         } else kDebug() << "---  CANNOT CONTACT NEPOMUK";
295         if (!annotation.isEmpty()) item->setText(2, annotation);
296     }
297     emit getFileProperties(clip->toXML(), clip->getId());
298 }
299
300 void ProjectList::slotUpdateClip(int id) {
301     ProjectItem *item = getItemById(id);
302     item->setData(1, UsageRole, QString::number(item->numReferences()));
303 }
304
305 void ProjectList::slotAddClip(QUrl givenUrl, QString group) {
306     if (!m_commandStack) kDebug() << "!!!!!!!!!!!!!!!!  NO CMD STK";
307     KUrl::List list;
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;
312
313     int groupId = -1;
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;
320             }
321         }
322         if (item && item->clipType() == FOLDER) {
323             group = item->groupName();
324             groupId = item->clipId();
325         }
326     }
327     foreach(const KUrl file, list) {
328         if (KIO::NetAccess::exists(file, KIO::NetAccess::SourceSide, NULL))
329             m_doc->slotAddClipFile(file, group, groupId);
330     }
331 }
332
333 void ProjectList::slotRemoveInvalidClip(int id) {
334     ProjectItem *item = getItemById(id);
335     if (item) {
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));
338
339     }
340     QList <int> ids;
341     ids << id;
342     m_doc->deleteProjectClip(ids);
343 }
344
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";
355
356         QString group = QString();
357         int groupId = -1;
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;
363             }
364         }
365         if (item && item->clipType() == FOLDER) {
366             group = item->groupName();
367             groupId = item->clipId();
368         }
369
370         m_doc->slotAddColorClipFile(dia_ui->clip_name->text(), color, dia_ui->clip_duration->text(), group, groupId);
371     }
372     delete dia_ui;
373     delete dia;
374 }
375
376
377 void ProjectList::slotAddSlideshowClip() {
378     if (!m_commandStack) kDebug() << "!!!!!!!!!!!!!!!!  NO CMD STK";
379     SlideshowClip *dia = new SlideshowClip(this);
380
381     if (dia->exec() == QDialog::Accepted) {
382
383         QString group = QString();
384         int groupId = -1;
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;
390             }
391         }
392         if (item && item->clipType() == FOLDER) {
393             group = item->groupName();
394             groupId = item->clipId();
395         }
396
397         m_doc->slotAddSlideshowClipFile(dia->clipName(), dia->selectedPath(), dia->imageCount(), dia->clipDuration(), dia->loop(), dia->fade(), dia->lumaDuration(), dia->lumaFile(), dia->softness(), group, groupId);
398     }
399     delete dia;
400 }
401
402 void ProjectList::slotAddTitleClip() {
403     QString group = QString();
404     int groupId = -1;
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;
410         }
411     }
412     if (item && item->clipType() == FOLDER) {
413         group = item->groupName();
414         groupId = item->clipId();
415     }
416
417     m_doc->slotCreateTextClip(group, groupId);
418 }
419
420 void ProjectList::setDocument(KdenliveDoc *doc) {
421     listView->clear();
422     QList <DocClipBase*> list = doc->clipManager()->documentClipList();
423     for (int i = 0; i < list.count(); i++) {
424         slotAddClip(list.at(i));
425     }
426
427     m_fps = doc->fps();
428     m_timecode = doc->timecode();
429     m_commandStack = doc->commandStack();
430     m_doc = doc;
431     QTreeWidgetItem *first = listView->topLevelItem(0);
432     if (first) listView->setCurrentItem(first);
433     m_toolbar->setEnabled(true);
434 }
435
436 QDomElement ProjectList::producersList() {
437     QDomDocument doc;
438     QDomElement prods = doc.createElement("producerlist");
439     doc.appendChild(prods);
440     kDebug() << "////////////  PRO LIST BUILD PRDSLIST ";
441     QTreeWidgetItemIterator it(listView);
442     while (*it) {
443         if (!((ProjectItem *)(*it))->isGroup())
444             prods.appendChild(doc.importNode(((ProjectItem *)(*it))->toXml(), true));
445         ++it;
446     }
447     return prods;
448 }
449
450 void ProjectList::slotRefreshClipThumbnail(int clipId) {
451     ProjectItem *item = getItemById(clipId);
452     if (item) slotRefreshClipThumbnail(item);
453 }
454
455 void ProjectList::slotRefreshClipThumbnail(ProjectItem *item) {
456     if (item) {
457         int height = 50;
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);
461     }
462 }
463
464 void ProjectList::slotReplyGetFileProperties(int clipId, Mlt::Producer *producer, const QMap < QString, QString > &properties, const QMap < QString, QString > &metadata) {
465     ProjectItem *item = getItemById(clipId);
466     if (item) {
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...";
472 }
473
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);
477 }
478
479 ProjectItem *ProjectList::getItemById(int id) {
480     QTreeWidgetItemIterator it(listView);
481     while (*it) {
482         if (((ProjectItem *)(*it))->clipId() == id)
483             break;
484         ++it;
485     }
486     if (*it) return ((ProjectItem *)(*it));
487     return NULL;
488 }
489
490 #include "projectlist.moc"