]> git.sesse.net Git - kdenlive/blob - src/projectlist.cpp
Allow changing advanced properties of several clips at once (aspect ratio, ...)
[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 "editclipcutcommand.h"
36 #include "editfoldercommand.h"
37 #include "addclipcutcommand.h"
38
39 #include "ui_templateclip_ui.h"
40
41 #include <KDebug>
42 #include <KAction>
43 #include <KLocale>
44 #include <KFileDialog>
45 #include <KInputDialog>
46 #include <KMessageBox>
47 #include <KIO/NetAccess>
48 #include <KFileItem>
49 #ifdef NEPOMUK
50 #include <nepomuk/global.h>
51 #include <nepomuk/resourcemanager.h>
52 //#include <nepomuk/tag.h>
53 #endif
54
55 #include <QMouseEvent>
56 #include <QStylePainter>
57 #include <QPixmap>
58 #include <QIcon>
59 #include <QMenu>
60 #include <QProcess>
61 #include <QHeaderView>
62
63 ProjectList::ProjectList(QWidget *parent) :
64         QWidget(parent),
65         m_render(NULL),
66         m_fps(-1),
67         m_commandStack(NULL),
68         m_editAction(NULL),
69         m_deleteAction(NULL),
70         m_openAction(NULL),
71         m_reloadAction(NULL),
72         m_transcodeAction(NULL),
73         m_doc(NULL),
74         m_refreshed(false),
75         m_infoQueue(),
76         m_thumbnailQueue()
77 {
78
79     m_listView = new ProjectListView(this);;
80     QVBoxLayout *layout = new QVBoxLayout;
81     layout->setContentsMargins(0, 0, 0, 0);
82
83     // setup toolbar
84     KTreeWidgetSearchLine *searchView = new KTreeWidgetSearchLine(this);
85     m_toolbar = new QToolBar("projectToolBar", this);
86     m_toolbar->addWidget(searchView);
87     int s = style()->pixelMetric(QStyle::PM_SmallIconSize);
88     m_toolbar->setIconSize(QSize(s, s));
89     searchView->setTreeWidget(m_listView);
90
91     m_addButton = new QToolButton(m_toolbar);
92     m_addButton->setPopupMode(QToolButton::MenuButtonPopup);
93     m_toolbar->addWidget(m_addButton);
94
95     layout->addWidget(m_toolbar);
96     layout->addWidget(m_listView);
97     setLayout(layout);
98
99     m_queueTimer.setInterval(100);
100     connect(&m_queueTimer, SIGNAL(timeout()), this, SLOT(slotProcessNextClipInQueue()));
101     m_queueTimer.setSingleShot(true);
102
103
104     connect(m_listView, SIGNAL(projectModified()), this, SIGNAL(projectModified()));
105     connect(m_listView, SIGNAL(itemSelectionChanged()), this, SLOT(slotClipSelected()));
106     connect(m_listView, SIGNAL(focusMonitor()), this, SLOT(slotClipSelected()));
107     connect(m_listView, SIGNAL(pauseMonitor()), this, SLOT(slotPauseMonitor()));
108     connect(m_listView, SIGNAL(requestMenu(const QPoint &, QTreeWidgetItem *)), this, SLOT(slotContextMenu(const QPoint &, QTreeWidgetItem *)));
109     connect(m_listView, SIGNAL(addClip()), this, SLOT(slotAddClip()));
110     connect(m_listView, SIGNAL(addClip(const QList <QUrl>, const QString &, const QString &)), this, SLOT(slotAddClip(const QList <QUrl>, const QString &, const QString &)));
111     connect(m_listView, SIGNAL(addClipCut(const QString &, int, int)), this, SLOT(slotAddClipCut(const QString &, int, int)));
112     connect(m_listView, SIGNAL(itemChanged(QTreeWidgetItem *, int)), this, SLOT(slotItemEdited(QTreeWidgetItem *, int)));
113     connect(m_listView, SIGNAL(showProperties(DocClipBase *)), this, SIGNAL(showClipProperties(DocClipBase *)));
114
115     m_listViewDelegate = new ItemDelegate(m_listView);
116     m_listView->setItemDelegate(m_listViewDelegate);
117 #ifdef NEPOMUK
118     if (KdenliveSettings::activate_nepomuk()) {
119         Nepomuk::ResourceManager::instance()->init();
120         if (!Nepomuk::ResourceManager::instance()->initialized()) {
121             kDebug() << "Cannot communicate with Nepomuk, DISABLING it";
122             KdenliveSettings::setActivate_nepomuk(false);
123         }
124     }
125 #endif
126 }
127
128 ProjectList::~ProjectList()
129 {
130     delete m_menu;
131     delete m_toolbar;
132     m_listView->blockSignals(true);
133     m_listView->clear();
134     delete m_listViewDelegate;
135 }
136
137 void ProjectList::focusTree() const
138 {
139     m_listView->setFocus();
140 }
141
142 void ProjectList::setupMenu(QMenu *addMenu, QAction *defaultAction)
143 {
144     QList <QAction *> actions = addMenu->actions();
145     for (int i = 0; i < actions.count(); i++) {
146         if (actions.at(i)->data().toString() == "clip_properties") {
147             m_editAction = actions.at(i);
148             m_toolbar->addAction(m_editAction);
149             actions.removeAt(i);
150             i--;
151         } else if (actions.at(i)->data().toString() == "delete_clip") {
152             m_deleteAction = actions.at(i);
153             m_toolbar->addAction(m_deleteAction);
154             actions.removeAt(i);
155             i--;
156         } else if (actions.at(i)->data().toString() == "edit_clip") {
157             m_openAction = actions.at(i);
158             actions.removeAt(i);
159             i--;
160         } else if (actions.at(i)->data().toString() == "reload_clip") {
161             m_reloadAction = actions.at(i);
162             actions.removeAt(i);
163             i--;
164         }
165     }
166
167     QMenu *m = new QMenu();
168     m->addActions(actions);
169     m_addButton->setMenu(m);
170     m_addButton->setDefaultAction(defaultAction);
171     m_menu = new QMenu();
172     m_menu->addActions(addMenu->actions());
173 }
174
175 void ProjectList::setupGeneratorMenu(QMenu *addMenu, QMenu *transcodeMenu)
176 {
177     if (!addMenu) return;
178     QMenu *menu = m_addButton->menu();
179     menu->addMenu(addMenu);
180     m_addButton->setMenu(menu);
181
182     m_menu->addMenu(addMenu);
183     if (addMenu->isEmpty()) addMenu->setEnabled(false);
184     m_menu->addMenu(transcodeMenu);
185     if (transcodeMenu->isEmpty()) transcodeMenu->setEnabled(false);
186     m_transcodeAction = transcodeMenu;
187     m_menu->addAction(m_reloadAction);
188     m_menu->addAction(m_editAction);
189     m_menu->addAction(m_openAction);
190     m_menu->addAction(m_deleteAction);
191     m_menu->insertSeparator(m_deleteAction);
192 }
193
194
195 QByteArray ProjectList::headerInfo() const
196 {
197     return m_listView->header()->saveState();
198 }
199
200 void ProjectList::setHeaderInfo(const QByteArray &state)
201 {
202     m_listView->header()->restoreState(state);
203 }
204
205 void ProjectList::slotEditClip()
206 {
207     QList<QTreeWidgetItem *> list = m_listView->selectedItems();
208     if (list.count() > 1) {
209         editClipSelection(list);
210         return;
211     }
212     ProjectItem *item;
213     if (!m_listView->currentItem() || m_listView->currentItem()->type() == PROJECTFOLDERTYPE) return;
214     if (m_listView->currentItem()->type() == PROJECTSUBCLIPTYPE) {
215         item = static_cast <ProjectItem*>(m_listView->currentItem()->parent());
216     } else item = static_cast <ProjectItem*>(m_listView->currentItem());
217     if (!(item->flags() & Qt::ItemIsDragEnabled)) return;
218     if (item) {
219         emit clipSelected(item->referencedClip());
220         emit showClipProperties(item->referencedClip());
221     }
222 }
223
224 void ProjectList::editClipSelection(QList<QTreeWidgetItem *> list)
225 {
226     // Gather all common properties
227     QMap <QString, QString> commonproperties;
228     QList <DocClipBase *> clipList;
229     commonproperties.insert("force_aspect_ratio", "-");
230     commonproperties.insert("force_progressive", "-");
231     commonproperties.insert("threads", "-");
232     commonproperties.insert("video_index", "-");
233     commonproperties.insert("audio_index", "-");
234
235     ProjectItem *item;
236     for (int i = 0; i < list.count(); i++) {
237         item = NULL;
238         if (list.at(i)->type() == PROJECTFOLDERTYPE) continue;
239         if (list.at(i)->type() == PROJECTSUBCLIPTYPE) {
240             item = static_cast <ProjectItem*>(list.at(i)->parent());
241         } else item = static_cast <ProjectItem*>(list.at(i));
242         if (!(item->flags() & Qt::ItemIsDragEnabled)) continue;
243         if (item) {
244             // check properties
245             DocClipBase *clip = item->referencedClip();
246             if (clipList.contains(clip)) continue;
247             clipList.append(clip);
248             QMap <QString, QString> clipprops = clip->properties();
249             QMapIterator<QString, QString> p(commonproperties);
250             while (p.hasNext()) {
251                 p.next();
252                 if (p.value().isEmpty()) continue;
253                 if (clipprops.contains(p.key())) {
254                     if (p.value() == "-") commonproperties.insert(p.key(), clipprops.value(p.key()));
255                     else if (p.value() != clipprops.value(p.key())) commonproperties.insert(p.key(), QString());
256                 } else commonproperties.insert(p.key(), QString());
257             }
258         }
259     }
260     QMapIterator<QString, QString> p(commonproperties);
261     while (p.hasNext()) {
262         p.next();
263         kDebug() << "Result: " << p.key() << " = " << p.value();
264     }
265     emit showClipProperties(clipList, commonproperties);
266 }
267
268 void ProjectList::slotOpenClip()
269 {
270     ProjectItem *item;
271     if (!m_listView->currentItem() || m_listView->currentItem()->type() == PROJECTFOLDERTYPE) return;
272     if (m_listView->currentItem()->type() == QTreeWidgetItem::UserType + 1) {
273         item = static_cast <ProjectItem*>(m_listView->currentItem()->parent());
274     } else item = static_cast <ProjectItem*>(m_listView->currentItem());
275     if (item) {
276         if (item->clipType() == IMAGE) {
277             if (KdenliveSettings::defaultimageapp().isEmpty()) KMessageBox::sorry(this, i18n("Please set a default application to open images in the Settings dialog"));
278             else QProcess::startDetached(KdenliveSettings::defaultimageapp(), QStringList() << item->clipUrl().path());
279         }
280         if (item->clipType() == AUDIO) {
281             if (KdenliveSettings::defaultaudioapp().isEmpty()) KMessageBox::sorry(this, i18n("Please set a default application to open audio files in the Settings dialog"));
282             else QProcess::startDetached(KdenliveSettings::defaultaudioapp(), QStringList() << item->clipUrl().path());
283         }
284     }
285 }
286
287 void ProjectList::cleanup()
288 {
289     m_listView->clearSelection();
290     QTreeWidgetItemIterator it(m_listView);
291     ProjectItem *item;
292     while (*it) {
293         if ((*it)->type() != PROJECTCLIPTYPE) {
294             it++;
295             continue;
296         }
297         item = static_cast <ProjectItem *>(*it);
298         if (item->numReferences() == 0) item->setSelected(true);
299         it++;
300     }
301     slotRemoveClip();
302 }
303
304 void ProjectList::trashUnusedClips()
305 {
306     QTreeWidgetItemIterator it(m_listView);
307     ProjectItem *item;
308     QStringList ids;
309     QStringList urls;
310     while (*it) {
311         if ((*it)->type() != PROJECTCLIPTYPE) {
312             it++;
313             continue;
314         }
315         item = static_cast <ProjectItem *>(*it);
316         if (item->numReferences() == 0) {
317             ids << item->clipId();
318             KUrl url = item->clipUrl();
319             if (!url.isEmpty() && !urls.contains(url.path())) urls << url.path();
320         }
321         it++;
322     }
323
324     // Check that we don't use the URL in another clip
325     QTreeWidgetItemIterator it2(m_listView);
326     while (*it2) {
327         if ((*it2)->type() != PROJECTCLIPTYPE) {
328             it2++;
329             continue;
330         }
331         item = static_cast <ProjectItem *>(*it2);
332         if (item->numReferences() > 0) {
333             KUrl url = item->clipUrl();
334             if (!url.isEmpty() && urls.contains(url.path())) urls.removeAll(url.path());
335         }
336         it2++;
337     }
338
339     m_doc->deleteProjectClip(ids);
340     for (int i = 0; i < urls.count(); i++) {
341         KIO::NetAccess::del(KUrl(urls.at(i)), this);
342     }
343 }
344
345 void ProjectList::slotReloadClip(const QString &id)
346 {
347     QList<QTreeWidgetItem *> selected;
348     if (id.isEmpty()) selected = m_listView->selectedItems();
349     else selected.append(getItemById(id));
350     ProjectItem *item;
351     for (int i = 0; i < selected.count(); i++) {
352         if (selected.at(i)->type() != PROJECTCLIPTYPE) continue;
353         item = static_cast <ProjectItem *>(selected.at(i));
354         if (item) {
355             if (item->clipType() == IMAGE) {
356                 item->referencedClip()->producer()->set("force_reload", 1);
357             } else if (item->clipType() == TEXT) {
358                 if (!item->referencedClip()->getProperty("xmltemplate").isEmpty()) regenerateTemplate(item);
359             }
360             //requestClipInfo(item->toXml(), item->clipId(), true);
361             // Clear the file_hash value, which will cause a complete reload of the clip
362             emit getFileProperties(item->toXml(), item->clipId(), m_listView->iconSize().height(), true);
363         }
364     }
365 }
366
367 void ProjectList::setRenderer(Render *projectRender)
368 {
369     m_render = projectRender;
370     m_listView->setIconSize(QSize((ProjectItem::itemDefaultHeight() - 2) * m_render->dar(), ProjectItem::itemDefaultHeight() - 2));
371 }
372
373 void ProjectList::slotClipSelected()
374 {
375     if (m_listView->currentItem()) {
376         if (m_listView->currentItem()->type() == PROJECTFOLDERTYPE) {
377             emit clipSelected(NULL);
378             m_editAction->setEnabled(false);
379             m_deleteAction->setEnabled(true);
380             m_openAction->setEnabled(false);
381             m_reloadAction->setEnabled(false);
382             m_transcodeAction->setEnabled(false);
383         } else {
384             ProjectItem *clip;
385             if (m_listView->currentItem()->type() == PROJECTSUBCLIPTYPE) {
386                 // this is a sub item, use base clip
387                 m_deleteAction->setEnabled(true);
388                 clip = static_cast <ProjectItem*>(m_listView->currentItem()->parent());
389                 if (clip == NULL) kDebug() << "-----------ERROR";
390                 SubProjectItem *sub = static_cast <SubProjectItem*>(m_listView->currentItem());
391                 emit clipSelected(clip->referencedClip(), sub->zone());
392                 return;
393             }
394             clip = static_cast <ProjectItem*>(m_listView->currentItem());
395             emit clipSelected(clip->referencedClip());
396             m_editAction->setEnabled(true);
397             m_deleteAction->setEnabled(true);
398             m_reloadAction->setEnabled(true);
399             m_transcodeAction->setEnabled(true);
400             if (clip->clipType() == IMAGE && !KdenliveSettings::defaultimageapp().isEmpty()) {
401                 m_openAction->setIcon(KIcon(KdenliveSettings::defaultimageapp()));
402                 m_openAction->setEnabled(true);
403             } else if (clip->clipType() == AUDIO && !KdenliveSettings::defaultaudioapp().isEmpty()) {
404                 m_openAction->setIcon(KIcon(KdenliveSettings::defaultaudioapp()));
405                 m_openAction->setEnabled(true);
406             } else m_openAction->setEnabled(false);
407         }
408     } else {
409         emit clipSelected(NULL);
410         m_editAction->setEnabled(false);
411         m_deleteAction->setEnabled(false);
412         m_openAction->setEnabled(false);
413         m_reloadAction->setEnabled(false);
414         m_transcodeAction->setEnabled(false);
415     }
416 }
417
418 void ProjectList::slotPauseMonitor()
419 {
420     if (m_render) m_render->pause();
421 }
422
423 void ProjectList::slotUpdateClipProperties(const QString &id, QMap <QString, QString> properties)
424 {
425     ProjectItem *item = getItemById(id);
426     if (item) {
427         slotUpdateClipProperties(item, properties);
428         if (properties.contains("out")) {
429             slotReloadClip(id);
430         } else if (properties.contains("colour") || properties.contains("resource") || properties.contains("xmldata") || properties.contains("force_aspect_ratio") || properties.contains("templatetext")) {
431             slotRefreshClipThumbnail(item);
432             emit refreshClip();
433         }
434     }
435 }
436
437 void ProjectList::slotUpdateClipProperties(ProjectItem *clip, QMap <QString, QString> properties)
438 {
439     if (!clip) return;
440     clip->setProperties(properties);
441     if (properties.contains("name")) {
442         m_listView->blockSignals(true);
443         clip->setText(0, properties.value("name"));
444         m_listView->blockSignals(false);
445         emit clipNameChanged(clip->clipId(), properties.value("name"));
446     }
447     if (properties.contains("description")) {
448         CLIPTYPE type = clip->clipType();
449         m_listView->blockSignals(true);
450         clip->setText(1, properties.value("description"));
451         m_listView->blockSignals(false);
452 #ifdef NEPOMUK
453         if (KdenliveSettings::activate_nepomuk() && (type == AUDIO || type == VIDEO || type == AV || type == IMAGE || type == PLAYLIST)) {
454             // Use Nepomuk system to store clip description
455             Nepomuk::Resource f(clip->clipUrl().path());
456             f.setDescription(properties.value("description"));
457         }
458 #endif
459         emit projectModified();
460     }
461 }
462
463 void ProjectList::slotItemEdited(QTreeWidgetItem *item, int column)
464 {
465     if (item->type() == PROJECTSUBCLIPTYPE) {
466         // this is a sub-item
467         if (column == 1) {
468             // user edited description
469             SubProjectItem *sub = static_cast <SubProjectItem*>(item);
470             ProjectItem *item = static_cast <ProjectItem *>(sub->parent());
471             EditClipCutCommand *command = new EditClipCutCommand(this, item->clipId(), sub->zone(), sub->zone(), sub->description(), sub->text(1), true);
472             m_commandStack->push(command);
473             //slotUpdateCutClipProperties(sub->clipId(), sub->zone(), sub->text(1), sub->text(1));
474         }
475         return;
476     }
477     if (item->type() == PROJECTFOLDERTYPE) {
478         if (column != 0) return;
479         FolderProjectItem *folder = static_cast <FolderProjectItem*>(item);
480         editFolder(item->text(0), folder->groupName(), folder->clipId());
481         folder->setGroupName(item->text(0));
482         m_doc->clipManager()->addFolder(folder->clipId(), item->text(0));
483         const int children = item->childCount();
484         for (int i = 0; i < children; i++) {
485             ProjectItem *child = static_cast <ProjectItem *>(item->child(i));
486             child->setProperty("groupname", item->text(0));
487         }
488         return;
489     }
490
491     ProjectItem *clip = static_cast <ProjectItem*>(item);
492     if (column == 1) {
493         if (clip->referencedClip()) {
494             QMap <QString, QString> oldprops;
495             QMap <QString, QString> newprops;
496             oldprops["description"] = clip->referencedClip()->getProperty("description");
497             newprops["description"] = item->text(1);
498
499             if (clip->clipType() == TEXT) {
500                 // This is a text template clip, update the image
501                 /*oldprops.insert("xmldata", clip->referencedClip()->getProperty("xmldata"));
502                 newprops.insert("xmldata", generateTemplateXml(clip->referencedClip()->getProperty("xmltemplate"), item->text(2)).toString());*/
503                 oldprops.insert("templatetext", clip->referencedClip()->getProperty("templatetext"));
504                 newprops.insert("templatetext", item->text(1));
505             }
506             slotUpdateClipProperties(clip->clipId(), newprops);
507             EditClipCommand *command = new EditClipCommand(this, clip->clipId(), oldprops, newprops, false);
508             m_commandStack->push(command);
509         }
510     } else if (column == 0) {
511         if (clip->referencedClip()) {
512             QMap <QString, QString> oldprops;
513             QMap <QString, QString> newprops;
514             oldprops["name"] = clip->referencedClip()->getProperty("name");
515             newprops["name"] = item->text(0);
516             slotUpdateClipProperties(clip, newprops);
517             emit projectModified();
518             EditClipCommand *command = new EditClipCommand(this, clip->clipId(), oldprops, newprops, false);
519             m_commandStack->push(command);
520         }
521     }
522 }
523
524 void ProjectList::slotContextMenu(const QPoint &pos, QTreeWidgetItem *item)
525 {
526     bool enable = false;
527     if (item) {
528         enable = true;
529     }
530     m_editAction->setEnabled(enable);
531     m_deleteAction->setEnabled(enable);
532     m_reloadAction->setEnabled(enable);
533     m_transcodeAction->setEnabled(enable);
534     if (enable) {
535         ProjectItem *clip = NULL;
536         if (m_listView->currentItem()->type() == PROJECTSUBCLIPTYPE) {
537             clip = static_cast <ProjectItem*>(item->parent());
538         } else if (m_listView->currentItem()->type() == PROJECTCLIPTYPE) clip = static_cast <ProjectItem*>(item);
539         if (clip && clip->clipType() == IMAGE && !KdenliveSettings::defaultimageapp().isEmpty()) {
540             m_openAction->setIcon(KIcon(KdenliveSettings::defaultimageapp()));
541             m_openAction->setEnabled(true);
542         } else if (clip && clip->clipType() == AUDIO && !KdenliveSettings::defaultaudioapp().isEmpty()) {
543             m_openAction->setIcon(KIcon(KdenliveSettings::defaultaudioapp()));
544             m_openAction->setEnabled(true);
545         } else m_openAction->setEnabled(false);
546     } else m_openAction->setEnabled(false);
547     m_menu->popup(pos);
548 }
549
550 void ProjectList::slotRemoveClip()
551 {
552     if (!m_listView->currentItem()) return;
553     QStringList ids;
554     QMap <QString, QString> folderids;
555     QList<QTreeWidgetItem *> selected = m_listView->selectedItems();
556
557     QUndoCommand *delCommand = new QUndoCommand();
558     delCommand->setText(i18n("Delete Clip Zone"));
559
560     for (int i = 0; i < selected.count(); i++) {
561         if (selected.at(i)->type() == PROJECTSUBCLIPTYPE) {
562             // subitem
563             SubProjectItem *sub = static_cast <SubProjectItem *>(selected.at(i));
564             ProjectItem *item = static_cast <ProjectItem *>(sub->parent());
565             new AddClipCutCommand(this, item->clipId(), sub->zone().x(), sub->zone().y(), sub->description(), false, true, delCommand);
566         } else if (selected.at(i)->type() == PROJECTFOLDERTYPE) {
567             // folder
568             FolderProjectItem *folder = static_cast <FolderProjectItem *>(selected.at(i));
569             folderids[folder->groupName()] = folder->clipId();
570             int children = folder->childCount();
571
572             if (children > 0 && 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, folder->text(1)), i18n("Delete Folder")) != KMessageBox::Yes) return;
573             for (int i = 0; i < children; ++i) {
574                 ProjectItem *child = static_cast <ProjectItem *>(folder->child(i));
575                 ids << child->clipId();
576             }
577         } else {
578             ProjectItem *item = static_cast <ProjectItem *>(selected.at(i));
579             ids << item->clipId();
580             if (item->numReferences() > 0) {
581                 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;
582             }
583         }
584     }
585
586     if (delCommand->childCount() == 0) delete delCommand;
587     else m_commandStack->push(delCommand);
588     if (!ids.isEmpty()) m_doc->deleteProjectClip(ids);
589     if (!folderids.isEmpty()) deleteProjectFolder(folderids);
590     if (m_listView->topLevelItemCount() == 0) {
591         m_editAction->setEnabled(false);
592         m_deleteAction->setEnabled(false);
593         m_openAction->setEnabled(false);
594         m_reloadAction->setEnabled(false);
595         m_transcodeAction->setEnabled(false);
596     }
597 }
598
599 void ProjectList::selectItemById(const QString &clipId)
600 {
601     ProjectItem *item = getItemById(clipId);
602     if (item) m_listView->setCurrentItem(item);
603 }
604
605
606 void ProjectList::slotDeleteClip(const QString &clipId)
607 {
608     ProjectItem *item = getItemById(clipId);
609     if (!item) {
610         kDebug() << "/// Cannot find clip to delete";
611         return;
612     }
613     m_listView->blockSignals(true);
614     delete item;
615     m_doc->clipManager()->deleteClip(clipId);
616     m_listView->blockSignals(false);
617     slotClipSelected();
618 }
619
620
621 void ProjectList::editFolder(const QString folderName, const QString oldfolderName, const QString &clipId)
622 {
623     EditFolderCommand *command = new EditFolderCommand(this, folderName, oldfolderName, clipId, false);
624     m_commandStack->push(command);
625     m_doc->setModified(true);
626 }
627
628 void ProjectList::slotAddFolder()
629 {
630     AddFolderCommand *command = new AddFolderCommand(this, i18n("Folder"), QString::number(m_doc->clipManager()->getFreeFolderId()), true);
631     m_commandStack->push(command);
632 }
633
634 void ProjectList::slotAddFolder(const QString foldername, const QString &clipId, bool remove, bool edit)
635 {
636     if (remove) {
637         FolderProjectItem *item = getFolderItemById(clipId);
638         if (item) {
639             m_doc->clipManager()->deleteFolder(clipId);
640             delete item;
641         }
642     } else {
643         if (edit) {
644             FolderProjectItem *item = getFolderItemById(clipId);
645             if (item) {
646                 m_listView->blockSignals(true);
647                 item->setGroupName(foldername);
648                 m_listView->blockSignals(false);
649                 m_doc->clipManager()->addFolder(clipId, foldername);
650                 const int children = item->childCount();
651                 for (int i = 0; i < children; i++) {
652                     ProjectItem *child = static_cast <ProjectItem *>(item->child(i));
653                     child->setProperty("groupname", foldername);
654                 }
655             }
656         } else {
657             QStringList text;
658             text << foldername;
659             m_listView->blockSignals(true);
660             m_listView->setCurrentItem(new FolderProjectItem(m_listView, text, clipId));
661             m_doc->clipManager()->addFolder(clipId, foldername);
662             m_listView->blockSignals(false);
663         }
664     }
665     m_doc->setModified(true);
666 }
667
668
669
670 void ProjectList::deleteProjectFolder(QMap <QString, QString> map)
671 {
672     QMapIterator<QString, QString> i(map);
673     QUndoCommand *delCommand = new QUndoCommand();
674     delCommand->setText(i18n("Delete Folder"));
675     while (i.hasNext()) {
676         i.next();
677         new AddFolderCommand(this, i.key(), i.value(), false, delCommand);
678     }
679     m_commandStack->push(delCommand);
680 }
681
682 void ProjectList::slotAddClip(DocClipBase *clip, bool getProperties)
683 {
684     m_listView->setEnabled(false);
685     if (getProperties) {
686         m_listView->blockSignals(true);
687         m_refreshed = false;
688         // remove file_hash so that we load all properties for the clip
689         QDomElement e = clip->toXML().cloneNode().toElement();
690         e.removeAttribute("file_hash");
691         m_infoQueue.insert(clip->getId(), e);
692         //m_render->getFileProperties(clip->toXML(), clip->getId(), true);
693     }
694     const QString parent = clip->getProperty("groupid");
695     ProjectItem *item = NULL;
696     if (!parent.isEmpty()) {
697         FolderProjectItem *parentitem = getFolderItemById(parent);
698         if (!parentitem) {
699             QStringList text;
700             QString groupName = clip->getProperty("groupname");
701             //kDebug() << "Adding clip to new group: " << groupName;
702             if (groupName.isEmpty()) groupName = i18n("Folder");
703             text << groupName;
704             parentitem = new FolderProjectItem(m_listView, text, parent);
705         } else {
706             //kDebug() << "Adding clip to existing group: " << parentitem->groupName();
707         }
708         if (parentitem) item = new ProjectItem(parentitem, clip);
709     }
710     if (item == NULL) item = new ProjectItem(m_listView, clip);
711     KUrl url = clip->fileURL();
712
713     if (getProperties == false && !clip->getClipHash().isEmpty()) {
714         QString cachedPixmap = m_doc->projectFolder().path(KUrl::AddTrailingSlash) + "thumbs/" + clip->getClipHash() + ".png";
715         if (QFile::exists(cachedPixmap)) {
716             item->setData(0, Qt::DecorationRole, QPixmap(cachedPixmap));
717         }
718     }
719 #ifdef NEPOMUK
720     if (!url.isEmpty() && KdenliveSettings::activate_nepomuk()) {
721         // if file has Nepomuk comment, use it
722         Nepomuk::Resource f(url.path());
723         QString annotation = f.description();
724         if (!annotation.isEmpty()) item->setText(1, annotation);
725         item->setText(2, QString::number(f.rating()));
726     }
727 #endif
728     // Add cut zones
729     QList <CutZoneInfo> cuts = clip->cutZones();
730     if (!cuts.isEmpty()) {
731         for (int i = 0; i < cuts.count(); i++) {
732             SubProjectItem *sub = new SubProjectItem(item, cuts.at(i).zone.x(), cuts.at(i).zone.y(), cuts.at(i).description);
733             if (!clip->getClipHash().isEmpty()) {
734                 QString cachedPixmap = m_doc->projectFolder().path(KUrl::AddTrailingSlash) + "thumbs/" + clip->getClipHash() + '#' + QString::number(cuts.at(i).zone.x()) + ".png";
735                 if (QFile::exists(cachedPixmap)) {
736                     sub->setData(0, Qt::DecorationRole, QPixmap(cachedPixmap));
737                 }
738             }
739         }
740     }
741
742     if (getProperties && m_listView->isEnabled()) m_listView->blockSignals(false);
743     if (getProperties && !m_queueTimer.isActive()) m_queueTimer.start();
744 }
745
746 void ProjectList::slotResetProjectList()
747 {
748     m_listView->clear();
749     emit clipSelected(NULL);
750     m_thumbnailQueue.clear();
751     m_infoQueue.clear();
752     m_refreshed = false;
753 }
754
755 void ProjectList::requestClipInfo(const QDomElement xml, const QString id)
756 {
757     m_refreshed = false;
758     m_infoQueue.insert(id, xml);
759     //if (m_infoQueue.count() == 1 || ) QTimer::singleShot(300, this, SLOT(slotProcessNextClipInQueue()));
760 }
761
762 void ProjectList::slotProcessNextClipInQueue()
763 {
764     if (m_infoQueue.isEmpty()) {
765         slotProcessNextThumbnail();
766         return;
767     }
768
769     QMap<QString, QDomElement>::const_iterator j = m_infoQueue.constBegin();
770     if (j != m_infoQueue.constEnd()) {
771         const QDomElement dom = j.value();
772         const QString id = j.key();
773         m_infoQueue.remove(j.key());
774         emit getFileProperties(dom, id, m_listView->iconSize().height(), false);
775     }
776 }
777
778 void ProjectList::slotUpdateClip(const QString &id)
779 {
780     ProjectItem *item = getItemById(id);
781     m_listView->blockSignals(true);
782     if (item) item->setData(1, UsageRole, QString::number(item->numReferences()));
783     m_listView->blockSignals(false);
784 }
785
786 void ProjectList::updateAllClips()
787 {
788     m_listView->setSortingEnabled(false);
789     kDebug() << "// UPDATE ALL CLPY";
790
791     QTreeWidgetItemIterator it(m_listView);
792     DocClipBase *clip;
793     ProjectItem *item;
794     m_listView->blockSignals(true);
795     while (*it) {
796         if ((*it)->type() == PROJECTSUBCLIPTYPE) {
797             // subitem
798             SubProjectItem *sub = static_cast <SubProjectItem *>(*it);
799             if (sub->data(0, Qt::DecorationRole).isNull()) {
800                 item = static_cast <ProjectItem *>((*it)->parent());
801                 requestClipThumbnail(item->clipId() + '#' + QString::number(sub->zone().x()));
802             }
803             ++it;
804             continue;
805         } else if ((*it)->type() == PROJECTFOLDERTYPE) {
806             // folder
807             ++it;
808             continue;
809         } else {
810             item = static_cast <ProjectItem *>(*it);
811             clip = item->referencedClip();
812             if (item->referencedClip()->producer() == NULL) {
813                 if (clip->isPlaceHolder() == false) {
814                     requestClipInfo(clip->toXML(), clip->getId());
815                 } else item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
816             } else {
817                 if (item->data(0, Qt::DecorationRole).isNull()) {
818                     requestClipThumbnail(clip->getId());
819                 }
820                 if (item->data(0, DurationRole).toString().isEmpty()) {
821                     item->changeDuration(item->referencedClip()->producer()->get_playtime());
822                 }
823             }
824             item->setData(1, UsageRole, QString::number(item->numReferences()));
825         }
826         //qApp->processEvents();
827         ++it;
828     }
829     if (!m_queueTimer.isActive()) m_queueTimer.start();
830     if (m_listView->isEnabled()) m_listView->blockSignals(false);
831     m_listView->setSortingEnabled(true);
832     if (m_infoQueue.isEmpty()) slotProcessNextThumbnail();
833 }
834
835 void ProjectList::slotAddClip(const QList <QUrl> givenList, const QString &groupName, const QString &groupId)
836 {
837     if (!m_commandStack) {
838         kDebug() << "!!!!!!!!!!!!!!!! NO CMD STK";
839     }
840     KUrl::List list;
841     if (givenList.isEmpty()) {
842         // Build list of mime types
843         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";
844
845         QString allExtensions;
846         foreach(const QString& mimeType, mimeTypes) {
847             KMimeType::Ptr mime(KMimeType::mimeType(mimeType));
848             if (mime) {
849                 allExtensions.append(mime->patterns().join(" "));
850                 allExtensions.append(' ');
851             }
852         }
853         const QString dialogFilter = allExtensions.simplified() + ' ' + QLatin1Char('|') + i18n("All Supported Files") + "\n* " + QLatin1Char('|') + i18n("All Files");
854         list = KFileDialog::getOpenUrls(KUrl("kfiledialog:///clipfolder"), dialogFilter, this);
855
856     } else {
857         for (int i = 0; i < givenList.count(); i++)
858             list << givenList.at(i);
859     }
860
861     foreach(const KUrl &file, list) {
862         // Check there is no folder here
863         KMimeType::Ptr type = KMimeType::findByUrl(file);
864         if (type->is("inode/directory")) {
865             // user dropped a folder
866             list.removeAll(file);
867         }
868     }
869
870     if (list.isEmpty()) return;
871
872     if (givenList.isEmpty()) {
873         QStringList groupInfo = getGroup();
874         m_doc->slotAddClipList(list, groupInfo.at(0), groupInfo.at(1));
875     } else m_doc->slotAddClipList(list, groupName, groupId);
876 }
877
878 void ProjectList::slotRemoveInvalidClip(const QString &id, bool replace)
879 {
880     ProjectItem *item = getItemById(id);
881     QTimer::singleShot(300, this, SLOT(slotProcessNextClipInQueue()));
882     if (item) {
883         const QString path = item->referencedClip()->fileURL().path();
884         if (!path.isEmpty()) {
885             if (replace) KMessageBox::sorry(this, i18n("Clip <b>%1</b><br>is invalid, will be removed from project.", path));
886             else {
887                 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;
888             }
889         }
890         QStringList ids;
891         ids << id;
892         if (replace) m_doc->deleteProjectClip(ids);
893     }
894 }
895
896 void ProjectList::slotAddColorClip()
897 {
898     if (!m_commandStack) {
899         kDebug() << "!!!!!!!!!!!!!!!! NO CMD STK";
900     }
901     QDialog *dia = new QDialog(this);
902     Ui::ColorClip_UI dia_ui;
903     dia_ui.setupUi(dia);
904     dia_ui.clip_name->setText(i18n("Color Clip"));
905     dia_ui.clip_duration->setText(KdenliveSettings::color_duration());
906     if (dia->exec() == QDialog::Accepted) {
907         QString color = dia_ui.clip_color->color().name();
908         color = color.replace(0, 1, "0x") + "ff";
909         QStringList groupInfo = getGroup();
910         m_doc->slotCreateColorClip(dia_ui.clip_name->text(), color, dia_ui.clip_duration->text(), groupInfo.at(0), groupInfo.at(1));
911     }
912     delete dia;
913 }
914
915
916 void ProjectList::slotAddSlideshowClip()
917 {
918     if (!m_commandStack) {
919         kDebug() << "!!!!!!!!!!!!!!!! NO CMD STK";
920     }
921     SlideshowClip *dia = new SlideshowClip(m_timecode, this);
922
923     if (dia->exec() == QDialog::Accepted) {
924         QStringList groupInfo = getGroup();
925         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));
926     }
927     delete dia;
928 }
929
930 void ProjectList::slotAddTitleClip()
931 {
932     QStringList groupInfo = getGroup();
933     m_doc->slotCreateTextClip(groupInfo.at(0), groupInfo.at(1));
934 }
935
936 void ProjectList::slotAddTitleTemplateClip()
937 {
938     QStringList groupInfo = getGroup();
939     if (!m_commandStack) {
940         kDebug() << "!!!!!!!!!!!!!!!! NO CMD STK";
941     }
942
943     // Get the list of existing templates
944     QStringList filter;
945     filter << "*.kdenlivetitle";
946     const QString path = m_doc->projectFolder().path(KUrl::AddTrailingSlash) + "titles/";
947     QStringList templateFiles = QDir(path).entryList(filter, QDir::Files);
948
949     QDialog *dia = new QDialog(this);
950     Ui::TemplateClip_UI dia_ui;
951     dia_ui.setupUi(dia);
952     for (int i = 0; i < templateFiles.size(); ++i) {
953         dia_ui.template_list->comboBox()->addItem(templateFiles.at(i), path + templateFiles.at(i));
954     }
955     dia_ui.template_list->fileDialog()->setFilter("*.kdenlivetitle");
956     //warning: setting base directory doesn't work??
957     KUrl startDir(path);
958     dia_ui.template_list->fileDialog()->setUrl(startDir);
959     dia_ui.text_box->setHidden(true);
960     if (dia->exec() == QDialog::Accepted) {
961         QString textTemplate = dia_ui.template_list->comboBox()->itemData(dia_ui.template_list->comboBox()->currentIndex()).toString();
962         if (textTemplate.isEmpty()) textTemplate = dia_ui.template_list->comboBox()->currentText();
963         // Create a cloned template clip
964         m_doc->slotCreateTextTemplateClip(groupInfo.at(0), groupInfo.at(1), KUrl(textTemplate));
965     }
966     delete dia;
967 }
968
969 QStringList ProjectList::getGroup() const
970 {
971     QStringList result;
972     QTreeWidgetItem *item = m_listView->currentItem();
973     while (item && item->type() != PROJECTFOLDERTYPE) {
974         item = item->parent();
975     }
976
977     if (item) {
978         FolderProjectItem *folder = static_cast <FolderProjectItem *>(item);
979         result << folder->groupName();
980         result << folder->clipId();
981     } else result << QString() << QString();
982     return result;
983 }
984
985 void ProjectList::setDocument(KdenliveDoc *doc)
986 {
987     m_listView->blockSignals(true);
988     m_listView->clear();
989     m_listView->setSortingEnabled(false);
990     emit clipSelected(NULL);
991     m_thumbnailQueue.clear();
992     m_infoQueue.clear();
993     m_refreshed = false;
994     m_fps = doc->fps();
995     m_timecode = doc->timecode();
996     m_commandStack = doc->commandStack();
997     m_doc = doc;
998
999     QMap <QString, QString> flist = doc->clipManager()->documentFolderList();
1000     QMapIterator<QString, QString> f(flist);
1001     while (f.hasNext()) {
1002         f.next();
1003         (void) new FolderProjectItem(m_listView, QStringList() << f.value(), f.key());
1004     }
1005
1006     QList <DocClipBase*> list = doc->clipManager()->documentClipList();
1007     for (int i = 0; i < list.count(); i++) {
1008         slotAddClip(list.at(i), false);
1009     }
1010
1011     m_listView->blockSignals(false);
1012     m_toolbar->setEnabled(true);
1013     connect(m_doc->clipManager(), SIGNAL(reloadClip(const QString &)), this, SLOT(slotReloadClip(const QString &)));
1014     connect(m_doc->clipManager(), SIGNAL(checkAllClips()), this, SLOT(updateAllClips()));
1015 }
1016
1017 QList <DocClipBase*> ProjectList::documentClipList() const
1018 {
1019     if (m_doc == NULL) return QList <DocClipBase*> ();
1020     return m_doc->clipManager()->documentClipList();
1021 }
1022
1023 QDomElement ProjectList::producersList()
1024 {
1025     QDomDocument doc;
1026     QDomElement prods = doc.createElement("producerlist");
1027     doc.appendChild(prods);
1028     kDebug() << "////////////  PRO LIST BUILD PRDSLIST ";
1029     QTreeWidgetItemIterator it(m_listView);
1030     while (*it) {
1031         if ((*it)->type() != PROJECTCLIPTYPE) {
1032             // subitem
1033             ++it;
1034             continue;
1035         }
1036         prods.appendChild(doc.importNode(((ProjectItem *)(*it))->toXml(), true));
1037         ++it;
1038     }
1039     return prods;
1040 }
1041
1042 void ProjectList::slotCheckForEmptyQueue()
1043 {
1044     if (!m_refreshed && m_thumbnailQueue.isEmpty() && m_infoQueue.isEmpty()) {
1045         m_refreshed = true;
1046         emit loadingIsOver();
1047         emit displayMessage(QString(), -1);
1048         m_listView->blockSignals(false);
1049         m_listView->setEnabled(true);
1050     } else if (!m_refreshed) QTimer::singleShot(300, this, SLOT(slotCheckForEmptyQueue()));
1051 }
1052
1053 void ProjectList::reloadClipThumbnails()
1054 {
1055     kDebug() << "//////////////  RELOAD CLIPS THUMBNAILS!!!";
1056     m_thumbnailQueue.clear();
1057     QTreeWidgetItemIterator it(m_listView);
1058     while (*it) {
1059         if ((*it)->type() != PROJECTCLIPTYPE) {
1060             // subitem
1061             ++it;
1062             continue;
1063         }
1064         m_thumbnailQueue << ((ProjectItem *)(*it))->clipId();
1065         ++it;
1066     }
1067     QTimer::singleShot(300, this, SLOT(slotProcessNextThumbnail()));
1068 }
1069
1070 void ProjectList::requestClipThumbnail(const QString id)
1071 {
1072     if (!m_thumbnailQueue.contains(id)) m_thumbnailQueue.append(id);
1073 }
1074
1075 void ProjectList::slotProcessNextThumbnail()
1076 {
1077     if (m_thumbnailQueue.isEmpty() && m_infoQueue.isEmpty()) {
1078         slotCheckForEmptyQueue();
1079         return;
1080     }
1081     if (!m_infoQueue.isEmpty()) {
1082         //QTimer::singleShot(300, this, SLOT(slotProcessNextThumbnail()));
1083         return;
1084     }
1085     if (m_thumbnailQueue.count() > 1) {
1086         int max = m_doc->clipManager()->clipsCount();
1087         emit displayMessage(i18n("Loading thumbnails"), (int)(100 *(max - m_thumbnailQueue.count()) / max));
1088     }
1089     slotRefreshClipThumbnail(m_thumbnailQueue.takeFirst(), false);
1090 }
1091
1092 void ProjectList::slotRefreshClipThumbnail(const QString &clipId, bool update)
1093 {
1094     QTreeWidgetItem *item = getAnyItemById(clipId);
1095     if (item) slotRefreshClipThumbnail(item, update);
1096     else slotProcessNextThumbnail();
1097 }
1098
1099 void ProjectList::slotRefreshClipThumbnail(QTreeWidgetItem *it, bool update)
1100 {
1101     if (it == NULL) return;
1102     ProjectItem *item = NULL;
1103     bool isSubItem = false;
1104     int frame;
1105     if (it->type() == PROJECTFOLDERTYPE) return;
1106     if (it->type() == PROJECTSUBCLIPTYPE) {
1107         item = static_cast <ProjectItem *>(it->parent());
1108         frame = static_cast <SubProjectItem *>(it)->zone().x();
1109         isSubItem = true;
1110     } else {
1111         item = static_cast <ProjectItem *>(it);
1112         frame = item->referencedClip()->getClipThumbFrame();
1113     }
1114
1115     if (item) {
1116         DocClipBase *clip = item->referencedClip();
1117         if (!clip) {
1118             slotProcessNextThumbnail();
1119             return;
1120         }
1121         QPixmap pix;
1122         int height = m_listView->iconSize().height();
1123         int width = (int)(height  * m_render->dar());
1124         if (clip->clipType() == AUDIO) pix = KIcon("audio-x-generic").pixmap(QSize(width, height));
1125         else if (clip->clipType() == IMAGE) pix = QPixmap::fromImage(KThumb::getFrame(item->referencedClip()->producer(), 0, width, height));
1126         else pix = item->referencedClip()->thumbProducer()->extractImage(frame, width, height);
1127
1128         if (!pix.isNull()) {
1129             m_listView->blockSignals(true);
1130             it->setData(0, Qt::DecorationRole, pix);
1131             if (m_listView->isEnabled()) m_listView->blockSignals(false);
1132             if (!isSubItem) m_doc->cachePixmap(item->getClipHash(), pix);
1133             else m_doc->cachePixmap(item->getClipHash() + '#' + QString::number(frame), pix);
1134         }
1135         if (update) emit projectModified();
1136         QTimer::singleShot(30, this, SLOT(slotProcessNextThumbnail()));
1137     }
1138 }
1139
1140 void ProjectList::slotReplyGetFileProperties(const QString &clipId, Mlt::Producer *producer, const QMap < QString, QString > &properties, const QMap < QString, QString > &metadata, bool replace)
1141 {
1142     ProjectItem *item = getItemById(clipId);
1143     if (item && producer) {
1144         m_listView->blockSignals(true);
1145         item->setProperties(properties, metadata);
1146         //Q_ASSERT_X(item->referencedClip(), "void ProjectList::slotReplyGetFileProperties", QString("Item with groupName %1 does not have a clip associated").arg(item->groupName()).toLatin1());
1147         item->referencedClip()->setProducer(producer, replace);
1148         if (!replace && item->data(0, Qt::DecorationRole).isNull()) {
1149             requestClipThumbnail(clipId);
1150         }
1151         //emit receivedClipDuration(clipId);
1152         if (m_listView->isEnabled() && replace) {
1153             // update clip in clip monitor
1154             emit clipSelected(NULL);
1155             emit clipSelected(item->referencedClip());
1156         }
1157         /*else {
1158             // Check if duration changed.
1159             emit receivedClipDuration(clipId);
1160             delete producer;
1161         }*/
1162         if (m_listView->isEnabled()) m_listView->blockSignals(false);
1163         /*if (item->icon(0).isNull()) {
1164             requestClipThumbnail(clipId);
1165         }*/
1166     } else kDebug() << "////////  COULD NOT FIND CLIP TO UPDATE PRPS...";
1167     int max = m_doc->clipManager()->clipsCount();
1168     emit displayMessage(i18n("Loading clips"), (int)(100 *(max - m_infoQueue.count()) / max));
1169     // small delay so that the app can display the progress info
1170     if (item && m_infoQueue.isEmpty() && m_thumbnailQueue.isEmpty()) {
1171         m_listView->setCurrentItem(item);
1172         emit clipSelected(item->referencedClip());
1173     }
1174     QTimer::singleShot(30, this, SLOT(slotProcessNextClipInQueue()));
1175 }
1176
1177 void ProjectList::slotReplyGetImage(const QString &clipId, const QPixmap &pix)
1178 {
1179     ProjectItem *item = getItemById(clipId);
1180     if (item && !pix.isNull()) {
1181         m_listView->blockSignals(true);
1182         item->setData(0, Qt::DecorationRole, pix);
1183         m_doc->cachePixmap(item->getClipHash(), pix);
1184         if (m_listView->isEnabled()) m_listView->blockSignals(false);
1185     }
1186 }
1187
1188 QTreeWidgetItem *ProjectList::getAnyItemById(const QString &id)
1189 {
1190     QTreeWidgetItemIterator it(m_listView);
1191     QString lookId = id;
1192     if (id.contains('#')) {
1193         lookId = id.section('#', 0, 0);
1194     }
1195
1196     ProjectItem *result = NULL;
1197     while (*it) {
1198         if ((*it)->type() != PROJECTCLIPTYPE) {
1199             // subitem
1200             ++it;
1201             continue;
1202         }
1203         ProjectItem *item = static_cast<ProjectItem *>(*it);
1204         if (item->clipId() == lookId) {
1205             result = item;
1206             break;
1207         }
1208         ++it;
1209     }
1210     if (result == NULL || !id.contains('#')) return result;
1211     else for (int i = 0; i < result->childCount(); i++) {
1212             SubProjectItem *sub = static_cast <SubProjectItem *>(result->child(i));
1213             if (sub && sub->zone().x() == id.section('#', 1, 1).toInt()) {
1214                 return sub;
1215             }
1216         }
1217
1218     return NULL;
1219 }
1220
1221
1222 ProjectItem *ProjectList::getItemById(const QString &id)
1223 {
1224     ProjectItem *item;
1225     QTreeWidgetItemIterator it(m_listView);
1226     while (*it) {
1227         if ((*it)->type() != PROJECTCLIPTYPE) {
1228             // subitem
1229             ++it;
1230             continue;
1231         }
1232         item = static_cast<ProjectItem *>(*it);
1233         if (item->clipId() == id)
1234             return item;
1235         ++it;
1236     }
1237     return NULL;
1238 }
1239
1240 FolderProjectItem *ProjectList::getFolderItemById(const QString &id)
1241 {
1242     FolderProjectItem *item;
1243     QTreeWidgetItemIterator it(m_listView);
1244     while (*it) {
1245         if ((*it)->type() == PROJECTFOLDERTYPE) {
1246             item = static_cast<FolderProjectItem *>(*it);
1247             if (item->clipId() == id) return item;
1248         }
1249         ++it;
1250     }
1251     return NULL;
1252 }
1253
1254 void ProjectList::slotSelectClip(const QString &ix)
1255 {
1256     ProjectItem *clip = getItemById(ix);
1257     if (clip) {
1258         m_listView->setCurrentItem(clip);
1259         m_listView->scrollToItem(clip);
1260         m_editAction->setEnabled(true);
1261         m_deleteAction->setEnabled(true);
1262         m_reloadAction->setEnabled(true);
1263         m_transcodeAction->setEnabled(true);
1264         if (clip->clipType() == IMAGE && !KdenliveSettings::defaultimageapp().isEmpty()) {
1265             m_openAction->setIcon(KIcon(KdenliveSettings::defaultimageapp()));
1266             m_openAction->setEnabled(true);
1267         } else if (clip->clipType() == AUDIO && !KdenliveSettings::defaultaudioapp().isEmpty()) {
1268             m_openAction->setIcon(KIcon(KdenliveSettings::defaultaudioapp()));
1269             m_openAction->setEnabled(true);
1270         } else m_openAction->setEnabled(false);
1271     }
1272 }
1273
1274 QString ProjectList::currentClipUrl() const
1275 {
1276     ProjectItem *item;
1277     if (!m_listView->currentItem() || m_listView->currentItem()->type() == PROJECTFOLDERTYPE) return QString();
1278     if (m_listView->currentItem()->type() == PROJECTSUBCLIPTYPE) {
1279         // subitem
1280         item = static_cast <ProjectItem*>(m_listView->currentItem()->parent());
1281     } else item = static_cast <ProjectItem*>(m_listView->currentItem());
1282     if (item == NULL) return QString();
1283     return item->clipUrl().path();
1284 }
1285
1286 void ProjectList::regenerateTemplate(const QString &id)
1287 {
1288     ProjectItem *clip = getItemById(id);
1289     if (clip) regenerateTemplate(clip);
1290 }
1291
1292 void ProjectList::regenerateTemplate(ProjectItem *clip)
1293 {
1294     //TODO: remove this unused method, only force_reload is necessary
1295     clip->referencedClip()->producer()->set("force_reload", 1);
1296 }
1297
1298 QDomDocument ProjectList::generateTemplateXml(QString path, const QString &replaceString)
1299 {
1300     QDomDocument doc;
1301     QFile file(path);
1302     if (!file.open(QIODevice::ReadOnly)) {
1303         kWarning() << "ERROR, CANNOT READ: " << path;
1304         return doc;
1305     }
1306     if (!doc.setContent(&file)) {
1307         kWarning() << "ERROR, CANNOT READ: " << path;
1308         file.close();
1309         return doc;
1310     }
1311     file.close();
1312     QDomNodeList texts = doc.elementsByTagName("content");
1313     for (int i = 0; i < texts.count(); i++) {
1314         QString data = texts.item(i).firstChild().nodeValue();
1315         data.replace("%s", replaceString);
1316         texts.item(i).firstChild().setNodeValue(data);
1317     }
1318     return doc;
1319 }
1320
1321
1322 void ProjectList::slotAddClipCut(const QString &id, int in, int out)
1323 {
1324     ProjectItem *clip = getItemById(id);
1325     if (clip == NULL) return;
1326     if (clip->referencedClip()->hasCutZone(QPoint(in, out))) return;
1327     AddClipCutCommand *command = new AddClipCutCommand(this, id, in, out, QString(), true, false);
1328     m_commandStack->push(command);
1329 }
1330
1331 void ProjectList::addClipCut(const QString &id, int in, int out, const QString desc, bool newItem)
1332 {
1333     ProjectItem *clip = getItemById(id);
1334     if (clip) {
1335         DocClipBase *base = clip->referencedClip();
1336         base->addCutZone(in, out);
1337         m_listView->blockSignals(true);
1338         SubProjectItem *sub = new SubProjectItem(clip, in, out, desc);
1339         if (newItem && desc.isEmpty() && !m_listView->isColumnHidden(1)) {
1340             if (!clip->isExpanded()) clip->setExpanded(true);
1341             m_listView->editItem(sub, 1);
1342         }
1343         QPixmap p = clip->referencedClip()->thumbProducer()->extractImage(in, (int)(sub->sizeHint(0).height()  * m_render->dar()), sub->sizeHint(0).height() - 2);
1344         sub->setData(0, Qt::DecorationRole, p);
1345         m_doc->cachePixmap(clip->getClipHash() + '#' + QString::number(in), p);
1346         m_listView->blockSignals(false);
1347     }
1348     emit projectModified();
1349 }
1350
1351 void ProjectList::removeClipCut(const QString &id, int in, int out)
1352 {
1353     ProjectItem *clip = getItemById(id);
1354     if (clip) {
1355         DocClipBase *base = clip->referencedClip();
1356         base->removeCutZone(in, out);
1357         SubProjectItem *sub = getSubItem(clip, QPoint(in, out));
1358         if (sub) {
1359             m_listView->blockSignals(true);
1360             delete sub;
1361             m_listView->blockSignals(false);
1362         }
1363     }
1364     emit projectModified();
1365 }
1366
1367 SubProjectItem *ProjectList::getSubItem(ProjectItem *clip, QPoint zone)
1368 {
1369     SubProjectItem *sub = NULL;
1370     if (clip) {
1371         for (int i = 0; i < clip->childCount(); i++) {
1372             QTreeWidgetItem *it = clip->child(i);
1373             if (it->type() == PROJECTSUBCLIPTYPE) {
1374                 sub = static_cast <SubProjectItem*>(it);
1375                 if (sub->zone() == zone) break;
1376                 else sub = NULL;
1377             }
1378         }
1379     }
1380     return sub;
1381 }
1382
1383 void ProjectList::slotUpdateClipCut(QPoint p)
1384 {
1385     if (!m_listView->currentItem() || m_listView->currentItem()->type() != PROJECTSUBCLIPTYPE) return;
1386     SubProjectItem *sub = static_cast <SubProjectItem*>(m_listView->currentItem());
1387     ProjectItem *item = static_cast <ProjectItem *>(sub->parent());
1388     EditClipCutCommand *command = new EditClipCutCommand(this, item->clipId(), sub->zone(), p, sub->text(1), sub->text(1), true);
1389     m_commandStack->push(command);
1390 }
1391
1392 void ProjectList::doUpdateClipCut(const QString &id, const QPoint oldzone, const QPoint zone, const QString &comment)
1393 {
1394     ProjectItem *clip = getItemById(id);
1395     SubProjectItem *sub = getSubItem(clip, oldzone);
1396     if (sub == NULL || clip == NULL) return;
1397     DocClipBase *base = clip->referencedClip();
1398     base->updateCutZone(oldzone.x(), oldzone.y(), zone.x(), zone.y(), comment);
1399     m_listView->blockSignals(true);
1400     sub->setZone(zone);
1401     sub->setDescription(comment);
1402     m_listView->blockSignals(false);
1403     emit projectModified();
1404 }
1405
1406 #include "projectlist.moc"