]> git.sesse.net Git - kdenlive/blob - src/abstractmonitor.cpp
Merge branch 'master' of git://anongit.kde.org/kdenlive
[kdenlive] / src / abstractmonitor.cpp
1 /***************************************************************************
2  *   Copyright (C) 2011 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 "abstractmonitor.h"
22 #include "kdenlivesettings.h"
23
24 #include <KDebug>
25
26 #include <QPaintEvent>
27
28 VideoPreviewContainer::VideoPreviewContainer(QWidget *parent) :
29     QFrame(parent),
30     m_dar(1.0),
31     m_refresh(false)
32 {
33     setFrameShape(QFrame::NoFrame);
34     setFocusPolicy(Qt::ClickFocus);
35     setAttribute(Qt::WA_PaintOnScreen);
36     setAttribute(Qt::WA_OpaquePaintEvent);
37     setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
38     connect(&m_refreshTimer, SIGNAL(timeout()), this, SLOT(update()));
39     m_refreshTimer.setSingleShot(false);
40     m_refreshTimer.setInterval(200);
41 }
42
43 VideoPreviewContainer::~VideoPreviewContainer()
44 {
45     qDeleteAll(m_imageQueue);
46 }
47
48 //virtual
49 void VideoPreviewContainer::resizeEvent( QResizeEvent * /*event*/ )
50 {
51     updateDisplayZone();
52 }
53
54 void VideoPreviewContainer::setRatio(double ratio)
55 {
56     m_dar = ratio;
57     updateDisplayZone();
58 }
59
60
61 void VideoPreviewContainer::updateDisplayZone()
62 {
63     QRect rect = this->frameRect();
64     int paintW = rect.height() * m_dar + 0.5;
65     if (paintW > rect.width()) {
66         int paintH = rect.width() / m_dar + 0.5;
67         int diff = (rect.height() - paintH)  / 2;
68         rect.adjust(0, diff, 0, 0);
69         rect.setHeight(paintH);
70     }
71     else {
72         int diff = (rect.width() - paintW)  / 2;
73         rect.adjust(diff, 0, 0, 0);
74         rect.setWidth(paintW);
75     }
76     m_displayRect = rect;
77     m_refresh = true;
78 }
79
80 void VideoPreviewContainer::setImage(QImage img)
81 {
82     if (m_imageQueue.count() > 2) {
83         delete m_imageQueue.takeLast();
84     }
85     m_imageQueue.prepend(new QImage(img));
86     update();
87 }
88
89 void VideoPreviewContainer::stop()
90 {
91     //m_refreshTimer.stop();
92     qDeleteAll(m_imageQueue);
93     m_imageQueue.clear();
94 }
95
96 void VideoPreviewContainer::start()
97 {
98     //m_refreshTimer.start();
99 }
100
101 // virtual
102 void VideoPreviewContainer::paintEvent(QPaintEvent *event)
103 {
104     if (m_imageQueue.isEmpty()) return;
105     QImage *img = m_imageQueue.takeFirst();
106     QPainter painter(this);
107     if (m_refresh) {
108         painter.fillRect(event->rect(), QColor(KdenliveSettings::window_background()));
109         m_refresh = false;
110     }
111     painter.drawImage(m_displayRect, *img);
112     delete img;
113 }
114
115