OpenShot Library | libopenshot  0.1.9
VideoRenderWidget.cpp
Go to the documentation of this file.
1 /**
2  * @file
3  * @brief Source file for Video RendererWidget class
4  * @author Jonathan Thomas <jonathan@openshot.org>
5  *
6  * @section LICENSE
7  *
8  * Copyright (c) 2008-2014 OpenShot Studios, LLC
9  * <http://www.openshotstudios.com/>. This file is part of
10  * OpenShot Library (libopenshot), an open-source project dedicated to
11  * delivering high quality video editing and animation solutions to the
12  * world. For more information visit <http://www.openshot.org/>.
13  *
14  * OpenShot Library (libopenshot) is free software: you can redistribute it
15  * and/or modify it under the terms of the GNU Lesser General Public License
16  * as published by the Free Software Foundation, either version 3 of the
17  * License, or (at your option) any later version.
18  *
19  * OpenShot Library (libopenshot) is distributed in the hope that it will be
20  * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22  * GNU Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public License
25  * along with OpenShot Library. If not, see <http://www.gnu.org/licenses/>.
26  */
27 
28 #include "../../include/Qt/VideoRenderWidget.h"
29 #include <QtGui/QPaintEvent>
30 
32  : QWidget(parent), renderer(new VideoRenderer(this))
33 {
34  QPalette p = palette();
35  p.setColor(QPalette::Window, Qt::black);
36  setPalette(p);
37  setAttribute(Qt::WA_OpaquePaintEvent);
38  setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
39 
40  // init aspect ratio settings (default values)
41  aspect_ratio.num = 16;
42  aspect_ratio.den = 9;
43  pixel_ratio.num = 1;
44  pixel_ratio.den = 1;
45 
46  connect(renderer, SIGNAL(present(const QImage &)), this, SLOT(present(const QImage &)));
47 }
48 
50 {
51 }
52 
54 {
55  return renderer;
56 }
57 
59 {
60  aspect_ratio = new_aspect_ratio;
61  pixel_ratio = new_pixel_ratio;
62 }
63 
64 QRect VideoRenderWidget::centeredViewport(int width, int height)
65 {
66  // calculate aspect ratio
67  float aspectRatio = aspect_ratio.ToFloat() * pixel_ratio.ToFloat();
68  int heightFromWidth = (int) (width / aspectRatio);
69  int widthFromHeight = (int) (height * aspectRatio);
70 
71  if (heightFromWidth <= height) {
72  return QRect(0,(height - heightFromWidth) / 2, width, heightFromWidth);
73  } else {
74  return QRect((width - widthFromHeight) / 2.0, 0, widthFromHeight, height);
75  }
76 }
77 
78 void VideoRenderWidget::paintEvent(QPaintEvent *event)
79 {
80  QPainter painter(this);
81 
82  // maintain aspect ratio
83  painter.fillRect(event->rect(), palette().window());
84  painter.setViewport(centeredViewport(width(), height()));
85  painter.drawImage(QRect(0, 0, width(), height()), image);
86 
87 }
88 
89 void VideoRenderWidget::present(const QImage &m)
90 {
91  image = m;
92  repaint();
93 }
int num
Numerator for the fraction.
Definition: Fraction.h:44
void paintEvent(QPaintEvent *event)
VideoRenderWidget(QWidget *parent=0)
float ToFloat()
Return this fraction as a float (i.e. 1/2 = 0.5)
Definition: Fraction.cpp:41
VideoRenderer * GetRenderer() const
QRect centeredViewport(int width, int height)
This class represents a fraction.
Definition: Fraction.h:42
void SetAspectRatio(openshot::Fraction new_aspect_ratio, openshot::Fraction new_pixel_ratio)
int den
Denominator for the fraction.
Definition: Fraction.h:45