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