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