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