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