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