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