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