Debian Codegraph @

Source/Core/DolphinQt/QtUtils/AspectRatioWidget.cpp

raw symbols SCIP
1 // Copyright 2018 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 // Based on:
5 // https://stackoverflow.com/questions/30005540/keeping-the-aspect-ratio-of-a-sub-classed-qwidget-during-resize
6
7 #include "DolphinQt/QtUtils/AspectRatioWidget.h"
8
9 #include <QBoxLayout>
10 #include <QResizeEvent>
11
12 AspectRatioWidget::AspectRatioWidget(QWidget* widget, float width, float height, QWidget* parent)
13 : QWidget(parent), m_ar_width(width), m_ar_height(height)
14 {
15 m_layout = new QBoxLayout(QBoxLayout::LeftToRight, this);
16 m_layout->setContentsMargins(0, 0, 0, 0);
17
18 // add spacer, then your widget, then spacer
19 m_layout->addItem(new QSpacerItem(0, 0));
20 m_layout->addWidget(widget);
21 m_layout->addItem(new QSpacerItem(0, 0));
22 }
23
24 void AspectRatioWidget::resizeEvent(QResizeEvent* event)
25 {
26 float aspect_ratio = static_cast<float>(event->size().width()) / event->size().height();
27 int widget_stretch, outer_stretch;
28
29 if (aspect_ratio > (m_ar_width / m_ar_height)) // too wide
30 {
31 m_layout->setDirection(QBoxLayout::LeftToRight);
32 widget_stretch = height() * (m_ar_width / m_ar_height); // i.e., my width
33 outer_stretch = (width() - widget_stretch) / 2 + 0.5;
34 }
35 else // too tall
36 {
37 m_layout->setDirection(QBoxLayout::TopToBottom);
38 widget_stretch = width() * (m_ar_height / m_ar_width); // i.e., my height
39 outer_stretch = (height() - widget_stretch) / 2 + 0.5;
40 }
41
42 m_layout->setStretch(0, outer_stretch);
43 m_layout->setStretch(1, widget_stretch);
44 m_layout->setStretch(2, outer_stretch);
45 }