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