2018-01-27 13:35:02 +00:00
|
|
|
// Copyright 2018 Dolphin Emulator Project
|
2021-07-05 01:22:19 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2018-01-27 13:35:02 +00:00
|
|
|
|
|
|
|
// Based on:
|
|
|
|
// https://stackoverflow.com/questions/30005540/keeping-the-aspect-ratio-of-a-sub-classed-qwidget-during-resize
|
|
|
|
|
2018-07-06 22:40:15 +00:00
|
|
|
#include "DolphinQt/QtUtils/AspectRatioWidget.h"
|
2018-01-27 13:35:02 +00:00
|
|
|
|
|
|
|
#include <QBoxLayout>
|
|
|
|
#include <QResizeEvent>
|
|
|
|
|
|
|
|
AspectRatioWidget::AspectRatioWidget(QWidget* widget, float width, float height, QWidget* parent)
|
|
|
|
: QWidget(parent), m_ar_width(width), m_ar_height(height)
|
|
|
|
{
|
|
|
|
m_layout = new QBoxLayout(QBoxLayout::LeftToRight, this);
|
|
|
|
|
|
|
|
// add spacer, then your widget, then spacer
|
|
|
|
m_layout->addItem(new QSpacerItem(0, 0));
|
|
|
|
m_layout->addWidget(widget);
|
|
|
|
m_layout->addItem(new QSpacerItem(0, 0));
|
|
|
|
}
|
|
|
|
|
|
|
|
void AspectRatioWidget::resizeEvent(QResizeEvent* event)
|
|
|
|
{
|
2018-01-31 11:35:09 +00:00
|
|
|
float aspect_ratio = static_cast<float>(event->size().width()) / event->size().height();
|
2018-01-27 13:35:02 +00:00
|
|
|
int widget_stretch, outer_stretch;
|
|
|
|
|
2018-01-31 11:35:09 +00:00
|
|
|
if (aspect_ratio > (m_ar_width / m_ar_height)) // too wide
|
2018-01-27 13:35:02 +00:00
|
|
|
{
|
|
|
|
m_layout->setDirection(QBoxLayout::LeftToRight);
|
|
|
|
widget_stretch = height() * (m_ar_width / m_ar_height); // i.e., my width
|
|
|
|
outer_stretch = (width() - widget_stretch) / 2 + 0.5;
|
|
|
|
}
|
|
|
|
else // too tall
|
|
|
|
{
|
|
|
|
m_layout->setDirection(QBoxLayout::TopToBottom);
|
|
|
|
widget_stretch = width() * (m_ar_height / m_ar_width); // i.e., my height
|
|
|
|
outer_stretch = (height() - widget_stretch) / 2 + 0.5;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_layout->setStretch(0, outer_stretch);
|
|
|
|
m_layout->setStretch(1, widget_stretch);
|
|
|
|
m_layout->setStretch(2, outer_stretch);
|
|
|
|
}
|