]> git.sesse.net Git - kdenlive/blob - src/projectlist.h
Fix crash in scopes: http://kdenlive.org/mantis/view.php?id=2779
[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 QString&,const QString&);
334     
335
336 private:
337     ProjectListView *m_listView;
338     Render *m_render;
339     Timecode m_timecode;
340     double m_fps;
341     QMenu *m_menu;
342     QFuture<void> m_queueRunner;
343     QUndoStack *m_commandStack;
344     ProjectItem *getItemById(const QString &id);
345     QTreeWidgetItem *getAnyItemById(const QString &id);
346     FolderProjectItem *getFolderItemById(const QString &id);
347     FolderProjectItem *getFolderItemByName(const QString &name);
348     QAction *m_openAction;
349     QAction *m_reloadAction;
350     QAction *m_discardCurrentClipJobs;
351     QMenu *m_extractAudioAction;
352     QMenu *m_transcodeAction;
353     QMenu *m_stabilizeAction;
354     KdenliveDoc *m_doc;
355     ItemDelegate *m_listViewDelegate;
356     /** @brief False if we have not yet finished opening the document. */
357     bool m_refreshed;
358     /** @brief False if we have not yet finished checking all project tree thumbs. */
359     bool m_allClipsProcessed;
360     QToolButton *m_addButton;
361     QToolButton *m_deleteButton;
362     QToolButton *m_editButton;
363     //QMap <QString, QDomElement> m_infoQueue;
364     QMap <QString, QDomElement> m_producerQueue;
365     QList <QString> m_thumbnailQueue;
366     QAction *m_proxyAction;
367     QMutex m_jobMutex;
368     bool m_abortAllJobs;
369     /** @brief We are cleaning up the project list, so stop processing signals. */
370     bool m_closing;
371     QList <AbstractClipJob *> m_jobList;
372     QFutureSynchronizer<void> m_jobThreads;
373     InvalidDialog *m_invalidClipDialog;
374     QMenu *m_jobsMenu;
375     SmallInfoLabel *m_infoLabel;
376     /** @brief A list of strings containing the last error logs for clip jobs. */
377     QStringList m_errorLog;
378
379 #if KDE_IS_VERSION(4,7,0)
380     MyMessageWidget *m_infoMessage;
381     /** @brief The action that will trigger the log dialog. */
382     QAction *m_logAction;
383 #endif
384     
385     void requestClipThumbnail(const QString id);
386
387     /** @brief Creates an EditFolderCommand to change the name of an folder item. */
388     void editFolder(const QString folderName, const QString oldfolderName, const QString &clipId);
389
390     /** @brief Gets the selected folder (or the folder of the selected item). */
391     QStringList getGroup() const;
392     void regenerateTemplate(ProjectItem *clip);
393     void editClipSelection(QList<QTreeWidgetItem *> list);
394
395     /** @brief Enables and disables transcode actions based on the selected clip's type. */
396     void adjustTranscodeActions(ProjectItem *clip) const;
397     /** @brief Enables and disables stabilize actions based on the selected clip's type. */
398     void adjustStabilizeActions(ProjectItem *clip) const;
399     /** @brief Enables and disables proxy action based on the selected clip. */
400     void adjustProxyActions(ProjectItem *clip) const;
401
402     /** @brief Sets the buttons enabled/disabled according to selected item. */
403     void updateButtons() const;
404
405     /** @brief Set the Proxy status on a clip. 
406      * @param item The clip item to set status
407      * @param jobType The job type 
408      * @param status The job status (see definitions.h)
409      * @param progress The job progress (in percents)
410      * @param statusMessage The job info message */
411     void setJobStatus(ProjectItem *item, JOBTYPE jobType, CLIPJOBSTATUS status, int progress = 0, const QString &statusMessage = QString());
412     void monitorItemEditing(bool enable);
413     /** @brief Get cached thumbnail for a project's clip or create it if no cache. */
414     void getCachedThumbnail(ProjectItem *item);
415     void getCachedThumbnail(SubProjectItem *item);
416     /** @brief The clip is about to be reloaded, cancel thumbnail requests. */
417     void resetThumbsProducer(DocClipBase *clip);
418     /** @brief Check if a clip has a running or pending job process. */
419     bool hasPendingJob(ProjectItem *item, JOBTYPE type);
420     /** @brief Delete pending jobs for a clip. */
421     void deleteJobsForClip(const QString &clipId);
422     /** @brief Discard specific job type for a clip. */
423     void discardJobs(const QString &id, JOBTYPE type = NOJOBTYPE);
424     /** @brief Get the list of job names for current clip. */
425     QStringList getPendingJobs(const QString &id);
426     /** @brief Start an MLT process job. */
427     void processClipJob(QStringList ids, const QString&destination, bool autoAdd, QStringList jobParams, const QString &description);
428
429 private slots:
430     void slotClipSelected();
431     void slotAddSlideshowClip();
432     void slotAddTitleClip();
433     void slotAddTitleTemplateClip();
434
435     /** @brief Shows the context menu after enabling and disabling actions based on the item's type.
436     * @param pos The position where the menu should pop up
437     * @param item The item for which the checks should be done */
438     void slotContextMenu(const QPoint &pos, QTreeWidgetItem *item);
439
440     /** @brief Creates an AddFolderCommand. */
441     void slotAddFolder(const QString &name = QString());
442
443     /** @brief This is triggered when a clip description has been modified. */
444     void slotItemEdited(QTreeWidgetItem *item, int column);
445     void slotUpdateClipProperties(ProjectItem *item, QMap <QString, QString> properties);
446     void slotProcessNextThumbnail();
447     void slotCheckForEmptyQueue();
448     void slotPauseMonitor();
449     /** A clip was modified externally, change icon so that user knows it */
450     void slotModifiedClip(const QString &id);
451     void slotMissingClip(const QString &id);
452     void slotAvailableClip(const QString &id);
453     /** @brief Try to find a matching profile for given item. */
454     bool adjustProjectProfileToItem(ProjectItem *item = NULL);
455     /** @brief Add a sequence from the stopmotion widget. */
456     void slotAddOrUpdateSequence(const QString frameName);
457     /** @brief A proxy clip was created, update display. */
458     void slotGotProxy(const QString &proxyPath);
459     void slotGotProxy(ProjectItem *item);
460     /** @brief Enable / disable proxy for current clip. */
461     void slotProxyCurrentItem(bool doProxy, ProjectItem *itemToProxy = NULL);
462     /** @brief Put clip in the proxy waiting list. */
463     void slotCreateProxy(const QString id);
464     /** @brief Stop creation of this clip's proxy. */
465     void slotAbortProxy(const QString id, const QString path);
466     /** @brief Start creation of clip jobs. */
467     void slotProcessJobs();
468     /** @brief Discard running and pending clip jobs. */
469     void slotCancelJobs();
470     /** @brief Discard a running clip jobs. */
471     void slotCancelRunningJob(const QString id, stringMap);
472     /** @brief Update a clip's job status. */
473     void slotProcessLog(const QString, int progress, int, const QString = QString());
474     /** @brief A clip job crashed, inform user. */
475     void slotUpdateJobStatus(const QString id, int type, int status, const QString label, const QString actionName, const QString details);
476     void slotUpdateJobStatus(ProjectItem *item, int type, int status, const QString &label, const QString &actionName = QString(), const QString details = QString());
477     /** @brief Display error log for last failed job. */
478     void slotShowJobLog();
479     /** @brief A proxy clip is ready. */
480     void slotGotProxyForId(const QString);
481     /** @brief Check if it is necessary to start a job thread. */
482     void slotCheckJobProcess();
483     /** @brief Fill the jobs menu with current clip's jobs. */
484     void slotPrepareJobsMenu();
485     /** @brief Discard all jobs for current clip. */
486     void slotDiscardClipJobs();
487     /** @brief Make sure current clip is visible in project tree. */
488     void slotCheckScrolling();
489     /** @brief Reset all text and log data from info message widget. */
490     void slotResetInfoMessage();
491     /** @brief close warning info passive popup. */
492     void slotClosePopup();
493
494 signals:
495     void clipSelected(DocClipBase *, QPoint zone = QPoint(), bool forceUpdate = false);
496     void receivedClipDuration(const QString &);
497     void showClipProperties(DocClipBase *);
498     void showClipProperties(QList <DocClipBase *>, QMap<QString, QString> commonproperties);
499     void projectModified();
500     void loadingIsOver();
501     void displayMessage(const QString, int progress);
502     void clipNameChanged(const QString, const QString);
503     void clipNeedsReload(const QString&);
504     /** @brief A property affecting display was changed, so we need to update monitors and thumbnails
505      *  @param id: The clip's id string
506      *  @param resetThumbs Should we recreate the timeline thumbnails. */
507     void refreshClip(const QString &id, bool resetThumbs);
508     void updateRenderStatus();
509     void deleteProjectClips(QStringList ids, QMap <QString, QString> folderids);
510     void findInTimeline(const QString &clipId);
511     /** @brief Request a profile change for current document. */
512     void updateProfile(const QString &);
513     void processNextThumbnail();
514     /** @brief Activate the clip monitor. */
515     void raiseClipMonitor();
516     /** @brief Set number of running jobs. */
517     void jobCount(int);
518     void cancelRunningJob(const QString, stringMap);
519     void processLog(const QString, int , int, const QString = QString());
520     void addClip(const QString, const QString &, const QString &);
521     void updateJobStatus(const QString, int, int, const QString label = QString(), const QString actionName = QString(), const QString details = QString());
522     void gotProxy(const QString);
523     void checkJobProcess();
524     /** @brief A Filter Job produced results, send them back to the clip. */
525     void gotFilterJobResults(const QString &id, int startPos, int track, const QString &filterName, stringMap params);
526     void pauseMonitor();
527 };
528
529 #endif
530
531