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