1 /***************************************************************************
2 * Copyright (C) 2007 by Jean-Baptiste Mardelle (jb@kdenlive.org) *
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. *
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. *
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 ***************************************************************************/
24 #include <QDomNodeList>
27 #include <QToolButton>
28 #include <QTreeWidget>
30 #include <QStyledItemDelegate>
33 #include <QApplication>
35 #include <QFutureSynchronizer>
36 #include <QListWidget>
38 #include <QPushButton>
40 #include <KTreeWidgetSearchLine>
43 #include <kdeversion.h>
46 #include <nepomuk/kratingpainter.h>
47 #include <nepomuk/resource.h>
50 #include "definitions.h"
52 #include "kdenlivesettings.h"
53 #include "folderprojectitem.h"
54 #include "subprojectitem.h"
55 #include "projecttree/abstractclipjob.h"
58 #if KDE_IS_VERSION(4,7,0)
59 #include <KMessageWidget>
61 // Dummy KMessageWidget to allow compilation of MyMessageWidget class since Qt's moc doesn work inside #ifdef
63 class KMessageWidget: public QLabel
66 KMessageWidget(QWidget * = 0) {};
67 KMessageWidget(const QString &, QWidget * = 0) {};
68 virtual ~KMessageWidget(){};
72 class MyMessageWidget: public KMessageWidget
76 MyMessageWidget(QWidget *parent = 0);
77 MyMessageWidget(const QString &text, QWidget *parent = 0);
80 bool event(QEvent* ev);
83 void messageClosing();
92 class ProjectListView;
96 class AbstractClipJob;
98 const int NameRole = Qt::UserRole;
99 const int DurationRole = NameRole + 1;
100 const int UsageRole = NameRole + 2;
102 class SmallInfoLabel: public QPushButton
106 SmallInfoLabel(QWidget *parent = 0);
107 static const QString getStyleSheet(const QPalette &p);
109 QTimeLine* m_timeLine;
112 void slotSetJobCount(int jobCount);
115 void slotTimeLineChanged(qreal value);
116 void slotTimeLineFinished();
119 class InvalidDialog: public KDialog
123 InvalidDialog(const QString &caption, const QString &message, bool infoOnly, QWidget *parent = 0);
124 virtual ~InvalidDialog();
125 void addClip(const QString &id, const QString &path);
126 QStringList getIds() const;
128 QListWidget *m_clipList;
132 class ItemDelegate: public QStyledItemDelegate
135 ItemDelegate(QAbstractItemView* parent = 0): QStyledItemDelegate(parent) {
138 /*void drawFocus(QPainter *, const QStyleOptionViewItem &, const QRect &) const {
141 void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const {
142 if (index.column() == 0 && !index.data(DurationRole).isNull()) {
143 QRect r1 = option.rect;
145 QStyleOptionViewItemV4 opt(option);
146 QStyle *style = opt.widget ? opt.widget->style() : QApplication::style();
147 style->drawPrimitive(QStyle::PE_PanelItemViewItem, &opt, painter, opt.widget);
149 if (option.state & QStyle::State_Selected) {
150 painter->setPen(option.palette.highlightedText().color());
152 const int textMargin = style->pixelMetric(QStyle::PM_FocusFrameHMargin) + 1;
153 QPixmap pixmap = qVariantValue<QPixmap>(index.data(Qt::DecorationRole));
154 QPoint pixmapPoint(r1.left() + textMargin, r1.top() + (r1.height() - pixmap.height()) / 2);
155 painter->drawPixmap(pixmapPoint, pixmap);
156 int decoWidth = pixmap.width() + 2 * textMargin;
158 QFont font = painter->font();
160 painter->setFont(font);
161 int mid = (int)((r1.height() / 2));
162 r1.adjust(decoWidth, 0, 0, -mid);
163 QRect r2 = option.rect;
164 r2.adjust(decoWidth, mid, 0, 0);
165 painter->drawText(r1, Qt::AlignLeft | Qt::AlignBottom, index.data().toString());
167 painter->setFont(font);
168 QString subText = index.data(DurationRole).toString();
169 int usage = index.data(UsageRole).toInt();
170 if (usage != 0) subText.append(QString(" (%1)").arg(usage));
172 painter->drawText(r2, Qt::AlignLeft | Qt::AlignVCenter , subText, &bounding);
173 int jobProgress = index.data(Qt::UserRole + 5).toInt();
174 if (jobProgress != 0 && jobProgress != JOBDONE && jobProgress != JOBABORTED) {
175 if (jobProgress != JOBCRASHED) {
176 // Draw job progress bar
177 QColor color = option.palette.alternateBase().color();
179 painter->setPen(option.palette.link().color());
180 QRect progress(pixmapPoint.x() + 2, pixmapPoint.y() + pixmap.height() - 9, pixmap.width() - 4, 7);
181 painter->setBrush(QBrush(color));
182 painter->drawRect(progress);
183 painter->setBrush(option.palette.link());
184 progress.adjust(2, 2, -2, -2);
185 if (jobProgress == JOBWAITING) {
186 progress.setLeft(progress.right() - 2);
187 painter->drawRect(progress);
188 progress.moveLeft(progress.left() - 5);
189 painter->drawRect(progress);
191 else if (jobProgress > 0) {
192 progress.setWidth(progress.width() * jobProgress / 100);
193 painter->drawRect(progress);
195 } else if (jobProgress == JOBCRASHED) {
196 QString jobText = index.data(Qt::UserRole + 7).toString();
197 if (!jobText.isEmpty()) {
198 QRectF txtBounding = painter->boundingRect(r2, Qt::AlignRight | Qt::AlignVCenter, " " + jobText + " ");
199 painter->setPen(Qt::NoPen);
200 painter->setBrush(option.palette.highlight());
201 painter->drawRoundedRect(txtBounding, 2, 2);
202 painter->setPen(option.palette.highlightedText().color());
203 painter->drawText(txtBounding, Qt::AlignCenter, jobText);
209 } else if (index.column() == 2 && KdenliveSettings::activate_nepomuk()) {
210 if (index.data().toString().isEmpty()) {
211 QStyledItemDelegate::paint(painter, option, index);
214 QRect r1 = option.rect;
215 if (option.state & (QStyle::State_Selected)) {
216 painter->fillRect(r1, option.palette.highlight());
219 KRatingPainter::paintRating(painter, r1, Qt::AlignCenter, index.data().toInt());
222 QStyledItemDelegate::paint(painter, option, index);
227 class ProjectList : public QWidget
232 ProjectList(QWidget *parent = 0);
233 virtual ~ProjectList();
235 QDomElement producersList();
236 void setRenderer(Render *projectRender);
237 void slotUpdateClipProperties(const QString &id, QMap <QString, QString> properties);
238 QByteArray headerInfo() const;
239 void setHeaderInfo(const QByteArray &state);
240 void updateProjectFormat(Timecode t);
241 void setupMenu(QMenu *addMenu, QAction *defaultAction);
242 void setupGeneratorMenu(const QHash<QString,QMenu*>& menus);
243 QString currentClipUrl() const;
244 KUrl::List getConditionalUrls(const QString &condition) const;
245 /** @brief Get a list of selected clip Id's that match a condition. */
246 QStringList getConditionalIds(const QString &condition) const;
247 QDomDocument generateTemplateXml(QString data, const QString &replaceString);
249 void trashUnusedClips();
250 QList <DocClipBase*> documentClipList() const;
251 void addClipCut(const QString &id, int in, int out, const QString desc, bool newItem);
252 void removeClipCut(const QString &id, int in, int out);
253 void focusTree() const;
254 SubProjectItem *getSubItem(ProjectItem *clip, QPoint zone);
255 void doUpdateClipCut(const QString &id, const QPoint oldzone, const QPoint zone, const QString &comment);
256 bool hasMissingClips();
257 void deleteProjectFolder(QMap <QString, QString> map);
258 void selectItemById(const QString &clipId);
260 /** @brief Returns a string list of all supported mime extensions. */
261 static QString getExtensions();
262 /** @brief Returns a list of urls containing original and proxy urls. */
263 QMap <QString, QString> getProxies();
264 /** @brief Enable / disable proxies. */
265 void updateProxyConfig();
266 /** @brief Get a property from the document. */
267 QString getDocumentProperty(const QString &key) const;
269 /** @brief Does this project allow proxies. */
270 bool useProxy() const;
271 /** @brief Should we automatically create proxy clips for newly added clips. */
272 bool generateProxy() const;
273 /** @brief Should we automatically create proxy clips for newly added clips. */
274 bool generateImageProxy() const;
275 /** @brief Returns a list of the expanded folder ids. */
276 QStringList expandedFolders() const;
277 /** @brief Deselect all clips in project tree. */
278 void clearSelection();
279 /** @brief Print required overlays over clip thumb (proxy, stabilized,...). */
280 void processThumbOverlays(ProjectItem *item, QPixmap &pix);
281 /** @brief Start an MLT process job. */
282 void startClipFilterJob(const QString &filterName, const QString &condition);
283 /** @brief Set current document for the project tree. */
284 void setDocument(KdenliveDoc *doc);
286 /** @brief Palette was changed, update style. */
287 void updatePalette();
290 void updateAllClips(bool displayRatioChanged, bool fpsChanged, QStringList brokenClips);
291 void slotReplyGetImage(const QString &clipId, const QImage &img);
292 void slotReplyGetImage(const QString &clipId, const QString &name, int width, int height);
293 void slotReplyGetFileProperties(const QString &clipId, Mlt::Producer *producer, const stringMap &properties, const stringMap &metadata, bool replace);
294 void slotAddClip(DocClipBase *clip, bool getProperties);
295 void slotDeleteClip(const QString &clipId);
296 void slotUpdateClip(const QString &id);
297 void slotRefreshClipThumbnail(const QString &clipId, bool update = true);
298 void slotRefreshClipThumbnail(QTreeWidgetItem *item, bool update = true);
299 void slotRemoveInvalidClip(const QString &id, bool replace);
300 void slotRemoveInvalidProxy(const QString &id, bool durationError);
301 void slotSelectClip(const QString &ix);
303 /** @brief Prepares removing the selected items. */
304 void slotRemoveClip();
305 void slotAddClip(const QString url, const QString &groupName, const QString &groupId);
306 void slotAddClip(const QList <QUrl> givenList = QList <QUrl> (), const QString &groupName = QString(), const QString &groupId = QString());
308 /** @brief Adds, edits or deletes a folder item.
310 * This is triggered by AddFolderCommand and EditFolderCommand. */
311 void slotAddFolder(const QString foldername, const QString &clipId, bool remove, bool edit = false);
312 void slotResetProjectList();
315 void slotReloadClip(const QString &id = QString());
317 /** @brief Shows dialog for setting up a color clip. */
318 void slotAddColorClip();
319 void regenerateTemplate(const QString &id);
320 void slotUpdateClipCut(QPoint p);
321 void slotAddClipCut(const QString &id, int in, int out);
322 void slotForceProcessing(const QString &id);
323 /** @brief Remove all instances of a proxy and delete the file. */
324 void slotDeleteProxy(const QString proxyPath);
325 /** @brief Start a hard cut clip job. */
326 void slotCutClipJob(const QString &id, QPoint zone);
327 /** @brief Start transcoding selected clips. */
328 void slotTranscodeClipJob(const QString &condition, QString params, QString desc);
329 /** @brief Start an MLT process job. */
330 void slotStartFilterJob(ItemInfo, const QString&,const QString&,const QString&,const QString&,const QString&,const QMap <QString, QString>&);
331 void slotSetThumbnail(const QString &id, int framePos, QImage img);
335 ProjectListView *m_listView;
340 QFuture<void> m_queueRunner;
341 QUndoStack *m_commandStack;
342 ProjectItem *getItemById(const QString &id);
343 QTreeWidgetItem *getAnyItemById(const QString &id);
344 FolderProjectItem *getFolderItemById(const QString &id);
345 FolderProjectItem *getFolderItemByName(const QString &name);
346 QAction *m_openAction;
347 QAction *m_reloadAction;
348 QAction *m_discardCurrentClipJobs;
349 QMenu *m_extractAudioAction;
350 QMenu *m_transcodeAction;
351 QMenu *m_clipsActionsMenu;
353 ItemDelegate *m_listViewDelegate;
354 /** @brief False if we have not yet finished opening the document. */
356 /** @brief False if we have not yet finished checking all project tree thumbs. */
357 bool m_allClipsProcessed;
358 QToolButton *m_addButton;
359 QToolButton *m_deleteButton;
360 QToolButton *m_editButton;
361 //QMap <QString, QDomElement> m_infoQueue;
362 QMap <QString, QDomElement> m_producerQueue;
363 QList <QString> m_thumbnailQueue;
364 QAction *m_proxyAction;
366 QMutex m_processMutex;
368 /** @brief We are cleaning up the project list, so stop processing signals. */
370 QList <AbstractClipJob *> m_jobList;
371 QFutureSynchronizer<void> m_jobThreads;
372 InvalidDialog *m_invalidClipDialog;
374 SmallInfoLabel *m_infoLabel;
375 /** @brief A list of strings containing the last error logs for clip jobs. */
376 QStringList m_errorLog;
378 #if KDE_IS_VERSION(4,7,0)
379 MyMessageWidget *m_infoMessage;
380 /** @brief The action that will trigger the log dialog. */
381 QAction *m_logAction;
384 void requestClipThumbnail(const QString id);
386 /** @brief Creates an EditFolderCommand to change the name of an folder item. */
387 void editFolder(const QString folderName, const QString oldfolderName, const QString &clipId);
389 /** @brief Gets the selected folder (or the folder of the selected item). */
390 QStringList getGroup() const;
391 void regenerateTemplate(ProjectItem *clip);
392 void editClipSelection(QList<QTreeWidgetItem *> list);
394 /** @brief Enables and disables transcode actions based on the selected clip's type. */
395 void adjustTranscodeActions(ProjectItem *clip) const;
396 /** @brief Enables and disables stabilize actions based on the selected clip's type. */
397 void adjustStabilizeActions(ProjectItem *clip) const;
398 /** @brief Enables and disables proxy action based on the selected clip. */
399 void adjustProxyActions(ProjectItem *clip) const;
401 /** @brief Sets the buttons enabled/disabled according to selected item. */
402 void updateButtons() const;
404 /** @brief Set the Proxy status on a clip.
405 * @param item The clip item to set status
406 * @param jobType The job type
407 * @param status The job status (see definitions.h)
408 * @param progress The job progress (in percents)
409 * @param statusMessage The job info message */
410 void setJobStatus(ProjectItem *item, JOBTYPE jobType, CLIPJOBSTATUS status, int progress = 0, const QString &statusMessage = QString());
411 void monitorItemEditing(bool enable);
412 /** @brief Get cached thumbnail for a project's clip or create it if no cache. */
413 void getCachedThumbnail(ProjectItem *item);
414 void getCachedThumbnail(SubProjectItem *item);
415 /** @brief The clip is about to be reloaded, cancel thumbnail requests. */
416 void resetThumbsProducer(DocClipBase *clip);
417 /** @brief Check if a clip has a running or pending job process. */
418 bool hasPendingJob(ProjectItem *item, JOBTYPE type);
419 /** @brief Delete pending jobs for a clip. */
420 void deleteJobsForClip(const QString &clipId);
421 /** @brief Discard specific job type for a clip. */
422 void discardJobs(const QString &id, JOBTYPE type = NOJOBTYPE);
423 /** @brief Get the list of job names for current clip. */
424 QStringList getPendingJobs(const QString &id);
425 /** @brief Start an MLT process job. */
426 void processClipJob(QStringList ids, const QString&destination, bool autoAdd, QStringList jobParams, const QString &description, stringMap extraParams = stringMap());
427 /** @brief Create rounded shape pixmap for project tree thumb. */
428 QPixmap roundedPixmap(QImage img);
429 QPixmap roundedPixmap(QPixmap source);
430 /** @brief Extract a clip's metadata with the exiftool program. */
431 void extractMetadata(DocClipBase *clip);
432 /** @brief Add a special FFmpeg tag if clip matches some properties (for example set full_luma for Sony NEX camcorders. */
433 //void checkCamcorderFilters(DocClipBase *clip, QMap <QString, QString> meta);
436 void slotClipSelected();
437 void slotAddSlideshowClip();
438 void slotAddTitleClip();
439 void slotAddTitleTemplateClip();
441 /** @brief Shows the context menu after enabling and disabling actions based on the item's type.
442 * @param pos The position where the menu should pop up
443 * @param item The item for which the checks should be done */
444 void slotContextMenu(const QPoint &pos, QTreeWidgetItem *item);
446 /** @brief Creates an AddFolderCommand. */
447 void slotAddFolder(const QString &name = QString());
449 /** @brief This is triggered when a clip description has been modified. */
450 void slotItemEdited(QTreeWidgetItem *item, int column);
451 void slotUpdateClipProperties(ProjectItem *item, QMap <QString, QString> properties);
452 void slotProcessNextThumbnail();
453 void slotCheckForEmptyQueue();
454 void slotPauseMonitor();
455 /** A clip was modified externally, change icon so that user knows it */
456 void slotModifiedClip(const QString &id);
457 void slotMissingClip(const QString &id);
458 void slotAvailableClip(const QString &id);
459 /** @brief Try to find a matching profile for given item. */
460 bool adjustProjectProfileToItem(ProjectItem *item = NULL);
461 /** @brief Add a sequence from the stopmotion widget. */
462 void slotAddOrUpdateSequence(const QString frameName);
463 /** @brief A proxy clip was created, update display. */
464 void slotGotProxy(const QString &proxyPath);
465 void slotGotProxy(ProjectItem *item);
466 /** @brief Enable / disable proxy for current clip. */
467 void slotProxyCurrentItem(bool doProxy, ProjectItem *itemToProxy = NULL);
468 /** @brief Put clip in the proxy waiting list. */
469 void slotCreateProxy(const QString id);
470 /** @brief Stop creation of this clip's proxy. */
471 void slotAbortProxy(const QString id, const QString path);
472 /** @brief Start creation of clip jobs. */
473 void slotProcessJobs();
474 /** @brief Discard running and pending clip jobs. */
475 void slotCancelJobs();
476 /** @brief Discard a running clip jobs. */
477 void slotCancelRunningJob(const QString id, stringMap);
478 /** @brief Update a clip's job status. */
479 void slotProcessLog(const QString, int progress, int, const QString = QString());
480 /** @brief A clip job crashed, inform user. */
481 void slotUpdateJobStatus(const QString id, int type, int status, const QString label, const QString actionName, const QString details);
482 void slotUpdateJobStatus(ProjectItem *item, int type, int status, const QString &label, const QString &actionName = QString(), const QString details = QString());
483 /** @brief Display error log for last failed job. */
484 void slotShowJobLog();
485 /** @brief A proxy clip is ready. */
486 void slotGotProxyForId(const QString);
487 /** @brief Check if it is necessary to start a job thread. */
488 void slotCheckJobProcess();
489 /** @brief Fill the jobs menu with current clip's jobs. */
490 void slotPrepareJobsMenu();
491 /** @brief Discard all jobs for current clip. */
492 void slotDiscardClipJobs();
493 /** @brief Make sure current clip is visible in project tree. */
494 void slotCheckScrolling();
495 /** @brief Reset all text and log data from info message widget. */
496 void slotResetInfoMessage();
497 /** @brief close warning info passive popup. */
498 void slotClosePopup();
499 /** @brief process clip job result. */
500 void slotGotFilterJobResults(QString ,int , int, stringMap, stringMap);
503 void clipSelected(DocClipBase *, QPoint zone = QPoint(), bool forceUpdate = false);
504 void receivedClipDuration(const QString &);
505 void showClipProperties(DocClipBase *);
506 void showClipProperties(QList <DocClipBase *>, QMap<QString, QString> commonproperties);
507 void projectModified();
508 void loadingIsOver();
509 void displayMessage(const QString, int progress, MessageType type = DefaultMessage);
510 void clipNameChanged(const QString, const QString);
511 void clipNeedsReload(const QString&);
512 /** @brief A property affecting display was changed, so we need to update monitors and thumbnails
513 * @param id: The clip's id string
514 * @param resetThumbs Should we recreate the timeline thumbnails. */
515 void refreshClip(const QString &id, bool resetThumbs);
516 void updateRenderStatus();
517 void deleteProjectClips(QStringList ids, QMap <QString, QString> folderids);
518 void findInTimeline(const QString &clipId);
519 /** @brief Request a profile change for current document. */
520 void updateProfile(const QString &);
521 void processNextThumbnail();
522 /** @brief Activate the clip monitor. */
523 void raiseClipMonitor(bool forceRefresh);
524 /** @brief Set number of running jobs. */
526 void cancelRunningJob(const QString, stringMap);
527 void processLog(const QString, int , int, const QString = QString());
528 void addClip(const QString, const QString &, const QString &);
529 void updateJobStatus(const QString, int, int, const QString label = QString(), const QString actionName = QString(), const QString details = QString());
530 void gotProxy(const QString);
531 void checkJobProcess();
532 /** @brief A Filter Job produced results, send them back to the clip. */
533 void gotFilterJobResults(const QString &id, int startPos, int track, stringMap params, stringMap extra);
535 void updateAnalysisData(DocClipBase *);
536 void addMarkers(const QString &, QList <CommentedTime>);