]> git.sesse.net Git - kdenlive/blob - src/projectlist.cpp
b2837a206950e25ffc1cd7b044bc69ccc319871e
[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 "timecodedisplay.h"
35 #include "profilesdialog.h"
36 #include "editclipcommand.h"
37 #include "editclipcutcommand.h"
38 #include "editfoldercommand.h"
39 #include "addclipcutcommand.h"
40
41 #include "ui_templateclip_ui.h"
42
43 #include <KDebug>
44 #include <KAction>
45 #include <KLocale>
46 #include <KFileDialog>
47 #include <KInputDialog>
48 #include <KMessageBox>
49 #include <KIO/NetAccess>
50 #include <KFileItem>
51 #include <KApplication>
52 #ifdef NEPOMUK
53 #include <nepomuk/global.h>
54 #include <nepomuk/resourcemanager.h>
55 //#include <nepomuk/tag.h>
56 #endif
57
58 #include <QMouseEvent>
59 #include <QStylePainter>
60 #include <QPixmap>
61 #include <QIcon>
62 #include <QMenu>
63 #include <QProcess>
64 #include <QHeaderView>
65 #include <QInputDialog>
66
67 ProjectList::ProjectList(QWidget *parent) :
68     QWidget(parent),
69     m_render(NULL),
70     m_fps(-1),
71     m_commandStack(NULL),
72     m_openAction(NULL),
73     m_reloadAction(NULL),
74     m_transcodeAction(NULL),
75     m_doc(NULL),
76     m_refreshed(false),
77     m_infoQueue(),
78     m_thumbnailQueue()
79 {
80     QVBoxLayout *layout = new QVBoxLayout;
81     layout->setContentsMargins(0, 0, 0, 0);
82     layout->setSpacing(0);
83
84     // setup toolbar
85     QFrame *frame = new QFrame;
86     frame->setFrameStyle(QFrame::NoFrame);
87     QHBoxLayout *box = new QHBoxLayout;
88     KTreeWidgetSearchLine *searchView = new KTreeWidgetSearchLine;
89
90     box->addWidget(searchView);
91     //int s = style()->pixelMetric(QStyle::PM_SmallIconSize);
92     //m_toolbar->setIconSize(QSize(s, s));
93
94     m_addButton = new QToolButton;
95     m_addButton->setPopupMode(QToolButton::MenuButtonPopup);
96     m_addButton->setAutoRaise(true);
97     box->addWidget(m_addButton);
98
99     m_editButton = new QToolButton;
100     m_editButton->setAutoRaise(true);
101     box->addWidget(m_editButton);
102
103     m_deleteButton = new QToolButton;
104     m_deleteButton->setAutoRaise(true);
105     box->addWidget(m_deleteButton);
106     frame->setLayout(box);
107     layout->addWidget(frame);
108
109     m_listView = new ProjectListView;
110     layout->addWidget(m_listView);
111     setLayout(layout);
112     searchView->setTreeWidget(m_listView);
113
114     m_queueTimer.setInterval(100);
115     connect(&m_queueTimer, SIGNAL(timeout()), this, SLOT(slotProcessNextClipInQueue()));
116     m_queueTimer.setSingleShot(true);
117
118
119     connect(m_listView, SIGNAL(projectModified()), this, SIGNAL(projectModified()));
120     connect(m_listView, SIGNAL(itemSelectionChanged()), this, SLOT(slotClipSelected()));
121     connect(m_listView, SIGNAL(focusMonitor()), this, SLOT(slotClipSelected()));
122     connect(m_listView, SIGNAL(pauseMonitor()), this, SLOT(slotPauseMonitor()));
123     connect(m_listView, SIGNAL(requestMenu(const QPoint &, QTreeWidgetItem *)), this, SLOT(slotContextMenu(const QPoint &, QTreeWidgetItem *)));
124     connect(m_listView, SIGNAL(addClip()), this, SLOT(slotAddClip()));
125     connect(m_listView, SIGNAL(addClip(const QList <QUrl>, const QString &, const QString &)), this, SLOT(slotAddClip(const QList <QUrl>, const QString &, const QString &)));
126     connect(m_listView, SIGNAL(addClipCut(const QString &, int, int)), this, SLOT(slotAddClipCut(const QString &, int, int)));
127     connect(m_listView, SIGNAL(itemChanged(QTreeWidgetItem *, int)), this, SLOT(slotItemEdited(QTreeWidgetItem *, int)));
128     connect(m_listView, SIGNAL(showProperties(DocClipBase *)), this, SIGNAL(showClipProperties(DocClipBase *)));
129
130     m_listViewDelegate = new ItemDelegate(m_listView);
131     m_listView->setItemDelegate(m_listViewDelegate);
132 #ifdef NEPOMUK
133     if (KdenliveSettings::activate_nepomuk()) {
134         Nepomuk::ResourceManager::instance()->init();
135         if (!Nepomuk::ResourceManager::instance()->initialized()) {
136             kDebug() << "Cannot communicate with Nepomuk, DISABLING it";
137             KdenliveSettings::setActivate_nepomuk(false);
138         }
139     }
140 #endif
141 }
142
143 ProjectList::~ProjectList()
144 {
145     delete m_menu;
146     m_listView->blockSignals(true);
147     m_listView->clear();
148     delete m_listViewDelegate;
149 }
150
151 void ProjectList::focusTree() const
152 {
153     m_listView->setFocus();
154 }
155
156 void ProjectList::setupMenu(QMenu *addMenu, QAction *defaultAction)
157 {
158     QList <QAction *> actions = addMenu->actions();
159     for (int i = 0; i < actions.count(); i++) {
160         if (actions.at(i)->data().toString() == "clip_properties") {
161             m_editButton->setDefaultAction(actions.at(i));
162             actions.removeAt(i);
163             i--;
164         } else if (actions.at(i)->data().toString() == "delete_clip") {
165             m_deleteButton->setDefaultAction(actions.at(i));
166             actions.removeAt(i);
167             i--;
168         } else if (actions.at(i)->data().toString() == "edit_clip") {
169             m_openAction = actions.at(i);
170             actions.removeAt(i);
171             i--;
172         } else if (actions.at(i)->data().toString() == "reload_clip") {
173             m_reloadAction = actions.at(i);
174             actions.removeAt(i);
175             i--;
176         }
177     }
178
179     QMenu *m = new QMenu();
180     m->addActions(actions);
181     m_addButton->setMenu(m);
182     m_addButton->setDefaultAction(defaultAction);
183     m_menu = new QMenu();
184     m_menu->addActions(addMenu->actions());
185 }
186
187 void ProjectList::setupGeneratorMenu(QMenu *addMenu, QMenu *transcodeMenu, QMenu *inTimelineMenu)
188 {
189     if (!addMenu)
190         return;
191     QMenu *menu = m_addButton->menu();
192     menu->addMenu(addMenu);
193     m_addButton->setMenu(menu);
194
195     m_menu->addMenu(addMenu);
196     if (addMenu->isEmpty())
197         addMenu->setEnabled(false);
198     m_menu->addMenu(transcodeMenu);
199     if (transcodeMenu->isEmpty())
200         transcodeMenu->setEnabled(false);
201     m_transcodeAction = transcodeMenu;
202     m_menu->addAction(m_reloadAction);
203     m_menu->addMenu(inTimelineMenu);
204     inTimelineMenu->setEnabled(false);
205     m_menu->addAction(m_editButton->defaultAction());
206     m_menu->addAction(m_openAction);
207     m_menu->addAction(m_deleteButton->defaultAction());
208     m_menu->insertSeparator(m_deleteButton->defaultAction());
209 }
210
211
212 QByteArray ProjectList::headerInfo() const
213 {
214     return m_listView->header()->saveState();
215 }
216
217 void ProjectList::setHeaderInfo(const QByteArray &state)
218 {
219     m_listView->header()->restoreState(state);
220 }
221
222 void ProjectList::updateProjectFormat(Timecode t)
223 {
224     m_timecode = t;
225 }
226
227 void ProjectList::slotEditClip()
228 {
229     QList<QTreeWidgetItem *> list = m_listView->selectedItems();
230     if (list.count() > 1) {
231         editClipSelection(list);
232         return;
233     }
234     ProjectItem *item;
235     if (!m_listView->currentItem() || m_listView->currentItem()->type() == PROJECTFOLDERTYPE)
236         return;
237     if (m_listView->currentItem()->type() == PROJECTSUBCLIPTYPE)
238         item = static_cast <ProjectItem*>(m_listView->currentItem()->parent());
239     else
240         item = static_cast <ProjectItem*>(m_listView->currentItem());
241     if (item && (item->flags() & Qt::ItemIsDragEnabled)) {
242         emit clipSelected(item->referencedClip());
243         emit showClipProperties(item->referencedClip());
244     }
245 }
246
247 void ProjectList::editClipSelection(QList<QTreeWidgetItem *> list)
248 {
249     // Gather all common properties
250     QMap <QString, QString> commonproperties;
251     QList <DocClipBase *> clipList;
252     commonproperties.insert("force_aspect_num", "-");
253     commonproperties.insert("force_aspect_den", "-");
254     commonproperties.insert("force_fps", "-");
255     commonproperties.insert("force_progressive", "-");
256     commonproperties.insert("force_tff", "-");
257     commonproperties.insert("threads", "-");
258     commonproperties.insert("video_index", "-");
259     commonproperties.insert("audio_index", "-");
260     commonproperties.insert("force_colorspace", "-");
261     commonproperties.insert("full_luma", "-");
262
263     bool allowDurationChange = true;
264     int commonDuration = -1;
265     ProjectItem *item;
266     for (int i = 0; i < list.count(); i++) {
267         item = NULL;
268         if (list.at(i)->type() == PROJECTFOLDERTYPE)
269             continue;
270         if (list.at(i)->type() == PROJECTSUBCLIPTYPE)
271             item = static_cast <ProjectItem*>(list.at(i)->parent());
272         else
273             item = static_cast <ProjectItem*>(list.at(i));
274         if (!(item->flags() & Qt::ItemIsDragEnabled))
275             continue;
276         if (item) {
277             // check properties
278             DocClipBase *clip = item->referencedClip();
279             if (clipList.contains(clip)) continue;
280             if (clip->clipType() != COLOR && clip->clipType() != IMAGE && clip->clipType() != TEXT)
281                 allowDurationChange = false;
282             if (allowDurationChange && commonDuration != 0) {
283                 if (commonDuration == -1)
284                     commonDuration = clip->duration().frames(m_fps);
285                 else if (commonDuration != clip->duration().frames(m_fps))
286                     commonDuration = 0;
287             }
288             clipList.append(clip);
289             QMap <QString, QString> clipprops = clip->properties();
290             QMapIterator<QString, QString> p(commonproperties);
291             while (p.hasNext()) {
292                 p.next();
293                 if (p.value().isEmpty()) continue;
294                 if (clipprops.contains(p.key())) {
295                     if (p.value() == "-")
296                         commonproperties.insert(p.key(), clipprops.value(p.key()));
297                     else if (p.value() != clipprops.value(p.key()))
298                         commonproperties.insert(p.key(), QString());
299                 } else {
300                     commonproperties.insert(p.key(), QString());
301                 }
302             }
303         }
304     }
305     if (allowDurationChange)
306         commonproperties.insert("out", QString::number(commonDuration));
307     QMapIterator<QString, QString> p(commonproperties);
308     while (p.hasNext()) {
309         p.next();
310         kDebug() << "Result: " << p.key() << " = " << p.value();
311     }
312     emit showClipProperties(clipList, commonproperties);
313 }
314
315 void ProjectList::slotOpenClip()
316 {
317     ProjectItem *item;
318     if (!m_listView->currentItem() || m_listView->currentItem()->type() == PROJECTFOLDERTYPE)
319         return;
320     if (m_listView->currentItem()->type() == QTreeWidgetItem::UserType + 1)
321         item = static_cast <ProjectItem*>(m_listView->currentItem()->parent());
322     else
323         item = static_cast <ProjectItem*>(m_listView->currentItem());
324     if (item) {
325         if (item->clipType() == IMAGE) {
326             if (KdenliveSettings::defaultimageapp().isEmpty())
327                 KMessageBox::sorry(kapp->activeWindow(), i18n("Please set a default application to open images in the Settings dialog"));
328             else
329                 QProcess::startDetached(KdenliveSettings::defaultimageapp(), QStringList() << item->clipUrl().path());
330         }
331         if (item->clipType() == AUDIO) {
332             if (KdenliveSettings::defaultaudioapp().isEmpty())
333                 KMessageBox::sorry(kapp->activeWindow(), i18n("Please set a default application to open audio files in the Settings dialog"));
334             else
335                 QProcess::startDetached(KdenliveSettings::defaultaudioapp(), QStringList() << item->clipUrl().path());
336         }
337     }
338 }
339
340 void ProjectList::cleanup()
341 {
342     m_listView->clearSelection();
343     QTreeWidgetItemIterator it(m_listView);
344     ProjectItem *item;
345     while (*it) {
346         if ((*it)->type() != PROJECTCLIPTYPE) {
347             it++;
348             continue;
349         }
350         item = static_cast <ProjectItem *>(*it);
351         if (item->numReferences() == 0)
352             item->setSelected(true);
353         it++;
354     }
355     slotRemoveClip();
356 }
357
358 void ProjectList::trashUnusedClips()
359 {
360     QTreeWidgetItemIterator it(m_listView);
361     ProjectItem *item;
362     QStringList ids;
363     QStringList urls;
364     while (*it) {
365         if ((*it)->type() != PROJECTCLIPTYPE) {
366             it++;
367             continue;
368         }
369         item = static_cast <ProjectItem *>(*it);
370         if (item->numReferences() == 0) {
371             ids << item->clipId();
372             KUrl url = item->clipUrl();
373             if (!url.isEmpty() && !urls.contains(url.path()))
374                 urls << url.path();
375         }
376         it++;
377     }
378
379     // Check that we don't use the URL in another clip
380     QTreeWidgetItemIterator it2(m_listView);
381     while (*it2) {
382         if ((*it2)->type() != PROJECTCLIPTYPE) {
383             it2++;
384             continue;
385         }
386         item = static_cast <ProjectItem *>(*it2);
387         if (item->numReferences() > 0) {
388             KUrl url = item->clipUrl();
389             if (!url.isEmpty() && urls.contains(url.path())) urls.removeAll(url.path());
390         }
391         it2++;
392     }
393
394     emit deleteProjectClips(ids, QMap <QString, QString>());
395     for (int i = 0; i < urls.count(); i++)
396         KIO::NetAccess::del(KUrl(urls.at(i)), this);
397 }
398
399 void ProjectList::slotReloadClip(const QString &id)
400 {
401     QList<QTreeWidgetItem *> selected;
402     if (id.isEmpty())
403         selected = m_listView->selectedItems();
404     else
405         selected.append(getItemById(id));
406     ProjectItem *item;
407     for (int i = 0; i < selected.count(); i++) {
408         if (selected.at(i)->type() != PROJECTCLIPTYPE) {
409             if (selected.at(i)->type() == PROJECTFOLDERTYPE) {
410                 for (int j = 0; j < selected.at(i)->childCount(); j++)
411                     selected.append(selected.at(i)->child(j));
412             }
413             continue;
414         }
415         item = static_cast <ProjectItem *>(selected.at(i));
416         if (item) {
417             CLIPTYPE t = item->clipType();
418             if (t == TEXT) {
419                 if (!item->referencedClip()->getProperty("xmltemplate").isEmpty())
420                     regenerateTemplate(item);
421             } else if (t != COLOR && t != SLIDESHOW && item->referencedClip() &&  item->referencedClip()->checkHash() == false) {
422                 item->referencedClip()->setPlaceHolder(true);
423                 item->setProperty("file_hash", QString());
424             } else if (t == IMAGE) {
425                 item->referencedClip()->producer()->set("force_reload", 1);
426             }
427
428             QDomElement e = item->toXml();
429             // Make sure we get the correct producer length if it was adjusted in timeline
430             if (t == COLOR || t == IMAGE || t == SLIDESHOW || t == TEXT) {
431                 int length = QString(item->referencedClip()->producerProperty("length")).toInt();
432                 if (length > 0 && !e.hasAttribute("length")) {
433                     e.setAttribute("length", length);
434                     e.setAttribute("out", length - 1);
435                 }
436             }
437             
438             emit getFileProperties(e, item->clipId(), m_listView->iconSize().height(), true);
439         }
440     }
441 }
442
443 void ProjectList::slotModifiedClip(const QString &id)
444 {
445     ProjectItem *item = getItemById(id);
446     if (item) {
447         QPixmap pixmap = qVariantValue<QPixmap>(item->data(0, Qt::DecorationRole));
448         if (!pixmap.isNull()) {
449             QPainter p(&pixmap);
450             p.fillRect(0, 0, pixmap.width(), pixmap.height(), QColor(255, 255, 255, 200));
451             p.drawPixmap(0, 0, KIcon("view-refresh").pixmap(m_listView->iconSize()));
452             p.end();
453         } else {
454             pixmap = KIcon("view-refresh").pixmap(m_listView->iconSize());
455         }
456         item->setData(0, Qt::DecorationRole, pixmap);
457     }
458 }
459
460 void ProjectList::slotMissingClip(const QString &id)
461 {
462     ProjectItem *item = getItemById(id);
463     if (item) {
464         item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsDropEnabled);
465         if (item->referencedClip()) {
466             item->referencedClip()->setPlaceHolder(true);
467             if (m_render == NULL) kDebug() << "*********  ERROR, NULL RENDR";
468             item->referencedClip()->setProducer(m_render->invalidProducer(id), true);
469             item->slotSetToolTip();
470             emit clipNeedsReload(id, true);
471         }
472     }
473     update();
474     emit displayMessage(i18n("Check missing clips"), -2);
475     emit updateRenderStatus();
476 }
477
478 void ProjectList::slotAvailableClip(const QString &id)
479 {
480     ProjectItem *item = getItemById(id);
481     if (item == NULL)
482         return;
483     item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsDragEnabled | Qt::ItemIsEnabled | Qt::ItemIsEditable | Qt::ItemIsDropEnabled);
484     if (item->referencedClip()) { // && item->referencedClip()->checkHash() == false) {
485         item->setProperty("file_hash", QString());
486         slotReloadClip(id);
487     }
488     /*else {
489     item->referencedClip()->setValid();
490     item->slotSetToolTip();
491     }
492     update();*/
493     emit updateRenderStatus();
494 }
495
496 bool ProjectList::hasMissingClips()
497 {
498     bool missing = false;
499     QTreeWidgetItemIterator it(m_listView);
500     while (*it) {
501         if ((*it)->type() == PROJECTCLIPTYPE && !((*it)->flags() & Qt::ItemIsDragEnabled)) {
502             missing = true;
503             break;
504         }
505         it++;
506     }
507     return missing;
508 }
509
510 void ProjectList::setRenderer(Render *projectRender)
511 {
512     m_render = projectRender;
513     m_listView->setIconSize(QSize((ProjectItem::itemDefaultHeight() - 2) * m_render->dar(), ProjectItem::itemDefaultHeight() - 2));
514 }
515
516 void ProjectList::slotClipSelected()
517 {
518     if (!m_listView->isEnabled()) return;
519     if (m_listView->currentItem()) {
520         if (m_listView->currentItem()->type() == PROJECTFOLDERTYPE) {
521             emit clipSelected(NULL);
522             m_editButton->defaultAction()->setEnabled(false);
523             m_deleteButton->defaultAction()->setEnabled(true);
524             m_openAction->setEnabled(false);
525             m_reloadAction->setEnabled(false);
526             m_transcodeAction->setEnabled(false);
527         } else {
528             ProjectItem *clip;
529             if (m_listView->currentItem()->type() == PROJECTSUBCLIPTYPE) {
530                 // this is a sub item, use base clip
531                 m_deleteButton->defaultAction()->setEnabled(true);
532                 clip = static_cast <ProjectItem*>(m_listView->currentItem()->parent());
533                 if (clip == NULL) kDebug() << "-----------ERROR";
534                 SubProjectItem *sub = static_cast <SubProjectItem*>(m_listView->currentItem());
535                 emit clipSelected(clip->referencedClip(), sub->zone());
536                 m_transcodeAction->setEnabled(false);
537                 return;
538             }
539             clip = static_cast <ProjectItem*>(m_listView->currentItem());
540             if (clip)
541                 emit clipSelected(clip->referencedClip());
542             m_editButton->defaultAction()->setEnabled(true);
543             m_deleteButton->defaultAction()->setEnabled(true);
544             m_reloadAction->setEnabled(true);
545             m_transcodeAction->setEnabled(true);
546             if (clip && clip->clipType() == IMAGE && !KdenliveSettings::defaultimageapp().isEmpty()) {
547                 m_openAction->setIcon(KIcon(KdenliveSettings::defaultimageapp()));
548                 m_openAction->setEnabled(true);
549             } else if (clip && clip->clipType() == AUDIO && !KdenliveSettings::defaultaudioapp().isEmpty()) {
550                 m_openAction->setIcon(KIcon(KdenliveSettings::defaultaudioapp()));
551                 m_openAction->setEnabled(true);
552             } else {
553                 m_openAction->setEnabled(false);
554             }
555             // Display relevant transcoding actions only
556             adjustTranscodeActions(clip);
557             // Display uses in timeline
558             emit findInTimeline(clip->clipId());
559         }
560     } else {
561         emit clipSelected(NULL);
562         m_editButton->defaultAction()->setEnabled(false);
563         m_deleteButton->defaultAction()->setEnabled(false);
564         m_openAction->setEnabled(false);
565         m_reloadAction->setEnabled(false);
566         m_transcodeAction->setEnabled(false);
567     }
568 }
569
570 void ProjectList::adjustTranscodeActions(ProjectItem *clip) const
571 {
572     if (clip == NULL || clip->type() != PROJECTCLIPTYPE || clip->clipType() == COLOR || clip->clipType() == TEXT || clip->clipType() == PLAYLIST || clip->clipType() == SLIDESHOW) {
573         m_transcodeAction->setEnabled(false);
574         return;
575     }
576     m_transcodeAction->setEnabled(true);
577     QList<QAction *> transcodeActions = m_transcodeAction->actions();
578     QStringList data;
579     QString condition;
580     for (int i = 0; i < transcodeActions.count(); i++) {
581         data = transcodeActions.at(i)->data().toStringList();
582         if (data.count() > 2) {
583             condition = data.at(2);
584             if (condition.startsWith("vcodec"))
585                 transcodeActions.at(i)->setEnabled(clip->referencedClip()->hasVideoCodec(condition.section('=', 1, 1)));
586             else if (condition.startsWith("acodec"))
587                 transcodeActions.at(i)->setEnabled(clip->referencedClip()->hasVideoCodec(condition.section('=', 1, 1)));
588         }
589     }
590
591 }
592
593 void ProjectList::slotPauseMonitor()
594 {
595     if (m_render)
596         m_render->pause();
597 }
598
599 void ProjectList::slotUpdateClipProperties(const QString &id, QMap <QString, QString> properties)
600 {
601     ProjectItem *item = getItemById(id);
602     if (item) {
603         slotUpdateClipProperties(item, properties);
604         if (properties.contains("out") || properties.contains("force_fps") || properties.contains("resource")) {
605             slotReloadClip(id);
606         } else if (properties.contains("colour") ||
607                    properties.contains("xmldata") ||
608                    properties.contains("force_aspect_num") ||
609                    properties.contains("force_aspect_den") ||
610                    properties.contains("templatetext")) {
611             slotRefreshClipThumbnail(item);
612             emit refreshClip();
613         } else if (properties.contains("full_luma") || properties.contains("force_colorspace")) {
614             emit refreshClip();
615         }
616     }
617 }
618
619 void ProjectList::slotUpdateClipProperties(ProjectItem *clip, QMap <QString, QString> properties)
620 {
621     if (!clip)
622         return;
623     clip->setProperties(properties);
624     if (properties.contains("name")) {
625         m_listView->blockSignals(true);
626         clip->setText(0, properties.value("name"));
627         m_listView->blockSignals(false);
628         emit clipNameChanged(clip->clipId(), properties.value("name"));
629     }
630     if (properties.contains("description")) {
631         CLIPTYPE type = clip->clipType();
632         m_listView->blockSignals(true);
633         clip->setText(1, properties.value("description"));
634         m_listView->blockSignals(false);
635 #ifdef NEPOMUK
636         if (KdenliveSettings::activate_nepomuk() && (type == AUDIO || type == VIDEO || type == AV || type == IMAGE || type == PLAYLIST)) {
637             // Use Nepomuk system to store clip description
638             Nepomuk::Resource f(clip->clipUrl().path());
639             f.setDescription(properties.value("description"));
640         }
641 #endif
642         emit projectModified();
643     }
644 }
645
646 void ProjectList::slotItemEdited(QTreeWidgetItem *item, int column)
647 {
648     if (item->type() == PROJECTSUBCLIPTYPE) {
649         // this is a sub-item
650         if (column == 1) {
651             // user edited description
652             SubProjectItem *sub = static_cast <SubProjectItem*>(item);
653             ProjectItem *item = static_cast <ProjectItem *>(sub->parent());
654             EditClipCutCommand *command = new EditClipCutCommand(this, item->clipId(), sub->zone(), sub->zone(), sub->description(), sub->text(1), true);
655             m_commandStack->push(command);
656             //slotUpdateCutClipProperties(sub->clipId(), sub->zone(), sub->text(1), sub->text(1));
657         }
658         return;
659     }
660     if (item->type() == PROJECTFOLDERTYPE) {
661         if (column == 0) {
662             FolderProjectItem *folder = static_cast <FolderProjectItem*>(item);
663             editFolder(item->text(0), folder->groupName(), folder->clipId());
664             folder->setGroupName(item->text(0));
665             m_doc->clipManager()->addFolder(folder->clipId(), item->text(0));
666             const int children = item->childCount();
667             for (int i = 0; i < children; i++) {
668                 ProjectItem *child = static_cast <ProjectItem *>(item->child(i));
669                 child->setProperty("groupname", item->text(0));
670             }
671         }
672         return;
673     }
674
675     ProjectItem *clip = static_cast <ProjectItem*>(item);
676     if (column == 1) {
677         if (clip->referencedClip()) {
678             QMap <QString, QString> oldprops;
679             QMap <QString, QString> newprops;
680             oldprops["description"] = clip->referencedClip()->getProperty("description");
681             newprops["description"] = item->text(1);
682
683             if (clip->clipType() == TEXT) {
684                 // This is a text template clip, update the image
685                 /*oldprops.insert("xmldata", clip->referencedClip()->getProperty("xmldata"));
686                 newprops.insert("xmldata", generateTemplateXml(clip->referencedClip()->getProperty("xmltemplate"), item->text(2)).toString());*/
687                 oldprops.insert("templatetext", clip->referencedClip()->getProperty("templatetext"));
688                 newprops.insert("templatetext", item->text(1));
689             }
690             slotUpdateClipProperties(clip->clipId(), newprops);
691             EditClipCommand *command = new EditClipCommand(this, clip->clipId(), oldprops, newprops, false);
692             m_commandStack->push(command);
693         }
694     } else if (column == 0) {
695         if (clip->referencedClip()) {
696             QMap <QString, QString> oldprops;
697             QMap <QString, QString> newprops;
698             oldprops["name"] = clip->referencedClip()->getProperty("name");
699             newprops["name"] = item->text(0);
700             slotUpdateClipProperties(clip, newprops);
701             emit projectModified();
702             EditClipCommand *command = new EditClipCommand(this, clip->clipId(), oldprops, newprops, false);
703             m_commandStack->push(command);
704         }
705     }
706 }
707
708 void ProjectList::slotContextMenu(const QPoint &pos, QTreeWidgetItem *item)
709 {
710     bool enable = item ? true : false;
711     m_editButton->defaultAction()->setEnabled(enable);
712     m_deleteButton->defaultAction()->setEnabled(enable);
713     m_reloadAction->setEnabled(enable);
714     m_transcodeAction->setEnabled(enable);
715     if (enable) {
716         ProjectItem *clip = NULL;
717         if (m_listView->currentItem()->type() == PROJECTSUBCLIPTYPE) {
718             clip = static_cast <ProjectItem*>(item->parent());
719             m_transcodeAction->setEnabled(false);
720         } else if (m_listView->currentItem()->type() == PROJECTCLIPTYPE) {
721             clip = static_cast <ProjectItem*>(item);
722             // Display relevant transcoding actions only
723             adjustTranscodeActions(clip);
724             // Display uses in timeline
725             emit findInTimeline(clip->clipId());
726         } else {
727             m_transcodeAction->setEnabled(false);
728         }
729         if (clip && clip->clipType() == IMAGE && !KdenliveSettings::defaultimageapp().isEmpty()) {
730             m_openAction->setIcon(KIcon(KdenliveSettings::defaultimageapp()));
731             m_openAction->setEnabled(true);
732         } else if (clip && clip->clipType() == AUDIO && !KdenliveSettings::defaultaudioapp().isEmpty()) {
733             m_openAction->setIcon(KIcon(KdenliveSettings::defaultaudioapp()));
734             m_openAction->setEnabled(true);
735         } else {
736             m_openAction->setEnabled(false);
737         }
738
739     } else {
740         m_openAction->setEnabled(false);
741     }
742     m_menu->popup(pos);
743 }
744
745 void ProjectList::slotRemoveClip()
746 {
747     if (!m_listView->currentItem())
748         return;
749     QStringList ids;
750     QMap <QString, QString> folderids;
751     QList<QTreeWidgetItem *> selected = m_listView->selectedItems();
752
753     QUndoCommand *delCommand = new QUndoCommand();
754     delCommand->setText(i18n("Delete Clip Zone"));
755     for (int i = 0; i < selected.count(); i++) {
756         if (selected.at(i)->type() == PROJECTSUBCLIPTYPE) {
757             // subitem
758             SubProjectItem *sub = static_cast <SubProjectItem *>(selected.at(i));
759             ProjectItem *item = static_cast <ProjectItem *>(sub->parent());
760             new AddClipCutCommand(this, item->clipId(), sub->zone().x(), sub->zone().y(), sub->description(), false, true, delCommand);
761         } else if (selected.at(i)->type() == PROJECTFOLDERTYPE) {
762             // folder
763             FolderProjectItem *folder = static_cast <FolderProjectItem *>(selected.at(i));
764             folderids[folder->groupName()] = folder->clipId();
765             int children = folder->childCount();
766
767             if (children > 0 && KMessageBox::questionYesNo(kapp->activeWindow(), i18np("Delete folder <b>%2</b>?<br />This will also remove the clip in that folder", "Delete folder <b>%2</b>?<br />This will also remove the %1 clips in that folder",  children, folder->text(1)), i18n("Delete Folder")) != KMessageBox::Yes)
768                 return;
769             for (int i = 0; i < children; ++i) {
770                 ProjectItem *child = static_cast <ProjectItem *>(folder->child(i));
771                 ids << child->clipId();
772             }
773         } else {
774             ProjectItem *item = static_cast <ProjectItem *>(selected.at(i));
775             ids << item->clipId();
776             if (item->numReferences() > 0 && KMessageBox::questionYesNo(kapp->activeWindow(), 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"), KStandardGuiItem::yes(), KStandardGuiItem::no(), "DeleteAll") == KMessageBox::No) {
777                 KMessageBox::enableMessage("DeleteAll");
778                 return;
779             }
780         }
781     }
782     KMessageBox::enableMessage("DeleteAll");
783     if (delCommand->childCount() == 0)
784         delete delCommand;
785     else
786         m_commandStack->push(delCommand);
787     emit deleteProjectClips(ids, folderids);
788 }
789
790 void ProjectList::updateButtons() const
791 {
792     if (m_listView->topLevelItemCount() == 0) {
793         m_deleteButton->defaultAction()->setEnabled(false);
794     } else {
795         m_deleteButton->defaultAction()->setEnabled(true);
796         if (!m_listView->currentItem())
797             m_listView->setCurrentItem(m_listView->topLevelItem(0));
798         QTreeWidgetItem *item = m_listView->currentItem();
799         if (item && item->type() == PROJECTCLIPTYPE) {
800             m_editButton->defaultAction()->setEnabled(true);
801             m_openAction->setEnabled(true);
802             m_reloadAction->setEnabled(true);
803             m_transcodeAction->setEnabled(true);
804             return;
805         }
806     }
807
808     m_editButton->defaultAction()->setEnabled(false);
809     m_openAction->setEnabled(false);
810     m_reloadAction->setEnabled(false);
811     m_transcodeAction->setEnabled(false);
812 }
813
814 void ProjectList::selectItemById(const QString &clipId)
815 {
816     ProjectItem *item = getItemById(clipId);
817     if (item)
818         m_listView->setCurrentItem(item);
819 }
820
821
822 void ProjectList::slotDeleteClip(const QString &clipId)
823 {
824     ProjectItem *item = getItemById(clipId);
825     if (!item) {
826         kDebug() << "/// Cannot find clip to delete";
827         return;
828     }
829     m_listView->blockSignals(true);
830     QTreeWidgetItem *newSelectedItem = m_listView->itemAbove(item);
831     if (!newSelectedItem)
832         newSelectedItem = m_listView->itemBelow(item);
833     delete item;
834     m_doc->clipManager()->deleteClip(clipId);
835     m_listView->blockSignals(false);
836     if (newSelectedItem) {
837         m_listView->setCurrentItem(newSelectedItem);
838     } else {
839         updateButtons();
840         emit clipSelected(NULL);
841     }
842 }
843
844
845 void ProjectList::editFolder(const QString folderName, const QString oldfolderName, const QString &clipId)
846 {
847     EditFolderCommand *command = new EditFolderCommand(this, folderName, oldfolderName, clipId, false);
848     m_commandStack->push(command);
849     m_doc->setModified(true);
850 }
851
852 void ProjectList::slotAddFolder()
853 {
854     AddFolderCommand *command = new AddFolderCommand(this, i18n("Folder"), QString::number(m_doc->clipManager()->getFreeFolderId()), true);
855     m_commandStack->push(command);
856 }
857
858 void ProjectList::slotAddFolder(const QString foldername, const QString &clipId, bool remove, bool edit)
859 {
860     if (remove) {
861         FolderProjectItem *item = getFolderItemById(clipId);
862         if (item) {
863             m_doc->clipManager()->deleteFolder(clipId);
864             QTreeWidgetItem *newSelectedItem = m_listView->itemAbove(item);
865             if (!newSelectedItem)
866                 newSelectedItem = m_listView->itemBelow(item);
867             delete item;
868             if (newSelectedItem)
869                 m_listView->setCurrentItem(newSelectedItem);
870             else
871                 updateButtons();
872         }
873     } else {
874         if (edit) {
875             FolderProjectItem *item = getFolderItemById(clipId);
876             if (item) {
877                 m_listView->blockSignals(true);
878                 item->setGroupName(foldername);
879                 m_listView->blockSignals(false);
880                 m_doc->clipManager()->addFolder(clipId, foldername);
881                 const int children = item->childCount();
882                 for (int i = 0; i < children; i++) {
883                     ProjectItem *child = static_cast <ProjectItem *>(item->child(i));
884                     child->setProperty("groupname", foldername);
885                 }
886             }
887         } else {
888             m_listView->blockSignals(true);
889             m_listView->setCurrentItem(new FolderProjectItem(m_listView, QStringList() << foldername, clipId));
890             m_doc->clipManager()->addFolder(clipId, foldername);
891             m_listView->blockSignals(false);
892             m_listView->editItem(m_listView->currentItem(), 0);
893         }
894         updateButtons();
895     }
896     m_doc->setModified(true);
897 }
898
899
900
901 void ProjectList::deleteProjectFolder(QMap <QString, QString> map)
902 {
903     QMapIterator<QString, QString> i(map);
904     QUndoCommand *delCommand = new QUndoCommand();
905     delCommand->setText(i18n("Delete Folder"));
906     while (i.hasNext()) {
907         i.next();
908         new AddFolderCommand(this, i.key(), i.value(), false, delCommand);
909     }
910     m_commandStack->push(delCommand);
911 }
912
913 void ProjectList::slotAddClip(DocClipBase *clip, bool getProperties)
914 {
915     m_listView->setEnabled(false);
916     if (getProperties) m_listView->blockSignals(true);
917     const QString parent = clip->getProperty("groupid");
918     ProjectItem *item = NULL;
919     if (!parent.isEmpty()) {
920         FolderProjectItem *parentitem = getFolderItemById(parent);
921         if (!parentitem) {
922             QStringList text;
923             QString groupName = clip->getProperty("groupname");
924             //kDebug() << "Adding clip to new group: " << groupName;
925             if (groupName.isEmpty()) groupName = i18n("Folder");
926             text << groupName;
927             parentitem = new FolderProjectItem(m_listView, text, parent);
928         }
929
930         if (parentitem)
931             item = new ProjectItem(parentitem, clip);
932     }
933     if (item == NULL)
934         item = new ProjectItem(m_listView, clip);
935     if (item->data(0, DurationRole).isNull()) item->setData(0, DurationRole, i18n("Loading"));
936     if (getProperties) {
937         m_listView->blockSignals(true);
938         m_refreshed = false;
939         
940         // Proxy clips
941         CLIPTYPE t = clip->clipType();
942         if ((t == VIDEO || t == AV || t == UNKNOWN) && KdenliveSettings::enableproxy()) {
943             if (clip->getProperty("proxy").isEmpty()) {
944                 connect(clip, SIGNAL(proxyReady(const QString, bool)), this, SLOT(slotGotProxy(const QString, bool)));
945                 item->setProxyStatus(1);
946                 clip->generateProxy(m_doc->projectFolder());
947             }
948             else {
949                 // Proxy clip already created
950                 item->setProxyStatus(2);
951                 QDomElement e = clip->toXML().cloneNode().toElement();
952                 e.removeAttribute("file_hash");
953                 m_infoQueue.insert(clip->getId(), e);
954                 
955             }
956         }
957         else {
958             // We don't use proxies
959             // remove file_hash so that we load all properties for the clip
960             QDomElement e = clip->toXML().cloneNode().toElement();
961             e.removeAttribute("file_hash");
962             m_infoQueue.insert(clip->getId(), e);
963         }
964         //m_render->getFileProperties(clip->toXML(), clip->getId(), true);
965     }
966     clip->askForAudioThumbs();
967     
968     KUrl url = clip->fileURL();
969     if (getProperties == false && !clip->getClipHash().isEmpty()) {
970         QString cachedPixmap = m_doc->projectFolder().path(KUrl::AddTrailingSlash) + "thumbs/" + clip->getClipHash() + ".png";
971         if (QFile::exists(cachedPixmap)) {
972             QPixmap pix(cachedPixmap);
973             if (pix.isNull())
974                 KIO::NetAccess::del(KUrl(cachedPixmap), this);
975             item->setData(0, Qt::DecorationRole, pix);
976         }
977     }
978 #ifdef NEPOMUK
979     if (!url.isEmpty() && KdenliveSettings::activate_nepomuk()) {
980         // if file has Nepomuk comment, use it
981         Nepomuk::Resource f(url.path());
982         QString annotation = f.description();
983         if (!annotation.isEmpty()) item->setText(1, annotation);
984         item->setText(2, QString::number(f.rating()));
985     }
986 #endif
987     // Add cut zones
988     QList <CutZoneInfo> cuts = clip->cutZones();
989     if (!cuts.isEmpty()) {
990         for (int i = 0; i < cuts.count(); i++) {
991             SubProjectItem *sub = new SubProjectItem(item, cuts.at(i).zone.x(), cuts.at(i).zone.y(), cuts.at(i).description);
992             if (!clip->getClipHash().isEmpty()) {
993                 QString cachedPixmap = m_doc->projectFolder().path(KUrl::AddTrailingSlash) + "thumbs/" + clip->getClipHash() + '#' + QString::number(cuts.at(i).zone.x()) + ".png";
994                 if (QFile::exists(cachedPixmap)) {
995                     QPixmap pix(cachedPixmap);
996                     if (pix.isNull())
997                         KIO::NetAccess::del(KUrl(cachedPixmap), this);
998                     sub->setData(0, Qt::DecorationRole, pix);
999                 }
1000             }
1001         }
1002     }
1003     if (m_listView->isEnabled()) {
1004         updateButtons();
1005         if (getProperties)
1006             m_listView->blockSignals(false);
1007     }
1008     
1009     if (getProperties && !m_queueTimer.isActive())
1010         slotProcessNextClipInQueue();
1011 }
1012
1013 void ProjectList::slotGotProxy(const QString id, bool success)
1014 {
1015     ProjectItem *item = getItemById(id);
1016     if (item) {
1017         disconnect(m_listView, SIGNAL(itemChanged(QTreeWidgetItem *, int)), this, SLOT(slotItemEdited(QTreeWidgetItem *, int)));
1018         if (success) {
1019             // Proxy clip successfully created
1020             item->setProxyStatus(2);
1021             QDomElement e = item->referencedClip()->toXML().cloneNode().toElement();  
1022             e.removeAttribute("file_hash");
1023             m_infoQueue.insert(id, e);
1024             if (!m_queueTimer.isActive()) slotProcessNextClipInQueue();
1025         }
1026         else item->setProxyStatus(0);
1027         connect(m_listView, SIGNAL(itemChanged(QTreeWidgetItem *, int)), this, SLOT(slotItemEdited(QTreeWidgetItem *, int)));
1028         update();
1029     }
1030 }
1031
1032 void ProjectList::slotResetProjectList()
1033 {
1034     m_listView->clear();
1035     emit clipSelected(NULL);
1036     m_thumbnailQueue.clear();
1037     m_infoQueue.clear();
1038     m_refreshed = false;
1039 }
1040
1041 void ProjectList::requestClipInfo(const QDomElement xml, const QString id)
1042 {
1043     m_refreshed = false;
1044     m_infoQueue.insert(id, xml);
1045     //if (m_infoQueue.count() == 1 || ) QTimer::singleShot(300, this, SLOT(slotProcessNextClipInQueue()));
1046 }
1047
1048 void ProjectList::slotProcessNextClipInQueue()
1049 {
1050     if (m_infoQueue.isEmpty()) {
1051         slotProcessNextThumbnail();
1052         return;
1053     }
1054
1055     QMap<QString, QDomElement>::const_iterator j = m_infoQueue.constBegin();
1056     if (j != m_infoQueue.constEnd()) {
1057         const QDomElement dom = j.value();
1058         const QString id = j.key();
1059         m_infoQueue.remove(j.key());
1060         emit getFileProperties(dom, id, m_listView->iconSize().height(), false);
1061     }
1062     if (!m_infoQueue.isEmpty()) m_queueTimer.start();
1063 }
1064
1065 void ProjectList::slotUpdateClip(const QString &id)
1066 {
1067     ProjectItem *item = getItemById(id);
1068     m_listView->blockSignals(true);
1069     if (item) item->setData(0, UsageRole, QString::number(item->numReferences()));
1070     m_listView->blockSignals(false);
1071 }
1072
1073 void ProjectList::updateAllClips()
1074 {
1075     m_listView->setSortingEnabled(false);
1076     kDebug() << "// UPDATE ALL CLPY";
1077
1078     QTreeWidgetItemIterator it(m_listView);
1079     DocClipBase *clip;
1080     ProjectItem *item;
1081     m_listView->blockSignals(true);
1082     while (*it) {
1083         if ((*it)->type() == PROJECTSUBCLIPTYPE) {
1084             // subitem
1085             SubProjectItem *sub = static_cast <SubProjectItem *>(*it);
1086             if (sub->data(0, Qt::DecorationRole).isNull()) {
1087                 item = static_cast <ProjectItem *>((*it)->parent());
1088                 requestClipThumbnail(item->clipId() + '#' + QString::number(sub->zone().x()));
1089             }
1090             ++it;
1091             continue;
1092         } else if ((*it)->type() == PROJECTFOLDERTYPE) {
1093             // folder
1094             ++it;
1095             continue;
1096         } else {
1097             item = static_cast <ProjectItem *>(*it);
1098             clip = item->referencedClip();
1099             if (item->referencedClip()->producer() == NULL) {
1100                 if (clip->isPlaceHolder() == false)
1101                     requestClipInfo(clip->toXML(), clip->getId());
1102                 else if (!clip->isPlaceHolder())
1103                     item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsDropEnabled);
1104             } else {
1105                 if (item->data(0, Qt::DecorationRole).isNull())
1106                     requestClipThumbnail(clip->getId());
1107                 if (item->data(0, DurationRole).toString().isEmpty())
1108                     item->changeDuration(item->referencedClip()->producer()->get_playtime());
1109             }
1110             item->setData(0, UsageRole, QString::number(item->numReferences()));
1111         }
1112         //qApp->processEvents();
1113         ++it;
1114     }
1115     if (!m_queueTimer.isActive())
1116         m_queueTimer.start();
1117     if (m_listView->isEnabled())
1118         m_listView->blockSignals(false);
1119     m_listView->setSortingEnabled(true);
1120     if (m_infoQueue.isEmpty())
1121         slotProcessNextThumbnail();
1122 }
1123
1124 // static
1125 QString ProjectList::getExtensions()
1126 {
1127     // Build list of mime types
1128     QStringList mimeTypes = QStringList() << "application/x-kdenlive" << "application/x-kdenlivetitle" << "video/mlt-playlist" << "text/plain"
1129                             << "video/x-flv" << "application/vnd.rn-realmedia" << "video/x-dv" << "video/dv" << "video/x-msvideo" << "video/x-matroska" << "video/mpeg" << "video/ogg" << "video/x-ms-wmv" << "video/mp4" << "video/quicktime" << "video/webm"
1130                             << "audio/x-flac" << "audio/x-matroska" << "audio/mp4" << "audio/mpeg" << "audio/x-mp3" << "audio/ogg" << "audio/x-wav" << "application/ogg" << "application/mxf" << "application/x-shockwave-flash"
1131                             << "image/gif" << "image/jpeg" << "image/png" << "image/x-tga" << "image/x-bmp" << "image/svg+xml" << "image/tiff" << "image/x-xcf" << "image/x-xcf-gimp" << "image/x-vnd.adobe.photoshop" << "image/x-pcx" << "image/x-exr";
1132
1133     QString allExtensions;
1134     foreach(const QString & mimeType, mimeTypes) {
1135         KMimeType::Ptr mime(KMimeType::mimeType(mimeType));
1136         if (mime) {
1137             allExtensions.append(mime->patterns().join(" "));
1138             allExtensions.append(' ');
1139         }
1140     }
1141     return allExtensions.simplified();
1142 }
1143
1144 void ProjectList::slotAddClip(const QList <QUrl> givenList, const QString &groupName, const QString &groupId)
1145 {
1146     if (!m_commandStack)
1147         kDebug() << "!!!!!!!!!!!!!!!! NO CMD STK";
1148
1149     KUrl::List list;
1150     if (givenList.isEmpty()) {
1151         QString allExtensions = getExtensions();
1152         const QString dialogFilter = allExtensions + ' ' + QLatin1Char('|') + i18n("All Supported Files") + "\n* " + QLatin1Char('|') + i18n("All Files");
1153         QCheckBox *b = new QCheckBox(i18n("Import image sequence"));
1154         b->setChecked(KdenliveSettings::autoimagesequence());
1155         KFileDialog *d = new KFileDialog(KUrl("kfiledialog:///clipfolder"), dialogFilter, kapp->activeWindow(), b);
1156         d->setOperationMode(KFileDialog::Opening);
1157         d->setMode(KFile::Files);
1158         d->exec();
1159         list = d->selectedUrls();
1160         if (b->isChecked() && list.count() == 1) {
1161             // Check for image sequence
1162             KUrl url = list.at(0);
1163             QString fileName = url.fileName().section('.', 0, -2);
1164             if (fileName.at(fileName.size() - 1).isDigit()) {
1165                 KFileItem item(KFileItem::Unknown, KFileItem::Unknown, url);
1166                 if (item.mimetype().startsWith("image")) {
1167                     // import as sequence if we found more than one image in the sequence
1168                     QStringList list;
1169                     QString pattern = SlideshowClip::selectedPath(url.path(), false, QString(), &list);
1170                     int count = list.count();
1171                     if (count > 1) {
1172                         delete d;
1173                         QStringList groupInfo = getGroup();
1174
1175                         // get image sequence base name
1176                         while (fileName.at(fileName.size() - 1).isDigit()) {
1177                             fileName.chop(1);
1178                         }
1179
1180                         m_doc->slotCreateSlideshowClipFile(fileName, pattern, count, m_timecode.reformatSeparators(KdenliveSettings::sequence_duration()),
1181                                                            false, false, false,
1182                                                            m_timecode.getTimecodeFromFrames(int(ceil(m_timecode.fps()))), QString(), 0,
1183                                                            QString(), groupInfo.at(0), groupInfo.at(1));
1184                         return;
1185                     }
1186                 }
1187             }
1188         }
1189         delete d;
1190     } else {
1191         for (int i = 0; i < givenList.count(); i++)
1192             list << givenList.at(i);
1193     }
1194
1195     foreach(const KUrl & file, list) {
1196         // Check there is no folder here
1197         KMimeType::Ptr type = KMimeType::findByUrl(file);
1198         if (type->is("inode/directory")) {
1199             // user dropped a folder
1200             list.removeAll(file);
1201         }
1202     }
1203
1204     if (list.isEmpty())
1205         return;
1206
1207     if (givenList.isEmpty()) {
1208         QStringList groupInfo = getGroup();
1209         m_doc->slotAddClipList(list, groupInfo.at(0), groupInfo.at(1));
1210     } else {
1211         m_doc->slotAddClipList(list, groupName, groupId);
1212     }
1213 }
1214
1215 void ProjectList::slotRemoveInvalidClip(const QString &id, bool replace)
1216 {
1217     ProjectItem *item = getItemById(id);
1218     QTimer::singleShot(300, this, SLOT(slotProcessNextClipInQueue()));
1219     if (item) {
1220         const QString path = item->referencedClip()->fileURL().path();
1221         if (item->referencedClip()->isPlaceHolder()) replace = false;
1222         if (!path.isEmpty()) {
1223             if (replace)
1224                 KMessageBox::sorry(kapp->activeWindow(), i18n("Clip <b>%1</b><br />is invalid, will be removed from project.", path));
1225             else if (KMessageBox::questionYesNo(kapp->activeWindow(), i18n("Clip <b>%1</b><br />is missing or invalid. Remove it from project?", path), i18n("Invalid clip")) == KMessageBox::Yes)
1226                 replace = true;
1227         }
1228         if (replace)
1229             emit deleteProjectClips(QStringList() << id, QMap <QString, QString>());
1230     }
1231 }
1232
1233 void ProjectList::slotAddColorClip()
1234 {
1235     if (!m_commandStack)
1236         kDebug() << "!!!!!!!!!!!!!!!! NO CMD STK";
1237
1238     QDialog *dia = new QDialog(this);
1239     Ui::ColorClip_UI dia_ui;
1240     dia_ui.setupUi(dia);
1241     dia->setWindowTitle(i18n("Color Clip"));
1242     dia_ui.clip_name->setText(i18n("Color Clip"));
1243
1244     TimecodeDisplay *t = new TimecodeDisplay(m_timecode);
1245     t->setValue(KdenliveSettings::color_duration());
1246     t->setTimeCodeFormat(false);
1247     dia_ui.clip_durationBox->addWidget(t);
1248     dia_ui.clip_color->setColor(KdenliveSettings::colorclipcolor());
1249
1250     if (dia->exec() == QDialog::Accepted) {
1251         QString color = dia_ui.clip_color->color().name();
1252         KdenliveSettings::setColorclipcolor(color);
1253         color = color.replace(0, 1, "0x") + "ff";
1254         QStringList groupInfo = getGroup();
1255         m_doc->slotCreateColorClip(dia_ui.clip_name->text(), color, m_timecode.getTimecode(t->gentime()), groupInfo.at(0), groupInfo.at(1));
1256     }
1257     delete t;
1258     delete dia;
1259 }
1260
1261
1262 void ProjectList::slotAddSlideshowClip()
1263 {
1264     if (!m_commandStack)
1265         kDebug() << "!!!!!!!!!!!!!!!! NO CMD STK";
1266
1267     SlideshowClip *dia = new SlideshowClip(m_timecode, this);
1268
1269     if (dia->exec() == QDialog::Accepted) {
1270         QStringList groupInfo = getGroup();
1271         m_doc->slotCreateSlideshowClipFile(dia->clipName(), dia->selectedPath(), dia->imageCount(), dia->clipDuration(),
1272                                            dia->loop(), dia->crop(), dia->fade(),
1273                                            dia->lumaDuration(), dia->lumaFile(), dia->softness(),
1274                                            dia->animation(), groupInfo.at(0), groupInfo.at(1));
1275     }
1276     delete dia;
1277 }
1278
1279 void ProjectList::slotAddTitleClip()
1280 {
1281     QStringList groupInfo = getGroup();
1282     m_doc->slotCreateTextClip(groupInfo.at(0), groupInfo.at(1));
1283 }
1284
1285 void ProjectList::slotAddTitleTemplateClip()
1286 {
1287     if (!m_commandStack)
1288         kDebug() << "!!!!!!!!!!!!!!!! NO CMD STK";
1289
1290     QStringList groupInfo = getGroup();
1291
1292     // Get the list of existing templates
1293     QStringList filter;
1294     filter << "*.kdenlivetitle";
1295     const QString path = m_doc->projectFolder().path(KUrl::AddTrailingSlash) + "titles/";
1296     QStringList templateFiles = QDir(path).entryList(filter, QDir::Files);
1297
1298     QDialog *dia = new QDialog(this);
1299     Ui::TemplateClip_UI dia_ui;
1300     dia_ui.setupUi(dia);
1301     for (int i = 0; i < templateFiles.size(); ++i)
1302         dia_ui.template_list->comboBox()->addItem(templateFiles.at(i), path + templateFiles.at(i));
1303
1304     if (!templateFiles.isEmpty())
1305         dia_ui.buttonBox->button(QDialogButtonBox::Ok)->setFocus();
1306     dia_ui.template_list->fileDialog()->setFilter("application/x-kdenlivetitle");
1307     //warning: setting base directory doesn't work??
1308     KUrl startDir(path);
1309     dia_ui.template_list->fileDialog()->setUrl(startDir);
1310     dia_ui.text_box->setHidden(true);
1311     if (dia->exec() == QDialog::Accepted) {
1312         QString textTemplate = dia_ui.template_list->comboBox()->itemData(dia_ui.template_list->comboBox()->currentIndex()).toString();
1313         if (textTemplate.isEmpty()) textTemplate = dia_ui.template_list->comboBox()->currentText();
1314         // Create a cloned template clip
1315         m_doc->slotCreateTextTemplateClip(groupInfo.at(0), groupInfo.at(1), KUrl(textTemplate));
1316     }
1317     delete dia;
1318 }
1319
1320 QStringList ProjectList::getGroup() const
1321 {
1322     QStringList result;
1323     QTreeWidgetItem *item = m_listView->currentItem();
1324     while (item && item->type() != PROJECTFOLDERTYPE)
1325         item = item->parent();
1326
1327     if (item) {
1328         FolderProjectItem *folder = static_cast <FolderProjectItem *>(item);
1329         result << folder->groupName() << folder->clipId();
1330     } else {
1331         result << QString() << QString();
1332     }
1333     return result;
1334 }
1335
1336 void ProjectList::setDocument(KdenliveDoc *doc)
1337 {
1338     m_listView->blockSignals(true);
1339     m_listView->clear();
1340     m_listView->setSortingEnabled(false);
1341     emit clipSelected(NULL);
1342     m_thumbnailQueue.clear();
1343     m_infoQueue.clear();
1344     m_refreshed = false;
1345     m_fps = doc->fps();
1346     m_timecode = doc->timecode();
1347     m_commandStack = doc->commandStack();
1348     m_doc = doc;
1349
1350     QMap <QString, QString> flist = doc->clipManager()->documentFolderList();
1351     QMapIterator<QString, QString> f(flist);
1352     while (f.hasNext()) {
1353         f.next();
1354         (void) new FolderProjectItem(m_listView, QStringList() << f.value(), f.key());
1355     }
1356
1357     QList <DocClipBase*> list = doc->clipManager()->documentClipList();
1358     for (int i = 0; i < list.count(); i++)
1359         slotAddClip(list.at(i), false);
1360
1361     m_listView->blockSignals(false);
1362     connect(m_doc->clipManager(), SIGNAL(reloadClip(const QString &)), this, SLOT(slotReloadClip(const QString &)));
1363     connect(m_doc->clipManager(), SIGNAL(modifiedClip(const QString &)), this, SLOT(slotModifiedClip(const QString &)));
1364     connect(m_doc->clipManager(), SIGNAL(missingClip(const QString &)), this, SLOT(slotMissingClip(const QString &)));
1365     connect(m_doc->clipManager(), SIGNAL(availableClip(const QString &)), this, SLOT(slotAvailableClip(const QString &)));
1366     connect(m_doc->clipManager(), SIGNAL(checkAllClips()), this, SLOT(updateAllClips()));
1367 }
1368
1369 QList <DocClipBase*> ProjectList::documentClipList() const
1370 {
1371     if (m_doc == NULL)
1372         return QList <DocClipBase*> ();
1373
1374     return m_doc->clipManager()->documentClipList();
1375 }
1376
1377 QDomElement ProjectList::producersList()
1378 {
1379     QDomDocument doc;
1380     QDomElement prods = doc.createElement("producerlist");
1381     doc.appendChild(prods);
1382     kDebug() << "////////////  PRO LIST BUILD PRDSLIST ";
1383     QTreeWidgetItemIterator it(m_listView);
1384     while (*it) {
1385         if ((*it)->type() != PROJECTCLIPTYPE) {
1386             // subitem
1387             ++it;
1388             continue;
1389         }
1390         prods.appendChild(doc.importNode(((ProjectItem *)(*it))->toXml(), true));
1391         ++it;
1392     }
1393     return prods;
1394 }
1395
1396 void ProjectList::slotCheckForEmptyQueue()
1397 {
1398     if (!m_refreshed && m_thumbnailQueue.isEmpty() && m_infoQueue.isEmpty()) {
1399         m_refreshed = true;
1400         emit loadingIsOver();
1401         emit displayMessage(QString(), -1);
1402         m_listView->blockSignals(false);
1403         m_listView->setEnabled(true);
1404         updateButtons();
1405     } else if (!m_refreshed) {
1406         QTimer::singleShot(300, this, SLOT(slotCheckForEmptyQueue()));
1407     }
1408 }
1409
1410 void ProjectList::reloadClipThumbnails()
1411 {
1412     m_thumbnailQueue.clear();
1413     QTreeWidgetItemIterator it(m_listView);
1414     while (*it) {
1415         if ((*it)->type() != PROJECTCLIPTYPE) {
1416             // subitem
1417             ++it;
1418             continue;
1419         }
1420         m_thumbnailQueue << ((ProjectItem *)(*it))->clipId();
1421         ++it;
1422     }
1423     QTimer::singleShot(300, this, SLOT(slotProcessNextThumbnail()));
1424 }
1425
1426 void ProjectList::requestClipThumbnail(const QString id)
1427 {
1428     if (!m_thumbnailQueue.contains(id)) m_thumbnailQueue.append(id);
1429 }
1430
1431 void ProjectList::slotProcessNextThumbnail()
1432 {
1433     if (m_thumbnailQueue.isEmpty() && m_infoQueue.isEmpty()) {
1434         slotCheckForEmptyQueue();
1435         return;
1436     }
1437     if (!m_infoQueue.isEmpty()) {
1438         //QTimer::singleShot(300, this, SLOT(slotProcessNextThumbnail()));
1439         return;
1440     }
1441     if (m_thumbnailQueue.count() > 1) {
1442         int max = m_doc->clipManager()->clipsCount();
1443         emit displayMessage(i18n("Loading thumbnails"), (int)(100 *(max - m_thumbnailQueue.count()) / max));
1444     }
1445     slotRefreshClipThumbnail(m_thumbnailQueue.takeFirst(), false);
1446 }
1447
1448 void ProjectList::slotRefreshClipThumbnail(const QString &clipId, bool update)
1449 {
1450     QTreeWidgetItem *item = getAnyItemById(clipId);
1451     if (item)
1452         slotRefreshClipThumbnail(item, update);
1453     else
1454         slotProcessNextThumbnail();
1455 }
1456
1457 void ProjectList::slotRefreshClipThumbnail(QTreeWidgetItem *it, bool update)
1458 {
1459     if (it == NULL) return;
1460     ProjectItem *item = NULL;
1461     bool isSubItem = false;
1462     int frame;
1463     if (it->type() == PROJECTFOLDERTYPE) return;
1464     if (it->type() == PROJECTSUBCLIPTYPE) {
1465         item = static_cast <ProjectItem *>(it->parent());
1466         frame = static_cast <SubProjectItem *>(it)->zone().x();
1467         isSubItem = true;
1468     } else {
1469         item = static_cast <ProjectItem *>(it);
1470         frame = item->referencedClip()->getClipThumbFrame();
1471     }
1472
1473     if (item) {
1474         DocClipBase *clip = item->referencedClip();
1475         if (!clip) {
1476             slotProcessNextThumbnail();
1477             return;
1478         }
1479         QPixmap pix;
1480         int height = m_listView->iconSize().height();
1481         int width = (int)(height  * m_render->dar());
1482         if (clip->clipType() == AUDIO)
1483             pix = KIcon("audio-x-generic").pixmap(QSize(width, height));
1484         else if (clip->clipType() == IMAGE)
1485             pix = QPixmap::fromImage(KThumb::getFrame(item->referencedClip()->producer(), 0, width, height));
1486         else
1487             pix = item->referencedClip()->thumbProducer()->extractImage(frame, width, height);
1488
1489         if (!pix.isNull()) {
1490             m_listView->blockSignals(true);
1491             it->setData(0, Qt::DecorationRole, pix);
1492             if (m_listView->isEnabled())
1493                 m_listView->blockSignals(false);
1494             if (!isSubItem)
1495                 m_doc->cachePixmap(item->getClipHash(), pix);
1496             else
1497                 m_doc->cachePixmap(item->getClipHash() + '#' + QString::number(frame), pix);
1498         }
1499         if (update)
1500             emit projectModified();
1501
1502         slotProcessNextThumbnail();
1503     }
1504 }
1505
1506 void ProjectList::slotReplyGetFileProperties(const QString &clipId, Mlt::Producer *producer, const QMap < QString, QString > &properties, const QMap < QString, QString > &metadata, bool replace)
1507 {
1508     QString toReload;
1509     ProjectItem *item = getItemById(clipId);
1510     if (item && producer) {
1511         m_listView->blockSignals(true);
1512         item->setProperties(properties, metadata);
1513         if (item->referencedClip()->isPlaceHolder() && producer->is_valid()) {
1514             item->referencedClip()->setValid();
1515             item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsDragEnabled | Qt::ItemIsEnabled | Qt::ItemIsEditable | Qt::ItemIsDropEnabled);
1516             toReload = clipId;
1517         }
1518         item->referencedClip()->setProducer(producer, replace);
1519         item->referencedClip()->askForAudioThumbs();
1520         if (!replace && item->data(0, Qt::DecorationRole).isNull())
1521             requestClipThumbnail(clipId);
1522         if (!toReload.isEmpty())
1523             item->slotSetToolTip();
1524
1525         if (m_listView->isEnabled() && replace) {
1526             // update clip in clip monitor
1527             emit clipSelected(NULL);
1528             emit clipSelected(item->referencedClip());
1529             //TODO: Make sure the line below has no side effect
1530             toReload = clipId;
1531         }
1532         /*else {
1533             // Check if duration changed.
1534             emit receivedClipDuration(clipId);
1535             delete producer;
1536         }*/
1537         if (m_listView->isEnabled())
1538             m_listView->blockSignals(false);
1539         /*if (item->icon(0).isNull()) {
1540             requestClipThumbnail(clipId);
1541         }*/
1542     } else kDebug() << "////////  COULD NOT FIND CLIP TO UPDATE PRPS...";
1543     if (item && m_infoQueue.isEmpty() && m_thumbnailQueue.isEmpty()) {
1544         m_listView->setCurrentItem(item);
1545         bool updatedProfile = false;
1546         if (item->parent()) {
1547             if (item->parent()->type() == PROJECTFOLDERTYPE)
1548                 static_cast <FolderProjectItem *>(item->parent())->switchIcon();
1549         } else if (KdenliveSettings::checkfirstprojectclip() &&  m_listView->topLevelItemCount() == 1) {
1550             // this is the first clip loaded in project, check if we want to adjust project settings to the clip
1551             updatedProfile = adjustProjectProfileToItem(item);
1552         }
1553         if (updatedProfile == false) emit clipSelected(item->referencedClip());
1554     } else {
1555         int max = m_doc->clipManager()->clipsCount();
1556         emit displayMessage(i18n("Loading clips"), (int)(100 *(max - m_infoQueue.count()) / max));
1557     }
1558     if (!toReload.isEmpty())
1559         emit clipNeedsReload(toReload, true);
1560
1561     qApp->processEvents();
1562     slotProcessNextClipInQueue();
1563 }
1564
1565 bool ProjectList::adjustProjectProfileToItem(ProjectItem *item)
1566 {
1567     if (item == NULL) {
1568         if (m_listView->currentItem() && m_listView->currentItem()->type() != PROJECTFOLDERTYPE)
1569             item = static_cast <ProjectItem*>(m_listView->currentItem());
1570     }
1571     if (item == NULL || item->referencedClip() == NULL) {
1572         KMessageBox::information(kapp->activeWindow(), i18n("Cannot find profile from current clip"));
1573         return false;
1574     }
1575     bool profileUpdated = false;
1576     QString size = item->referencedClip()->getProperty("frame_size");
1577     int width = size.section('x', 0, 0).toInt();
1578     int height = size.section('x', -1).toInt();
1579     double fps = item->referencedClip()->getProperty("fps").toDouble();
1580     double par = item->referencedClip()->getProperty("aspect_ratio").toDouble();
1581     if (item->clipType() == IMAGE || item->clipType() == AV || item->clipType() == VIDEO) {
1582         if (ProfilesDialog::matchProfile(width, height, fps, par, item->clipType() == IMAGE, m_doc->mltProfile()) == false) {
1583             // get a list of compatible profiles
1584             QMap <QString, QString> suggestedProfiles = ProfilesDialog::getProfilesFromProperties(width, height, fps, par, item->clipType() == IMAGE);
1585             if (!suggestedProfiles.isEmpty()) {
1586                 KDialog *dialog = new KDialog(this);
1587                 dialog->setCaption(i18n("Change project profile"));
1588                 dialog->setButtons(KDialog::Ok | KDialog::Cancel);
1589
1590                 QWidget container;
1591                 QVBoxLayout *l = new QVBoxLayout;
1592                 QLabel *label = new QLabel(i18n("Your clip does not match current project's profile.\nDo you want to change the project profile?\n\nThe following profiles match the clip (size: %1, fps: %2)", size, fps));
1593                 l->addWidget(label);
1594                 QListWidget *list = new QListWidget;
1595                 list->setAlternatingRowColors(true);
1596                 QMapIterator<QString, QString> i(suggestedProfiles);
1597                 while (i.hasNext()) {
1598                     i.next();
1599                     QListWidgetItem *item = new QListWidgetItem(i.value(), list);
1600                     item->setData(Qt::UserRole, i.key());
1601                     item->setToolTip(i.key());
1602                 }
1603                 list->setCurrentRow(0);
1604                 l->addWidget(list);
1605                 container.setLayout(l);
1606                 dialog->setButtonText(KDialog::Ok, i18n("Update profile"));
1607                 dialog->setMainWidget(&container);
1608                 if (dialog->exec() == QDialog::Accepted) {
1609                     //Change project profile
1610                     profileUpdated = true;
1611                     if (list->currentItem())
1612                         emit updateProfile(list->currentItem()->data(Qt::UserRole).toString());
1613                 }
1614                 delete list;
1615                 delete label;
1616             } else if (fps > 0) {
1617                 KMessageBox::information(kapp->activeWindow(), i18n("Your clip does not match current project's profile.\nNo existing profile found to match the clip's properties.\nClip size: %1\nFps: %2\n", size, fps));
1618             }
1619         }
1620     }
1621     return profileUpdated;
1622 }
1623
1624 void ProjectList::slotReplyGetImage(const QString &clipId, const QPixmap &pix)
1625 {
1626     ProjectItem *item = getItemById(clipId);
1627     if (item && !pix.isNull()) {
1628         m_listView->blockSignals(true);
1629         item->setData(0, Qt::DecorationRole, pix);
1630         m_doc->cachePixmap(item->getClipHash(), pix);
1631         if (m_listView->isEnabled())
1632             m_listView->blockSignals(false);
1633     }
1634 }
1635
1636 QTreeWidgetItem *ProjectList::getAnyItemById(const QString &id)
1637 {
1638     QTreeWidgetItemIterator it(m_listView);
1639     QString lookId = id;
1640     if (id.contains('#'))
1641         lookId = id.section('#', 0, 0);
1642
1643     ProjectItem *result = NULL;
1644     while (*it) {
1645         if ((*it)->type() != PROJECTCLIPTYPE) {
1646             // subitem
1647             ++it;
1648             continue;
1649         }
1650         ProjectItem *item = static_cast<ProjectItem *>(*it);
1651         if (item->clipId() == lookId) {
1652             result = item;
1653             break;
1654         }
1655         ++it;
1656     }
1657     if (result == NULL || !id.contains('#')) {
1658         return result;
1659     } else {
1660         for (int i = 0; i < result->childCount(); i++) {
1661             SubProjectItem *sub = static_cast <SubProjectItem *>(result->child(i));
1662             if (sub && sub->zone().x() == id.section('#', 1, 1).toInt())
1663                 return sub;
1664         }
1665     }
1666
1667     return NULL;
1668 }
1669
1670
1671 ProjectItem *ProjectList::getItemById(const QString &id)
1672 {
1673     ProjectItem *item;
1674     QTreeWidgetItemIterator it(m_listView);
1675     while (*it) {
1676         if ((*it)->type() != PROJECTCLIPTYPE) {
1677             // subitem
1678             ++it;
1679             continue;
1680         }
1681         item = static_cast<ProjectItem *>(*it);
1682         if (item->clipId() == id)
1683             return item;
1684         ++it;
1685     }
1686     return NULL;
1687 }
1688
1689 FolderProjectItem *ProjectList::getFolderItemById(const QString &id)
1690 {
1691     FolderProjectItem *item;
1692     QTreeWidgetItemIterator it(m_listView);
1693     while (*it) {
1694         if ((*it)->type() == PROJECTFOLDERTYPE) {
1695             item = static_cast<FolderProjectItem *>(*it);
1696             if (item->clipId() == id)
1697                 return item;
1698         }
1699         ++it;
1700     }
1701     return NULL;
1702 }
1703
1704 void ProjectList::slotSelectClip(const QString &ix)
1705 {
1706     ProjectItem *clip = getItemById(ix);
1707     if (clip) {
1708         m_listView->setCurrentItem(clip);
1709         m_listView->scrollToItem(clip);
1710         m_editButton->defaultAction()->setEnabled(true);
1711         m_deleteButton->defaultAction()->setEnabled(true);
1712         m_reloadAction->setEnabled(true);
1713         m_transcodeAction->setEnabled(true);
1714         if (clip->clipType() == IMAGE && !KdenliveSettings::defaultimageapp().isEmpty()) {
1715             m_openAction->setIcon(KIcon(KdenliveSettings::defaultimageapp()));
1716             m_openAction->setEnabled(true);
1717         } else if (clip->clipType() == AUDIO && !KdenliveSettings::defaultaudioapp().isEmpty()) {
1718             m_openAction->setIcon(KIcon(KdenliveSettings::defaultaudioapp()));
1719             m_openAction->setEnabled(true);
1720         } else {
1721             m_openAction->setEnabled(false);
1722         }
1723     }
1724 }
1725
1726 QString ProjectList::currentClipUrl() const
1727 {
1728     ProjectItem *item;
1729     if (!m_listView->currentItem() || m_listView->currentItem()->type() == PROJECTFOLDERTYPE) return QString();
1730     if (m_listView->currentItem()->type() == PROJECTSUBCLIPTYPE) {
1731         // subitem
1732         item = static_cast <ProjectItem*>(m_listView->currentItem()->parent());
1733     } else {
1734         item = static_cast <ProjectItem*>(m_listView->currentItem());
1735     }
1736     if (item == NULL)
1737         return QString();
1738     return item->clipUrl().path();
1739 }
1740
1741 KUrl::List ProjectList::getConditionalUrls(const QString &condition) const
1742 {
1743     KUrl::List result;
1744     ProjectItem *item;
1745     QList<QTreeWidgetItem *> list = m_listView->selectedItems();
1746     for (int i = 0; i < list.count(); i++) {
1747         if (list.at(i)->type() == PROJECTFOLDERTYPE)
1748             continue;
1749         if (list.at(i)->type() == PROJECTSUBCLIPTYPE) {
1750             // subitem
1751             item = static_cast <ProjectItem*>(list.at(i)->parent());
1752         } else {
1753             item = static_cast <ProjectItem*>(list.at(i));
1754         }
1755         if (item == NULL || item->type() == COLOR || item->type() == SLIDESHOW || item->type() == TEXT)
1756             continue;
1757         DocClipBase *clip = item->referencedClip();
1758         if (!condition.isEmpty()) {
1759             if (condition.startsWith("vcodec") && !clip->hasVideoCodec(condition.section('=', 1, 1)))
1760                 continue;
1761             else if (condition.startsWith("acodec") && !clip->hasAudioCodec(condition.section('=', 1, 1)))
1762                 continue;
1763         }
1764         result.append(item->clipUrl());
1765     }
1766     return result;
1767 }
1768
1769 void ProjectList::regenerateTemplate(const QString &id)
1770 {
1771     ProjectItem *clip = getItemById(id);
1772     if (clip)
1773         regenerateTemplate(clip);
1774 }
1775
1776 void ProjectList::regenerateTemplate(ProjectItem *clip)
1777 {
1778     //TODO: remove this unused method, only force_reload is necessary
1779     clip->referencedClip()->producer()->set("force_reload", 1);
1780 }
1781
1782 QDomDocument ProjectList::generateTemplateXml(QString path, const QString &replaceString)
1783 {
1784     QDomDocument doc;
1785     QFile file(path);
1786     if (!file.open(QIODevice::ReadOnly)) {
1787         kWarning() << "ERROR, CANNOT READ: " << path;
1788         return doc;
1789     }
1790     if (!doc.setContent(&file)) {
1791         kWarning() << "ERROR, CANNOT READ: " << path;
1792         file.close();
1793         return doc;
1794     }
1795     file.close();
1796     QDomNodeList texts = doc.elementsByTagName("content");
1797     for (int i = 0; i < texts.count(); i++) {
1798         QString data = texts.item(i).firstChild().nodeValue();
1799         data.replace("%s", replaceString);
1800         texts.item(i).firstChild().setNodeValue(data);
1801     }
1802     return doc;
1803 }
1804
1805
1806 void ProjectList::slotAddClipCut(const QString &id, int in, int out)
1807 {
1808     ProjectItem *clip = getItemById(id);
1809     if (clip == NULL || clip->referencedClip()->hasCutZone(QPoint(in, out)))
1810         return;
1811     AddClipCutCommand *command = new AddClipCutCommand(this, id, in, out, QString(), true, false);
1812     m_commandStack->push(command);
1813 }
1814
1815 void ProjectList::addClipCut(const QString &id, int in, int out, const QString desc, bool newItem)
1816 {
1817     ProjectItem *clip = getItemById(id);
1818     if (clip) {
1819         DocClipBase *base = clip->referencedClip();
1820         base->addCutZone(in, out);
1821         m_listView->blockSignals(true);
1822         SubProjectItem *sub = new SubProjectItem(clip, in, out, desc);
1823         if (newItem && desc.isEmpty() && !m_listView->isColumnHidden(1)) {
1824             if (!clip->isExpanded())
1825                 clip->setExpanded(true);
1826             m_listView->scrollToItem(sub);
1827             m_listView->editItem(sub, 1);
1828         }
1829         QPixmap p = clip->referencedClip()->thumbProducer()->extractImage(in, (int)(sub->sizeHint(0).height()  * m_render->dar()), sub->sizeHint(0).height() - 2);
1830         sub->setData(0, Qt::DecorationRole, p);
1831         m_doc->cachePixmap(clip->getClipHash() + '#' + QString::number(in), p);
1832         m_listView->blockSignals(false);
1833     }
1834     emit projectModified();
1835 }
1836
1837 void ProjectList::removeClipCut(const QString &id, int in, int out)
1838 {
1839     ProjectItem *clip = getItemById(id);
1840     if (clip) {
1841         DocClipBase *base = clip->referencedClip();
1842         base->removeCutZone(in, out);
1843         SubProjectItem *sub = getSubItem(clip, QPoint(in, out));
1844         if (sub) {
1845             m_listView->blockSignals(true);
1846             delete sub;
1847             m_listView->blockSignals(false);
1848         }
1849     }
1850     emit projectModified();
1851 }
1852
1853 SubProjectItem *ProjectList::getSubItem(ProjectItem *clip, QPoint zone)
1854 {
1855     SubProjectItem *sub = NULL;
1856     if (clip) {
1857         for (int i = 0; i < clip->childCount(); i++) {
1858             QTreeWidgetItem *it = clip->child(i);
1859             if (it->type() == PROJECTSUBCLIPTYPE) {
1860                 sub = static_cast <SubProjectItem*>(it);
1861                 if (sub->zone() == zone)
1862                     break;
1863                 else
1864                     sub = NULL;
1865             }
1866         }
1867     }
1868     return sub;
1869 }
1870
1871 void ProjectList::slotUpdateClipCut(QPoint p)
1872 {
1873     if (!m_listView->currentItem() || m_listView->currentItem()->type() != PROJECTSUBCLIPTYPE)
1874         return;
1875     SubProjectItem *sub = static_cast <SubProjectItem*>(m_listView->currentItem());
1876     ProjectItem *item = static_cast <ProjectItem *>(sub->parent());
1877     EditClipCutCommand *command = new EditClipCutCommand(this, item->clipId(), sub->zone(), p, sub->text(1), sub->text(1), true);
1878     m_commandStack->push(command);
1879 }
1880
1881 void ProjectList::doUpdateClipCut(const QString &id, const QPoint oldzone, const QPoint zone, const QString &comment)
1882 {
1883     ProjectItem *clip = getItemById(id);
1884     SubProjectItem *sub = getSubItem(clip, oldzone);
1885     if (sub == NULL || clip == NULL)
1886         return;
1887     DocClipBase *base = clip->referencedClip();
1888     base->updateCutZone(oldzone.x(), oldzone.y(), zone.x(), zone.y(), comment);
1889     m_listView->blockSignals(true);
1890     sub->setZone(zone);
1891     sub->setDescription(comment);
1892     m_listView->blockSignals(false);
1893     emit projectModified();
1894 }
1895
1896 void ProjectList::slotForceProcessing(const QString &id)
1897 {
1898     while (m_infoQueue.contains(id)) {
1899         slotProcessNextClipInQueue();
1900     }
1901 }
1902
1903 void ProjectList::slotAddOrUpdateSequence(const QString frameName)
1904 {
1905     QString fileName = KUrl(frameName).fileName().section('_', 0, -2);
1906     QStringList list;
1907     QString pattern = SlideshowClip::selectedPath(frameName, false, QString(), &list);
1908     int count = list.count();
1909     if (count > 1) {
1910         const QList <DocClipBase *> existing = m_doc->clipManager()->getClipByResource(pattern);
1911         if (!existing.isEmpty()) {
1912             // Sequence already exists, update
1913             QString id = existing.at(0)->getId();
1914             //ProjectItem *item = getItemById(id);
1915             QMap <QString, QString> oldprops;
1916             QMap <QString, QString> newprops;
1917             int ttl = existing.at(0)->getProperty("ttl").toInt();
1918             oldprops["out"] = existing.at(0)->getProperty("out");
1919             newprops["out"] = QString::number(ttl * count - 1);
1920             slotUpdateClipProperties(id, newprops);
1921             EditClipCommand *command = new EditClipCommand(this, id, oldprops, newprops, false);
1922             m_commandStack->push(command);
1923         } else {
1924             // Create sequence
1925             QStringList groupInfo = getGroup();
1926             m_doc->slotCreateSlideshowClipFile(fileName, pattern, count, m_timecode.reformatSeparators(KdenliveSettings::sequence_duration()),
1927                                                false, false, false,
1928                                                m_timecode.getTimecodeFromFrames(int(ceil(m_timecode.fps()))), QString(), 0,
1929                                                QString(), groupInfo.at(0), groupInfo.at(1));
1930         }
1931     } else emit displayMessage(i18n("Sequence not found"), -2);
1932 }
1933
1934 QMap <QString, QString> ProjectList::getProxies()
1935 {
1936     QMap <QString, QString> list;
1937     ProjectItem *item;
1938     QTreeWidgetItemIterator it(m_listView);
1939     while (*it) {
1940         if ((*it)->type() != PROJECTCLIPTYPE) {
1941             // subitem
1942             ++it;
1943             continue;
1944         }
1945         item = static_cast<ProjectItem *>(*it);
1946         if (item && item->referencedClip() != NULL) {
1947             QString proxy = item->referencedClip()->getProperty("proxy");
1948             if (!proxy.isEmpty()) list.insert(proxy, item->clipUrl().path());
1949         }
1950         ++it;
1951     }
1952     return list;
1953 }
1954
1955 #include "projectlist.moc"