]> git.sesse.net Git - kdenlive/blob - src/monitor.cpp
Fix timeline cursor frozen (wrongly removed line in previous commit):
[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_OPEN_GL)
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 #if defined (USE_OPEN_GL)
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 #if defined(Q_WS_MAC) || defined(USE_OPEN_GL)
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 #if KDE_IS_VERSION(4,2,0)
582     fs->setConfirmOverwrite(true);
583 #endif
584     fs->setKeepLocation(true);
585     fs->exec();
586     QString path = fs->selectedFile();
587     delete fs;
588     if (!path.isEmpty()) {
589         frame.save(path);
590     }
591 }
592
593 bool Monitor::isActive() const
594 {
595     return m_monitorManager->isActive(m_name);
596 }
597
598 bool Monitor::activateMonitor()
599 {
600     return m_monitorManager->activateMonitor(m_name);
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     if (render == NULL) return;
617     activateMonitor();
618     render->seekToFrame(pos);
619 }
620
621 void Monitor::checkOverlay()
622 {
623     if (m_overlay == NULL) return;
624     int pos = m_ruler->position();
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     m_playAction->setIcon(m_playIcon);
723 }
724
725 void Monitor::adjustRulerSize(int length)
726 {
727     if (length > 0) m_length = length;
728     m_ruler->adjustScale(m_length);
729     if (m_currentClip != NULL) {
730         QPoint zone = m_currentClip->zone();
731         m_ruler->setZone(zone.x(), zone.y());
732     }
733 }
734
735 void Monitor::stop()
736 {
737     if (render) render->stop();
738 }
739
740 void Monitor::start()
741 {
742     if (!isVisible()) return;
743     if (render) render->start();
744 }
745
746 void Monitor::refreshMonitor(bool visible)
747 {
748     if (visible && render) {
749         if (!activateMonitor()) {
750             // the monitor was already active, simply refreshClipThumbnail
751             render->doRefresh();
752         }
753     }
754 }
755
756 void Monitor::refreshMonitor()
757 {
758     if (isActive()) {
759         render->doRefresh();
760     }
761 }
762
763 void Monitor::pause()
764 {
765     if (render == NULL) return;
766     activateMonitor();
767     render->pause();
768     //m_playAction->setChecked(true);
769     m_playAction->setIcon(m_playIcon);
770 }
771
772 void Monitor::slotPlay()
773 {
774     if (render == NULL) return;
775     activateMonitor();
776     if (render->playSpeed() == 0.0) {
777         m_playAction->setIcon(m_pauseIcon);
778         render->switchPlay(true);
779     } else {
780         m_playAction->setIcon(m_playIcon);
781         render->switchPlay(false);
782     }
783 }
784
785 void Monitor::slotPlayZone()
786 {
787     if (render == NULL) return;
788     activateMonitor();
789     QPoint p = m_ruler->zone();
790     render->playZone(GenTime(p.x(), m_monitorManager->timecode().fps()), GenTime(p.y(), m_monitorManager->timecode().fps()));
791     //m_playAction->setChecked(true);
792     m_playAction->setIcon(m_pauseIcon);
793 }
794
795 void Monitor::slotLoopZone()
796 {
797     if (render == NULL) return;
798     activateMonitor();
799     QPoint p = m_ruler->zone();
800     render->loopZone(GenTime(p.x(), m_monitorManager->timecode().fps()), GenTime(p.y(), m_monitorManager->timecode().fps()));
801     //m_playAction->setChecked(true);
802     m_playAction->setIcon(m_pauseIcon);
803 }
804
805 void Monitor::slotLoopClip()
806 {
807     if (render == NULL || m_selectedClip == NULL)
808         return;
809     activateMonitor();
810     render->loopZone(m_selectedClip->startPos(), m_selectedClip->endPos());
811     //m_playAction->setChecked(true);
812     m_playAction->setIcon(m_pauseIcon);
813 }
814
815 void Monitor::updateClipProducer(Mlt::Producer *prod)
816 {
817     if (render == NULL) return;
818    render->setProducer(prod, render->seekFramePosition());
819 }
820
821 void Monitor::slotSetClipProducer(DocClipBase *clip, QPoint zone, int position)
822 {
823     if (render == NULL) return;
824     if (clip == NULL && m_currentClip != NULL) {
825         kDebug()<<"// SETTING NULL CLIP MONITOR";
826         m_currentClip = NULL;
827         m_length = -1;
828         render->setProducer(NULL, -1);
829         return;
830     }
831     
832     if (clip != m_currentClip) {
833         m_currentClip = clip;
834         if (m_currentClip) activateMonitor();
835         updateMarkers(clip);
836         Mlt::Producer *prod = NULL;
837         if (clip) prod = clip->getCloneProducer();
838         if (render->setProducer(prod, position) == -1) {
839             // MLT CONSUMER is broken
840             kDebug(QtWarningMsg) << "ERROR, Cannot start monitor";
841         }
842     } else {
843         if (m_currentClip) {
844             activateMonitor();
845             if (position == -1) position = render->seekFramePosition();
846             render->seek(position);
847         }
848     }
849     if (!zone.isNull()) {
850         m_ruler->setZone(zone.x(), zone.y());
851         render->seek(zone.x());
852     }
853 }
854
855 void Monitor::slotOpenFile(const QString &file)
856 {
857     if (render == NULL) return;
858     activateMonitor();
859     QDomDocument doc;
860     QDomElement mlt = doc.createElement("mlt");
861     doc.appendChild(mlt);
862     QDomElement prod = doc.createElement("producer");
863     mlt.appendChild(prod);
864     prod.setAttribute("mlt_service", "avformat");
865     prod.setAttribute("resource", file);
866     render->setSceneList(doc, 0);
867 }
868
869 void Monitor::slotSaveZone()
870 {
871     if (render == NULL) return;
872     emit saveZone(render, m_ruler->zone(), m_currentClip);
873
874     //render->setSceneList(doc, 0);
875 }
876
877 void Monitor::resetProfile(const QString &profile)
878 {
879     m_timePos->updateTimeCode(m_monitorManager->timecode());
880     if (render == NULL) return;
881     if (!render->hasProfile(profile)) {
882         activateMonitor();
883         render->resetProfile(profile);
884     }
885     if (m_effectWidget)
886         m_effectWidget->resetProfile(render);
887 }
888
889 void Monitor::saveSceneList(QString path, QDomElement info)
890 {
891     if (render == NULL) return;
892     render->saveSceneList(path, info);
893 }
894
895 const QString Monitor::sceneList()
896 {
897     if (render == NULL) return QString();
898     return render->sceneList();
899 }
900
901 void Monitor::setClipZone(QPoint pos)
902 {
903     if (m_currentClip == NULL) return;
904     m_currentClip->setZone(pos);
905 }
906
907 void Monitor::slotSwitchDropFrames(bool show)
908 {
909     render->setDropFrames(show);
910 }
911
912 void Monitor::slotSwitchMonitorInfo(bool show)
913 {
914     KdenliveSettings::setDisplayMonitorInfo(show);
915     if (show) {
916         if (m_overlay) return;
917         if (m_monitorRefresh == NULL) {
918             // Using OpenGL display
919 #if defined(Q_WS_MAC) || defined(USE_OPEN_GL)
920             if (m_glWidget->layout()) delete m_glWidget->layout();
921             m_overlay = new Overlay();
922             connect(m_overlay, SIGNAL(editMarker()), this, SLOT(slotEditMarker()));
923             QVBoxLayout *layout = new QVBoxLayout;
924             layout->addStretch(10);
925             layout->addWidget(m_overlay);
926             m_glWidget->setLayout(layout);
927 #endif
928         } else {
929             if (m_monitorRefresh->layout()) delete m_monitorRefresh->layout();
930             m_overlay = new Overlay();
931             connect(m_overlay, SIGNAL(editMarker()), this, SLOT(slotEditMarker()));
932             QVBoxLayout *layout = new QVBoxLayout;
933             layout->addStretch(10);
934             layout->addWidget(m_overlay);
935             m_monitorRefresh->setLayout(layout);
936             m_overlay->raise();
937             m_overlay->setHidden(true);
938         }
939         checkOverlay();
940     } else {
941         delete m_overlay;
942         m_overlay = NULL;
943     }
944 }
945
946 void Monitor::slotEditMarker()
947 {
948     if (m_editMarker) m_editMarker->trigger();
949 }
950
951 void Monitor::updateTimecodeFormat()
952 {
953     m_timePos->slotUpdateTimeCodeFormat();
954 }
955
956 QStringList Monitor::getZoneInfo() const
957 {
958     QStringList result;
959     if (m_currentClip == NULL) return result;
960     result << m_currentClip->getId();
961     QPoint zone = m_ruler->zone();
962     result << QString::number(zone.x()) << QString::number(zone.y());
963     return result;
964 }
965
966 void Monitor::slotSetSelectedClip(AbstractClipItem* item)
967 {
968     if (item) {
969         m_loopClipAction->setEnabled(true);
970         m_selectedClip = item;
971     } else {
972         m_loopClipAction->setEnabled(false);
973     }
974 }
975
976 void Monitor::slotSetSelectedClip(ClipItem* item)
977 {
978     if (item || (!item && !m_loopClipTransition)) {
979         m_loopClipTransition = false;
980         slotSetSelectedClip((AbstractClipItem*)item);
981     }
982 }
983
984 void Monitor::slotSetSelectedClip(Transition* item)
985 {
986     if (item || (!item && m_loopClipTransition)) {
987         m_loopClipTransition = true;
988         slotSetSelectedClip((AbstractClipItem*)item);
989     }
990 }
991
992
993 void Monitor::slotEffectScene(bool show)
994 {
995     if (m_name == "project") {
996         if (m_monitorRefresh) {
997             m_monitorRefresh->setVisible(!show);
998         } else {
999 #if defined(Q_WS_MAC) || defined(USE_OPEN_GL)
1000             m_glWidget->setVisible(!show);
1001 #endif
1002         }
1003         m_effectWidget->setVisible(show);
1004         m_effectWidget->getVisibilityAction()->setChecked(show);
1005         emit requestFrameForAnalysis(show);
1006         if (show) {
1007             m_effectWidget->getScene()->slotZoomFit();
1008         }
1009         render->doRefresh();
1010     }
1011 }
1012
1013 MonitorEditWidget* Monitor::getEffectEdit()
1014 {
1015     return m_effectWidget;
1016 }
1017
1018 bool Monitor::effectSceneDisplayed()
1019 {
1020     return m_effectWidget->isVisible();
1021 }
1022
1023 void Monitor::slotSetVolume(int volume)
1024 {
1025     KdenliveSettings::setVolume(volume);
1026     KIcon icon;
1027     if (volume == 0) icon = KIcon("audio-volume-muted");
1028     else icon = KIcon("audio-volume-medium");
1029     static_cast <QToolButton *>(m_volumeWidget)->setIcon(icon);
1030     render->slotSetVolume(volume);
1031 }
1032
1033 void Monitor::slotShowVolume()
1034 {
1035     m_volumePopup->move(mapToGlobal(m_toolbar->geometry().topLeft()) + QPoint(mapToParent(m_volumeWidget->geometry().bottomLeft()).x(), -m_volumePopup->height()));
1036     int vol = render->volume();
1037     // Disable widget if we cannot get the volume
1038     m_volumePopup->setEnabled(vol != -1);
1039     m_audioSlider->blockSignals(true);
1040     m_audioSlider->setValue(vol);
1041     m_audioSlider->blockSignals(false);
1042     m_volumePopup->show();
1043 }
1044
1045 AbstractRender *Monitor::abstractRender()
1046 {
1047     return render;
1048 }
1049
1050 MonitorRefresh::MonitorRefresh(QWidget* parent) :
1051     QWidget(parent)
1052     , m_renderer(NULL)
1053 {
1054     // MonitorRefresh is used as container for the SDL display (it's window id is passed to SDL)
1055     setAttribute(Qt::WA_PaintOnScreen);
1056     setAttribute(Qt::WA_OpaquePaintEvent);
1057     setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
1058     //setAttribute(Qt::WA_NoSystemBackground);
1059 }
1060
1061 void MonitorRefresh::setRenderer(Render* render)
1062 {
1063     m_renderer = render;
1064 }
1065
1066
1067 Overlay::Overlay(QWidget* parent) :
1068     QLabel(parent)
1069 {
1070     //setAttribute(Qt::WA_TransparentForMouseEvents);
1071     setAutoFillBackground(true);
1072     setBackgroundRole(QPalette::Base);
1073     setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
1074     setCursor(Qt::PointingHandCursor);
1075 }
1076
1077 // virtual
1078 void Overlay::mouseReleaseEvent ( QMouseEvent * event )
1079 {
1080     event->accept();
1081 }
1082
1083 // virtual
1084 void Overlay::mousePressEvent( QMouseEvent * event )
1085 {
1086     event->accept();
1087 }
1088
1089 // virtual
1090 void Overlay::mouseDoubleClickEvent ( QMouseEvent * event )
1091 {
1092     emit editMarker();
1093     event->accept();
1094 }
1095
1096 void Overlay::setOverlayText(const QString &text, bool isZone)
1097 {
1098     setHidden(true);
1099     m_isZone = isZone;
1100     QPalette p;
1101     p.setColor(QPalette::Text, Qt::white);
1102     if (m_isZone) p.setColor(QPalette::Base, QColor(200, 0, 0));
1103     else p.setColor(QPalette::Base, QColor(0, 0, 200));
1104     setPalette(p);
1105     setText(' ' + text + ' ');
1106     setHidden(false);
1107     update();
1108 }
1109
1110 VideoContainer::VideoContainer(Monitor* parent) :
1111     QFrame()
1112     , m_monitor(parent)
1113 {
1114     setFrameShape(QFrame::NoFrame);
1115     setFocusPolicy(Qt::ClickFocus);
1116     setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
1117 }
1118
1119 // virtual
1120 void VideoContainer::mousePressEvent(QMouseEvent * event)
1121 {
1122     if (m_monitor->underMouse()) event->setAccepted(false);
1123 }
1124
1125 // virtual
1126 void VideoContainer::mouseReleaseEvent(QMouseEvent * event)
1127 {
1128     if (m_monitor->underMouse()) event->setAccepted(false);
1129     else {
1130         if (m_monitor->isActive()) {
1131             m_monitor->slotPlay();
1132             event->accept();
1133         }
1134     }
1135 }
1136
1137 // virtual
1138 void VideoContainer::mouseMoveEvent(QMouseEvent *event)
1139 {
1140     if (m_monitor->underMouse()) event->setAccepted(false);
1141 }
1142
1143 // virtual
1144 void VideoContainer::keyPressEvent(QKeyEvent *event)
1145 {
1146     // Exit fullscreen with Esc key
1147     if (event->key() == Qt::Key_Escape && isFullScreen()) {
1148         switchFullScreen();
1149         event->setAccepted(true);
1150     } else event->setAccepted(false);
1151 }
1152
1153 // virtual
1154 void VideoContainer::wheelEvent(QWheelEvent * event)
1155 {
1156     if (m_monitor->underMouse()) event->setAccepted(false);
1157     else {
1158         m_monitor->slotMouseSeek(event->delta(), event->modifiers() == Qt::ControlModifier);
1159         event->accept();
1160     }
1161 }
1162
1163 void VideoContainer::mouseDoubleClickEvent(QMouseEvent * event)
1164 {
1165     Q_UNUSED(event)
1166
1167     if (!KdenliveSettings::openglmonitors())
1168         switchFullScreen();
1169 }
1170
1171 void VideoContainer::switchFullScreen()
1172 {
1173     // TODO: disable screensaver?
1174     Qt::WindowFlags flags = windowFlags();
1175     if (!isFullScreen()) {
1176         // Check if we ahave a multiple monitor setup
1177         setUpdatesEnabled(false);
1178 #if QT_VERSION >= 0x040600
1179         int monitors = QApplication::desktop()->screenCount();
1180 #else
1181         int monitors = QApplication::desktop()->numScreens();
1182 #endif
1183         if (monitors > 1) {
1184             QRect screenres;
1185             // Move monitor widget to the second screen (one screen for Kdenlive, the other one for the Monitor widget
1186             int currentScreen = QApplication::desktop()->screenNumber(this);
1187             if (currentScreen < monitors - 1)
1188                 screenres = QApplication::desktop()->screenGeometry(currentScreen + 1);
1189             else
1190                 screenres = QApplication::desktop()->screenGeometry(currentScreen - 1);
1191             move(QPoint(screenres.x(), screenres.y()));
1192             resize(screenres.width(), screenres.height());
1193         }
1194
1195         m_baseFlags = flags & (Qt::Window | Qt::SubWindow);
1196         flags |= Qt::Window;
1197         flags ^= Qt::SubWindow;
1198         setWindowFlags(flags);
1199 #ifdef Q_WS_X11
1200         // This works around a bug with Compiz
1201         // as the window must be visible before we can set the state
1202         show();
1203         raise();
1204         setWindowState(windowState() | Qt::WindowFullScreen);   // set
1205 #else
1206         setWindowState(windowState() | Qt::WindowFullScreen);   // set
1207         setUpdatesEnabled(true);
1208         show();
1209 #endif
1210     } else {
1211         setUpdatesEnabled(false);
1212         flags ^= (Qt::Window | Qt::SubWindow); //clear the flags...
1213         flags |= m_baseFlags; //then we reset the flags (window and subwindow)
1214         setWindowFlags(flags);
1215         setWindowState(windowState()  ^ Qt::WindowFullScreen);   // reset
1216         setUpdatesEnabled(true);
1217         show();
1218     }
1219     m_monitor->pause();
1220 }
1221
1222
1223 #include "monitor.moc"