OpenShot Library | libopenshot  0.3.3
VideoRenderWidget.cpp
Go to the documentation of this file.
1 
9 // Copyright (c) 2008-2019 OpenShot Studios, LLC
10 //
11 // SPDX-License-Identifier: LGPL-3.0-or-later
12 
13 #include "VideoRenderWidget.h"
14 #include <QWidget>
15 #include <QImage>
16 #include <QPainter>
17 #include <QPaintEvent>
18 #include <QSizePolicy>
19 #include <QPalette>
20 
21 
23  : QWidget(parent), renderer(new VideoRenderer(this))
24 {
25  QPalette p = palette();
26  p.setColor(QPalette::Window, Qt::black);
27  setPalette(p);
28  setAttribute(Qt::WA_OpaquePaintEvent);
29  setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
30 
31  // init aspect ratio settings (default values)
32  aspect_ratio.num = 16;
33  aspect_ratio.den = 9;
34  pixel_ratio.num = 1;
35  pixel_ratio.den = 1;
36 
37  connect(renderer, SIGNAL(present(const QImage &)), this, SLOT(present(const QImage &)));
38 }
39 
41 {
42 }
43 
45 {
46  return renderer;
47 }
48 
50 {
51  aspect_ratio = new_aspect_ratio;
52  pixel_ratio = new_pixel_ratio;
53 }
54 
55 QRect VideoRenderWidget::centeredViewport(int width, int height)
56 {
57  // calculate aspect ratio
58  float aspectRatio = aspect_ratio.ToFloat() * pixel_ratio.ToFloat();
59  int heightFromWidth = (int) (width / aspectRatio);
60  int widthFromHeight = (int) (height * aspectRatio);
61 
62  if (heightFromWidth <= height) {
63  return QRect(0,(height - heightFromWidth) / 2, width, heightFromWidth);
64  } else {
65  return QRect((width - widthFromHeight) / 2.0, 0, widthFromHeight, height);
66  }
67 }
68 
69 void VideoRenderWidget::paintEvent(QPaintEvent *event)
70 {
71  QPainter painter(this);
72 
73  // maintain aspect ratio
74  painter.fillRect(event->rect(), palette().window());
75  painter.setViewport(centeredViewport(width(), height()));
76  painter.drawImage(QRect(0, 0, width(), height()), image);
77 
78 }
79 
80 void VideoRenderWidget::present(const QImage &m)
81 {
82  image = m;
83  repaint();
84 }
openshot::Fraction::ToFloat
float ToFloat()
Return this fraction as a float (i.e. 1/2 = 0.5)
Definition: Fraction.cpp:35
VideoRenderWidget::GetRenderer
VideoRenderer * GetRenderer() const
Definition: VideoRenderWidget.cpp:44
openshot::Fraction
This class represents a fraction.
Definition: Fraction.h:30
VideoRenderWidget::paintEvent
void paintEvent(QPaintEvent *event)
Definition: VideoRenderWidget.cpp:69
VideoRenderWidget::centeredViewport
QRect centeredViewport(int width, int height)
Definition: VideoRenderWidget.cpp:55
VideoRenderWidget::VideoRenderWidget
VideoRenderWidget(QWidget *parent=0)
Definition: VideoRenderWidget.cpp:22
openshot::Fraction::num
int num
Numerator for the fraction.
Definition: Fraction.h:32
openshot::Fraction::den
int den
Denominator for the fraction.
Definition: Fraction.h:33
VideoRenderer
Definition: VideoRenderer.h:24
VideoRenderWidget::SetAspectRatio
void SetAspectRatio(openshot::Fraction new_aspect_ratio, openshot::Fraction new_pixel_ratio)
Definition: VideoRenderWidget.cpp:49
VideoRenderWidget::~VideoRenderWidget
~VideoRenderWidget()
Definition: VideoRenderWidget.cpp:40
VideoRenderWidget.h
Header file for Video RendererWidget class.