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