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