-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathQQuickStretchRowContainer.cpp
92 lines (83 loc) · 3.44 KB
/
QQuickStretchRowContainer.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
84
85
86
87
88
89
90
91
92
#include "QQuickStretchRowContainer.h"
#include <QElapsedTimer>
#include <QDebug>
#include <qmath.h>
QQuickStretchRowContainer::QQuickStretchRowContainer (QQuickItem * parent) : QQuickItem (parent)
, m_spacing (0)
{ }
int QQuickStretchRowContainer::getSpacing (void) const {
return m_spacing;
}
void QQuickStretchRowContainer::setSpacing (int spacing) {
if (m_spacing != spacing) {
m_spacing = spacing;
emit spacingChanged (spacing);
}
}
void QQuickStretchRowContainer::classBegin (void) {
connect (this, &QQuickStretchRowContainer::childrenChanged, this, &QQuickStretchRowContainer::polish);
connect (this, &QQuickStretchRowContainer::visibleChanged, this, &QQuickStretchRowContainer::polish);
connect (this, &QQuickStretchRowContainer::widthChanged, this, &QQuickStretchRowContainer::polish);
connect (this, &QQuickStretchRowContainer::heightChanged, this, &QQuickStretchRowContainer::polish);
connect (this, &QQuickStretchRowContainer::spacingChanged, this, &QQuickStretchRowContainer::polish);
}
void QQuickStretchRowContainer::componentComplete (void) {
polish ();
}
void QQuickStretchRowContainer::updatePolish (void) {
const QList<QQuickItem *> childrenList = childItems ();
/// find items and stretchers
qreal tmpW = 0;
qreal tmpH = 0;
int nbItems = 0;
int nbStretch = 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 ()) {
if (child->implicitHeight () > tmpH) {
tmpH = child->implicitHeight ();
}
if (child->implicitWidth () >= 0) {
tmpW += child->implicitWidth ();
}
else {
nbStretch++;
}
nbItems++;
}
}
/// resize layout
if (nbItems > 1) {
tmpW += (m_spacing * (nbItems -1));
}
setImplicitWidth (tmpW);
setImplicitHeight (tmpH);
const qreal layoutWidth = width ();
const qreal autoSize = (nbStretch > 0 ? (layoutWidth - implicitWidth ()) / qreal (nbStretch) : 0);
/// position children
int currX = 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 ()) {
if (currX) {
currX += m_spacing;
}
child->setX (currX);
child->setWidth (child->implicitWidth () >= 0 ? child->implicitWidth () : autoSize);
currX += child->width ();
}
}
}
void QQuickStretchRowContainer::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, &QQuickStretchRowContainer::polish, Qt::UniqueConnection);
connect (child, &QQuickItem::implicitWidthChanged,
this, &QQuickStretchRowContainer::polish, Qt::UniqueConnection);
connect (child, &QQuickItem::implicitHeightChanged,
this, &QQuickStretchRowContainer::polish, Qt::UniqueConnection);
}
}
}