Repository navigation
Expand file tree
/
Copy pathQQuickGridContainer.cpp
More file actions
209 lines (186 loc) · 7.63 KB
/
Copy pathQQuickGridContainer.cpp
File metadata and controls
209 lines (186 loc) · 7.63 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#include "QQuickGridContainer.h"
#include <QtMath>
#include <QDebug>
#include <QElapsedTimer>
QQuickGridContainer::QQuickGridContainer (QQuickItem * parent) : QQuickItem (parent)
, m_cols (1)
, m_rows (1)
, m_colSpacing (0)
, m_rowSpacing (0)
, m_capacity (-1)
, m_fillEmpty (true)
, m_verticalFlow (false)
, m_invertDirection (false)
{ }
int QQuickGridContainer::getCols (void) const {
return m_cols;
}
int QQuickGridContainer::getRows (void) const {
return m_rows;
}
int QQuickGridContainer::getColSpacing (void) const {
return m_colSpacing;
}
int QQuickGridContainer::getRowSpacing (void) const {
return m_rowSpacing;
}
int QQuickGridContainer::getCapacity (void) const {
return m_capacity;
}
bool QQuickGridContainer::getFillEmpty (void) const {
return m_fillEmpty;
}
bool QQuickGridContainer::getVerticalFlow (void) const {
return m_verticalFlow;
}
bool QQuickGridContainer::getInvertDirection (void) const {
return m_invertDirection;
}
void QQuickGridContainer::setCols (int cols) {
if (m_cols != cols) {
m_cols = cols;
emit colsChanged (cols);
}
}
void QQuickGridContainer::setRows (int rows) {
if (m_rows != rows) {
m_rows = rows;
emit rowsChanged (rows);
}
}
void QQuickGridContainer::setColSpacing (int colSpacing) {
if (m_colSpacing != colSpacing) {
m_colSpacing = colSpacing;
emit colSpacingChanged (colSpacing);
}
}
void QQuickGridContainer::setRowSpacing (int rowSpacing) {
if (m_rowSpacing != rowSpacing) {
m_rowSpacing = rowSpacing;
emit rowSpacingChanged (rowSpacing);
}
}
void QQuickGridContainer::setCapacity (int capacity) {
if (m_capacity != capacity) {
m_capacity = capacity;
emit capacityChanged (capacity);
}
}
void QQuickGridContainer::setFillEmpty (bool fillEmpty) {
if (m_fillEmpty != fillEmpty) {
m_fillEmpty = fillEmpty;
emit fillEmptyChanged (fillEmpty);
}
}
void QQuickGridContainer::setVerticalFlow (bool verticalFlow) {
if (m_verticalFlow != verticalFlow) {
m_verticalFlow = verticalFlow;
emit verticalFlowChanged (verticalFlow);
}
}
void QQuickGridContainer::setInvertDirection (bool invertDirection) {
if (m_invertDirection != invertDirection) {
m_invertDirection = invertDirection;
emit invertDirectionChanged (invertDirection);
}
}
void QQuickGridContainer::classBegin (void) {
connect (this, &QQuickGridContainer::childrenChanged, this, &QQuickGridContainer::polish);
connect (this, &QQuickGridContainer::visibleChanged, this, &QQuickGridContainer::polish);
connect (this, &QQuickGridContainer::widthChanged, this, &QQuickGridContainer::polish);
connect (this, &QQuickGridContainer::heightChanged, this, &QQuickGridContainer::polish);
connect (this, &QQuickGridContainer::colsChanged, this, &QQuickGridContainer::polish);
connect (this, &QQuickGridContainer::rowsChanged, this, &QQuickGridContainer::polish);
connect (this, &QQuickGridContainer::rowSpacingChanged, this, &QQuickGridContainer::polish);
connect (this, &QQuickGridContainer::colSpacingChanged, this, &QQuickGridContainer::polish);
connect (this, &QQuickGridContainer::capacityChanged, this, &QQuickGridContainer::polish);
connect (this, &QQuickGridContainer::fillEmptyChanged, this, &QQuickGridContainer::polish);
connect (this, &QQuickGridContainer::verticalFlowChanged, this, &QQuickGridContainer::polish);
connect (this, &QQuickGridContainer::invertDirectionChanged, this, &QQuickGridContainer::polish);
}
void QQuickGridContainer::componentComplete (void) {
polish ();
}
void QQuickGridContainer::updatePolish (void) {
const QList<QQuickItem *> childrenList = childItems ();
/// find max children size and count
int count = 0;
qreal maxChildWidth = 0;
qreal maxChildHeight = 0;
for (QList<QQuickItem *>::const_iterator it = childrenList.constBegin (); it != childrenList.constEnd (); ++it) {
QQuickItem * child = (* it);
if (child != Q_NULLPTR && !child->inherits ("QQuickRepeater") && (child->isVisible () || !m_fillEmpty)) {
if (child->implicitWidth () > maxChildWidth) {
maxChildWidth = child->implicitWidth ();
}
if (child->implicitHeight () > maxChildHeight) {
maxChildHeight = child->implicitHeight ();
}
count++;
}
}
/// recompute rows count
int tmpRows = 1;
if (m_cols > 0) {
if (m_capacity > 0) {
tmpRows = qCeil (qreal (m_capacity) / qreal (m_cols));
}
else if (count > 0) {
tmpRows = qCeil (qreal (count) / qreal (m_cols));
}
}
setRows (tmpRows);
/// recompute implicit size
setImplicitWidth ((m_cols * maxChildWidth) + ((m_cols -1) * m_colSpacing));
setImplicitHeight ((m_rows * maxChildHeight) + ((m_rows -1) * m_rowSpacing));
/// relayout children
const qreal layoutWidth = width ();
const qreal layoutHeight = height ();
const qreal itemWidth = ((qreal (layoutWidth + m_colSpacing) / qreal (m_cols)) - qreal (m_colSpacing));
const qreal itemHeight = ((qreal (layoutHeight + m_rowSpacing) / qreal (m_rows)) - qreal (m_rowSpacing));
const qreal stepX = (itemWidth + qreal (m_colSpacing));
const qreal stepY = (itemHeight + qreal (m_rowSpacing));
qreal curX = (m_invertDirection ? layoutWidth - itemWidth : 0);
qreal curY = (m_invertDirection ? layoutHeight - itemHeight : 0);
if (m_rows > 0 && m_cols > 0) {
int nb = 0;
for (QList<QQuickItem *>::const_iterator it = childrenList.constBegin (); it != childrenList.constEnd (); ++it) {
QQuickItem * child = (* it);
if (child != Q_NULLPTR && !child->inherits ("QQuickRepeater") && (child->isVisible () || !m_fillEmpty)) {
child->setX (curX);
child->setY (curY);
child->setWidth (itemWidth);
child->setHeight (itemHeight);
if (!m_verticalFlow) { // horizontal
if ((nb +1) % m_cols > 0) { // next column
curX = (m_invertDirection ? curX - stepX : curX + stepX);
}
else { // next row
curX = (m_invertDirection ? layoutWidth - itemWidth : 0);
curY = (m_invertDirection ? curY - stepY : curY + stepY);
}
}
else { // vertical
if ((nb +1) % m_rows > 0) { // next row
curY = (m_invertDirection ? curY - stepY : curY + stepY);
}
else { // next column
curY = (m_invertDirection ? layoutHeight - itemHeight : 0);
curX = (m_invertDirection ? curX - stepX : curX + stepX);
}
}
nb++;
}
}
}
}
void QQuickGridContainer::itemChange (QQuickItem::ItemChange change, const QQuickItem::ItemChangeData & value) {
if (change == QQuickItem::ItemChildAddedChange) {
QQuickItem * child = value.item;
if (child != Q_NULLPTR) {
connect (child, &QQuickItem::visibleChanged, this, &QQuickGridContainer::polish, Qt::UniqueConnection);
connect (child, &QQuickItem::implicitWidthChanged, this, &QQuickGridContainer::polish, Qt::UniqueConnection);
connect (child, &QQuickItem::implicitHeightChanged, this, &QQuickGridContainer::polish, Qt::UniqueConnection);
}
}
}