]> git.sesse.net Git - kdenlive/blob - src/projectlist.h
small optimizations, make sure we use cached thumbs when loading a doc
[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 QImage &img);
229     void slotReplyGetImage(const QString &clipId, const QString &name, int width, int height);
230     void slotReplyGetFileProperties(const QString &clipId, Mlt::Producer *producer, const stringMap &properties, const stringMap &metadata, bool replace, bool refreshThumbnail);
231     void slotAddClip(DocClipBase *clip, bool getProperties);
232     void slotDeleteClip(const QString &clipId);
233     void slotUpdateClip(const QString &id);
234     void slotRefreshClipThumbnail(const QString &clipId, bool update = true);
235     void slotRefreshClipThumbnail(QTreeWidgetItem *item, bool update = true);
236     void slotRemoveInvalidClip(const QString &id, bool replace);
237     void slotRemoveInvalidProxy(const QString &id, bool durationError);
238     void slotSelectClip(const QString &ix);
239
240     /** @brief Prepares removing the selected items. */
241     void slotRemoveClip();
242     void slotAddClip(const QList <QUrl> givenList = QList <QUrl> (), const QString &groupName = QString(), const QString &groupId = QString());
243
244     /** @brief Adds, edits or deletes a folder item.
245     *
246     * This is triggered by AddFolderCommand and EditFolderCommand. */
247     void slotAddFolder(const QString foldername, const QString &clipId, bool remove, bool edit = false);
248     void slotResetProjectList();
249     void slotOpenClip();
250     void slotEditClip();
251     void slotReloadClip(const QString &id = QString());
252
253     /** @brief Shows dialog for setting up a color clip. */
254     void slotAddColorClip();
255     void regenerateTemplate(const QString &id);
256     void slotUpdateClipCut(QPoint p);
257     void slotAddClipCut(const QString &id, int in, int out);
258     void slotForceProcessing(const QString &id);
259     /** @brief Remove all instances of a proxy and delete the file. */
260     void slotDeleteProxy(const QString proxyPath);
261
262 private:
263     ProjectListView *m_listView;
264     Render *m_render;
265     Timecode m_timecode;
266     double m_fps;
267     QMenu *m_menu;
268     QFuture<void> m_queueRunner;
269     QUndoStack *m_commandStack;
270     ProjectItem *getItemById(const QString &id);
271     QTreeWidgetItem *getAnyItemById(const QString &id);
272     FolderProjectItem *getFolderItemById(const QString &id);
273     QAction *m_openAction;
274     QAction *m_reloadAction;
275     QMenu *m_transcodeAction;
276     KdenliveDoc *m_doc;
277     ItemDelegate *m_listViewDelegate;
278     /** @brief False if we have not yet finished opening the document. */
279     bool m_refreshed;
280     QToolButton *m_addButton;
281     QToolButton *m_deleteButton;
282     QToolButton *m_editButton;
283     //QMap <QString, QDomElement> m_infoQueue;
284     QMap <QString, QDomElement> m_producerQueue;
285     QList <QString> m_thumbnailQueue;
286     QAction *m_proxyAction;
287     QStringList m_processingClips;
288     /** @brief Holds a list of proxy urls that should be aborted. */
289     QStringList m_abortProxy;
290     /** @brief Holds a list of proxy urls that are currently being created. */
291     QStringList m_processingProxy;
292     QMutex m_mutex;
293     bool m_abortAllProxies;
294     QList <PROXYINFO> m_proxyList;
295     QFutureSynchronizer<void> m_proxyThreads;
296     
297     void requestClipThumbnail(const QString id);
298
299     /** @brief Creates an EditFolderCommand to change the name of an folder item. */
300     void editFolder(const QString folderName, const QString oldfolderName, const QString &clipId);
301
302     /** @brief Gets the selected folder (or the folder of the selected item). */
303     QStringList getGroup() const;
304     void regenerateTemplate(ProjectItem *clip);
305     void editClipSelection(QList<QTreeWidgetItem *> list);
306
307     /** @brief Enables and disables transcode actions based on the selected clip's type. */
308     void adjustTranscodeActions(ProjectItem *clip) const;
309     /** @brief Enables and disables proxy action based on the selected clip. */
310     void adjustProxyActions(ProjectItem *clip) const;
311
312     /** @brief Sets the buttons enabled/disabled according to selected item. */
313     void updateButtons() const;
314
315     /** @brief Set the Proxy status on a clip. 
316      * @param item The clip item to set status
317      * @param status The proxy status (see definitions.h) */
318     void setProxyStatus(const QString proxyPath, PROXYSTATUS status, int progress = 0);
319     void setProxyStatus(ProjectItem *item, PROXYSTATUS status, int progress = 0);
320     /** @brief Process ffmpeg output to find out process progress. */
321     void processLogInfo(const QString &path, int *duration, const QString &log);
322     void monitorItemEditing(bool enable);
323     /** @brief Set thumbnail for a project's clip. */
324     void setThumbnail(const QString &clipId, const QPixmap &pix);
325     /** @brief Get cached thumbnail for a project's clip or create it if no cache. */
326     void getCachedThumbnail(ProjectItem *item);
327
328 private slots:
329     void slotClipSelected();
330     void slotAddSlideshowClip();
331     void slotAddTitleClip();
332     void slotAddTitleTemplateClip();
333
334     /** @brief Shows the context menu after enabling and disabling actions based on the item's type.
335     * @param pos The position where the menu should pop up
336     * @param item The item for which the checks should be done */
337     void slotContextMenu(const QPoint &pos, QTreeWidgetItem *item);
338
339     /** @brief Creates an AddFolderCommand. */
340     void slotAddFolder();
341
342     /** @brief This is triggered when a clip description has been modified. */
343     void slotItemEdited(QTreeWidgetItem *item, int column);
344     void slotUpdateClipProperties(ProjectItem *item, QMap <QString, QString> properties);
345     void slotProcessNextThumbnail();
346     void slotCheckForEmptyQueue();
347     void slotPauseMonitor();
348     /** A clip was modified externally, change icon so that user knows it */
349     void slotModifiedClip(const QString &id);
350     void slotMissingClip(const QString &id);
351     void slotAvailableClip(const QString &id);
352     /** @brief Try to find a matching profile for given item. */
353     bool adjustProjectProfileToItem(ProjectItem *item = NULL);
354     /** @brief Add a sequence from the stopmotion widget. */
355     void slotAddOrUpdateSequence(const QString frameName);
356     /** @brief A proxy clip was created, update display. */
357     void slotGotProxy(const QString &proxyPath);
358     void slotGotProxy(ProjectItem *item);
359     /** @brief Enable / disable proxy for current clip. */
360     void slotProxyCurrentItem(bool doProxy);
361     /** @brief Put clip in the proxy waiting list. */
362     void slotCreateProxy(const QString id);
363     /** @brief Stop creation of this clip's proxy. */
364     void slotAbortProxy(const QString id, const QString path);
365     /** @brief Start creation of proxy clip. */
366     void slotGenerateProxy();//const QString destPath, const QString sourcePath, int clipType, int exif);
367
368 signals:
369     void clipSelected(DocClipBase *, QPoint zone = QPoint());
370     void getFileProperties(const QDomElement, const QString &, int pixHeight, bool);
371     void receivedClipDuration(const QString &);
372     void showClipProperties(DocClipBase *);
373     void showClipProperties(QList <DocClipBase *>, QMap<QString, QString> commonproperties);
374     void projectModified();
375     void loadingIsOver();
376     void displayMessage(const QString, int progress);
377     void clipNameChanged(const QString, const QString);
378     void clipNeedsReload(const QString&, bool);
379     /** @brief A property affecting display was changed, so we need to update monitors and thumbnails
380      *  @param id: The clip's id string
381      *  @param resetThumbs Should we recreate the timeline thumbnails. */
382     void refreshClip(const QString &id, bool resetThumbs);
383     void updateRenderStatus();
384     void deleteProjectClips(QStringList ids, QMap <QString, QString> folderids);
385     void findInTimeline(const QString &clipId);
386     /** @brief Request a profile change for current document. */
387     void updateProfile(const QString &);
388     void processNextThumbnail();
389 };
390
391 #endif
392