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