]> git.sesse.net Git - kdenlive/blob - src/projectlist.cpp
Add subclips (dragging a zone from clip monitor to project tree)
[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         m_listView->blockSignals(true);
648         for (int i = 0; i < cuts.count(); i++) {
649             SubProjectItem *sub = new SubProjectItem(item, cuts.at(i).x(), cuts.at(i).y());
650             if (!clip->getClipHash().isEmpty()) {
651                 QString cachedPixmap = m_doc->projectFolder().path(KUrl::AddTrailingSlash) + "thumbs/" + clip->getClipHash() + '#' + QString::number(cuts.at(i).x()) + ".png";
652                 if (QFile::exists(cachedPixmap)) {
653                     sub->setIcon(0, QPixmap(cachedPixmap));
654                 }
655             }
656         }
657         m_listView->blockSignals(false);
658     }
659
660     if (getProperties && m_listView->isEnabled()) m_listView->blockSignals(false);
661     if (getProperties && !m_queueTimer.isActive()) m_queueTimer.start();
662 }
663
664 void ProjectList::slotResetProjectList()
665 {
666     m_listView->clear();
667     emit clipSelected(NULL);
668     m_thumbnailQueue.clear();
669     m_infoQueue.clear();
670     m_refreshed = false;
671 }
672
673 void ProjectList::requestClipInfo(const QDomElement xml, const QString id)
674 {
675     m_refreshed = false;
676     m_infoQueue.insert(id, xml);
677     //if (m_infoQueue.count() == 1 || ) QTimer::singleShot(300, this, SLOT(slotProcessNextClipInQueue()));
678 }
679
680 void ProjectList::slotProcessNextClipInQueue()
681 {
682     if (m_infoQueue.isEmpty()) {
683         slotProcessNextThumbnail();
684         return;
685     }
686
687     QMap<QString, QDomElement>::const_iterator j = m_infoQueue.constBegin();
688     if (j != m_infoQueue.constEnd()) {
689         const QDomElement dom = j.value();
690         const QString id = j.key();
691         m_infoQueue.remove(j.key());
692         emit getFileProperties(dom, id, false);
693     }
694 }
695
696 void ProjectList::slotUpdateClip(const QString &id)
697 {
698     ProjectItem *item = getItemById(id);
699     m_listView->blockSignals(true);
700     if (item) item->setData(1, UsageRole, QString::number(item->numReferences()));
701     m_listView->blockSignals(false);
702 }
703
704 void ProjectList::updateAllClips()
705 {
706     m_listView->setSortingEnabled(false);
707
708     QTreeWidgetItemIterator it(m_listView);
709     DocClipBase *clip;
710     ProjectItem *item;
711     m_listView->blockSignals(true);
712     while (*it) {
713         if ((*it)->type() == QTreeWidgetItem::UserType + 1) {
714             // subitem
715             SubProjectItem *sub = static_cast <SubProjectItem *>(*it);
716             if (sub->icon(0).isNull()) {
717                 item = static_cast <ProjectItem *>((*it)->parent());
718                 requestClipThumbnail(item->clipId() + '#' + QString::number(sub->zone().x()));
719             }
720             ++it;
721             continue;
722         }
723         item = static_cast <ProjectItem *>(*it);
724         if (!item->isGroup()) {
725             clip = item->referencedClip();
726             if (item->referencedClip()->producer() == NULL) {
727                 if (clip->isPlaceHolder() == false) {
728                     requestClipInfo(clip->toXML(), clip->getId());
729                 } else item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
730             } else {
731                 if (item->icon(0).isNull()) {
732                     requestClipThumbnail(clip->getId());
733                 }
734                 if (item->data(1, DurationRole).toString().isEmpty()) {
735                     item->changeDuration(item->referencedClip()->producer()->get_playtime());
736                 }
737             }
738             item->setData(1, UsageRole, QString::number(item->numReferences()));
739             //qApp->processEvents();
740         }
741         ++it;
742     }
743     qApp->processEvents();
744     if (!m_queueTimer.isActive()) m_queueTimer.start();
745
746     if (m_listView->isEnabled()) m_listView->blockSignals(false);
747     m_listView->setSortingEnabled(true);
748     if (m_infoQueue.isEmpty()) slotProcessNextThumbnail();
749 }
750
751 void ProjectList::slotAddClip(const QList <QUrl> givenList, const QString &groupName, const QString &groupId)
752 {
753     if (!m_commandStack) {
754         kDebug() << "!!!!!!!!!!!!!!!! NO CMD STK";
755     }
756     KUrl::List list;
757     if (givenList.isEmpty()) {
758         // Build list of mime types
759         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";
760
761         QString allExtensions;
762         foreach(const QString& mimeType, mimeTypes) {
763             KMimeType::Ptr mime(KMimeType::mimeType(mimeType));
764             if (mime) {
765                 allExtensions.append(mime->patterns().join(" "));
766                 allExtensions.append(' ');
767             }
768         }
769         const QString dialogFilter = allExtensions.simplified() + ' ' + QLatin1Char('|') + i18n("All Supported Files") + "\n* " + QLatin1Char('|') + i18n("All Files");
770         list = KFileDialog::getOpenUrls(KUrl("kfiledialog:///clipfolder"), dialogFilter, this);
771
772     } else {
773         for (int i = 0; i < givenList.count(); i++)
774             list << givenList.at(i);
775     }
776     if (list.isEmpty()) return;
777
778     if (givenList.isEmpty()) {
779         QStringList groupInfo = getGroup();
780         m_doc->slotAddClipList(list, groupInfo.at(0), groupInfo.at(1));
781     } else m_doc->slotAddClipList(list, groupName, groupId);
782 }
783
784 void ProjectList::slotRemoveInvalidClip(const QString &id, bool replace)
785 {
786     ProjectItem *item = getItemById(id);
787     QTimer::singleShot(300, this, SLOT(slotProcessNextClipInQueue()));
788     if (item) {
789         const QString path = item->referencedClip()->fileURL().path();
790         if (!path.isEmpty()) {
791             if (replace) KMessageBox::sorry(this, i18n("Clip <b>%1</b><br>is invalid, will be removed from project.", path));
792             else {
793                 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;
794             }
795         }
796         QStringList ids;
797         ids << id;
798         if (replace) m_doc->deleteProjectClip(ids);
799     }
800 }
801
802 void ProjectList::slotAddColorClip()
803 {
804     if (!m_commandStack) {
805         kDebug() << "!!!!!!!!!!!!!!!! NO CMD STK";
806     }
807     QDialog *dia = new QDialog(this);
808     Ui::ColorClip_UI dia_ui;
809     dia_ui.setupUi(dia);
810     dia_ui.clip_name->setText(i18n("Color Clip"));
811     dia_ui.clip_duration->setText(KdenliveSettings::color_duration());
812     if (dia->exec() == QDialog::Accepted) {
813         QString color = dia_ui.clip_color->color().name();
814         color = color.replace(0, 1, "0x") + "ff";
815         QStringList groupInfo = getGroup();
816         m_doc->slotCreateColorClip(dia_ui.clip_name->text(), color, dia_ui.clip_duration->text(), groupInfo.at(0), groupInfo.at(1));
817     }
818     delete dia;
819 }
820
821
822 void ProjectList::slotAddSlideshowClip()
823 {
824     if (!m_commandStack) {
825         kDebug() << "!!!!!!!!!!!!!!!! NO CMD STK";
826     }
827     SlideshowClip *dia = new SlideshowClip(m_timecode, this);
828
829     if (dia->exec() == QDialog::Accepted) {
830         QStringList groupInfo = getGroup();
831         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));
832     }
833     delete dia;
834 }
835
836 void ProjectList::slotAddTitleClip()
837 {
838     QStringList groupInfo = getGroup();
839     m_doc->slotCreateTextClip(groupInfo.at(0), groupInfo.at(1));
840 }
841
842 void ProjectList::slotAddTitleTemplateClip()
843 {
844     QStringList groupInfo = getGroup();
845     if (!m_commandStack) {
846         kDebug() << "!!!!!!!!!!!!!!!! NO CMD STK";
847     }
848
849     // Get the list of existing templates
850     QStringList filter;
851     filter << "*.kdenlivetitle";
852     const QString path = m_doc->projectFolder().path(KUrl::AddTrailingSlash) + "titles/";
853     QStringList templateFiles = QDir(path).entryList(filter, QDir::Files);
854
855     QDialog *dia = new QDialog(this);
856     Ui::TemplateClip_UI dia_ui;
857     dia_ui.setupUi(dia);
858     for (int i = 0; i < templateFiles.size(); ++i) {
859         dia_ui.template_list->comboBox()->addItem(templateFiles.at(i), path + templateFiles.at(i));
860     }
861     dia_ui.template_list->fileDialog()->setFilter("*.kdenlivetitle");
862     //warning: setting base directory doesn't work??
863     KUrl startDir(path);
864     dia_ui.template_list->fileDialog()->setUrl(startDir);
865     dia_ui.text_box->setHidden(true);
866     if (dia->exec() == QDialog::Accepted) {
867         QString textTemplate = dia_ui.template_list->comboBox()->itemData(dia_ui.template_list->comboBox()->currentIndex()).toString();
868         if (textTemplate.isEmpty()) textTemplate = dia_ui.template_list->comboBox()->currentText();
869         // Create a cloned template clip
870         m_doc->slotCreateTextTemplateClip(groupInfo.at(0), groupInfo.at(1), KUrl(textTemplate));
871     }
872     delete dia;
873 }
874
875 QStringList ProjectList::getGroup() const
876 {
877     QStringList result;
878     ProjectItem *item = NULL;
879     if (m_listView->currentItem() && m_listView->currentItem()->type() == QTreeWidgetItem::UserType + 1) {
880         // sub item selected
881         item = static_cast <ProjectItem*>(m_listView->currentItem()->parent());
882     } else item = static_cast <ProjectItem*>(m_listView->currentItem());
883     if (item && !item->isGroup()) {
884         while (item->parent()) {
885             item = static_cast <ProjectItem*>(item->parent());
886             if (item->isGroup()) break;
887         }
888     }
889     if (item && item->isGroup()) {
890         result << item->groupName();
891         result << item->clipId();
892     } else result << QString() << QString();
893     return result;
894 }
895
896 void ProjectList::setDocument(KdenliveDoc *doc)
897 {
898     m_listView->blockSignals(true);
899     m_listView->clear();
900     m_listView->setSortingEnabled(false);
901     emit clipSelected(NULL);
902     m_thumbnailQueue.clear();
903     m_infoQueue.clear();
904     m_refreshed = false;
905     m_fps = doc->fps();
906     m_timecode = doc->timecode();
907     m_commandStack = doc->commandStack();
908     m_doc = doc;
909
910     QMap <QString, QString> flist = doc->clipManager()->documentFolderList();
911     QMapIterator<QString, QString> f(flist);
912     while (f.hasNext()) {
913         f.next();
914         (void) new ProjectItem(m_listView, QStringList() << QString() << f.value(), f.key());
915     }
916
917     QList <DocClipBase*> list = doc->clipManager()->documentClipList();
918     for (int i = 0; i < list.count(); i++) {
919         slotAddClip(list.at(i), false);
920     }
921
922     m_listView->blockSignals(false);
923     m_toolbar->setEnabled(true);
924     connect(m_doc->clipManager(), SIGNAL(reloadClip(const QString &)), this, SLOT(slotReloadClip(const QString &)));
925     connect(m_doc->clipManager(), SIGNAL(checkAllClips()), this, SLOT(updateAllClips()));
926 }
927
928 QList <DocClipBase*> ProjectList::documentClipList() const
929 {
930     if (m_doc == NULL) return QList <DocClipBase*> ();
931     return m_doc->clipManager()->documentClipList();
932 }
933
934 QDomElement ProjectList::producersList()
935 {
936     QDomDocument doc;
937     QDomElement prods = doc.createElement("producerlist");
938     doc.appendChild(prods);
939     kDebug() << "////////////  PRO LIST BUILD PRDSLIST ";
940     QTreeWidgetItemIterator it(m_listView);
941     while (*it) {
942         if ((*it)->type() == QTreeWidgetItem::UserType + 1) {
943             // subitem
944             ++it;
945             continue;
946         }
947         if (!((ProjectItem *)(*it))->isGroup())
948             prods.appendChild(doc.importNode(((ProjectItem *)(*it))->toXml(), true));
949         ++it;
950     }
951     return prods;
952 }
953
954 void ProjectList::slotCheckForEmptyQueue()
955 {
956     if (!m_refreshed && m_thumbnailQueue.isEmpty() && m_infoQueue.isEmpty()) {
957         m_refreshed = true;
958         emit loadingIsOver();
959         emit displayMessage(QString(), -1);
960         m_listView->blockSignals(false);
961         m_listView->setEnabled(true);
962     } else if (!m_refreshed) QTimer::singleShot(300, this, SLOT(slotCheckForEmptyQueue()));
963 }
964
965 void ProjectList::reloadClipThumbnails()
966 {
967     kDebug() << "//////////////  RELOAD CLIPS THUMBNAILS!!!";
968     m_thumbnailQueue.clear();
969     QTreeWidgetItemIterator it(m_listView);
970     while (*it) {
971         if ((*it)->type() == QTreeWidgetItem::UserType + 1) {
972             // subitem
973             ++it;
974             continue;
975         }
976         if (!((ProjectItem *)(*it))->isGroup())
977             m_thumbnailQueue << ((ProjectItem *)(*it))->clipId();
978         ++it;
979     }
980     QTimer::singleShot(300, this, SLOT(slotProcessNextThumbnail()));
981 }
982
983 void ProjectList::requestClipThumbnail(const QString id)
984 {
985     if (!m_thumbnailQueue.contains(id)) m_thumbnailQueue.append(id);
986 }
987
988 void ProjectList::slotProcessNextThumbnail()
989 {
990     if (m_thumbnailQueue.isEmpty() && m_infoQueue.isEmpty()) {
991         slotCheckForEmptyQueue();
992         return;
993     }
994     if (!m_infoQueue.isEmpty()) {
995         //QTimer::singleShot(300, this, SLOT(slotProcessNextThumbnail()));
996         return;
997     }
998     if (m_thumbnailQueue.count() > 1) {
999         int max = m_doc->clipManager()->clipsCount();
1000         emit displayMessage(i18n("Loading thumbnails"), (int)(100 * (max - m_thumbnailQueue.count()) / max));
1001     }
1002     slotRefreshClipThumbnail(m_thumbnailQueue.takeFirst(), false);
1003 }
1004
1005 void ProjectList::slotRefreshClipThumbnail(const QString &clipId, bool update)
1006 {
1007     QTreeWidgetItem *item = getAnyItemById(clipId);
1008     if (item) slotRefreshClipThumbnail(item, update);
1009     else slotProcessNextThumbnail();
1010 }
1011
1012 void ProjectList::slotRefreshClipThumbnail(QTreeWidgetItem *it, bool update)
1013 {
1014     if (it == NULL) return;
1015     ProjectItem *item = NULL;
1016     bool isSubItem = false;
1017     int frame;
1018     if (it->type() == QTreeWidgetItem::UserType + 1) {
1019         item = static_cast <ProjectItem *>(it->parent());
1020         frame = static_cast <SubProjectItem *>(it)->zone().x();
1021         isSubItem = true;
1022     } else {
1023         item = static_cast <ProjectItem *>(it);
1024         frame = item->referencedClip()->getClipThumbFrame();
1025     }
1026
1027     if (item) {
1028         DocClipBase *clip = item->referencedClip();
1029         if (!clip) {
1030             slotProcessNextThumbnail();
1031             return;
1032         }
1033         QPixmap pix;
1034         int height = it->sizeHint(0).height();
1035         int width = (int)(height  * m_render->dar());
1036         if (clip->clipType() == AUDIO) pix = KIcon("audio-x-generic").pixmap(QSize(width, height));
1037         else if (clip->clipType() == IMAGE) pix = QPixmap::fromImage(KThumb::getFrame(item->referencedClip()->producer(), 0, width, height));
1038         else pix = item->referencedClip()->thumbProducer()->extractImage(frame, width, height);
1039
1040         if (!pix.isNull()) {
1041             m_listView->blockSignals(true);
1042             it->setIcon(0, pix);
1043             if (m_listView->isEnabled()) m_listView->blockSignals(false);
1044             if (!isSubItem) m_doc->cachePixmap(item->getClipHash(), pix);
1045             else m_doc->cachePixmap(item->getClipHash() + '#' + QString::number(frame), pix);
1046         }
1047         if (update) emit projectModified();
1048         QTimer::singleShot(30, this, SLOT(slotProcessNextThumbnail()));
1049     }
1050 }
1051
1052 void ProjectList::slotReplyGetFileProperties(const QString &clipId, Mlt::Producer *producer, const QMap < QString, QString > &properties, const QMap < QString, QString > &metadata, bool replace)
1053 {
1054     ProjectItem *item = getItemById(clipId);
1055     if (item && producer) {
1056         m_listView->blockSignals(true);
1057         item->setProperties(properties, metadata);
1058         Q_ASSERT_X(item->referencedClip(), "void ProjectList::slotReplyGetFileProperties", QString("Item with groupName %1 does not have a clip associated").arg(item->groupName()).toLatin1());
1059         item->referencedClip()->setProducer(producer, replace);
1060         //emit receivedClipDuration(clipId);
1061         if (m_listView->isEnabled() && replace) {
1062             // update clip in clip monitor
1063             emit clipSelected(NULL);
1064             emit clipSelected(item->referencedClip());
1065         }
1066         /*else {
1067             // Check if duration changed.
1068             emit receivedClipDuration(clipId);
1069             delete producer;
1070         }*/
1071         if (m_listView->isEnabled()) m_listView->blockSignals(false);
1072         if (item->icon(0).isNull()) {
1073             requestClipThumbnail(clipId);
1074         }
1075     } else kDebug() << "////////  COULD NOT FIND CLIP TO UPDATE PRPS...";
1076     int max = m_doc->clipManager()->clipsCount();
1077     emit displayMessage(i18n("Loading clips"), (int)(100 * (max - m_infoQueue.count()) / max));
1078     // small delay so that the app can display the progress info
1079     if (item && m_infoQueue.isEmpty() && m_thumbnailQueue.isEmpty()) {
1080         m_listView->setCurrentItem(item);
1081         emit clipSelected(item->referencedClip());
1082     }
1083     QTimer::singleShot(30, this, SLOT(slotProcessNextClipInQueue()));
1084 }
1085
1086 void ProjectList::slotReplyGetImage(const QString &clipId, const QPixmap &pix)
1087 {
1088     ProjectItem *item = getItemById(clipId);
1089     if (item && !pix.isNull()) {
1090         m_listView->blockSignals(true);
1091         item->setIcon(0, pix);
1092         m_doc->cachePixmap(item->getClipHash(), pix);
1093         if (m_listView->isEnabled()) m_listView->blockSignals(false);
1094     }
1095 }
1096
1097 QTreeWidgetItem *ProjectList::getAnyItemById(const QString &id)
1098 {
1099     QTreeWidgetItemIterator it(m_listView);
1100     QString lookId = id;
1101     if (id.contains('#')) {
1102         lookId = id.section('#', 0, 0);
1103     }
1104
1105     ProjectItem *result = NULL;
1106     while (*it) {
1107         if ((*it)->type() == QTreeWidgetItem::UserType + 1) {
1108             // subitem
1109             ++it;
1110             continue;
1111         }
1112         ProjectItem *item = static_cast<ProjectItem *>(*it);
1113         if (item->clipId() == lookId && item->clipType() != FOLDER) {
1114             result = item;
1115             break;
1116         }
1117         ++it;
1118     }
1119     if (!id.contains('#')) return result;
1120     else for (int i = 0; i < result->childCount(); i++) {
1121             SubProjectItem *sub = static_cast <SubProjectItem *>(result->child(i));
1122             if (sub && sub->zone().x() == id.section('#', 1, 1).toInt()) {
1123                 return sub;
1124             }
1125         }
1126
1127     return NULL;
1128 }
1129
1130
1131 ProjectItem *ProjectList::getItemById(const QString &id)
1132 {
1133     ProjectItem *item;
1134     QTreeWidgetItemIterator it(m_listView);
1135     while (*it) {
1136         if ((*it)->type() == QTreeWidgetItem::UserType + 1) {
1137             // subitem
1138             ++it;
1139             continue;
1140         }
1141         item = static_cast<ProjectItem *>(*it);
1142         if (item->clipId() == id && item->clipType() != FOLDER)
1143             return item;
1144         ++it;
1145     }
1146     return NULL;
1147 }
1148
1149 ProjectItem *ProjectList::getFolderItemById(const QString &id)
1150 {
1151     ProjectItem *item;
1152     QTreeWidgetItemIterator it(m_listView);
1153     while (*it) {
1154         item = static_cast<ProjectItem *>(*it);
1155         if (item->clipId() == id && item->clipType() == FOLDER)
1156             return item;
1157         ++it;
1158     }
1159     return NULL;
1160 }
1161
1162 void ProjectList::slotSelectClip(const QString &ix)
1163 {
1164     ProjectItem *clip = getItemById(ix);
1165     if (clip) {
1166         m_listView->setCurrentItem(clip);
1167         m_listView->scrollToItem(clip);
1168         m_editAction->setEnabled(true);
1169         m_deleteAction->setEnabled(true);
1170         m_reloadAction->setEnabled(true);
1171         m_transcodeAction->setEnabled(true);
1172         if (clip->clipType() == IMAGE && !KdenliveSettings::defaultimageapp().isEmpty()) {
1173             m_openAction->setIcon(KIcon(KdenliveSettings::defaultimageapp()));
1174             m_openAction->setEnabled(true);
1175         } else if (clip->clipType() == AUDIO && !KdenliveSettings::defaultaudioapp().isEmpty()) {
1176             m_openAction->setIcon(KIcon(KdenliveSettings::defaultaudioapp()));
1177             m_openAction->setEnabled(true);
1178         } else m_openAction->setEnabled(false);
1179     }
1180 }
1181
1182 QString ProjectList::currentClipUrl() const
1183 {
1184     ProjectItem *item;
1185     if (m_listView->currentItem()->type() == QTreeWidgetItem::UserType + 1) {
1186         // subitem
1187         item = static_cast <ProjectItem*>(m_listView->currentItem()->parent());
1188     } else item = static_cast <ProjectItem*>(m_listView->currentItem());
1189     if (item == NULL) return QString();
1190     return item->clipUrl().path();
1191 }
1192
1193 void ProjectList::regenerateTemplate(const QString &id)
1194 {
1195     ProjectItem *clip = getItemById(id);
1196     if (clip) regenerateTemplate(clip);
1197 }
1198
1199 void ProjectList::regenerateTemplate(ProjectItem *clip)
1200 {
1201     //TODO: remove this unused method, only force_reload is necessary
1202     clip->referencedClip()->producer()->set("force_reload", 1);
1203 }
1204
1205 QDomDocument ProjectList::generateTemplateXml(QString path, const QString &replaceString)
1206 {
1207     QDomDocument doc;
1208     QFile file(path);
1209     if (!file.open(QIODevice::ReadOnly)) {
1210         kWarning() << "ERROR, CANNOT READ: " << path;
1211         return doc;
1212     }
1213     if (!doc.setContent(&file)) {
1214         kWarning() << "ERROR, CANNOT READ: " << path;
1215         file.close();
1216         return doc;
1217     }
1218     file.close();
1219     QDomNodeList texts = doc.elementsByTagName("content");
1220     for (int i = 0; i < texts.count(); i++) {
1221         QString data = texts.item(i).firstChild().nodeValue();
1222         data.replace("%s", replaceString);
1223         texts.item(i).firstChild().setNodeValue(data);
1224     }
1225     return doc;
1226 }
1227
1228
1229 void ProjectList::slotAddClipCut(const QString &id, int in, int out)
1230 {
1231     ProjectItem *clip = getItemById(id);
1232     if (clip == NULL) return;
1233     if (clip->referencedClip()->hasCutZone(QPoint(in, out))) return;
1234     AddClipCutCommand *command = new AddClipCutCommand(this, id, in, out, false);
1235     m_commandStack->push(command);
1236 }
1237
1238 void ProjectList::addClipCut(const QString &id, int in, int out)
1239 {
1240     //m_doc->slotAddClipCut(id, in, out, groupName, groupId);
1241     ProjectItem *clip = getItemById(id);
1242     if (clip) {
1243         DocClipBase *base = clip->referencedClip();
1244         base->addCutZone(in, out);
1245         m_listView->blockSignals(true);
1246         SubProjectItem *sub = new SubProjectItem(clip, in, out);
1247
1248         QPixmap p = clip->referencedClip()->thumbProducer()->extractImage(in, (int)(sub->sizeHint(0).height()  * m_render->dar()), sub->sizeHint(0).height());
1249         sub->setIcon(0, p);
1250         m_doc->cachePixmap(clip->getClipHash() + '#' + QString::number(in), p);
1251         m_listView->blockSignals(false);
1252     }
1253 }
1254
1255 void ProjectList::removeClipCut(const QString &id, int in, int out)
1256 {
1257     //m_doc->slotAddClipCut(id, in, out, groupName, groupId);
1258     ProjectItem *clip = getItemById(id);
1259     if (clip) {
1260         DocClipBase *base = clip->referencedClip();
1261         base->removeCutZone(in, out);
1262         for (int i = 0; i < clip->childCount(); i++) {
1263             QTreeWidgetItem *it = clip->child(i);
1264             if (it->type() != QTreeWidgetItem::UserType + 1) continue;
1265             SubProjectItem *sub = static_cast <SubProjectItem*>(it);
1266             if (sub->zone() == QPoint(in, out)) {
1267                 m_listView->blockSignals(true);
1268                 delete it;
1269                 m_listView->blockSignals(false);
1270                 break;
1271             }
1272         }
1273     }
1274 }
1275
1276 #include "projectlist.moc"