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