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