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