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