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