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