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