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