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