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