]> git.sesse.net Git - kdenlive/blob - src/monitor.cpp
*Allow users to choose between SDL and OpenGL for monitor display
[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 #include "audiosignal.h"
30
31 #include <KDebug>
32 #include <KLocale>
33 #include <KFileDialog>
34 #include <KApplication>
35 #include <KMessageBox>
36
37 #include <QMouseEvent>
38 #include <QStylePainter>
39 #include <QMenu>
40 #include <QToolButton>
41 #include <QToolBar>
42 #include <QDesktopWidget>
43 #include <QLabel>
44 #include <QIntValidator>
45 #include <QVBoxLayout>
46 #include <QGraphicsView>
47
48
49 Monitor::Monitor(QString name, MonitorManager *manager, QString profile, QWidget *parent) :
50     QWidget(parent),
51     render(NULL),
52     m_audiosignal(NULL),
53     m_name(name),
54     m_monitorManager(manager),
55     m_currentClip(NULL),
56     m_ruler(new SmallRuler(m_monitorManager)),
57     m_overlay(NULL),
58     m_isActive(false),
59     m_scale(1),
60     m_length(0),
61     m_dragStarted(false),
62     m_monitorRefresh(NULL),
63     m_effectScene(NULL),
64     m_effectView(NULL),
65     m_selectedClip(NULL),
66     m_loopClipTransition(true)
67
68 {
69     m_ui.setupUi(this);
70     QVBoxLayout *layout = new QVBoxLayout;
71     layout->setContentsMargins(0, 0, 0, 0);
72     layout->addWidget(m_ruler);
73     m_ui.ruler_frame->setLayout(layout);
74     setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
75     setMinimumHeight(200);
76     QToolBar *toolbar = new QToolBar(name, this);
77     QVBoxLayout *layout2 = new QVBoxLayout;
78     layout2->setContentsMargins(0, 0, 0, 0);
79
80     m_playIcon = KIcon("media-playback-start");
81     m_pauseIcon = KIcon("media-playback-pause");
82
83     if (name != "chapter") {
84         toolbar->addAction(KIcon("kdenlive-zone-start"), i18n("Set zone start"), this, SLOT(slotSetZoneStart()));
85         toolbar->addAction(KIcon("kdenlive-zone-end"), i18n("Set zone end"), this, SLOT(slotSetZoneEnd()));
86     } else {
87         m_ruler->setZone(-3, -2);
88     }
89
90     toolbar->addAction(KIcon("media-seek-backward"), i18n("Rewind"), this, SLOT(slotRewind()));
91     //toolbar->addAction(KIcon("media-skip-backward"), i18n("Rewind 1 frame"), this, SLOT(slotRewindOneFrame()));
92
93     QToolButton *playButton = new QToolButton(toolbar);
94     m_playMenu = new QMenu(i18n("Play..."), this);
95     m_playAction = m_playMenu->addAction(m_playIcon, i18n("Play"));
96     m_playAction->setCheckable(true);
97     connect(m_playAction, SIGNAL(triggered()), this, SLOT(slotPlay()));
98
99     playButton->setMenu(m_playMenu);
100     playButton->setPopupMode(QToolButton::MenuButtonPopup);
101     toolbar->addWidget(playButton);
102
103     //toolbar->addAction(KIcon("media-skip-forward"), i18n("Forward 1 frame"), this, SLOT(slotForwardOneFrame()));
104     toolbar->addAction(KIcon("media-seek-forward"), i18n("Forward"), this, SLOT(slotForward()));
105
106     playButton->setDefaultAction(m_playAction);
107
108     if (name != "chapter") {
109         QToolButton *configButton = new QToolButton(toolbar);
110         m_configMenu = new QMenu(i18n("Misc..."), this);
111         configButton->setIcon(KIcon("system-run"));
112         configButton->setMenu(m_configMenu);
113         configButton->setPopupMode(QToolButton::QToolButton::InstantPopup);
114         toolbar->addWidget(configButton);
115
116         if (name == "clip") {
117             m_markerMenu = new QMenu(i18n("Go to marker..."), this);
118             m_markerMenu->setEnabled(false);
119             m_configMenu->addMenu(m_markerMenu);
120             connect(m_markerMenu, SIGNAL(triggered(QAction *)), this, SLOT(slotGoToMarker(QAction *)));
121         }
122         m_configMenu->addAction(KIcon("transform-scale"), i18n("Resize (100%)"), this, SLOT(slotSetSizeOneToOne()));
123         m_configMenu->addAction(KIcon("transform-scale"), i18n("Resize (50%)"), this, SLOT(slotSetSizeOneToTwo()));
124     }
125
126     QWidget *spacer = new QWidget(this);
127     spacer->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
128     toolbar->addWidget(spacer);
129     m_timePos = new TimecodeDisplay(m_monitorManager->timecode(), this);
130     toolbar->addWidget(m_timePos);
131     connect(m_timePos, SIGNAL(editingFinished()), this, SLOT(slotSeek()));
132
133     layout2->addWidget(toolbar);
134     m_ui.button_frame->setLayout(layout2);
135     const int toolHeight = toolbar->height();
136     m_ui.button_frame->setMinimumHeight(toolHeight);
137
138     //m_ruler->setPixelPerMark(3);
139
140     if (profile.isEmpty()) profile = KdenliveSettings::current_profile();
141
142     QVBoxLayout *rendererBox = new QVBoxLayout(m_ui.video_frame);
143     rendererBox->setContentsMargins(0, 0, 0, 0);
144     bool monitorCreated = false;
145 #ifdef Q_WS_MAC
146     createOpenGlWidget(rendererBox, profile);
147     monitorCreated = true;
148     //m_glWidget->setFixedSize(width, height);
149 #elif defined (USE_OPEN_GL)
150     if (KdenliveSettings::openglmonitors()) {
151         monitorCreated = createOpenGlWidget(rendererBox, profile);
152     }
153 #endif
154     if (!monitorCreated) {
155         m_monitorRefresh = new MonitorRefresh(m_ui.video_frame);
156         rendererBox->addWidget(m_monitorRefresh);
157         render = new Render(m_name, (int) m_monitorRefresh->winId(), profile, this);
158         m_monitorRefresh->setRenderer(render);
159     }
160
161     QVBoxLayout *audioBox = new QVBoxLayout;
162     audioBox->setContentsMargins(0, 0, 0, 0);
163     m_audiosignal = new AudioSignal();
164     audioBox->addWidget(m_audiosignal);
165     m_ui.audio_monitor->setLayout(audioBox);
166     connect(render, SIGNAL(showAudioSignal(const QByteArray)), m_audiosignal, SLOT(showAudio(const QByteArray)));
167     connect(m_ruler, SIGNAL(seekRenderer(int)), this, SLOT(slotSeek(int)));
168     connect(render, SIGNAL(durationChanged(int)), this, SLOT(adjustRulerSize(int)));
169     connect(render, SIGNAL(rendererStopped(int)), this, SLOT(rendererStopped(int)));
170
171     //render->createVideoXWindow(m_ui.video_frame->winId(), -1);
172
173     if (name != "clip") {
174         connect(render, SIGNAL(rendererPosition(int)), this, SIGNAL(renderPosition(int)));
175         connect(render, SIGNAL(durationChanged(int)), this, SIGNAL(durationChanged(int)));
176         connect(m_ruler, SIGNAL(zoneChanged(QPoint)), this, SIGNAL(zoneUpdated(QPoint)));
177     } else {
178         connect(m_ruler, SIGNAL(zoneChanged(QPoint)), this, SLOT(setClipZone(QPoint)));
179     }
180
181     if (m_monitorRefresh) m_monitorRefresh->show();
182
183     if (name == "project") {
184         m_effectScene = new MonitorScene(render);
185         m_effectView = new QGraphicsView(m_effectScene, m_ui.video_frame);
186         m_effectView->setRenderHints(QFlags<QPainter::RenderHint>());
187         m_effectView->scale(((double) render->renderWidth()) / render->frameRenderWidth(), 1.0);
188         rendererBox->addWidget(m_effectView);
189         m_effectView->setMouseTracking(true);
190         m_effectScene->setUp();
191         m_effectView->hide();
192     }
193 }
194
195 Monitor::~Monitor()
196 {
197     delete m_ruler;
198     delete m_timePos;
199     delete m_overlay;
200     if (m_name == "project") {
201         delete m_effectView;
202         delete m_effectScene;
203     }
204     delete m_monitorRefresh;
205     delete render;
206 }
207
208 QString Monitor::name() const
209 {
210     return m_name;
211 }
212
213 #if defined(Q_WS_MAC) || defined(USE_OPEN_GL)
214 bool Monitor::createOpenGlWidget(QVBoxLayout *rendererBox, const QString profile)
215 {
216     render = new Render(m_name, 0, profile, this);
217     m_glWidget = new VideoGLWidget(m_ui.video_frame);
218     if (m_glWidget == NULL) {
219         // Creation failed, we are in trouble...
220         return false;
221     }
222     rendererBox->addWidget(m_glWidget);
223     m_glWidget->setImageAspectRatio(render->dar());
224     m_glWidget->setBackgroundColor(KdenliveSettings::window_background());
225     connect(render, SIGNAL(showImageSignal(QImage)), m_glWidget, SLOT(showImage(QImage)));
226     return true;
227 }
228 #endif
229
230 void Monitor::setupMenu(QMenu *goMenu, QAction *playZone, QAction *loopZone, QMenu *markerMenu, QAction *loopClip)
231 {
232     m_contextMenu = new QMenu(this);
233     m_contextMenu->addMenu(m_playMenu);
234     if (goMenu)
235         m_contextMenu->addMenu(goMenu);
236     if (markerMenu)
237         m_contextMenu->addMenu(markerMenu);
238
239     m_playMenu->addAction(playZone);
240     m_playMenu->addAction(loopZone);
241     if (loopClip) {
242         m_loopClipAction = loopClip;
243         m_playMenu->addAction(loopClip);
244     }
245
246     //TODO: add save zone to timeline monitor when fixed
247     if (m_name == "clip") {
248         m_contextMenu->addMenu(m_markerMenu);
249         m_contextMenu->addAction(KIcon("document-save"), i18n("Save zone"), this, SLOT(slotSaveZone()));
250     }
251     QAction *extractFrame = m_configMenu->addAction(KIcon("document-new"), i18n("Extract frame"), this, SLOT(slotExtractCurrentFrame()));
252     m_contextMenu->addAction(extractFrame);
253
254     if (m_name != "clip") {
255 #if defined(Q_WS_MAC) || defined(USE_OPEN_GL)
256         // TODO: why disable in OpenGL?
257         // Don't show split action
258 #else
259         QAction *splitView = m_contextMenu->addAction(KIcon("view-split-left-right"), i18n("Split view"), render, SLOT(slotSplitView(bool)));
260         splitView->setCheckable(true);
261         m_configMenu->addAction(splitView);
262 #endif
263     } else {
264         QAction *setThumbFrame = m_contextMenu->addAction(KIcon("document-new"), i18n("Set current image as thumbnail"), this, SLOT(slotSetThumbFrame()));
265         m_configMenu->addAction(setThumbFrame);
266     }
267
268     QAction *showTips = m_contextMenu->addAction(KIcon("help-hint"), i18n("Monitor overlay infos"));
269     showTips->setCheckable(true);
270     connect(showTips, SIGNAL(toggled(bool)), this, SLOT(slotSwitchMonitorInfo(bool)));
271     showTips->setChecked(KdenliveSettings::displayMonitorInfo());
272
273     QAction *dropFrames = m_contextMenu->addAction(KIcon(), i18n("Real time (drop frames)"));
274     dropFrames->setCheckable(true);
275     dropFrames->setChecked(true);
276     connect(dropFrames, SIGNAL(toggled(bool)), this, SLOT(slotSwitchDropFrames(bool)));
277
278     m_configMenu->addAction(showTips);
279     m_configMenu->addAction(dropFrames);
280
281 }
282
283 void Monitor::slotGoToMarker(QAction *action)
284 {
285     int pos = action->data().toInt();
286     slotSeek(pos);
287 }
288
289 void Monitor::slotSetSizeOneToOne()
290 {
291     QRect r = QApplication::desktop()->screenGeometry();
292     const int maxWidth = r.width() - 20;
293     const int maxHeight = r.height() - 20;
294     int width = render->renderWidth();
295     int height = render->renderHeight();
296     kDebug() << "// render info: " << width << "x" << height;
297     while (width >= maxWidth || height >= maxHeight) {
298         width = width * 0.8;
299         height = height * 0.8;
300     }
301     kDebug() << "// MONITOR; set SIZE: " << width << ", " << height;
302     m_ui.video_frame->setFixedSize(width, height);
303     updateGeometry();
304     adjustSize();
305     //m_ui.video_frame->setMinimumSize(0, 0);
306     emit adjustMonitorSize();
307 }
308
309 void Monitor::slotSetSizeOneToTwo()
310 {
311     QRect r = QApplication::desktop()->screenGeometry();
312     const int maxWidth = r.width() - 20;
313     const int maxHeight = r.height() - 20;
314     int width = render->renderWidth() / 2;
315     int height = render->renderHeight() / 2;
316     kDebug() << "// render info: " << width << "x" << height;
317     while (width >= maxWidth || height >= maxHeight) {
318         width = width * 0.8;
319         height = height * 0.8;
320     }
321     kDebug() << "// MONITOR; set SIZE: " << width << ", " << height;
322     m_ui.video_frame->setFixedSize(width, height);
323     updateGeometry();
324     adjustSize();
325     //m_ui.video_frame->setMinimumSize(0, 0);
326     emit adjustMonitorSize();
327 }
328
329 void Monitor::resetSize()
330 {
331     m_ui.video_frame->setMinimumSize(0, 0);
332 }
333
334 DocClipBase *Monitor::activeClip()
335 {
336     return m_currentClip;
337 }
338
339 void Monitor::updateMarkers(DocClipBase *source)
340 {
341     if (source == m_currentClip && source != NULL) {
342         m_markerMenu->clear();
343         QList <CommentedTime> markers = m_currentClip->commentedSnapMarkers();
344         if (!markers.isEmpty()) {
345             QList <int> marks;
346             for (int i = 0; i < markers.count(); i++) {
347                 int pos = (int) markers.at(i).time().frames(m_monitorManager->timecode().fps());
348                 marks.append(pos);
349                 QString position = m_monitorManager->timecode().getTimecode(markers.at(i).time()) + ' ' + markers.at(i).comment();
350                 QAction *go = m_markerMenu->addAction(position);
351                 go->setData(pos);
352             }
353             m_ruler->setMarkers(marks);
354         } else m_ruler->setMarkers(QList <int>());
355         m_markerMenu->setEnabled(!m_markerMenu->isEmpty());
356     }
357 }
358
359 void Monitor::slotSeekToPreviousSnap()
360 {
361     if (m_currentClip) slotSeek(getSnapForPos(true).frames(m_monitorManager->timecode().fps()));
362 }
363
364 void Monitor::slotSeekToNextSnap()
365 {
366     if (m_currentClip) slotSeek(getSnapForPos(false).frames(m_monitorManager->timecode().fps()));
367 }
368
369 GenTime Monitor::position()
370 {
371     return render->seekPosition();
372 }
373
374 GenTime Monitor::getSnapForPos(bool previous)
375 {
376     QList <GenTime> snaps;
377     QList < GenTime > markers = m_currentClip->snapMarkers();
378     for (int i = 0; i < markers.size(); ++i) {
379         GenTime t = markers.at(i);
380         snaps.append(t);
381     }
382     QPoint zone = m_ruler->zone();
383     snaps.append(GenTime(zone.x(), m_monitorManager->timecode().fps()));
384     snaps.append(GenTime(zone.y(), m_monitorManager->timecode().fps()));
385     snaps.append(GenTime());
386     snaps.append(m_currentClip->duration());
387     qSort(snaps);
388
389     const GenTime pos = render->seekPosition();
390     for (int i = 0; i < snaps.size(); ++i) {
391         if (previous && snaps.at(i) >= pos) {
392             if (i == 0) i = 1;
393             return snaps.at(i - 1);
394         } else if (!previous && snaps.at(i) > pos) {
395             return snaps.at(i);
396         }
397     }
398     return GenTime();
399 }
400
401 void Monitor::slotZoneMoved(int start, int end)
402 {
403     m_ruler->setZone(start, end);
404     checkOverlay();
405     setClipZone(m_ruler->zone());
406 }
407
408 void Monitor::slotSetZoneStart()
409 {
410     m_ruler->setZone(render->seekFramePosition(), -1);
411     emit zoneUpdated(m_ruler->zone());
412     checkOverlay();
413     setClipZone(m_ruler->zone());
414 }
415
416 void Monitor::slotSetZoneEnd()
417 {
418     m_ruler->setZone(-1, render->seekFramePosition());
419     emit zoneUpdated(m_ruler->zone());
420     checkOverlay();
421     setClipZone(m_ruler->zone());
422 }
423
424 // virtual
425 void Monitor::mousePressEvent(QMouseEvent * event)
426 {
427     if (event->button() != Qt::RightButton) {
428         if (m_ui.video_frame->underMouse()) {
429             m_dragStarted = true;
430             m_DragStartPosition = event->pos();
431         }
432     } else m_contextMenu->popup(event->globalPos());
433 }
434
435 // virtual
436 void Monitor::mouseReleaseEvent(QMouseEvent * event)
437 {
438     if (m_dragStarted) {
439         if (m_ui.video_frame->underMouse() && (!m_effectView || !m_effectView->isVisible())) {
440             if (isActive()) slotPlay();
441             else activateMonitor();
442         } else QWidget::mouseReleaseEvent(event);
443         m_dragStarted = false;
444     }
445 }
446
447 // virtual
448 void Monitor::mouseMoveEvent(QMouseEvent *event)
449 {
450     // kDebug() << "// DRAG STARTED, MOUSE MOVED: ";
451     if (!m_dragStarted || m_currentClip == NULL) return;
452
453     if ((event->pos() - m_DragStartPosition).manhattanLength()
454             < QApplication::startDragDistance())
455         return;
456
457     {
458         QDrag *drag = new QDrag(this);
459         QMimeData *mimeData = new QMimeData;
460
461         QStringList list;
462         list.append(m_currentClip->getId());
463         QPoint p = m_ruler->zone();
464         list.append(QString::number(p.x()));
465         list.append(QString::number(p.y()));
466         QByteArray data;
467         data.append(list.join(";").toUtf8());
468         mimeData->setData("kdenlive/clip", data);
469         drag->setMimeData(mimeData);
470         QPixmap pix = m_currentClip->thumbnail();
471         drag->setPixmap(pix);
472         drag->setHotSpot(QPoint(0, 50));
473         drag->start(Qt::MoveAction);
474
475         //Qt::DropAction dropAction;
476         //dropAction = drag->start(Qt::CopyAction | Qt::MoveAction);
477
478         //Qt::DropAction dropAction = drag->exec();
479
480     }
481     //event->accept();
482 }
483
484 /*void Monitor::dragMoveEvent(QDragMoveEvent * event) {
485     event->setDropAction(Qt::IgnoreAction);
486     event->setDropAction(Qt::MoveAction);
487     if (event->mimeData()->hasText()) {
488         event->acceptProposedAction();
489     }
490 }
491
492 Qt::DropActions Monitor::supportedDropActions() const {
493     // returns what actions are supported when dropping
494     return Qt::MoveAction;
495 }*/
496
497 QStringList Monitor::mimeTypes() const
498 {
499     QStringList qstrList;
500     // list of accepted mime types for drop
501     qstrList.append("kdenlive/clip");
502     return qstrList;
503 }
504
505 // virtual
506 void Monitor::wheelEvent(QWheelEvent * event)
507 {
508     if (event->modifiers() == Qt::ControlModifier) {
509         int delta = m_monitorManager->timecode().fps();
510         if (event->delta() > 0) delta = 0 - delta;
511         slotSeek(render->seekFramePosition() - delta);
512     } else {
513         if (event->delta() >= 0) slotForwardOneFrame();
514         else slotRewindOneFrame();
515     }
516 }
517
518 void Monitor::slotSetThumbFrame()
519 {
520     if (m_currentClip == NULL) {
521         return;
522     }
523     m_currentClip->setClipThumbFrame((uint) render->seekFramePosition());
524     emit refreshClipThumbnail(m_currentClip->getId());
525 }
526
527 void Monitor::slotExtractCurrentFrame()
528 {
529     QImage frame = render->extractFrame(render->seekFramePosition());
530     KFileDialog *fs = new KFileDialog(KUrl(), "image/png", this);
531     fs->setOperationMode(KFileDialog::Saving);
532     fs->setMode(KFile::File);
533 #if KDE_IS_VERSION(4,2,0)
534     fs->setConfirmOverwrite(true);
535 #endif
536     fs->setKeepLocation(true);
537     fs->exec();
538     QString path = fs->selectedFile();
539     delete fs;
540     if (!path.isEmpty()) {
541         frame.save(path);
542     }
543 }
544
545 bool Monitor::isActive() const
546 {
547     return m_isActive;
548 }
549
550 void Monitor::activateMonitor()
551 {
552     if (!m_isActive) {
553         m_monitorManager->slotSwitchMonitors(m_name == "clip");
554     }
555 }
556
557 void Monitor::setTimePos(const QString &pos)
558 {
559     m_timePos->setValue(pos);
560     slotSeek();
561 }
562
563 void Monitor::slotSeek()
564 {
565     slotSeek(m_timePos->getValue());
566 }
567
568 void Monitor::slotSeek(int pos)
569 {
570     activateMonitor();
571     if (render == NULL) return;
572     render->seekToFrame(pos);
573 }
574
575 void Monitor::checkOverlay()
576 {
577     if (m_overlay == NULL) return;
578     int pos = render->seekFramePosition();
579     QPoint zone = m_ruler->zone();
580     if (pos == zone.x())
581         m_overlay->setOverlayText(i18n("In Point"));
582     else if (pos == zone.y())
583         m_overlay->setOverlayText(i18n("Out Point"));
584     else {
585         if (m_currentClip) {
586             QString markerComment = m_currentClip->markerComment(GenTime(pos, m_monitorManager->timecode().fps()));
587             if (markerComment.isEmpty())
588                 m_overlay->setHidden(true);
589             else
590                 m_overlay->setOverlayText(markerComment, false);
591         } else m_overlay->setHidden(true);
592     }
593 }
594
595 void Monitor::slotStart()
596 {
597     activateMonitor();
598     render->play(0);
599     render->seekToFrame(0);
600 }
601
602 void Monitor::slotEnd()
603 {
604     activateMonitor();
605     render->play(0);
606     render->seekToFrame(render->getLength());
607 }
608
609 void Monitor::slotZoneStart()
610 {
611     activateMonitor();
612     render->play(0);
613     render->seekToFrame(m_ruler->zone().x());
614 }
615
616 void Monitor::slotZoneEnd()
617 {
618     activateMonitor();
619     render->play(0);
620     render->seekToFrame(m_ruler->zone().y());
621 }
622
623 void Monitor::slotRewind(double speed)
624 {
625     activateMonitor();
626     if (speed == 0) {
627         double currentspeed = render->playSpeed();
628         if (currentspeed >= 0) render->play(-2);
629         else render->play(currentspeed * 2);
630     } else render->play(speed);
631     m_playAction->setChecked(true);
632     m_playAction->setIcon(m_pauseIcon);
633 }
634
635 void Monitor::slotForward(double speed)
636 {
637     activateMonitor();
638     if (speed == 0) {
639         double currentspeed = render->playSpeed();
640         if (currentspeed <= 1) render->play(2);
641         else render->play(currentspeed * 2);
642     } else render->play(speed);
643     m_playAction->setChecked(true);
644     m_playAction->setIcon(m_pauseIcon);
645 }
646
647 void Monitor::slotRewindOneFrame(int diff)
648 {
649     activateMonitor();
650     render->play(0);
651     render->seekToFrameDiff(-diff);
652 }
653
654 void Monitor::slotForwardOneFrame(int diff)
655 {
656     activateMonitor();
657     render->play(0);
658     render->seekToFrameDiff(diff);
659 }
660
661 void Monitor::seekCursor(int pos)
662 {
663     activateMonitor();
664     if (m_ruler->slotNewValue(pos)) {
665         checkOverlay();
666         m_timePos->setValue(pos);
667     }
668 }
669
670 void Monitor::rendererStopped(int pos)
671 {
672     if (m_ruler->slotNewValue(pos)) {
673         checkOverlay();
674         m_timePos->setValue(pos);
675     }
676     disconnect(m_playAction, SIGNAL(triggered()), this, SLOT(slotPlay()));
677     m_playAction->setChecked(false);
678     connect(m_playAction, SIGNAL(triggered()), this, SLOT(slotPlay()));
679     m_playAction->setIcon(m_playIcon);
680 }
681
682 void Monitor::adjustRulerSize(int length)
683 {
684     if (length > 0) m_length = length;
685     m_ruler->adjustScale(m_length);
686     if (m_currentClip != NULL) {
687         QPoint zone = m_currentClip->zone();
688         m_ruler->setZone(zone.x(), zone.y());
689     }
690 }
691
692 void Monitor::stop()
693 {
694     m_isActive = false;
695     disconnect(render, SIGNAL(rendererPosition(int)), this, SLOT(seekCursor(int)));
696     if (render) render->stop();
697 }
698
699 void Monitor::start()
700 {
701     m_isActive = true;
702     if (render) render->start();
703     connect(render, SIGNAL(rendererPosition(int)), this, SLOT(seekCursor(int)));
704 }
705
706 void Monitor::refreshMonitor(bool visible)
707 {
708     if (visible && render && !m_isActive) {
709         activateMonitor();
710         render->doRefresh(); //askForRefresh();
711     }
712 }
713
714 void Monitor::pause()
715 {
716     if (render == NULL) return;
717     activateMonitor();
718     render->pause();
719     //m_playAction->setChecked(true);
720     //m_playAction->setIcon(m_pauseIcon);
721 }
722
723 void Monitor::slotPlay()
724 {
725     if (render == NULL) return;
726     activateMonitor();
727     if (render->playSpeed() == 0) {
728         m_playAction->setChecked(true);
729         m_playAction->setIcon(m_pauseIcon);
730     } else {
731         m_playAction->setChecked(false);
732         m_playAction->setIcon(m_playIcon);
733     }
734     render->switchPlay();
735 }
736
737 void Monitor::slotPlayZone()
738 {
739     if (render == NULL) return;
740     activateMonitor();
741     QPoint p = m_ruler->zone();
742     render->playZone(GenTime(p.x(), m_monitorManager->timecode().fps()), GenTime(p.y(), m_monitorManager->timecode().fps()));
743     m_playAction->setChecked(true);
744     m_playAction->setIcon(m_pauseIcon);
745 }
746
747 void Monitor::slotLoopZone()
748 {
749     if (render == NULL) return;
750     activateMonitor();
751     QPoint p = m_ruler->zone();
752     render->loopZone(GenTime(p.x(), m_monitorManager->timecode().fps()), GenTime(p.y(), m_monitorManager->timecode().fps()));
753     m_playAction->setChecked(true);
754     m_playAction->setIcon(m_pauseIcon);
755 }
756
757 void Monitor::slotLoopClip()
758 {
759     if (render == NULL || m_selectedClip == NULL)
760         return;
761     activateMonitor();
762     render->loopZone(m_selectedClip->startPos(), m_selectedClip->endPos());
763     m_playAction->setChecked(true);
764     m_playAction->setIcon(m_pauseIcon);
765 }
766
767 void Monitor::slotSetXml(DocClipBase *clip, QPoint zone, const int position)
768 {
769     if (render == NULL) return;
770     if (clip == NULL && m_currentClip != NULL) {
771         m_currentClip = NULL;
772         m_length = -1;
773         render->setProducer(NULL, -1);
774         return;
775     }
776     if (m_currentClip != NULL || clip != NULL) activateMonitor();
777     if (clip != m_currentClip) {
778         m_currentClip = clip;
779         updateMarkers(clip);
780         if (render->setProducer(clip->producer(), position) == -1) {
781             // MLT CONSUMER is broken
782             kDebug(QtWarningMsg) << "ERROR, Cannot start monitor";
783         }
784     } else if (position != -1) render->seek(GenTime(position, m_monitorManager->timecode().fps()));
785     if (!zone.isNull()) {
786         m_ruler->setZone(zone.x(), zone.y());
787         render->seek(GenTime(zone.x(), m_monitorManager->timecode().fps()));
788     }
789 }
790
791 void Monitor::slotOpenFile(const QString &file)
792 {
793     if (render == NULL) return;
794     activateMonitor();
795     QDomDocument doc;
796     QDomElement mlt = doc.createElement("mlt");
797     doc.appendChild(mlt);
798     QDomElement prod = doc.createElement("producer");
799     mlt.appendChild(prod);
800     prod.setAttribute("mlt_service", "avformat");
801     prod.setAttribute("resource", file);
802     render->setSceneList(doc, 0);
803 }
804
805 void Monitor::slotSaveZone()
806 {
807     if (render == NULL) return;
808     emit saveZone(render, m_ruler->zone());
809
810     //render->setSceneList(doc, 0);
811 }
812
813 void Monitor::resetProfile(const QString profile)
814 {
815     m_timePos->updateTimeCode(m_monitorManager->timecode());
816     if (render == NULL) return;
817     render->resetProfile(profile);
818     if (m_effectScene) {
819         m_effectView->scale(((double) render->renderWidth()) / render->frameRenderWidth(), 1.0);
820         m_effectScene->resetProfile();
821     }
822 }
823
824 void Monitor::saveSceneList(QString path, QDomElement info)
825 {
826     if (render == NULL) return;
827     render->saveSceneList(path, info);
828 }
829
830 const QString Monitor::sceneList()
831 {
832     if (render == NULL) return QString();
833     return render->sceneList();
834 }
835
836 void Monitor::setClipZone(QPoint pos)
837 {
838     if (m_currentClip == NULL) return;
839     m_currentClip->setZone(pos);
840 }
841
842 void Monitor::slotSwitchDropFrames(bool show)
843 {
844     render->setDropFrames(show);
845 }
846
847 void Monitor::slotSwitchMonitorInfo(bool show)
848 {
849     KdenliveSettings::setDisplayMonitorInfo(show);
850     if (show) {
851         if (m_overlay) return;
852         if (m_monitorRefresh == NULL) {
853             // Using OpenGL display
854 #if defined(Q_WS_MAC) || defined(USE_OPEN_GL)
855             m_overlay = new Overlay(m_glWidget);
856 #endif
857         } else {
858             m_overlay = new Overlay(m_monitorRefresh);
859             m_overlay->raise();
860             m_overlay->setHidden(true);
861         }
862     } else {
863         delete m_overlay;
864         m_overlay = NULL;
865     }
866 }
867
868 void Monitor::updateTimecodeFormat()
869 {
870     m_timePos->slotUpdateTimeCodeFormat();
871 }
872
873 QStringList Monitor::getZoneInfo() const
874 {
875     QStringList result;
876     if (m_currentClip == NULL) return result;
877     result << m_currentClip->getId();
878     QPoint zone = m_ruler->zone();
879     result << QString::number(zone.x()) << QString::number(zone.y());
880     return result;
881 }
882
883 void Monitor::slotSetSelectedClip(AbstractClipItem* item)
884 {
885     if (item) {
886         m_loopClipAction->setEnabled(true);
887         m_selectedClip = item;
888     } else {
889         m_loopClipAction->setEnabled(false);
890     }
891 }
892
893 void Monitor::slotSetSelectedClip(ClipItem* item)
894 {
895     if (item || (!item && !m_loopClipTransition)) {
896         m_loopClipTransition = false;
897         slotSetSelectedClip((AbstractClipItem*)item);
898     }
899 }
900
901 void Monitor::slotSetSelectedClip(Transition* item)
902 {
903     if (item || (!item && m_loopClipTransition)) {
904         m_loopClipTransition = true;
905         slotSetSelectedClip((AbstractClipItem*)item);
906     }
907 }
908
909
910 void Monitor::slotEffectScene(bool show)
911 {
912     if (m_name == "project") {
913         if (m_monitorRefresh) {
914             m_monitorRefresh->setVisible(!show);
915         } else {
916 #if defined(Q_WS_MAC) || defined(USE_OPEN_GL)
917             m_glWidget->setVisible(!show);
918 #endif
919         }
920         m_effectView->setVisible(show);
921         emit requestFrameForAnalysis(show);
922         if (show) {
923             render->doRefresh();
924             m_effectScene->slotZoomFit();
925         }
926     }
927 }
928
929 MonitorScene * Monitor::getEffectScene()
930 {
931     return m_effectScene;
932 }
933
934 bool Monitor::effectSceneDisplayed()
935 {
936     return m_effectView->isVisible();
937 }
938
939 MonitorRefresh::MonitorRefresh(QWidget* parent) :
940     QWidget(parent),
941     m_renderer(NULL)
942 {
943     setAttribute(Qt::WA_PaintOnScreen);
944     setAttribute(Qt::WA_OpaquePaintEvent);
945     //setAttribute(Qt::WA_NoSystemBackground);
946 }
947
948 void MonitorRefresh::setRenderer(Render* render)
949 {
950     m_renderer = render;
951 }
952
953 void MonitorRefresh::paintEvent(QPaintEvent *event)
954 {
955     Q_UNUSED(event);
956     if (m_renderer) m_renderer->doRefresh();
957 }
958
959 Overlay::Overlay(QWidget* parent) :
960     QLabel(parent)
961 {
962     setAttribute(Qt::WA_TransparentForMouseEvents);
963     //setAttribute(Qt::WA_OpaquePaintEvent);
964     //setAttribute(Qt::WA_NoSystemBackground);
965     setAutoFillBackground(true);
966     setBackgroundRole(QPalette::Base);
967 }
968
969 void Overlay::setOverlayText(const QString &text, bool isZone)
970 {
971     setHidden(true);
972     m_isZone = isZone;
973     QPalette p;
974     p.setColor(QPalette::Text, Qt::white);
975     if (m_isZone) p.setColor(QPalette::Base, QColor(200, 0, 0));
976     else p.setColor(QPalette::Base, QColor(0, 0, 200));
977     setPalette(p);
978     setText(' ' + text + ' ');
979     setHidden(false);
980     update();
981 }
982
983 #include "monitor.moc"