]> git.sesse.net Git - kdenlive/blob - src/projectlist.h
Merge branch 'master' into stabilize
[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 <QHash>
26 #include <QToolBar>
27 #include <QToolButton>
28 #include <QTreeWidget>
29 #include <QPainter>
30 #include <QStyledItemDelegate>
31 #include <QUndoStack>
32 #include <QTimer>
33 #include <QApplication>
34 #include <QFuture>
35 #include <QFutureSynchronizer>
36 #include <QListWidget>
37
38 #include <KTreeWidgetSearchLine>
39 #include <KUrl>
40 #include <KIcon>
41
42 #ifdef NEPOMUK
43 #include <nepomuk/kratingpainter.h>
44 #include <nepomuk/resource.h>
45 #endif
46
47 #include "definitions.h"
48 #include "timecode.h"
49 #include "kdenlivesettings.h"
50 #include "folderprojectitem.h"
51 #include "subprojectitem.h"
52 #include <kdialog.h>
53
54 namespace Mlt
55 {
56 class Producer;
57 };
58
59 struct PROXYINFO {
60     QString dest;
61     QString src;
62     CLIPTYPE type;
63     int exif;
64 };
65
66 class ProjectItem;
67 class ProjectListView;
68 class Render;
69 class KdenliveDoc;
70 class DocClipBase;
71
72 const int NameRole = Qt::UserRole;
73 const int DurationRole = NameRole + 1;
74 const int UsageRole = NameRole + 2;
75
76 class InvalidDialog: public KDialog
77 {
78     Q_OBJECT
79 public:
80     InvalidDialog(const QString &caption, const QString &message, bool infoOnly, QWidget *parent = 0);
81     virtual ~InvalidDialog();
82     void addClip(const QString &id, const QString &path);
83     QStringList getIds() const;
84 private:
85     QListWidget *m_clipList;
86 };
87
88
89 class ItemDelegate: public QStyledItemDelegate
90 {
91 public:
92     ItemDelegate(QAbstractItemView* parent = 0): QStyledItemDelegate(parent) {
93     }
94
95     /*void drawFocus(QPainter *, const QStyleOptionViewItem &, const QRect &) const {
96     }*/
97
98     void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const {
99         if (index.column() == 0 && !index.data(DurationRole).isNull()) {
100             QRect r1 = option.rect;
101             painter->save();
102             QStyleOptionViewItemV4 opt(option);
103             QStyle *style = opt.widget ? opt.widget->style() : QApplication::style();
104             style->drawPrimitive(QStyle::PE_PanelItemViewItem, &opt, painter, opt.widget);
105
106             if (option.state & QStyle::State_Selected) {
107                 painter->setPen(option.palette.highlightedText().color());
108             }
109             const int textMargin = style->pixelMetric(QStyle::PM_FocusFrameHMargin) + 1;
110             QPixmap pixmap = qVariantValue<QPixmap>(index.data(Qt::DecorationRole));
111             painter->drawPixmap(r1.left() + textMargin, r1.top() + (r1.height() - pixmap.height()) / 2, pixmap);
112             int decoWidth = pixmap.width() + 2 * textMargin;
113
114             QFont font = painter->font();
115             font.setBold(true);
116             painter->setFont(font);
117             int mid = (int)((r1.height() / 2));
118             r1.adjust(decoWidth, 0, 0, -mid);
119             QRect r2 = option.rect;
120             r2.adjust(decoWidth, mid, 0, 0);
121             painter->drawText(r1, Qt::AlignLeft | Qt::AlignBottom , index.data().toString());
122             font.setBold(false);
123             painter->setFont(font);
124             QString subText = index.data(DurationRole).toString();
125             int usage = index.data(UsageRole).toInt();
126             if (usage != 0) subText.append(QString(" (%1)").arg(usage));
127             if (option.state & (QStyle::State_Selected)) painter->setPen(option.palette.color(QPalette::Mid));
128             QRectF bounding;
129             painter->drawText(r2, Qt::AlignLeft | Qt::AlignVCenter , subText, &bounding);
130             
131             int proxy = index.data(Qt::UserRole + 5).toInt();
132             if (proxy != 0) {
133                 QRectF txtBounding;
134                 QString proxyText;
135                 QBrush brush;
136                 QColor color;
137                 if (proxy > 0) {
138                     proxyText = i18n("Proxy %1\%", 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(const QHash<QString,QMenu*>& menus);
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     /** @brief Deselect all clips in project tree. */
240     void clearSelection();
241
242 public slots:
243     void setDocument(KdenliveDoc *doc);
244     void updateAllClips(bool displayRatioChanged, bool fpsChanged, QStringList brokenClips);
245     void slotReplyGetImage(const QString &clipId, const QImage &img);
246     void slotReplyGetImage(const QString &clipId, const QString &name, int width, int height);
247     void slotReplyGetFileProperties(const QString &clipId, Mlt::Producer *producer, const stringMap &properties, const stringMap &metadata, bool replace);
248     void slotAddClip(DocClipBase *clip, bool getProperties);
249     void slotDeleteClip(const QString &clipId);
250     void slotUpdateClip(const QString &id);
251     void slotRefreshClipThumbnail(const QString &clipId, bool update = true);
252     void slotRefreshClipThumbnail(QTreeWidgetItem *item, bool update = true);
253     void slotRemoveInvalidClip(const QString &id, bool replace);
254     void slotRemoveInvalidProxy(const QString &id, bool durationError);
255     void slotSelectClip(const QString &ix);
256
257     /** @brief Prepares removing the selected items. */
258     void slotRemoveClip();
259     void slotAddClip(const QList <QUrl> givenList = QList <QUrl> (), const QString &groupName = QString(), const QString &groupId = QString());
260
261     /** @brief Adds, edits or deletes a folder item.
262     *
263     * This is triggered by AddFolderCommand and EditFolderCommand. */
264     void slotAddFolder(const QString foldername, const QString &clipId, bool remove, bool edit = false);
265     void slotResetProjectList();
266     void slotOpenClip();
267     void slotEditClip();
268     void slotReloadClip(const QString &id = QString());
269
270     /** @brief Shows dialog for setting up a color clip. */
271     void slotAddColorClip();
272     void regenerateTemplate(const QString &id);
273     void slotUpdateClipCut(QPoint p);
274     void slotAddClipCut(const QString &id, int in, int out);
275     void slotForceProcessing(const QString &id);
276     /** @brief Remove all instances of a proxy and delete the file. */
277     void slotDeleteProxy(const QString proxyPath);
278
279 private:
280     ProjectListView *m_listView;
281     Render *m_render;
282     Timecode m_timecode;
283     double m_fps;
284     QMenu *m_menu;
285     QFuture<void> m_queueRunner;
286     QUndoStack *m_commandStack;
287     ProjectItem *getItemById(const QString &id);
288     QTreeWidgetItem *getAnyItemById(const QString &id);
289     FolderProjectItem *getFolderItemById(const QString &id);
290     QAction *m_openAction;
291     QAction *m_reloadAction;
292     QMenu *m_transcodeAction;
293     QMenu *m_stabilizeAction;
294     KdenliveDoc *m_doc;
295     ItemDelegate *m_listViewDelegate;
296     /** @brief False if we have not yet finished opening the document. */
297     bool m_refreshed;
298     /** @brief False if we have not yet finished checking all project tree thumbs. */
299     bool m_allClipsProcessed;
300     QToolButton *m_addButton;
301     QToolButton *m_deleteButton;
302     QToolButton *m_editButton;
303     //QMap <QString, QDomElement> m_infoQueue;
304     QMap <QString, QDomElement> m_producerQueue;
305     QList <QString> m_thumbnailQueue;
306     QAction *m_proxyAction;
307     QStringList m_processingClips;
308     /** @brief Holds a list of proxy urls that should be aborted. */
309     QStringList m_abortProxy;
310     /** @brief Holds a list of proxy urls that are currently being created. */
311     QStringList m_processingProxy;
312     QMutex m_mutex;
313     bool m_abortAllProxies;
314     QList <PROXYINFO> m_proxyList;
315     QFutureSynchronizer<void> m_proxyThreads;
316     InvalidDialog *m_invalidClipDialog;
317     
318     void requestClipThumbnail(const QString id);
319
320     /** @brief Creates an EditFolderCommand to change the name of an folder item. */
321     void editFolder(const QString folderName, const QString oldfolderName, const QString &clipId);
322
323     /** @brief Gets the selected folder (or the folder of the selected item). */
324     QStringList getGroup() const;
325     void regenerateTemplate(ProjectItem *clip);
326     void editClipSelection(QList<QTreeWidgetItem *> list);
327
328     /** @brief Enables and disables transcode actions based on the selected clip's type. */
329     void adjustTranscodeActions(ProjectItem *clip) const;
330     /** @brief Enables and disables stabilize actions based on the selected clip's type. */
331     void adjustStabilizeActions(ProjectItem *clip) const;
332     /** @brief Enables and disables proxy action based on the selected clip. */
333     void adjustProxyActions(ProjectItem *clip) const;
334
335     /** @brief Sets the buttons enabled/disabled according to selected item. */
336     void updateButtons() const;
337
338     /** @brief Set the Proxy status on a clip. 
339      * @param item The clip item to set status
340      * @param status The proxy status (see definitions.h) */
341     void setProxyStatus(ProjectItem *item, PROXYSTATUS status, int progress = 0);
342     /** @brief Process ffmpeg output to find out process progress. */
343     void processLogInfo(QList <ProjectItem *>items, int *duration, const QString &log);
344     void monitorItemEditing(bool enable);
345     /** @brief Get cached thumbnail for a project's clip or create it if no cache. */
346     void getCachedThumbnail(ProjectItem *item);
347     void getCachedThumbnail(SubProjectItem *item);
348     /** @brief The clip is about to be reloaded, cancel thumbnail requests. */
349     void resetThumbsProducer(DocClipBase *clip);
350
351 private slots:
352     void slotClipSelected();
353     void slotAddSlideshowClip();
354     void slotAddTitleClip();
355     void slotAddTitleTemplateClip();
356
357     /** @brief Shows the context menu after enabling and disabling actions based on the item's type.
358     * @param pos The position where the menu should pop up
359     * @param item The item for which the checks should be done */
360     void slotContextMenu(const QPoint &pos, QTreeWidgetItem *item);
361
362     /** @brief Creates an AddFolderCommand. */
363     void slotAddFolder();
364
365     /** @brief This is triggered when a clip description has been modified. */
366     void slotItemEdited(QTreeWidgetItem *item, int column);
367     void slotUpdateClipProperties(ProjectItem *item, QMap <QString, QString> properties);
368     void slotProcessNextThumbnail();
369     void slotCheckForEmptyQueue();
370     void slotPauseMonitor();
371     /** A clip was modified externally, change icon so that user knows it */
372     void slotModifiedClip(const QString &id);
373     void slotMissingClip(const QString &id);
374     void slotAvailableClip(const QString &id);
375     /** @brief Try to find a matching profile for given item. */
376     bool adjustProjectProfileToItem(ProjectItem *item = NULL);
377     /** @brief Add a sequence from the stopmotion widget. */
378     void slotAddOrUpdateSequence(const QString frameName);
379     /** @brief A proxy clip was created, update display. */
380     void slotGotProxy(const QString &proxyPath);
381     void slotGotProxy(ProjectItem *item);
382     /** @brief Enable / disable proxy for current clip. */
383     void slotProxyCurrentItem(bool doProxy, ProjectItem *itemToProxy = NULL);
384     /** @brief Put clip in the proxy waiting list. */
385     void slotCreateProxy(const QString id);
386     /** @brief Stop creation of this clip's proxy. */
387     void slotAbortProxy(const QString id, const QString path);
388     /** @brief Start creation of proxy clip. */
389     void slotGenerateProxy();
390
391 signals:
392     void clipSelected(DocClipBase *, QPoint zone = QPoint(), bool forceUpdate = false);
393     void receivedClipDuration(const QString &);
394     void showClipProperties(DocClipBase *);
395     void showClipProperties(QList <DocClipBase *>, QMap<QString, QString> commonproperties);
396     void projectModified();
397     void loadingIsOver();
398     void displayMessage(const QString, int progress);
399     void clipNameChanged(const QString, const QString);
400     void clipNeedsReload(const QString&);
401     /** @brief A property affecting display was changed, so we need to update monitors and thumbnails
402      *  @param id: The clip's id string
403      *  @param resetThumbs Should we recreate the timeline thumbnails. */
404     void refreshClip(const QString &id, bool resetThumbs);
405     void updateRenderStatus();
406     void deleteProjectClips(QStringList ids, QMap <QString, QString> folderids);
407     void findInTimeline(const QString &clipId);
408     /** @brief Request a profile change for current document. */
409     void updateProfile(const QString &);
410     void processNextThumbnail();
411     /** @brief Activate the clip monitor. */
412     void raiseClipMonitor();
413 };
414
415 #endif
416