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