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