]> git.sesse.net Git - kdenlive/blob - src/monitor.cpp
fixed gl widget on mac (did not resize anymore)
[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 "renderer.h"
23 #include "monitormanager.h"
24 #include "smallruler.h"
25 #include "docclipbase.h"
26 #include "monitorscene.h"
27 #include "abstractclipitem.h"
28 #include "kdenlivesettings.h"
29
30 #include <KDebug>
31 #include <KLocale>
32 #include <KFileDialog>
33 #include <KApplication>
34 #include <KMessageBox>
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 <QGraphicsView>
46
47
48 Monitor::Monitor(QString name, MonitorManager *manager, QString profile, QWidget *parent) :
49     QWidget(parent),
50     render(NULL),
51     m_name(name),
52     m_monitorManager(manager),
53     m_currentClip(NULL),
54     m_ruler(new SmallRuler(m_monitorManager)),
55     m_overlay(NULL),
56     m_isActive(false),
57     m_scale(1),
58     m_length(0),
59     m_dragStarted(false),
60     m_monitorRefresh(NULL),
61     m_effectScene(NULL),
62     m_effectView(NULL),
63     m_selectedClip(NULL),
64     m_loopClipTransition(true)
65
66 {
67     QVBoxLayout *layout = new QVBoxLayout;
68     layout->setContentsMargins(0, 0, 0, 0);
69     layout->setSpacing(0);
70
71     // Video widget holder
72     m_videoBox = new VideoContainer(this);
73     m_videoBox->setContentsMargins(0, 0, 0, 0);
74     m_videoBox->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
75     layout->addWidget(m_videoBox, 10);
76     layout->addStretch();
77
78     // Get base size for icons
79     int s = style()->pixelMetric(QStyle::PM_SmallIconSize);
80
81     // Monitor ruler
82     layout->addWidget(m_ruler);
83     // Tool bar buttons
84     m_toolbar = new QToolBar(name, this);
85     m_toolbar->setIconSize(QSize(s, s));
86
87     m_playIcon = KIcon("media-playback-start");
88     m_pauseIcon = KIcon("media-playback-pause");
89
90     if (name != "chapter") {
91         m_toolbar->addAction(KIcon("kdenlive-zone-start"), i18n("Set zone start"), this, SLOT(slotSetZoneStart()));
92         m_toolbar->addAction(KIcon("kdenlive-zone-end"), i18n("Set zone end"), this, SLOT(slotSetZoneEnd()));
93     } else {
94         m_ruler->setZone(-3, -2);
95     }
96
97     m_toolbar->addAction(KIcon("media-seek-backward"), i18n("Rewind"), this, SLOT(slotRewind()));
98     //m_toolbar->addAction(KIcon("media-skip-backward"), i18n("Rewind 1 frame"), this, SLOT(slotRewindOneFrame()));
99
100     QToolButton *playButton = new QToolButton(m_toolbar);
101     m_playMenu = new QMenu(i18n("Play..."), this);
102     m_playAction = m_playMenu->addAction(m_playIcon, i18n("Play"));
103     //m_playAction->setCheckable(true);
104     connect(m_playAction, SIGNAL(triggered()), this, SLOT(slotPlay()));
105
106     playButton->setMenu(m_playMenu);
107     playButton->setPopupMode(QToolButton::MenuButtonPopup);
108     m_toolbar->addWidget(playButton);
109
110     //m_toolbar->addAction(KIcon("media-skip-forward"), i18n("Forward 1 frame"), this, SLOT(slotForwardOneFrame()));
111     m_toolbar->addAction(KIcon("media-seek-forward"), i18n("Forward"), this, SLOT(slotForward()));
112
113     playButton->setDefaultAction(m_playAction);
114
115     if (name != "chapter") {
116         QToolButton *configButton = new QToolButton(m_toolbar);
117         m_configMenu = new QMenu(i18n("Misc..."), this);
118         configButton->setIcon(KIcon("system-run"));
119         configButton->setMenu(m_configMenu);
120         configButton->setPopupMode(QToolButton::QToolButton::InstantPopup);
121         m_toolbar->addWidget(configButton);
122
123         if (name == "clip") {
124             m_markerMenu = new QMenu(i18n("Go to marker..."), this);
125             m_markerMenu->setEnabled(false);
126             m_configMenu->addMenu(m_markerMenu);
127             connect(m_markerMenu, SIGNAL(triggered(QAction *)), this, SLOT(slotGoToMarker(QAction *)));
128         }
129         m_configMenu->addAction(KIcon("transform-scale"), i18n("Resize (100%)"), this, SLOT(slotSetSizeOneToOne()));
130         m_configMenu->addAction(KIcon("transform-scale"), i18n("Resize (50%)"), this, SLOT(slotSetSizeOneToTwo()));
131     }
132
133     // Create Volume slider popup
134     m_volumePopup = new QFrame(this, Qt::Popup);
135     QVBoxLayout *poplayout = new QVBoxLayout;
136     poplayout->setContentsMargins(0, 0, 0, 0);
137     m_audioSlider = new QSlider(Qt::Vertical);
138     m_audioSlider->setRange(0, 100);
139     poplayout->addWidget(m_audioSlider);
140     m_volumePopup->setLayout(poplayout);
141     KIcon icon;
142     if (KdenliveSettings::volume() == 0) icon = KIcon("audio-volume-muted");
143     else icon = KIcon("audio-volume-medium");
144
145     m_volumeWidget = m_toolbar->widgetForAction(m_toolbar->addAction(icon, i18n("Audio volume"), this, SLOT(slotShowVolume())));
146
147     // we need to show / hide the popup once so that it's geometry can be calculated in slotShowVolume
148     m_volumePopup->show();
149     m_volumePopup->hide();
150
151     QWidget *spacer = new QWidget(this);
152     spacer->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
153     m_toolbar->addWidget(spacer);
154     m_timePos = new TimecodeDisplay(m_monitorManager->timecode(), this);
155     m_toolbar->addWidget(m_timePos);
156     connect(m_timePos, SIGNAL(editingFinished()), this, SLOT(slotSeek()));
157     m_toolbar->setMaximumHeight(s * 1.5);
158     layout->addWidget(m_toolbar);
159
160     setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
161     setLayout(layout);
162     setMinimumHeight(200);
163
164     if (profile.isEmpty()) profile = KdenliveSettings::current_profile();
165
166     bool monitorCreated = false;
167 #ifdef Q_WS_MAC
168     createOpenGlWidget(m_videoBox, profile);
169     monitorCreated = true;
170     //m_glWidget->setFixedSize(width, height);
171 #elif defined (USE_OPEN_GL)
172     if (KdenliveSettings::openglmonitors()) {
173         monitorCreated = createOpenGlWidget(m_videoBox, profile);
174     }
175 #endif
176     QVBoxLayout *lay = new QVBoxLayout;
177     lay->setContentsMargins(0, 0, 0, 0);
178     if (!monitorCreated) {
179         m_monitorRefresh = new MonitorRefresh;
180         lay->addWidget(m_monitorRefresh);
181         m_videoBox->setLayout(lay);
182         render = new Render(m_name, (int) m_monitorRefresh->winId(), profile, this);
183         m_monitorRefresh->setRenderer(render);
184     }else if (m_glWidget) {
185         lay->addWidget(m_glWidget);
186         m_videoBox->setLayout(lay);
187     }
188
189     connect(m_audioSlider, SIGNAL(valueChanged(int)), this, SLOT(slotSetVolume(int)));
190     connect(m_ruler, SIGNAL(seekRenderer(int)), this, SLOT(slotSeek(int)));
191     connect(render, SIGNAL(durationChanged(int)), this, SLOT(adjustRulerSize(int)));
192     connect(render, SIGNAL(rendererStopped(int)), this, SLOT(rendererStopped(int)));
193
194     //render->createVideoXWindow(m_ui.video_frame->winId(), -1);
195
196     if (name != "clip") {
197         connect(render, SIGNAL(rendererPosition(int)), this, SIGNAL(renderPosition(int)));
198         connect(render, SIGNAL(durationChanged(int)), this, SIGNAL(durationChanged(int)));
199         connect(m_ruler, SIGNAL(zoneChanged(QPoint)), this, SIGNAL(zoneUpdated(QPoint)));
200     } else {
201         connect(m_ruler, SIGNAL(zoneChanged(QPoint)), this, SLOT(setClipZone(QPoint)));
202     }
203
204     if (m_monitorRefresh) m_monitorRefresh->show();
205
206     if (name == "project") {
207         m_effectScene = new MonitorScene(render);
208         m_effectView = new QGraphicsView(m_effectScene, m_videoBox);
209         lay->addWidget(m_effectView);
210         m_effectView->setRenderHints(QFlags<QPainter::RenderHint>());
211         m_effectView->scale(((double) render->renderWidth()) / render->frameRenderWidth(), 1.0);
212         m_effectView->setMouseTracking(true);
213         m_effectScene->setUp();
214         m_effectView->hide();
215     }
216 }
217
218 Monitor::~Monitor()
219 {
220     delete m_ruler;
221     delete m_timePos;
222     delete m_overlay;
223     if (m_name == "project") {
224         delete m_effectView;
225         delete m_effectScene;
226     }
227     delete m_monitorRefresh;
228     delete render;
229 }
230
231 QWidget *Monitor::container()
232 {
233     return m_videoBox;
234 }
235
236 QString Monitor::name() const
237 {
238     return m_name;
239 }
240
241 #if defined(Q_WS_MAC) || defined(USE_OPEN_GL)
242 bool Monitor::createOpenGlWidget(QWidget *parent, const QString profile)
243 {
244     render = new Render(m_name, 0, profile, this);
245     m_glWidget = new VideoGLWidget(parent);
246     if (m_glWidget == NULL) {
247         // Creation failed, we are in trouble...
248         return false;
249     }
250     m_glWidget->setImageAspectRatio(render->dar());
251     m_glWidget->setBackgroundColor(KdenliveSettings::window_background());
252     connect(render, SIGNAL(showImageSignal(QImage)), m_glWidget, SLOT(showImage(QImage)));
253     return true;
254 }
255 #endif
256
257 void Monitor::setupMenu(QMenu *goMenu, QAction *playZone, QAction *loopZone, QMenu *markerMenu, QAction *loopClip)
258 {
259     m_contextMenu = new QMenu(this);
260     m_contextMenu->addMenu(m_playMenu);
261     if (goMenu)
262         m_contextMenu->addMenu(goMenu);
263     if (markerMenu)
264         m_contextMenu->addMenu(markerMenu);
265
266     m_playMenu->addAction(playZone);
267     m_playMenu->addAction(loopZone);
268     if (loopClip) {
269         m_loopClipAction = loopClip;
270         m_playMenu->addAction(loopClip);
271     }
272
273     //TODO: add save zone to timeline monitor when fixed
274     if (m_name == "clip") {
275         m_contextMenu->addMenu(m_markerMenu);
276         m_contextMenu->addAction(KIcon("document-save"), i18n("Save zone"), this, SLOT(slotSaveZone()));
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_name != "clip") {
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
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     m_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     m_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     m_videoBox->setMinimumSize(0, 0);
354 }
355
356 DocClipBase *Monitor::activeClip()
357 {
358     return m_currentClip;
359 }
360
361 void Monitor::updateMarkers(DocClipBase *source)
362 {
363     if (source == m_currentClip && source != NULL) {
364         m_markerMenu->clear();
365         QList <CommentedTime> markers = m_currentClip->commentedSnapMarkers();
366         if (!markers.isEmpty()) {
367             QList <int> marks;
368             for (int i = 0; i < markers.count(); i++) {
369                 int pos = (int) markers.at(i).time().frames(m_monitorManager->timecode().fps());
370                 marks.append(pos);
371                 QString position = m_monitorManager->timecode().getTimecode(markers.at(i).time()) + ' ' + markers.at(i).comment();
372                 QAction *go = m_markerMenu->addAction(position);
373                 go->setData(pos);
374             }
375             m_ruler->setMarkers(marks);
376         } else m_ruler->setMarkers(QList <int>());
377         m_markerMenu->setEnabled(!m_markerMenu->isEmpty());
378     }
379 }
380
381 void Monitor::slotSeekToPreviousSnap()
382 {
383     if (m_currentClip) slotSeek(getSnapForPos(true).frames(m_monitorManager->timecode().fps()));
384 }
385
386 void Monitor::slotSeekToNextSnap()
387 {
388     if (m_currentClip) slotSeek(getSnapForPos(false).frames(m_monitorManager->timecode().fps()));
389 }
390
391 GenTime Monitor::position()
392 {
393     return render->seekPosition();
394 }
395
396 GenTime Monitor::getSnapForPos(bool previous)
397 {
398     QList <GenTime> snaps;
399     QList < GenTime > markers = m_currentClip->snapMarkers();
400     for (int i = 0; i < markers.size(); ++i) {
401         GenTime t = markers.at(i);
402         snaps.append(t);
403     }
404     QPoint zone = m_ruler->zone();
405     snaps.append(GenTime(zone.x(), m_monitorManager->timecode().fps()));
406     snaps.append(GenTime(zone.y(), m_monitorManager->timecode().fps()));
407     snaps.append(GenTime());
408     snaps.append(m_currentClip->duration());
409     qSort(snaps);
410
411     const GenTime pos = render->seekPosition();
412     for (int i = 0; i < snaps.size(); ++i) {
413         if (previous && snaps.at(i) >= pos) {
414             if (i == 0) i = 1;
415             return snaps.at(i - 1);
416         } else if (!previous && snaps.at(i) > pos) {
417             return snaps.at(i);
418         }
419     }
420     return GenTime();
421 }
422
423 void Monitor::slotZoneMoved(int start, int end)
424 {
425     m_ruler->setZone(start, end);
426     checkOverlay();
427     setClipZone(m_ruler->zone());
428 }
429
430 void Monitor::slotSetZoneStart()
431 {
432     m_ruler->setZone(render->seekFramePosition(), -1);
433     emit zoneUpdated(m_ruler->zone());
434     checkOverlay();
435     setClipZone(m_ruler->zone());
436 }
437
438 void Monitor::slotSetZoneEnd()
439 {
440     m_ruler->setZone(-1, render->seekFramePosition());
441     emit zoneUpdated(m_ruler->zone());
442     checkOverlay();
443     setClipZone(m_ruler->zone());
444 }
445
446 // virtual
447 void Monitor::mousePressEvent(QMouseEvent * event)
448 {
449     if (event->button() != Qt::RightButton) {
450         if (m_videoBox->underMouse()) {
451             m_dragStarted = true;
452             m_DragStartPosition = event->pos();
453         }
454     } else m_contextMenu->popup(event->globalPos());
455 }
456
457 void Monitor::slotSwitchFullScreen()
458 {
459     m_videoBox->switchFullScreen();
460 }
461
462 // virtual
463 void Monitor::mouseReleaseEvent(QMouseEvent * event)
464 {
465     if (m_dragStarted) {
466         if (m_videoBox->underMouse() && (!m_effectView || !m_effectView->isVisible())) {
467             if (isActive()) slotPlay();
468             else activateMonitor();
469         } else QWidget::mouseReleaseEvent(event);
470         m_dragStarted = false;
471     }
472 }
473
474 // virtual
475 void Monitor::mouseMoveEvent(QMouseEvent *event)
476 {
477     // kDebug() << "// DRAG STARTED, MOUSE MOVED: ";
478     if (!m_dragStarted || m_currentClip == NULL) return;
479
480     if ((event->pos() - m_DragStartPosition).manhattanLength()
481             < QApplication::startDragDistance())
482         return;
483
484     {
485         QDrag *drag = new QDrag(this);
486         QMimeData *mimeData = new QMimeData;
487
488         QStringList list;
489         list.append(m_currentClip->getId());
490         QPoint p = m_ruler->zone();
491         list.append(QString::number(p.x()));
492         list.append(QString::number(p.y()));
493         QByteArray data;
494         data.append(list.join(";").toUtf8());
495         mimeData->setData("kdenlive/clip", data);
496         drag->setMimeData(mimeData);
497         QPixmap pix = m_currentClip->thumbnail();
498         drag->setPixmap(pix);
499         drag->setHotSpot(QPoint(0, 50));
500         drag->start(Qt::MoveAction);
501
502         //Qt::DropAction dropAction;
503         //dropAction = drag->start(Qt::CopyAction | Qt::MoveAction);
504
505         //Qt::DropAction dropAction = drag->exec();
506
507     }
508     //event->accept();
509 }
510
511 /*void Monitor::dragMoveEvent(QDragMoveEvent * event) {
512     event->setDropAction(Qt::IgnoreAction);
513     event->setDropAction(Qt::MoveAction);
514     if (event->mimeData()->hasText()) {
515         event->acceptProposedAction();
516     }
517 }
518
519 Qt::DropActions Monitor::supportedDropActions() const {
520     // returns what actions are supported when dropping
521     return Qt::MoveAction;
522 }*/
523
524 QStringList Monitor::mimeTypes() const
525 {
526     QStringList qstrList;
527     // list of accepted mime types for drop
528     qstrList.append("kdenlive/clip");
529     return qstrList;
530 }
531
532 // virtual
533 void Monitor::wheelEvent(QWheelEvent * event)
534 {
535     slotMouseSeek(event->delta(), event->modifiers() == Qt::ControlModifier);
536     event->accept();
537 }
538
539 void Monitor::slotMouseSeek(int eventDelta, bool fast)
540 {
541     if (fast) {
542         int delta = m_monitorManager->timecode().fps();
543         if (eventDelta > 0) delta = 0 - delta;
544         slotSeek(render->seekFramePosition() - delta);
545     } else {
546         if (eventDelta >= 0) slotForwardOneFrame();
547         else slotRewindOneFrame();
548     }
549 }
550
551 void Monitor::slotSetThumbFrame()
552 {
553     if (m_currentClip == NULL) {
554         return;
555     }
556     m_currentClip->setClipThumbFrame((uint) render->seekFramePosition());
557     emit refreshClipThumbnail(m_currentClip->getId());
558 }
559
560 void Monitor::slotExtractCurrentFrame()
561 {
562     QImage frame = render->extractFrame(render->seekFramePosition());
563     KFileDialog *fs = new KFileDialog(KUrl(), "image/png", this);
564     fs->setOperationMode(KFileDialog::Saving);
565     fs->setMode(KFile::File);
566 #if KDE_IS_VERSION(4,2,0)
567     fs->setConfirmOverwrite(true);
568 #endif
569     fs->setKeepLocation(true);
570     fs->exec();
571     QString path = fs->selectedFile();
572     delete fs;
573     if (!path.isEmpty()) {
574         frame.save(path);
575     }
576 }
577
578 bool Monitor::isActive() const
579 {
580     return m_isActive;
581 }
582
583 void Monitor::activateMonitor()
584 {
585     if (!m_isActive) {
586         m_monitorManager->slotSwitchMonitors(m_name == "clip");
587     }
588 }
589
590 void Monitor::setTimePos(const QString &pos)
591 {
592     m_timePos->setValue(pos);
593     slotSeek();
594 }
595
596 void Monitor::slotSeek()
597 {
598     slotSeek(m_timePos->getValue());
599 }
600
601 void Monitor::slotSeek(int pos)
602 {
603     activateMonitor();
604     if (render == NULL) return;
605     render->seekToFrame(pos);
606 }
607
608 void Monitor::checkOverlay()
609 {
610     if (m_overlay == NULL) return;
611     int pos = render->seekFramePosition();
612     QPoint zone = m_ruler->zone();
613     if (pos == zone.x())
614         m_overlay->setOverlayText(i18n("In Point"));
615     else if (pos == zone.y())
616         m_overlay->setOverlayText(i18n("Out Point"));
617     else {
618         if (m_currentClip) {
619             QString markerComment = m_currentClip->markerComment(GenTime(pos, m_monitorManager->timecode().fps()));
620             if (markerComment.isEmpty())
621                 m_overlay->setHidden(true);
622             else
623                 m_overlay->setOverlayText(markerComment, false);
624         } else m_overlay->setHidden(true);
625     }
626 }
627
628 void Monitor::slotStart()
629 {
630     activateMonitor();
631     render->play(0);
632     render->seekToFrame(0);
633 }
634
635 void Monitor::slotEnd()
636 {
637     activateMonitor();
638     render->play(0);
639     render->seekToFrame(render->getLength());
640 }
641
642 void Monitor::slotZoneStart()
643 {
644     activateMonitor();
645     render->play(0);
646     render->seekToFrame(m_ruler->zone().x());
647 }
648
649 void Monitor::slotZoneEnd()
650 {
651     activateMonitor();
652     render->play(0);
653     render->seekToFrame(m_ruler->zone().y());
654 }
655
656 void Monitor::slotRewind(double speed)
657 {
658     activateMonitor();
659     if (speed == 0) {
660         double currentspeed = render->playSpeed();
661         if (currentspeed >= 0) render->play(-2);
662         else render->play(currentspeed * 2);
663     } else render->play(speed);
664     //m_playAction->setChecked(true);
665     m_playAction->setIcon(m_pauseIcon);
666 }
667
668 void Monitor::slotForward(double speed)
669 {
670     activateMonitor();
671     if (speed == 0) {
672         double currentspeed = render->playSpeed();
673         if (currentspeed <= 1) render->play(2);
674         else render->play(currentspeed * 2);
675     } else render->play(speed);
676     //m_playAction->setChecked(true);
677     m_playAction->setIcon(m_pauseIcon);
678 }
679
680 void Monitor::slotRewindOneFrame(int diff)
681 {
682     activateMonitor();
683     render->play(0);
684     render->seekToFrameDiff(-diff);
685 }
686
687 void Monitor::slotForwardOneFrame(int diff)
688 {
689     activateMonitor();
690     render->play(0);
691     render->seekToFrameDiff(diff);
692 }
693
694 void Monitor::seekCursor(int pos)
695 {
696     activateMonitor();
697     if (m_ruler->slotNewValue(pos)) {
698         checkOverlay();
699         m_timePos->setValue(pos);
700     }
701 }
702
703 void Monitor::rendererStopped(int pos)
704 {
705     if (m_ruler->slotNewValue(pos)) {
706         checkOverlay();
707         m_timePos->setValue(pos);
708     }
709     disconnect(m_playAction, SIGNAL(triggered()), this, SLOT(slotPlay()));
710     //m_playAction->setChecked(false);
711     connect(m_playAction, SIGNAL(triggered()), this, SLOT(slotPlay()));
712     m_playAction->setIcon(m_playIcon);
713 }
714
715 void Monitor::adjustRulerSize(int length)
716 {
717     if (length > 0) m_length = length;
718     m_ruler->adjustScale(m_length);
719     if (m_currentClip != NULL) {
720         QPoint zone = m_currentClip->zone();
721         m_ruler->setZone(zone.x(), zone.y());
722     }
723 }
724
725 void Monitor::stop()
726 {
727     m_isActive = false;
728     disconnect(render, SIGNAL(rendererPosition(int)), this, SLOT(seekCursor(int)));
729     if (render) render->stop();
730 }
731
732 void Monitor::start()
733 {
734     m_isActive = true;
735     if (render) render->start();
736     connect(render, SIGNAL(rendererPosition(int)), this, SLOT(seekCursor(int)));
737 }
738
739 void Monitor::refreshMonitor(bool visible)
740 {
741     if (visible && render && !m_isActive) {
742         activateMonitor();
743         render->doRefresh(); //askForRefresh();
744     }
745 }
746
747 void Monitor::refreshMonitor()
748 {
749     if (m_isActive) {
750         render->doRefresh();
751     }
752 }
753
754 void Monitor::pause()
755 {
756     if (render == NULL) return;
757     activateMonitor();
758     render->pause();
759     //m_playAction->setChecked(true);
760     m_playAction->setIcon(m_playIcon);
761 }
762
763 void Monitor::slotPlay()
764 {
765     if (render == NULL) return;
766     activateMonitor();
767     if (render->playSpeed() == 0) {
768         //m_playAction->setChecked(true);
769         m_playAction->setIcon(m_pauseIcon);
770     } else {
771         //m_playAction->setChecked(false);
772         m_playAction->setIcon(m_playIcon);
773     }
774     render->switchPlay();
775 }
776
777 void Monitor::slotPlayZone()
778 {
779     if (render == NULL) return;
780     activateMonitor();
781     QPoint p = m_ruler->zone();
782     render->playZone(GenTime(p.x(), m_monitorManager->timecode().fps()), GenTime(p.y(), m_monitorManager->timecode().fps()));
783     //m_playAction->setChecked(true);
784     m_playAction->setIcon(m_pauseIcon);
785 }
786
787 void Monitor::slotLoopZone()
788 {
789     if (render == NULL) return;
790     activateMonitor();
791     QPoint p = m_ruler->zone();
792     render->loopZone(GenTime(p.x(), m_monitorManager->timecode().fps()), GenTime(p.y(), m_monitorManager->timecode().fps()));
793     //m_playAction->setChecked(true);
794     m_playAction->setIcon(m_pauseIcon);
795 }
796
797 void Monitor::slotLoopClip()
798 {
799     if (render == NULL || m_selectedClip == NULL)
800         return;
801     activateMonitor();
802     render->loopZone(m_selectedClip->startPos(), m_selectedClip->endPos());
803     //m_playAction->setChecked(true);
804     m_playAction->setIcon(m_pauseIcon);
805 }
806
807 void Monitor::slotSetXml(DocClipBase *clip, QPoint zone, const int position)
808 {
809     if (render == NULL) return;
810     if (clip == NULL && m_currentClip != NULL) {
811         m_currentClip = NULL;
812         m_length = -1;
813         render->setProducer(NULL, -1);
814         return;
815     }
816     if (m_currentClip != NULL || clip != NULL) activateMonitor();
817     if (clip != m_currentClip) {
818         m_currentClip = clip;
819         updateMarkers(clip);
820         if (render->setProducer(clip->producer(), position) == -1) {
821             // MLT CONSUMER is broken
822             kDebug(QtWarningMsg) << "ERROR, Cannot start monitor";
823         }
824     } else if (position != -1) render->seek(GenTime(position, m_monitorManager->timecode().fps()));
825     if (!zone.isNull()) {
826         m_ruler->setZone(zone.x(), zone.y());
827         render->seek(GenTime(zone.x(), m_monitorManager->timecode().fps()));
828     }
829 }
830
831 void Monitor::slotOpenFile(const QString &file)
832 {
833     if (render == NULL) return;
834     activateMonitor();
835     QDomDocument doc;
836     QDomElement mlt = doc.createElement("mlt");
837     doc.appendChild(mlt);
838     QDomElement prod = doc.createElement("producer");
839     mlt.appendChild(prod);
840     prod.setAttribute("mlt_service", "avformat");
841     prod.setAttribute("resource", file);
842     render->setSceneList(doc, 0);
843 }
844
845 void Monitor::slotSaveZone()
846 {
847     if (render == NULL) return;
848     emit saveZone(render, m_ruler->zone());
849
850     //render->setSceneList(doc, 0);
851 }
852
853 void Monitor::resetProfile(const QString profile)
854 {
855     m_timePos->updateTimeCode(m_monitorManager->timecode());
856     if (render == NULL) return;
857     render->resetProfile(profile);
858     if (m_effectScene) {
859         m_effectView->scale(((double) render->renderWidth()) / render->frameRenderWidth(), 1.0);
860         m_effectScene->resetProfile();
861     }
862 }
863
864 void Monitor::saveSceneList(QString path, QDomElement info)
865 {
866     if (render == NULL) return;
867     render->saveSceneList(path, info);
868 }
869
870 const QString Monitor::sceneList()
871 {
872     if (render == NULL) return QString();
873     return render->sceneList();
874 }
875
876 void Monitor::setClipZone(QPoint pos)
877 {
878     if (m_currentClip == NULL) return;
879     m_currentClip->setZone(pos);
880 }
881
882 void Monitor::slotSwitchDropFrames(bool show)
883 {
884     render->setDropFrames(show);
885 }
886
887 void Monitor::slotSwitchMonitorInfo(bool show)
888 {
889     KdenliveSettings::setDisplayMonitorInfo(show);
890     if (show) {
891         if (m_overlay) return;
892         if (m_monitorRefresh == NULL) {
893             // Using OpenGL display
894 #if defined(Q_WS_MAC) || defined(USE_OPEN_GL)
895             m_overlay = new Overlay(m_glWidget);
896 #endif
897         } else {
898             m_overlay = new Overlay(m_monitorRefresh);
899             m_overlay->raise();
900             m_overlay->setHidden(true);
901         }
902     } else {
903         delete m_overlay;
904         m_overlay = NULL;
905     }
906 }
907
908 void Monitor::updateTimecodeFormat()
909 {
910     m_timePos->slotUpdateTimeCodeFormat();
911 }
912
913 QStringList Monitor::getZoneInfo() const
914 {
915     QStringList result;
916     if (m_currentClip == NULL) return result;
917     result << m_currentClip->getId();
918     QPoint zone = m_ruler->zone();
919     result << QString::number(zone.x()) << QString::number(zone.y());
920     return result;
921 }
922
923 void Monitor::slotSetSelectedClip(AbstractClipItem* item)
924 {
925     if (item) {
926         m_loopClipAction->setEnabled(true);
927         m_selectedClip = item;
928     } else {
929         m_loopClipAction->setEnabled(false);
930     }
931 }
932
933 void Monitor::slotSetSelectedClip(ClipItem* item)
934 {
935     if (item || (!item && !m_loopClipTransition)) {
936         m_loopClipTransition = false;
937         slotSetSelectedClip((AbstractClipItem*)item);
938     }
939 }
940
941 void Monitor::slotSetSelectedClip(Transition* item)
942 {
943     if (item || (!item && m_loopClipTransition)) {
944         m_loopClipTransition = true;
945         slotSetSelectedClip((AbstractClipItem*)item);
946     }
947 }
948
949
950 void Monitor::slotEffectScene(bool show)
951 {
952     if (m_name == "project") {
953         if (m_monitorRefresh) {
954             m_monitorRefresh->setVisible(!show);
955         } else {
956 #if defined(Q_WS_MAC) || defined(USE_OPEN_GL)
957             m_glWidget->setVisible(!show);
958 #endif
959         }
960         m_effectView->setVisible(show);
961         emit requestFrameForAnalysis(show);
962         if (show) {
963             render->doRefresh();
964             m_effectScene->slotZoomFit();
965         }
966     }
967 }
968
969 MonitorScene * Monitor::getEffectScene()
970 {
971     return m_effectScene;
972 }
973
974 bool Monitor::effectSceneDisplayed()
975 {
976     return m_effectView->isVisible();
977 }
978
979 void Monitor::slotSetVolume(int volume)
980 {
981     KdenliveSettings::setVolume(volume);
982     KIcon icon;
983     if (volume == 0) icon = KIcon("audio-volume-muted");
984     else icon = KIcon("audio-volume-medium");
985     static_cast <QToolButton *>(m_volumeWidget)->setIcon(icon);
986     render->slotSetVolume(volume);
987 }
988
989 void Monitor::slotShowVolume()
990 {
991     m_volumePopup->move(mapToGlobal(m_toolbar->geometry().topLeft()) + QPoint(mapToParent(m_volumeWidget->geometry().bottomLeft()).x(), -m_volumePopup->height()));
992     int vol = render->volume();
993     // Disable widget if we cannot get the volume
994     m_volumePopup->setEnabled(vol != -1);
995     m_audioSlider->blockSignals(true);
996     m_audioSlider->setValue(vol);
997     m_audioSlider->blockSignals(false);
998     m_volumePopup->show();
999 }
1000
1001 MonitorRefresh::MonitorRefresh(QWidget* parent) :
1002     QWidget(parent)
1003     , m_renderer(NULL)
1004 {
1005     // MonitorRefresh is used as container for the SDL display (it's window id is passed to SDL)
1006     setAttribute(Qt::WA_PaintOnScreen);
1007     setAttribute(Qt::WA_OpaquePaintEvent);
1008     //setAttribute(Qt::WA_NoSystemBackground);
1009 }
1010
1011 void MonitorRefresh::setRenderer(Render* render)
1012 {
1013     m_renderer = render;
1014 }
1015
1016 void MonitorRefresh::paintEvent(QPaintEvent *event)
1017 {
1018     Q_UNUSED(event);
1019     if (m_renderer) m_renderer->doRefresh();
1020 }
1021
1022 Overlay::Overlay(QWidget* parent) :
1023     QLabel(parent)
1024 {
1025     setAttribute(Qt::WA_TransparentForMouseEvents);
1026     //setAttribute(Qt::WA_OpaquePaintEvent);
1027     //setAttribute(Qt::WA_NoSystemBackground);
1028     setAutoFillBackground(true);
1029     setBackgroundRole(QPalette::Base);
1030 }
1031
1032 void Overlay::setOverlayText(const QString &text, bool isZone)
1033 {
1034     setHidden(true);
1035     m_isZone = isZone;
1036     QPalette p;
1037     p.setColor(QPalette::Text, Qt::white);
1038     if (m_isZone) p.setColor(QPalette::Base, QColor(200, 0, 0));
1039     else p.setColor(QPalette::Base, QColor(0, 0, 200));
1040     setPalette(p);
1041     setText(' ' + text + ' ');
1042     setHidden(false);
1043     update();
1044 }
1045
1046 VideoContainer::VideoContainer(Monitor* parent) :
1047     QFrame()
1048     , m_monitor(parent)
1049 {
1050     setFrameShape(QFrame::NoFrame);
1051     setFocusPolicy(Qt::ClickFocus);
1052     setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
1053 }
1054
1055 // virtual
1056 void VideoContainer::mousePressEvent(QMouseEvent * event)
1057 {
1058     if (m_monitor->underMouse()) event->setAccepted(false);
1059 }
1060
1061 // virtual
1062 void VideoContainer::mouseReleaseEvent(QMouseEvent * event)
1063 {
1064     if (m_monitor->underMouse()) event->setAccepted(false);
1065     else {
1066         if (m_monitor->isActive()) {
1067             m_monitor->slotPlay();
1068             event->accept();
1069         }
1070     }
1071 }
1072
1073 // virtual
1074 void VideoContainer::mouseMoveEvent(QMouseEvent *event)
1075 {
1076     if (m_monitor->underMouse()) event->setAccepted(false);
1077 }
1078
1079 // virtual
1080 void VideoContainer::keyPressEvent(QKeyEvent *event)
1081 {
1082     // Exit fullscreen with Esc key
1083     if (event->key() == Qt::Key_Escape && isFullScreen()) {
1084         switchFullScreen();
1085         event->setAccepted(true);
1086     } else event->setAccepted(false);
1087 }
1088
1089 // virtual
1090 void VideoContainer::wheelEvent(QWheelEvent * event)
1091 {
1092     if (m_monitor->underMouse()) event->setAccepted(false);
1093     else {
1094         m_monitor->slotMouseSeek(event->delta(), event->modifiers() == Qt::ControlModifier);
1095         event->accept();
1096     }
1097 }
1098
1099 void VideoContainer::mouseDoubleClickEvent(QMouseEvent * event)
1100 {
1101     Q_UNUSED(event);
1102
1103     if (!KdenliveSettings::openglmonitors())
1104         switchFullScreen();
1105 }
1106
1107 void VideoContainer::switchFullScreen()
1108 {
1109     // TODO: disable screensaver?
1110     Qt::WindowFlags flags = windowFlags();
1111     if (!isFullScreen()) {
1112         // Check if we ahave a multiple monitor setup
1113         setUpdatesEnabled(false);
1114 #if QT_VERSION >= 0x040600
1115         int monitors = QApplication::desktop()->screenCount();
1116 #else
1117         int monitors = QApplication::desktop()->numScreens();
1118 #endif
1119         if (monitors > 1) {
1120             QRect screenres;
1121             // Move monitor widget to the second screen (one screen for Kdenlive, the other one for the Monitor widget
1122             int currentScreen = QApplication::desktop()->screenNumber(this);
1123             if (currentScreen < monitors - 1)
1124                 screenres = QApplication::desktop()->screenGeometry(currentScreen + 1);
1125             else
1126                 screenres = QApplication::desktop()->screenGeometry(currentScreen - 1);
1127             move(QPoint(screenres.x(), screenres.y()));
1128             resize(screenres.width(), screenres.height());
1129         }
1130
1131         m_baseFlags = flags & (Qt::Window | Qt::SubWindow);
1132         flags |= Qt::Window;
1133         flags ^= Qt::SubWindow;
1134         setWindowFlags(flags);
1135 #ifdef Q_WS_X11
1136         // This works around a bug with Compiz
1137         // as the window must be visible before we can set the state
1138         show();
1139         raise();
1140         setWindowState(windowState() | Qt::WindowFullScreen);   // set
1141 #else
1142         setWindowState(windowState() | Qt::WindowFullScreen);   // set
1143         setUpdatesEnabled(true);
1144         show();
1145 #endif
1146     } else {
1147         setUpdatesEnabled(false);
1148         flags ^= (Qt::Window | Qt::SubWindow); //clear the flags...
1149         flags |= m_baseFlags; //then we reset the flags (window and subwindow)
1150         setWindowFlags(flags);
1151         setWindowState(windowState()  ^ Qt::WindowFullScreen);   // reset
1152         setUpdatesEnabled(true);
1153         show();
1154     }
1155     m_monitor->pause();
1156 }
1157
1158
1159 #include "monitor.moc"