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