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