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