]> git.sesse.net Git - kdenlive/blob - src/monitor.cpp
f9043b5a6038004247c37146ff71562d83823ccb
[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 "renderer.h"
23 #include "monitormanager.h"
24 #include "smallruler.h"
25 #include "docclipbase.h"
26 #include "monitorscene.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 #include <QGraphicsView>
45
46
47 Monitor::Monitor(QString name, MonitorManager *manager, QString profile, QWidget *parent) :
48         QWidget(parent),
49         render(NULL),
50         m_name(name),
51         m_monitorManager(manager),
52         m_currentClip(NULL),
53         m_ruler(new SmallRuler(m_monitorManager)),
54         m_overlay(NULL),
55         m_isActive(false),
56         m_scale(1),
57         m_length(0),
58         m_dragStarted(false),
59         m_effectScene(NULL),
60         m_effectView(NULL)
61 {
62     m_ui.setupUi(this);
63     QVBoxLayout *layout = new QVBoxLayout;
64     layout->setContentsMargins(0, 0, 0, 0);
65     layout->addWidget(m_ruler);
66     m_ui.ruler_frame->setLayout(layout);
67     setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
68     setMinimumHeight(200);
69     QToolBar *toolbar = new QToolBar(name, this);
70     QVBoxLayout *layout2 = new QVBoxLayout;
71     layout2->setContentsMargins(0, 0, 0, 0);
72
73     m_playIcon = KIcon("media-playback-start");
74     m_pauseIcon = KIcon("media-playback-pause");
75
76     if (name != "chapter") {
77         toolbar->addAction(KIcon("kdenlive-zone-start"), i18n("Set zone start"), this, SLOT(slotSetZoneStart()));
78         toolbar->addAction(KIcon("kdenlive-zone-end"), i18n("Set zone end"), this, SLOT(slotSetZoneEnd()));
79     } else {
80         m_ruler->setZone(-3, -2);
81     }
82
83     toolbar->addAction(KIcon("media-seek-backward"), i18n("Rewind"), this, SLOT(slotRewind()));
84     //toolbar->addAction(KIcon("media-skip-backward"), i18n("Rewind 1 frame"), this, SLOT(slotRewindOneFrame()));
85
86     QToolButton *playButton = new QToolButton(toolbar);
87     m_playMenu = new QMenu(i18n("Play..."), this);
88     m_playAction = m_playMenu->addAction(m_playIcon, i18n("Play"));
89     m_playAction->setCheckable(true);
90     connect(m_playAction, SIGNAL(triggered()), this, SLOT(slotPlay()));
91
92     playButton->setMenu(m_playMenu);
93     playButton->setPopupMode(QToolButton::MenuButtonPopup);
94     toolbar->addWidget(playButton);
95
96     //toolbar->addAction(KIcon("media-skip-forward"), i18n("Forward 1 frame"), this, SLOT(slotForwardOneFrame()));
97     toolbar->addAction(KIcon("media-seek-forward"), i18n("Forward"), this, SLOT(slotForward()));
98
99     playButton->setDefaultAction(m_playAction);
100
101     if (name != "chapter") {
102         QToolButton *configButton = new QToolButton(toolbar);
103         m_configMenu = new QMenu(i18n("Misc..."), this);
104         configButton->setIcon(KIcon("system-run"));
105         configButton->setMenu(m_configMenu);
106         configButton->setPopupMode(QToolButton::QToolButton::InstantPopup);
107         toolbar->addWidget(configButton);
108
109         if (name == "clip") {
110             m_markerMenu = new QMenu(i18n("Go to marker..."), this);
111             m_markerMenu->setEnabled(false);
112             m_configMenu->addMenu(m_markerMenu);
113             connect(m_markerMenu, SIGNAL(triggered(QAction *)), this, SLOT(slotGoToMarker(QAction *)));
114         }
115         m_configMenu->addAction(KIcon("transform-scale"), i18n("Resize (100%)"), this, SLOT(slotSetSizeOneToOne()));
116         m_configMenu->addAction(KIcon("transform-scale"), i18n("Resize (50%)"), this, SLOT(slotSetSizeOneToTwo()));
117     }
118
119     QWidget *spacer = new QWidget(this);
120     spacer->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
121     toolbar->addWidget(spacer);
122     m_timePos = new TimecodeDisplay(m_monitorManager->timecode(), this);
123     toolbar->addWidget(m_timePos);
124     connect(m_timePos, SIGNAL(editingFinished()), this, SLOT(slotSeek()));
125
126     layout2->addWidget(toolbar);
127     m_ui.button_frame->setLayout(layout2);
128     const int toolHeight = toolbar->height();
129     m_ui.button_frame->setMinimumHeight(toolHeight);
130
131     //m_ruler->setPixelPerMark(3);
132
133     if (profile.isEmpty()) profile = KdenliveSettings::current_profile();
134
135     QVBoxLayout *rendererBox = new QVBoxLayout(m_ui.video_frame);
136     rendererBox->setContentsMargins(0, 0, 0, 0);
137 #ifdef Q_WS_MAC
138     m_glWidget = new VideoGLWidget(m_ui.video_frame);
139     rendererBox->addWidget(m_glWidget);
140     render = new Render(m_name, (int) m_ui.video_frame->winId(), -1, profile, this);
141     m_glWidget->setImageAspectRatio(render->dar());
142     m_glWidget->setBackgroundColor(KdenliveSettings::window_background());
143     m_glWidget->resize(m_ui.video_frame->size());
144     connect(render, SIGNAL(showImageSignal(QImage)), m_glWidget, SLOT(showImage(QImage)));
145     m_monitorRefresh = 0;
146 #else
147     m_monitorRefresh = new MonitorRefresh(m_ui.video_frame);
148     rendererBox->addWidget(m_monitorRefresh);
149     render = new Render(m_name, (int) m_monitorRefresh->winId(), -1, profile, this);
150     m_monitorRefresh->setRenderer(render);
151 #endif
152
153     connect(m_ruler, SIGNAL(seekRenderer(int)), this, SLOT(slotSeek(int)));
154     connect(render, SIGNAL(durationChanged(int)), this, SLOT(adjustRulerSize(int)));
155     connect(render, SIGNAL(rendererStopped(int)), this, SLOT(rendererStopped(int)));
156
157     //render->createVideoXWindow(m_ui.video_frame->winId(), -1);
158
159     if (name != "clip") {
160         connect(render, SIGNAL(rendererPosition(int)), this, SIGNAL(renderPosition(int)));
161         connect(render, SIGNAL(durationChanged(int)), this, SIGNAL(durationChanged(int)));
162         connect(m_ruler, SIGNAL(zoneChanged(QPoint)), this, SIGNAL(zoneUpdated(QPoint)));
163     } else {
164         connect(m_ruler, SIGNAL(zoneChanged(QPoint)), this, SLOT(setClipZone(QPoint)));
165     }
166 #ifndef Q_WS_MAC
167     m_monitorRefresh->show();
168 #endif
169
170     if (name == "project") {
171         m_effectScene = new MonitorScene(render);
172         m_effectView = new QGraphicsView(m_effectScene, m_ui.video_frame);
173         m_effectView->setRenderHints(QFlags<QPainter::RenderHint>());
174         m_effectView->scale(((double) render->renderWidth()) / render->frameRenderWidth(), 1.0);
175         rendererBox->addWidget(m_effectView);
176         m_effectView->setMouseTracking(true);
177         m_effectScene->setUp();
178         m_effectView->hide();
179     }
180
181     kDebug() << "/////// BUILDING MONITOR, ID: " << m_ui.video_frame->winId();
182 }
183
184 Monitor::~Monitor()
185 {
186     delete m_ruler;
187     delete m_timePos;
188     delete m_overlay;
189     if (m_name == "project") {
190         delete m_effectView;
191         delete m_effectScene;
192     }
193     delete m_monitorRefresh;
194     delete render;
195 }
196
197 QString Monitor::name() const
198 {
199     return m_name;
200 }
201
202 void Monitor::setupMenu(QMenu *goMenu, QAction *playZone, QAction *loopZone, QMenu *markerMenu)
203 {
204     m_contextMenu = new QMenu(this);
205     m_contextMenu->addMenu(m_playMenu);
206     if (goMenu) m_contextMenu->addMenu(goMenu);
207     if (markerMenu) m_contextMenu->addMenu(markerMenu);
208
209     m_playMenu->addAction(playZone);
210     m_playMenu->addAction(loopZone);
211
212     //TODO: add save zone to timeline monitor when fixed
213     if (m_name == "clip") {
214         m_contextMenu->addMenu(m_markerMenu);
215         m_contextMenu->addAction(KIcon("document-save"), i18n("Save zone"), this, SLOT(slotSaveZone()));
216     }
217     QAction *extractFrame = m_configMenu->addAction(KIcon("document-new"), i18n("Extract frame"), this, SLOT(slotExtractCurrentFrame()));
218     m_contextMenu->addAction(extractFrame);
219
220     if (m_name != "clip") {
221 #ifndef Q_WS_MAC
222         QAction *splitView = m_contextMenu->addAction(KIcon("view-split-left-right"), i18n("Split view"), render, SLOT(slotSplitView(bool)));
223         splitView->setCheckable(true);
224         m_configMenu->addAction(splitView);
225 #endif
226     } else {
227         QAction *setThumbFrame = m_contextMenu->addAction(KIcon("document-new"), i18n("Set current image as thumbnail"), this, SLOT(slotSetThumbFrame()));
228         m_configMenu->addAction(setThumbFrame);
229     }
230
231     QAction *showTips = m_contextMenu->addAction(KIcon("help-hint"), i18n("Monitor overlay infos"));
232     showTips->setCheckable(true);
233     connect(showTips, SIGNAL(toggled(bool)), this, SLOT(slotSwitchMonitorInfo(bool)));
234     showTips->setChecked(KdenliveSettings::displayMonitorInfo());
235
236     QAction *dropFrames = m_contextMenu->addAction(KIcon(), i18n("Real time (drop frames)"));
237     dropFrames->setCheckable(true);
238     dropFrames->setChecked(true);
239     connect(dropFrames, SIGNAL(toggled(bool)), this, SLOT(slotSwitchDropFrames(bool)));
240
241     m_configMenu->addAction(showTips);
242     m_configMenu->addAction(dropFrames);
243
244 }
245
246 void Monitor::slotGoToMarker(QAction *action)
247 {
248     int pos = action->data().toInt();
249     slotSeek(pos);
250 }
251
252 void Monitor::slotSetSizeOneToOne()
253 {
254     QRect r = QApplication::desktop()->screenGeometry();
255     const int maxWidth = r.width() - 20;
256     const int maxHeight = r.height() - 20;
257     int width = render->renderWidth();
258     int height = render->renderHeight();
259     kDebug() << "// render info: " << width << "x" << height;
260     while (width >= maxWidth || height >= maxHeight) {
261         width = width * 0.8;
262         height = height * 0.8;
263     }
264     kDebug() << "// MONITOR; set SIZE: " << width << ", " << height;
265     m_ui.video_frame->setFixedSize(width, height);
266     updateGeometry();
267     adjustSize();
268     //m_ui.video_frame->setMinimumSize(0, 0);
269     emit adjustMonitorSize();
270 }
271
272 void Monitor::slotSetSizeOneToTwo()
273 {
274     QRect r = QApplication::desktop()->screenGeometry();
275     const int maxWidth = r.width() - 20;
276     const int maxHeight = r.height() - 20;
277     int width = render->renderWidth() / 2;
278     int height = render->renderHeight() / 2;
279     kDebug() << "// render info: " << width << "x" << height;
280     while (width >= maxWidth || height >= maxHeight) {
281         width = width * 0.8;
282         height = height * 0.8;
283     }
284     kDebug() << "// MONITOR; set SIZE: " << width << ", " << height;
285     m_ui.video_frame->setFixedSize(width, height);
286     updateGeometry();
287     adjustSize();
288     //m_ui.video_frame->setMinimumSize(0, 0);
289     emit adjustMonitorSize();
290 }
291
292 void Monitor::resetSize()
293 {
294     m_ui.video_frame->setMinimumSize(0, 0);
295 }
296
297 DocClipBase *Monitor::activeClip()
298 {
299     return m_currentClip;
300 }
301
302 void Monitor::updateMarkers(DocClipBase *source)
303 {
304     if (source == m_currentClip && source != NULL) {
305         m_markerMenu->clear();
306         QList <CommentedTime> markers = m_currentClip->commentedSnapMarkers();
307         if (!markers.isEmpty()) {
308             QList <int> marks;
309             for (int i = 0; i < markers.count(); i++) {
310                 int pos = (int) markers.at(i).time().frames(m_monitorManager->timecode().fps());
311                 marks.append(pos);
312                 QString position = m_monitorManager->timecode().getTimecode(markers.at(i).time()) + ' ' + markers.at(i).comment();
313                 QAction *go = m_markerMenu->addAction(position);
314                 go->setData(pos);
315             }
316             m_ruler->setMarkers(marks);
317         } else m_ruler->setMarkers(QList <int>());
318         m_markerMenu->setEnabled(!m_markerMenu->isEmpty());
319     }
320 }
321
322 void Monitor::slotSeekToPreviousSnap()
323 {
324     if (m_currentClip) slotSeek(getSnapForPos(true).frames(m_monitorManager->timecode().fps()));
325 }
326
327 void Monitor::slotSeekToNextSnap()
328 {
329     if (m_currentClip) slotSeek(getSnapForPos(false).frames(m_monitorManager->timecode().fps()));
330 }
331
332 GenTime Monitor::position()
333 {
334     return render->seekPosition();
335 }
336
337 GenTime Monitor::getSnapForPos(bool previous)
338 {
339     QList <GenTime> snaps;
340     QList < GenTime > markers = m_currentClip->snapMarkers();
341     for (int i = 0; i < markers.size(); ++i) {
342         GenTime t = markers.at(i);
343         snaps.append(t);
344     }
345     QPoint zone = m_ruler->zone();
346     snaps.append(GenTime(zone.x(), m_monitorManager->timecode().fps()));
347     snaps.append(GenTime(zone.y(), m_monitorManager->timecode().fps()));
348     snaps.append(GenTime());
349     snaps.append(m_currentClip->duration());
350     qSort(snaps);
351
352     const GenTime pos = render->seekPosition();
353     for (int i = 0; i < snaps.size(); ++i) {
354         if (previous && snaps.at(i) >= pos) {
355             if (i == 0) i = 1;
356             return snaps.at(i - 1);
357         } else if (!previous && snaps.at(i) > pos) {
358             return snaps.at(i);
359         }
360     }
361     return GenTime();
362 }
363
364 void Monitor::slotZoneMoved(int start, int end)
365 {
366     m_ruler->setZone(start, end);
367     checkOverlay();
368     setClipZone(m_ruler->zone());
369 }
370
371 void Monitor::slotSetZoneStart()
372 {
373     m_ruler->setZone(render->seekFramePosition(), -1);
374     emit zoneUpdated(m_ruler->zone());
375     checkOverlay();
376     setClipZone(m_ruler->zone());
377 }
378
379 void Monitor::slotSetZoneEnd()
380 {
381     m_ruler->setZone(-1, render->seekFramePosition());
382     emit zoneUpdated(m_ruler->zone());
383     checkOverlay();
384     setClipZone(m_ruler->zone());
385 }
386
387 // virtual
388 void Monitor::mousePressEvent(QMouseEvent * event)
389 {
390     if (event->button() != Qt::RightButton) {
391         if (m_ui.video_frame->underMouse()) {
392             m_dragStarted = true;
393             m_DragStartPosition = event->pos();
394         }
395     } else m_contextMenu->popup(event->globalPos());
396 }
397
398 // virtual
399 void Monitor::mouseReleaseEvent(QMouseEvent * event)
400 {
401     if (m_dragStarted) {
402         if (m_ui.video_frame->underMouse() && (!m_effectView || !m_effectView->isVisible())) {
403             if (isActive()) slotPlay();
404             else activateMonitor();
405         } else QWidget::mouseReleaseEvent(event);
406         m_dragStarted = false;
407     }
408 }
409
410 // virtual
411 void Monitor::mouseMoveEvent(QMouseEvent *event)
412 {
413     // kDebug() << "// DRAG STARTED, MOUSE MOVED: ";
414     if (!m_dragStarted || m_currentClip == NULL) return;
415
416     if ((event->pos() - m_DragStartPosition).manhattanLength()
417             < QApplication::startDragDistance())
418         return;
419
420     {
421         QDrag *drag = new QDrag(this);
422         QMimeData *mimeData = new QMimeData;
423
424         QStringList list;
425         list.append(m_currentClip->getId());
426         QPoint p = m_ruler->zone();
427         list.append(QString::number(p.x()));
428         list.append(QString::number(p.y()));
429         QByteArray data;
430         data.append(list.join(";").toUtf8());
431         mimeData->setData("kdenlive/clip", data);
432         drag->setMimeData(mimeData);
433         QPixmap pix = m_currentClip->thumbnail();
434         drag->setPixmap(pix);
435         drag->setHotSpot(QPoint(0, 50));
436         drag->start(Qt::MoveAction);
437
438         //Qt::DropAction dropAction;
439         //dropAction = drag->start(Qt::CopyAction | Qt::MoveAction);
440
441         //Qt::DropAction dropAction = drag->exec();
442
443     }
444     //event->accept();
445 }
446
447 /*void Monitor::dragMoveEvent(QDragMoveEvent * event) {
448     event->setDropAction(Qt::IgnoreAction);
449     event->setDropAction(Qt::MoveAction);
450     if (event->mimeData()->hasText()) {
451         event->acceptProposedAction();
452     }
453 }
454
455 Qt::DropActions Monitor::supportedDropActions() const {
456     // returns what actions are supported when dropping
457     return Qt::MoveAction;
458 }*/
459
460 QStringList Monitor::mimeTypes() const
461 {
462     QStringList qstrList;
463     // list of accepted mime types for drop
464     qstrList.append("kdenlive/clip");
465     return qstrList;
466 }
467
468 // virtual
469 void Monitor::wheelEvent(QWheelEvent * event)
470 {
471     if (event->modifiers() == Qt::ControlModifier) {
472         int delta = m_monitorManager->timecode().fps();
473         if (event->delta() > 0) delta = 0 - delta;
474         slotSeek(render->seekFramePosition() - delta);
475     } else {
476         if (event->delta() >= 0) slotForwardOneFrame();
477         else slotRewindOneFrame();
478     }
479 }
480
481 void Monitor::slotSetThumbFrame()
482 {
483     if (m_currentClip == NULL) {
484         return;
485     }
486     m_currentClip->setClipThumbFrame((uint) render->seekFramePosition());
487     emit refreshClipThumbnail(m_currentClip->getId());
488 }
489
490 void Monitor::slotExtractCurrentFrame()
491 {
492     QImage frame = render->extractFrame(render->seekFramePosition());
493     KFileDialog *fs = new KFileDialog(KUrl(), "image/png", this);
494     fs->setOperationMode(KFileDialog::Saving);
495     fs->setMode(KFile::File);
496 #if KDE_IS_VERSION(4,2,0)
497     fs->setConfirmOverwrite(true);
498 #endif
499     fs->setKeepLocation(true);
500     fs->exec();
501     QString path = fs->selectedFile();
502     delete fs;
503     if (!path.isEmpty()) {
504         frame.save(path);
505     }
506 }
507
508 bool Monitor::isActive() const
509 {
510     return m_isActive;
511 }
512
513 void Monitor::activateMonitor()
514 {
515     if (!m_isActive) {
516         m_monitorManager->slotSwitchMonitors(m_name == "clip");
517     }
518 }
519
520 void Monitor::setTimePos(const QString &pos)
521 {
522     m_timePos->setValue(pos);
523     slotSeek();
524 }
525
526 void Monitor::slotSeek()
527 {
528     slotSeek(m_timePos->getValue());
529 }
530
531 void Monitor::slotSeek(int pos)
532 {
533     activateMonitor();
534     if (render == NULL) return;
535     render->seekToFrame(pos);
536 }
537
538 void Monitor::checkOverlay()
539 {
540     if (m_overlay == NULL) return;
541     int pos = render->seekFramePosition();
542     QPoint zone = m_ruler->zone();
543     if (pos == zone.x())
544         m_overlay->setOverlayText(i18n("In Point"));
545     else if (pos == zone.y())
546         m_overlay->setOverlayText(i18n("Out Point"));
547     else {
548         if (m_currentClip) {
549             QString markerComment = m_currentClip->markerComment(GenTime(pos, m_monitorManager->timecode().fps()));
550             if (markerComment.isEmpty())
551                 m_overlay->setHidden(true);
552             else
553                 m_overlay->setOverlayText(markerComment, false);
554         } else m_overlay->setHidden(true);
555     }
556 }
557
558 void Monitor::slotStart()
559 {
560     activateMonitor();
561     render->play(0);
562     render->seekToFrame(0);
563 }
564
565 void Monitor::slotEnd()
566 {
567     activateMonitor();
568     render->play(0);
569     render->seekToFrame(render->getLength());
570 }
571
572 void Monitor::slotZoneStart()
573 {
574     activateMonitor();
575     render->play(0);
576     render->seekToFrame(m_ruler->zone().x());
577 }
578
579 void Monitor::slotZoneEnd()
580 {
581     activateMonitor();
582     render->play(0);
583     render->seekToFrame(m_ruler->zone().y());
584 }
585
586 void Monitor::slotRewind(double speed)
587 {
588     activateMonitor();
589     if (speed == 0) {
590         double currentspeed = render->playSpeed();
591         if (currentspeed >= 0) render->play(-2);
592         else render->play(currentspeed * 2);
593     } else render->play(speed);
594     m_playAction->setChecked(true);
595     m_playAction->setIcon(m_pauseIcon);
596 }
597
598 void Monitor::slotForward(double speed)
599 {
600     activateMonitor();
601     if (speed == 0) {
602         double currentspeed = render->playSpeed();
603         if (currentspeed <= 1) render->play(2);
604         else render->play(currentspeed * 2);
605     } else render->play(speed);
606     m_playAction->setChecked(true);
607     m_playAction->setIcon(m_pauseIcon);
608 }
609
610 void Monitor::slotRewindOneFrame(int diff)
611 {
612     activateMonitor();
613     render->play(0);
614     render->seekToFrameDiff(-diff);
615 }
616
617 void Monitor::slotForwardOneFrame(int diff)
618 {
619     activateMonitor();
620     render->play(0);
621     render->seekToFrameDiff(diff);
622 }
623
624 void Monitor::seekCursor(int pos)
625 {
626     activateMonitor();
627     if (m_ruler->slotNewValue(pos)) {
628         checkOverlay();
629         m_timePos->setValue(pos);
630     }
631 }
632
633 void Monitor::rendererStopped(int pos)
634 {
635     if (m_ruler->slotNewValue(pos)) {
636         checkOverlay();
637         m_timePos->setValue(pos);
638     }
639     disconnect(m_playAction, SIGNAL(triggered()), this, SLOT(slotPlay()));
640     m_playAction->setChecked(false);
641     connect(m_playAction, SIGNAL(triggered()), this, SLOT(slotPlay()));
642     m_playAction->setIcon(m_playIcon);
643 }
644
645 void Monitor::initMonitor()
646 {
647     kDebug() << "/////// INITING MONITOR, ID: " << m_ui.video_frame->winId();
648 }
649
650 // virtual
651 /*void Monitor::resizeEvent(QResizeEvent * event) {
652     QWidget::resizeEvent(event);
653     adjustRulerSize(-1);
654     if (render && m_isActive) render->doRefresh();
655     //
656 }*/
657
658 void Monitor::adjustRulerSize(int length)
659 {
660     if (length > 0) m_length = length;
661     m_ruler->adjustScale(m_length);
662     if (m_currentClip != NULL) {
663         QPoint zone = m_currentClip->zone();
664         m_ruler->setZone(zone.x(), zone.y());
665     }
666 }
667
668 void Monitor::stop()
669 {
670     m_isActive = false;
671     disconnect(render, SIGNAL(rendererPosition(int)), this, SLOT(seekCursor(int)));
672     if (render) render->stop();
673 }
674
675 void Monitor::start()
676 {
677     m_isActive = true;
678     if (render) render->start();
679     connect(render, SIGNAL(rendererPosition(int)), this, SLOT(seekCursor(int)));
680 }
681
682 void Monitor::refreshMonitor(bool visible)
683 {
684     if (visible && render && !m_isActive) {
685         activateMonitor();
686         render->doRefresh(); //askForRefresh();
687     }
688 }
689
690 void Monitor::pause()
691 {
692     if (render == NULL) return;
693     activateMonitor();
694     render->pause();
695     //m_playAction->setChecked(true);
696     //m_playAction->setIcon(m_pauseIcon);
697 }
698
699 void Monitor::slotPlay()
700 {
701     if (render == NULL) return;
702     activateMonitor();
703     if (render->playSpeed() == 0) {
704         m_playAction->setChecked(true);
705         m_playAction->setIcon(m_pauseIcon);
706     } else {
707         m_playAction->setChecked(false);
708         m_playAction->setIcon(m_playIcon);
709     }
710     render->switchPlay();
711 }
712
713 void Monitor::slotPlayZone()
714 {
715     if (render == NULL) return;
716     activateMonitor();
717     QPoint p = m_ruler->zone();
718     render->playZone(GenTime(p.x(), m_monitorManager->timecode().fps()), GenTime(p.y(), m_monitorManager->timecode().fps()));
719     m_playAction->setChecked(true);
720     m_playAction->setIcon(m_pauseIcon);
721 }
722
723 void Monitor::slotLoopZone()
724 {
725     if (render == NULL) return;
726     activateMonitor();
727     QPoint p = m_ruler->zone();
728     render->loopZone(GenTime(p.x(), m_monitorManager->timecode().fps()), GenTime(p.y(), m_monitorManager->timecode().fps()));
729     m_playAction->setChecked(true);
730     m_playAction->setIcon(m_pauseIcon);
731 }
732
733 void Monitor::slotSetXml(DocClipBase *clip, QPoint zone, const int position)
734 {
735     if (render == NULL) return;
736     if (clip == NULL && m_currentClip != NULL) {
737         m_currentClip = NULL;
738         m_length = -1;
739         render->setProducer(NULL, -1);
740         return;
741     }
742     if (m_currentClip != NULL) activateMonitor();
743     if (clip != m_currentClip) {
744         m_currentClip = clip;
745         updateMarkers(clip);
746         if (render->setProducer(clip->producer(), position) == -1) {
747             // MLT CONSUMER is broken
748             kDebug(QtWarningMsg) << "ERROR, Cannot start monitor";
749         }
750     } else if (position != -1) render->seek(GenTime(position, m_monitorManager->timecode().fps()));
751     if (!zone.isNull()) {
752         m_ruler->setZone(zone.x(), zone.y());
753         render->seek(GenTime(zone.x(), m_monitorManager->timecode().fps()));
754     }
755 }
756
757 void Monitor::slotOpenFile(const QString &file)
758 {
759     if (render == NULL) return;
760     activateMonitor();
761     QDomDocument doc;
762     QDomElement mlt = doc.createElement("mlt");
763     doc.appendChild(mlt);
764     QDomElement prod = doc.createElement("producer");
765     mlt.appendChild(prod);
766     prod.setAttribute("mlt_service", "avformat");
767     prod.setAttribute("resource", file);
768     render->setSceneList(doc, 0);
769 }
770
771 void Monitor::slotSaveZone()
772 {
773     if (render == NULL) return;
774     emit saveZone(render, m_ruler->zone());
775
776     //render->setSceneList(doc, 0);
777 }
778
779 void Monitor::resetProfile(const QString profile)
780 {
781     m_timePos->updateTimeCode(m_monitorManager->timecode());
782     if (render == NULL) return;
783     render->resetProfile(profile);
784     if (m_effectScene) {
785         m_effectView->scale(((double) render->renderWidth()) / render->frameRenderWidth(), 1.0);
786         m_effectScene->resetProfile();
787     }
788 }
789
790 void Monitor::saveSceneList(QString path, QDomElement info)
791 {
792     if (render == NULL) return;
793     render->saveSceneList(path, info);
794 }
795
796 const QString Monitor::sceneList()
797 {
798     if (render == NULL) return QString();
799     return render->sceneList();
800 }
801
802 void Monitor::setClipZone(QPoint pos)
803 {
804     if (m_currentClip == NULL) return;
805     m_currentClip->setZone(pos);
806 }
807
808 void Monitor::slotSwitchDropFrames(bool show)
809 {
810     render->setDropFrames(show);
811 }
812
813 void Monitor::slotSwitchMonitorInfo(bool show)
814 {
815     KdenliveSettings::setDisplayMonitorInfo(show);
816     if (show) {
817         if (m_overlay) return;
818 #ifndef Q_WS_MAC
819         m_overlay = new Overlay(m_monitorRefresh);
820         m_overlay->raise();
821         m_overlay->setHidden(true);
822 #else
823         m_overlay = new Overlay(m_glWidget);
824 #endif
825     } else {
826         delete m_overlay;
827         m_overlay = NULL;
828     }
829 }
830
831 void Monitor::updateTimecodeFormat()
832 {
833     m_timePos->slotUpdateTimeCodeFormat();
834 }
835
836 QStringList Monitor::getZoneInfo() const
837 {
838     QStringList result;
839     if (m_currentClip == NULL) return result;
840     result << m_currentClip->getId();
841     QPoint zone = m_ruler->zone();
842     result << QString::number(zone.x()) << QString::number(zone.y());
843     return result;
844 }
845
846 void Monitor::slotEffectScene(bool show)
847 {
848     if (m_name == "project") {
849 #ifdef Q_WS_MAC
850         m_glWidget->setVisible(!show);
851 #else
852         m_monitorRefresh->setVisible(!show);
853 #endif
854         m_effectView->setVisible(show);
855         emit requestFrameForAnalysis(show);
856         if (show) {
857             render->doRefresh();
858             m_effectScene->slotZoomFit();
859         }
860     }
861 }
862
863 MonitorScene * Monitor::getEffectScene()
864 {
865     return m_effectScene;
866 }
867
868 bool Monitor::effectSceneDisplayed()
869 {
870     return m_effectView->isVisible();
871 }
872
873 MonitorRefresh::MonitorRefresh(QWidget* parent) :
874         QWidget(parent),
875         m_renderer(NULL)
876 {
877     setAttribute(Qt::WA_PaintOnScreen);
878     setAttribute(Qt::WA_OpaquePaintEvent);
879     //setAttribute(Qt::WA_NoSystemBackground);
880 }
881
882 void MonitorRefresh::setRenderer(Render* render)
883 {
884     m_renderer = render;
885 }
886
887 void MonitorRefresh::paintEvent(QPaintEvent *event)
888 {
889     Q_UNUSED(event);
890     if (m_renderer) m_renderer->doRefresh();
891 }
892
893 Overlay::Overlay(QWidget* parent) :
894         QLabel(parent)
895 {
896     setAttribute(Qt::WA_TransparentForMouseEvents);
897     //setAttribute(Qt::WA_OpaquePaintEvent);
898     //setAttribute(Qt::WA_NoSystemBackground);
899     setAutoFillBackground(true);
900     setBackgroundRole(QPalette::Base);
901 }
902
903 void Overlay::setOverlayText(const QString &text, bool isZone)
904 {
905     setHidden(true);
906     m_isZone = isZone;
907     QPalette p;
908     p.setColor(QPalette::Text, Qt::white);
909     if (m_isZone) p.setColor(QPalette::Base, QColor(200, 0, 0));
910     else p.setColor(QPalette::Base, QColor(0, 0, 200));
911     setPalette(p);
912     setText(' ' + text + ' ');
913     setHidden(false);
914     update();
915 }
916
917 #include "monitor.moc"