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