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