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