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