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