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