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