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