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