]> git.sesse.net Git - kdenlive/blob - src/projectlist.cpp
c9dccf7df52442a3ab57adb40add514a53e3c94b
[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::requestClipInfo(const QDomElement xml, const QString id) {
398     kDebug() << " PRG LISTĀ REQUEST CLP INFO: " << id;
399     m_infoQueue.insert(id, xml);
400     listView->setEnabled(false);
401     if (m_infoQueue.count() == 1) QTimer::singleShot(300, this, SLOT(slotProcessNextClipInQueue()));
402 }
403
404 void ProjectList::slotProcessNextClipInQueue() {
405     if (m_infoQueue.isEmpty()) {
406         listView->setEnabled(true);
407         return;
408     }
409     QMap<QString, QDomElement>::const_iterator i = m_infoQueue.constBegin();
410     if (i != m_infoQueue.constEnd()) {
411         const QDomElement dom = i.value();
412         const QString id = i.key();
413         m_infoQueue.remove(i.key());
414         emit getFileProperties(dom, id);
415     }
416     if (m_infoQueue.isEmpty()) listView->setEnabled(true);
417 }
418
419 void ProjectList::slotUpdateClip(const QString &id) {
420     ProjectItem *item = getItemById(id);
421     listView->blockSignals(true);
422     if (item) item->setData(1, UsageRole, QString::number(item->numReferences()));
423     listView->blockSignals(false);
424 }
425
426 void ProjectList::updateAllClips() {
427     QTreeWidgetItemIterator it(listView);
428     while (*it) {
429         ProjectItem *item = static_cast <ProjectItem *>(*it);
430         if (!item->isGroup()) {
431             if (item->referencedClip()->producer() == NULL) {
432                 DocClipBase *clip = item->referencedClip();
433                 if (clip->clipType() == TEXT && !QFile::exists(clip->fileURL().path())) {
434                     // regenerate text clip image if required
435                     TitleWidget *dia_ui = new TitleWidget(KUrl(), QString(), m_render, this);
436                     QDomDocument doc;
437                     doc.setContent(clip->getProperty("xmldata"));
438                     dia_ui->setXml(doc);
439                     QImage pix = dia_ui->renderedPixmap();
440                     pix.save(clip->fileURL().path());
441                     delete dia_ui;
442                 }
443                 requestClipInfo(clip->toXML(), clip->getId());
444             } else {
445                 QString cachedPixmap = m_doc->projectFolder().path() + "/thumbs/" + item->getClipHash() + ".png";
446                 if (QFile::exists(cachedPixmap)) {
447                     //kDebug()<<"// USING CACHED PIX: "<<cachedPixmap;
448                     listView->blockSignals(true);
449                     item->setIcon(0, QPixmap(cachedPixmap));
450                     listView->blockSignals(false);
451                 } else requestClipThumbnail(item->clipId());
452                 listView->blockSignals(true);
453                 item->changeDuration(item->referencedClip()->producer()->get_playtime());
454                 listView->blockSignals(false);
455             }
456             listView->blockSignals(true);
457             item->setData(1, UsageRole, QString::number(item->numReferences()));
458             listView->blockSignals(false);
459             qApp->processEvents();
460         }
461         ++it;
462     }
463     QTimer::singleShot(500, this, SLOT(slotCheckForEmptyQueue()));
464 }
465
466 void ProjectList::slotAddClip(KUrl givenUrl, QString group) {
467     if (!m_commandStack) kDebug() << "!!!!!!!!!!!!!!!! NO CMD STK";
468     KUrl::List list;
469     if (givenUrl.isEmpty()) {
470         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);
471     } else list.append(givenUrl);
472     if (list.isEmpty()) return;
473
474     QString groupId = QString();
475     if (group.isEmpty()) {
476         ProjectItem *item = static_cast <ProjectItem*>(listView->currentItem());
477         if (item && !item->isGroup()) {
478             while (item->parent()) {
479                 item = static_cast <ProjectItem*>(item->parent());
480                 if (item->isGroup()) break;
481             }
482         }
483         if (item && item->isGroup()) {
484             group = item->groupName();
485             groupId = item->clipId();
486         }
487     }
488     m_doc->slotAddClipList(list, group, groupId);
489 }
490
491 void ProjectList::slotRemoveInvalidClip(const QString &id) {
492     ProjectItem *item = getItemById(id);
493     if (item) {
494         const QString path = item->referencedClip()->fileURL().path();
495         if (!path.isEmpty()) KMessageBox::sorry(this, i18n("Clip <b>%1</b><br>is invalid, will be removed from project.", path));
496         QList <QString> ids;
497         ids << id;
498         m_doc->deleteProjectClip(ids);
499     }
500     if (!m_infoQueue.isEmpty()) QTimer::singleShot(300, this, SLOT(slotProcessNextClipInQueue()));
501 }
502
503 void ProjectList::slotAddColorClip() {
504     if (!m_commandStack) kDebug() << "!!!!!!!!!!!!!!!! NO CMD STK";
505     QDialog *dia = new QDialog(this);
506     Ui::ColorClip_UI *dia_ui = new Ui::ColorClip_UI();
507     dia_ui->setupUi(dia);
508     dia_ui->clip_name->setText(i18n("Color Clip"));
509     dia_ui->clip_duration->setText(KdenliveSettings::color_duration());
510     if (dia->exec() == QDialog::Accepted) {
511         QString color = dia_ui->clip_color->color().name();
512         color = color.replace(0, 1, "0x") + "ff";
513
514         QString group = QString();
515         QString groupId = QString();
516         ProjectItem *item = static_cast <ProjectItem*>(listView->currentItem());
517         if (item && !item->isGroup()) {
518             while (item->parent()) {
519                 item = static_cast <ProjectItem*>(item->parent());
520                 if (item->isGroup()) break;
521             }
522         }
523         if (item && item->isGroup()) {
524             group = item->groupName();
525             groupId = item->clipId();
526         }
527
528         m_doc->slotAddColorClipFile(dia_ui->clip_name->text(), color, dia_ui->clip_duration->text(), group, groupId);
529     }
530     delete dia_ui;
531     delete dia;
532 }
533
534
535 void ProjectList::slotAddSlideshowClip() {
536     if (!m_commandStack) kDebug() << "!!!!!!!!!!!!!!!! NO CMD STK";
537     SlideshowClip *dia = new SlideshowClip(this);
538
539     if (dia->exec() == QDialog::Accepted) {
540
541         QString group = QString();
542         QString groupId = QString();
543         ProjectItem *item = static_cast <ProjectItem*>(listView->currentItem());
544         if (item && !item->isGroup()) {
545             while (item->parent()) {
546                 item = static_cast <ProjectItem*>(item->parent());
547                 if (item->isGroup()) break;
548             }
549         }
550         if (item && item->isGroup()) {
551             group = item->groupName();
552             groupId = item->clipId();
553         }
554
555         m_doc->slotAddSlideshowClipFile(dia->clipName(), dia->selectedPath(), dia->imageCount(), dia->clipDuration(), dia->loop(), dia->fade(), dia->lumaDuration(), dia->lumaFile(), dia->softness(), group, groupId);
556     }
557     delete dia;
558 }
559
560 void ProjectList::slotAddTitleClip() {
561     QString group = QString();
562     QString groupId = QString();
563     ProjectItem *item = static_cast <ProjectItem*>(listView->currentItem());
564     if (item && !item->isGroup()) {
565         while (item->parent()) {
566             item = static_cast <ProjectItem*>(item->parent());
567             if (item->isGroup()) break;
568         }
569     }
570     if (item && item->isGroup()) {
571         group = item->groupName();
572         groupId = item->clipId();
573     }
574
575     m_doc->slotCreateTextClip(group, groupId);
576 }
577
578 void ProjectList::setDocument(KdenliveDoc *doc) {
579     listView->blockSignals(true);
580     listView->clear();
581     m_thumbnailQueue.clear();
582     m_infoQueue.clear();
583     m_refreshed = false;
584     QMap <QString, QString> flist = doc->clipManager()->documentFolderList();
585     QMapIterator<QString, QString> f(flist);
586     while (f.hasNext()) {
587         f.next();
588         (void) new ProjectItem(listView, QStringList() << QString() << f.value(), f.key());
589     }
590
591     QList <DocClipBase*> list = doc->clipManager()->documentClipList();
592     for (int i = 0; i < list.count(); i++) {
593         slotAddClip(list.at(i), false);
594     }
595
596     m_fps = doc->fps();
597     m_timecode = doc->timecode();
598     m_commandStack = doc->commandStack();
599     m_doc = doc;
600     QTreeWidgetItem *first = listView->topLevelItem(0);
601     if (first) listView->setCurrentItem(first);
602     listView->blockSignals(false);
603     m_toolbar->setEnabled(true);
604 }
605
606 QDomElement ProjectList::producersList() {
607     QDomDocument doc;
608     QDomElement prods = doc.createElement("producerlist");
609     doc.appendChild(prods);
610     kDebug() << "////////////  PRO LISTĀ BUILD PRDSLIST ";
611     QTreeWidgetItemIterator it(listView);
612     while (*it) {
613         if (!((ProjectItem *)(*it))->isGroup())
614             prods.appendChild(doc.importNode(((ProjectItem *)(*it))->toXml(), true));
615         ++it;
616     }
617     return prods;
618 }
619
620 void ProjectList::slotCheckForEmptyQueue() {
621     if (!m_refreshed && m_thumbnailQueue.isEmpty() && m_infoQueue.isEmpty()) {
622         m_refreshed = true;
623         emit loadingIsOver();
624     } else QTimer::singleShot(500, this, SLOT(slotCheckForEmptyQueue()));
625 }
626
627 void ProjectList::requestClipThumbnail(const QString &id) {
628     m_thumbnailQueue.append(id);
629     if (m_thumbnailQueue.count() == 1) QTimer::singleShot(300, this, SLOT(slotProcessNextThumbnail()));
630 }
631
632 void ProjectList::slotProcessNextThumbnail() {
633     if (m_thumbnailQueue.isEmpty()) {
634         return;
635     }
636     slotRefreshClipThumbnail(m_thumbnailQueue.takeFirst(), false);
637 }
638
639 void ProjectList::slotRefreshClipThumbnail(const QString &clipId, bool update) {
640     ProjectItem *item = getItemById(clipId);
641     if (item) slotRefreshClipThumbnail(item, update);
642     else slotProcessNextThumbnail();
643 }
644
645 void ProjectList::slotRefreshClipThumbnail(ProjectItem *item, bool update) {
646     if (item) {
647         if (!item->referencedClip()) return;
648         int height = 50;
649         int width = (int)(height  * m_render->dar());
650         DocClipBase *clip = item->referencedClip();
651         if (!clip) slotProcessNextThumbnail();
652         QPixmap pix;
653         if (clip->clipType() == AUDIO) pix = KIcon("audio-x-generic").pixmap(QSize(width, height));
654         else pix = item->referencedClip()->thumbProducer()->extractImage(item->referencedClip()->getClipThumbFrame(), width, height);
655         listView->blockSignals(true);
656         item->setIcon(0, pix);
657         listView->blockSignals(false);
658         m_doc->cachePixmap(item->getClipHash(), pix);
659         if (update) emit projectModified();
660         if (!m_thumbnailQueue.isEmpty()) QTimer::singleShot(300, this, SLOT(slotProcessNextThumbnail()));
661     }
662 }
663
664 void ProjectList::slotReplyGetFileProperties(const QString &clipId, Mlt::Producer *producer, const QMap < QString, QString > &properties, const QMap < QString, QString > &metadata) {
665     ProjectItem *item = getItemById(clipId);
666     if (item && producer) {
667         listView->blockSignals(true);
668         item->setProperties(properties, metadata);
669         Q_ASSERT_X( item->referencedClip(), "void ProjectList::slotReplyGetFileProperties", QString( "Item with groupName %1 does not have a clip associated" ).arg( item->groupName() ).toLatin1() );
670         item->referencedClip()->setProducer(producer);
671         emit receivedClipDuration(clipId, item->clipMaxDuration());
672         listView->blockSignals(false);
673     } else kDebug() << "////////  COULD NOT FIND CLIP TO UPDATE PRPS...";
674     if (!m_infoQueue.isEmpty()) QTimer::singleShot(300, this, SLOT(slotProcessNextClipInQueue()));
675     else listView->setEnabled(true);
676 }
677
678 void ProjectList::slotReplyGetImage(const QString &clipId, int pos, const QPixmap &pix, int w, int h) {
679     ProjectItem *item = getItemById(clipId);
680     if (item) {
681         listView->blockSignals(true);
682         item->setIcon(0, pix);
683         m_doc->cachePixmap(item->getClipHash(), pix);
684         listView->blockSignals(false);
685     }
686 }
687
688 ProjectItem *ProjectList::getItemById(const QString &id) {
689     ProjectItem *item;
690     QTreeWidgetItemIterator it(listView);
691     while (*it) {
692         item = static_cast<ProjectItem *>(*it);
693         if (item->clipId() == id && item->clipType() != FOLDER)
694             return item;
695         ++it;
696     }
697     return NULL;
698 }
699
700 ProjectItem *ProjectList::getFolderItemById(const QString &id) {
701     ProjectItem *item;
702     QTreeWidgetItemIterator it(listView);
703     while (*it) {
704         item = static_cast<ProjectItem *>(*it);
705         if (item->clipId() == id && item->clipType() == FOLDER)
706             return item;
707         ++it;
708     }
709     return NULL;
710 }
711
712 void ProjectList::slotSelectClip(const QString &ix) {
713     ProjectItem *p = getItemById(ix);
714     if (p) {
715         listView->setCurrentItem(p);
716         listView->scrollToItem(p);
717         m_editAction->setEnabled(true);
718         m_deleteAction->setEnabled(true);
719     }
720 }
721
722 #include "projectlist.moc"