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