]> git.sesse.net Git - kdenlive/blob - src/projectlist.cpp
0a311341f15bff190a1d2f5ecb9080bf17055df1
[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 #include "projectlist.h"
21 #include "projectitem.h"
22 #include "addfoldercommand.h"
23 #include "kdenlivesettings.h"
24 #include "slideshowclip.h"
25 #include "ui_colorclip_ui.h"
26 #include "titlewidget.h"
27 #include "definitions.h"
28 #include "clipmanager.h"
29 #include "docclipbase.h"
30 #include "kdenlivedoc.h"
31 #include "renderer.h"
32 #include "kthumb.h"
33 #include "projectlistview.h"
34 #include "editclipcommand.h"
35 #include "editfoldercommand.h"
36
37
38 #include <KDebug>
39 #include <KAction>
40 #include <KLocale>
41 #include <KFileDialog>
42 #include <KInputDialog>
43 #include <KMessageBox>
44
45 #include <nepomuk/global.h>
46 #include <nepomuk/resourcemanager.h>
47 //#include <nepomuk/tag.h>
48
49 #include <QMouseEvent>
50 #include <QStylePainter>
51 #include <QPixmap>
52 #include <QIcon>
53 #include <QMenu>
54 #include <QProcess>
55 #include <QHeaderView>
56
57 ProjectList::ProjectList(QWidget *parent) :
58         QWidget(parent),
59         m_render(NULL),
60         m_fps(-1),
61         m_commandStack(NULL),
62         m_editAction(NULL),
63         m_deleteAction(NULL),
64         m_openAction(NULL),
65         m_reloadAction(NULL),
66         m_selectedItem(NULL),
67         m_refreshed(false),
68         m_infoQueue(),
69         m_thumbnailQueue()
70 {
71
72     m_listView = new ProjectListView(this);;
73     QVBoxLayout *layout = new QVBoxLayout;
74     layout->setContentsMargins(0, 0, 0, 0);
75
76     // setup toolbar
77     KTreeWidgetSearchLine *searchView = new KTreeWidgetSearchLine(this);
78     m_toolbar = new QToolBar("projectToolBar", this);
79     m_toolbar->addWidget(searchView);
80     searchView->setTreeWidget(m_listView);
81
82     m_addButton = new QToolButton(m_toolbar);
83     m_addButton->setPopupMode(QToolButton::MenuButtonPopup);
84     m_toolbar->addWidget(m_addButton);
85
86     layout->addWidget(m_toolbar);
87     layout->addWidget(m_listView);
88     setLayout(layout);
89     
90
91
92     connect(m_listView, SIGNAL(itemSelectionChanged()), this, SLOT(slotClipSelected()));
93     connect(m_listView, SIGNAL(focusMonitor()), this, SLOT(slotClipSelected()));
94     connect(m_listView, SIGNAL(pauseMonitor()), this, SLOT(slotPauseMonitor()));
95     connect(m_listView, SIGNAL(requestMenu(const QPoint &, QTreeWidgetItem *)), this, SLOT(slotContextMenu(const QPoint &, QTreeWidgetItem *)));
96     connect(m_listView, SIGNAL(addClip()), this, SLOT(slotAddClip()));
97     connect(m_listView, SIGNAL(addClip(const QList <QUrl>, const QString &)), this, SLOT(slotAddClip(const QList <QUrl>, const QString &)));
98     connect(m_listView, SIGNAL(itemChanged(QTreeWidgetItem *, int)), this, SLOT(slotItemEdited(QTreeWidgetItem *, int)));
99     connect(m_listView, SIGNAL(showProperties(DocClipBase *)), this, SIGNAL(showClipProperties(DocClipBase *)));
100
101     ItemDelegate *listViewDelegate = new ItemDelegate(m_listView);
102     m_listView->setItemDelegate(listViewDelegate);
103
104     if (KdenliveSettings::activate_nepomuk()) {
105         Nepomuk::ResourceManager::instance()->init();
106         if (!Nepomuk::ResourceManager::instance()->initialized()) {
107             kDebug() << "Cannot communicate with Nepomuk, DISABLING it";
108             KdenliveSettings::setActivate_nepomuk(false);
109         }
110     }
111 }
112
113 ProjectList::~ProjectList()
114 {
115     delete m_menu;
116     delete m_toolbar;
117 }
118
119 void ProjectList::setupMenu(QMenu *addMenu, QAction *defaultAction)
120 {
121     QList <QAction *> actions = addMenu->actions();
122     for (int i = 0; i < actions.count(); i++) {
123         if (actions.at(i)->data().toString() == "clip_properties") {
124             m_editAction = actions.at(i);
125             m_toolbar->addAction(m_editAction);
126             actions.removeAt(i);
127             i--;
128         } else if (actions.at(i)->data().toString() == "delete_clip") {
129             m_deleteAction = actions.at(i);
130             m_toolbar->addAction(m_deleteAction);
131             actions.removeAt(i);
132             i--;
133         } else if (actions.at(i)->data().toString() == "edit_clip") {
134             m_openAction = actions.at(i);
135             actions.removeAt(i);
136             i--;
137         } else if (actions.at(i)->data().toString() == "reload_clip") {
138             m_reloadAction = actions.at(i);
139             actions.removeAt(i);
140             i--;
141         }
142     }
143
144     QMenu *m = new QMenu();
145     m->addActions(actions);
146     m_addButton->setMenu(m);
147     m_addButton->setDefaultAction(defaultAction);
148     m_menu = new QMenu();
149     m_menu->addActions(addMenu->actions());
150 }
151
152 void ProjectList::setupGeneratorMenu(QMenu *addMenu)
153 {
154     if (!addMenu) return;
155     QMenu *menu = m_addButton->menu();
156     menu->addMenu(addMenu);
157     m_addButton->setMenu(menu);
158
159     m_menu->addMenu(addMenu);
160     if (addMenu->isEmpty()) addMenu->setEnabled(false);
161     m_menu->addAction(m_reloadAction);
162     m_menu->addAction(m_editAction);
163     m_menu->addAction(m_openAction);
164     m_menu->addAction(m_deleteAction);
165     m_menu->insertSeparator(m_deleteAction);
166 }
167
168
169 QByteArray ProjectList::headerInfo()
170 {
171     return m_listView->header()->saveState();
172 }
173
174 void ProjectList::setHeaderInfo(const QByteArray &state)
175 {
176     m_listView->header()->restoreState(state);
177 }
178
179 void ProjectList::slotEditClip()
180 {
181     ProjectItem *item = static_cast <ProjectItem*>(m_listView->currentItem());
182     if (!(item->flags() & Qt::ItemIsDragEnabled)) return;
183     if (item && !item->isGroup()) {
184         emit clipSelected(item->referencedClip());
185         emit showClipProperties(item->referencedClip());
186     }
187 }
188
189 void ProjectList::slotOpenClip()
190 {
191     ProjectItem *item = static_cast <ProjectItem*>(m_listView->currentItem());
192     if (item && !item->isGroup()) {
193         if (item->clipType() == IMAGE) {
194             if (KdenliveSettings::defaultimageapp().isEmpty()) KMessageBox::sorry(this, i18n("Please set a default application to open images in the Settings dialog"));
195             else QProcess::startDetached(KdenliveSettings::defaultimageapp(), QStringList() << item->clipUrl().path());
196         }
197         if (item->clipType() == AUDIO) {
198             if (KdenliveSettings::defaultaudioapp().isEmpty()) KMessageBox::sorry(this, i18n("Please set a default application to open audio files in the Settings dialog"));
199             else QProcess::startDetached(KdenliveSettings::defaultaudioapp(), QStringList() << item->clipUrl().path());
200         }
201     }
202 }
203
204 void ProjectList::slotReloadClip()
205 {
206     ProjectItem *item = static_cast <ProjectItem*>(m_listView->currentItem());
207     if (item && !item->isGroup()) {
208         if (item->clipType() == IMAGE) {
209             item->referencedClip()->producer()->set("force_reload", 1);
210         }
211         emit getFileProperties(item->toXml(), item->clipId(), false);
212     }
213 }
214
215 void ProjectList::setRenderer(Render *projectRender)
216 {
217     m_render = projectRender;
218     m_listView->setIconSize(QSize(40 * m_render->dar(), 40));
219 }
220
221 void ProjectList::slotClipSelected()
222 {
223     if (m_listView->currentItem()) {
224         ProjectItem *clip = static_cast <ProjectItem*>(m_listView->currentItem());
225         if (!clip->isGroup()) {
226             m_selectedItem = clip;
227             emit clipSelected(clip->referencedClip());
228         }
229         m_editAction->setEnabled(true);
230         m_deleteAction->setEnabled(true);
231         m_reloadAction->setEnabled(true);
232         if (clip->clipType() == IMAGE && !KdenliveSettings::defaultimageapp().isEmpty()) {
233             m_openAction->setIcon(KIcon(KdenliveSettings::defaultimageapp()));
234             m_openAction->setEnabled(true);
235         } else if (clip->clipType() == AUDIO && !KdenliveSettings::defaultaudioapp().isEmpty()) {
236             m_openAction->setIcon(KIcon(KdenliveSettings::defaultaudioapp()));
237             m_openAction->setEnabled(true);
238         } else m_openAction->setEnabled(false);
239     } else {
240         emit clipSelected(NULL);
241         m_editAction->setEnabled(false);
242         m_deleteAction->setEnabled(false);
243         m_openAction->setEnabled(false);
244         m_reloadAction->setEnabled(false);
245     }
246 }
247
248 void ProjectList::slotPauseMonitor()
249 {
250     if (m_render) m_render->pause();
251 }
252
253 void ProjectList::slotUpdateClipProperties(const QString &id, QMap <QString, QString> properties)
254 {
255     ProjectItem *item = getItemById(id);
256     if (item) {
257         slotUpdateClipProperties(item, properties);
258         if (properties.contains("colour") || properties.contains("resource") || properties.contains("xmldata")) slotRefreshClipThumbnail(item);
259         if (properties.contains("out")) item->changeDuration(properties.value("out").toInt());
260     }
261 }
262
263 void ProjectList::slotUpdateClipProperties(ProjectItem *clip, QMap <QString, QString> properties)
264 {
265     if (!clip) return;
266     if (!clip->isGroup()) clip->setProperties(properties);
267     if (properties.contains("name")) {
268         m_listView->blockSignals(true);
269         clip->setText(1, properties.value("name"));
270         m_listView->blockSignals(false);
271         emit clipNameChanged(clip->clipId(), properties.value("name"));
272     }
273     if (properties.contains("description")) {
274         CLIPTYPE type = clip->clipType();
275         m_listView->blockSignals(true);
276         clip->setText(2, properties.value("description"));
277         m_listView->blockSignals(false);
278         if (KdenliveSettings::activate_nepomuk() && (type == AUDIO || type == VIDEO || type == AV || type == IMAGE || type == PLAYLIST)) {
279             // Use Nepomuk system to store clip description
280             Nepomuk::Resource f(clip->clipUrl().path());
281             f.setDescription(properties.value("description"));
282         }
283         emit projectModified();
284     }
285 }
286
287 void ProjectList::slotItemEdited(QTreeWidgetItem *item, int column)
288 {
289     ProjectItem *clip = static_cast <ProjectItem*>(item);
290     if (column == 2) {
291         if (clip->referencedClip()) {
292             QMap <QString, QString> oldprops;
293             QMap <QString, QString> newprops;
294             oldprops["description"] = clip->referencedClip()->getProperty("description");
295             newprops["description"] = item->text(2);
296             slotUpdateClipProperties(clip, newprops);
297             EditClipCommand *command = new EditClipCommand(this, clip->clipId(), oldprops, newprops, false);
298             m_commandStack->push(command);
299         }
300     } else if (column == 1) {
301         if (clip->isGroup()) {
302             editFolder(item->text(1), clip->groupName(), clip->clipId());
303             clip->setGroupName(item->text(1));
304             m_doc->clipManager()->addFolder(clip->clipId(), item->text(1));
305             const int children = item->childCount();
306             for (int i = 0; i < children; i++) {
307                 ProjectItem *child = static_cast <ProjectItem *>(item->child(i));
308                 child->setProperty("groupname", item->text(1));
309             }
310         } else {
311             if (clip->referencedClip()) {
312                 QMap <QString, QString> oldprops;
313                 QMap <QString, QString> newprops;
314                 oldprops["name"] = clip->referencedClip()->getProperty("name");
315                 newprops["name"] = item->text(1);
316                 slotUpdateClipProperties(clip, newprops);
317                 emit projectModified();
318                 EditClipCommand *command = new EditClipCommand(this, clip->clipId(), oldprops, newprops, false);
319                 m_commandStack->push(command);
320             }
321         }
322     }
323 }
324
325 void ProjectList::slotContextMenu(const QPoint &pos, QTreeWidgetItem *item)
326 {
327     bool enable = false;
328     if (item) {
329         enable = true;
330     }
331     m_editAction->setEnabled(enable);
332     m_deleteAction->setEnabled(enable);
333     m_reloadAction->setEnabled(enable);
334     if (enable) {
335         ProjectItem *clip = static_cast <ProjectItem*>(item);
336         if (clip->clipType() == IMAGE && !KdenliveSettings::defaultimageapp().isEmpty()) {
337             m_openAction->setIcon(KIcon(KdenliveSettings::defaultimageapp()));
338             m_openAction->setEnabled(true);
339         } else if (clip->clipType() == AUDIO && !KdenliveSettings::defaultaudioapp().isEmpty()) {
340             m_openAction->setIcon(KIcon(KdenliveSettings::defaultaudioapp()));
341             m_openAction->setEnabled(true);
342         } else m_openAction->setEnabled(false);
343     } else m_openAction->setEnabled(false);
344     m_menu->popup(pos);
345 }
346
347 void ProjectList::slotRemoveClip()
348 {
349     if (!m_listView->currentItem()) return;
350     QList <QString> ids;
351     QMap <QString, QString> folderids;
352     QList<QTreeWidgetItem *> selected = m_listView->selectedItems();
353     ProjectItem *item;
354     for (int i = 0; i < selected.count(); i++) {
355         item = static_cast <ProjectItem *>(selected.at(i));
356         if (item->isGroup()) folderids[item->groupName()] = item->clipId();
357         else ids << item->clipId();
358         if (item->numReferences() > 0) {
359             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;
360         } else if (item->isGroup() && item->childCount() > 0) {
361             int children = item->childCount();
362             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;
363             for (int i = 0; i < children; ++i) {
364                 ProjectItem *child = static_cast <ProjectItem *>(item->child(i));
365                 ids << child->clipId();
366             }
367         }
368     }
369     if (!ids.isEmpty()) m_doc->deleteProjectClip(ids);
370     if (!folderids.isEmpty()) deleteProjectFolder(folderids);
371     if (m_listView->topLevelItemCount() == 0) {
372         m_editAction->setEnabled(false);
373         m_deleteAction->setEnabled(false);
374         m_openAction->setEnabled(false);
375         m_reloadAction->setEnabled(false);
376     }
377 }
378
379 void ProjectList::selectItemById(const QString &clipId)
380 {
381     ProjectItem *item = getItemById(clipId);
382     if (item) m_listView->setCurrentItem(item);
383 }
384
385
386 void ProjectList::slotDeleteClip(const QString &clipId)
387 {
388     ProjectItem *item = getItemById(clipId);
389     if (!item) {
390         kDebug() << "/// Cannot find clip to delete";
391         return;
392     }
393     delete item;
394 }
395
396
397 void ProjectList::editFolder(const QString folderName, const QString oldfolderName, const QString &clipId)
398 {
399     EditFolderCommand *command = new EditFolderCommand(this, folderName, oldfolderName, clipId, false);
400     m_commandStack->push(command);
401     m_doc->setModified(true);
402 }
403
404 void ProjectList::slotAddFolder()
405 {
406     AddFolderCommand *command = new AddFolderCommand(this, i18n("Folder"), QString::number(m_doc->clipManager()->getFreeFolderId()), true);
407     m_commandStack->push(command);
408 }
409
410 void ProjectList::slotAddFolder(const QString foldername, const QString &clipId, bool remove, bool edit)
411 {
412     if (remove) {
413         ProjectItem *item = getFolderItemById(clipId);
414         if (item) {
415             m_doc->clipManager()->deleteFolder(clipId);
416             delete item;
417         }
418     } else {
419         if (edit) {
420             ProjectItem *item = getFolderItemById(clipId);
421             QTreeWidgetItemIterator it(m_listView);
422             if (item) {
423                 m_listView->blockSignals(true);
424                 item->setGroupName(foldername);
425                 m_listView->blockSignals(false);
426                 m_doc->clipManager()->addFolder(clipId, foldername);
427                 const int children = item->childCount();
428                 for (int i = 0; i < children; i++) {
429                     ProjectItem *child = static_cast <ProjectItem *>(item->child(i));
430                     child->setProperty("groupname", foldername);
431                 }
432             }
433         } else {
434             QStringList text;
435             text << QString() << foldername;
436             m_listView->blockSignals(true);
437             (void) new ProjectItem(m_listView, text, clipId);
438             m_doc->clipManager()->addFolder(clipId, foldername);
439             m_listView->blockSignals(false);
440         }
441     }
442 }
443
444
445
446 void ProjectList::deleteProjectFolder(QMap <QString, QString> map)
447 {
448     QMapIterator<QString, QString> i(map);
449     QUndoCommand *delCommand = new QUndoCommand();
450     delCommand->setText(i18n("Delete Folder"));
451     while (i.hasNext()) {
452         i.next();
453         new AddFolderCommand(this, i.key(), i.value(), false, delCommand);
454     }
455     m_commandStack->push(delCommand);
456     m_doc->setModified(true);
457 }
458
459 void ProjectList::slotAddClip(DocClipBase *clip, bool getProperties)
460 {
461     if (getProperties) m_listView->setEnabled(false);
462     m_listView->blockSignals(true);
463     const QString parent = clip->getProperty("groupid");
464     kDebug() << "Adding clip with groupid: " << parent;
465     ProjectItem *item = NULL;
466     if (!parent.isEmpty()) {
467         ProjectItem *parentitem = getFolderItemById(parent);
468         if (!parentitem) {
469             QStringList text;
470             QString groupName = clip->getProperty("groupname");
471             //kDebug() << "Adding clip to new group: " << groupName;
472             if (groupName.isEmpty()) groupName = i18n("Folder");
473             text << QString() << groupName;
474             parentitem = new ProjectItem(m_listView, text, parent);
475         } else {
476             //kDebug() << "Adding clip to existing group: " << parentitem->groupName();
477         }
478         if (parentitem) item = new ProjectItem(parentitem, clip);
479     }
480     if (item == NULL) item = new ProjectItem(m_listView, clip);
481
482     KUrl url = clip->fileURL();
483     if (!url.isEmpty() && KdenliveSettings::activate_nepomuk()) {
484         // if file has Nepomuk comment, use it
485         Nepomuk::Resource f(url.path());
486         QString annotation = f.description();
487         if (!annotation.isEmpty()) item->setText(2, annotation);
488         item->setText(3, QString::number(f.rating()));
489     }
490     m_listView->blockSignals(false);
491 }
492
493 void ProjectList::slotResetProjectList()
494 {
495     m_listView->clear();
496     emit clipSelected(NULL);
497     m_thumbnailQueue.clear();
498     m_infoQueue.clear();
499     m_refreshed = false;
500 }
501
502 void ProjectList::requestClipInfo(const QDomElement xml, const QString id)
503 {
504     kDebug() << " PRG LISTĀ REQUEST CLP INFO: " << id;
505     m_infoQueue.insert(id, xml);
506     m_listView->setEnabled(false);
507     if (m_infoQueue.count() == 1) QTimer::singleShot(300, this, SLOT(slotProcessNextClipInQueue()));
508 }
509
510 void ProjectList::slotProcessNextClipInQueue()
511 {
512     if (m_infoQueue.isEmpty()) {
513         m_listView->setEnabled(true);
514         return;
515     }
516     QMap<QString, QDomElement>::const_iterator i = m_infoQueue.constBegin();
517     if (i != m_infoQueue.constEnd()) {
518         const QDomElement dom = i.value();
519         const QString id = i.key();
520         m_infoQueue.remove(i.key());
521         emit getFileProperties(dom, id, true);
522     }
523     if (m_infoQueue.isEmpty()) m_listView->setEnabled(true);
524 }
525
526 void ProjectList::slotUpdateClip(const QString &id)
527 {
528     ProjectItem *item = getItemById(id);
529     m_listView->blockSignals(true);
530     if (item) item->setData(1, UsageRole, QString::number(item->numReferences()));
531     m_listView->blockSignals(false);
532 }
533
534 void ProjectList::updateAllClips()
535 {
536     QTreeWidgetItemIterator it(m_listView);
537     while (*it) {
538         ProjectItem *item = static_cast <ProjectItem *>(*it);
539         if (!item->isGroup()) {
540             if (item->referencedClip()->producer() == NULL) {
541                 DocClipBase *clip = item->referencedClip();
542                 if (clip->clipType() == TEXT && !QFile::exists(clip->fileURL().path())) {
543                     // regenerate text clip image if required
544                     TitleWidget *dia_ui = new TitleWidget(KUrl(), QString(), m_render, this);
545                     QDomDocument doc;
546                     doc.setContent(clip->getProperty("xmldata"));
547                     dia_ui->setXml(doc);
548                     QImage pix = dia_ui->renderedPixmap();
549                     pix.save(clip->fileURL().path());
550                     delete dia_ui;
551                 }
552                 if (clip->isPlaceHolder() == false) requestClipInfo(clip->toXML(), clip->getId());
553                 else item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
554             } else {
555                 QString cachedPixmap = m_doc->projectFolder().path() + "/thumbs/" + item->getClipHash() + ".png";
556                 if (QFile::exists(cachedPixmap)) {
557                     //kDebug()<<"// USING CACHED PIX: "<<cachedPixmap;
558                     m_listView->blockSignals(true);
559                     item->setIcon(0, QPixmap(cachedPixmap));
560                     m_listView->blockSignals(false);
561                 } else requestClipThumbnail(item->clipId());
562
563                 if (item->data(1, DurationRole).toString().isEmpty()) {
564                     m_listView->blockSignals(true);
565                     item->changeDuration(item->referencedClip()->producer()->get_playtime());
566                     m_listView->blockSignals(false);
567                 }
568             }
569             m_listView->blockSignals(true);
570             item->setData(1, UsageRole, QString::number(item->numReferences()));
571             m_listView->blockSignals(false);
572             qApp->processEvents();
573         }
574         ++it;
575     }
576     QTimer::singleShot(500, this, SLOT(slotCheckForEmptyQueue()));
577 }
578
579 void ProjectList::slotAddClip(const QList <QUrl> givenList, QString group)
580 {
581     if (!m_commandStack) kDebug() << "!!!!!!!!!!!!!!!! NO CMD STK";
582     KUrl::List list;
583     if (givenList.isEmpty()) {
584         // Build list of mime types
585         QStringList mimeTypes = QStringList() << "application/x-kdenlive" << "video/x-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-tga" << "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" << "audio/x-flac" << "audio/mp4" << "video/x-matroska" << "audio/x-matroska";
586
587         QString allExtensions;
588         foreach(const QString& mimeType, mimeTypes) {
589             KMimeType::Ptr mime(KMimeType::mimeType(mimeType));
590             if (mime) {
591                 allExtensions.append(mime->patterns().join(" "));
592                 allExtensions.append(' ');
593             }
594         }
595         QString dialogFilter = allExtensions + ' ' + QLatin1Char('|') + i18n("All Supported Files");
596         dialogFilter.append("\n*" + QLatin1Char('|') + i18n("All Files"));
597         list = KFileDialog::getOpenUrls(KUrl("kfiledialog:///clipfolder"), dialogFilter, this);
598
599     } else {
600         for (int i = 0; i < givenList.count(); i++)
601             list << givenList.at(i);
602     }
603     if (list.isEmpty()) return;
604
605     QString groupId;
606     if (group.isEmpty()) {
607         ProjectItem *item = static_cast <ProjectItem*>(m_listView->currentItem());
608         if (item && !item->isGroup()) {
609             while (item->parent()) {
610                 item = static_cast <ProjectItem*>(item->parent());
611                 if (item->isGroup()) break;
612             }
613         }
614         if (item && item->isGroup()) {
615             group = item->groupName();
616             groupId = item->clipId();
617         }
618     }
619     m_doc->slotAddClipList(list, group, groupId);
620 }
621
622 void ProjectList::slotRemoveInvalidClip(const QString &id)
623 {
624     ProjectItem *item = getItemById(id);
625     if (item) {
626         const QString path = item->referencedClip()->fileURL().path();
627         if (!path.isEmpty()) KMessageBox::sorry(this, i18n("Clip <b>%1</b><br>is invalid, will be removed from project.", path));
628         QList <QString> ids;
629         ids << id;
630         m_doc->deleteProjectClip(ids);
631     }
632     if (!m_infoQueue.isEmpty()) QTimer::singleShot(300, this, SLOT(slotProcessNextClipInQueue()));
633     else m_listView->setEnabled(true);
634 }
635
636 void ProjectList::slotAddColorClip()
637 {
638     if (!m_commandStack) kDebug() << "!!!!!!!!!!!!!!!! NO CMD STK";
639     QDialog *dia = new QDialog(this);
640     Ui::ColorClip_UI *dia_ui = new Ui::ColorClip_UI();
641     dia_ui->setupUi(dia);
642     dia_ui->clip_name->setText(i18n("Color Clip"));
643     dia_ui->clip_duration->setText(KdenliveSettings::color_duration());
644     if (dia->exec() == QDialog::Accepted) {
645         QString color = dia_ui->clip_color->color().name();
646         color = color.replace(0, 1, "0x") + "ff";
647
648         QString group;
649         QString groupId;
650         ProjectItem *item = static_cast <ProjectItem*>(m_listView->currentItem());
651         if (item && !item->isGroup()) {
652             while (item->parent()) {
653                 item = static_cast <ProjectItem*>(item->parent());
654                 if (item->isGroup()) break;
655             }
656         }
657         if (item && item->isGroup()) {
658             group = item->groupName();
659             groupId = item->clipId();
660         }
661
662         m_doc->clipManager()->slotAddColorClipFile(dia_ui->clip_name->text(), color, dia_ui->clip_duration->text(), group, groupId);
663         m_doc->setModified(true);
664     }
665     delete dia_ui;
666     delete dia;
667 }
668
669
670 void ProjectList::slotAddSlideshowClip()
671 {
672     if (!m_commandStack) kDebug() << "!!!!!!!!!!!!!!!! NO CMD STK";
673     SlideshowClip *dia = new SlideshowClip(this);
674
675     if (dia->exec() == QDialog::Accepted) {
676
677         QString group;
678         QString groupId;
679         ProjectItem *item = static_cast <ProjectItem*>(m_listView->currentItem());
680         if (item && !item->isGroup()) {
681             while (item->parent()) {
682                 item = static_cast <ProjectItem*>(item->parent());
683                 if (item->isGroup()) break;
684             }
685         }
686         if (item && item->isGroup()) {
687             group = item->groupName();
688             groupId = item->clipId();
689         }
690
691         m_doc->clipManager()->slotAddSlideshowClipFile(dia->clipName(), dia->selectedPath(), dia->imageCount(), dia->clipDuration(), dia->loop(), dia->fade(), dia->lumaDuration(), dia->lumaFile(), dia->softness(), group, groupId);
692         m_doc->setModified(true);
693     }
694     delete dia;
695 }
696
697 void ProjectList::slotAddTitleClip()
698 {
699     QString group;
700     QString groupId;
701     ProjectItem *item = static_cast <ProjectItem*>(m_listView->currentItem());
702     if (item && !item->isGroup()) {
703         while (item->parent()) {
704             item = static_cast <ProjectItem*>(item->parent());
705             if (item->isGroup()) break;
706         }
707     }
708     if (item && item->isGroup()) {
709         group = item->groupName();
710         groupId = item->clipId();
711     }
712
713     m_doc->slotCreateTextClip(group, groupId);
714 }
715
716 void ProjectList::setDocument(KdenliveDoc *doc)
717 {
718     m_listView->blockSignals(true);
719     m_listView->clear();
720     emit clipSelected(NULL);
721     m_thumbnailQueue.clear();
722     m_infoQueue.clear();
723     m_refreshed = false;
724     QMap <QString, QString> flist = doc->clipManager()->documentFolderList();
725     QMapIterator<QString, QString> f(flist);
726     while (f.hasNext()) {
727         f.next();
728         (void) new ProjectItem(m_listView, QStringList() << QString() << f.value(), f.key());
729     }
730
731     QList <DocClipBase*> list = doc->clipManager()->documentClipList();
732     for (int i = 0; i < list.count(); i++) {
733         slotAddClip(list.at(i), false);
734     }
735
736     m_fps = doc->fps();
737     m_timecode = doc->timecode();
738     m_commandStack = doc->commandStack();
739     m_doc = doc;
740     QTreeWidgetItem *first = m_listView->topLevelItem(0);
741     if (first) m_listView->setCurrentItem(first);
742     m_listView->blockSignals(false);
743     m_toolbar->setEnabled(true);
744 }
745
746 QDomElement ProjectList::producersList()
747 {
748     QDomDocument doc;
749     QDomElement prods = doc.createElement("producerlist");
750     doc.appendChild(prods);
751     kDebug() << "////////////  PRO LISTĀ BUILD PRDSLIST ";
752     QTreeWidgetItemIterator it(m_listView);
753     while (*it) {
754         if (!((ProjectItem *)(*it))->isGroup())
755             prods.appendChild(doc.importNode(((ProjectItem *)(*it))->toXml(), true));
756         ++it;
757     }
758     return prods;
759 }
760
761 void ProjectList::slotCheckForEmptyQueue()
762 {
763     if (!m_refreshed && m_thumbnailQueue.isEmpty() && m_infoQueue.isEmpty()) {
764         m_refreshed = true;
765         emit loadingIsOver();
766     } else QTimer::singleShot(500, this, SLOT(slotCheckForEmptyQueue()));
767 }
768
769 void ProjectList::requestClipThumbnail(const QString &id)
770 {
771     m_thumbnailQueue.append(id);
772     if (m_thumbnailQueue.count() == 1) QTimer::singleShot(300, this, SLOT(slotProcessNextThumbnail()));
773 }
774
775 void ProjectList::slotProcessNextThumbnail()
776 {
777     if (m_thumbnailQueue.isEmpty()) {
778         return;
779     }
780     slotRefreshClipThumbnail(m_thumbnailQueue.takeFirst(), false);
781 }
782
783 void ProjectList::slotRefreshClipThumbnail(const QString &clipId, bool update)
784 {
785     ProjectItem *item = getItemById(clipId);
786     if (item) slotRefreshClipThumbnail(item, update);
787     else slotProcessNextThumbnail();
788 }
789
790 void ProjectList::slotRefreshClipThumbnail(ProjectItem *item, bool update)
791 {
792     if (item) {
793         if (!item->referencedClip()) return;
794         int height = 50;
795         int width = (int)(height  * m_render->dar());
796         DocClipBase *clip = item->referencedClip();
797         if (!clip) slotProcessNextThumbnail();
798         QPixmap pix;
799         if (clip->clipType() == AUDIO) pix = KIcon("audio-x-generic").pixmap(QSize(width, height));
800         else pix = item->referencedClip()->thumbProducer()->extractImage(item->referencedClip()->getClipThumbFrame(), width, height);
801         m_listView->blockSignals(true);
802         item->setIcon(0, pix);
803         m_listView->blockSignals(false);
804         m_doc->cachePixmap(item->getClipHash(), pix);
805         if (update) emit projectModified();
806         if (!m_thumbnailQueue.isEmpty()) QTimer::singleShot(300, this, SLOT(slotProcessNextThumbnail()));
807     }
808 }
809
810 void ProjectList::slotReplyGetFileProperties(const QString &clipId, Mlt::Producer *producer, const QMap < QString, QString > &properties, const QMap < QString, QString > &metadata, bool replace)
811 {
812     ProjectItem *item = getItemById(clipId);
813     if (item && producer) {
814         m_listView->blockSignals(true);
815         item->setProperties(properties, metadata);
816         Q_ASSERT_X(item->referencedClip(), "void ProjectList::slotReplyGetFileProperties", QString("Item with groupName %1 does not have a clip associated").arg(item->groupName()).toLatin1());
817         if (replace) item->referencedClip()->setProducer(producer);
818         else {
819             // Check if duration changed.
820             emit receivedClipDuration(clipId);
821             delete producer;
822         }
823         m_listView->blockSignals(false);
824     } else kDebug() << "////////  COULD NOT FIND CLIP TO UPDATE PRPS...";
825     if (!m_infoQueue.isEmpty()) QTimer::singleShot(300, this, SLOT(slotProcessNextClipInQueue()));
826     else m_listView->setEnabled(true);
827 }
828
829 void ProjectList::slotReplyGetImage(const QString &clipId, const QPixmap &pix)
830 {
831     ProjectItem *item = getItemById(clipId);
832     if (item) {
833         m_listView->blockSignals(true);
834         item->setIcon(0, pix);
835         m_doc->cachePixmap(item->getClipHash(), pix);
836         m_listView->blockSignals(false);
837     }
838 }
839
840 ProjectItem *ProjectList::getItemById(const QString &id)
841 {
842     ProjectItem *item;
843     QTreeWidgetItemIterator it(m_listView);
844     while (*it) {
845         item = static_cast<ProjectItem *>(*it);
846         if (item->clipId() == id && item->clipType() != FOLDER)
847             return item;
848         ++it;
849     }
850     return NULL;
851 }
852
853 ProjectItem *ProjectList::getFolderItemById(const QString &id)
854 {
855     ProjectItem *item;
856     QTreeWidgetItemIterator it(m_listView);
857     while (*it) {
858         item = static_cast<ProjectItem *>(*it);
859         if (item->clipId() == id && item->clipType() == FOLDER)
860             return item;
861         ++it;
862     }
863     return NULL;
864 }
865
866 void ProjectList::slotSelectClip(const QString &ix)
867 {
868     ProjectItem *clip = getItemById(ix);
869     if (clip) {
870         m_listView->setCurrentItem(clip);
871         m_listView->scrollToItem(clip);
872         m_editAction->setEnabled(true);
873         m_deleteAction->setEnabled(true);
874         m_reloadAction->setEnabled(true);
875         if (clip->clipType() == IMAGE && !KdenliveSettings::defaultimageapp().isEmpty()) {
876             m_openAction->setIcon(KIcon(KdenliveSettings::defaultimageapp()));
877             m_openAction->setEnabled(true);
878         } else if (clip->clipType() == AUDIO && !KdenliveSettings::defaultaudioapp().isEmpty()) {
879             m_openAction->setIcon(KIcon(KdenliveSettings::defaultaudioapp()));
880             m_openAction->setEnabled(true);
881         } else m_openAction->setEnabled(false);
882     }
883 }
884
885 #include "projectlist.moc"