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