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