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