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