-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaspectratiowidget.cpp
31 lines (29 loc) · 1.4 KB
/
aspectratiowidget.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include "aspectratiowidget.h"
#include <QResizeEvent>
#include <QSpacerItem>
AspectRatioWidget::AspectRatioWidget(QWidget *widget, float aspectWidth, float aspectHeight, QWidget *parent)
: mWidget(widget), aspectWidth(aspectWidth), aspectHeight(aspectHeight), QWidget(parent) {
layout = new QBoxLayout(QBoxLayout::LeftToRight, this);
layout->setContentsMargins(0, 0, 0, 0);
layout->addItem(new QSpacerItem(0, 0));
layout->addWidget(widget);
layout->addItem(new QSpacerItem(0, 0));
}
void AspectRatioWidget::resizeEvent(QResizeEvent *event) {
float aspectRatio = static_cast<float>(event->size().width()) / static_cast<float>(event->size().height());
int widgetStretch;
int outerStretch;
if (aspectRatio > aspectWidth / aspectHeight) {
layout->setDirection(QBoxLayout::LeftToRight);
widgetStretch = static_cast<int>(static_cast<float>(height()) * (aspectWidth / aspectHeight));
outerStretch = static_cast<int>(lround(static_cast<float>(event->size().width() - widgetStretch) / 2.0f + 0.5f));
}
else {
layout->setDirection(QBoxLayout::TopToBottom);
widgetStretch = static_cast<int>(static_cast<float>(width()) * (aspectHeight / aspectWidth));
outerStretch = static_cast<int>(lround(static_cast<float>(event->size().height() - widgetStretch) / 2.0f + 0.5f));
}
layout->setStretch(0, outerStretch);
layout->setStretch(1, widgetStretch);
layout->setStretch(2, outerStretch);
}