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