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