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