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