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