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