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