]> git.sesse.net Git - kdenlive/blob - src/projectlist.cpp
b41fc50e8d33f926283d8749aa4aaaeb597c6d7c
[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                     int count = 0;
1108                     // import as sequence if we found more than one image in the sequence
1109                     QString pattern = SlideshowClip::selectedPath(url.path(), false, QString(), &count);
1110                     if (count > 1) {
1111                         delete d;
1112                         QStringList groupInfo = getGroup();
1113
1114                         // get image sequence base name
1115                         while (fileName.at(fileName.size() - 1).isDigit()) {
1116                             fileName.chop(1);
1117                         }
1118
1119                         m_doc->slotCreateSlideshowClipFile(fileName, pattern, count, m_timecode.reformatSeparators(KdenliveSettings::sequence_duration()),
1120                                                            false, false, false,
1121                                                            m_timecode.getTimecodeFromFrames(int(ceil(m_timecode.fps()))), QString(), 0,
1122                                                            QString(), groupInfo.at(0), groupInfo.at(1));
1123                         return;
1124                     }
1125                 }
1126             }
1127         }
1128         delete d;
1129     } else {
1130         for (int i = 0; i < givenList.count(); i++)
1131             list << givenList.at(i);
1132     }
1133
1134     foreach(const KUrl & file, list) {
1135         // Check there is no folder here
1136         KMimeType::Ptr type = KMimeType::findByUrl(file);
1137         if (type->is("inode/directory")) {
1138             // user dropped a folder
1139             list.removeAll(file);
1140         }
1141     }
1142
1143     if (list.isEmpty())
1144         return;
1145
1146     if (givenList.isEmpty()) {
1147         QStringList groupInfo = getGroup();
1148         m_doc->slotAddClipList(list, groupInfo.at(0), groupInfo.at(1));
1149     } else {
1150         m_doc->slotAddClipList(list, groupName, groupId);
1151     }
1152 }
1153
1154 void ProjectList::slotRemoveInvalidClip(const QString &id, bool replace)
1155 {
1156     ProjectItem *item = getItemById(id);
1157     QTimer::singleShot(300, this, SLOT(slotProcessNextClipInQueue()));
1158     if (item) {
1159         const QString path = item->referencedClip()->fileURL().path();
1160         if (item->referencedClip()->isPlaceHolder()) replace = false;
1161         if (!path.isEmpty()) {
1162             if (replace)
1163                 KMessageBox::sorry(kapp->activeWindow(), i18n("Clip <b>%1</b><br />is invalid, will be removed from project.", path));
1164             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)
1165                 replace = true;
1166         }
1167         if (replace)
1168             emit deleteProjectClips(QStringList() << id, QMap <QString, QString>());
1169     }
1170 }
1171
1172 void ProjectList::slotAddColorClip()
1173 {
1174     if (!m_commandStack)
1175         kDebug() << "!!!!!!!!!!!!!!!! NO CMD STK";
1176
1177     QDialog *dia = new QDialog(this);
1178     Ui::ColorClip_UI dia_ui;
1179     dia_ui.setupUi(dia);
1180     dia->setWindowTitle(i18n("Color Clip"));
1181     dia_ui.clip_name->setText(i18n("Color Clip"));
1182
1183     TimecodeDisplay *t = new TimecodeDisplay(m_timecode);
1184     t->setValue(KdenliveSettings::color_duration());
1185     t->setTimeCodeFormat(false);
1186     dia_ui.clip_durationBox->addWidget(t);
1187     dia_ui.clip_color->setColor(KdenliveSettings::colorclipcolor());
1188
1189     if (dia->exec() == QDialog::Accepted) {
1190         QString color = dia_ui.clip_color->color().name();
1191         KdenliveSettings::setColorclipcolor(color);
1192         color = color.replace(0, 1, "0x") + "ff";
1193         QStringList groupInfo = getGroup();
1194         m_doc->slotCreateColorClip(dia_ui.clip_name->text(), color, m_timecode.getTimecode(t->gentime()), groupInfo.at(0), groupInfo.at(1));
1195     }
1196     delete t;
1197     delete dia;
1198 }
1199
1200
1201 void ProjectList::slotAddSlideshowClip()
1202 {
1203     if (!m_commandStack)
1204         kDebug() << "!!!!!!!!!!!!!!!! NO CMD STK";
1205
1206     SlideshowClip *dia = new SlideshowClip(m_timecode, this);
1207
1208     if (dia->exec() == QDialog::Accepted) {
1209         QStringList groupInfo = getGroup();
1210         m_doc->slotCreateSlideshowClipFile(dia->clipName(), dia->selectedPath(), dia->imageCount(), dia->clipDuration(),
1211                                            dia->loop(), dia->crop(), dia->fade(),
1212                                            dia->lumaDuration(), dia->lumaFile(), dia->softness(),
1213                                            dia->animation(), groupInfo.at(0), groupInfo.at(1));
1214     }
1215     delete dia;
1216 }
1217
1218 void ProjectList::slotAddTitleClip()
1219 {
1220     QStringList groupInfo = getGroup();
1221     m_doc->slotCreateTextClip(groupInfo.at(0), groupInfo.at(1));
1222 }
1223
1224 void ProjectList::slotAddTitleTemplateClip()
1225 {
1226     if (!m_commandStack)
1227         kDebug() << "!!!!!!!!!!!!!!!! NO CMD STK";
1228
1229     QStringList groupInfo = getGroup();
1230
1231     // Get the list of existing templates
1232     QStringList filter;
1233     filter << "*.kdenlivetitle";
1234     const QString path = m_doc->projectFolder().path(KUrl::AddTrailingSlash) + "titles/";
1235     QStringList templateFiles = QDir(path).entryList(filter, QDir::Files);
1236
1237     QDialog *dia = new QDialog(this);
1238     Ui::TemplateClip_UI dia_ui;
1239     dia_ui.setupUi(dia);
1240     for (int i = 0; i < templateFiles.size(); ++i)
1241         dia_ui.template_list->comboBox()->addItem(templateFiles.at(i), path + templateFiles.at(i));
1242
1243     if (!templateFiles.isEmpty())
1244         dia_ui.buttonBox->button(QDialogButtonBox::Ok)->setFocus();
1245     dia_ui.template_list->fileDialog()->setFilter("application/x-kdenlivetitle");
1246     //warning: setting base directory doesn't work??
1247     KUrl startDir(path);
1248     dia_ui.template_list->fileDialog()->setUrl(startDir);
1249     dia_ui.text_box->setHidden(true);
1250     if (dia->exec() == QDialog::Accepted) {
1251         QString textTemplate = dia_ui.template_list->comboBox()->itemData(dia_ui.template_list->comboBox()->currentIndex()).toString();
1252         if (textTemplate.isEmpty()) textTemplate = dia_ui.template_list->comboBox()->currentText();
1253         // Create a cloned template clip
1254         m_doc->slotCreateTextTemplateClip(groupInfo.at(0), groupInfo.at(1), KUrl(textTemplate));
1255     }
1256     delete dia;
1257 }
1258
1259 QStringList ProjectList::getGroup() const
1260 {
1261     QStringList result;
1262     QTreeWidgetItem *item = m_listView->currentItem();
1263     while (item && item->type() != PROJECTFOLDERTYPE)
1264         item = item->parent();
1265
1266     if (item) {
1267         FolderProjectItem *folder = static_cast <FolderProjectItem *>(item);
1268         result << folder->groupName() << folder->clipId();
1269     } else {
1270         result << QString() << QString();
1271     }
1272     return result;
1273 }
1274
1275 void ProjectList::setDocument(KdenliveDoc *doc)
1276 {
1277     m_listView->blockSignals(true);
1278     m_listView->clear();
1279     m_listView->setSortingEnabled(false);
1280     emit clipSelected(NULL);
1281     m_thumbnailQueue.clear();
1282     m_infoQueue.clear();
1283     m_refreshed = false;
1284     m_fps = doc->fps();
1285     m_timecode = doc->timecode();
1286     m_commandStack = doc->commandStack();
1287     m_doc = doc;
1288
1289     QMap <QString, QString> flist = doc->clipManager()->documentFolderList();
1290     QMapIterator<QString, QString> f(flist);
1291     while (f.hasNext()) {
1292         f.next();
1293         (void) new FolderProjectItem(m_listView, QStringList() << f.value(), f.key());
1294     }
1295
1296     QList <DocClipBase*> list = doc->clipManager()->documentClipList();
1297     for (int i = 0; i < list.count(); i++)
1298         slotAddClip(list.at(i), false);
1299
1300     m_listView->blockSignals(false);
1301     connect(m_doc->clipManager(), SIGNAL(reloadClip(const QString &)), this, SLOT(slotReloadClip(const QString &)));
1302     connect(m_doc->clipManager(), SIGNAL(modifiedClip(const QString &)), this, SLOT(slotModifiedClip(const QString &)));
1303     connect(m_doc->clipManager(), SIGNAL(missingClip(const QString &)), this, SLOT(slotMissingClip(const QString &)));
1304     connect(m_doc->clipManager(), SIGNAL(availableClip(const QString &)), this, SLOT(slotAvailableClip(const QString &)));
1305     connect(m_doc->clipManager(), SIGNAL(checkAllClips()), this, SLOT(updateAllClips()));
1306 }
1307
1308 QList <DocClipBase*> ProjectList::documentClipList() const
1309 {
1310     if (m_doc == NULL)
1311         return QList <DocClipBase*> ();
1312
1313     return m_doc->clipManager()->documentClipList();
1314 }
1315
1316 QDomElement ProjectList::producersList()
1317 {
1318     QDomDocument doc;
1319     QDomElement prods = doc.createElement("producerlist");
1320     doc.appendChild(prods);
1321     kDebug() << "////////////  PRO LIST BUILD PRDSLIST ";
1322     QTreeWidgetItemIterator it(m_listView);
1323     while (*it) {
1324         if ((*it)->type() != PROJECTCLIPTYPE) {
1325             // subitem
1326             ++it;
1327             continue;
1328         }
1329         prods.appendChild(doc.importNode(((ProjectItem *)(*it))->toXml(), true));
1330         ++it;
1331     }
1332     return prods;
1333 }
1334
1335 void ProjectList::slotCheckForEmptyQueue()
1336 {
1337     if (!m_refreshed && m_thumbnailQueue.isEmpty() && m_infoQueue.isEmpty()) {
1338         m_refreshed = true;
1339         emit loadingIsOver();
1340         emit displayMessage(QString(), -1);
1341         m_listView->blockSignals(false);
1342         m_listView->setEnabled(true);
1343         updateButtons();
1344     } else if (!m_refreshed) {
1345         QTimer::singleShot(300, this, SLOT(slotCheckForEmptyQueue()));
1346     }
1347 }
1348
1349 void ProjectList::reloadClipThumbnails()
1350 {
1351     kDebug() << "//////////////  RELOAD CLIPS THUMBNAILS!!!";
1352     m_thumbnailQueue.clear();
1353     QTreeWidgetItemIterator it(m_listView);
1354     while (*it) {
1355         if ((*it)->type() != PROJECTCLIPTYPE) {
1356             // subitem
1357             ++it;
1358             continue;
1359         }
1360         m_thumbnailQueue << ((ProjectItem *)(*it))->clipId();
1361         ++it;
1362     }
1363     QTimer::singleShot(300, this, SLOT(slotProcessNextThumbnail()));
1364 }
1365
1366 void ProjectList::requestClipThumbnail(const QString id)
1367 {
1368     if (!m_thumbnailQueue.contains(id)) m_thumbnailQueue.append(id);
1369 }
1370
1371 void ProjectList::slotProcessNextThumbnail()
1372 {
1373     if (m_thumbnailQueue.isEmpty() && m_infoQueue.isEmpty()) {
1374         slotCheckForEmptyQueue();
1375         return;
1376     }
1377     if (!m_infoQueue.isEmpty()) {
1378         //QTimer::singleShot(300, this, SLOT(slotProcessNextThumbnail()));
1379         return;
1380     }
1381     if (m_thumbnailQueue.count() > 1) {
1382         int max = m_doc->clipManager()->clipsCount();
1383         emit displayMessage(i18n("Loading thumbnails"), (int)(100 *(max - m_thumbnailQueue.count()) / max));
1384     }
1385     slotRefreshClipThumbnail(m_thumbnailQueue.takeFirst(), false);
1386 }
1387
1388 void ProjectList::slotRefreshClipThumbnail(const QString &clipId, bool update)
1389 {
1390     QTreeWidgetItem *item = getAnyItemById(clipId);
1391     if (item)
1392         slotRefreshClipThumbnail(item, update);
1393     else
1394         slotProcessNextThumbnail();
1395 }
1396
1397 void ProjectList::slotRefreshClipThumbnail(QTreeWidgetItem *it, bool update)
1398 {
1399     if (it == NULL) return;
1400     ProjectItem *item = NULL;
1401     bool isSubItem = false;
1402     int frame;
1403     if (it->type() == PROJECTFOLDERTYPE) return;
1404     if (it->type() == PROJECTSUBCLIPTYPE) {
1405         item = static_cast <ProjectItem *>(it->parent());
1406         frame = static_cast <SubProjectItem *>(it)->zone().x();
1407         isSubItem = true;
1408     } else {
1409         item = static_cast <ProjectItem *>(it);
1410         frame = item->referencedClip()->getClipThumbFrame();
1411     }
1412
1413     if (item) {
1414         DocClipBase *clip = item->referencedClip();
1415         if (!clip) {
1416             slotProcessNextThumbnail();
1417             return;
1418         }
1419         QPixmap pix;
1420         int height = m_listView->iconSize().height();
1421         int width = (int)(height  * m_render->dar());
1422         if (clip->clipType() == AUDIO)
1423             pix = KIcon("audio-x-generic").pixmap(QSize(width, height));
1424         else if (clip->clipType() == IMAGE)
1425             pix = QPixmap::fromImage(KThumb::getFrame(item->referencedClip()->producer(), 0, width, height));
1426         else
1427             pix = item->referencedClip()->thumbProducer()->extractImage(frame, width, height);
1428
1429         if (!pix.isNull()) {
1430             m_listView->blockSignals(true);
1431             it->setData(0, Qt::DecorationRole, pix);
1432             if (m_listView->isEnabled())
1433                 m_listView->blockSignals(false);
1434             if (!isSubItem)
1435                 m_doc->cachePixmap(item->getClipHash(), pix);
1436             else
1437                 m_doc->cachePixmap(item->getClipHash() + '#' + QString::number(frame), pix);
1438         }
1439         if (update)
1440             emit projectModified();
1441         QTimer::singleShot(30, this, SLOT(slotProcessNextThumbnail()));
1442     }
1443 }
1444
1445 void ProjectList::slotReplyGetFileProperties(const QString &clipId, Mlt::Producer *producer, const QMap < QString, QString > &properties, const QMap < QString, QString > &metadata, bool replace)
1446 {
1447     QString toReload;
1448     ProjectItem *item = getItemById(clipId);
1449     if (item && producer) {
1450         m_listView->blockSignals(true);
1451         item->setProperties(properties, metadata);
1452         if (item->referencedClip()->isPlaceHolder() && producer->is_valid()) {
1453             item->referencedClip()->setValid();
1454             item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsDragEnabled | Qt::ItemIsEnabled | Qt::ItemIsEditable | Qt::ItemIsDropEnabled);
1455             toReload = clipId;
1456         }
1457         //Q_ASSERT_X(item->referencedClip(), "void ProjectList::slotReplyGetFileProperties", QString("Item with groupName %1 does not have a clip associated").arg(item->groupName()).toLatin1());
1458         item->referencedClip()->setProducer(producer, replace);
1459         item->referencedClip()->askForAudioThumbs();
1460         if (!replace && item->data(0, Qt::DecorationRole).isNull())
1461             requestClipThumbnail(clipId);
1462         if (!toReload.isEmpty())
1463             item->slotSetToolTip();
1464
1465         //emit receivedClipDuration(clipId);
1466         if (m_listView->isEnabled() && replace) {
1467             // update clip in clip monitor
1468             emit clipSelected(NULL);
1469             emit clipSelected(item->referencedClip());
1470             //TODO: Make sure the line below has no side effect
1471             toReload = clipId;
1472         }
1473         /*else {
1474             // Check if duration changed.
1475             emit receivedClipDuration(clipId);
1476             delete producer;
1477         }*/
1478         if (m_listView->isEnabled())
1479             m_listView->blockSignals(false);
1480         /*if (item->icon(0).isNull()) {
1481             requestClipThumbnail(clipId);
1482         }*/
1483     } else kDebug() << "////////  COULD NOT FIND CLIP TO UPDATE PRPS...";
1484     int max = m_doc->clipManager()->clipsCount();
1485     if (item && m_infoQueue.isEmpty() && m_thumbnailQueue.isEmpty()) {
1486         m_listView->setCurrentItem(item);
1487         bool updatedProfile = false;
1488         if (item->parent()) {
1489             if (item->parent()->type() == PROJECTFOLDERTYPE)
1490                 static_cast <FolderProjectItem *>(item->parent())->switchIcon();
1491         } else if (KdenliveSettings::checkfirstprojectclip() &&  m_listView->topLevelItemCount() == 1) {
1492             // this is the first clip loaded in project, check if we want to adjust project settings to the clip
1493             updatedProfile = adjustProjectProfileToItem(item);
1494         }
1495         if (updatedProfile == false) emit clipSelected(item->referencedClip());
1496     } else {
1497         emit displayMessage(i18n("Loading clips"), (int)(100 *(max - m_infoQueue.count()) / max));
1498     }
1499     if (!toReload.isEmpty())
1500         emit clipNeedsReload(toReload, true);
1501     // small delay so that the app can display the progress info
1502     QTimer::singleShot(30, this, SLOT(slotProcessNextClipInQueue()));
1503 }
1504
1505 bool ProjectList::adjustProjectProfileToItem(ProjectItem *item)
1506 {
1507     if (item == NULL) {
1508         if (m_listView->currentItem() && m_listView->currentItem()->type() != PROJECTFOLDERTYPE)
1509             item = static_cast <ProjectItem*>(m_listView->currentItem());
1510     }
1511     if (item == NULL || item->referencedClip() == NULL) {
1512         KMessageBox::information(kapp->activeWindow(), i18n("Cannot find profile from current clip"));
1513         return false;
1514     }
1515     bool profileUpdated = false;
1516     QString size = item->referencedClip()->getProperty("frame_size");
1517     int width = size.section('x', 0, 0).toInt();
1518     int height = size.section('x', -1).toInt();
1519     double fps = item->referencedClip()->getProperty("fps").toDouble();
1520     double par = item->referencedClip()->getProperty("aspect_ratio").toDouble();
1521     if (item->clipType() == IMAGE || item->clipType() == AV || item->clipType() == VIDEO) {
1522         if (ProfilesDialog::matchProfile(width, height, fps, par, item->clipType() == IMAGE, m_doc->mltProfile()) == false) {
1523             // get a list of compatible profiles
1524             QMap <QString, QString> suggestedProfiles = ProfilesDialog::getProfilesFromProperties(width, height, fps, par, item->clipType() == IMAGE);
1525             if (!suggestedProfiles.isEmpty()) {
1526                 KDialog *dialog = new KDialog(this);
1527                 dialog->setCaption(i18n("Change project profile"));
1528                 dialog->setButtons(KDialog::Ok | KDialog::Cancel);
1529
1530                 QWidget container;
1531                 QVBoxLayout *l = new QVBoxLayout;
1532                 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));
1533                 l->addWidget(label);
1534                 QListWidget *list = new QListWidget;
1535                 list->setAlternatingRowColors(true);
1536                 QMapIterator<QString, QString> i(suggestedProfiles);
1537                 while (i.hasNext()) {
1538                     i.next();
1539                     QListWidgetItem *item = new QListWidgetItem(i.value(), list);
1540                     item->setData(Qt::UserRole, i.key());
1541                     item->setToolTip(i.key());
1542                 }
1543                 list->setCurrentRow(0);
1544                 l->addWidget(list);
1545                 container.setLayout(l);
1546                 dialog->setButtonText(KDialog::Ok, i18n("Update profile"));
1547                 dialog->setMainWidget(&container);
1548                 if (dialog->exec() == QDialog::Accepted) {
1549                     //Change project profile
1550                     profileUpdated = true;
1551                     if (list->currentItem())
1552                         emit updateProfile(list->currentItem()->data(Qt::UserRole).toString());
1553                 }
1554                 delete list;
1555                 delete label;
1556             } 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));
1557         }
1558     }
1559     return profileUpdated;
1560 }
1561
1562 void ProjectList::slotReplyGetImage(const QString &clipId, const QPixmap &pix)
1563 {
1564     ProjectItem *item = getItemById(clipId);
1565     if (item && !pix.isNull()) {
1566         m_listView->blockSignals(true);
1567         item->setData(0, Qt::DecorationRole, pix);
1568         m_doc->cachePixmap(item->getClipHash(), pix);
1569         if (m_listView->isEnabled())
1570             m_listView->blockSignals(false);
1571     }
1572 }
1573
1574 QTreeWidgetItem *ProjectList::getAnyItemById(const QString &id)
1575 {
1576     QTreeWidgetItemIterator it(m_listView);
1577     QString lookId = id;
1578     if (id.contains('#'))
1579         lookId = id.section('#', 0, 0);
1580
1581     ProjectItem *result = NULL;
1582     while (*it) {
1583         if ((*it)->type() != PROJECTCLIPTYPE) {
1584             // subitem
1585             ++it;
1586             continue;
1587         }
1588         ProjectItem *item = static_cast<ProjectItem *>(*it);
1589         if (item->clipId() == lookId) {
1590             result = item;
1591             break;
1592         }
1593         ++it;
1594     }
1595     if (result == NULL || !id.contains('#')) {
1596         return result;
1597     } else {
1598         for (int i = 0; i < result->childCount(); i++) {
1599             SubProjectItem *sub = static_cast <SubProjectItem *>(result->child(i));
1600             if (sub && sub->zone().x() == id.section('#', 1, 1).toInt())
1601                 return sub;
1602         }
1603     }
1604
1605     return NULL;
1606 }
1607
1608
1609 ProjectItem *ProjectList::getItemById(const QString &id)
1610 {
1611     ProjectItem *item;
1612     QTreeWidgetItemIterator it(m_listView);
1613     while (*it) {
1614         if ((*it)->type() != PROJECTCLIPTYPE) {
1615             // subitem
1616             ++it;
1617             continue;
1618         }
1619         item = static_cast<ProjectItem *>(*it);
1620         if (item->clipId() == id)
1621             return item;
1622         ++it;
1623     }
1624     return NULL;
1625 }
1626
1627 FolderProjectItem *ProjectList::getFolderItemById(const QString &id)
1628 {
1629     FolderProjectItem *item;
1630     QTreeWidgetItemIterator it(m_listView);
1631     while (*it) {
1632         if ((*it)->type() == PROJECTFOLDERTYPE) {
1633             item = static_cast<FolderProjectItem *>(*it);
1634             if (item->clipId() == id)
1635                 return item;
1636         }
1637         ++it;
1638     }
1639     return NULL;
1640 }
1641
1642 void ProjectList::slotSelectClip(const QString &ix)
1643 {
1644     ProjectItem *clip = getItemById(ix);
1645     if (clip) {
1646         m_listView->setCurrentItem(clip);
1647         m_listView->scrollToItem(clip);
1648         m_editButton->defaultAction()->setEnabled(true);
1649         m_deleteButton->defaultAction()->setEnabled(true);
1650         m_reloadAction->setEnabled(true);
1651         m_transcodeAction->setEnabled(true);
1652         if (clip->clipType() == IMAGE && !KdenliveSettings::defaultimageapp().isEmpty()) {
1653             m_openAction->setIcon(KIcon(KdenliveSettings::defaultimageapp()));
1654             m_openAction->setEnabled(true);
1655         } else if (clip->clipType() == AUDIO && !KdenliveSettings::defaultaudioapp().isEmpty()) {
1656             m_openAction->setIcon(KIcon(KdenliveSettings::defaultaudioapp()));
1657             m_openAction->setEnabled(true);
1658         } else {
1659             m_openAction->setEnabled(false);
1660         }
1661     }
1662 }
1663
1664 QString ProjectList::currentClipUrl() const
1665 {
1666     ProjectItem *item;
1667     if (!m_listView->currentItem() || m_listView->currentItem()->type() == PROJECTFOLDERTYPE) return QString();
1668     if (m_listView->currentItem()->type() == PROJECTSUBCLIPTYPE) {
1669         // subitem
1670         item = static_cast <ProjectItem*>(m_listView->currentItem()->parent());
1671     } else {
1672         item = static_cast <ProjectItem*>(m_listView->currentItem());
1673     }
1674     if (item == NULL)
1675         return QString();
1676     return item->clipUrl().path();
1677 }
1678
1679 KUrl::List ProjectList::getConditionalUrls(const QString &condition) const
1680 {
1681     KUrl::List result;
1682     ProjectItem *item;
1683     QList<QTreeWidgetItem *> list = m_listView->selectedItems();
1684     for (int i = 0; i < list.count(); i++) {
1685         if (list.at(i)->type() == PROJECTFOLDERTYPE)
1686             continue;
1687         if (list.at(i)->type() == PROJECTSUBCLIPTYPE) {
1688             // subitem
1689             item = static_cast <ProjectItem*>(list.at(i)->parent());
1690         } else {
1691             item = static_cast <ProjectItem*>(list.at(i));
1692         }
1693         if (item == NULL || item->type() == COLOR || item->type() == SLIDESHOW || item->type() == TEXT)
1694             continue;
1695         DocClipBase *clip = item->referencedClip();
1696         if (!condition.isEmpty()) {
1697             if (condition.startsWith("vcodec") && !clip->hasVideoCodec(condition.section('=', 1, 1)))
1698                 continue;
1699             else if (condition.startsWith("acodec") && !clip->hasAudioCodec(condition.section('=', 1, 1)))
1700                 continue;
1701         }
1702         result.append(item->clipUrl());
1703     }
1704     return result;
1705 }
1706
1707 void ProjectList::regenerateTemplate(const QString &id)
1708 {
1709     ProjectItem *clip = getItemById(id);
1710     if (clip)
1711         regenerateTemplate(clip);
1712 }
1713
1714 void ProjectList::regenerateTemplate(ProjectItem *clip)
1715 {
1716     //TODO: remove this unused method, only force_reload is necessary
1717     clip->referencedClip()->producer()->set("force_reload", 1);
1718 }
1719
1720 QDomDocument ProjectList::generateTemplateXml(QString path, const QString &replaceString)
1721 {
1722     QDomDocument doc;
1723     QFile file(path);
1724     if (!file.open(QIODevice::ReadOnly)) {
1725         kWarning() << "ERROR, CANNOT READ: " << path;
1726         return doc;
1727     }
1728     if (!doc.setContent(&file)) {
1729         kWarning() << "ERROR, CANNOT READ: " << path;
1730         file.close();
1731         return doc;
1732     }
1733     file.close();
1734     QDomNodeList texts = doc.elementsByTagName("content");
1735     for (int i = 0; i < texts.count(); i++) {
1736         QString data = texts.item(i).firstChild().nodeValue();
1737         data.replace("%s", replaceString);
1738         texts.item(i).firstChild().setNodeValue(data);
1739     }
1740     return doc;
1741 }
1742
1743
1744 void ProjectList::slotAddClipCut(const QString &id, int in, int out)
1745 {
1746     ProjectItem *clip = getItemById(id);
1747     if (clip == NULL || clip->referencedClip()->hasCutZone(QPoint(in, out)))
1748         return;
1749     AddClipCutCommand *command = new AddClipCutCommand(this, id, in, out, QString(), true, false);
1750     m_commandStack->push(command);
1751 }
1752
1753 void ProjectList::addClipCut(const QString &id, int in, int out, const QString desc, bool newItem)
1754 {
1755     ProjectItem *clip = getItemById(id);
1756     if (clip) {
1757         DocClipBase *base = clip->referencedClip();
1758         base->addCutZone(in, out);
1759         m_listView->blockSignals(true);
1760         SubProjectItem *sub = new SubProjectItem(clip, in, out, desc);
1761         if (newItem && desc.isEmpty() && !m_listView->isColumnHidden(1)) {
1762             if (!clip->isExpanded())
1763                 clip->setExpanded(true);
1764             m_listView->scrollToItem(sub);
1765             m_listView->editItem(sub, 1);
1766         }
1767         QPixmap p = clip->referencedClip()->thumbProducer()->extractImage(in, (int)(sub->sizeHint(0).height()  * m_render->dar()), sub->sizeHint(0).height() - 2);
1768         sub->setData(0, Qt::DecorationRole, p);
1769         m_doc->cachePixmap(clip->getClipHash() + '#' + QString::number(in), p);
1770         m_listView->blockSignals(false);
1771     }
1772     emit projectModified();
1773 }
1774
1775 void ProjectList::removeClipCut(const QString &id, int in, int out)
1776 {
1777     ProjectItem *clip = getItemById(id);
1778     if (clip) {
1779         DocClipBase *base = clip->referencedClip();
1780         base->removeCutZone(in, out);
1781         SubProjectItem *sub = getSubItem(clip, QPoint(in, out));
1782         if (sub) {
1783             m_listView->blockSignals(true);
1784             delete sub;
1785             m_listView->blockSignals(false);
1786         }
1787     }
1788     emit projectModified();
1789 }
1790
1791 SubProjectItem *ProjectList::getSubItem(ProjectItem *clip, QPoint zone)
1792 {
1793     SubProjectItem *sub = NULL;
1794     if (clip) {
1795         for (int i = 0; i < clip->childCount(); i++) {
1796             QTreeWidgetItem *it = clip->child(i);
1797             if (it->type() == PROJECTSUBCLIPTYPE) {
1798                 sub = static_cast <SubProjectItem*>(it);
1799                 if (sub->zone() == zone)
1800                     break;
1801                 else
1802                     sub = NULL;
1803             }
1804         }
1805     }
1806     return sub;
1807 }
1808
1809 void ProjectList::slotUpdateClipCut(QPoint p)
1810 {
1811     if (!m_listView->currentItem() || m_listView->currentItem()->type() != PROJECTSUBCLIPTYPE)
1812         return;
1813     SubProjectItem *sub = static_cast <SubProjectItem*>(m_listView->currentItem());
1814     ProjectItem *item = static_cast <ProjectItem *>(sub->parent());
1815     EditClipCutCommand *command = new EditClipCutCommand(this, item->clipId(), sub->zone(), p, sub->text(1), sub->text(1), true);
1816     m_commandStack->push(command);
1817 }
1818
1819 void ProjectList::doUpdateClipCut(const QString &id, const QPoint oldzone, const QPoint zone, const QString &comment)
1820 {
1821     ProjectItem *clip = getItemById(id);
1822     SubProjectItem *sub = getSubItem(clip, oldzone);
1823     if (sub == NULL || clip == NULL)
1824         return;
1825     DocClipBase *base = clip->referencedClip();
1826     base->updateCutZone(oldzone.x(), oldzone.y(), zone.x(), zone.y(), comment);
1827     m_listView->blockSignals(true);
1828     sub->setZone(zone);
1829     sub->setDescription(comment);
1830     m_listView->blockSignals(false);
1831     emit projectModified();
1832 }
1833
1834 void ProjectList::slotForceProcessing(const QString &id)
1835 {
1836     while (m_infoQueue.contains(id)) {
1837         slotProcessNextClipInQueue();
1838     }
1839 }
1840
1841 void ProjectList::slotAddOrUpdateSequence(const QString frameName)
1842 {
1843     QString fileName = KUrl(frameName).fileName().section('_', 0, -2);
1844     int count;
1845     QString pattern = SlideshowClip::selectedPath(frameName, false, QString(), &count);
1846     if (count > 1) {
1847          const QList <DocClipBase *> existing = m_doc->clipManager()->getClipByResource(pattern);
1848         if (!existing.isEmpty()) {
1849             // Sequence already exists, update
1850             QString id = existing.at(0)->getId();
1851             //ProjectItem *item = getItemById(id);
1852             QMap <QString, QString> oldprops;
1853             QMap <QString, QString> newprops;
1854             int ttl = existing.at(0)->getProperty("ttl").toInt();
1855             oldprops["out"] = existing.at(0)->getProperty("out");
1856             newprops["out"] = QString::number(ttl * count - 1);
1857             slotUpdateClipProperties(id, newprops);
1858             EditClipCommand *command = new EditClipCommand(this, id, oldprops, newprops, false);
1859             m_commandStack->push(command);
1860         }
1861         else {
1862             // Create sequence
1863             QStringList groupInfo = getGroup();
1864             m_doc->slotCreateSlideshowClipFile(fileName, pattern, count, m_timecode.reformatSeparators(KdenliveSettings::sequence_duration()),
1865                                                            false, false, false,
1866                                                            m_timecode.getTimecodeFromFrames(int(ceil(m_timecode.fps()))), QString(), 0,
1867                                                            QString(), groupInfo.at(0), groupInfo.at(1));
1868         }
1869     }
1870     else emit displayMessage(i18n("Sequence not found"), -2);
1871 }
1872
1873 #include "projectlist.moc"