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