]> git.sesse.net Git - kdenlive/blob - src/projectlist.cpp
Fix project tree butons update when adding/removing clips
[kdenlive] / src / projectlist.cpp
1 /***************************************************************************
2  *   Copyright (C) 2007 by Jean-Baptiste Mardelle (jb@kdenlive.org)        *
3  *                                                                         *
4  *   This program is free software; you can redistribute it and/or modify  *
5  *   it under the terms of the GNU General Public License as published by  *
6  *   the Free Software Foundation; either version 2 of the License, or     *
7  *   (at your option) any later version.                                   *
8  *                                                                         *
9  *   This program is distributed in the hope that it will be useful,       *
10  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
11  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
12  *   GNU General Public License for more details.                          *
13  *                                                                         *
14  *   You should have received a copy of the GNU General Public License     *
15  *   along with this program; if not, write to the                         *
16  *   Free Software Foundation, Inc.,                                       *
17  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA          *
18  ***************************************************************************/
19
20 #include "projectlist.h"
21 #include "projectitem.h"
22 #include "addfoldercommand.h"
23 #include "kdenlivesettings.h"
24 #include "slideshowclip.h"
25 #include "ui_colorclip_ui.h"
26 #include "titlewidget.h"
27 #include "definitions.h"
28 #include "clipmanager.h"
29 #include "docclipbase.h"
30 #include "kdenlivedoc.h"
31 #include "renderer.h"
32 #include "kthumb.h"
33 #include "projectlistview.h"
34 #include "editclipcommand.h"
35 #include "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_fps", "-");
231     commonproperties.insert("force_progressive", "-");
232     commonproperties.insert("threads", "-");
233     commonproperties.insert("video_index", "-");
234     commonproperties.insert("audio_index", "-");
235
236     bool allowDurationChange = true;
237     int commonDuration = -1;
238     ProjectItem *item;
239     for (int i = 0; i < list.count(); i++) {
240         item = NULL;
241         if (list.at(i)->type() == PROJECTFOLDERTYPE) continue;
242         if (list.at(i)->type() == PROJECTSUBCLIPTYPE) {
243             item = static_cast <ProjectItem*>(list.at(i)->parent());
244         } else item = static_cast <ProjectItem*>(list.at(i));
245         if (!(item->flags() & Qt::ItemIsDragEnabled)) continue;
246         if (item) {
247             // check properties
248             DocClipBase *clip = item->referencedClip();
249             if (clipList.contains(clip)) continue;
250             if (clip->clipType() != COLOR && clip->clipType() != IMAGE && clip->clipType() != TEXT) {
251                 allowDurationChange = false;
252             }
253             if (allowDurationChange && commonDuration != 0) {
254                 if (commonDuration == -1) {
255                     commonDuration = clip->duration().frames(m_fps);
256                 } else if (commonDuration != clip->duration().frames(m_fps)) {
257                     commonDuration = 0;
258                 }
259             }
260             clipList.append(clip);
261             QMap <QString, QString> clipprops = clip->properties();
262             QMapIterator<QString, QString> p(commonproperties);
263             while (p.hasNext()) {
264                 p.next();
265                 if (p.value().isEmpty()) continue;
266                 if (clipprops.contains(p.key())) {
267                     if (p.value() == "-") commonproperties.insert(p.key(), clipprops.value(p.key()));
268                     else if (p.value() != clipprops.value(p.key())) commonproperties.insert(p.key(), QString());
269                 } else commonproperties.insert(p.key(), QString());
270             }
271         }
272     }
273     if (allowDurationChange) commonproperties.insert("out", QString::number(commonDuration));
274     QMapIterator<QString, QString> p(commonproperties);
275     while (p.hasNext()) {
276         p.next();
277         kDebug() << "Result: " << p.key() << " = " << p.value();
278     }
279     emit showClipProperties(clipList, commonproperties);
280 }
281
282 void ProjectList::slotOpenClip()
283 {
284     ProjectItem *item;
285     if (!m_listView->currentItem() || m_listView->currentItem()->type() == PROJECTFOLDERTYPE) return;
286     if (m_listView->currentItem()->type() == QTreeWidgetItem::UserType + 1) {
287         item = static_cast <ProjectItem*>(m_listView->currentItem()->parent());
288     } else item = static_cast <ProjectItem*>(m_listView->currentItem());
289     if (item) {
290         if (item->clipType() == IMAGE) {
291             if (KdenliveSettings::defaultimageapp().isEmpty()) KMessageBox::sorry(this, i18n("Please set a default application to open images in the Settings dialog"));
292             else QProcess::startDetached(KdenliveSettings::defaultimageapp(), QStringList() << item->clipUrl().path());
293         }
294         if (item->clipType() == AUDIO) {
295             if (KdenliveSettings::defaultaudioapp().isEmpty()) KMessageBox::sorry(this, i18n("Please set a default application to open audio files in the Settings dialog"));
296             else QProcess::startDetached(KdenliveSettings::defaultaudioapp(), QStringList() << item->clipUrl().path());
297         }
298     }
299 }
300
301 void ProjectList::cleanup()
302 {
303     m_listView->clearSelection();
304     QTreeWidgetItemIterator it(m_listView);
305     ProjectItem *item;
306     while (*it) {
307         if ((*it)->type() != PROJECTCLIPTYPE) {
308             it++;
309             continue;
310         }
311         item = static_cast <ProjectItem *>(*it);
312         if (item->numReferences() == 0) item->setSelected(true);
313         it++;
314     }
315     slotRemoveClip();
316 }
317
318 void ProjectList::trashUnusedClips()
319 {
320     QTreeWidgetItemIterator it(m_listView);
321     ProjectItem *item;
322     QStringList ids;
323     QStringList urls;
324     while (*it) {
325         if ((*it)->type() != PROJECTCLIPTYPE) {
326             it++;
327             continue;
328         }
329         item = static_cast <ProjectItem *>(*it);
330         if (item->numReferences() == 0) {
331             ids << item->clipId();
332             KUrl url = item->clipUrl();
333             if (!url.isEmpty() && !urls.contains(url.path())) urls << url.path();
334         }
335         it++;
336     }
337
338     // Check that we don't use the URL in another clip
339     QTreeWidgetItemIterator it2(m_listView);
340     while (*it2) {
341         if ((*it2)->type() != PROJECTCLIPTYPE) {
342             it2++;
343             continue;
344         }
345         item = static_cast <ProjectItem *>(*it2);
346         if (item->numReferences() > 0) {
347             KUrl url = item->clipUrl();
348             if (!url.isEmpty() && urls.contains(url.path())) urls.removeAll(url.path());
349         }
350         it2++;
351     }
352
353     emit deleteProjectClips(ids, QMap <QString, QString>());
354     for (int i = 0; i < urls.count(); i++) {
355         KIO::NetAccess::del(KUrl(urls.at(i)), this);
356     }
357 }
358
359 void ProjectList::slotReloadClip(const QString &id)
360 {
361     QList<QTreeWidgetItem *> selected;
362     if (id.isEmpty()) selected = m_listView->selectedItems();
363     else selected.append(getItemById(id));
364     ProjectItem *item;
365     for (int i = 0; i < selected.count(); i++) {
366         if (selected.at(i)->type() != PROJECTCLIPTYPE) continue;
367         item = static_cast <ProjectItem *>(selected.at(i));
368         if (item) {
369             if (item->clipType() == IMAGE) {
370                 item->referencedClip()->producer()->set("force_reload", 1);
371             } else if (item->clipType() == TEXT) {
372                 if (!item->referencedClip()->getProperty("xmltemplate").isEmpty()) regenerateTemplate(item);
373             }
374             //requestClipInfo(item->toXml(), item->clipId(), true);
375             // Clear the file_hash value, which will cause a complete reload of the clip
376             emit getFileProperties(item->toXml(), item->clipId(), m_listView->iconSize().height(), true);
377         }
378     }
379 }
380
381 void ProjectList::setRenderer(Render *projectRender)
382 {
383     m_render = projectRender;
384     m_listView->setIconSize(QSize((ProjectItem::itemDefaultHeight() - 2) * m_render->dar(), ProjectItem::itemDefaultHeight() - 2));
385 }
386
387 void ProjectList::slotClipSelected()
388 {
389     if (m_listView->currentItem()) {
390         if (m_listView->currentItem()->type() == PROJECTFOLDERTYPE) {
391             emit clipSelected(NULL);
392             m_editAction->setEnabled(false);
393             m_deleteAction->setEnabled(true);
394             m_openAction->setEnabled(false);
395             m_reloadAction->setEnabled(false);
396             m_transcodeAction->setEnabled(false);
397         } else {
398             ProjectItem *clip;
399             if (m_listView->currentItem()->type() == PROJECTSUBCLIPTYPE) {
400                 // this is a sub item, use base clip
401                 m_deleteAction->setEnabled(true);
402                 clip = static_cast <ProjectItem*>(m_listView->currentItem()->parent());
403                 if (clip == NULL) kDebug() << "-----------ERROR";
404                 SubProjectItem *sub = static_cast <SubProjectItem*>(m_listView->currentItem());
405                 emit clipSelected(clip->referencedClip(), sub->zone());
406                 m_transcodeAction->setEnabled(false);
407                 return;
408             }
409             clip = static_cast <ProjectItem*>(m_listView->currentItem());
410             if (clip) emit clipSelected(clip->referencedClip());
411             m_editAction->setEnabled(true);
412             m_deleteAction->setEnabled(true);
413             m_reloadAction->setEnabled(true);
414             m_transcodeAction->setEnabled(true);
415             if (clip && clip->clipType() == IMAGE && !KdenliveSettings::defaultimageapp().isEmpty()) {
416                 m_openAction->setIcon(KIcon(KdenliveSettings::defaultimageapp()));
417                 m_openAction->setEnabled(true);
418             } else if (clip && clip->clipType() == AUDIO && !KdenliveSettings::defaultaudioapp().isEmpty()) {
419                 m_openAction->setIcon(KIcon(KdenliveSettings::defaultaudioapp()));
420                 m_openAction->setEnabled(true);
421             } else m_openAction->setEnabled(false);
422             // Display relevant transcoding actions only
423             adjustTranscodeActions(clip);
424         }
425     } else {
426         emit clipSelected(NULL);
427         m_editAction->setEnabled(false);
428         m_deleteAction->setEnabled(false);
429         m_openAction->setEnabled(false);
430         m_reloadAction->setEnabled(false);
431         m_transcodeAction->setEnabled(false);
432     }
433 }
434
435 void ProjectList::adjustTranscodeActions(ProjectItem *clip) const
436 {
437     if (clip == NULL || clip->type() != PROJECTCLIPTYPE || clip->clipType() == COLOR || clip->clipType() == TEXT || clip->clipType() == PLAYLIST) {
438         m_transcodeAction->setEnabled(false);
439         return;
440     }
441     m_transcodeAction->setEnabled(true);
442     QList<QAction *> transcodeActions = m_transcodeAction->actions();
443     QStringList data;
444     QString condition;
445     for (int i = 0; i < transcodeActions.count(); i++) {
446         data = transcodeActions.at(i)->data().toStringList();
447         if (data.count() > 2) {
448             condition = data.at(2);
449             if (condition.startsWith("vcodec")) transcodeActions.at(i)->setEnabled(clip->referencedClip()->hasVideoCodec(condition.section("=", 1, 1)));
450             else if (condition.startsWith("acodec")) transcodeActions.at(i)->setEnabled(clip->referencedClip()->hasVideoCodec(condition.section("=", 1, 1)));
451         }
452     }
453
454 }
455
456 void ProjectList::slotPauseMonitor()
457 {
458     if (m_render) m_render->pause();
459 }
460
461 void ProjectList::slotUpdateClipProperties(const QString &id, QMap <QString, QString> properties)
462 {
463     ProjectItem *item = getItemById(id);
464     if (item) {
465         slotUpdateClipProperties(item, properties);
466         if (properties.contains("out") || properties.contains("force_fps")) {
467             slotReloadClip(id);
468         } else if (properties.contains("colour") || properties.contains("resource") || properties.contains("xmldata") || properties.contains("force_aspect_ratio") || properties.contains("templatetext")) {
469             slotRefreshClipThumbnail(item);
470             emit refreshClip();
471         }
472     }
473 }
474
475 void ProjectList::slotUpdateClipProperties(ProjectItem *clip, QMap <QString, QString> properties)
476 {
477     if (!clip) return;
478     clip->setProperties(properties);
479     if (properties.contains("name")) {
480         m_listView->blockSignals(true);
481         clip->setText(0, properties.value("name"));
482         m_listView->blockSignals(false);
483         emit clipNameChanged(clip->clipId(), properties.value("name"));
484     }
485     if (properties.contains("description")) {
486         CLIPTYPE type = clip->clipType();
487         m_listView->blockSignals(true);
488         clip->setText(1, properties.value("description"));
489         m_listView->blockSignals(false);
490 #ifdef NEPOMUK
491         if (KdenliveSettings::activate_nepomuk() && (type == AUDIO || type == VIDEO || type == AV || type == IMAGE || type == PLAYLIST)) {
492             // Use Nepomuk system to store clip description
493             Nepomuk::Resource f(clip->clipUrl().path());
494             f.setDescription(properties.value("description"));
495         }
496 #endif
497         emit projectModified();
498     }
499 }
500
501 void ProjectList::slotItemEdited(QTreeWidgetItem *item, int column)
502 {
503     if (item->type() == PROJECTSUBCLIPTYPE) {
504         // this is a sub-item
505         if (column == 1) {
506             // user edited description
507             SubProjectItem *sub = static_cast <SubProjectItem*>(item);
508             ProjectItem *item = static_cast <ProjectItem *>(sub->parent());
509             EditClipCutCommand *command = new EditClipCutCommand(this, item->clipId(), sub->zone(), sub->zone(), sub->description(), sub->text(1), true);
510             m_commandStack->push(command);
511             //slotUpdateCutClipProperties(sub->clipId(), sub->zone(), sub->text(1), sub->text(1));
512         }
513         return;
514     }
515     if (item->type() == PROJECTFOLDERTYPE) {
516         if (column != 0) return;
517         FolderProjectItem *folder = static_cast <FolderProjectItem*>(item);
518         editFolder(item->text(0), folder->groupName(), folder->clipId());
519         folder->setGroupName(item->text(0));
520         m_doc->clipManager()->addFolder(folder->clipId(), item->text(0));
521         const int children = item->childCount();
522         for (int i = 0; i < children; i++) {
523             ProjectItem *child = static_cast <ProjectItem *>(item->child(i));
524             child->setProperty("groupname", item->text(0));
525         }
526         return;
527     }
528
529     ProjectItem *clip = static_cast <ProjectItem*>(item);
530     if (column == 1) {
531         if (clip->referencedClip()) {
532             QMap <QString, QString> oldprops;
533             QMap <QString, QString> newprops;
534             oldprops["description"] = clip->referencedClip()->getProperty("description");
535             newprops["description"] = item->text(1);
536
537             if (clip->clipType() == TEXT) {
538                 // This is a text template clip, update the image
539                 /*oldprops.insert("xmldata", clip->referencedClip()->getProperty("xmldata"));
540                 newprops.insert("xmldata", generateTemplateXml(clip->referencedClip()->getProperty("xmltemplate"), item->text(2)).toString());*/
541                 oldprops.insert("templatetext", clip->referencedClip()->getProperty("templatetext"));
542                 newprops.insert("templatetext", item->text(1));
543             }
544             slotUpdateClipProperties(clip->clipId(), newprops);
545             EditClipCommand *command = new EditClipCommand(this, clip->clipId(), oldprops, newprops, false);
546             m_commandStack->push(command);
547         }
548     } else if (column == 0) {
549         if (clip->referencedClip()) {
550             QMap <QString, QString> oldprops;
551             QMap <QString, QString> newprops;
552             oldprops["name"] = clip->referencedClip()->getProperty("name");
553             newprops["name"] = item->text(0);
554             slotUpdateClipProperties(clip, newprops);
555             emit projectModified();
556             EditClipCommand *command = new EditClipCommand(this, clip->clipId(), oldprops, newprops, false);
557             m_commandStack->push(command);
558         }
559     }
560 }
561
562 void ProjectList::slotContextMenu(const QPoint &pos, QTreeWidgetItem *item)
563 {
564     bool enable = false;
565     if (item) {
566         enable = true;
567     }
568     m_editAction->setEnabled(enable);
569     m_deleteAction->setEnabled(enable);
570     m_reloadAction->setEnabled(enable);
571     m_transcodeAction->setEnabled(enable);
572     if (enable) {
573         ProjectItem *clip = NULL;
574         if (m_listView->currentItem()->type() == PROJECTSUBCLIPTYPE) {
575             clip = static_cast <ProjectItem*>(item->parent());
576             m_transcodeAction->setEnabled(false);
577         } else if (m_listView->currentItem()->type() == PROJECTCLIPTYPE) {
578             clip = static_cast <ProjectItem*>(item);
579             // Display relevant transcoding actions only
580             adjustTranscodeActions(clip);
581         } else m_transcodeAction->setEnabled(false);
582         if (clip && clip->clipType() == IMAGE && !KdenliveSettings::defaultimageapp().isEmpty()) {
583             m_openAction->setIcon(KIcon(KdenliveSettings::defaultimageapp()));
584             m_openAction->setEnabled(true);
585         } else if (clip && clip->clipType() == AUDIO && !KdenliveSettings::defaultaudioapp().isEmpty()) {
586             m_openAction->setIcon(KIcon(KdenliveSettings::defaultaudioapp()));
587             m_openAction->setEnabled(true);
588         } else m_openAction->setEnabled(false);
589
590     } else m_openAction->setEnabled(false);
591     m_menu->popup(pos);
592 }
593
594 void ProjectList::slotRemoveClip()
595 {
596     if (!m_listView->currentItem()) return;
597     QStringList ids;
598     QMap <QString, QString> folderids;
599     QList<QTreeWidgetItem *> selected = m_listView->selectedItems();
600
601     QUndoCommand *delCommand = new QUndoCommand();
602     delCommand->setText(i18n("Delete Clip Zone"));
603
604     for (int i = 0; i < selected.count(); i++) {
605         if (selected.at(i)->type() == PROJECTSUBCLIPTYPE) {
606             // subitem
607             SubProjectItem *sub = static_cast <SubProjectItem *>(selected.at(i));
608             ProjectItem *item = static_cast <ProjectItem *>(sub->parent());
609             new AddClipCutCommand(this, item->clipId(), sub->zone().x(), sub->zone().y(), sub->description(), false, true, delCommand);
610         } else if (selected.at(i)->type() == PROJECTFOLDERTYPE) {
611             // folder
612             FolderProjectItem *folder = static_cast <FolderProjectItem *>(selected.at(i));
613             folderids[folder->groupName()] = folder->clipId();
614             int children = folder->childCount();
615
616             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;
617             for (int i = 0; i < children; ++i) {
618                 ProjectItem *child = static_cast <ProjectItem *>(folder->child(i));
619                 ids << child->clipId();
620             }
621         } else {
622             ProjectItem *item = static_cast <ProjectItem *>(selected.at(i));
623             ids << item->clipId();
624             if (item->numReferences() > 0) {
625                 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;
626             }
627         }
628     }
629
630     if (delCommand->childCount() == 0) delete delCommand;
631     else m_commandStack->push(delCommand);
632     emit deleteProjectClips(ids, folderids);
633 }
634
635 void ProjectList::updateButtons() const
636 {
637     if (m_listView->topLevelItemCount() == 0) {
638         m_deleteAction->setEnabled(false);
639     } else {
640         m_deleteAction->setEnabled(true);
641         if (!m_listView->currentItem()) m_listView->setCurrentItem(m_listView->topLevelItem(0));
642         QTreeWidgetItem *item = m_listView->currentItem();
643         if (item && item->type() == PROJECTCLIPTYPE) {
644             m_editAction->setEnabled(true);
645             m_openAction->setEnabled(true);
646             m_reloadAction->setEnabled(true);
647             m_transcodeAction->setEnabled(true);
648             return;
649         }
650     }
651
652     m_editAction->setEnabled(false);
653     m_openAction->setEnabled(false);
654     m_reloadAction->setEnabled(false);
655     m_transcodeAction->setEnabled(false);
656 }
657
658 void ProjectList::selectItemById(const QString &clipId)
659 {
660     ProjectItem *item = getItemById(clipId);
661     if (item) m_listView->setCurrentItem(item);
662 }
663
664
665 void ProjectList::slotDeleteClip(const QString &clipId)
666 {
667     ProjectItem *item = getItemById(clipId);
668     if (!item) {
669         kDebug() << "/// Cannot find clip to delete";
670         return;
671     }
672     m_listView->blockSignals(true);
673     QTreeWidgetItem *newSelectedItem = m_listView->itemAbove(item);
674     if (!newSelectedItem) newSelectedItem = m_listView->itemBelow(item);
675     delete item;
676     m_doc->clipManager()->deleteClip(clipId);
677     m_listView->blockSignals(false);
678     if (newSelectedItem) m_listView->setCurrentItem(newSelectedItem);
679     else updateButtons();
680 }
681
682
683 void ProjectList::editFolder(const QString folderName, const QString oldfolderName, const QString &clipId)
684 {
685     EditFolderCommand *command = new EditFolderCommand(this, folderName, oldfolderName, clipId, false);
686     m_commandStack->push(command);
687     m_doc->setModified(true);
688 }
689
690 void ProjectList::slotAddFolder()
691 {
692     AddFolderCommand *command = new AddFolderCommand(this, i18n("Folder"), QString::number(m_doc->clipManager()->getFreeFolderId()), true);
693     m_commandStack->push(command);
694 }
695
696 void ProjectList::slotAddFolder(const QString foldername, const QString &clipId, bool remove, bool edit)
697 {
698     if (remove) {
699         FolderProjectItem *item = getFolderItemById(clipId);
700         if (item) {
701             m_doc->clipManager()->deleteFolder(clipId);
702             QTreeWidgetItem *newSelectedItem = m_listView->itemAbove(item);
703             if (!newSelectedItem) newSelectedItem = m_listView->itemBelow(item);
704             delete item;
705             if (newSelectedItem) m_listView->setCurrentItem(newSelectedItem);
706             else updateButtons();
707         }
708     } else {
709         if (edit) {
710             FolderProjectItem *item = getFolderItemById(clipId);
711             if (item) {
712                 m_listView->blockSignals(true);
713                 item->setGroupName(foldername);
714                 m_listView->blockSignals(false);
715                 m_doc->clipManager()->addFolder(clipId, foldername);
716                 const int children = item->childCount();
717                 for (int i = 0; i < children; i++) {
718                     ProjectItem *child = static_cast <ProjectItem *>(item->child(i));
719                     child->setProperty("groupname", foldername);
720                 }
721             }
722         } else {
723             QStringList text;
724             text << foldername;
725             m_listView->blockSignals(true);
726             m_listView->setCurrentItem(new FolderProjectItem(m_listView, text, clipId));
727             m_doc->clipManager()->addFolder(clipId, foldername);
728             m_listView->blockSignals(false);
729         }
730         updateButtons();
731     }
732     m_doc->setModified(true);
733 }
734
735
736
737 void ProjectList::deleteProjectFolder(QMap <QString, QString> map)
738 {
739     QMapIterator<QString, QString> i(map);
740     QUndoCommand *delCommand = new QUndoCommand();
741     delCommand->setText(i18n("Delete Folder"));
742     while (i.hasNext()) {
743         i.next();
744         new AddFolderCommand(this, i.key(), i.value(), false, delCommand);
745     }
746     m_commandStack->push(delCommand);
747 }
748
749 void ProjectList::slotAddClip(DocClipBase *clip, bool getProperties)
750 {
751     m_listView->setEnabled(false);
752     if (getProperties) {
753         m_listView->blockSignals(true);
754         m_refreshed = false;
755         // remove file_hash so that we load all properties for the clip
756         QDomElement e = clip->toXML().cloneNode().toElement();
757         e.removeAttribute("file_hash");
758         m_infoQueue.insert(clip->getId(), e);
759         //m_render->getFileProperties(clip->toXML(), clip->getId(), true);
760     }
761     const QString parent = clip->getProperty("groupid");
762     ProjectItem *item = NULL;
763     if (!parent.isEmpty()) {
764         FolderProjectItem *parentitem = getFolderItemById(parent);
765         if (!parentitem) {
766             QStringList text;
767             QString groupName = clip->getProperty("groupname");
768             //kDebug() << "Adding clip to new group: " << groupName;
769             if (groupName.isEmpty()) groupName = i18n("Folder");
770             text << groupName;
771             parentitem = new FolderProjectItem(m_listView, text, parent);
772         } else {
773             //kDebug() << "Adding clip to existing group: " << parentitem->groupName();
774         }
775         if (parentitem) item = new ProjectItem(parentitem, clip);
776     }
777     if (item == NULL) item = new ProjectItem(m_listView, clip);
778     KUrl url = clip->fileURL();
779
780     if (getProperties == false && !clip->getClipHash().isEmpty()) {
781         QString cachedPixmap = m_doc->projectFolder().path(KUrl::AddTrailingSlash) + "thumbs/" + clip->getClipHash() + ".png";
782         if (QFile::exists(cachedPixmap)) {
783             QPixmap pix(cachedPixmap);
784             if (pix.isNull()) KIO::NetAccess::del(KUrl(cachedPixmap), this);
785             item->setData(0, Qt::DecorationRole, pix);
786         }
787     }
788 #ifdef NEPOMUK
789     if (!url.isEmpty() && KdenliveSettings::activate_nepomuk()) {
790         // if file has Nepomuk comment, use it
791         Nepomuk::Resource f(url.path());
792         QString annotation = f.description();
793         if (!annotation.isEmpty()) item->setText(1, annotation);
794         item->setText(2, QString::number(f.rating()));
795     }
796 #endif
797     // Add cut zones
798     QList <CutZoneInfo> cuts = clip->cutZones();
799     if (!cuts.isEmpty()) {
800         for (int i = 0; i < cuts.count(); i++) {
801             SubProjectItem *sub = new SubProjectItem(item, cuts.at(i).zone.x(), cuts.at(i).zone.y(), cuts.at(i).description);
802             if (!clip->getClipHash().isEmpty()) {
803                 QString cachedPixmap = m_doc->projectFolder().path(KUrl::AddTrailingSlash) + "thumbs/" + clip->getClipHash() + '#' + QString::number(cuts.at(i).zone.x()) + ".png";
804                 if (QFile::exists(cachedPixmap)) {
805                     QPixmap pix(cachedPixmap);
806                     if (pix.isNull()) KIO::NetAccess::del(KUrl(cachedPixmap), this);
807                     sub->setData(0, Qt::DecorationRole, pix);
808                 }
809             }
810         }
811     }
812     if (m_listView->isEnabled()) {
813         updateButtons();
814         if (getProperties) m_listView->blockSignals(false);
815     }
816     if (getProperties && !m_queueTimer.isActive()) m_queueTimer.start();
817 }
818
819 void ProjectList::slotResetProjectList()
820 {
821     m_listView->clear();
822     emit clipSelected(NULL);
823     m_thumbnailQueue.clear();
824     m_infoQueue.clear();
825     m_refreshed = false;
826 }
827
828 void ProjectList::requestClipInfo(const QDomElement xml, const QString id)
829 {
830     m_refreshed = false;
831     m_infoQueue.insert(id, xml);
832     //if (m_infoQueue.count() == 1 || ) QTimer::singleShot(300, this, SLOT(slotProcessNextClipInQueue()));
833 }
834
835 void ProjectList::slotProcessNextClipInQueue()
836 {
837     if (m_infoQueue.isEmpty()) {
838         slotProcessNextThumbnail();
839         return;
840     }
841
842     QMap<QString, QDomElement>::const_iterator j = m_infoQueue.constBegin();
843     if (j != m_infoQueue.constEnd()) {
844         const QDomElement dom = j.value();
845         const QString id = j.key();
846         m_infoQueue.remove(j.key());
847         emit getFileProperties(dom, id, m_listView->iconSize().height(), false);
848     }
849 }
850
851 void ProjectList::slotUpdateClip(const QString &id)
852 {
853     ProjectItem *item = getItemById(id);
854     m_listView->blockSignals(true);
855     if (item) item->setData(0, UsageRole, QString::number(item->numReferences()));
856     m_listView->blockSignals(false);
857 }
858
859 void ProjectList::updateAllClips()
860 {
861     m_listView->setSortingEnabled(false);
862     kDebug() << "// UPDATE ALL CLPY";
863
864     QTreeWidgetItemIterator it(m_listView);
865     DocClipBase *clip;
866     ProjectItem *item;
867     m_listView->blockSignals(true);
868     while (*it) {
869         if ((*it)->type() == PROJECTSUBCLIPTYPE) {
870             // subitem
871             SubProjectItem *sub = static_cast <SubProjectItem *>(*it);
872             if (sub->data(0, Qt::DecorationRole).isNull()) {
873                 item = static_cast <ProjectItem *>((*it)->parent());
874                 requestClipThumbnail(item->clipId() + '#' + QString::number(sub->zone().x()));
875             }
876             ++it;
877             continue;
878         } else if ((*it)->type() == PROJECTFOLDERTYPE) {
879             // folder
880             ++it;
881             continue;
882         } else {
883             item = static_cast <ProjectItem *>(*it);
884             clip = item->referencedClip();
885             if (item->referencedClip()->producer() == NULL) {
886                 if (clip->isPlaceHolder() == false) {
887                     requestClipInfo(clip->toXML(), clip->getId());
888                 } else if (!clip->isPlaceHolder()) item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
889             } else {
890                 if (item->data(0, Qt::DecorationRole).isNull()) {
891                     requestClipThumbnail(clip->getId());
892                 }
893                 if (item->data(0, DurationRole).toString().isEmpty()) {
894                     item->changeDuration(item->referencedClip()->producer()->get_playtime());
895                 }
896             }
897             item->setData(0, UsageRole, QString::number(item->numReferences()));
898         }
899         //qApp->processEvents();
900         ++it;
901     }
902     if (!m_queueTimer.isActive()) m_queueTimer.start();
903     if (m_listView->isEnabled()) m_listView->blockSignals(false);
904     m_listView->setSortingEnabled(true);
905     if (m_infoQueue.isEmpty()) slotProcessNextThumbnail();
906 }
907
908 void ProjectList::slotAddClip(const QList <QUrl> givenList, const QString &groupName, const QString &groupId)
909 {
910     if (!m_commandStack) {
911         kDebug() << "!!!!!!!!!!!!!!!! NO CMD STK";
912     }
913     KUrl::List list;
914     if (givenList.isEmpty()) {
915         // Build list of mime types
916         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";
917
918         QString allExtensions;
919         foreach(const QString& mimeType, mimeTypes) {
920             KMimeType::Ptr mime(KMimeType::mimeType(mimeType));
921             if (mime) {
922                 allExtensions.append(mime->patterns().join(" "));
923                 allExtensions.append(' ');
924             }
925         }
926         const QString dialogFilter = allExtensions.simplified() + ' ' + QLatin1Char('|') + i18n("All Supported Files") + "\n* " + QLatin1Char('|') + i18n("All Files");
927         list = KFileDialog::getOpenUrls(KUrl("kfiledialog:///clipfolder"), dialogFilter, this);
928
929     } else {
930         for (int i = 0; i < givenList.count(); i++)
931             list << givenList.at(i);
932     }
933
934     foreach(const KUrl &file, list) {
935         // Check there is no folder here
936         KMimeType::Ptr type = KMimeType::findByUrl(file);
937         if (type->is("inode/directory")) {
938             // user dropped a folder
939             list.removeAll(file);
940         }
941     }
942
943     if (list.isEmpty()) return;
944
945     if (givenList.isEmpty()) {
946         QStringList groupInfo = getGroup();
947         m_doc->slotAddClipList(list, groupInfo.at(0), groupInfo.at(1));
948     } else m_doc->slotAddClipList(list, groupName, groupId);
949 }
950
951 void ProjectList::slotRemoveInvalidClip(const QString &id, bool replace)
952 {
953     ProjectItem *item = getItemById(id);
954     QTimer::singleShot(300, this, SLOT(slotProcessNextClipInQueue()));
955     if (item) {
956         const QString path = item->referencedClip()->fileURL().path();
957         if (item->referencedClip()->isPlaceHolder()) replace = false;
958         if (!path.isEmpty()) {
959             if (replace) KMessageBox::sorry(this, i18n("Clip <b>%1</b><br>is invalid, will be removed from project.", path));
960             else {
961                 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;
962             }
963         }
964         QStringList ids;
965         ids << id;
966         if (replace) emit deleteProjectClips(ids, QMap <QString, QString>());
967     }
968 }
969
970 void ProjectList::slotAddColorClip()
971 {
972     if (!m_commandStack) {
973         kDebug() << "!!!!!!!!!!!!!!!! NO CMD STK";
974     }
975     QDialog *dia = new QDialog(this);
976     Ui::ColorClip_UI dia_ui;
977     dia_ui.setupUi(dia);
978     dia->setWindowTitle(i18n("Color Clip"));
979     dia_ui.clip_name->setText(i18n("Color Clip"));
980     dia_ui.clip_duration->setText(KdenliveSettings::color_duration());
981     if (dia->exec() == QDialog::Accepted) {
982         QString color = dia_ui.clip_color->color().name();
983         color = color.replace(0, 1, "0x") + "ff";
984         QStringList groupInfo = getGroup();
985         m_doc->slotCreateColorClip(dia_ui.clip_name->text(), color, dia_ui.clip_duration->text(), groupInfo.at(0), groupInfo.at(1));
986     }
987     delete dia;
988 }
989
990
991 void ProjectList::slotAddSlideshowClip()
992 {
993     if (!m_commandStack) {
994         kDebug() << "!!!!!!!!!!!!!!!! NO CMD STK";
995     }
996     SlideshowClip *dia = new SlideshowClip(m_timecode, this);
997
998     if (dia->exec() == QDialog::Accepted) {
999         QStringList groupInfo = getGroup();
1000         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));
1001     }
1002     delete dia;
1003 }
1004
1005 void ProjectList::slotAddTitleClip()
1006 {
1007     QStringList groupInfo = getGroup();
1008     m_doc->slotCreateTextClip(groupInfo.at(0), groupInfo.at(1));
1009 }
1010
1011 void ProjectList::slotAddTitleTemplateClip()
1012 {
1013     QStringList groupInfo = getGroup();
1014     if (!m_commandStack) {
1015         kDebug() << "!!!!!!!!!!!!!!!! NO CMD STK";
1016     }
1017
1018     // Get the list of existing templates
1019     QStringList filter;
1020     filter << "*.kdenlivetitle";
1021     const QString path = m_doc->projectFolder().path(KUrl::AddTrailingSlash) + "titles/";
1022     QStringList templateFiles = QDir(path).entryList(filter, QDir::Files);
1023
1024     QDialog *dia = new QDialog(this);
1025     Ui::TemplateClip_UI dia_ui;
1026     dia_ui.setupUi(dia);
1027     for (int i = 0; i < templateFiles.size(); ++i) {
1028         dia_ui.template_list->comboBox()->addItem(templateFiles.at(i), path + templateFiles.at(i));
1029     }
1030     dia_ui.template_list->fileDialog()->setFilter("*.kdenlivetitle");
1031     //warning: setting base directory doesn't work??
1032     KUrl startDir(path);
1033     dia_ui.template_list->fileDialog()->setUrl(startDir);
1034     dia_ui.text_box->setHidden(true);
1035     if (dia->exec() == QDialog::Accepted) {
1036         QString textTemplate = dia_ui.template_list->comboBox()->itemData(dia_ui.template_list->comboBox()->currentIndex()).toString();
1037         if (textTemplate.isEmpty()) textTemplate = dia_ui.template_list->comboBox()->currentText();
1038         // Create a cloned template clip
1039         m_doc->slotCreateTextTemplateClip(groupInfo.at(0), groupInfo.at(1), KUrl(textTemplate));
1040     }
1041     delete dia;
1042 }
1043
1044 QStringList ProjectList::getGroup() const
1045 {
1046     QStringList result;
1047     QTreeWidgetItem *item = m_listView->currentItem();
1048     while (item && item->type() != PROJECTFOLDERTYPE) {
1049         item = item->parent();
1050     }
1051
1052     if (item) {
1053         FolderProjectItem *folder = static_cast <FolderProjectItem *>(item);
1054         result << folder->groupName();
1055         result << folder->clipId();
1056     } else result << QString() << QString();
1057     return result;
1058 }
1059
1060 void ProjectList::setDocument(KdenliveDoc *doc)
1061 {
1062     m_listView->blockSignals(true);
1063     m_listView->clear();
1064     m_listView->setSortingEnabled(false);
1065     emit clipSelected(NULL);
1066     m_thumbnailQueue.clear();
1067     m_infoQueue.clear();
1068     m_refreshed = false;
1069     m_fps = doc->fps();
1070     m_timecode = doc->timecode();
1071     m_commandStack = doc->commandStack();
1072     m_doc = doc;
1073
1074     QMap <QString, QString> flist = doc->clipManager()->documentFolderList();
1075     QMapIterator<QString, QString> f(flist);
1076     while (f.hasNext()) {
1077         f.next();
1078         (void) new FolderProjectItem(m_listView, QStringList() << f.value(), f.key());
1079     }
1080
1081     QList <DocClipBase*> list = doc->clipManager()->documentClipList();
1082     for (int i = 0; i < list.count(); i++) {
1083         slotAddClip(list.at(i), false);
1084     }
1085
1086     m_listView->blockSignals(false);
1087     m_toolbar->setEnabled(true);
1088     connect(m_doc->clipManager(), SIGNAL(reloadClip(const QString &)), this, SLOT(slotReloadClip(const QString &)));
1089     connect(m_doc->clipManager(), SIGNAL(checkAllClips()), this, SLOT(updateAllClips()));
1090 }
1091
1092 QList <DocClipBase*> ProjectList::documentClipList() const
1093 {
1094     if (m_doc == NULL) return QList <DocClipBase*> ();
1095     return m_doc->clipManager()->documentClipList();
1096 }
1097
1098 QDomElement ProjectList::producersList()
1099 {
1100     QDomDocument doc;
1101     QDomElement prods = doc.createElement("producerlist");
1102     doc.appendChild(prods);
1103     kDebug() << "////////////  PRO LIST BUILD PRDSLIST ";
1104     QTreeWidgetItemIterator it(m_listView);
1105     while (*it) {
1106         if ((*it)->type() != PROJECTCLIPTYPE) {
1107             // subitem
1108             ++it;
1109             continue;
1110         }
1111         prods.appendChild(doc.importNode(((ProjectItem *)(*it))->toXml(), true));
1112         ++it;
1113     }
1114     return prods;
1115 }
1116
1117 void ProjectList::slotCheckForEmptyQueue()
1118 {
1119     if (!m_refreshed && m_thumbnailQueue.isEmpty() && m_infoQueue.isEmpty()) {
1120         m_refreshed = true;
1121         emit loadingIsOver();
1122         emit displayMessage(QString(), -1);
1123         m_listView->blockSignals(false);
1124         m_listView->setEnabled(true);
1125         updateButtons();
1126     } else if (!m_refreshed) QTimer::singleShot(300, this, SLOT(slotCheckForEmptyQueue()));
1127 }
1128
1129 void ProjectList::reloadClipThumbnails()
1130 {
1131     kDebug() << "//////////////  RELOAD CLIPS THUMBNAILS!!!";
1132     m_thumbnailQueue.clear();
1133     QTreeWidgetItemIterator it(m_listView);
1134     while (*it) {
1135         if ((*it)->type() != PROJECTCLIPTYPE) {
1136             // subitem
1137             ++it;
1138             continue;
1139         }
1140         m_thumbnailQueue << ((ProjectItem *)(*it))->clipId();
1141         ++it;
1142     }
1143     QTimer::singleShot(300, this, SLOT(slotProcessNextThumbnail()));
1144 }
1145
1146 void ProjectList::requestClipThumbnail(const QString id)
1147 {
1148     if (!m_thumbnailQueue.contains(id)) m_thumbnailQueue.append(id);
1149 }
1150
1151 void ProjectList::slotProcessNextThumbnail()
1152 {
1153     if (m_thumbnailQueue.isEmpty() && m_infoQueue.isEmpty()) {
1154         slotCheckForEmptyQueue();
1155         return;
1156     }
1157     if (!m_infoQueue.isEmpty()) {
1158         //QTimer::singleShot(300, this, SLOT(slotProcessNextThumbnail()));
1159         return;
1160     }
1161     if (m_thumbnailQueue.count() > 1) {
1162         int max = m_doc->clipManager()->clipsCount();
1163         emit displayMessage(i18n("Loading thumbnails"), (int)(100 *(max - m_thumbnailQueue.count()) / max));
1164     }
1165     slotRefreshClipThumbnail(m_thumbnailQueue.takeFirst(), false);
1166 }
1167
1168 void ProjectList::slotRefreshClipThumbnail(const QString &clipId, bool update)
1169 {
1170     QTreeWidgetItem *item = getAnyItemById(clipId);
1171     if (item) slotRefreshClipThumbnail(item, update);
1172     else slotProcessNextThumbnail();
1173 }
1174
1175 void ProjectList::slotRefreshClipThumbnail(QTreeWidgetItem *it, bool update)
1176 {
1177     if (it == NULL) return;
1178     ProjectItem *item = NULL;
1179     bool isSubItem = false;
1180     int frame;
1181     if (it->type() == PROJECTFOLDERTYPE) return;
1182     if (it->type() == PROJECTSUBCLIPTYPE) {
1183         item = static_cast <ProjectItem *>(it->parent());
1184         frame = static_cast <SubProjectItem *>(it)->zone().x();
1185         isSubItem = true;
1186     } else {
1187         item = static_cast <ProjectItem *>(it);
1188         frame = item->referencedClip()->getClipThumbFrame();
1189     }
1190
1191     if (item) {
1192         DocClipBase *clip = item->referencedClip();
1193         if (!clip) {
1194             slotProcessNextThumbnail();
1195             return;
1196         }
1197         QPixmap pix;
1198         int height = m_listView->iconSize().height();
1199         int width = (int)(height  * m_render->dar());
1200         if (clip->clipType() == AUDIO) pix = KIcon("audio-x-generic").pixmap(QSize(width, height));
1201         else if (clip->clipType() == IMAGE) pix = QPixmap::fromImage(KThumb::getFrame(item->referencedClip()->producer(), 0, width, height));
1202         else pix = item->referencedClip()->thumbProducer()->extractImage(frame, width, height);
1203
1204         if (!pix.isNull()) {
1205             m_listView->blockSignals(true);
1206             it->setData(0, Qt::DecorationRole, pix);
1207             if (m_listView->isEnabled()) m_listView->blockSignals(false);
1208             if (!isSubItem) m_doc->cachePixmap(item->getClipHash(), pix);
1209             else m_doc->cachePixmap(item->getClipHash() + '#' + QString::number(frame), pix);
1210         }
1211         if (update) emit projectModified();
1212         QTimer::singleShot(30, this, SLOT(slotProcessNextThumbnail()));
1213     }
1214 }
1215
1216 void ProjectList::slotReplyGetFileProperties(const QString &clipId, Mlt::Producer *producer, const QMap < QString, QString > &properties, const QMap < QString, QString > &metadata, bool replace)
1217 {
1218     QString toReload;
1219     ProjectItem *item = getItemById(clipId);
1220     if (item && producer) {
1221         m_listView->blockSignals(true);
1222         item->setProperties(properties, metadata);
1223         if (item->referencedClip()->isPlaceHolder() && producer->is_valid()) {
1224             item->referencedClip()->setValid();
1225             item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsDragEnabled | Qt::ItemIsEnabled | Qt::ItemIsEditable);
1226             toReload = clipId;
1227         }
1228         //Q_ASSERT_X(item->referencedClip(), "void ProjectList::slotReplyGetFileProperties", QString("Item with groupName %1 does not have a clip associated").arg(item->groupName()).toLatin1());
1229         item->referencedClip()->setProducer(producer, replace);
1230         if (!replace && item->data(0, Qt::DecorationRole).isNull()) {
1231             requestClipThumbnail(clipId);
1232         }
1233         //emit receivedClipDuration(clipId);
1234         if (m_listView->isEnabled() && replace) {
1235             // update clip in clip monitor
1236             emit clipSelected(NULL);
1237             emit clipSelected(item->referencedClip());
1238         }
1239         /*else {
1240             // Check if duration changed.
1241             emit receivedClipDuration(clipId);
1242             delete producer;
1243         }*/
1244         if (m_listView->isEnabled()) m_listView->blockSignals(false);
1245         /*if (item->icon(0).isNull()) {
1246             requestClipThumbnail(clipId);
1247         }*/
1248     } else kDebug() << "////////  COULD NOT FIND CLIP TO UPDATE PRPS...";
1249     int max = m_doc->clipManager()->clipsCount();
1250     emit displayMessage(i18n("Loading clips"), (int)(100 *(max - m_infoQueue.count()) / max));
1251     // small delay so that the app can display the progress info
1252     if (item && m_infoQueue.isEmpty() && m_thumbnailQueue.isEmpty()) {
1253         m_listView->setCurrentItem(item);
1254         emit clipSelected(item->referencedClip());
1255     }
1256     if (!toReload.isEmpty()) emit clipNeedsReload(toReload, true);
1257     QTimer::singleShot(30, this, SLOT(slotProcessNextClipInQueue()));
1258 }
1259
1260 void ProjectList::slotReplyGetImage(const QString &clipId, const QPixmap &pix)
1261 {
1262     ProjectItem *item = getItemById(clipId);
1263     if (item && !pix.isNull()) {
1264         m_listView->blockSignals(true);
1265         item->setData(0, Qt::DecorationRole, pix);
1266         m_doc->cachePixmap(item->getClipHash(), pix);
1267         if (m_listView->isEnabled()) m_listView->blockSignals(false);
1268     }
1269 }
1270
1271 QTreeWidgetItem *ProjectList::getAnyItemById(const QString &id)
1272 {
1273     QTreeWidgetItemIterator it(m_listView);
1274     QString lookId = id;
1275     if (id.contains('#')) {
1276         lookId = id.section('#', 0, 0);
1277     }
1278
1279     ProjectItem *result = NULL;
1280     while (*it) {
1281         if ((*it)->type() != PROJECTCLIPTYPE) {
1282             // subitem
1283             ++it;
1284             continue;
1285         }
1286         ProjectItem *item = static_cast<ProjectItem *>(*it);
1287         if (item->clipId() == lookId) {
1288             result = item;
1289             break;
1290         }
1291         ++it;
1292     }
1293     if (result == NULL || !id.contains('#')) return result;
1294     else for (int i = 0; i < result->childCount(); i++) {
1295             SubProjectItem *sub = static_cast <SubProjectItem *>(result->child(i));
1296             if (sub && sub->zone().x() == id.section('#', 1, 1).toInt()) {
1297                 return sub;
1298             }
1299         }
1300
1301     return NULL;
1302 }
1303
1304
1305 ProjectItem *ProjectList::getItemById(const QString &id)
1306 {
1307     ProjectItem *item;
1308     QTreeWidgetItemIterator it(m_listView);
1309     while (*it) {
1310         if ((*it)->type() != PROJECTCLIPTYPE) {
1311             // subitem
1312             ++it;
1313             continue;
1314         }
1315         item = static_cast<ProjectItem *>(*it);
1316         if (item->clipId() == id)
1317             return item;
1318         ++it;
1319     }
1320     return NULL;
1321 }
1322
1323 FolderProjectItem *ProjectList::getFolderItemById(const QString &id)
1324 {
1325     FolderProjectItem *item;
1326     QTreeWidgetItemIterator it(m_listView);
1327     while (*it) {
1328         if ((*it)->type() == PROJECTFOLDERTYPE) {
1329             item = static_cast<FolderProjectItem *>(*it);
1330             if (item->clipId() == id) return item;
1331         }
1332         ++it;
1333     }
1334     return NULL;
1335 }
1336
1337 void ProjectList::slotSelectClip(const QString &ix)
1338 {
1339     ProjectItem *clip = getItemById(ix);
1340     if (clip) {
1341         m_listView->setCurrentItem(clip);
1342         m_listView->scrollToItem(clip);
1343         m_editAction->setEnabled(true);
1344         m_deleteAction->setEnabled(true);
1345         m_reloadAction->setEnabled(true);
1346         m_transcodeAction->setEnabled(true);
1347         if (clip->clipType() == IMAGE && !KdenliveSettings::defaultimageapp().isEmpty()) {
1348             m_openAction->setIcon(KIcon(KdenliveSettings::defaultimageapp()));
1349             m_openAction->setEnabled(true);
1350         } else if (clip->clipType() == AUDIO && !KdenliveSettings::defaultaudioapp().isEmpty()) {
1351             m_openAction->setIcon(KIcon(KdenliveSettings::defaultaudioapp()));
1352             m_openAction->setEnabled(true);
1353         } else m_openAction->setEnabled(false);
1354     }
1355 }
1356
1357 QString ProjectList::currentClipUrl() const
1358 {
1359     ProjectItem *item;
1360     if (!m_listView->currentItem() || m_listView->currentItem()->type() == PROJECTFOLDERTYPE) return QString();
1361     if (m_listView->currentItem()->type() == PROJECTSUBCLIPTYPE) {
1362         // subitem
1363         item = static_cast <ProjectItem*>(m_listView->currentItem()->parent());
1364     } else item = static_cast <ProjectItem*>(m_listView->currentItem());
1365     if (item == NULL) return QString();
1366     return item->clipUrl().path();
1367 }
1368
1369 KUrl::List ProjectList::getConditionalUrls(const QString &condition) const
1370 {
1371     KUrl::List result;
1372     ProjectItem *item;
1373     QList<QTreeWidgetItem *> list = m_listView->selectedItems();
1374     for (int i = 0; i < list.count(); i++) {
1375         if (list.at(i)->type() == PROJECTFOLDERTYPE) continue;
1376         if (list.at(i)->type() == PROJECTSUBCLIPTYPE) {
1377             // subitem
1378             item = static_cast <ProjectItem*>(list.at(i)->parent());
1379         } else item = static_cast <ProjectItem*>(list.at(i));
1380         if (item == NULL) continue;
1381         if (item->type() == COLOR || item->type() == SLIDESHOW || item->type() == TEXT) continue;
1382         DocClipBase *clip = item->referencedClip();
1383         if (!condition.isEmpty()) {
1384             if (condition.startsWith("vcodec") && !clip->hasVideoCodec(condition.section("=", 1, 1))) continue;
1385             else if (condition.startsWith("acodec") && !clip->hasAudioCodec(condition.section("=", 1, 1))) continue;
1386         }
1387         result.append(item->clipUrl());
1388     }
1389     return result;
1390 }
1391
1392 void ProjectList::regenerateTemplate(const QString &id)
1393 {
1394     ProjectItem *clip = getItemById(id);
1395     if (clip) regenerateTemplate(clip);
1396 }
1397
1398 void ProjectList::regenerateTemplate(ProjectItem *clip)
1399 {
1400     //TODO: remove this unused method, only force_reload is necessary
1401     clip->referencedClip()->producer()->set("force_reload", 1);
1402 }
1403
1404 QDomDocument ProjectList::generateTemplateXml(QString path, const QString &replaceString)
1405 {
1406     QDomDocument doc;
1407     QFile file(path);
1408     if (!file.open(QIODevice::ReadOnly)) {
1409         kWarning() << "ERROR, CANNOT READ: " << path;
1410         return doc;
1411     }
1412     if (!doc.setContent(&file)) {
1413         kWarning() << "ERROR, CANNOT READ: " << path;
1414         file.close();
1415         return doc;
1416     }
1417     file.close();
1418     QDomNodeList texts = doc.elementsByTagName("content");
1419     for (int i = 0; i < texts.count(); i++) {
1420         QString data = texts.item(i).firstChild().nodeValue();
1421         data.replace("%s", replaceString);
1422         texts.item(i).firstChild().setNodeValue(data);
1423     }
1424     return doc;
1425 }
1426
1427
1428 void ProjectList::slotAddClipCut(const QString &id, int in, int out)
1429 {
1430     ProjectItem *clip = getItemById(id);
1431     if (clip == NULL) return;
1432     if (clip->referencedClip()->hasCutZone(QPoint(in, out))) return;
1433     AddClipCutCommand *command = new AddClipCutCommand(this, id, in, out, QString(), true, false);
1434     m_commandStack->push(command);
1435 }
1436
1437 void ProjectList::addClipCut(const QString &id, int in, int out, const QString desc, bool newItem)
1438 {
1439     ProjectItem *clip = getItemById(id);
1440     if (clip) {
1441         DocClipBase *base = clip->referencedClip();
1442         base->addCutZone(in, out);
1443         m_listView->blockSignals(true);
1444         SubProjectItem *sub = new SubProjectItem(clip, in, out, desc);
1445         if (newItem && desc.isEmpty() && !m_listView->isColumnHidden(1)) {
1446             if (!clip->isExpanded()) clip->setExpanded(true);
1447             m_listView->editItem(sub, 1);
1448         }
1449         QPixmap p = clip->referencedClip()->thumbProducer()->extractImage(in, (int)(sub->sizeHint(0).height()  * m_render->dar()), sub->sizeHint(0).height() - 2);
1450         sub->setData(0, Qt::DecorationRole, p);
1451         m_doc->cachePixmap(clip->getClipHash() + '#' + QString::number(in), p);
1452         m_listView->blockSignals(false);
1453     }
1454     emit projectModified();
1455 }
1456
1457 void ProjectList::removeClipCut(const QString &id, int in, int out)
1458 {
1459     ProjectItem *clip = getItemById(id);
1460     if (clip) {
1461         DocClipBase *base = clip->referencedClip();
1462         base->removeCutZone(in, out);
1463         SubProjectItem *sub = getSubItem(clip, QPoint(in, out));
1464         if (sub) {
1465             m_listView->blockSignals(true);
1466             delete sub;
1467             m_listView->blockSignals(false);
1468         }
1469     }
1470     emit projectModified();
1471 }
1472
1473 SubProjectItem *ProjectList::getSubItem(ProjectItem *clip, QPoint zone)
1474 {
1475     SubProjectItem *sub = NULL;
1476     if (clip) {
1477         for (int i = 0; i < clip->childCount(); i++) {
1478             QTreeWidgetItem *it = clip->child(i);
1479             if (it->type() == PROJECTSUBCLIPTYPE) {
1480                 sub = static_cast <SubProjectItem*>(it);
1481                 if (sub->zone() == zone) break;
1482                 else sub = NULL;
1483             }
1484         }
1485     }
1486     return sub;
1487 }
1488
1489 void ProjectList::slotUpdateClipCut(QPoint p)
1490 {
1491     if (!m_listView->currentItem() || m_listView->currentItem()->type() != PROJECTSUBCLIPTYPE) return;
1492     SubProjectItem *sub = static_cast <SubProjectItem*>(m_listView->currentItem());
1493     ProjectItem *item = static_cast <ProjectItem *>(sub->parent());
1494     EditClipCutCommand *command = new EditClipCutCommand(this, item->clipId(), sub->zone(), p, sub->text(1), sub->text(1), true);
1495     m_commandStack->push(command);
1496 }
1497
1498 void ProjectList::doUpdateClipCut(const QString &id, const QPoint oldzone, const QPoint zone, const QString &comment)
1499 {
1500     ProjectItem *clip = getItemById(id);
1501     SubProjectItem *sub = getSubItem(clip, oldzone);
1502     if (sub == NULL || clip == NULL) return;
1503     DocClipBase *base = clip->referencedClip();
1504     base->updateCutZone(oldzone.x(), oldzone.y(), zone.x(), zone.y(), comment);
1505     m_listView->blockSignals(true);
1506     sub->setZone(zone);
1507     sub->setDescription(comment);
1508     m_listView->blockSignals(false);
1509     emit projectModified();
1510 }
1511
1512 #include "projectlist.moc"