OpenShot Library | libopenshot  0.2.7
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 "VideoRenderWidget.h"
32 #include <QWidget>
33 #include <QImage>
34 #include <QPainter>
35 #include <QPaintEvent>
36 #include <QSizePolicy>
37 #include <QPalette>
38 
39 
41  : QWidget(parent), renderer(new VideoRenderer(this))
42 {
43  QPalette p = palette();
44  p.setColor(QPalette::Window, Qt::black);
45  setPalette(p);
46  setAttribute(Qt::WA_OpaquePaintEvent);
47  setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
48 
49  // init aspect ratio settings (default values)
50  aspect_ratio.num = 16;
51  aspect_ratio.den = 9;
52  pixel_ratio.num = 1;
53  pixel_ratio.den = 1;
54 
55  connect(renderer, SIGNAL(present(const QImage &)), this, SLOT(present(const QImage &)));
56 }
57 
59 {
60 }
61 
63 {
64  return renderer;
65 }
66 
68 {
69  aspect_ratio = new_aspect_ratio;
70  pixel_ratio = new_pixel_ratio;
71 }
72 
73 QRect VideoRenderWidget::centeredViewport(int width, int height)
74 {
75  // calculate aspect ratio
76  float aspectRatio = aspect_ratio.ToFloat() * pixel_ratio.ToFloat();
77  int heightFromWidth = (int) (width / aspectRatio);
78  int widthFromHeight = (int) (height * aspectRatio);
79 
80  if (heightFromWidth <= height) {
81  return QRect(0,(height - heightFromWidth) / 2, width, heightFromWidth);
82  } else {
83  return QRect((width - widthFromHeight) / 2.0, 0, widthFromHeight, height);
84  }
85 }
86 
87 void VideoRenderWidget::paintEvent(QPaintEvent *event)
88 {
89  QPainter painter(this);
90 
91  // maintain aspect ratio
92  painter.fillRect(event->rect(), palette().window());
93  painter.setViewport(centeredViewport(width(), height()));
94  painter.drawImage(QRect(0, 0, width(), height()), image);
95 
96 }
97 
98 void VideoRenderWidget::present(const QImage &m)
99 {
100  image = m;
101  repaint();
102 }
Header file for Video RendererWidget class.
QRect centeredViewport(int width, int height)
VideoRenderer * GetRenderer() const
void paintEvent(QPaintEvent *event)
void SetAspectRatio(openshot::Fraction new_aspect_ratio, openshot::Fraction new_pixel_ratio)
VideoRenderWidget(QWidget *parent=0)
This class represents a fraction.
Definition: Fraction.h:48
int num
Numerator for the fraction.
Definition: Fraction.h:50
float ToFloat()
Return this fraction as a float (i.e. 1/2 = 0.5)
Definition: Fraction.cpp:54
int den
Denominator for the fraction.
Definition: Fraction.h:51