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