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