]> git.sesse.net Git - kdenlive/blob - src/projectlist.h
Some progress on clip jobs
[kdenlive] / src / projectlist.h
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
21 #ifndef PROJECTLIST_H
22 #define PROJECTLIST_H
23
24 #include <QDomNodeList>
25 #include <QHash>
26 #include <QToolBar>
27 #include <QToolButton>
28 #include <QTreeWidget>
29 #include <QPainter>
30 #include <QStyledItemDelegate>
31 #include <QUndoStack>
32 #include <QTimer>
33 #include <QApplication>
34 #include <QFuture>
35 #include <QFutureSynchronizer>
36 #include <QListWidget>
37 #include <QTimeLine>
38 #include <QPushButton>
39
40 #include <KTreeWidgetSearchLine>
41 #include <KUrl>
42 #include <KIcon>
43
44 #ifdef NEPOMUK
45 #include <nepomuk/kratingpainter.h>
46 #include <nepomuk/resource.h>
47 #endif
48
49 #include "definitions.h"
50 #include "timecode.h"
51 #include "kdenlivesettings.h"
52 #include "folderprojectitem.h"
53 #include "subprojectitem.h"
54 #include <kdialog.h>
55
56 namespace Mlt
57 {
58 class Producer;
59 };
60
61 class ProjectItem;
62 class ProjectListView;
63 class Render;
64 class KdenliveDoc;
65 class DocClipBase;
66 class AbstractClipJob;
67
68 const int NameRole = Qt::UserRole;
69 const int DurationRole = NameRole + 1;
70 const int UsageRole = NameRole + 2;
71
72 class SmallInfoLabel: public QPushButton
73 {
74     Q_OBJECT
75 public:
76     SmallInfoLabel(QWidget *parent = 0);
77
78 private:
79     QTimeLine* m_timeLine;
80
81 public slots:
82     void slotSetJobCount(int jobCount);
83
84 private slots:
85     void slotTimeLineChanged(qreal value);
86     void slotTimeLineFinished();
87 };
88     
89 class InvalidDialog: public KDialog
90 {
91     Q_OBJECT
92 public:
93     InvalidDialog(const QString &caption, const QString &message, bool infoOnly, QWidget *parent = 0);
94     virtual ~InvalidDialog();
95     void addClip(const QString &id, const QString &path);
96     QStringList getIds() const;
97 private:
98     QListWidget *m_clipList;
99 };
100
101
102 class ItemDelegate: public QStyledItemDelegate
103 {
104 public:
105     ItemDelegate(QAbstractItemView* parent = 0): QStyledItemDelegate(parent) {
106     }
107     
108     /*void drawFocus(QPainter *, const QStyleOptionViewItem &, const QRect &) const {
109     }*/
110
111     void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const {
112         if (index.column() == 0 && !index.data(DurationRole).isNull()) {
113             QRect r1 = option.rect;
114             painter->save();
115             QStyleOptionViewItemV4 opt(option);
116             QStyle *style = opt.widget ? opt.widget->style() : QApplication::style();
117             style->drawPrimitive(QStyle::PE_PanelItemViewItem, &opt, painter, opt.widget);
118
119             if (option.state & QStyle::State_Selected) {
120                 painter->setPen(option.palette.highlightedText().color());
121             }
122             const int textMargin = style->pixelMetric(QStyle::PM_FocusFrameHMargin) + 1;
123             QPixmap pixmap = qVariantValue<QPixmap>(index.data(Qt::DecorationRole));
124             QPoint pixmapPoint(r1.left() + textMargin, r1.top() + (r1.height() - pixmap.height()) / 2);
125             painter->drawPixmap(pixmapPoint, pixmap);
126             int decoWidth = pixmap.width() + 2 * textMargin;
127
128             QFont font = painter->font();
129             font.setBold(true);
130             painter->setFont(font);
131             int mid = (int)((r1.height() / 2));
132             r1.adjust(decoWidth, 0, 0, -mid);
133             QRect r2 = option.rect;
134             r2.adjust(decoWidth, mid, 0, 0);
135             painter->drawText(r1, Qt::AlignLeft | Qt::AlignBottom, index.data().toString());
136             font.setBold(false);
137             painter->setFont(font);
138             QString subText = index.data(DurationRole).toString();
139             int usage = index.data(UsageRole).toInt();
140             if (usage != 0) subText.append(QString(" (%1)").arg(usage));
141             if (option.state & (QStyle::State_Selected)) painter->setPen(option.palette.color(QPalette::Mid));
142             QRectF bounding;
143             painter->drawText(r2, Qt::AlignLeft | Qt::AlignVCenter , subText, &bounding);
144             
145             int proxy = index.data(Qt::UserRole + 5).toInt();
146             if (proxy != 0 && proxy != JOBDONE) {
147                 QString proxyText;
148                 QColor color;
149                 if (proxy != JOBCRASHED) {
150                     // Draw proxy progress bar
151                     color = option.palette.alternateBase().color();
152                     painter->setPen(Qt::NoPen);
153                     color.setAlpha(180);
154                     painter->setBrush(QBrush(color));
155                     QRect progress(pixmapPoint.x() + 1, pixmapPoint.y() + pixmap.height() - 5, pixmap.width() - 2, 4);
156                     painter->drawRect(progress);
157                     painter->setBrush(option.palette.text());
158                     if (proxy > 0) {
159                         progress.adjust(1, 1, 0, -1);
160                         progress.setWidth((pixmap.width() - 4) * proxy / 100);
161                         painter->drawRect(progress);
162                     }
163                     else if (proxy == JOBWAITING) {
164                         // Draw kind of a pause icon
165                         progress.adjust(1, 1, 0, -1);
166                         progress.setWidth(2);
167                         painter->drawRect(progress);
168                         progress.moveLeft(progress.right() + 2);
169                         painter->drawRect(progress);
170                     }
171                 }
172                 else if (proxy == JOBCRASHED) {
173                     proxyText = i18n("Proxy crashed");
174                     QRectF txtBounding = painter->boundingRect(r2, Qt::AlignRight | Qt::AlignVCenter, " " + proxyText + " ");
175                     painter->setPen(Qt::NoPen);
176                     painter->setBrush(option.palette.highlight());
177                     painter->drawRoundedRect(txtBounding, 2, 2);
178                     painter->setPen(option.palette.highlightedText().color());
179                     painter->drawText(txtBounding, Qt::AlignHCenter | Qt::AlignVCenter , proxyText);
180                 }
181             }
182             
183             painter->restore();
184         } else if (index.column() == 2 && KdenliveSettings::activate_nepomuk()) {
185             if (index.data().toString().isEmpty()) {
186                 QStyledItemDelegate::paint(painter, option, index);
187                 return;
188             }
189             QRect r1 = option.rect;
190             if (option.state & (QStyle::State_Selected)) {
191                 painter->fillRect(r1, option.palette.highlight());
192             }
193 #ifdef NEPOMUK
194             KRatingPainter::paintRating(painter, r1, Qt::AlignCenter, index.data().toInt());
195 #endif
196         } else {
197             QStyledItemDelegate::paint(painter, option, index);
198         }
199     }
200 };
201
202 class ProjectList : public QWidget
203 {
204     Q_OBJECT
205
206 public:
207     ProjectList(QWidget *parent = 0);
208     virtual ~ProjectList();
209
210     QDomElement producersList();
211     void setRenderer(Render *projectRender);
212     void slotUpdateClipProperties(const QString &id, QMap <QString, QString> properties);
213     QByteArray headerInfo() const;
214     void setHeaderInfo(const QByteArray &state);
215     void updateProjectFormat(Timecode t);
216     void setupMenu(QMenu *addMenu, QAction *defaultAction);
217     void setupGeneratorMenu(const QHash<QString,QMenu*>& menus);
218     QString currentClipUrl() const;
219     KUrl::List getConditionalUrls(const QString &condition) const;
220     QDomDocument generateTemplateXml(QString data, const QString &replaceString);
221     void cleanup();
222     void trashUnusedClips();
223     QList <DocClipBase*> documentClipList() const;
224     void addClipCut(const QString &id, int in, int out, const QString desc, bool newItem);
225     void removeClipCut(const QString &id, int in, int out);
226     void focusTree() const;
227     SubProjectItem *getSubItem(ProjectItem *clip, QPoint zone);
228     void doUpdateClipCut(const QString &id, const QPoint oldzone, const QPoint zone, const QString &comment);
229     bool hasMissingClips();
230     void deleteProjectFolder(QMap <QString, QString> map);
231     void selectItemById(const QString &clipId);
232
233     /** @brief Returns a string list of all supported mime extensions. */
234     static QString getExtensions();
235     /** @brief Returns a list of urls containing original and proxy urls. */
236     QMap <QString, QString> getProxies();
237     /** @brief Enable / disable proxies. */
238     void updateProxyConfig();
239     /** @brief Get a property from the document. */
240     QString getDocumentProperty(const QString &key) const;
241     
242     /** @brief Does this project allow proxies. */
243     bool useProxy() const;
244     /** @brief Should we automatically create proxy clips for newly added clips. */
245     bool generateProxy() const;
246     /** @brief Should we automatically create proxy clips for newly added clips. */
247     bool generateImageProxy() const;
248     /** @brief Returns a list of the expanded folder ids. */
249     QStringList expandedFolders() const;
250     /** @brief Deselect all clips in project tree. */
251     void clearSelection();
252     /** @brief Print required overlays over clip thumb (proxy, stabilized,...). */
253     void processThumbOverlays(ProjectItem *item, QPixmap &pix);
254
255 public slots:
256     void setDocument(KdenliveDoc *doc);
257     void updateAllClips(bool displayRatioChanged, bool fpsChanged, QStringList brokenClips);
258     void slotReplyGetImage(const QString &clipId, const QImage &img);
259     void slotReplyGetImage(const QString &clipId, const QString &name, int width, int height);
260     void slotReplyGetFileProperties(const QString &clipId, Mlt::Producer *producer, const stringMap &properties, const stringMap &metadata, bool replace);
261     void slotAddClip(DocClipBase *clip, bool getProperties);
262     void slotDeleteClip(const QString &clipId);
263     void slotUpdateClip(const QString &id);
264     void slotRefreshClipThumbnail(const QString &clipId, bool update = true);
265     void slotRefreshClipThumbnail(QTreeWidgetItem *item, bool update = true);
266     void slotRemoveInvalidClip(const QString &id, bool replace);
267     void slotRemoveInvalidProxy(const QString &id, bool durationError);
268     void slotSelectClip(const QString &ix);
269
270     /** @brief Prepares removing the selected items. */
271     void slotRemoveClip();
272     void slotAddClip(const QList <QUrl> givenList = QList <QUrl> (), const QString &groupName = QString(), const QString &groupId = QString());
273
274     /** @brief Adds, edits or deletes a folder item.
275     *
276     * This is triggered by AddFolderCommand and EditFolderCommand. */
277     void slotAddFolder(const QString foldername, const QString &clipId, bool remove, bool edit = false);
278     void slotResetProjectList();
279     void slotOpenClip();
280     void slotEditClip();
281     void slotReloadClip(const QString &id = QString());
282
283     /** @brief Shows dialog for setting up a color clip. */
284     void slotAddColorClip();
285     void regenerateTemplate(const QString &id);
286     void slotUpdateClipCut(QPoint p);
287     void slotAddClipCut(const QString &id, int in, int out);
288     void slotForceProcessing(const QString &id);
289     /** @brief Remove all instances of a proxy and delete the file. */
290     void slotDeleteProxy(const QString proxyPath);
291
292 private:
293     ProjectListView *m_listView;
294     Render *m_render;
295     Timecode m_timecode;
296     double m_fps;
297     QMenu *m_menu;
298     QFuture<void> m_queueRunner;
299     QUndoStack *m_commandStack;
300     ProjectItem *getItemById(const QString &id);
301     QTreeWidgetItem *getAnyItemById(const QString &id);
302     FolderProjectItem *getFolderItemById(const QString &id);
303     QAction *m_openAction;
304     QAction *m_reloadAction;
305     QMenu *m_transcodeAction;
306     QMenu *m_stabilizeAction;
307     KdenliveDoc *m_doc;
308     ItemDelegate *m_listViewDelegate;
309     /** @brief False if we have not yet finished opening the document. */
310     bool m_refreshed;
311     /** @brief False if we have not yet finished checking all project tree thumbs. */
312     bool m_allClipsProcessed;
313     QToolButton *m_addButton;
314     QToolButton *m_deleteButton;
315     QToolButton *m_editButton;
316     //QMap <QString, QDomElement> m_infoQueue;
317     QMap <QString, QDomElement> m_producerQueue;
318     QList <QString> m_thumbnailQueue;
319     QAction *m_proxyAction;
320     QStringList m_processingClips;
321     /** @brief Holds a list of proxy urls that should be aborted. */
322     QStringList m_abortProxy;
323     /** @brief Holds a list of proxy urls that are currently being created. */
324     QStringList m_processingProxy;
325     QMutex m_mutex;
326     bool m_abortAllJobs;
327     /** @brief We are cleaning up the project list, so stop processing signals. */
328     bool m_closing;
329     QList <AbstractClipJob *> m_jobList;
330     QFutureSynchronizer<void> m_proxyThreads;
331     InvalidDialog *m_invalidClipDialog;
332     QMenu *m_jobsMenu;
333     SmallInfoLabel *m_infoLabel;
334     
335     void requestClipThumbnail(const QString id);
336
337     /** @brief Creates an EditFolderCommand to change the name of an folder item. */
338     void editFolder(const QString folderName, const QString oldfolderName, const QString &clipId);
339
340     /** @brief Gets the selected folder (or the folder of the selected item). */
341     QStringList getGroup() const;
342     void regenerateTemplate(ProjectItem *clip);
343     void editClipSelection(QList<QTreeWidgetItem *> list);
344
345     /** @brief Enables and disables transcode actions based on the selected clip's type. */
346     void adjustTranscodeActions(ProjectItem *clip) const;
347     /** @brief Enables and disables stabilize actions based on the selected clip's type. */
348     void adjustStabilizeActions(ProjectItem *clip) const;
349     /** @brief Enables and disables proxy action based on the selected clip. */
350     void adjustProxyActions(ProjectItem *clip) const;
351
352     /** @brief Sets the buttons enabled/disabled according to selected item. */
353     void updateButtons() const;
354
355     /** @brief Set the Proxy status on a clip. 
356      * @param item The clip item to set status
357      * @param status The proxy status (see definitions.h) */
358     void setProxyStatus(ProjectItem *item, CLIPJOBSTATUS status, int progress = 0);
359     /** @brief Process ffmpeg output to find out process progress. */
360     void processLogInfo(QList <ProjectItem *>items, int progress);
361     void monitorItemEditing(bool enable);
362     /** @brief Get cached thumbnail for a project's clip or create it if no cache. */
363     void getCachedThumbnail(ProjectItem *item);
364     void getCachedThumbnail(SubProjectItem *item);
365     /** @brief The clip is about to be reloaded, cancel thumbnail requests. */
366     void resetThumbsProducer(DocClipBase *clip);
367
368 private slots:
369     void slotClipSelected();
370     void slotAddSlideshowClip();
371     void slotAddTitleClip();
372     void slotAddTitleTemplateClip();
373
374     /** @brief Shows the context menu after enabling and disabling actions based on the item's type.
375     * @param pos The position where the menu should pop up
376     * @param item The item for which the checks should be done */
377     void slotContextMenu(const QPoint &pos, QTreeWidgetItem *item);
378
379     /** @brief Creates an AddFolderCommand. */
380     void slotAddFolder();
381
382     /** @brief This is triggered when a clip description has been modified. */
383     void slotItemEdited(QTreeWidgetItem *item, int column);
384     void slotUpdateClipProperties(ProjectItem *item, QMap <QString, QString> properties);
385     void slotProcessNextThumbnail();
386     void slotCheckForEmptyQueue();
387     void slotPauseMonitor();
388     /** A clip was modified externally, change icon so that user knows it */
389     void slotModifiedClip(const QString &id);
390     void slotMissingClip(const QString &id);
391     void slotAvailableClip(const QString &id);
392     /** @brief Try to find a matching profile for given item. */
393     bool adjustProjectProfileToItem(ProjectItem *item = NULL);
394     /** @brief Add a sequence from the stopmotion widget. */
395     void slotAddOrUpdateSequence(const QString frameName);
396     /** @brief A proxy clip was created, update display. */
397     void slotGotProxy(const QString &proxyPath);
398     void slotGotProxy(ProjectItem *item);
399     /** @brief Enable / disable proxy for current clip. */
400     void slotProxyCurrentItem(bool doProxy, ProjectItem *itemToProxy = NULL);
401     /** @brief Put clip in the proxy waiting list. */
402     void slotCreateProxy(const QString id);
403     /** @brief Stop creation of this clip's proxy. */
404     void slotAbortProxy(const QString id, const QString path);
405     /** @brief Start creation of proxy clip. */
406     void slotGenerateProxy();
407     /** @brief Discard running and pending clip jobs. */
408     void slotCancelJobs();
409     /** @brief Discard a running clip jobs. */
410     void slotCancelRunningJob(const QString id, stringMap);
411
412 signals:
413     void clipSelected(DocClipBase *, QPoint zone = QPoint(), bool forceUpdate = false);
414     void receivedClipDuration(const QString &);
415     void showClipProperties(DocClipBase *);
416     void showClipProperties(QList <DocClipBase *>, QMap<QString, QString> commonproperties);
417     void projectModified();
418     void loadingIsOver();
419     void displayMessage(const QString, int progress);
420     void clipNameChanged(const QString, const QString);
421     void clipNeedsReload(const QString&);
422     /** @brief A property affecting display was changed, so we need to update monitors and thumbnails
423      *  @param id: The clip's id string
424      *  @param resetThumbs Should we recreate the timeline thumbnails. */
425     void refreshClip(const QString &id, bool resetThumbs);
426     void updateRenderStatus();
427     void deleteProjectClips(QStringList ids, QMap <QString, QString> folderids);
428     void findInTimeline(const QString &clipId);
429     /** @brief Request a profile change for current document. */
430     void updateProfile(const QString &);
431     void processNextThumbnail();
432     /** @brief Activate the clip monitor. */
433     void raiseClipMonitor();
434     /** @brief Set number of running jobs. */
435     void jobCount(int);
436     void cancelRunningJob(const QString, stringMap);
437 };
438
439 #endif
440