-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaspectratiolayout.cpp
89 lines (71 loc) · 1.88 KB
/
aspectratiolayout.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include "aspectratiolayout.h"
AspectRatioLayout::AspectRatioLayout(QWidget *parent)
: QLayout(parent), item(nullptr)
{}
AspectRatioLayout::~AspectRatioLayout()
{
if (item)
delete item;
}
void AspectRatioLayout::addItem(QLayoutItem *item)
{
if(this->item)
qWarning("AspectRatioLayout: Attempting to add more items.");
this->item = item;
}
QLayoutItem *AspectRatioLayout::itemAt(int index) const
{
if (index == 0)
return item;
return nullptr;
}
QLayoutItem *AspectRatioLayout::takeAt(int index)
{
if (index == 0) {
QLayoutItem *temp = item;
item = nullptr;
return temp;
}
return nullptr;
}
int AspectRatioLayout::count() const
{
if (item)
return 1;
return 0;
}
QSize AspectRatioLayout::minimumSize() const
{
if (item)
return item->minimumSize();
return QSize();
}
QSize AspectRatioLayout::sizeHint() const
{
if (item)
return item->sizeHint();
return QSize();
}
void AspectRatioLayout::setGeometry(const QRect &rect)
{
QLayout::setGeometry(rect);
if (!item) return;
auto height = rect.height();
QSize hint = item->sizeHint();
qreal aspectRatio = 0;
if (hint.width() && hint.height())
aspectRatio = static_cast<qreal>(hint.width()) / hint.height();
auto currentWidth = qRound(rect.height() * aspectRatio);
if (currentWidth <= rect.width()) {
int offset = (rect.width() - currentWidth) / 2;
item->setGeometry(QRect(rect.x() + offset, rect.y(), currentWidth, rect.height()));
} else {
int currentHeight = qRound(rect.width() / aspectRatio);
int offset = (height - currentHeight) / 2;
item->setGeometry(QRect(rect.x(), rect.y() + offset, rect.width(), currentHeight));
}
}
Qt::Orientations AspectRatioLayout::expandingDirections() const
{
return Qt::Orientations(0);
}