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