]> git.sesse.net Git - kdenlive/blob - src/projectlist.h
Display proxy creation progress in project tree
[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 <QToolBar>
26 #include <QToolButton>
27 #include <QTreeWidget>
28 #include <QPainter>
29 #include <QStyledItemDelegate>
30 #include <QUndoStack>
31 #include <QTimer>
32 #include <QApplication>
33 #include <QFuture>
34 #include <QFutureSynchronizer>
35
36 #include <KTreeWidgetSearchLine>
37 #include <KUrl>
38 #include <KIcon>
39
40 #ifdef NEPOMUK
41 #include <nepomuk/kratingpainter.h>
42 #include <nepomuk/resource.h>
43 #endif
44
45 #include "definitions.h"
46 #include "timecode.h"
47 #include "kdenlivesettings.h"
48 #include "folderprojectitem.h"
49 #include "subprojectitem.h"
50
51 namespace Mlt
52 {
53 class Producer;
54 };
55
56 struct PROXYINFO {
57     QString dest;
58     QString src;
59     CLIPTYPE type;
60     int exif;
61 };
62
63 class ProjectItem;
64 class ProjectListView;
65 class Render;
66 class KdenliveDoc;
67 class DocClipBase;
68
69 const int NameRole = Qt::UserRole;
70 const int DurationRole = NameRole + 1;
71 const int UsageRole = NameRole + 2;
72
73 class ItemDelegate: public QStyledItemDelegate
74 {
75 public:
76     ItemDelegate(QAbstractItemView* parent = 0): QStyledItemDelegate(parent) {
77     }
78
79     /*void drawFocus(QPainter *, const QStyleOptionViewItem &, const QRect &) const {
80     }*/
81
82     void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const {
83         if (index.column() == 0 && !index.data(DurationRole).isNull()) {
84             QRect r1 = option.rect;
85             painter->save();
86             QStyleOptionViewItemV4 opt(option);
87             QStyle *style = opt.widget ? opt.widget->style() : QApplication::style();
88             style->drawPrimitive(QStyle::PE_PanelItemViewItem, &opt, painter, opt.widget);
89
90             if (option.state & QStyle::State_Selected) {
91                 painter->setPen(option.palette.highlightedText().color());
92             }
93             const int textMargin = style->pixelMetric(QStyle::PM_FocusFrameHMargin) + 1;
94             QPixmap pixmap = qVariantValue<QPixmap>(index.data(Qt::DecorationRole));
95             painter->drawPixmap(r1.left() + textMargin, r1.top() + (r1.height() - pixmap.height()) / 2, pixmap);
96             int decoWidth = pixmap.width() + 2 * textMargin;
97
98             QFont font = painter->font();
99             font.setBold(true);
100             painter->setFont(font);
101             int mid = (int)((r1.height() / 2));
102             r1.adjust(decoWidth, 0, 0, -mid);
103             QRect r2 = option.rect;
104             r2.adjust(decoWidth, mid, 0, 0);
105             painter->drawText(r1, Qt::AlignLeft | Qt::AlignBottom , index.data().toString());
106             font.setBold(false);
107             painter->setFont(font);
108             QString subText = index.data(DurationRole).toString();
109             int usage = index.data(UsageRole).toInt();
110             if (usage != 0) subText.append(QString(" (%1)").arg(usage));
111             if (option.state & (QStyle::State_Selected)) painter->setPen(option.palette.color(QPalette::Mid));
112             QRectF bounding;
113             painter->drawText(r2, Qt::AlignLeft | Qt::AlignVCenter , subText, &bounding);
114             
115             int proxy = index.data(Qt::UserRole + 5).toInt();
116             if (proxy != 0) {
117                 QRectF txtBounding;
118                 QString proxyText;
119                 QBrush brush;
120                 QColor color;
121                 if (proxy > 0) {
122                     proxyText = QString::number(proxy) + "% ";
123                     proxyText.append(i18n("Generating proxy ..."));
124                     brush = option.palette.highlight();
125                     color = option.palette.color(QPalette::HighlightedText);
126                     
127                 }
128                 else if (proxy == PROXYDONE) {
129                     proxyText = i18n("Proxy");
130                     brush = option.palette.mid();
131                     color = option.palette.color(QPalette::WindowText);
132                 }
133                 else {
134                     switch (proxy)  {
135                         case CREATINGPROXY:
136                             proxyText = i18n("Generating proxy ...");
137                             break;
138                         case PROXYWAITING:
139                             proxyText = i18n("Waiting proxy ...");
140                             break;
141                         case PROXYCRASHED:
142                         default:
143                             proxyText = i18n("Proxy crashed");
144                     }
145                     brush = option.palette.highlight();
146                     color = option.palette.color(QPalette::HighlightedText);
147                 }
148                
149                 txtBounding = painter->boundingRect(r2, Qt::AlignRight | Qt::AlignVCenter, " " + proxyText + " ");
150                 painter->setPen(Qt::NoPen);
151                 painter->setBrush(brush);
152                 painter->drawRoundedRect(txtBounding, 2, 2);
153                 painter->setPen(option.palette.highlightedText().color());
154                 painter->drawText(txtBounding, Qt::AlignHCenter | Qt::AlignVCenter , proxyText);
155             }
156             
157             painter->restore();
158         } else if (index.column() == 2 && KdenliveSettings::activate_nepomuk()) {
159             if (index.data().toString().isEmpty()) {
160                 QStyledItemDelegate::paint(painter, option, index);
161                 return;
162             }
163             QRect r1 = option.rect;
164             if (option.state & (QStyle::State_Selected)) {
165                 painter->fillRect(r1, option.palette.highlight());
166             }
167 #ifdef NEPOMUK
168             KRatingPainter::paintRating(painter, r1, Qt::AlignCenter, index.data().toInt());
169 #endif
170         } else {
171             QStyledItemDelegate::paint(painter, option, index);
172         }
173     }
174 };
175
176 class ProjectList : public QWidget
177 {
178     Q_OBJECT
179
180 public:
181     ProjectList(QWidget *parent = 0);
182     virtual ~ProjectList();
183
184     QDomElement producersList();
185     void setRenderer(Render *projectRender);
186     void slotUpdateClipProperties(const QString &id, QMap <QString, QString> properties);
187     QByteArray headerInfo() const;
188     void setHeaderInfo(const QByteArray &state);
189     void updateProjectFormat(Timecode t);
190     void setupMenu(QMenu *addMenu, QAction *defaultAction);
191     void setupGeneratorMenu(QMenu *addMenu, QMenu *transcodeMenu, QMenu *inTimelineMenu);
192     QString currentClipUrl() const;
193     KUrl::List getConditionalUrls(const QString &condition) const;
194     QDomDocument generateTemplateXml(QString data, const QString &replaceString);
195     void cleanup();
196     void trashUnusedClips();
197     QList <DocClipBase*> documentClipList() const;
198     void addClipCut(const QString &id, int in, int out, const QString desc, bool newItem);
199     void removeClipCut(const QString &id, int in, int out);
200     void focusTree() const;
201     SubProjectItem *getSubItem(ProjectItem *clip, QPoint zone);
202     void doUpdateClipCut(const QString &id, const QPoint oldzone, const QPoint zone, const QString &comment);
203     bool hasMissingClips();
204     void deleteProjectFolder(QMap <QString, QString> map);
205     void selectItemById(const QString &clipId);
206
207     /** @brief Returns a string list of all supported mime extensions. */
208     static QString getExtensions();
209     /** @brief Returns a list of urls containing original and proxy urls. */
210     QMap <QString, QString> getProxies();
211     /** @brief Enable / disable proxies. */
212     void updateProxyConfig();
213     /** @brief Get a property from the document. */
214     QString getDocumentProperty(const QString &key) const;
215     
216     /** @brief Does this project allow proxies. */
217     bool useProxy() const;
218     /** @brief Should we automatically create proxy clips for newly added clips. */
219     bool generateProxy() const;
220     /** @brief Should we automatically create proxy clips for newly added clips. */
221     bool generateImageProxy() const;
222     /** @brief Returns a list of the expanded folder ids. */
223     QStringList expandedFolders() const;
224
225 public slots:
226     void setDocument(KdenliveDoc *doc);
227     void updateAllClips(bool displayRatioChanged, bool fpsChanged);
228     void slotReplyGetImage(const QString &clipId, const QPixmap &pix);
229     void slotReplyGetFileProperties(const QString &clipId, Mlt::Producer *producer, const QMap < QString, QString > &properties, const QMap < QString, QString > &metadata, bool replace, bool selectClip);
230     void slotAddClip(DocClipBase *clip, bool getProperties);
231     void slotDeleteClip(const QString &clipId);
232     void slotUpdateClip(const QString &id);
233     void slotRefreshClipThumbnail(const QString &clipId, bool update = true);
234     void slotRefreshClipThumbnail(QTreeWidgetItem *item, bool update = true);
235     void slotRemoveInvalidClip(const QString &id, bool replace);
236     void slotRemoveInvalidProxy(const QString &id, bool durationError);
237     void slotSelectClip(const QString &ix);
238
239     /** @brief Prepares removing the selected items. */
240     void slotRemoveClip();
241     void slotAddClip(const QList <QUrl> givenList = QList <QUrl> (), const QString &groupName = QString(), const QString &groupId = QString());
242
243     /** @brief Adds, edits or deletes a folder item.
244     *
245     * This is triggered by AddFolderCommand and EditFolderCommand. */
246     void slotAddFolder(const QString foldername, const QString &clipId, bool remove, bool edit = false);
247     void slotResetProjectList();
248     void slotOpenClip();
249     void slotEditClip();
250     void slotReloadClip(const QString &id = QString());
251
252     /** @brief Shows dialog for setting up a color clip. */
253     void slotAddColorClip();
254     void regenerateTemplate(const QString &id);
255     void slotUpdateClipCut(QPoint p);
256     void slotAddClipCut(const QString &id, int in, int out);
257     void slotForceProcessing(const QString &id);
258     /** @brief Remove all instances of a proxy and delete the file. */
259     void slotDeleteProxy(const QString proxyPath);
260
261 private:
262     ProjectListView *m_listView;
263     Render *m_render;
264     Timecode m_timecode;
265     double m_fps;
266     QMenu *m_menu;
267     QFuture<void> m_queueRunner;
268     QUndoStack *m_commandStack;
269     ProjectItem *getItemById(const QString &id);
270     QTreeWidgetItem *getAnyItemById(const QString &id);
271     FolderProjectItem *getFolderItemById(const QString &id);
272     QAction *m_openAction;
273     QAction *m_reloadAction;
274     QMenu *m_transcodeAction;
275     KdenliveDoc *m_doc;
276     ItemDelegate *m_listViewDelegate;
277     /** @brief False if we have not yet finished opening the document. */
278     bool m_refreshed;
279     QToolButton *m_addButton;
280     QToolButton *m_deleteButton;
281     QToolButton *m_editButton;
282     QMap <QString, QDomElement> m_infoQueue;
283     QMap <QString, QDomElement> m_producerQueue;
284     QList <QString> m_thumbnailQueue;
285     QAction *m_proxyAction;
286     QStringList m_processingClips;
287     /** @brief Holds a list of proxy urls that should be aborted. */
288     QStringList m_abortProxy;
289     /** @brief Holds a list of proxy urls that are currently being created. */
290     QStringList m_processingProxy;
291     QMutex m_mutex;
292     bool m_abortAllProxies;
293     QList <PROXYINFO> m_proxyList;
294     QFutureSynchronizer<void> m_proxyThreads;
295     
296     void requestClipThumbnail(const QString id);
297
298     /** @brief Creates an EditFolderCommand to change the name of an folder item. */
299     void editFolder(const QString folderName, const QString oldfolderName, const QString &clipId);
300
301     /** @brief Gets the selected folder (or the folder of the selected item). */
302     QStringList getGroup() const;
303     void regenerateTemplate(ProjectItem *clip);
304     void editClipSelection(QList<QTreeWidgetItem *> list);
305
306     /** @brief Enables and disables transcode actions based on the selected clip's type. */
307     void adjustTranscodeActions(ProjectItem *clip) const;
308     /** @brief Enables and disables proxy action based on the selected clip. */
309     void adjustProxyActions(ProjectItem *clip) const;
310
311     /** @brief Sets the buttons enabled/disabled according to selected item. */
312     void updateButtons() const;
313
314     /** @brief Set the Proxy status on a clip. 
315      * @param item The clip item to set status
316      * @param status The proxy status (see definitions.h) */
317     void setProxyStatus(const QString proxyPath, PROXYSTATUS status, int progress = 0);
318     void setProxyStatus(ProjectItem *item, PROXYSTATUS status, int progress = 0);
319     /** @brief Process ffmpeg output to find out process progress. */
320     void processLogInfo(const QString &path, int *duration, const QString &log);
321     void monitorItemEditing(bool enable);
322
323 private slots:
324     void slotClipSelected();
325     void slotAddSlideshowClip();
326     void slotAddTitleClip();
327     void slotAddTitleTemplateClip();
328
329     /** @brief Shows the context menu after enabling and disabling actions based on the item's type.
330     * @param pos The position where the menu should pop up
331     * @param item The item for which the checks should be done */
332     void slotContextMenu(const QPoint &pos, QTreeWidgetItem *item);
333
334     /** @brief Creates an AddFolderCommand. */
335     void slotAddFolder();
336
337     /** @brief This is triggered when a clip description has been modified. */
338     void slotItemEdited(QTreeWidgetItem *item, int column);
339     void slotUpdateClipProperties(ProjectItem *item, QMap <QString, QString> properties);
340     void slotProcessNextClipInQueue();
341     void slotProcessNextThumbnail();
342     void slotCheckForEmptyQueue();
343     void slotPauseMonitor();
344     /** A clip was modified externally, change icon so that user knows it */
345     void slotModifiedClip(const QString &id);
346     void slotMissingClip(const QString &id);
347     void slotAvailableClip(const QString &id);
348     /** @brief Try to find a matching profile for given item. */
349     bool adjustProjectProfileToItem(ProjectItem *item = NULL);
350     /** @brief Add a sequence from the stopmotion widget. */
351     void slotAddOrUpdateSequence(const QString frameName);
352     /** @brief A proxy clip was created, update display. */
353     void slotGotProxy(const QString &proxyPath);
354     void slotGotProxy(ProjectItem *item);
355     /** @brief Enable / disable proxy for current clip. */
356     void slotProxyCurrentItem(bool doProxy);
357     /** @brief Put clip in the proxy waiting list. */
358     void slotCreateProxy(const QString id);
359     /** @brief Stop creation of this clip's proxy. */
360     void slotAbortProxy(const QString id, const QString path);
361     /** @brief Start creation of proxy clip. */
362     void slotGenerateProxy();//const QString destPath, const QString sourcePath, int clipType, int exif);
363
364 signals:
365     void clipSelected(DocClipBase *, QPoint zone = QPoint());
366     void getFileProperties(const QDomElement, const QString &, int pixHeight, bool, bool);
367     void receivedClipDuration(const QString &);
368     void showClipProperties(DocClipBase *);
369     void showClipProperties(QList <DocClipBase *>, QMap<QString, QString> commonproperties);
370     void projectModified();
371     void loadingIsOver();
372     void displayMessage(const QString, int progress);
373     void clipNameChanged(const QString, const QString);
374     void clipNeedsReload(const QString&, bool);
375     /** @brief A property affecting display was changed, so we need to update monitors and thumbnails
376      *  @param id: The clip's id string
377      *  @param resetThumbs Should we recreate the timeline thumbnails. */
378     void refreshClip(const QString &id, bool resetThumbs);
379     void updateRenderStatus();
380     void deleteProjectClips(QStringList ids, QMap <QString, QString> folderids);
381     void findInTimeline(const QString &clipId);
382     /** @brief Request a profile change for current document. */
383     void updateProfile(const QString &);
384     void processNextThumbnail();
385 };
386
387 #endif
388