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