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