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