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