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