]> git.sesse.net Git - kdenlive/blob - src/projectlist.cpp
Make sure icons have small size with non KDE window manager
[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 "commands/addfoldercommand.h"
23 #include "kdenlivesettings.h"
24 #include "slideshowclip.h"
25 #include "ui_colorclip_ui.h"
26 #include "titlewidget.h"
27 #include "definitions.h"
28 #include "clipmanager.h"
29 #include "docclipbase.h"
30 #include "kdenlivedoc.h"
31 #include "renderer.h"
32 #include "kthumb.h"
33 #include "projectlistview.h"
34 #include "timecodedisplay.h"
35 #include "profilesdialog.h"
36 #include "commands/editclipcommand.h"
37 #include "commands/editclipcutcommand.h"
38 #include "commands/editfoldercommand.h"
39 #include "commands/addclipcutcommand.h"
40
41 #include "ui_templateclip_ui.h"
42
43 #include <KDebug>
44 #include <KAction>
45 #include <KLocale>
46 #include <KFileDialog>
47 #include <KInputDialog>
48 #include <KMessageBox>
49 #include <KIO/NetAccess>
50 #include <KFileItem>
51 #include <KApplication>
52 #include <KStandardDirs>
53
54 #ifdef NEPOMUK
55 #include <nepomuk/global.h>
56 #include <nepomuk/resourcemanager.h>
57 //#include <nepomuk/tag.h>
58 #endif
59
60 #include <QMouseEvent>
61 #include <QStylePainter>
62 #include <QPixmap>
63 #include <QIcon>
64 #include <QMenu>
65 #include <QProcess>
66 #include <QHeaderView>
67 #include <QInputDialog>
68 #include <QtConcurrentRun>
69 #include <QVBoxLayout>
70
71 InvalidDialog::InvalidDialog(const QString &caption, const QString &message, bool infoOnly, QWidget *parent) : KDialog(parent)
72 {
73     setCaption(caption);
74     if (infoOnly) setButtons(KDialog::Ok);
75     else setButtons(KDialog::Yes | KDialog::No);
76     QWidget *w = new QWidget(this);
77     QVBoxLayout *l = new QVBoxLayout;
78     l->addWidget(new QLabel(message));
79     m_clipList = new QListWidget;
80     l->addWidget(m_clipList);
81     w->setLayout(l);
82     setMainWidget(w);
83 }
84
85 InvalidDialog::~InvalidDialog()
86 {
87     delete m_clipList;
88 }
89
90
91 void InvalidDialog::addClip(const QString &id, const QString &path)
92 {
93     QListWidgetItem *item = new QListWidgetItem(path);
94     item->setData(Qt::UserRole, id);
95     m_clipList->addItem(item);
96 }
97
98 QStringList InvalidDialog::getIds() const
99 {
100     QStringList ids;
101     for (int i = 0; i < m_clipList->count(); i++) {
102         ids << m_clipList->item(i)->data(Qt::UserRole).toString();
103     }
104     return ids;
105 }
106
107
108 ProjectList::ProjectList(QWidget *parent) :
109     QWidget(parent),
110     m_render(NULL),
111     m_fps(-1),
112     m_commandStack(NULL),
113     m_openAction(NULL),
114     m_reloadAction(NULL),
115     m_stabilizeAction(NULL),
116     m_transcodeAction(NULL),
117     m_doc(NULL),
118     m_refreshed(false),
119     m_allClipsProcessed(false),
120     m_thumbnailQueue(),
121     m_abortAllProxies(false),
122     m_invalidClipDialog(NULL)
123 {
124     QVBoxLayout *layout = new QVBoxLayout;
125     layout->setContentsMargins(0, 0, 0, 0);
126     layout->setSpacing(0);
127     qRegisterMetaType<QDomElement>("QDomElement");
128     // setup toolbar
129     QFrame *frame = new QFrame;
130     frame->setFrameStyle(QFrame::NoFrame);
131     QHBoxLayout *box = new QHBoxLayout;
132     KTreeWidgetSearchLine *searchView = new KTreeWidgetSearchLine;
133     box->addWidget(searchView);
134
135     int size = style()->pixelMetric(QStyle::PM_SmallIconSize);
136     QSize iconSize(size, size);
137
138     m_addButton = new QToolButton;
139     m_addButton->setPopupMode(QToolButton::MenuButtonPopup);
140     m_addButton->setAutoRaise(true);
141     m_addButton->setIconSize(iconSize);
142     box->addWidget(m_addButton);
143
144     m_editButton = new QToolButton;
145     m_editButton->setAutoRaise(true);
146     m_editButton->setIconSize(iconSize);
147     box->addWidget(m_editButton);
148
149     m_deleteButton = new QToolButton;
150     m_deleteButton->setAutoRaise(true);
151     m_deleteButton->setIconSize(iconSize);
152     box->addWidget(m_deleteButton);
153     frame->setLayout(box);
154     layout->addWidget(frame);
155
156     m_listView = new ProjectListView;
157     layout->addWidget(m_listView);
158     setLayout(layout);
159     searchView->setTreeWidget(m_listView);
160
161     connect(this, SIGNAL(processNextThumbnail()), this, SLOT(slotProcessNextThumbnail()));
162     connect(m_listView, SIGNAL(projectModified()), this, SIGNAL(projectModified()));
163     connect(m_listView, SIGNAL(itemSelectionChanged()), this, SLOT(slotClipSelected()));
164     connect(m_listView, SIGNAL(focusMonitor()), this, SIGNAL(raiseClipMonitor()));
165     connect(m_listView, SIGNAL(pauseMonitor()), this, SLOT(slotPauseMonitor()));
166     connect(m_listView, SIGNAL(requestMenu(const QPoint &, QTreeWidgetItem *)), this, SLOT(slotContextMenu(const QPoint &, QTreeWidgetItem *)));
167     connect(m_listView, SIGNAL(addClip()), this, SLOT(slotAddClip()));
168     connect(m_listView, SIGNAL(addClip(const QList <QUrl>, const QString &, const QString &)), this, SLOT(slotAddClip(const QList <QUrl>, const QString &, const QString &)));
169     connect(m_listView, SIGNAL(addClipCut(const QString &, int, int)), this, SLOT(slotAddClipCut(const QString &, int, int)));
170     connect(m_listView, SIGNAL(itemChanged(QTreeWidgetItem *, int)), this, SLOT(slotItemEdited(QTreeWidgetItem *, int)));
171     connect(m_listView, SIGNAL(showProperties(DocClipBase *)), this, SIGNAL(showClipProperties(DocClipBase *)));
172
173     m_listViewDelegate = new ItemDelegate(m_listView);
174     m_listView->setItemDelegate(m_listViewDelegate);
175 #ifdef NEPOMUK
176     if (KdenliveSettings::activate_nepomuk()) {
177         Nepomuk::ResourceManager::instance()->init();
178         if (!Nepomuk::ResourceManager::instance()->initialized()) {
179             kDebug() << "Cannot communicate with Nepomuk, DISABLING it";
180             KdenliveSettings::setActivate_nepomuk(false);
181         }
182     }
183 #endif
184 }
185
186 ProjectList::~ProjectList()
187 {
188     m_abortAllProxies = true;
189     m_thumbnailQueue.clear();
190     delete m_menu;
191     m_listView->blockSignals(true);
192     m_listView->clear();
193     delete m_listViewDelegate;
194 }
195
196 void ProjectList::focusTree() const
197 {
198     m_listView->setFocus();
199 }
200
201 void ProjectList::setupMenu(QMenu *addMenu, QAction *defaultAction)
202 {
203     QList <QAction *> actions = addMenu->actions();
204     for (int i = 0; i < actions.count(); i++) {
205         if (actions.at(i)->data().toString() == "clip_properties") {
206             m_editButton->setDefaultAction(actions.at(i));
207             actions.removeAt(i);
208             i--;
209         } else if (actions.at(i)->data().toString() == "delete_clip") {
210             m_deleteButton->setDefaultAction(actions.at(i));
211             actions.removeAt(i);
212             i--;
213         } else if (actions.at(i)->data().toString() == "edit_clip") {
214             m_openAction = actions.at(i);
215             actions.removeAt(i);
216             i--;
217         } else if (actions.at(i)->data().toString() == "reload_clip") {
218             m_reloadAction = actions.at(i);
219             actions.removeAt(i);
220             i--;
221         } else if (actions.at(i)->data().toString() == "proxy_clip") {
222             m_proxyAction = actions.at(i);
223             actions.removeAt(i);
224             i--;
225         }
226     }
227
228     QMenu *m = new QMenu();
229     m->addActions(actions);
230     m_addButton->setMenu(m);
231     m_addButton->setDefaultAction(defaultAction);
232     m_menu = new QMenu();
233     m_menu->addActions(addMenu->actions());
234 }
235
236 void ProjectList::setupGeneratorMenu(const QHash<QString,QMenu*>& menus)
237 {
238     if (!menus.contains("addMenu") && ! menus.value("addMenu") )
239         return;
240     QMenu *menu = m_addButton->menu();
241         if (menus.contains("addMenu") && menus.value("addMenu")){ 
242                 QMenu* addMenu=menus.value("addMenu");
243                 menu->addMenu(addMenu);
244                 m_addButton->setMenu(menu);
245
246                 m_menu->addMenu(addMenu);
247                 if (addMenu->isEmpty())
248                         addMenu->setEnabled(false);
249         }
250         if (menus.contains("transcodeMenu") && menus.value("transcodeMenu") ){
251                 QMenu* transcodeMenu=menus.value("transcodeMenu");
252                 m_menu->addMenu(transcodeMenu);
253                 if (transcodeMenu->isEmpty())
254                         transcodeMenu->setEnabled(false);
255                 m_transcodeAction = transcodeMenu;
256         }
257         if (menus.contains("stabilizeMenu") && menus.value("stabilizeMenu") ){
258                 QMenu* stabilizeMenu=menus.value("stabilizeMenu");
259                 m_menu->addMenu(stabilizeMenu);
260                 if (stabilizeMenu->isEmpty())
261                         stabilizeMenu->setEnabled(false);
262                 m_stabilizeAction=stabilizeMenu;
263
264         }
265     m_menu->addAction(m_reloadAction);
266     m_menu->addAction(m_proxyAction);
267         if (menus.contains("inTimelineMenu") && menus.value("inTimelineMenu")){
268                 QMenu* inTimelineMenu=menus.value("inTimelineMenu");
269                 m_menu->addMenu(inTimelineMenu);
270                 inTimelineMenu->setEnabled(false);
271         }
272     m_menu->addAction(m_editButton->defaultAction());
273     m_menu->addAction(m_openAction);
274     m_menu->addAction(m_deleteButton->defaultAction());
275     m_menu->insertSeparator(m_deleteButton->defaultAction());
276 }
277
278 void ProjectList::clearSelection()
279 {
280     m_listView->clearSelection();
281 }
282
283 QByteArray ProjectList::headerInfo() const
284 {
285     return m_listView->header()->saveState();
286 }
287
288 void ProjectList::setHeaderInfo(const QByteArray &state)
289 {
290     m_listView->header()->restoreState(state);
291 }
292
293 void ProjectList::updateProjectFormat(Timecode t)
294 {
295     m_timecode = t;
296 }
297
298 void ProjectList::slotEditClip()
299 {
300     QList<QTreeWidgetItem *> list = m_listView->selectedItems();
301     if (list.isEmpty()) return;
302     if (list.count() > 1 || list.at(0)->type() == PROJECTFOLDERTYPE) {
303         editClipSelection(list);
304         return;
305     }
306     ProjectItem *item;
307     if (!m_listView->currentItem() || m_listView->currentItem()->type() == PROJECTFOLDERTYPE)
308         return;
309     if (m_listView->currentItem()->type() == PROJECTSUBCLIPTYPE)
310         item = static_cast <ProjectItem*>(m_listView->currentItem()->parent());
311     else
312         item = static_cast <ProjectItem*>(m_listView->currentItem());
313     if (item && (item->flags() & Qt::ItemIsDragEnabled)) {
314         emit clipSelected(item->referencedClip());
315         emit showClipProperties(item->referencedClip());
316     }
317 }
318
319 void ProjectList::editClipSelection(QList<QTreeWidgetItem *> list)
320 {
321     // Gather all common properties
322     QMap <QString, QString> commonproperties;
323     QList <DocClipBase *> clipList;
324     commonproperties.insert("force_aspect_num", "-");
325     commonproperties.insert("force_aspect_den", "-");
326     commonproperties.insert("force_fps", "-");
327     commonproperties.insert("force_progressive", "-");
328     commonproperties.insert("force_tff", "-");
329     commonproperties.insert("threads", "-");
330     commonproperties.insert("video_index", "-");
331     commonproperties.insert("audio_index", "-");
332     commonproperties.insert("force_colorspace", "-");
333     commonproperties.insert("full_luma", "-");
334     QString transparency = "-";
335
336     bool allowDurationChange = true;
337     int commonDuration = -1;
338     bool hasImages = false;;
339     ProjectItem *item;
340     for (int i = 0; i < list.count(); i++) {
341         item = NULL;
342         if (list.at(i)->type() == PROJECTFOLDERTYPE) {
343             // Add folder items to the list
344             int ct = list.at(i)->childCount();
345             for (int j = 0; j < ct; j++) {
346                 list.append(list.at(i)->child(j));
347             }
348             continue;
349         }
350         else if (list.at(i)->type() == PROJECTSUBCLIPTYPE)
351             item = static_cast <ProjectItem*>(list.at(i)->parent());
352         else
353             item = static_cast <ProjectItem*>(list.at(i));
354         if (!(item->flags() & Qt::ItemIsDragEnabled))
355             continue;
356         if (item) {
357             // check properties
358             DocClipBase *clip = item->referencedClip();
359             if (clipList.contains(clip)) continue;
360             if (clip->clipType() == IMAGE) {
361                 hasImages = true;
362                 if (clip->getProperty("transparency").isEmpty() || clip->getProperty("transparency").toInt() == 0) {
363                     if (transparency == "-") {
364                         // first non transparent image
365                         transparency = "0";
366                     }
367                     else if (transparency == "1") {
368                         // we have transparent and non transparent clips
369                         transparency = "-1";
370                     }
371                 }
372                 else {
373                     if (transparency == "-") {
374                         // first transparent image
375                         transparency = "1";
376                     }
377                     else if (transparency == "0") {
378                         // we have transparent and non transparent clips
379                         transparency = "-1";
380                     }
381                 }
382             }
383             if (clip->clipType() != COLOR && clip->clipType() != IMAGE && clip->clipType() != TEXT)
384                 allowDurationChange = false;
385             if (allowDurationChange && commonDuration != 0) {
386                 if (commonDuration == -1)
387                     commonDuration = clip->duration().frames(m_fps);
388                 else if (commonDuration != clip->duration().frames(m_fps))
389                     commonDuration = 0;
390             }
391             clipList.append(clip);
392             QMap <QString, QString> clipprops = clip->properties();
393             QMapIterator<QString, QString> p(commonproperties);
394             while (p.hasNext()) {
395                 p.next();
396                 if (p.value().isEmpty()) continue;
397                 if (clipprops.contains(p.key())) {
398                     if (p.value() == "-")
399                         commonproperties.insert(p.key(), clipprops.value(p.key()));
400                     else if (p.value() != clipprops.value(p.key()))
401                         commonproperties.insert(p.key(), QString());
402                 } else {
403                     commonproperties.insert(p.key(), QString());
404                 }
405             }
406         }
407     }
408     if (allowDurationChange)
409         commonproperties.insert("out", QString::number(commonDuration));
410     if (hasImages)
411         commonproperties.insert("transparency", transparency);
412     /*QMapIterator<QString, QString> p(commonproperties);
413     while (p.hasNext()) {
414         p.next();
415         kDebug() << "Result: " << p.key() << " = " << p.value();
416     }*/
417     emit showClipProperties(clipList, commonproperties);
418 }
419
420 void ProjectList::slotOpenClip()
421 {
422     ProjectItem *item;
423     if (!m_listView->currentItem() || m_listView->currentItem()->type() == PROJECTFOLDERTYPE)
424         return;
425     if (m_listView->currentItem()->type() == QTreeWidgetItem::UserType + 1)
426         item = static_cast <ProjectItem*>(m_listView->currentItem()->parent());
427     else
428         item = static_cast <ProjectItem*>(m_listView->currentItem());
429     if (item) {
430         if (item->clipType() == IMAGE) {
431             if (KdenliveSettings::defaultimageapp().isEmpty())
432                 KMessageBox::sorry(kapp->activeWindow(), i18n("Please set a default application to open images in the Settings dialog"));
433             else
434                 QProcess::startDetached(KdenliveSettings::defaultimageapp(), QStringList() << item->clipUrl().path());
435         }
436         if (item->clipType() == AUDIO) {
437             if (KdenliveSettings::defaultaudioapp().isEmpty())
438                 KMessageBox::sorry(kapp->activeWindow(), i18n("Please set a default application to open audio files in the Settings dialog"));
439             else
440                 QProcess::startDetached(KdenliveSettings::defaultaudioapp(), QStringList() << item->clipUrl().path());
441         }
442     }
443 }
444
445 void ProjectList::cleanup()
446 {
447     m_listView->clearSelection();
448     QTreeWidgetItemIterator it(m_listView);
449     ProjectItem *item;
450     while (*it) {
451         if ((*it)->type() != PROJECTCLIPTYPE) {
452             it++;
453             continue;
454         }
455         item = static_cast <ProjectItem *>(*it);
456         if (item->numReferences() == 0)
457             item->setSelected(true);
458         it++;
459     }
460     slotRemoveClip();
461 }
462
463 void ProjectList::trashUnusedClips()
464 {
465     QTreeWidgetItemIterator it(m_listView);
466     ProjectItem *item;
467     QStringList ids;
468     QStringList urls;
469     while (*it) {
470         if ((*it)->type() != PROJECTCLIPTYPE) {
471             it++;
472             continue;
473         }
474         item = static_cast <ProjectItem *>(*it);
475         if (item->numReferences() == 0) {
476             ids << item->clipId();
477             KUrl url = item->clipUrl();
478             if (!url.isEmpty() && !urls.contains(url.path()))
479                 urls << url.path();
480         }
481         it++;
482     }
483
484     // Check that we don't use the URL in another clip
485     QTreeWidgetItemIterator it2(m_listView);
486     while (*it2) {
487         if ((*it2)->type() != PROJECTCLIPTYPE) {
488             it2++;
489             continue;
490         }
491         item = static_cast <ProjectItem *>(*it2);
492         if (item->numReferences() > 0) {
493             KUrl url = item->clipUrl();
494             if (!url.isEmpty() && urls.contains(url.path())) urls.removeAll(url.path());
495         }
496         it2++;
497     }
498
499     emit deleteProjectClips(ids, QMap <QString, QString>());
500     for (int i = 0; i < urls.count(); i++)
501         KIO::NetAccess::del(KUrl(urls.at(i)), this);
502 }
503
504 void ProjectList::slotReloadClip(const QString &id)
505 {
506     QList<QTreeWidgetItem *> selected;
507     if (id.isEmpty())
508         selected = m_listView->selectedItems();
509     else {
510         ProjectItem *itemToReLoad = getItemById(id);
511         if (itemToReLoad) selected.append(itemToReLoad);
512     }
513     ProjectItem *item;
514     for (int i = 0; i < selected.count(); i++) {
515         if (selected.at(i)->type() != PROJECTCLIPTYPE) {
516             if (selected.at(i)->type() == PROJECTFOLDERTYPE) {
517                 for (int j = 0; j < selected.at(i)->childCount(); j++)
518                     selected.append(selected.at(i)->child(j));
519             }
520             continue;
521         }
522         item = static_cast <ProjectItem *>(selected.at(i));
523         if (item && !item->isProxyRunning()) {
524             DocClipBase *clip = item->referencedClip();
525             if (!clip || !clip->isClean() || m_render->isProcessing(item->clipId())) {
526                 kDebug()<<"//// TRYING TO RELOAD: "<<item->clipId()<<", but it is busy";
527                 continue;
528             }
529             CLIPTYPE t = item->clipType();
530             if (t == TEXT) {
531                 if (clip && !clip->getProperty("xmltemplate").isEmpty())
532                     regenerateTemplate(item);
533             } else if (t != COLOR && t != SLIDESHOW && clip && clip->checkHash() == false) {
534                 item->referencedClip()->setPlaceHolder(true);
535                 item->setProperty("file_hash", QString());
536             } else if (t == IMAGE) {
537                 clip->getProducer()->set("force_reload", 1);
538             }
539
540             QDomElement e = item->toXml();
541             // Make sure we get the correct producer length if it was adjusted in timeline
542             if (t == COLOR || t == IMAGE || t == SLIDESHOW || t == TEXT) {
543                 int length = QString(clip->producerProperty("length")).toInt();
544                 if (length > 0 && !e.hasAttribute("length")) {
545                     e.setAttribute("length", length);
546                 }
547             }
548             resetThumbsProducer(clip);
549             m_render->getFileProperties(e, item->clipId(), m_listView->iconSize().height(), true);
550         }
551     }
552 }
553
554 void ProjectList::slotModifiedClip(const QString &id)
555 {
556     ProjectItem *item = getItemById(id);
557     if (item) {
558         QPixmap pixmap = qVariantValue<QPixmap>(item->data(0, Qt::DecorationRole));
559         if (!pixmap.isNull()) {
560             QPainter p(&pixmap);
561             p.fillRect(0, 0, pixmap.width(), pixmap.height(), QColor(255, 255, 255, 200));
562             p.drawPixmap(0, 0, KIcon("view-refresh").pixmap(m_listView->iconSize()));
563             p.end();
564         } else {
565             pixmap = KIcon("view-refresh").pixmap(m_listView->iconSize());
566         }
567         item->setData(0, Qt::DecorationRole, pixmap);
568     }
569 }
570
571 void ProjectList::slotMissingClip(const QString &id)
572 {
573     ProjectItem *item = getItemById(id);
574     if (item) {
575         item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsDropEnabled);
576         int height = m_listView->iconSize().height();
577         int width = (int)(height  * m_render->dar());
578         QPixmap pixmap = qVariantValue<QPixmap>(item->data(0, Qt::DecorationRole));
579         if (pixmap.isNull()) {
580             pixmap = QPixmap(width, height);
581             pixmap.fill(Qt::transparent);
582         }
583         KIcon icon("dialog-close");
584         QPainter p(&pixmap);
585         p.drawPixmap(3, 3, icon.pixmap(width - 6, height - 6));
586         p.end();
587         item->setData(0, Qt::DecorationRole, pixmap);
588         if (item->referencedClip()) {
589             item->referencedClip()->setPlaceHolder(true);
590             if (m_render == NULL) {
591                 kDebug() << "*********  ERROR, NULL RENDR";
592                 return;
593             }
594             Mlt::Producer *newProd = m_render->invalidProducer(id);
595             if (item->referencedClip()->getProducer()) {
596                 Mlt::Properties props(newProd->get_properties());
597                 Mlt::Properties src_props(item->referencedClip()->getProducer()->get_properties());
598                 props.inherit(src_props);
599             }
600             item->referencedClip()->setProducer(newProd, true);
601             item->slotSetToolTip();
602             emit clipNeedsReload(id);
603         }
604     }
605     update();
606     emit displayMessage(i18n("Check missing clips"), -2);
607     emit updateRenderStatus();
608 }
609
610 void ProjectList::slotAvailableClip(const QString &id)
611 {
612     ProjectItem *item = getItemById(id);
613     if (item == NULL)
614         return;
615     item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsDragEnabled | Qt::ItemIsEnabled | Qt::ItemIsEditable | Qt::ItemIsDropEnabled);
616     if (item->referencedClip()) { // && item->referencedClip()->checkHash() == false) {
617         item->setProperty("file_hash", QString());
618         slotReloadClip(id);
619     }
620     /*else {
621     item->referencedClip()->setValid();
622     item->slotSetToolTip();
623     }
624     update();*/
625     emit updateRenderStatus();
626 }
627
628 bool ProjectList::hasMissingClips()
629 {
630     bool missing = false;
631     QTreeWidgetItemIterator it(m_listView);
632     while (*it) {
633         if ((*it)->type() == PROJECTCLIPTYPE && !((*it)->flags() & Qt::ItemIsDragEnabled)) {
634             missing = true;
635             break;
636         }
637         it++;
638     }
639     return missing;
640 }
641
642 void ProjectList::setRenderer(Render *projectRender)
643 {
644     m_render = projectRender;
645     m_listView->setIconSize(QSize((ProjectItem::itemDefaultHeight() - 2) * m_render->dar(), ProjectItem::itemDefaultHeight() - 2));
646 }
647
648 void ProjectList::slotClipSelected()
649 {
650     QTreeWidgetItem *item = m_listView->currentItem();
651     ProjectItem *clip = NULL;
652     if (item) {
653         if (item->type() == PROJECTFOLDERTYPE) {
654             emit clipSelected(NULL);
655             m_editButton->defaultAction()->setEnabled(item->childCount() > 0);
656             m_deleteButton->defaultAction()->setEnabled(true);
657             m_openAction->setEnabled(false);
658             m_reloadAction->setEnabled(false);
659             m_transcodeAction->setEnabled(false);
660             m_stabilizeAction->setEnabled(false);
661         } else {
662             if (item->type() == PROJECTSUBCLIPTYPE) {
663                 // this is a sub item, use base clip
664                 m_deleteButton->defaultAction()->setEnabled(true);
665                 clip = static_cast <ProjectItem*>(item->parent());
666                 if (clip == NULL) kDebug() << "-----------ERROR";
667                 SubProjectItem *sub = static_cast <SubProjectItem*>(item);
668                 emit clipSelected(clip->referencedClip(), sub->zone());
669                 m_transcodeAction->setEnabled(false);
670                 m_stabilizeAction->setEnabled(false);
671                 m_reloadAction->setEnabled(false);
672                 adjustProxyActions(clip);
673                 return;
674             }
675             clip = static_cast <ProjectItem*>(item);
676             if (clip && clip->referencedClip())
677                 emit clipSelected(clip->referencedClip());
678             m_editButton->defaultAction()->setEnabled(true);
679             m_deleteButton->defaultAction()->setEnabled(true);
680             m_reloadAction->setEnabled(true);
681             m_transcodeAction->setEnabled(true);
682             m_stabilizeAction->setEnabled(true);
683             if (clip && clip->clipType() == IMAGE && !KdenliveSettings::defaultimageapp().isEmpty()) {
684                 m_openAction->setIcon(KIcon(KdenliveSettings::defaultimageapp()));
685                 m_openAction->setEnabled(true);
686             } else if (clip && clip->clipType() == AUDIO && !KdenliveSettings::defaultaudioapp().isEmpty()) {
687                 m_openAction->setIcon(KIcon(KdenliveSettings::defaultaudioapp()));
688                 m_openAction->setEnabled(true);
689             } else {
690                 m_openAction->setEnabled(false);
691             }
692             // Display relevant transcoding actions only
693             adjustTranscodeActions(clip);
694             adjustStabilizeActions(clip);
695             // Display uses in timeline
696             emit findInTimeline(clip->clipId());
697         }
698     } else {
699         emit clipSelected(NULL);
700         m_editButton->defaultAction()->setEnabled(false);
701         m_deleteButton->defaultAction()->setEnabled(false);
702         m_openAction->setEnabled(false);
703         m_reloadAction->setEnabled(false);
704         m_transcodeAction->setEnabled(false);
705         m_stabilizeAction->setEnabled(false);
706     }
707     adjustProxyActions(clip);
708 }
709
710 void ProjectList::adjustProxyActions(ProjectItem *clip) const
711 {
712     if (clip == NULL || clip->type() != PROJECTCLIPTYPE || clip->clipType() == COLOR || clip->clipType() == TEXT || clip->clipType() == SLIDESHOW || clip->clipType() == AUDIO) {
713         m_proxyAction->setEnabled(false);
714         return;
715     }
716     m_proxyAction->setEnabled(useProxy());
717     m_proxyAction->blockSignals(true);
718     m_proxyAction->setChecked(clip->hasProxy());
719     m_proxyAction->blockSignals(false);
720 }
721
722 void ProjectList::adjustStabilizeActions(ProjectItem *clip) const
723 {
724
725     if (clip == NULL || clip->type() != PROJECTCLIPTYPE || clip->clipType() == COLOR || clip->clipType() == TEXT || clip->clipType() == PLAYLIST || clip->clipType() == SLIDESHOW) {
726         m_stabilizeAction->setEnabled(false);
727         return;
728     }
729         m_stabilizeAction->setEnabled(true);
730
731 }
732
733 void ProjectList::adjustTranscodeActions(ProjectItem *clip) const
734 {
735     if (clip == NULL || clip->type() != PROJECTCLIPTYPE || clip->clipType() == COLOR || clip->clipType() == TEXT || clip->clipType() == PLAYLIST || clip->clipType() == SLIDESHOW) {
736         m_transcodeAction->setEnabled(false);
737         return;
738     }
739     m_transcodeAction->setEnabled(true);
740     QList<QAction *> transcodeActions = m_transcodeAction->actions();
741     QStringList data;
742     QString condition;
743     for (int i = 0; i < transcodeActions.count(); i++) {
744         data = transcodeActions.at(i)->data().toStringList();
745         if (data.count() > 2) {
746             condition = data.at(2);
747             if (condition.startsWith("vcodec"))
748                 transcodeActions.at(i)->setEnabled(clip->referencedClip()->hasVideoCodec(condition.section('=', 1, 1)));
749             else if (condition.startsWith("acodec"))
750                 transcodeActions.at(i)->setEnabled(clip->referencedClip()->hasVideoCodec(condition.section('=', 1, 1)));
751         }
752     }
753
754 }
755
756 void ProjectList::slotPauseMonitor()
757 {
758     if (m_render)
759         m_render->pause();
760 }
761
762 void ProjectList::slotUpdateClipProperties(const QString &id, QMap <QString, QString> properties)
763 {
764     ProjectItem *item = getItemById(id);
765     if (item) {
766         slotUpdateClipProperties(item, properties);
767         if (properties.contains("out") || properties.contains("force_fps") || properties.contains("resource")) {
768             slotReloadClip(id);
769         } else if (properties.contains("colour") ||
770                    properties.contains("xmldata") ||
771                    properties.contains("force_aspect_num") ||
772                    properties.contains("force_aspect_den") ||
773                    properties.contains("templatetext")) {
774             slotRefreshClipThumbnail(item);
775             emit refreshClip(id, true);
776         } else if (properties.contains("full_luma") || properties.contains("force_colorspace") || properties.contains("loop")) {
777             emit refreshClip(id, false);
778         }
779     }
780 }
781
782 void ProjectList::slotUpdateClipProperties(ProjectItem *clip, QMap <QString, QString> properties)
783 {
784     if (!clip)
785         return;
786     clip->setProperties(properties);
787     if (properties.contains("name")) {
788         monitorItemEditing(false);
789         clip->setText(0, properties.value("name"));
790         monitorItemEditing(true);
791         emit clipNameChanged(clip->clipId(), properties.value("name"));
792     }
793     if (properties.contains("description")) {
794         CLIPTYPE type = clip->clipType();
795         monitorItemEditing(false);
796         clip->setText(1, properties.value("description"));
797         monitorItemEditing(true);
798 #ifdef NEPOMUK
799         if (KdenliveSettings::activate_nepomuk() && (type == AUDIO || type == VIDEO || type == AV || type == IMAGE || type == PLAYLIST)) {
800             // Use Nepomuk system to store clip description
801             Nepomuk::Resource f(clip->clipUrl().path());
802             f.setDescription(properties.value("description"));
803         }
804 #endif
805         emit projectModified();
806     }
807 }
808
809 void ProjectList::slotItemEdited(QTreeWidgetItem *item, int column)
810 {
811     if (item->type() == PROJECTSUBCLIPTYPE) {
812         // this is a sub-item
813         if (column == 1) {
814             // user edited description
815             SubProjectItem *sub = static_cast <SubProjectItem*>(item);
816             ProjectItem *item = static_cast <ProjectItem *>(sub->parent());
817             EditClipCutCommand *command = new EditClipCutCommand(this, item->clipId(), sub->zone(), sub->zone(), sub->description(), sub->text(1), true);
818             m_commandStack->push(command);
819             //slotUpdateCutClipProperties(sub->clipId(), sub->zone(), sub->text(1), sub->text(1));
820         }
821         return;
822     }
823     if (item->type() == PROJECTFOLDERTYPE) {
824         if (column == 0) {
825             FolderProjectItem *folder = static_cast <FolderProjectItem*>(item);
826             editFolder(item->text(0), folder->groupName(), folder->clipId());
827             folder->setGroupName(item->text(0));
828             m_doc->clipManager()->addFolder(folder->clipId(), item->text(0));
829             const int children = item->childCount();
830             for (int i = 0; i < children; i++) {
831                 ProjectItem *child = static_cast <ProjectItem *>(item->child(i));
832                 child->setProperty("groupname", item->text(0));
833             }
834         }
835         return;
836     }
837
838     ProjectItem *clip = static_cast <ProjectItem*>(item);
839     if (column == 1) {
840         if (clip->referencedClip()) {
841             QMap <QString, QString> oldprops;
842             QMap <QString, QString> newprops;
843             oldprops["description"] = clip->referencedClip()->getProperty("description");
844             newprops["description"] = item->text(1);
845
846             if (clip->clipType() == TEXT) {
847                 // This is a text template clip, update the image
848                 /*oldprops.insert("xmldata", clip->referencedClip()->getProperty("xmldata"));
849                 newprops.insert("xmldata", generateTemplateXml(clip->referencedClip()->getProperty("xmltemplate"), item->text(2)).toString());*/
850                 oldprops.insert("templatetext", clip->referencedClip()->getProperty("templatetext"));
851                 newprops.insert("templatetext", item->text(1));
852             }
853             slotUpdateClipProperties(clip->clipId(), newprops);
854             EditClipCommand *command = new EditClipCommand(this, clip->clipId(), oldprops, newprops, false);
855             m_commandStack->push(command);
856         }
857     } else if (column == 0) {
858         if (clip->referencedClip()) {
859             QMap <QString, QString> oldprops;
860             QMap <QString, QString> newprops;
861             oldprops["name"] = clip->referencedClip()->getProperty("name");
862             if (oldprops.value("name") != item->text(0)) {
863                 newprops["name"] = item->text(0);
864                 slotUpdateClipProperties(clip, newprops);
865                 emit projectModified();
866                 EditClipCommand *command = new EditClipCommand(this, clip->clipId(), oldprops, newprops, false);
867                 m_commandStack->push(command);
868             }
869         }
870     }
871 }
872
873 void ProjectList::slotContextMenu(const QPoint &pos, QTreeWidgetItem *item)
874 {
875     bool enable = item ? true : false;
876     m_editButton->defaultAction()->setEnabled(enable);
877     m_deleteButton->defaultAction()->setEnabled(enable);
878     m_reloadAction->setEnabled(enable);
879     m_transcodeAction->setEnabled(enable);
880     m_stabilizeAction->setEnabled(enable);
881     if (enable) {
882         ProjectItem *clip = NULL;
883         if (m_listView->currentItem()->type() == PROJECTSUBCLIPTYPE) {
884             clip = static_cast <ProjectItem*>(item->parent());
885             m_transcodeAction->setEnabled(false);
886             m_stabilizeAction->setEnabled(false);
887             adjustProxyActions(clip);
888         } else if (m_listView->currentItem()->type() == PROJECTCLIPTYPE) {
889             clip = static_cast <ProjectItem*>(item);
890             // Display relevant transcoding actions only
891             adjustTranscodeActions(clip);
892             adjustStabilizeActions(clip);
893             adjustProxyActions(clip);
894             // Display uses in timeline
895             emit findInTimeline(clip->clipId());
896         } else {
897             m_transcodeAction->setEnabled(false);
898             m_stabilizeAction->setEnabled(false);
899         }
900         if (clip && clip->clipType() == IMAGE && !KdenliveSettings::defaultimageapp().isEmpty()) {
901             m_openAction->setIcon(KIcon(KdenliveSettings::defaultimageapp()));
902             m_openAction->setEnabled(true);
903         } else if (clip && clip->clipType() == AUDIO && !KdenliveSettings::defaultaudioapp().isEmpty()) {
904             m_openAction->setIcon(KIcon(KdenliveSettings::defaultaudioapp()));
905             m_openAction->setEnabled(true);
906         } else {
907             m_openAction->setEnabled(false);
908         }
909
910     } else {
911         m_openAction->setEnabled(false);
912     }
913     m_menu->popup(pos);
914 }
915
916 void ProjectList::slotRemoveClip()
917 {
918     if (!m_listView->currentItem())
919         return;
920     QStringList ids;
921     QMap <QString, QString> folderids;
922     QList<QTreeWidgetItem *> selected = m_listView->selectedItems();
923
924     QUndoCommand *delCommand = new QUndoCommand();
925     delCommand->setText(i18n("Delete Clip Zone"));
926     for (int i = 0; i < selected.count(); i++) {
927         if (selected.at(i)->type() == PROJECTSUBCLIPTYPE) {
928             // subitem
929             SubProjectItem *sub = static_cast <SubProjectItem *>(selected.at(i));
930             ProjectItem *item = static_cast <ProjectItem *>(sub->parent());
931             new AddClipCutCommand(this, item->clipId(), sub->zone().x(), sub->zone().y(), sub->description(), false, true, delCommand);
932         } else if (selected.at(i)->type() == PROJECTFOLDERTYPE) {
933             // folder
934             FolderProjectItem *folder = static_cast <FolderProjectItem *>(selected.at(i));
935             folderids[folder->groupName()] = folder->clipId();
936             int children = folder->childCount();
937
938             if (children > 0 && KMessageBox::questionYesNo(kapp->activeWindow(), i18np("Delete folder <b>%2</b>?<br />This will also remove the clip in that folder", "Delete folder <b>%2</b>?<br />This will also remove the %1 clips in that folder",  children, folder->text(1)), i18n("Delete Folder")) != KMessageBox::Yes)
939                 return;
940             for (int i = 0; i < children; ++i) {
941                 ProjectItem *child = static_cast <ProjectItem *>(folder->child(i));
942                 ids << child->clipId();
943             }
944         } else {
945             ProjectItem *item = static_cast <ProjectItem *>(selected.at(i));
946             ids << item->clipId();
947             if (item->numReferences() > 0 && KMessageBox::questionYesNo(kapp->activeWindow(), i18np("Delete clip <b>%2</b>?<br />This will also remove the clip in timeline", "Delete clip <b>%2</b>?<br />This will also remove its %1 clips in timeline", item->numReferences(), item->names().at(1)), i18n("Delete Clip"), KStandardGuiItem::yes(), KStandardGuiItem::no(), "DeleteAll") == KMessageBox::No) {
948                 KMessageBox::enableMessage("DeleteAll");
949                 return;
950             }
951         }
952     }
953     KMessageBox::enableMessage("DeleteAll");
954     if (delCommand->childCount() == 0)
955         delete delCommand;
956     else
957         m_commandStack->push(delCommand);
958     emit deleteProjectClips(ids, folderids);
959 }
960
961 void ProjectList::updateButtons() const
962 {
963     if (m_listView->topLevelItemCount() == 0) {
964         m_deleteButton->defaultAction()->setEnabled(false);
965         m_editButton->defaultAction()->setEnabled(false);
966     } else {
967         m_deleteButton->defaultAction()->setEnabled(true);
968         if (!m_listView->currentItem())
969             m_listView->setCurrentItem(m_listView->topLevelItem(0));
970         QTreeWidgetItem *item = m_listView->currentItem();
971         if (item && item->type() == PROJECTCLIPTYPE) {
972             m_editButton->defaultAction()->setEnabled(true);
973             m_openAction->setEnabled(true);
974             m_reloadAction->setEnabled(true);
975             m_transcodeAction->setEnabled(true);
976             m_stabilizeAction->setEnabled(true);
977             return;
978         }
979         else if (item && item->type() == PROJECTFOLDERTYPE && item->childCount() > 0) {
980             m_editButton->defaultAction()->setEnabled(true);
981         }
982         else m_editButton->defaultAction()->setEnabled(false);
983     }
984     m_openAction->setEnabled(false);
985     m_reloadAction->setEnabled(false);
986     m_transcodeAction->setEnabled(false);
987     m_stabilizeAction->setEnabled(false);
988     m_proxyAction->setEnabled(false);
989 }
990
991 void ProjectList::selectItemById(const QString &clipId)
992 {
993     ProjectItem *item = getItemById(clipId);
994     if (item)
995         m_listView->setCurrentItem(item);
996 }
997
998
999 void ProjectList::slotDeleteClip(const QString &clipId)
1000 {
1001     ProjectItem *item = getItemById(clipId);
1002     if (!item) {
1003         kDebug() << "/// Cannot find clip to delete";
1004         return;
1005     }
1006     if (item->isProxyRunning()) m_abortProxy.append(item->referencedClip()->getProperty("proxy"));
1007     m_listView->blockSignals(true);
1008     QTreeWidgetItem *newSelectedItem = m_listView->itemAbove(item);
1009     if (!newSelectedItem)
1010         newSelectedItem = m_listView->itemBelow(item);
1011     delete item;
1012     // Pause playing to prevent crash while deleting clip
1013     slotPauseMonitor();
1014     m_doc->clipManager()->deleteClip(clipId);
1015     m_listView->blockSignals(false);
1016     if (newSelectedItem) {
1017         m_listView->setCurrentItem(newSelectedItem);
1018     } else {
1019         updateButtons();
1020         emit clipSelected(NULL);
1021     }
1022 }
1023
1024
1025 void ProjectList::editFolder(const QString folderName, const QString oldfolderName, const QString &clipId)
1026 {
1027     EditFolderCommand *command = new EditFolderCommand(this, folderName, oldfolderName, clipId, false);
1028     m_commandStack->push(command);
1029     m_doc->setModified(true);
1030 }
1031
1032 void ProjectList::slotAddFolder()
1033 {
1034     AddFolderCommand *command = new AddFolderCommand(this, i18n("Folder"), QString::number(m_doc->clipManager()->getFreeFolderId()), true);
1035     m_commandStack->push(command);
1036 }
1037
1038 void ProjectList::slotAddFolder(const QString foldername, const QString &clipId, bool remove, bool edit)
1039 {
1040     if (remove) {
1041         FolderProjectItem *item = getFolderItemById(clipId);
1042         if (item) {
1043             m_doc->clipManager()->deleteFolder(clipId);
1044             QTreeWidgetItem *newSelectedItem = m_listView->itemAbove(item);
1045             if (!newSelectedItem)
1046                 newSelectedItem = m_listView->itemBelow(item);
1047             delete item;
1048             if (newSelectedItem)
1049                 m_listView->setCurrentItem(newSelectedItem);
1050             else
1051                 updateButtons();
1052         }
1053     } else {
1054         if (edit) {
1055             FolderProjectItem *item = getFolderItemById(clipId);
1056             if (item) {
1057                 m_listView->blockSignals(true);
1058                 item->setGroupName(foldername);
1059                 m_listView->blockSignals(false);
1060                 m_doc->clipManager()->addFolder(clipId, foldername);
1061                 const int children = item->childCount();
1062                 for (int i = 0; i < children; i++) {
1063                     ProjectItem *child = static_cast <ProjectItem *>(item->child(i));
1064                     child->setProperty("groupname", foldername);
1065                 }
1066             }
1067         } else {
1068             m_listView->blockSignals(true);
1069             m_listView->setCurrentItem(new FolderProjectItem(m_listView, QStringList() << foldername, clipId));
1070             m_doc->clipManager()->addFolder(clipId, foldername);
1071             m_listView->blockSignals(false);
1072             m_listView->editItem(m_listView->currentItem(), 0);
1073         }
1074         updateButtons();
1075     }
1076     m_doc->setModified(true);
1077 }
1078
1079
1080
1081 void ProjectList::deleteProjectFolder(QMap <QString, QString> map)
1082 {
1083     QMapIterator<QString, QString> i(map);
1084     QUndoCommand *delCommand = new QUndoCommand();
1085     delCommand->setText(i18n("Delete Folder"));
1086     while (i.hasNext()) {
1087         i.next();
1088         new AddFolderCommand(this, i.key(), i.value(), false, delCommand);
1089     }
1090     if (delCommand->childCount() > 0) m_commandStack->push(delCommand);
1091     else delete delCommand;
1092 }
1093
1094 void ProjectList::slotAddClip(DocClipBase *clip, bool getProperties)
1095 {
1096     //m_listView->setEnabled(false);
1097     const QString parent = clip->getProperty("groupid");
1098     ProjectItem *item = NULL;
1099     monitorItemEditing(false);
1100     if (!parent.isEmpty()) {
1101         FolderProjectItem *parentitem = getFolderItemById(parent);
1102         if (!parentitem) {
1103             QStringList text;
1104             QString groupName = clip->getProperty("groupname");
1105             //kDebug() << "Adding clip to new group: " << groupName;
1106             if (groupName.isEmpty()) groupName = i18n("Folder");
1107             text << groupName;
1108             parentitem = new FolderProjectItem(m_listView, text, parent);
1109         }
1110
1111         if (parentitem)
1112             item = new ProjectItem(parentitem, clip);
1113     }
1114     if (item == NULL) {
1115         item = new ProjectItem(m_listView, clip);
1116     }
1117     if (item->data(0, DurationRole).isNull()) item->setData(0, DurationRole, i18n("Loading"));
1118     QString proxy = clip->getProperty("proxy");
1119     if (!proxy.isEmpty() && proxy != "-") slotCreateProxy(clip->getId());
1120     connect(clip, SIGNAL(createProxy(const QString &)), this, SLOT(slotCreateProxy(const QString &)));
1121     connect(clip, SIGNAL(abortProxy(const QString &, const QString &)), this, SLOT(slotAbortProxy(const QString, const QString)));
1122     if (getProperties) {
1123         int height = m_listView->iconSize().height();
1124         int width = (int)(height  * m_render->dar());
1125         QPixmap pix =  KIcon("video-x-generic").pixmap(QSize(width, height));
1126         item->setData(0, Qt::DecorationRole, pix);
1127         //item->setFlags(Qt::ItemIsSelectable);
1128         m_listView->processLayout();
1129         QDomElement e = clip->toXML().cloneNode().toElement();
1130         e.removeAttribute("file_hash");
1131         resetThumbsProducer(clip);
1132         m_render->getFileProperties(e, clip->getId(), m_listView->iconSize().height(), true);
1133     }
1134     else if (item->hasProxy() && !item->isProxyRunning()) {
1135         slotCreateProxy(clip->getId());
1136     }
1137     
1138     KUrl url = clip->fileURL();
1139 #ifdef NEPOMUK
1140     if (!url.isEmpty() && KdenliveSettings::activate_nepomuk()) {
1141         // if file has Nepomuk comment, use it
1142         Nepomuk::Resource f(url.path());
1143         QString annotation = f.description();
1144         if (!annotation.isEmpty()) item->setText(1, annotation);
1145         item->setText(2, QString::number(f.rating()));
1146     }
1147 #endif
1148
1149     // Add info to date column
1150     QFileInfo fileInfo(url.path());
1151     if (fileInfo.exists()) {
1152         item->setText(3, fileInfo.lastModified().toString(QString("yyyy/MM/dd hh:mm:ss")));
1153     }
1154
1155     // Add cut zones
1156     QList <CutZoneInfo> cuts = clip->cutZones();
1157     if (!cuts.isEmpty()) {
1158         for (int i = 0; i < cuts.count(); i++) {
1159             SubProjectItem *sub = new SubProjectItem(item, cuts.at(i).zone.x(), cuts.at(i).zone.y(), cuts.at(i).description);
1160             if (!clip->getClipHash().isEmpty()) {
1161                 QString cachedPixmap = m_doc->projectFolder().path(KUrl::AddTrailingSlash) + "thumbs/" + clip->getClipHash() + '#' + QString::number(cuts.at(i).zone.x()) + ".png";
1162                 if (QFile::exists(cachedPixmap)) {
1163                     QPixmap pix(cachedPixmap);
1164                     if (pix.isNull())
1165                         KIO::NetAccess::del(KUrl(cachedPixmap), this);
1166                     sub->setData(0, Qt::DecorationRole, pix);
1167                 }
1168             }
1169         }
1170     }
1171     monitorItemEditing(true);
1172     updateButtons();
1173 }
1174
1175 void ProjectList::slotGotProxy(const QString &proxyPath)
1176 {
1177     if (proxyPath.isEmpty() || m_abortAllProxies) return;
1178     QTreeWidgetItemIterator it(m_listView);
1179     ProjectItem *item;
1180
1181     while (*it && !m_abortAllProxies) {
1182         if ((*it)->type() == PROJECTCLIPTYPE) {
1183             item = static_cast <ProjectItem *>(*it);
1184             if (item->referencedClip()->getProperty("proxy") == proxyPath)
1185                 slotGotProxy(item);
1186         }
1187         ++it;
1188     }
1189 }
1190
1191 void ProjectList::slotGotProxy(ProjectItem *item)
1192 {
1193     if (item == NULL) return;
1194     DocClipBase *clip = item->referencedClip();
1195     if (!clip || !clip->isClean() || m_render->isProcessing(item->clipId())) {
1196         // Clip is being reprocessed, abort
1197         kDebug()<<"//// TRYING TO PROXY: "<<item->clipId()<<", but it is busy";
1198         return;
1199     }
1200     
1201     // Proxy clip successfully created
1202     QDomElement e = clip->toXML().cloneNode().toElement();
1203
1204     // Make sure we get the correct producer length if it was adjusted in timeline
1205     CLIPTYPE t = item->clipType();
1206     if (t == COLOR || t == IMAGE || t == SLIDESHOW || t == TEXT) {
1207         int length = QString(clip->producerProperty("length")).toInt();
1208         if (length > 0 && !e.hasAttribute("length")) {
1209             e.setAttribute("length", length);
1210         }
1211     }
1212     resetThumbsProducer(clip);
1213     m_render->getFileProperties(e, clip->getId(), m_listView->iconSize().height(), true);
1214 }
1215
1216 void ProjectList::slotResetProjectList()
1217 {
1218     m_listView->blockSignals(true);
1219     m_abortAllProxies = true;
1220     m_proxyThreads.waitForFinished();
1221     m_proxyThreads.clearFutures();
1222     m_thumbnailQueue.clear();
1223     m_listView->clear();
1224     m_listView->setEnabled(true);
1225     emit clipSelected(NULL);
1226     m_refreshed = false;
1227     m_allClipsProcessed = false;
1228     m_abortAllProxies = false;
1229     m_listView->blockSignals(false);
1230 }
1231
1232 void ProjectList::slotUpdateClip(const QString &id)
1233 {
1234     ProjectItem *item = getItemById(id);
1235     monitorItemEditing(false);
1236     if (item) item->setData(0, UsageRole, QString::number(item->numReferences()));
1237     monitorItemEditing(true);
1238 }
1239
1240 void ProjectList::getCachedThumbnail(ProjectItem *item)
1241 {
1242     if (!item) return;
1243     DocClipBase *clip = item->referencedClip();
1244     if (!clip) return;
1245     QString cachedPixmap = m_doc->projectFolder().path(KUrl::AddTrailingSlash) + "thumbs/" + clip->getClipHash() + ".png";
1246     if (QFile::exists(cachedPixmap)) {
1247         QPixmap pix(cachedPixmap);
1248         if (pix.isNull()) {
1249             KIO::NetAccess::del(KUrl(cachedPixmap), this);
1250             requestClipThumbnail(item->clipId());
1251         }
1252         else item->setData(0, Qt::DecorationRole, pix);
1253     }
1254     else {
1255         requestClipThumbnail(item->clipId());
1256     }
1257 }
1258
1259 void ProjectList::getCachedThumbnail(SubProjectItem *item)
1260 {
1261     if (!item) return;
1262     ProjectItem *parentItem = static_cast <ProjectItem *>(item->parent());
1263     if (!parentItem) return;
1264     DocClipBase *clip = parentItem->referencedClip();
1265     if (!clip) return;
1266     int pos = item->zone().x();
1267     QString cachedPixmap = m_doc->projectFolder().path(KUrl::AddTrailingSlash) + "thumbs/" + clip->getClipHash() + "#" + QString::number(pos) + ".png";
1268     if (QFile::exists(cachedPixmap)) {
1269         QPixmap pix(cachedPixmap);
1270         if (pix.isNull()) {
1271             KIO::NetAccess::del(KUrl(cachedPixmap), this);
1272             requestClipThumbnail(parentItem->clipId() + '#' + QString::number(pos));
1273         }
1274         else item->setData(0, Qt::DecorationRole, pix);
1275     }
1276     else requestClipThumbnail(parentItem->clipId() + '#' + QString::number(pos));
1277 }
1278
1279 void ProjectList::updateAllClips(bool displayRatioChanged, bool fpsChanged, QStringList brokenClips)
1280 {
1281     if (!m_allClipsProcessed) m_listView->setEnabled(false);
1282     m_listView->setSortingEnabled(false);
1283     QTreeWidgetItemIterator it(m_listView);
1284     DocClipBase *clip;
1285     ProjectItem *item;
1286     monitorItemEditing(false);
1287     int height = m_listView->iconSize().height();
1288     int width = (int)(height  * m_render->dar());
1289     QPixmap missingPixmap = QPixmap(width, height);
1290     missingPixmap.fill(Qt::transparent);
1291     KIcon icon("dialog-close");
1292     QPainter p(&missingPixmap);
1293     p.drawPixmap(3, 3, icon.pixmap(width - 6, height - 6));
1294     p.end();
1295     
1296     int max = m_doc->clipManager()->clipsCount();
1297     max = qMax(1, max);
1298     int ct = 0;
1299
1300     while (*it) {
1301         emit displayMessage(i18n("Loading thumbnails"), (int)(100 *(max - ct++) / max));
1302         if ((*it)->type() == PROJECTSUBCLIPTYPE) {
1303             // subitem
1304             SubProjectItem *sub = static_cast <SubProjectItem *>(*it);
1305             if (displayRatioChanged) {
1306                 item = static_cast <ProjectItem *>((*it)->parent());
1307                 requestClipThumbnail(item->clipId() + '#' + QString::number(sub->zone().x()));
1308             }
1309             else if (sub->data(0, Qt::DecorationRole).isNull()) {
1310                 getCachedThumbnail(sub);
1311             }
1312             ++it;
1313             continue;
1314         } else if ((*it)->type() == PROJECTFOLDERTYPE) {
1315             // folder
1316             ++it;
1317             continue;
1318         } else {
1319             item = static_cast <ProjectItem *>(*it);
1320             clip = item->referencedClip();
1321             if (item->referencedClip()->getProducer() == NULL) {
1322                 bool replace = false;
1323                 if (brokenClips.contains(item->clipId())) {
1324                     // if this is a proxy clip, disable proxy
1325                     item->setProxyStatus(NOPROXY);
1326                     clip->setProperty("proxy", "-");
1327                     replace = true;
1328                 }
1329                 if (clip->isPlaceHolder() == false && !item->isProxyRunning()) {
1330                     QDomElement xml = clip->toXML();
1331                     if (fpsChanged) {
1332                         xml.removeAttribute("out");
1333                         xml.removeAttribute("file_hash");
1334                         xml.removeAttribute("proxy_out");
1335                     }
1336                     if (!replace) replace = xml.attribute("replace") == "1";
1337                     if (replace) resetThumbsProducer(clip);
1338                     m_render->getFileProperties(xml, clip->getId(), m_listView->iconSize().height(), replace);
1339                 }
1340                 else if (clip->isPlaceHolder()) {
1341                     item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsDropEnabled);
1342                     if (item->data(0, Qt::DecorationRole).isNull()) {
1343                         item->setData(0, Qt::DecorationRole, missingPixmap);
1344                     }
1345                     else {
1346                         QPixmap pixmap = qVariantValue<QPixmap>(item->data(0, Qt::DecorationRole));
1347                         QPainter p(&pixmap);
1348                         p.drawPixmap(3, 3, KIcon("dialog-close").pixmap(pixmap.width() - 6, pixmap.height() - 6));
1349                         p.end();
1350                         item->setData(0, Qt::DecorationRole, pixmap);
1351                     }
1352                 }
1353             } else {              
1354                 if (displayRatioChanged)
1355                     requestClipThumbnail(clip->getId());
1356                 else if (item->data(0, Qt::DecorationRole).isNull()) {
1357                     getCachedThumbnail(item);
1358                 }
1359                 if (item->data(0, DurationRole).toString().isEmpty()) {
1360                     item->changeDuration(item->referencedClip()->getProducer()->get_playtime());
1361                 }
1362                 if (clip->isPlaceHolder()) {
1363                     QPixmap pixmap = qVariantValue<QPixmap>(item->data(0, Qt::DecorationRole));
1364                     if (pixmap.isNull()) {
1365                         pixmap = QPixmap(width, height);
1366                         pixmap.fill(Qt::transparent);
1367                     }
1368                     QPainter p(&pixmap);
1369                     p.drawPixmap(3, 3, KIcon("dialog-close").pixmap(pixmap.width() - 6, pixmap.height() - 6));
1370                     p.end();
1371                     item->setData(0, Qt::DecorationRole, pixmap);
1372                 }
1373             }
1374             item->setData(0, UsageRole, QString::number(item->numReferences()));
1375         }
1376         ++it;
1377     }
1378
1379     m_listView->setSortingEnabled(true);
1380     m_allClipsProcessed = true;
1381     if (m_render->processingItems() == 0) {
1382        monitorItemEditing(true);
1383        slotProcessNextThumbnail();
1384     }
1385 }
1386
1387 // static
1388 QString ProjectList::getExtensions()
1389 {
1390     // Build list of mime types
1391     QStringList mimeTypes = QStringList() << "application/x-kdenlive" << "application/x-kdenlivetitle" << "video/mlt-playlist" << "text/plain"
1392                             << "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" << "video/webm"
1393                             << "audio/x-flac" << "audio/x-matroska" << "audio/mp4" << "audio/mpeg" << "audio/x-mp3" << "audio/ogg" << "audio/x-wav" << "audio/x-aiff" << "audio/aiff" << "application/ogg" << "application/mxf" << "application/x-shockwave-flash"
1394                             << "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";
1395
1396     QString allExtensions;
1397     foreach(const QString & mimeType, mimeTypes) {
1398         KMimeType::Ptr mime(KMimeType::mimeType(mimeType));
1399         if (mime) {
1400             allExtensions.append(mime->patterns().join(" "));
1401             allExtensions.append(' ');
1402         }
1403     }
1404     return allExtensions.simplified();
1405 }
1406
1407 void ProjectList::slotAddClip(const QList <QUrl> givenList, const QString &groupName, const QString &groupId)
1408 {
1409     if (!m_commandStack)
1410         kDebug() << "!!!!!!!!!!!!!!!! NO CMD STK";
1411
1412     KUrl::List list;
1413     if (givenList.isEmpty()) {
1414         QString allExtensions = getExtensions();
1415         const QString dialogFilter = allExtensions + ' ' + QLatin1Char('|') + i18n("All Supported Files") + "\n* " + QLatin1Char('|') + i18n("All Files");
1416         QCheckBox *b = new QCheckBox(i18n("Import image sequence"));
1417         b->setChecked(KdenliveSettings::autoimagesequence());
1418         QCheckBox *c = new QCheckBox(i18n("Transparent background for images"));
1419         c->setChecked(KdenliveSettings::autoimagetransparency());
1420         QFrame *f = new QFrame;
1421         f->setFrameShape(QFrame::NoFrame);
1422         QHBoxLayout *l = new QHBoxLayout;
1423         l->addWidget(b);
1424         l->addWidget(c);
1425         l->addStretch(5);
1426         f->setLayout(l);
1427         KFileDialog *d = new KFileDialog(KUrl("kfiledialog:///clipfolder"), dialogFilter, kapp->activeWindow(), f);
1428         d->setOperationMode(KFileDialog::Opening);
1429         d->setMode(KFile::Files);
1430         if (d->exec() == QDialog::Accepted) {
1431             KdenliveSettings::setAutoimagetransparency(c->isChecked());
1432         }
1433         list = d->selectedUrls();
1434         if (b->isChecked() && list.count() == 1) {
1435             // Check for image sequence
1436             KUrl url = list.at(0);
1437             QString fileName = url.fileName().section('.', 0, -2);
1438             if (fileName.at(fileName.size() - 1).isDigit()) {
1439                 KFileItem item(KFileItem::Unknown, KFileItem::Unknown, url);
1440                 if (item.mimetype().startsWith("image")) {
1441                     // import as sequence if we found more than one image in the sequence
1442                     QStringList list;
1443                     QString pattern = SlideshowClip::selectedPath(url.path(), false, QString(), &list);
1444                     int count = list.count();
1445                     if (count > 1) {
1446                         delete d;
1447                         QStringList groupInfo = getGroup();
1448
1449                         // get image sequence base name
1450                         while (fileName.at(fileName.size() - 1).isDigit()) {
1451                             fileName.chop(1);
1452                         }
1453
1454                         m_doc->slotCreateSlideshowClipFile(fileName, pattern, count, m_timecode.reformatSeparators(KdenliveSettings::sequence_duration()),
1455                                                            false, false, false,
1456                                                            m_timecode.getTimecodeFromFrames(int(ceil(m_timecode.fps()))), QString(), 0,
1457                                                            QString(), groupInfo.at(0), groupInfo.at(1));
1458                         return;
1459                     }
1460                 }
1461             }
1462         }
1463         delete d;
1464     } else {
1465         for (int i = 0; i < givenList.count(); i++)
1466             list << givenList.at(i);
1467     }
1468
1469     foreach(const KUrl & file, list) {
1470         // Check there is no folder here
1471         KMimeType::Ptr type = KMimeType::findByUrl(file);
1472         if (type->is("inode/directory")) {
1473             // user dropped a folder
1474             list.removeAll(file);
1475         }
1476     }
1477
1478     if (list.isEmpty())
1479         return;
1480
1481     if (givenList.isEmpty()) {
1482         QStringList groupInfo = getGroup();
1483         m_doc->slotAddClipList(list, groupInfo.at(0), groupInfo.at(1));
1484     } else {
1485         m_doc->slotAddClipList(list, groupName, groupId);
1486     }
1487 }
1488
1489 void ProjectList::slotRemoveInvalidClip(const QString &id, bool replace)
1490 {
1491     ProjectItem *item = getItemById(id);
1492     m_processingClips.removeAll(id);
1493     m_thumbnailQueue.removeAll(id);
1494     if (item) {
1495         item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsDragEnabled | Qt::ItemIsEnabled | Qt::ItemIsEditable | Qt::ItemIsDropEnabled);
1496         const QString path = item->referencedClip()->fileURL().path();
1497         if (item->referencedClip()->isPlaceHolder()) replace = false;
1498         if (!path.isEmpty()) {
1499             if (m_invalidClipDialog) {
1500                 m_invalidClipDialog->addClip(id, path);
1501                 return;
1502             }
1503             else {
1504                 if (replace)
1505                     m_invalidClipDialog = new InvalidDialog(i18n("Invalid clip"),  i18n("Clip <b>%1</b><br />is invalid, will be removed from project.", QString()), replace, kapp->activeWindow());
1506                 else {
1507                     m_invalidClipDialog = new InvalidDialog(i18n("Invalid clip"),  i18n("Clip <b>%1</b><br />is missing or invalid. Remove it from project?", QString()), replace, kapp->activeWindow());
1508                 }
1509                 m_invalidClipDialog->addClip(id, path);
1510                 int result = m_invalidClipDialog->exec();
1511                 if (result == KDialog::Yes) replace = true;
1512             }
1513         }
1514         if (m_invalidClipDialog) {
1515             if (replace)
1516                 emit deleteProjectClips(m_invalidClipDialog->getIds(), QMap <QString, QString>());
1517             delete m_invalidClipDialog;
1518             m_invalidClipDialog = NULL;
1519         }
1520         
1521     }
1522 }
1523
1524 void ProjectList::slotRemoveInvalidProxy(const QString &id, bool durationError)
1525 {
1526     ProjectItem *item = getItemById(id);
1527     if (item) {
1528         kDebug()<<"// Proxy for clip "<<id<<" is invalid, delete";
1529         item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsDragEnabled | Qt::ItemIsEnabled | Qt::ItemIsEditable | Qt::ItemIsDropEnabled);
1530         if (durationError) {
1531             kDebug() << "Proxy duration is wrong, try changing transcoding parameters.";
1532             emit displayMessage(i18n("Proxy clip unusable (duration is different from original)."), -2);
1533         }
1534         item->setProxyStatus(PROXYCRASHED);
1535         QString path = item->referencedClip()->getProperty("proxy");
1536         KUrl proxyFolder(m_doc->projectFolder().path( KUrl::AddTrailingSlash) + "proxy/");
1537
1538         //Security check: make sure the invalid proxy file is in the proxy folder
1539         if (proxyFolder.isParentOf(KUrl(path))) {
1540             QFile::remove(path);
1541         }
1542         if (item->referencedClip()->getProducer() == NULL) {
1543             // Clip has no valid producer, request it
1544             slotProxyCurrentItem(false, item);
1545         }
1546         else {
1547             // refresh thumbs producer
1548             item->referencedClip()->reloadThumbProducer();
1549         }
1550     }
1551     m_processingClips.removeAll(id);
1552     m_thumbnailQueue.removeAll(id);
1553 }
1554
1555 void ProjectList::slotAddColorClip()
1556 {
1557     if (!m_commandStack)
1558         kDebug() << "!!!!!!!!!!!!!!!! NO CMD STK";
1559
1560     QDialog *dia = new QDialog(this);
1561     Ui::ColorClip_UI dia_ui;
1562     dia_ui.setupUi(dia);
1563     dia->setWindowTitle(i18n("Color Clip"));
1564     dia_ui.clip_name->setText(i18n("Color Clip"));
1565
1566     TimecodeDisplay *t = new TimecodeDisplay(m_timecode);
1567     t->setValue(KdenliveSettings::color_duration());
1568     dia_ui.clip_durationBox->addWidget(t);
1569     dia_ui.clip_color->setColor(KdenliveSettings::colorclipcolor());
1570
1571     if (dia->exec() == QDialog::Accepted) {
1572         QString color = dia_ui.clip_color->color().name();
1573         KdenliveSettings::setColorclipcolor(color);
1574         color = color.replace(0, 1, "0x") + "ff";
1575         QStringList groupInfo = getGroup();
1576         m_doc->slotCreateColorClip(dia_ui.clip_name->text(), color, m_timecode.getTimecode(t->gentime()), groupInfo.at(0), groupInfo.at(1));
1577     }
1578     delete t;
1579     delete dia;
1580 }
1581
1582
1583 void ProjectList::slotAddSlideshowClip()
1584 {
1585     if (!m_commandStack)
1586         kDebug() << "!!!!!!!!!!!!!!!! NO CMD STK";
1587
1588     SlideshowClip *dia = new SlideshowClip(m_timecode, this);
1589
1590     if (dia->exec() == QDialog::Accepted) {
1591         QStringList groupInfo = getGroup();
1592         m_doc->slotCreateSlideshowClipFile(dia->clipName(), dia->selectedPath(), dia->imageCount(), dia->clipDuration(),
1593                                            dia->loop(), dia->crop(), dia->fade(),
1594                                            dia->lumaDuration(), dia->lumaFile(), dia->softness(),
1595                                            dia->animation(), groupInfo.at(0), groupInfo.at(1));
1596     }
1597     delete dia;
1598 }
1599
1600 void ProjectList::slotAddTitleClip()
1601 {
1602     QStringList groupInfo = getGroup();
1603     m_doc->slotCreateTextClip(groupInfo.at(0), groupInfo.at(1));
1604 }
1605
1606 void ProjectList::slotAddTitleTemplateClip()
1607 {
1608     if (!m_commandStack)
1609         kDebug() << "!!!!!!!!!!!!!!!! NO CMD STK";
1610
1611     QStringList groupInfo = getGroup();
1612
1613     // Get the list of existing templates
1614     QStringList filter;
1615     filter << "*.kdenlivetitle";
1616     const QString path = m_doc->projectFolder().path(KUrl::AddTrailingSlash) + "titles/";
1617     QStringList templateFiles = QDir(path).entryList(filter, QDir::Files);
1618
1619     QDialog *dia = new QDialog(this);
1620     Ui::TemplateClip_UI dia_ui;
1621     dia_ui.setupUi(dia);
1622     for (int i = 0; i < templateFiles.size(); ++i)
1623         dia_ui.template_list->comboBox()->addItem(templateFiles.at(i), path + templateFiles.at(i));
1624
1625     if (!templateFiles.isEmpty())
1626         dia_ui.buttonBox->button(QDialogButtonBox::Ok)->setFocus();
1627     dia_ui.template_list->fileDialog()->setFilter("application/x-kdenlivetitle");
1628     //warning: setting base directory doesn't work??
1629     KUrl startDir(path);
1630     dia_ui.template_list->fileDialog()->setUrl(startDir);
1631     dia_ui.text_box->setHidden(true);
1632     if (dia->exec() == QDialog::Accepted) {
1633         QString textTemplate = dia_ui.template_list->comboBox()->itemData(dia_ui.template_list->comboBox()->currentIndex()).toString();
1634         if (textTemplate.isEmpty()) textTemplate = dia_ui.template_list->comboBox()->currentText();
1635         // Create a cloned template clip
1636         m_doc->slotCreateTextTemplateClip(groupInfo.at(0), groupInfo.at(1), KUrl(textTemplate));
1637     }
1638     delete dia;
1639 }
1640
1641 QStringList ProjectList::getGroup() const
1642 {
1643     QStringList result;
1644     QTreeWidgetItem *item = m_listView->currentItem();
1645     while (item && item->type() != PROJECTFOLDERTYPE)
1646         item = item->parent();
1647
1648     if (item) {
1649         FolderProjectItem *folder = static_cast <FolderProjectItem *>(item);
1650         result << folder->groupName() << folder->clipId();
1651     } else {
1652         result << QString() << QString();
1653     }
1654     return result;
1655 }
1656
1657 void ProjectList::setDocument(KdenliveDoc *doc)
1658 {
1659     m_listView->blockSignals(true);
1660     m_abortAllProxies = true;
1661     m_proxyThreads.waitForFinished();
1662     m_proxyThreads.clearFutures();
1663     m_thumbnailQueue.clear();
1664     m_listView->clear();
1665     m_processingClips.clear();
1666     
1667     m_listView->setSortingEnabled(false);
1668     emit clipSelected(NULL);
1669     m_refreshed = false;
1670     m_allClipsProcessed = false;
1671     m_fps = doc->fps();
1672     m_timecode = doc->timecode();
1673     m_commandStack = doc->commandStack();
1674     m_doc = doc;
1675     m_abortAllProxies = false;
1676
1677     QMap <QString, QString> flist = doc->clipManager()->documentFolderList();
1678     QStringList openedFolders = doc->getExpandedFolders();
1679     QMapIterator<QString, QString> f(flist);
1680     while (f.hasNext()) {
1681         f.next();
1682         FolderProjectItem *folder = new FolderProjectItem(m_listView, QStringList() << f.value(), f.key());
1683         folder->setExpanded(openedFolders.contains(f.key()));
1684     }
1685
1686     QList <DocClipBase*> list = doc->clipManager()->documentClipList();
1687     if (list.isEmpty()) {
1688         // blank document
1689         m_refreshed = true;
1690         m_allClipsProcessed = true;
1691     }
1692     for (int i = 0; i < list.count(); i++)
1693         slotAddClip(list.at(i), false);
1694
1695     m_listView->blockSignals(false);
1696     connect(m_doc->clipManager(), SIGNAL(reloadClip(const QString &)), this, SLOT(slotReloadClip(const QString &)));
1697     connect(m_doc->clipManager(), SIGNAL(modifiedClip(const QString &)), this, SLOT(slotModifiedClip(const QString &)));
1698     connect(m_doc->clipManager(), SIGNAL(missingClip(const QString &)), this, SLOT(slotMissingClip(const QString &)));
1699     connect(m_doc->clipManager(), SIGNAL(availableClip(const QString &)), this, SLOT(slotAvailableClip(const QString &)));
1700     connect(m_doc->clipManager(), SIGNAL(checkAllClips(bool, bool, QStringList)), this, SLOT(updateAllClips(bool, bool, QStringList)));
1701 }
1702
1703 QList <DocClipBase*> ProjectList::documentClipList() const
1704 {
1705     if (m_doc == NULL)
1706         return QList <DocClipBase*> ();
1707
1708     return m_doc->clipManager()->documentClipList();
1709 }
1710
1711 QDomElement ProjectList::producersList()
1712 {
1713     QDomDocument doc;
1714     QDomElement prods = doc.createElement("producerlist");
1715     doc.appendChild(prods);
1716     QTreeWidgetItemIterator it(m_listView);
1717     while (*it) {
1718         if ((*it)->type() != PROJECTCLIPTYPE) {
1719             // subitem
1720             ++it;
1721             continue;
1722         }
1723         prods.appendChild(doc.importNode(((ProjectItem *)(*it))->toXml(), true));
1724         ++it;
1725     }
1726     return prods;
1727 }
1728
1729 void ProjectList::slotCheckForEmptyQueue()
1730 {
1731     if (m_render->processingItems() == 0 && m_thumbnailQueue.isEmpty()) {
1732         if (!m_refreshed && m_allClipsProcessed) {
1733             m_refreshed = true;
1734             m_listView->setEnabled(true);
1735             slotClipSelected();
1736             QTimer::singleShot(500, this, SIGNAL(loadingIsOver()));
1737             emit displayMessage(QString(), -1);
1738         }
1739         updateButtons();
1740     } else if (!m_refreshed) {
1741         QTimer::singleShot(300, this, SLOT(slotCheckForEmptyQueue()));
1742     }
1743 }
1744
1745
1746 void ProjectList::requestClipThumbnail(const QString id)
1747 {
1748     if (!m_thumbnailQueue.contains(id)) m_thumbnailQueue.append(id);
1749     slotProcessNextThumbnail();
1750 }
1751
1752 void ProjectList::resetThumbsProducer(DocClipBase *clip)
1753 {
1754     if (!clip) return;
1755     clip->clearThumbProducer();
1756     QString id = clip->getId();
1757     m_thumbnailQueue.removeAll(id);
1758 }
1759
1760 void ProjectList::slotProcessNextThumbnail()
1761 {
1762     if (m_render->processingItems() > 0) {
1763         return;
1764     }
1765     if (m_thumbnailQueue.isEmpty()) {
1766         slotCheckForEmptyQueue();
1767         return;
1768     }
1769     int max = m_doc->clipManager()->clipsCount();
1770     emit displayMessage(i18n("Loading thumbnails"), (int)(100 *(max - m_thumbnailQueue.count()) / max));
1771     slotRefreshClipThumbnail(m_thumbnailQueue.takeFirst(), false);
1772 }
1773
1774 void ProjectList::slotRefreshClipThumbnail(const QString &clipId, bool update)
1775 {
1776     QTreeWidgetItem *item = getAnyItemById(clipId);
1777     if (item)
1778         slotRefreshClipThumbnail(item, update);
1779     else {
1780         slotProcessNextThumbnail();
1781     }
1782 }
1783
1784 void ProjectList::slotRefreshClipThumbnail(QTreeWidgetItem *it, bool update)
1785 {
1786     if (it == NULL) return;
1787     ProjectItem *item = NULL;
1788     bool isSubItem = false;
1789     int frame;
1790     if (it->type() == PROJECTFOLDERTYPE) return;
1791     if (it->type() == PROJECTSUBCLIPTYPE) {
1792         item = static_cast <ProjectItem *>(it->parent());
1793         frame = static_cast <SubProjectItem *>(it)->zone().x();
1794         isSubItem = true;
1795     } else {
1796         item = static_cast <ProjectItem *>(it);
1797         frame = item->referencedClip()->getClipThumbFrame();
1798     }
1799
1800     if (item) {
1801         DocClipBase *clip = item->referencedClip();
1802         if (!clip) {
1803             slotProcessNextThumbnail();
1804             return;
1805         }
1806         QPixmap pix;
1807         QImage img;
1808         int height = m_listView->iconSize().height();
1809         int swidth = (int)(height  * m_render->frameRenderWidth() / m_render->renderHeight()+ 0.5);
1810         int dwidth = (int)(height  * m_render->dar() + 0.5);
1811         if (clip->clipType() == AUDIO)
1812             pix = KIcon("audio-x-generic").pixmap(QSize(dwidth, height));
1813         else if (clip->clipType() == IMAGE)
1814             img = KThumb::getFrame(item->referencedClip()->getProducer(), 0, swidth, dwidth, height);
1815         else {
1816             img = item->referencedClip()->extractImage(frame, dwidth, height);
1817         }
1818
1819         if (!pix.isNull() || !img.isNull()) {
1820             monitorItemEditing(false);
1821             if (!img.isNull()) pix = QPixmap::fromImage(img);
1822             it->setData(0, Qt::DecorationRole, pix);
1823             monitorItemEditing(true);
1824             
1825             QString hash = item->getClipHash();
1826             if (!hash.isEmpty() && !img.isNull()) {
1827                 if (!isSubItem)
1828                     m_doc->cacheImage(hash, img);
1829                 else
1830                     m_doc->cacheImage(hash + '#' + QString::number(frame), img);
1831             }
1832         }
1833         if (update)
1834             emit projectModified();
1835         slotProcessNextThumbnail();
1836     }
1837 }
1838
1839
1840 void ProjectList::slotReplyGetFileProperties(const QString &clipId, Mlt::Producer *producer, const stringMap &properties, const stringMap &metadata, bool replace)
1841 {
1842     QString toReload;
1843     ProjectItem *item = getItemById(clipId);
1844
1845     int queue = m_render->processingItems();
1846     if (item && producer) {
1847         monitorItemEditing(false);
1848         DocClipBase *clip = item->referencedClip();
1849         if (producer->is_valid()) {
1850             if (clip->isPlaceHolder()) {
1851                 clip->setValid();
1852                 toReload = clipId;
1853             }
1854             item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsDragEnabled | Qt::ItemIsEnabled | Qt::ItemIsEditable | Qt::ItemIsDropEnabled);
1855         }
1856         item->setProperties(properties, metadata);
1857         clip->setProducer(producer, replace);
1858
1859         // Proxy stuff
1860         QString size = properties.value("frame_size");
1861         if (!useProxy() && clip->getProperty("proxy").isEmpty()) setProxyStatus(item, NOPROXY);
1862         if (useProxy() && generateProxy() && clip->getProperty("proxy") == "-") setProxyStatus(item, NOPROXY);
1863         else if (useProxy() && !item->hasProxy() && !item->isProxyRunning()) {
1864             // proxy video and image clips
1865             int maxSize;
1866             CLIPTYPE t = item->clipType();
1867             if (t == IMAGE) maxSize = m_doc->getDocumentProperty("proxyimageminsize").toInt();
1868             else maxSize = m_doc->getDocumentProperty("proxyminsize").toInt();
1869             if ((((t == AV || t == VIDEO || t == PLAYLIST) && generateProxy()) || (t == IMAGE && generateImageProxy())) && (size.section('x', 0, 0).toInt() > maxSize || size.section('x', 1, 1).toInt() > maxSize)) {
1870                 if (clip->getProperty("proxy").isEmpty()) {
1871                     KUrl proxyPath = m_doc->projectFolder();
1872                     proxyPath.addPath("proxy/");
1873                     proxyPath.addPath(clip->getClipHash() + "." + (t == IMAGE ? "png" : m_doc->getDocumentProperty("proxyextension")));
1874                     QMap <QString, QString> newProps;
1875                     // insert required duration for proxy
1876                     if (t != IMAGE) newProps.insert("proxy_out", clip->producerProperty("out"));
1877                     newProps.insert("proxy", proxyPath.path());
1878                     QMap <QString, QString> oldProps = clip->properties();
1879                     oldProps.insert("proxy", QString());
1880                     EditClipCommand *command = new EditClipCommand(this, clipId, oldProps, newProps, true);
1881                     m_doc->commandStack()->push(command);
1882                 }
1883             }
1884         }
1885
1886         if (!replace && m_allClipsProcessed && item->data(0, Qt::DecorationRole).isNull()) {
1887             getCachedThumbnail(item);
1888         }
1889         if (!toReload.isEmpty())
1890             item->slotSetToolTip();
1891     } else kDebug() << "////////  COULD NOT FIND CLIP TO UPDATE PRPS...";
1892     if (queue == 0) {
1893         monitorItemEditing(true);
1894         if (item && m_thumbnailQueue.isEmpty()) {
1895             if (!item->hasProxy() || m_render->activeClipId() == item->clipId())
1896                 m_listView->setCurrentItem(item);
1897             bool updatedProfile = false;
1898             if (item->parent()) {
1899                 if (item->parent()->type() == PROJECTFOLDERTYPE)
1900                     static_cast <FolderProjectItem *>(item->parent())->switchIcon();
1901             } else if (KdenliveSettings::checkfirstprojectclip() &&  m_listView->topLevelItemCount() == 1 && m_refreshed && m_allClipsProcessed) {
1902                 // this is the first clip loaded in project, check if we want to adjust project settings to the clip
1903                 updatedProfile = adjustProjectProfileToItem(item);
1904             }
1905             if (updatedProfile == false) {
1906                 //emit clipSelected(item->referencedClip());
1907             }
1908         } else {
1909             int max = m_doc->clipManager()->clipsCount();
1910             if (max > 0) emit displayMessage(i18n("Loading clips"), (int)(100 *(max - queue) / max));
1911         }
1912         if (m_allClipsProcessed) emit processNextThumbnail();
1913     }
1914     if (!item) {
1915         // no item for producer, delete it
1916         delete producer;
1917         return;
1918     }
1919     if (replace) toReload = clipId;
1920     if (!toReload.isEmpty())
1921         emit clipNeedsReload(toReload);
1922 }
1923
1924 bool ProjectList::adjustProjectProfileToItem(ProjectItem *item)
1925 {
1926     if (item == NULL) {
1927         if (m_listView->currentItem() && m_listView->currentItem()->type() != PROJECTFOLDERTYPE)
1928             item = static_cast <ProjectItem*>(m_listView->currentItem());
1929     }
1930     if (item == NULL || item->referencedClip() == NULL) {
1931         KMessageBox::information(kapp->activeWindow(), i18n("Cannot find profile from current clip"));
1932         return false;
1933     }
1934     bool profileUpdated = false;
1935     QString size = item->referencedClip()->getProperty("frame_size");
1936     int width = size.section('x', 0, 0).toInt();
1937     int height = size.section('x', -1).toInt();
1938     double fps = item->referencedClip()->getProperty("fps").toDouble();
1939     double par = item->referencedClip()->getProperty("aspect_ratio").toDouble();
1940     if (item->clipType() == IMAGE || item->clipType() == AV || item->clipType() == VIDEO) {
1941         if (ProfilesDialog::matchProfile(width, height, fps, par, item->clipType() == IMAGE, m_doc->mltProfile()) == false) {
1942             // get a list of compatible profiles
1943             QMap <QString, QString> suggestedProfiles = ProfilesDialog::getProfilesFromProperties(width, height, fps, par, item->clipType() == IMAGE);
1944             if (!suggestedProfiles.isEmpty()) {
1945                 KDialog *dialog = new KDialog(this);
1946                 dialog->setCaption(i18n("Change project profile"));
1947                 dialog->setButtons(KDialog::Ok | KDialog::Cancel);
1948
1949                 QWidget container;
1950                 QVBoxLayout *l = new QVBoxLayout;
1951                 QLabel *label = new QLabel(i18n("Your clip does not match current project's profile.\nDo you want to change the project profile?\n\nThe following profiles match the clip (size: %1, fps: %2)", size, fps));
1952                 l->addWidget(label);
1953                 QListWidget *list = new QListWidget;
1954                 list->setAlternatingRowColors(true);
1955                 QMapIterator<QString, QString> i(suggestedProfiles);
1956                 while (i.hasNext()) {
1957                     i.next();
1958                     QListWidgetItem *item = new QListWidgetItem(i.value(), list);
1959                     item->setData(Qt::UserRole, i.key());
1960                     item->setToolTip(i.key());
1961                 }
1962                 list->setCurrentRow(0);
1963                 l->addWidget(list);
1964                 container.setLayout(l);
1965                 dialog->setButtonText(KDialog::Ok, i18n("Update profile"));
1966                 dialog->setMainWidget(&container);
1967                 if (dialog->exec() == QDialog::Accepted) {
1968                     //Change project profile
1969                     profileUpdated = true;
1970                     if (list->currentItem())
1971                         emit updateProfile(list->currentItem()->data(Qt::UserRole).toString());
1972                 }
1973                 delete list;
1974                 delete label;
1975             } else if (fps > 0) {
1976                 KMessageBox::information(kapp->activeWindow(), i18n("Your clip does not match current project's profile.\nNo existing profile found to match the clip's properties.\nClip size: %1\nFps: %2\n", size, fps));
1977             }
1978         }
1979     }
1980     return profileUpdated;
1981 }
1982
1983 QString ProjectList::getDocumentProperty(const QString &key) const
1984 {
1985     return m_doc->getDocumentProperty(key);
1986 }
1987
1988 bool ProjectList::useProxy() const
1989 {
1990     return m_doc->getDocumentProperty("enableproxy").toInt();
1991 }
1992
1993 bool ProjectList::generateProxy() const
1994 {
1995     return m_doc->getDocumentProperty("generateproxy").toInt();
1996 }
1997
1998 bool ProjectList::generateImageProxy() const
1999 {
2000     return m_doc->getDocumentProperty("generateimageproxy").toInt();
2001 }
2002
2003 void ProjectList::slotReplyGetImage(const QString &clipId, const QImage &img)
2004 {
2005     ProjectItem *item = getItemById(clipId);
2006     if (item && !img.isNull()) {
2007         QPixmap pix = QPixmap::fromImage(img);
2008         monitorItemEditing(false);
2009         item->setData(0, Qt::DecorationRole, pix);
2010         monitorItemEditing(true);
2011         QString hash = item->getClipHash();
2012         if (!hash.isEmpty()) m_doc->cacheImage(hash, img);
2013     }
2014 }
2015
2016 void ProjectList::slotReplyGetImage(const QString &clipId, const QString &name, int width, int height)
2017 {
2018     // For clips that have a generic icon (like audio clips...)
2019     ProjectItem *item = getItemById(clipId);
2020     QPixmap pix =  KIcon(name).pixmap(QSize(width, height));
2021     if (item && !pix.isNull()) {
2022         monitorItemEditing(false);
2023         item->setData(0, Qt::DecorationRole, pix);
2024         monitorItemEditing(true);
2025     }
2026 }
2027
2028 QTreeWidgetItem *ProjectList::getAnyItemById(const QString &id)
2029 {
2030     QTreeWidgetItemIterator it(m_listView);
2031     QString lookId = id;
2032     if (id.contains('#'))
2033         lookId = id.section('#', 0, 0);
2034
2035     ProjectItem *result = NULL;
2036     while (*it) {
2037         if ((*it)->type() != PROJECTCLIPTYPE) {
2038             // subitem
2039             ++it;
2040             continue;
2041         }
2042         ProjectItem *item = static_cast<ProjectItem *>(*it);
2043         if (item->clipId() == lookId) {
2044             result = item;
2045             break;
2046         }
2047         ++it;
2048     }
2049     if (result == NULL || !id.contains('#')) {
2050         return result;
2051     } else {
2052         for (int i = 0; i < result->childCount(); i++) {
2053             SubProjectItem *sub = static_cast <SubProjectItem *>(result->child(i));
2054             if (sub && sub->zone().x() == id.section('#', 1, 1).toInt())
2055                 return sub;
2056         }
2057     }
2058
2059     return NULL;
2060 }
2061
2062
2063 ProjectItem *ProjectList::getItemById(const QString &id)
2064 {
2065     ProjectItem *item;
2066     QTreeWidgetItemIterator it(m_listView);
2067     while (*it) {
2068         if ((*it)->type() != PROJECTCLIPTYPE) {
2069             // subitem or folder
2070             ++it;
2071             continue;
2072         }
2073         item = static_cast<ProjectItem *>(*it);
2074         if (item->clipId() == id)
2075             return item;
2076         ++it;
2077     }
2078     return NULL;
2079 }
2080
2081 FolderProjectItem *ProjectList::getFolderItemById(const QString &id)
2082 {
2083     FolderProjectItem *item;
2084     QTreeWidgetItemIterator it(m_listView);
2085     while (*it) {
2086         if ((*it)->type() == PROJECTFOLDERTYPE) {
2087             item = static_cast<FolderProjectItem *>(*it);
2088             if (item->clipId() == id)
2089                 return item;
2090         }
2091         ++it;
2092     }
2093     return NULL;
2094 }
2095
2096 void ProjectList::slotSelectClip(const QString &ix)
2097 {
2098     ProjectItem *clip = getItemById(ix);
2099     if (clip) {
2100         m_listView->setCurrentItem(clip);
2101         m_listView->scrollToItem(clip);
2102         m_editButton->defaultAction()->setEnabled(true);
2103         m_deleteButton->defaultAction()->setEnabled(true);
2104         m_reloadAction->setEnabled(true);
2105         m_transcodeAction->setEnabled(true);
2106         m_stabilizeAction->setEnabled(true);
2107         if (clip->clipType() == IMAGE && !KdenliveSettings::defaultimageapp().isEmpty()) {
2108             m_openAction->setIcon(KIcon(KdenliveSettings::defaultimageapp()));
2109             m_openAction->setEnabled(true);
2110         } else if (clip->clipType() == AUDIO && !KdenliveSettings::defaultaudioapp().isEmpty()) {
2111             m_openAction->setIcon(KIcon(KdenliveSettings::defaultaudioapp()));
2112             m_openAction->setEnabled(true);
2113         } else {
2114             m_openAction->setEnabled(false);
2115         }
2116     }
2117 }
2118
2119 QString ProjectList::currentClipUrl() const
2120 {
2121     ProjectItem *item;
2122     if (!m_listView->currentItem() || m_listView->currentItem()->type() == PROJECTFOLDERTYPE) return QString();
2123     if (m_listView->currentItem()->type() == PROJECTSUBCLIPTYPE) {
2124         // subitem
2125         item = static_cast <ProjectItem*>(m_listView->currentItem()->parent());
2126     } else {
2127         item = static_cast <ProjectItem*>(m_listView->currentItem());
2128     }
2129     if (item == NULL)
2130         return QString();
2131     return item->clipUrl().path();
2132 }
2133
2134 KUrl::List ProjectList::getConditionalUrls(const QString &condition) const
2135 {
2136     KUrl::List result;
2137     ProjectItem *item;
2138     QList<QTreeWidgetItem *> list = m_listView->selectedItems();
2139     for (int i = 0; i < list.count(); i++) {
2140         if (list.at(i)->type() == PROJECTFOLDERTYPE)
2141             continue;
2142         if (list.at(i)->type() == PROJECTSUBCLIPTYPE) {
2143             // subitem
2144             item = static_cast <ProjectItem*>(list.at(i)->parent());
2145         } else {
2146             item = static_cast <ProjectItem*>(list.at(i));
2147         }
2148         if (item == NULL || item->type() == COLOR || item->type() == SLIDESHOW || item->type() == TEXT)
2149             continue;
2150         DocClipBase *clip = item->referencedClip();
2151         if (!condition.isEmpty()) {
2152             if (condition.startsWith("vcodec") && !clip->hasVideoCodec(condition.section('=', 1, 1)))
2153                 continue;
2154             else if (condition.startsWith("acodec") && !clip->hasAudioCodec(condition.section('=', 1, 1)))
2155                 continue;
2156         }
2157         result.append(item->clipUrl());
2158     }
2159     return result;
2160 }
2161
2162 void ProjectList::regenerateTemplate(const QString &id)
2163 {
2164     ProjectItem *clip = getItemById(id);
2165     if (clip)
2166         regenerateTemplate(clip);
2167 }
2168
2169 void ProjectList::regenerateTemplate(ProjectItem *clip)
2170 {
2171     //TODO: remove this unused method, only force_reload is necessary
2172     clip->referencedClip()->getProducer()->set("force_reload", 1);
2173 }
2174
2175 QDomDocument ProjectList::generateTemplateXml(QString path, const QString &replaceString)
2176 {
2177     QDomDocument doc;
2178     QFile file(path);
2179     if (!file.open(QIODevice::ReadOnly)) {
2180         kWarning() << "ERROR, CANNOT READ: " << path;
2181         return doc;
2182     }
2183     if (!doc.setContent(&file)) {
2184         kWarning() << "ERROR, CANNOT READ: " << path;
2185         file.close();
2186         return doc;
2187     }
2188     file.close();
2189     QDomNodeList texts = doc.elementsByTagName("content");
2190     for (int i = 0; i < texts.count(); i++) {
2191         QString data = texts.item(i).firstChild().nodeValue();
2192         data.replace("%s", replaceString);
2193         texts.item(i).firstChild().setNodeValue(data);
2194     }
2195     return doc;
2196 }
2197
2198
2199 void ProjectList::slotAddClipCut(const QString &id, int in, int out)
2200 {
2201     ProjectItem *clip = getItemById(id);
2202     if (clip == NULL || clip->referencedClip()->hasCutZone(QPoint(in, out)))
2203         return;
2204     AddClipCutCommand *command = new AddClipCutCommand(this, id, in, out, QString(), true, false);
2205     m_commandStack->push(command);
2206 }
2207
2208 void ProjectList::addClipCut(const QString &id, int in, int out, const QString desc, bool newItem)
2209 {
2210     ProjectItem *clip = getItemById(id);
2211     if (clip) {
2212         DocClipBase *base = clip->referencedClip();
2213         base->addCutZone(in, out);
2214         monitorItemEditing(false);
2215         SubProjectItem *sub = new SubProjectItem(clip, in, out, desc);
2216         if (newItem && desc.isEmpty() && !m_listView->isColumnHidden(1)) {
2217             if (!clip->isExpanded())
2218                 clip->setExpanded(true);
2219             m_listView->scrollToItem(sub);
2220             m_listView->editItem(sub, 1);
2221         }
2222         QImage img = clip->referencedClip()->extractImage(in, (int)(sub->sizeHint(0).height()  * m_render->dar()), sub->sizeHint(0).height() - 2);
2223         sub->setData(0, Qt::DecorationRole, QPixmap::fromImage(img));
2224         QString hash = clip->getClipHash();
2225         if (!hash.isEmpty()) m_doc->cacheImage(hash + '#' + QString::number(in), img);
2226         monitorItemEditing(true);
2227     }
2228     emit projectModified();
2229 }
2230
2231 void ProjectList::removeClipCut(const QString &id, int in, int out)
2232 {
2233     ProjectItem *clip = getItemById(id);
2234     if (clip) {
2235         DocClipBase *base = clip->referencedClip();
2236         base->removeCutZone(in, out);
2237         SubProjectItem *sub = getSubItem(clip, QPoint(in, out));
2238         if (sub) {
2239             monitorItemEditing(false);
2240             delete sub;
2241             monitorItemEditing(true);
2242         }
2243     }
2244     emit projectModified();
2245 }
2246
2247 SubProjectItem *ProjectList::getSubItem(ProjectItem *clip, QPoint zone)
2248 {
2249     SubProjectItem *sub = NULL;
2250     if (clip) {
2251         for (int i = 0; i < clip->childCount(); i++) {
2252             QTreeWidgetItem *it = clip->child(i);
2253             if (it->type() == PROJECTSUBCLIPTYPE) {
2254                 sub = static_cast <SubProjectItem*>(it);
2255                 if (sub->zone() == zone)
2256                     break;
2257                 else
2258                     sub = NULL;
2259             }
2260         }
2261     }
2262     return sub;
2263 }
2264
2265 void ProjectList::slotUpdateClipCut(QPoint p)
2266 {
2267     if (!m_listView->currentItem() || m_listView->currentItem()->type() != PROJECTSUBCLIPTYPE)
2268         return;
2269     SubProjectItem *sub = static_cast <SubProjectItem*>(m_listView->currentItem());
2270     ProjectItem *item = static_cast <ProjectItem *>(sub->parent());
2271     EditClipCutCommand *command = new EditClipCutCommand(this, item->clipId(), sub->zone(), p, sub->text(1), sub->text(1), true);
2272     m_commandStack->push(command);
2273 }
2274
2275 void ProjectList::doUpdateClipCut(const QString &id, const QPoint oldzone, const QPoint zone, const QString &comment)
2276 {
2277     ProjectItem *clip = getItemById(id);
2278     SubProjectItem *sub = getSubItem(clip, oldzone);
2279     if (sub == NULL || clip == NULL)
2280         return;
2281     DocClipBase *base = clip->referencedClip();
2282     base->updateCutZone(oldzone.x(), oldzone.y(), zone.x(), zone.y(), comment);
2283     monitorItemEditing(false);
2284     sub->setZone(zone);
2285     sub->setDescription(comment);
2286     monitorItemEditing(true);
2287     emit projectModified();
2288 }
2289
2290 void ProjectList::slotForceProcessing(const QString &id)
2291 {
2292     m_render->forceProcessing(id);
2293 }
2294
2295 void ProjectList::slotAddOrUpdateSequence(const QString frameName)
2296 {
2297     QString fileName = KUrl(frameName).fileName().section('_', 0, -2);
2298     QStringList list;
2299     QString pattern = SlideshowClip::selectedPath(frameName, false, QString(), &list);
2300     int count = list.count();
2301     if (count > 1) {
2302         const QList <DocClipBase *> existing = m_doc->clipManager()->getClipByResource(pattern);
2303         if (!existing.isEmpty()) {
2304             // Sequence already exists, update
2305             QString id = existing.at(0)->getId();
2306             //ProjectItem *item = getItemById(id);
2307             QMap <QString, QString> oldprops;
2308             QMap <QString, QString> newprops;
2309             int ttl = existing.at(0)->getProperty("ttl").toInt();
2310             oldprops["out"] = existing.at(0)->getProperty("out");
2311             newprops["out"] = QString::number(ttl * count - 1);
2312             slotUpdateClipProperties(id, newprops);
2313             EditClipCommand *command = new EditClipCommand(this, id, oldprops, newprops, false);
2314             m_commandStack->push(command);
2315         } else {
2316             // Create sequence
2317             QStringList groupInfo = getGroup();
2318             m_doc->slotCreateSlideshowClipFile(fileName, pattern, count, m_timecode.reformatSeparators(KdenliveSettings::sequence_duration()),
2319                                                false, false, false,
2320                                                m_timecode.getTimecodeFromFrames(int(ceil(m_timecode.fps()))), QString(), 0,
2321                                                QString(), groupInfo.at(0), groupInfo.at(1));
2322         }
2323     } else emit displayMessage(i18n("Sequence not found"), -2);
2324 }
2325
2326 QMap <QString, QString> ProjectList::getProxies()
2327 {
2328     QMap <QString, QString> list;
2329     ProjectItem *item;
2330     QTreeWidgetItemIterator it(m_listView);
2331     while (*it) {
2332         if ((*it)->type() != PROJECTCLIPTYPE) {
2333             ++it;
2334             continue;
2335         }
2336         item = static_cast<ProjectItem *>(*it);
2337         if (item && item->referencedClip() != NULL) {
2338             if (item->hasProxy()) {
2339                 QString proxy = item->referencedClip()->getProperty("proxy");
2340                 list.insert(proxy, item->clipUrl().path());
2341             }
2342         }
2343         ++it;
2344     }
2345     return list;
2346 }
2347
2348 void ProjectList::slotCreateProxy(const QString id)
2349 {
2350     ProjectItem *item = getItemById(id);
2351     if (!item || item->isProxyRunning() || item->referencedClip()->isPlaceHolder()) return;
2352     QString path = item->referencedClip()->getProperty("proxy");
2353     if (path.isEmpty()) {
2354         setProxyStatus(item, PROXYCRASHED);
2355         return;
2356     }
2357     setProxyStatus(item, PROXYWAITING);
2358     if (m_abortProxy.contains(path)) m_abortProxy.removeAll(path);
2359     if (m_processingProxy.contains(path)) {
2360         // Proxy is already being generated
2361         return;
2362     }
2363     if (QFileInfo(path).size() > 0) {
2364         // Proxy already created
2365         setProxyStatus(item, PROXYDONE);
2366         slotGotProxy(path);
2367         return;
2368     }
2369     m_processingProxy.append(path);
2370
2371     PROXYINFO info;
2372     info.dest = path;
2373     info.src = item->clipUrl().path();
2374     info.type = item->clipType();
2375     info.exif = QString(item->referencedClip()->producerProperty("_exif_orientation")).toInt();
2376     m_proxyList.append(info);
2377     if (!m_proxyThreads.futures().isEmpty()) {
2378         // Remove inactive threads
2379         QList <QFuture<void> > futures = m_proxyThreads.futures();
2380         m_proxyThreads.clearFutures();
2381         for (int i = 0; i < futures.count(); i++)
2382             if (!futures.at(i).isFinished()) {
2383                 m_proxyThreads.addFuture(futures.at(i));
2384             }
2385     }
2386     if (m_proxyThreads.futures().isEmpty() || m_proxyThreads.futures().count() < KdenliveSettings::proxythreads()) m_proxyThreads.addFuture(QtConcurrent::run(this, &ProjectList::slotGenerateProxy));
2387 }
2388
2389 void ProjectList::slotAbortProxy(const QString id, const QString path)
2390 {
2391     QTreeWidgetItemIterator it(m_listView);
2392     ProjectItem *item = getItemById(id);
2393     if (!path.isEmpty() && m_processingProxy.contains(path)) {
2394         m_abortProxy << path;
2395         setProxyStatus(item, NOPROXY);
2396     }
2397     else {
2398         setProxyStatus(item, NOPROXY);
2399         slotGotProxy(item);
2400     }
2401 }
2402
2403 void ProjectList::slotGenerateProxy()
2404 {
2405     while (!m_proxyList.isEmpty() && !m_abortAllProxies) {
2406         emit projectModified();
2407         PROXYINFO info = m_proxyList.takeFirst();
2408         if (m_abortProxy.contains(info.dest)) {
2409             m_abortProxy.removeAll(info.dest);
2410             continue;
2411         }
2412         // Get the list of clips that will need to get progress info
2413         QTreeWidgetItemIterator it(m_listView);
2414         QList <ProjectItem *> processingItems;
2415         while (*it && !m_abortAllProxies) {
2416             if ((*it)->type() == PROJECTCLIPTYPE) {
2417                 ProjectItem *item = static_cast <ProjectItem *>(*it);
2418                 if (item->referencedClip()->getProperty("proxy") == info.dest) {
2419                     processingItems.append(item);
2420                 }
2421             }
2422             ++it;
2423         }
2424
2425         // Make sure proxy path is writable
2426         QFile file(info.dest);
2427         if (!file.open(QIODevice::WriteOnly)) {
2428             for (int i = 0; i < processingItems.count(); i++)
2429                 setProxyStatus(processingItems.at(i), PROXYCRASHED);
2430             m_processingProxy.removeAll(info.dest);
2431             continue;
2432         }
2433         file.close();
2434         QFile::remove(info.dest);
2435     
2436         for (int i = 0; i < processingItems.count(); i++)
2437             setProxyStatus(processingItems.at(i), CREATINGPROXY);
2438
2439         // Special case: playlist clips (.mlt or .kdenlive project files)
2440         if (info.type == PLAYLIST) {
2441             // change FFmpeg params to MLT format
2442             QStringList parameters;
2443             parameters << info.src;
2444             parameters << "-consumer" << "avformat:" + info.dest;
2445             QStringList params = m_doc->getDocumentProperty("proxyparams").simplified().split('-', QString::SkipEmptyParts);
2446         
2447             foreach(QString s, params) {
2448                 s = s.simplified();
2449                 if (s.count(' ') == 0) {
2450                     s.append("=1");
2451                 }
2452                 else s.replace(' ', '=');
2453                 parameters << s;
2454             }
2455         
2456             parameters.append(QString("real_time=-%1").arg(KdenliveSettings::mltthreads()));
2457
2458             //TODO: currently, when rendering an xml file through melt, the display ration is lost, so we enforce it manualy
2459             double display_ratio = KdenliveDoc::getDisplayRatio(info.src);
2460             parameters << "aspect=" + QString::number(display_ratio);
2461
2462             //kDebug()<<"TRANSCOD: "<<parameters;
2463             QProcess myProcess;
2464             myProcess.setProcessChannelMode(QProcess::MergedChannels);
2465             myProcess.start(KdenliveSettings::rendererpath(), parameters);
2466             myProcess.waitForStarted();
2467             int result = -1;
2468             int duration = 0;
2469             while (myProcess.state() != QProcess::NotRunning) {
2470                 // building proxy file
2471                 if (m_abortProxy.contains(info.dest) || m_abortAllProxies) {
2472                     myProcess.close();
2473                     myProcess.waitForFinished();
2474                     QFile::remove(info.dest);
2475                     m_abortProxy.removeAll(info.dest);
2476                     m_processingProxy.removeAll(info.dest);
2477                     for (int i = 0; i < processingItems.count(); i++)
2478                         setProxyStatus(processingItems.at(i), NOPROXY);
2479                     result = -2;
2480                 }
2481                 else {
2482                     QString log = QString(myProcess.readAll());
2483                     processLogInfo(processingItems, &duration, log);
2484                 }
2485                 myProcess.waitForFinished(500);
2486             }
2487             myProcess.waitForFinished();
2488             m_processingProxy.removeAll(info.dest);
2489             if (result == -1) result = myProcess.exitStatus();
2490             if (result == 0) {
2491                 // proxy successfully created
2492                 for (int i = 0; i < processingItems.count(); i++)
2493                     setProxyStatus(processingItems.at(i), PROXYDONE);
2494                 slotGotProxy(info.dest);
2495             }
2496             else if (result == 1) {
2497                 // Proxy process crashed
2498                 QFile::remove(info.dest);
2499                 for (int i = 0; i < processingItems.count(); i++)
2500                     setProxyStatus(processingItems.at(i), PROXYCRASHED);
2501             }
2502             continue;
2503         }
2504     
2505         if (info.type == IMAGE) {
2506             // Image proxy
2507             QImage i(info.src);
2508             if (i.isNull()) {
2509                 // Cannot load image
2510                 for (int i = 0; i < processingItems.count(); i++)
2511                     setProxyStatus(processingItems.at(i), PROXYCRASHED);
2512                 continue;
2513             }
2514             QImage proxy;
2515             // Images are scaled to profile size. 
2516             //TODO: Make it be configurable?
2517             if (i.width() > i.height()) proxy = i.scaledToWidth(m_render->frameRenderWidth());
2518             else proxy = i.scaledToHeight(m_render->renderHeight());
2519             if (info.exif > 1) {
2520                 // Rotate image according to exif data
2521                 QImage processed;
2522                 QMatrix matrix;
2523
2524                 switch ( info.exif ) {
2525                     case 2:
2526                         matrix.scale( -1, 1 );
2527                         break;
2528                     case 3:
2529                         matrix.rotate( 180 );
2530                         break;
2531                     case 4:
2532                         matrix.scale( 1, -1 );
2533                         break;
2534                     case 5:
2535                         matrix.rotate( 270 );
2536                         matrix.scale( -1, 1 );
2537                         break;
2538                     case 6:
2539                         matrix.rotate( 90 );
2540                         break;
2541                     case 7:
2542                         matrix.rotate( 90 );
2543                         matrix.scale( -1, 1 );
2544                         break;
2545                     case 8:
2546                         matrix.rotate( 270 );
2547                         break;
2548                 }
2549                 processed = proxy.transformed( matrix );
2550                 processed.save(info.dest);
2551             }
2552             else proxy.save(info.dest);
2553             for (int i = 0; i < processingItems.count(); i++)
2554                 setProxyStatus(processingItems.at(i), PROXYDONE);
2555             slotGotProxy(info.dest);
2556             m_abortProxy.removeAll(info.dest);
2557             m_processingProxy.removeAll(info.dest);
2558             continue;
2559         }
2560
2561         QStringList parameters;
2562         parameters << "-i" << info.src;
2563         QString params = m_doc->getDocumentProperty("proxyparams").simplified();
2564         foreach(QString s, params.split(' '))
2565         parameters << s;
2566
2567         // Make sure we don't block when proxy file already exists
2568         parameters << "-y";
2569         parameters << info.dest;
2570         QProcess myProcess;
2571         myProcess.setProcessChannelMode(QProcess::MergedChannels);
2572         myProcess.start("ffmpeg", parameters);
2573         myProcess.waitForStarted();
2574         int result = -1;
2575         int duration = 0;
2576    
2577         while (myProcess.state() != QProcess::NotRunning) {
2578             // building proxy file
2579             if (m_abortProxy.contains(info.dest) || m_abortAllProxies) {
2580                 myProcess.close();
2581                 myProcess.waitForFinished();
2582                 m_abortProxy.removeAll(info.dest);
2583                 m_processingProxy.removeAll(info.dest);
2584                 QFile::remove(info.dest);
2585                 if (!m_abortAllProxies) {
2586                     for (int i = 0; i < processingItems.count(); i++)
2587                         setProxyStatus(processingItems.at(i), NOPROXY);
2588                 }
2589                 else continue;
2590                 result = -2;
2591             }
2592             else {
2593                 QString log = QString(myProcess.readAll());
2594                 processLogInfo(processingItems, &duration, log);
2595             }
2596             myProcess.waitForFinished(500);
2597         }
2598         myProcess.waitForFinished();
2599         m_abortProxy.removeAll(info.dest);
2600         m_processingProxy.removeAll(info.dest);
2601         if (result == -1) {
2602             result = myProcess.exitStatus();
2603         }
2604         
2605         // FFmpeg process terminated normally, but make sure proxy clip exists
2606         if (result != -2 && QFileInfo(info.dest).size() == 0) {
2607             result = QProcess::CrashExit;
2608         }
2609
2610         if (result == QProcess::NormalExit) {
2611             // proxy successfully created
2612             for (int i = 0; i < processingItems.count(); i++)
2613                 setProxyStatus(processingItems.at(i), PROXYDONE);
2614             slotGotProxy(info.dest);
2615         }
2616         else if (result == QProcess::CrashExit) {
2617             // Proxy process crashed
2618             QFile::remove(info.dest);
2619             for (int i = 0; i < processingItems.count(); i++)
2620                 setProxyStatus(processingItems.at(i), PROXYCRASHED);
2621         }
2622     }
2623 }
2624
2625
2626 void ProjectList::processLogInfo(QList <ProjectItem *>items, int *duration, const QString &log)
2627 {
2628     int progress;
2629     if (*duration == 0) {
2630         if (log.contains("Duration:")) {
2631             QString data = log.section("Duration:", 1, 1).section(',', 0, 0).simplified();
2632             QStringList numbers = data.split(':');
2633             *duration = (int) (numbers.at(0).toInt() * 3600 + numbers.at(1).toInt() * 60 + numbers.at(2).toDouble());
2634         }
2635     }
2636     else if (log.contains("time=")) {
2637         QString time = log.section("time=", 1, 1).simplified().section(' ', 0, 0);
2638         if (time.contains(':')) {
2639             QStringList numbers = time.split(':');
2640             progress = numbers.at(0).toInt() * 3600 + numbers.at(1).toInt() * 60 + numbers.at(2).toDouble();
2641         }
2642         else progress = (int) time.toDouble();
2643         for (int i = 0; i < items.count(); i++)
2644             setProxyStatus(items.at(i), CREATINGPROXY, (int) (100.0 * progress / (*duration)));
2645     }
2646 }
2647
2648 void ProjectList::updateProxyConfig()
2649 {
2650     ProjectItem *item;
2651     QTreeWidgetItemIterator it(m_listView);
2652     QUndoCommand *command = new QUndoCommand();
2653     command->setText(i18n("Update proxy settings"));
2654     QString proxydir = m_doc->projectFolder().path( KUrl::AddTrailingSlash) + "proxy/";
2655     while (*it) {
2656         if ((*it)->type() != PROJECTCLIPTYPE) {
2657             ++it;
2658             continue;
2659         }
2660         item = static_cast<ProjectItem *>(*it);
2661         if (item == NULL) {
2662             ++it;
2663             continue;
2664         }
2665         CLIPTYPE t = item->clipType();
2666         if ((t == VIDEO || t == AV || t == UNKNOWN) && item->referencedClip() != NULL) {
2667             if  (generateProxy() && useProxy() && !item->isProxyRunning()) {
2668                 DocClipBase *clip = item->referencedClip();
2669                 if (clip->getProperty("frame_size").section('x', 0, 0).toInt() > m_doc->getDocumentProperty("proxyminsize").toInt()) {
2670                     if (clip->getProperty("proxy").isEmpty()) {
2671                         // We need to insert empty proxy in old properties so that undo will work
2672                         QMap <QString, QString> oldProps;// = clip->properties();
2673                         oldProps.insert("proxy", QString());
2674                         QMap <QString, QString> newProps;
2675                         newProps.insert("proxy", proxydir + item->referencedClip()->getClipHash() + "." + m_doc->getDocumentProperty("proxyextension"));
2676                         new EditClipCommand(this, clip->getId(), oldProps, newProps, true, command);
2677                     }
2678                 }
2679             }
2680             else if (item->hasProxy()) {
2681                 // remove proxy
2682                 QMap <QString, QString> newProps;
2683                 newProps.insert("proxy", QString());
2684                 newProps.insert("replace", "1");
2685                 // insert required duration for proxy
2686                 newProps.insert("proxy_out", item->referencedClip()->producerProperty("out"));
2687                 new EditClipCommand(this, item->clipId(), item->referencedClip()->currentProperties(newProps), newProps, true, command);
2688             }
2689         }
2690         else if (t == IMAGE && item->referencedClip() != NULL) {
2691             if  (generateImageProxy() && useProxy()) {
2692                 DocClipBase *clip = item->referencedClip();
2693                 int maxImageSize = m_doc->getDocumentProperty("proxyimageminsize").toInt();
2694                 if (clip->getProperty("frame_size").section('x', 0, 0).toInt() > maxImageSize || clip->getProperty("frame_size").section('x', 1, 1).toInt() > maxImageSize) {
2695                     if (clip->getProperty("proxy").isEmpty()) {
2696                         // We need to insert empty proxy in old properties so that undo will work
2697                         QMap <QString, QString> oldProps = clip->properties();
2698                         oldProps.insert("proxy", QString());
2699                         QMap <QString, QString> newProps;
2700                         newProps.insert("proxy", proxydir + item->referencedClip()->getClipHash() + ".png");
2701                         new EditClipCommand(this, clip->getId(), oldProps, newProps, true, command);
2702                     }
2703                 }
2704             }
2705             else if (item->hasProxy()) {
2706                 // remove proxy
2707                 QMap <QString, QString> newProps;
2708                 newProps.insert("proxy", QString());
2709                 newProps.insert("replace", "1");
2710                 new EditClipCommand(this, item->clipId(), item->referencedClip()->properties(), newProps, true, command);
2711             }
2712         }
2713         ++it;
2714     }
2715     if (command->childCount() > 0) m_doc->commandStack()->push(command);
2716     else delete command;
2717 }
2718
2719 void ProjectList::slotProxyCurrentItem(bool doProxy, ProjectItem *itemToProxy)
2720 {
2721     QList<QTreeWidgetItem *> list;
2722     if (itemToProxy == NULL) list = m_listView->selectedItems();
2723     else list << itemToProxy;
2724
2725     // expand list (folders, subclips) to get real clips
2726     QTreeWidgetItem *listItem;
2727     QList<ProjectItem *> clipList;
2728     for (int i = 0; i < list.count(); i++) {
2729         listItem = list.at(i);
2730         if (listItem->type() == PROJECTFOLDERTYPE) {
2731             for (int j = 0; j < listItem->childCount(); j++) {
2732                 QTreeWidgetItem *sub = listItem->child(j);
2733                 if (sub->type() == PROJECTCLIPTYPE) {
2734                     ProjectItem *item = static_cast <ProjectItem*>(sub);
2735                     if (!clipList.contains(item)) clipList.append(item);
2736                 }
2737             }
2738         }
2739         else if (listItem->type() == PROJECTSUBCLIPTYPE) {
2740             QTreeWidgetItem *sub = listItem->parent();
2741             ProjectItem *item = static_cast <ProjectItem*>(sub);
2742             if (!clipList.contains(item)) clipList.append(item);
2743         }
2744         else if (listItem->type() == PROJECTCLIPTYPE) {
2745             ProjectItem *item = static_cast <ProjectItem*>(listItem);
2746             if (!clipList.contains(item)) clipList.append(item);
2747         }
2748     }
2749     
2750     QUndoCommand *command = new QUndoCommand();
2751     if (doProxy) command->setText(i18np("Add proxy clip", "Add proxy clips", clipList.count()));
2752     else command->setText(i18np("Remove proxy clip", "Remove proxy clips", clipList.count()));
2753     
2754     // Make sure the proxy folder exists
2755     QString proxydir = m_doc->projectFolder().path( KUrl::AddTrailingSlash) + "proxy/";
2756     KStandardDirs::makeDir(proxydir);
2757                 
2758     QMap <QString, QString> newProps;
2759     QMap <QString, QString> oldProps;
2760     if (!doProxy) newProps.insert("proxy", "-");
2761     for (int i = 0; i < clipList.count(); i++) {
2762         ProjectItem *item = clipList.at(i);
2763         CLIPTYPE t = item->clipType();
2764         if ((t == VIDEO || t == AV || t == UNKNOWN || t == IMAGE || t == PLAYLIST) && item->referencedClip()) {
2765             if ((doProxy && item->hasProxy()) || (!doProxy && !item->hasProxy() && item->referencedClip()->getProducer() != NULL)) continue;
2766             DocClipBase *clip = item->referencedClip();
2767             if (!clip || !clip->isClean() || m_render->isProcessing(item->clipId())) {
2768                 kDebug()<<"//// TRYING TO PROXY: "<<item->clipId()<<", but it is busy";
2769                 continue;
2770             }
2771                 
2772             oldProps = clip->properties();
2773             if (doProxy) {
2774                 newProps.clear();
2775                 QString path = proxydir + clip->getClipHash() + "." + (t == IMAGE ? "png" : m_doc->getDocumentProperty("proxyextension"));
2776                 // insert required duration for proxy
2777                 newProps.insert("proxy_out", clip->producerProperty("out"));
2778                 newProps.insert("proxy", path);
2779                 // We need to insert empty proxy so that undo will work
2780                 oldProps.insert("proxy", QString());
2781             }
2782             else if (item->referencedClip()->getProducer() == NULL) {
2783                 // Force clip reload
2784                 kDebug()<<"// CLIP HAD NULL PROD------------";
2785                 newProps.insert("resource", item->referencedClip()->getProperty("resource"));
2786             }
2787             new EditClipCommand(this, item->clipId(), oldProps, newProps, true, command);
2788         }
2789     }
2790     if (command->childCount() > 0) {
2791         m_doc->commandStack()->push(command);
2792     }
2793     else delete command;
2794 }
2795
2796
2797 void ProjectList::slotDeleteProxy(const QString proxyPath)
2798 {
2799     if (proxyPath.isEmpty()) return;
2800     QUndoCommand *proxyCommand = new QUndoCommand();
2801     proxyCommand->setText(i18n("Remove Proxy"));
2802     QTreeWidgetItemIterator it(m_listView);
2803     ProjectItem *item;
2804     while (*it) {
2805         if ((*it)->type() == PROJECTCLIPTYPE) {
2806             item = static_cast <ProjectItem *>(*it);
2807             if (item->referencedClip()->getProperty("proxy") == proxyPath) {
2808                 QMap <QString, QString> props;
2809                 props.insert("proxy", QString());
2810                 new EditClipCommand(this, item->clipId(), item->referencedClip()->currentProperties(props), props, true, proxyCommand);
2811             
2812             }
2813         }
2814         ++it;
2815     }
2816     if (proxyCommand->childCount() == 0)
2817         delete proxyCommand;
2818     else
2819         m_commandStack->push(proxyCommand);
2820     QFile::remove(proxyPath);
2821 }
2822
2823 void ProjectList::setProxyStatus(ProjectItem *item, PROXYSTATUS status, int progress)
2824 {
2825     if (item == NULL || m_abortAllProxies) return;
2826     monitorItemEditing(false);
2827     item->setProxyStatus(status, progress);
2828     if (status == PROXYCRASHED) {
2829         DocClipBase *clip = item->referencedClip();
2830         if (!clip) {
2831             kDebug()<<"// PROXY CRASHED";
2832         }
2833         else if (clip->getProducer() == NULL && !clip->isPlaceHolder()) {
2834             // disable proxy and fetch real clip
2835             clip->setProperty("proxy", "-");
2836             QDomElement xml = clip->toXML();
2837             m_render->getFileProperties(xml, clip->getId(), m_listView->iconSize().height(), true);
2838         }
2839         else {
2840             // Disable proxy for this clip
2841             clip->setProperty("proxy", "-");
2842         }
2843     }
2844     monitorItemEditing(true);
2845 }
2846
2847 void ProjectList::monitorItemEditing(bool enable)
2848 {
2849     if (enable) connect(m_listView, SIGNAL(itemChanged(QTreeWidgetItem *, int)), this, SLOT(slotItemEdited(QTreeWidgetItem *, int)));     
2850     else disconnect(m_listView, SIGNAL(itemChanged(QTreeWidgetItem *, int)), this, SLOT(slotItemEdited(QTreeWidgetItem *, int)));     
2851 }
2852
2853 QStringList ProjectList::expandedFolders() const
2854 {
2855     QStringList result;
2856     FolderProjectItem *item;
2857     QTreeWidgetItemIterator it(m_listView);
2858     while (*it) {
2859         if ((*it)->type() != PROJECTFOLDERTYPE) {
2860             ++it;
2861             continue;
2862         }
2863         if ((*it)->isExpanded()) {
2864             item = static_cast<FolderProjectItem *>(*it);
2865             result.append(item->clipId());
2866         }
2867         ++it;
2868     }
2869     return result;
2870 }
2871
2872 #include "projectlist.moc"