2018-01-27 06:35:02 -07:00
|
|
|
// Copyright 2018 Dolphin Emulator Project
|
|
|
|
// Licensed under GPLv2+
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
// Based on:
|
|
|
|
// https://stackoverflow.com/questions/30005540/keeping-the-aspect-ratio-of-a-sub-classed-qwidget-during-resize
|
|
|
|
|
|
|
|
#include "DolphinQt2/QtUtils/AspectRatioWidget.h"
|
|
|
|
|
|
|
|
#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 04:35:09 -07:00
|
|
|
float aspect_ratio = static_cast<float>(event->size().width()) / event->size().height();
|
2018-01-27 06:35:02 -07:00
|
|
|
int widget_stretch, outer_stretch;
|
|
|
|
|
2018-01-31 04:35:09 -07:00
|
|
|
if (aspect_ratio > (m_ar_width / m_ar_height)) // too wide
|
2018-01-27 06:35:02 -07: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);
|
|
|
|
}
|