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