]> git.sesse.net Git - kdenlive/blob - src/monitor.cpp
8e86d23fa6bbd3d9f1b283f52f2c50b0d21c8a20
[kdenlive] / src / monitor.cpp
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 #include "monitor.h"
22 #include "smallruler.h"
23 #include "docclipbase.h"
24 #include "abstractclipitem.h"
25 #include "monitorscene.h"
26 #include "widgets/monitoreditwidget.h"
27 #include "kdenlivesettings.h"
28
29 #include <KDebug>
30 #include <KLocalizedString>
31 #include <KFileDialog>
32 #include <KApplication>
33 #include <KMessageBox>
34 #include <KSelectAction>
35
36 #include <QMouseEvent>
37 #include <QStylePainter>
38 #include <QMenu>
39 #include <QToolButton>
40 #include <QToolBar>
41 #include <QDesktopWidget>
42 #include <QLabel>
43 #include <QIntValidator>
44 #include <QVBoxLayout>
45 #include <QSlider>
46
47
48 #define SEEK_INACTIVE (-1)
49
50
51 Monitor::Monitor(Kdenlive::MONITORID id, MonitorManager *manager, QString profile, QWidget *parent) :
52     AbstractMonitor(id, manager, parent)
53     , render(NULL)
54     , m_currentClip(NULL)
55     , m_overlay(NULL)
56     , m_scale(1)
57     , m_length(2)
58     , m_dragStarted(false)
59     , m_loopClipAction(NULL)
60     , m_contextMenu(NULL)
61     , m_effectWidget(NULL)
62     , m_selectedClip(NULL)
63     , m_loopClipTransition(true)
64 #ifdef USE_OPENGL
65     , m_glWidget(NULL)
66 #endif
67     , m_editMarker(NULL)
68 {
69     QVBoxLayout *layout = new QVBoxLayout;
70     layout->setContentsMargins(0, 0, 0, 0);
71     layout->setSpacing(0);
72
73     // Video widget holder
74     layout->addWidget(videoBox, 10);
75     layout->addStretch();
76
77     // Get base size for icons
78     int s = style()->pixelMetric(QStyle::PM_SmallIconSize);
79
80
81     // Tool bar buttons
82     m_toolbar = new QToolBar(this);
83     m_toolbar->setIconSize(QSize(s, s));
84
85     m_playIcon = KIcon("media-playback-start");
86     m_pauseIcon = KIcon("media-playback-pause");
87
88
89     if (id != Kdenlive::dvdMonitor) {
90         m_toolbar->addAction(KIcon("kdenlive-zone-start"), i18n("Set zone start"), this, SLOT(slotSetZoneStart()));
91         m_toolbar->addAction(KIcon("kdenlive-zone-end"), i18n("Set zone end"), this, SLOT(slotSetZoneEnd()));
92     }
93
94     m_toolbar->addAction(KIcon("media-seek-backward"), i18n("Rewind"), this, SLOT(slotRewind()));
95     //m_toolbar->addAction(KIcon("media-skip-backward"), i18n("Rewind 1 frame"), this, SLOT(slotRewindOneFrame()));
96
97     QToolButton *playButton = new QToolButton(m_toolbar);
98     m_playMenu = new QMenu(i18n("Play..."), this);
99     m_playAction = m_playMenu->addAction(m_playIcon, i18n("Play"));
100     //m_playAction->setCheckable(true);
101     connect(m_playAction, SIGNAL(triggered()), this, SLOT(slotPlay()));
102
103     playButton->setMenu(m_playMenu);
104     playButton->setPopupMode(QToolButton::MenuButtonPopup);
105     m_toolbar->addWidget(playButton);
106
107     //m_toolbar->addAction(KIcon("media-skip-forward"), i18n("Forward 1 frame"), this, SLOT(slotForwardOneFrame()));
108     m_toolbar->addAction(KIcon("media-seek-forward"), i18n("Forward"), this, SLOT(slotForward()));
109
110     playButton->setDefaultAction(m_playAction);
111
112     if (id != Kdenlive::dvdMonitor) {
113         QToolButton *configButton = new QToolButton(m_toolbar);
114         m_configMenu = new QMenu(i18n("Misc..."), this);
115         configButton->setIcon(KIcon("system-run"));
116         configButton->setMenu(m_configMenu);
117         configButton->setPopupMode(QToolButton::QToolButton::InstantPopup);
118         m_toolbar->addWidget(configButton);
119
120         if (id == Kdenlive::clipMonitor) {
121             m_markerMenu = new QMenu(i18n("Go to marker..."), this);
122             m_markerMenu->setEnabled(false);
123             m_configMenu->addMenu(m_markerMenu);
124             connect(m_markerMenu, SIGNAL(triggered(QAction*)), this, SLOT(slotGoToMarker(QAction*)));
125         }
126         m_configMenu->addAction(KIcon("transform-scale"), i18n("Resize (100%)"), this, SLOT(slotSetSizeOneToOne()));
127         m_configMenu->addAction(KIcon("transform-scale"), i18n("Resize (50%)"), this, SLOT(slotSetSizeOneToTwo()));
128     }
129
130     // Create Volume slider popup
131     m_volumePopup = new QFrame(this, Qt::Popup);
132     QVBoxLayout *poplayout = new QVBoxLayout;
133     poplayout->setContentsMargins(0, 0, 0, 0);
134     m_audioSlider = new QSlider(Qt::Vertical);
135     m_audioSlider->setRange(0, 100);
136     poplayout->addWidget(m_audioSlider);
137     m_volumePopup->setLayout(poplayout);
138     KIcon icon;
139     if (KdenliveSettings::volume() == 0) icon = KIcon("audio-volume-muted");
140     else icon = KIcon("audio-volume-medium");
141
142     m_volumeWidget = m_toolbar->widgetForAction(m_toolbar->addAction(icon, i18n("Audio volume"), this, SLOT(slotShowVolume())));
143
144     // we need to show / hide the popup once so that it's geometry can be calculated in slotShowVolume
145     m_volumePopup->show();
146     m_volumePopup->hide();
147
148     setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
149     setLayout(layout);
150     setMinimumHeight(200);
151
152     if (profile.isEmpty())
153         profile = KdenliveSettings::current_profile();
154
155     bool monitorCreated = false;
156 #ifdef Q_WS_MAC
157     createOpenGlWidget(videoBox, profile);
158     monitorCreated = true;
159     //m_glWidget->setFixedSize(width, height);
160 #elif defined(USE_OPENGL)
161     if (KdenliveSettings::openglmonitors()) {
162         monitorCreated = createOpenGlWidget(videoBox, profile);
163     }
164 #endif
165     if (!monitorCreated) {
166         createVideoSurface();
167         render = new Render(m_id, (int) videoSurface->winId(), profile, this);
168         connect(videoSurface, SIGNAL(refreshMonitor()), render, SLOT(doRefresh()));
169     }
170 #ifdef USE_OPENGL
171     else if (m_glWidget) {
172         QVBoxLayout *lay = new QVBoxLayout;
173         lay->setContentsMargins(0, 0, 0, 0);
174         lay->addWidget(m_glWidget);
175         videoBox->setLayout(lay);
176     }
177 #endif
178
179     // Monitor ruler
180     m_ruler = new SmallRuler(this, render);
181     if (id == Kdenlive::dvdMonitor) m_ruler->setZone(-3, -2);
182     layout->addWidget(m_ruler);
183     
184     connect(m_audioSlider, SIGNAL(valueChanged(int)), this, SLOT(slotSetVolume(int)));
185     connect(render, SIGNAL(durationChanged(int)), this, SLOT(adjustRulerSize(int)));
186     connect(render, SIGNAL(rendererStopped(int)), this, SLOT(rendererStopped(int)));
187     connect(render, SIGNAL(rendererPosition(int)), this, SLOT(seekCursor(int)));
188
189     if (id != Kdenlive::clipMonitor) {
190         connect(render, SIGNAL(rendererPosition(int)), this, SIGNAL(renderPosition(int)));
191         connect(render, SIGNAL(durationChanged(int)), this, SIGNAL(durationChanged(int)));
192         connect(m_ruler, SIGNAL(zoneChanged(QPoint)), this, SIGNAL(zoneUpdated(QPoint)));
193     } else {
194         connect(m_ruler, SIGNAL(zoneChanged(QPoint)), this, SLOT(setClipZone(QPoint)));
195     }
196
197     if (videoSurface) videoSurface->show();
198
199     if (id == Kdenlive::projectMonitor) {
200         m_effectWidget = new MonitorEditWidget(render, videoBox);
201         connect(m_effectWidget, SIGNAL(showEdit(bool,bool)), this, SLOT(slotShowEffectScene(bool,bool)));
202         m_toolbar->addAction(m_effectWidget->getVisibilityAction());
203         videoBox->layout()->addWidget(m_effectWidget);
204         m_effectWidget->hide();
205     }
206
207     QWidget *spacer = new QWidget(this);
208     spacer->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Preferred);
209     m_toolbar->addWidget(spacer);
210     m_timePos = new TimecodeDisplay(m_monitorManager->timecode(), this);
211     m_toolbar->addWidget(m_timePos);
212     connect(m_timePos, SIGNAL(timeCodeEditingFinished()), this, SLOT(slotSeek()));
213     m_toolbar->setMaximumHeight(m_timePos->height());
214     layout->addWidget(m_toolbar);
215 }
216
217 Monitor::~Monitor()
218 {
219     delete m_ruler;
220     delete m_timePos;
221     delete m_overlay;
222     if (m_effectWidget)
223         delete m_effectWidget;
224     delete render;
225 }
226
227 QWidget *Monitor::container()
228 {
229     return videoBox;
230 }
231
232 #ifdef USE_OPENGL
233 bool Monitor::createOpenGlWidget(QWidget *parent, const QString &profile)
234 {
235     render = new Render(id(), 0, profile, this);
236     m_glWidget = new VideoGLWidget(parent);
237     if (m_glWidget == NULL) {
238         // Creation failed, we are in trouble...
239         return false;
240     }
241     m_glWidget->setImageAspectRatio(render->dar());
242     m_glWidget->setBackgroundColor(KdenliveSettings::window_background());
243     connect(render, SIGNAL(showImageSignal(QImage)), m_glWidget, SLOT(showImage(QImage)));
244     return true;
245 }
246 #endif
247
248 void Monitor::setupMenu(QMenu *goMenu, QAction *playZone, QAction *loopZone, QMenu *markerMenu, QAction *loopClip)
249 {
250     m_contextMenu = new QMenu(this);
251     m_contextMenu->addMenu(m_playMenu);
252     if (goMenu)
253         m_contextMenu->addMenu(goMenu);
254     if (markerMenu) {
255         m_contextMenu->addMenu(markerMenu);
256         QList <QAction *>list = markerMenu->actions();
257         for (int i = 0; i < list.count(); ++i) {
258             if (list.at(i)->data().toString() == "edit_marker") {
259                 m_editMarker = list.at(i);
260                 break;
261             }
262         }
263     }
264
265     m_playMenu->addAction(playZone);
266     m_playMenu->addAction(loopZone);
267     if (loopClip) {
268         m_loopClipAction = loopClip;
269         m_playMenu->addAction(loopClip);
270     }
271
272     //TODO: add save zone to timeline monitor when fixed
273     if (m_id == Kdenlive::clipMonitor) {
274         m_contextMenu->addMenu(m_markerMenu);
275         m_contextMenu->addAction(KIcon("document-save"), i18n("Save zone"), this, SLOT(slotSaveZone()));
276         QAction *extractZone = m_configMenu->addAction(KIcon("document-new"), i18n("Extract Zone"), this, SLOT(slotExtractCurrentZone()));
277         m_contextMenu->addAction(extractZone);
278     }
279     QAction *extractFrame = m_configMenu->addAction(KIcon("document-new"), i18n("Extract frame"), this, SLOT(slotExtractCurrentFrame()));
280     m_contextMenu->addAction(extractFrame);
281
282     if (m_id != Kdenlive::clipMonitor) {
283         QAction *splitView = m_contextMenu->addAction(KIcon("view-split-left-right"), i18n("Split view"), render, SLOT(slotSplitView(bool)));
284         splitView->setCheckable(true);
285         m_configMenu->addAction(splitView);
286     } else {
287         QAction *setThumbFrame = m_contextMenu->addAction(KIcon("document-new"), i18n("Set current image as thumbnail"), this, SLOT(slotSetThumbFrame()));
288         m_configMenu->addAction(setThumbFrame);
289     }
290
291     QAction *showTips = m_contextMenu->addAction(KIcon("help-hint"), i18n("Monitor overlay infos"));
292     showTips->setCheckable(true);
293     connect(showTips, SIGNAL(toggled(bool)), this, SLOT(slotSwitchMonitorInfo(bool)));
294     showTips->setChecked(KdenliveSettings::displayMonitorInfo());
295
296     QAction *dropFrames = m_contextMenu->addAction(KIcon(), i18n("Real time (drop frames)"));
297     dropFrames->setCheckable(true);
298     dropFrames->setChecked(true);
299     connect(dropFrames, SIGNAL(toggled(bool)), this, SLOT(slotSwitchDropFrames(bool)));
300     
301     m_configMenu->addAction(showTips);
302     m_configMenu->addAction(dropFrames);
303 }
304
305 void Monitor::slotGoToMarker(QAction *action)
306 {
307     int pos = action->data().toInt();
308     slotSeek(pos);
309 }
310
311 void Monitor::slotSetSizeOneToOne()
312 {
313     QRect r = QApplication::desktop()->screenGeometry();
314     const int maxWidth = r.width() - 20;
315     const int maxHeight = r.height() - 20;
316     int width = render->renderWidth();
317     int height = render->renderHeight();
318     kDebug() << "// render info: " << width << "x" << height;
319     while (width >= maxWidth || height >= maxHeight) {
320         width = width * 0.8;
321         height = height * 0.8;
322     }
323     kDebug() << "// MONITOR; set SIZE: " << width << ", " << height;
324     videoBox->setFixedSize(width, height);
325     updateGeometry();
326     adjustSize();
327     //m_ui.video_frame->setMinimumSize(0, 0);
328     emit adjustMonitorSize();
329 }
330
331 void Monitor::slotSetSizeOneToTwo()
332 {
333     QRect r = QApplication::desktop()->screenGeometry();
334     const int maxWidth = r.width() - 20;
335     const int maxHeight = r.height() - 20;
336     int width = render->renderWidth() / 2;
337     int height = render->renderHeight() / 2;
338     kDebug() << "// render info: " << width << "x" << height;
339     while (width >= maxWidth || height >= maxHeight) {
340         width = width * 0.8;
341         height = height * 0.8;
342     }
343     kDebug() << "// MONITOR; set SIZE: " << width << ", " << height;
344     videoBox->setFixedSize(width, height);
345     updateGeometry();
346     adjustSize();
347     //m_ui.video_frame->setMinimumSize(0, 0);
348     emit adjustMonitorSize();
349 }
350
351 void Monitor::resetSize()
352 {
353     videoBox->setMinimumSize(0, 0);
354 }
355
356 QString Monitor::getTimecodeFromFrames(int pos)
357 {
358     return m_monitorManager->timecode().getTimecodeFromFrames(pos);
359 }
360
361 double Monitor::fps() const
362 {
363     return m_monitorManager->timecode().fps();
364 }
365
366 DocClipBase *Monitor::activeClip()
367 {
368     return m_currentClip;
369 }
370
371 void Monitor::updateMarkers(DocClipBase *source)
372 {
373     if (source == m_currentClip && source != NULL) {
374         m_markerMenu->clear();
375         QList <CommentedTime> markers = m_currentClip->commentedSnapMarkers();
376         if (!markers.isEmpty()) {
377             QList <int> marks;
378             for (int i = 0; i < markers.count(); ++i) {
379                 int pos = (int) markers.at(i).time().frames(m_monitorManager->timecode().fps());
380                 marks.append(pos);
381                 QString position = m_monitorManager->timecode().getTimecode(markers.at(i).time()) + ' ' + markers.at(i).comment();
382                 QAction *go = m_markerMenu->addAction(position);
383                 go->setData(pos);
384             }
385         }
386         m_ruler->setMarkers(markers);
387         m_markerMenu->setEnabled(!m_markerMenu->isEmpty());
388     }
389 }
390
391 void Monitor::setMarkers(const QList<CommentedTime> &markers)
392 {
393     m_ruler->setMarkers(markers);
394 }
395
396 void Monitor::slotSeekToPreviousSnap()
397 {
398     if (m_currentClip) slotSeek(getSnapForPos(true).frames(m_monitorManager->timecode().fps()));
399 }
400
401 void Monitor::slotSeekToNextSnap()
402 {
403     if (m_currentClip) slotSeek(getSnapForPos(false).frames(m_monitorManager->timecode().fps()));
404 }
405
406 GenTime Monitor::position()
407 {
408     return render->seekPosition();
409 }
410
411 GenTime Monitor::getSnapForPos(bool previous)
412 {
413     QList <GenTime> snaps;
414     QList < GenTime > markers = m_currentClip->snapMarkers();
415     for (int i = 0; i < markers.size(); ++i) {
416         GenTime t = markers.at(i);
417         snaps.append(t);
418     }
419     QPoint zone = m_ruler->zone();
420     snaps.append(GenTime(zone.x(), m_monitorManager->timecode().fps()));
421     snaps.append(GenTime(zone.y(), m_monitorManager->timecode().fps()));
422     snaps.append(GenTime());
423     snaps.append(m_currentClip->duration());
424     qSort(snaps);
425
426     const GenTime pos = render->seekPosition();
427     for (int i = 0; i < snaps.size(); ++i) {
428         if (previous && snaps.at(i) >= pos) {
429             if (i == 0) i = 1;
430             return snaps.at(i - 1);
431         } else if (!previous && snaps.at(i) > pos) {
432             return snaps.at(i);
433         }
434     }
435     return GenTime();
436 }
437
438 void Monitor::slotZoneMoved(int start, int end)
439 {
440     m_ruler->setZone(start, end);
441     setClipZone(m_ruler->zone());
442     checkOverlay();
443 }
444
445 void Monitor::slotSetZoneStart()
446 {
447     m_ruler->setZoneStart();
448     emit zoneUpdated(m_ruler->zone());
449     setClipZone(m_ruler->zone());
450     checkOverlay();
451 }
452
453 void Monitor::slotSetZoneEnd()
454 {
455     m_ruler->setZoneEnd();
456     emit zoneUpdated(m_ruler->zone());
457     setClipZone(m_ruler->zone());
458     checkOverlay();
459 }
460
461 // virtual
462 void Monitor::mousePressEvent(QMouseEvent * event)
463 {
464     if (render) render->setActiveMonitor();
465     if (event->button() != Qt::RightButton) {
466         if (videoBox->geometry().contains(event->pos()) && (!m_overlay || !m_overlay->underMouse())) {
467             m_dragStarted = true;
468             m_DragStartPosition = event->pos();
469         }
470     } else if (m_contextMenu && (!m_effectWidget || !m_effectWidget->isVisible())) {
471         m_contextMenu->popup(event->globalPos());
472     }
473 }
474
475 void Monitor::resizeEvent(QResizeEvent *event)
476 {
477     Q_UNUSED(event)
478     if (render && isVisible() && isActive()) render->doRefresh();
479 }
480
481 void Monitor::slotSwitchFullScreen()
482 {
483     videoBox->switchFullScreen();
484 }
485
486 // virtual
487 void Monitor::mouseReleaseEvent(QMouseEvent * event)
488 {
489     if (m_dragStarted && event->button() != Qt::RightButton) {
490         if (videoBox->geometry().contains(event->pos()) && (!m_effectWidget || !m_effectWidget->isVisible())) {
491             if (isActive()) slotPlay();
492             else slotActivateMonitor();
493         } //else event->ignore(); //QWidget::mouseReleaseEvent(event);
494     }
495     m_dragStarted = false;
496     event->accept();
497 }
498
499 // virtual
500 void Monitor::mouseMoveEvent(QMouseEvent *event)
501 {
502     if (!m_dragStarted || m_currentClip == NULL) return;
503
504     if ((event->pos() - m_DragStartPosition).manhattanLength()
505             < QApplication::startDragDistance())
506         return;
507
508     {
509         QDrag *drag = new QDrag(this);
510         QMimeData *mimeData = new QMimeData;
511
512         QStringList list;
513         list.append(m_currentClip->getId());
514         QPoint p = m_ruler->zone();
515         list.append(QString::number(p.x()));
516         list.append(QString::number(p.y()));
517         QByteArray data;
518         data.append(list.join(";").toUtf8());
519         mimeData->setData("kdenlive/clip", data);
520         drag->setMimeData(mimeData);
521         /*QPixmap pix = m_currentClip->thumbnail();
522         drag->setPixmap(pix);
523         drag->setHotSpot(QPoint(0, 50));*/
524         drag->start(Qt::MoveAction);
525         /*Qt::DropAction dropAction = drag->exec(Qt::CopyAction | Qt::MoveAction);
526         Qt::DropAction dropAction;
527         dropAction = drag->start(Qt::CopyAction | Qt::MoveAction);*/
528
529         //Qt::DropAction dropAction = drag->exec();
530
531     }
532     //event->accept();
533 }
534
535
536 /*void Monitor::dragMoveEvent(QDragMoveEvent * event) {
537     event->setDropAction(Qt::IgnoreAction);
538     event->setDropAction(Qt::MoveAction);
539     if (event->mimeData()->hasText()) {
540         event->acceptProposedAction();
541     }
542 }
543
544 Qt::DropActions Monitor::supportedDropActions() const {
545     // returns what actions are supported when dropping
546     return Qt::MoveAction;
547 }*/
548
549 QStringList Monitor::mimeTypes() const
550 {
551     QStringList qstrList;
552     // list of accepted mime types for drop
553     qstrList.append("kdenlive/clip");
554     return qstrList;
555 }
556
557 // virtual
558 void Monitor::wheelEvent(QWheelEvent * event)
559 {
560     slotMouseSeek(event->delta(), event->modifiers() == Qt::ControlModifier);
561     event->accept();
562 }
563
564 void Monitor::mouseDoubleClickEvent(QMouseEvent * event)
565 {
566     if (!KdenliveSettings::openglmonitors()) {
567         videoBox->switchFullScreen();
568         event->accept();
569     }
570 }
571
572 void Monitor::slotMouseSeek(int eventDelta, bool fast)
573 {
574     if (fast) {
575         int delta = m_monitorManager->timecode().fps();
576         if (eventDelta > 0) delta = 0 - delta;
577         if (render->requestedSeekPosition != SEEK_INACTIVE)
578             slotSeek(render->requestedSeekPosition - delta);
579         else slotSeek(render->seekFramePosition() - delta);
580     } else {
581         if (eventDelta >= 0) slotForwardOneFrame();
582         else slotRewindOneFrame();
583     }
584 }
585
586 void Monitor::slotSetThumbFrame()
587 {
588     if (m_currentClip == NULL) {
589         return;
590     }
591     m_currentClip->setClipThumbFrame((uint) render->seekFramePosition());
592     emit refreshClipThumbnail(m_currentClip->getId(), true);
593 }
594
595 void Monitor::slotExtractCurrentZone()
596 {
597     if (m_currentClip == NULL) return;
598     emit extractZone(m_currentClip->getId(), m_ruler->zone());
599 }
600
601 void Monitor::slotExtractCurrentFrame()
602 {
603     QImage frame;
604     // check if we are using a proxy
605     if (m_currentClip && !m_currentClip->getProperty("proxy").isEmpty() && m_currentClip->getProperty("proxy") != "-") {
606         // using proxy, use original clip url to get frame
607         frame = render->extractFrame(render->seekFramePosition(), m_currentClip->fileURL().path());
608     }
609     else frame = render->extractFrame(render->seekFramePosition());
610     QPointer<KFileDialog> fs = new KFileDialog(KUrl(), "image/png", this);
611     fs->setOperationMode(KFileDialog::Saving);
612     fs->setMode(KFile::File);
613     fs->setConfirmOverwrite(true);
614     fs->setKeepLocation(true);
615     if (fs->exec()) {
616         QString path = fs->selectedFile();
617         if (!path.isEmpty()) {
618             frame.save(path);
619         }
620     }
621     delete fs;
622 }
623
624 void Monitor::setTimePos(const QString &pos)
625 {
626     m_timePos->setValue(pos);
627     slotSeek();
628 }
629
630 void Monitor::slotSeek()
631 {
632     slotSeek(m_timePos->getValue());
633 }
634
635 void Monitor::slotSeek(int pos)
636 {
637     if (render == NULL) return;
638     slotActivateMonitor();
639     render->seekToFrame(pos);
640     m_ruler->update();
641 }
642
643 void Monitor::checkOverlay()
644 {
645     if (m_overlay == NULL) return;
646     QString overlayText;
647     int pos = m_timePos->getValue();//render->seekFramePosition();
648     QPoint zone = m_ruler->zone();
649     if (pos == zone.x())
650         overlayText = i18n("In Point");
651     else if (pos == zone.y())
652         overlayText = i18n("Out Point");
653     else {
654         if (m_currentClip) {
655             overlayText = m_currentClip->markerComment(GenTime(pos, m_monitorManager->timecode().fps()));
656             if (!overlayText.isEmpty()) {
657                 m_overlay->setOverlayText(overlayText, false);
658                 return;
659             }
660         }
661     }
662     if (m_overlay->isVisible() && overlayText.isEmpty()) m_overlay->setOverlayText(QString(), false);
663     else m_overlay->setOverlayText(overlayText);
664 }
665
666 void Monitor::slotStart()
667 {
668     slotActivateMonitor();
669     render->play(0);
670     render->seekToFrame(0);
671 }
672
673 void Monitor::slotEnd()
674 {
675     slotActivateMonitor();
676     render->play(0);
677     render->seekToFrame(render->getLength());
678 }
679
680 void Monitor::slotZoneStart()
681 {
682     slotActivateMonitor();
683     render->play(0);
684     render->seekToFrame(m_ruler->zone().x());
685 }
686
687 void Monitor::slotZoneEnd()
688 {
689     slotActivateMonitor();
690     render->play(0);
691     render->seekToFrame(m_ruler->zone().y());
692 }
693
694 void Monitor::slotRewind(double speed)
695 {
696     slotActivateMonitor();
697     if (speed == 0) {
698         double currentspeed = render->playSpeed();
699         if (currentspeed >= 0) render->play(-1);
700         else switch((int) currentspeed) {
701             case -1:
702                 render->play(-2);
703                 break;
704             case -2:
705                 render->play(-3);
706                 break;
707             case -3:
708                 render->play(-5);
709                 break;
710             default:
711                 render->play(-8);
712         }
713     } else render->play(speed);
714     //m_playAction->setChecked(true);
715     m_playAction->setIcon(m_pauseIcon);
716 }
717
718 void Monitor::slotForward(double speed)
719 {
720     slotActivateMonitor();
721     if (speed == 0) {
722         double currentspeed = render->playSpeed();
723         if (currentspeed <= 0) render->play(1);
724         else switch((int) currentspeed) {
725             case 1:
726                 render->play(2);
727                 break;
728             case 2:
729                 render->play(3);
730                 break;
731             case 3:
732                 render->play(5);
733                 break;
734             default:
735                 render->play(8);
736         }
737     } else render->play(speed);
738     //m_playAction->setChecked(true);
739     m_playAction->setIcon(m_pauseIcon);
740 }
741
742 void Monitor::slotRewindOneFrame(int diff)
743 {
744     slotActivateMonitor();
745     render->play(0);
746     render->seekToFrameDiff(-diff);
747     m_ruler->update();
748 }
749
750 void Monitor::slotForwardOneFrame(int diff)
751 {
752     slotActivateMonitor();
753     render->play(0);
754     render->seekToFrameDiff(diff);
755     m_ruler->update();
756 }
757
758 void Monitor::seekCursor(int pos)
759 {
760     if (m_ruler->slotNewValue(pos)) {
761         m_timePos->setValue(pos);
762         checkOverlay();
763     }
764 }
765
766 void Monitor::rendererStopped(int pos)
767 {
768     if (m_ruler->slotNewValue(pos)) {
769         m_timePos->setValue(pos);
770         checkOverlay();
771     }
772     m_playAction->setIcon(m_playIcon);
773 }
774
775 void Monitor::adjustRulerSize(int length)
776 {
777     if (length > 0) m_length = length;
778     m_ruler->adjustScale(m_length);
779     if (m_currentClip != NULL) {
780         QPoint zone = m_currentClip->zone();
781         m_ruler->setZone(zone.x(), zone.y());
782     }
783 }
784
785 void Monitor::stop()
786 {
787     if (render) render->stop();
788 }
789
790 void Monitor::start()
791 {
792     if (!isVisible() || !isActive()) return;
793 #ifdef USE_OPENGL    
794     if (m_glWidget) m_glWidget->activateMonitor();
795 #endif
796     if (render) render->startConsumer();
797 }
798
799 void Monitor::refreshMonitor(bool visible)
800 {
801     if (visible && render) {
802         if (!slotActivateMonitor()) {
803             // the monitor was already active, simply refreshClipThumbnail
804             render->doRefresh();
805         }
806     }
807 }
808
809 void Monitor::refreshMonitor()
810 {
811     if (isActive()) {
812         render->doRefresh();
813     }
814 }
815
816 void Monitor::pause()
817 {
818     if (render == NULL) return;
819     slotActivateMonitor();
820     render->pause();
821     //m_playAction->setChecked(true);
822     m_playAction->setIcon(m_playIcon);
823 }
824
825 void Monitor::unpause()
826 {
827 }
828
829 void Monitor::slotPlay()
830 {
831     if (render == NULL) return;
832     slotActivateMonitor();
833     if (render->isPlaying()) {
834         m_playAction->setIcon(m_playIcon);
835         render->switchPlay(false);
836     }
837     else {
838         m_playAction->setIcon(m_pauseIcon);
839         render->switchPlay(true);
840     }
841     m_ruler->refreshRuler();
842 }
843
844 void Monitor::slotPlayZone()
845 {
846     if (render == NULL) return;
847     slotActivateMonitor();
848     QPoint p = m_ruler->zone();
849     render->playZone(GenTime(p.x(), m_monitorManager->timecode().fps()), GenTime(p.y(), m_monitorManager->timecode().fps()));
850     //m_playAction->setChecked(true);
851     m_playAction->setIcon(m_pauseIcon);
852 }
853
854 void Monitor::slotLoopZone()
855 {
856     if (render == NULL) return;
857     slotActivateMonitor();
858     QPoint p = m_ruler->zone();
859     render->loopZone(GenTime(p.x(), m_monitorManager->timecode().fps()), GenTime(p.y(), m_monitorManager->timecode().fps()));
860     //m_playAction->setChecked(true);
861     m_playAction->setIcon(m_pauseIcon);
862 }
863
864 void Monitor::slotLoopClip()
865 {
866     if (render == NULL || m_selectedClip == NULL)
867         return;
868     slotActivateMonitor();
869     render->loopZone(m_selectedClip->startPos(), m_selectedClip->endPos());
870     //m_playAction->setChecked(true);
871     m_playAction->setIcon(m_pauseIcon);
872 }
873
874 void Monitor::updateClipProducer(Mlt::Producer *prod)
875 {
876     if (render == NULL) return;
877     render->setProducer(prod, render->seekFramePosition());
878 }
879
880 void Monitor::slotSetClipProducer(DocClipBase *clip, QPoint zone, bool forceUpdate, int position)
881 {
882     if (render == NULL) return;
883     if (clip == NULL && m_currentClip != NULL) {
884         m_currentClip->lastSeekPosition = render->seekFramePosition();
885         m_currentClip = NULL;
886         m_length = -1;
887         render->setProducer(NULL, -1);
888         return;
889     }
890
891     if (clip != m_currentClip || forceUpdate) {
892         if (m_currentClip) m_currentClip->lastSeekPosition = render->seekFramePosition();
893         m_currentClip = clip;
894         if (position == -1) position = clip->lastSeekPosition;
895         updateMarkers(clip);
896         Mlt::Producer *prod = NULL;
897         if (clip) prod = clip->getCloneProducer();
898         if (render->setProducer(prod, position) == -1) {
899             // MLT CONSUMER is broken
900             kDebug(QtWarningMsg) << "ERROR, Cannot start monitor";
901         } else start();
902     } else {
903         if (m_currentClip) {
904             slotActivateMonitor();
905             if (position == -1) position = render->seekFramePosition();
906             render->seek(position);
907             if (zone.isNull()) {
908                 zone = m_currentClip->zone();
909                 m_ruler->setZone(zone.x(), zone.y());
910                 return;
911             }
912         }
913     }
914     if (!zone.isNull()) {
915         m_ruler->setZone(zone.x(), zone.y());
916         render->seek(zone.x());
917     }
918 }
919
920 void Monitor::slotOpenFile(const QString &file)
921 {
922     if (render == NULL) return;
923     slotActivateMonitor();
924     render->loadUrl(file);
925 }
926
927 void Monitor::slotSaveZone()
928 {
929     if (render == NULL) return;
930     emit saveZone(render, m_ruler->zone(), m_currentClip);
931
932     //render->setSceneList(doc, 0);
933 }
934
935 void Monitor::setCustomProfile(const QString &profile, const Timecode &tc)
936 {
937     m_timePos->updateTimeCode(tc);
938     if (render == NULL) return;
939     if (!render->hasProfile(profile)) {
940         slotActivateMonitor();
941         render->resetProfile(profile);
942 #ifdef USE_OPENGL    
943         if (m_glWidget) m_glWidget->setImageAspectRatio(render->dar());
944 #endif
945     }
946 }
947
948 void Monitor::resetProfile(const QString &profile)
949 {
950     m_timePos->updateTimeCode(m_monitorManager->timecode());
951     if (render == NULL) return;
952     if (!render->hasProfile(profile)) {
953         slotActivateMonitor();
954         render->resetProfile(profile);
955 #ifdef USE_OPENGL
956         if (m_glWidget) m_glWidget->setImageAspectRatio(render->dar());
957 #endif
958     }
959     if (m_effectWidget)
960         m_effectWidget->resetProfile(render);
961 }
962
963 void Monitor::saveSceneList(const QString &path, const QDomElement &info)
964 {
965     if (render == NULL) return;
966     render->saveSceneList(path, info);
967 }
968
969 const QString Monitor::sceneList()
970 {
971     if (render == NULL) return QString();
972     return render->sceneList();
973 }
974
975 void Monitor::setClipZone(const QPoint &pos)
976 {
977     if (m_currentClip == NULL) return;
978     m_currentClip->setZone(pos);
979 }
980
981 void Monitor::slotSwitchDropFrames(bool show)
982 {
983     render->setDropFrames(show);
984 }
985
986 void Monitor::slotSwitchMonitorInfo(bool show)
987 {
988     KdenliveSettings::setDisplayMonitorInfo(show);
989     if (show) {
990         if (m_overlay) return;
991         if (videoSurface == NULL) {
992             // Using OpenGL display
993 #ifdef USE_OPENGL
994             if (m_glWidget->layout()) delete m_glWidget->layout();
995             m_overlay = new Overlay();
996             connect(m_overlay, SIGNAL(editMarker()), this, SLOT(slotEditMarker()));
997             QVBoxLayout *layout = new QVBoxLayout;
998             layout->addStretch(10);
999             layout->addWidget(m_overlay);
1000             m_glWidget->setLayout(layout);
1001 #endif
1002         } else {
1003             if (videoSurface->layout()) delete videoSurface->layout();
1004             m_overlay = new Overlay();
1005             connect(m_overlay, SIGNAL(editMarker()), this, SLOT(slotEditMarker()));
1006             QVBoxLayout *layout = new QVBoxLayout;
1007             layout->addStretch(10);
1008             layout->addWidget(m_overlay);
1009             videoSurface->setLayout(layout);
1010             m_overlay->raise();
1011             m_overlay->setHidden(true);
1012         }
1013         checkOverlay();
1014     } else {
1015         delete m_overlay;
1016         m_overlay = NULL;
1017     }
1018 }
1019
1020 void Monitor::slotEditMarker()
1021 {
1022     if (m_editMarker) m_editMarker->trigger();
1023 }
1024
1025 void Monitor::updateTimecodeFormat()
1026 {
1027     m_timePos->slotUpdateTimeCodeFormat();
1028 }
1029
1030 QStringList Monitor::getZoneInfo() const
1031 {
1032     QStringList result;
1033     if (m_currentClip == NULL) return result;
1034     result << m_currentClip->getId();
1035     QPoint zone = m_ruler->zone();
1036     result << QString::number(zone.x()) << QString::number(zone.y());
1037     return result;
1038 }
1039
1040 void Monitor::slotSetSelectedClip(AbstractClipItem* item)
1041 {
1042     if (item) {
1043         if (m_loopClipAction) m_loopClipAction->setEnabled(true);
1044         m_selectedClip = item;
1045     } else {
1046         if (m_loopClipAction) m_loopClipAction->setEnabled(false);
1047     }
1048 }
1049
1050 void Monitor::slotSetSelectedClip(ClipItem* item)
1051 {
1052     if (item || (!item && !m_loopClipTransition)) {
1053         m_loopClipTransition = false;
1054         slotSetSelectedClip((AbstractClipItem*)item);
1055     }
1056 }
1057
1058 void Monitor::slotSetSelectedClip(Transition* item)
1059 {
1060     if (item || (!item && m_loopClipTransition)) {
1061         m_loopClipTransition = true;
1062         slotSetSelectedClip((AbstractClipItem*)item);
1063     }
1064 }
1065
1066
1067 void Monitor::slotShowEffectScene(bool show, bool manuallyTriggered)
1068 {
1069     if (m_id == Kdenlive::projectMonitor) {
1070         if (!m_effectWidget->getVisibilityAction()->isChecked())
1071             show = false;
1072         if (m_effectWidget->isVisible() == show)
1073             return;
1074         setUpdatesEnabled(false);
1075         if (show) {
1076             if (videoSurface) {
1077                 videoSurface->setVisible(false);
1078                 // Preview is handeled internally through the Render::showFrame method
1079                 render->disablePreview(true);
1080 #ifdef USE_OPENGL
1081             } else {
1082                 m_glWidget->setVisible(false);
1083 #endif
1084             }
1085             m_effectWidget->setVisible(true);
1086             m_effectWidget->getScene()->slotZoomFit();
1087             emit requestFrameForAnalysis(true);
1088         } else {    
1089             m_effectWidget->setVisible(false);
1090             emit requestFrameForAnalysis(false);
1091             if (videoSurface) {
1092                 videoSurface->setVisible(true);
1093                 // Preview is handeled internally through the Render::showFrame method
1094                 render->disablePreview(false);
1095             
1096 #ifdef USE_OPENGL
1097             } else {
1098                 m_glWidget->setVisible(true);
1099 #endif
1100             }
1101         }
1102         if (!manuallyTriggered)
1103             m_effectWidget->showVisibilityButton(show);
1104         setUpdatesEnabled(true);
1105         videoBox->setEnabled(show);
1106         //render->doRefresh();
1107     }
1108 }
1109
1110 MonitorEditWidget* Monitor::getEffectEdit()
1111 {
1112     return m_effectWidget;
1113 }
1114
1115 bool Monitor::effectSceneDisplayed()
1116 {
1117     return m_effectWidget->isVisible();
1118 }
1119
1120 void Monitor::slotSetVolume(int volume)
1121 {
1122     KdenliveSettings::setVolume(volume);
1123     KIcon icon;
1124     if (volume == 0) icon = KIcon("audio-volume-muted");
1125     else icon = KIcon("audio-volume-medium");
1126     static_cast <QToolButton *>(m_volumeWidget)->setIcon(icon);
1127     render->slotSetVolume(volume);
1128 }
1129
1130 void Monitor::slotShowVolume()
1131 {
1132     m_volumePopup->move(mapToGlobal(m_toolbar->geometry().topLeft()) + QPoint(mapToParent(m_volumeWidget->geometry().bottomLeft()).x(), -m_volumePopup->height()));
1133     int vol = render->volume();
1134     // Disable widget if we cannot get the volume
1135     m_volumePopup->setEnabled(vol != -1);
1136     m_audioSlider->blockSignals(true);
1137     m_audioSlider->setValue(vol);
1138     m_audioSlider->blockSignals(false);
1139     m_volumePopup->show();
1140 }
1141
1142 AbstractRender *Monitor::abstractRender()
1143 {
1144     return render;
1145 }
1146
1147 void Monitor::reloadProducer(const QString &id)
1148 {
1149     if (!m_currentClip) return;
1150     if (m_currentClip->getId() == id)
1151         slotSetClipProducer(m_currentClip, m_currentClip->zone(), true);
1152 }
1153
1154 QString Monitor::getMarkerThumb(GenTime pos)
1155 {
1156     if (!m_currentClip) return QString();
1157     if (!m_currentClip->getClipHash().isEmpty()) {
1158         QString url = m_monitorManager->getProjectFolder() + "thumbs/" + m_currentClip->getClipHash() + '#' + QString::number((int) pos.frames(m_monitorManager->timecode().fps())) + ".png";
1159         if (QFile::exists(url)) return url;
1160     }
1161     return QString();
1162 }
1163
1164 void Monitor::setPalette ( const QPalette & p)
1165 {
1166     QWidget::setPalette(p);
1167     if (m_ruler) m_ruler->updatePalette();
1168     
1169 }
1170
1171 Overlay::Overlay(QWidget* parent) :
1172     QLabel(parent)
1173 {
1174     //setAttribute(Qt::WA_TransparentForMouseEvents);
1175     setAutoFillBackground(true);
1176     setBackgroundRole(QPalette::Base);
1177     setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
1178     setCursor(Qt::PointingHandCursor);
1179
1180 }
1181
1182 // virtual
1183 void Overlay::mouseReleaseEvent ( QMouseEvent * event )
1184 {
1185     event->ignore();
1186 }
1187
1188 // virtual
1189 void Overlay::mousePressEvent( QMouseEvent * event )
1190 {
1191     event->ignore();
1192 }
1193
1194 // virtual
1195 void Overlay::mouseDoubleClickEvent ( QMouseEvent * event )
1196 {
1197     emit editMarker();
1198     event->ignore();
1199 }
1200
1201 void Overlay::setOverlayText(const QString &text, bool isZone)
1202 {
1203     if (text.isEmpty()) {
1204         /*QPalette p;
1205         p.setColor(QPalette::Base, KdenliveSettings::window_background());
1206         setPalette(p);
1207         setText(QString());
1208         repaint();*/
1209         setHidden(true);
1210         return;
1211     }
1212     setHidden(true);
1213     QPalette p;
1214     p.setColor(QPalette::Text, Qt::white);
1215     if (isZone) p.setColor(QPalette::Base, QColor(200, 0, 0));
1216     else p.setColor(QPalette::Base, QColor(0, 0, 200));
1217     setPalette(p);
1218     setText(' ' + text + ' ');
1219     setHidden(false);
1220 }
1221
1222
1223 #include "monitor.moc"