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