-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathflowlayout.cpp
More file actions
181 lines (151 loc) · 6.04 KB
/
flowlayout.cpp
File metadata and controls
181 lines (151 loc) · 6.04 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#include "flowlayout.h"
#include <QWidget>
FlowLayout::FlowLayout(QWidget *parent, int margin, int hSpacing, int vSpacing)
: QLayout(parent), m_hSpace(hSpacing), m_vSpace(vSpacing)
{
setContentsMargins(margin, margin, margin, margin);
}
//------------------------------------------------------------------------------------------------------------------------------------------
FlowLayout::~FlowLayout()
{
QLayoutItem *item;
while ((item = takeAt(0)))
delete item;
}
//------------------------------------------------------------------------------------------------------------------------------------------
void FlowLayout::addItem(QLayoutItem *item)
{
itemList.append(item);
}
//------------------------------------------------------------------------------------------------------------------------------------------
int FlowLayout::count() const
{
return itemList.size();
}
//------------------------------------------------------------------------------------------------------------------------------------------
QLayoutItem *FlowLayout::itemAt(int index) const
{
return itemList.value(index);
}
//------------------------------------------------------------------------------------------------------------------------------------------
QLayoutItem *FlowLayout::takeAt(int index)
{
return index >= 0 && index < itemList.size() ? itemList.takeAt(index) : nullptr;
}
//------------------------------------------------------------------------------------------------------------------------------------------
int FlowLayout::smartSpacing(QStyle::PixelMetric pm) const
{
QObject *parent = this->parent();
if (!parent) return -1;
if (parent->isWidgetType()) {
QWidget *pw = static_cast<QWidget *>(parent);
return pw->style()->pixelMetric(pm, nullptr, pw);
}
return static_cast<QLayout *>(parent)->spacing();
}
//------------------------------------------------------------------------------------------------------------------------------------------
int FlowLayout::doLayout(const QRect &rect, bool testOnly) const
{
int x = rect.x();
int y = rect.y();
int lineHeight = 0;
for (QLayoutItem *item : itemList) {
QWidget *wid = item->widget();
int spaceX = horizontalSpacing();
int spaceY = verticalSpacing();
QSize itemSize = item->sizeHint();
if (m_wrap) {
if (x + itemSize.width() > rect.right() && lineHeight > 0) {
x = rect.x();
y += lineHeight + spaceY;
lineHeight = 0;
}
}
if (!testOnly) {
item->setGeometry(QRect(QPoint(x, y), itemSize));
}
x += itemSize.width() + spaceX;
lineHeight = qMax(lineHeight, itemSize.height());
}
return y + lineHeight - rect.y();
}
//------------------------------------------------------------------------------------------------------------------------------------------
int FlowLayout::heightForWidth(int width) const
{
return doLayout(QRect(0, 0, width, 0), true);
}
//------------------------------------------------------------------------------------------------------------------------------------------
void FlowLayout::setGeometry(const QRect &rect)
{
QLayout::setGeometry(rect);
int x = rect.x();
int y = rect.y();
int lineHeight = 0;
QList<QLayoutItem*> lineItems;
for (int i = 0; i < itemList.size(); ++i) {
QLayoutItem *item = itemList.at(i);
QWidget *wid = item->widget();
QSize itemSize = item->sizeHint();
if (x + itemSize.width() > rect.right() && !lineItems.isEmpty()) {
// переносим на новую строку
stretchLastInRow(lineItems, rect.right() - x + rect.x());
lineItems.clear();
x = rect.x();
y += lineHeight + spacing();
lineHeight = 0;
}
item->setGeometry(QRect(QPoint(x, y), itemSize));
x += itemSize.width() + spacing();
lineHeight = qMax(lineHeight, itemSize.height());
lineItems << item;
}
// последняя строка
if (!lineItems.isEmpty()) {
stretchLastInRow(lineItems, rect.right() - x + rect.x());
}
}
//------------------------------------------------------------------------------------------------------------------------------------------
void FlowLayout::stretchLastInRow(const QList<QLayoutItem*> &lineItems, int extraWidth)
{
if (lineItems.isEmpty())
return;
QLayoutItem *last = lineItems.last();
QRect geom = last->geometry();
geom.setWidth(geom.width() + extraWidth); // растягиваем на остаток
last->setGeometry(geom);
}
//------------------------------------------------------------------------------------------------------------------------------------------
QSize FlowLayout::sizeHint() const
{
return minimumSize();
}
//------------------------------------------------------------------------------------------------------------------------------------------
QSize FlowLayout::minimumSize() const
{
QSize size;
for (QLayoutItem *item : itemList)
size = size.expandedTo(item->minimumSize());
int left, top, right, bottom;
getContentsMargins(&left, &top, &right, &bottom);
size += QSize(left + right, top + bottom);
return size;
}
//------------------------------------------------------------------------------------------------------------------------------------------
int FlowLayout::horizontalSpacing() const
{
if (m_hSpace >= 0) {
return m_hSpace;
} else {
return smartSpacing(QStyle::PM_LayoutHorizontalSpacing);
}
}
//------------------------------------------------------------------------------------------------------------------------------------------
int FlowLayout::verticalSpacing() const
{
if (m_vSpace >= 0) {
return m_vSpace;
} else {
return smartSpacing(QStyle::PM_LayoutVerticalSpacing);
}
}
//------------------------------------------------------------------------------------------------------------------------------------------