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