Debian Codegraph @

tests/manual/examples/widgets/graphicsview/flowlayout/flowlayout.cpp

raw
1 // Copyright (C) 2016 The Qt Company Ltd.
2 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
3
4 #include "flowlayout.h"
5
6 #include <QtMath>
7
8 FlowLayout::FlowLayout(QGraphicsLayoutItem *parent) : QGraphicsLayout(parent)
9 {
10 QSizePolicy sp = sizePolicy();
11 sp.setHeightForWidth(true);
12 setSizePolicy(sp);
13 }
14
15 void FlowLayout::insertItem(int index, QGraphicsLayoutItem *item)
16 {
17 item->setParentLayoutItem(this);
18 if (index > m_items.count() || index < 0)
19 index = m_items.count();
20 m_items.insert(index, item);
21 invalidate();
22 }
23
24 int FlowLayout::count() const
25 {
26 return m_items.count();
27 }
28
29 QGraphicsLayoutItem *FlowLayout::itemAt(int index) const
30 {
31 return m_items.value(index);
32 }
33
34 void FlowLayout::removeAt(int index)
35 {
36 m_items.removeAt(index);
37 invalidate();
38 }
39
40 qreal FlowLayout::spacing(Qt::Orientation o) const
41 {
42 return m_spacing[int(o) - 1];
43 }
44
45 void FlowLayout::setSpacing(Qt::Orientations o, qreal spacing)
46 {
47 if (o & Qt::Horizontal)
48 m_spacing[0] = spacing;
49 if (o & Qt::Vertical)
50 m_spacing[1] = spacing;
51 }
52
53 void FlowLayout::setGeometry(const QRectF &geom)
54 {
55 QGraphicsLayout::setGeometry(geom);
56 doLayout(geom, true);
57 }
58
59 qreal FlowLayout::doLayout(const QRectF &geom, bool applyNewGeometry) const
60 {
61 qreal left, top, right, bottom;
62 getContentsMargins(&left, &top, &right, &bottom);
63 const qreal maxw = geom.width() - left - right;
64
65 qreal x = 0;
66 qreal y = 0;
67 qreal maxRowHeight = 0;
68 QSizeF pref;
69 for (QGraphicsLayoutItem *item : m_items) {
70 pref = item->effectiveSizeHint(Qt::PreferredSize);
71 maxRowHeight = qMax(maxRowHeight, pref.height());
72
73 qreal next_x;
74 next_x = x + pref.width();
75 if (next_x > maxw) {
76 if (qFuzzyIsNull(x)) {
77 pref.setWidth(maxw);
78 } else {
79 x = 0;
80 next_x = pref.width();
81 }
82 y += maxRowHeight + spacing(Qt::Vertical);
83 maxRowHeight = 0;
84 }
85
86 if (applyNewGeometry)
87 item->setGeometry(QRectF(QPointF(left + x, top + y), pref));
88 x = next_x + spacing(Qt::Horizontal);
89 }
90 maxRowHeight = qMax(maxRowHeight, pref.height());
91 return top + y + maxRowHeight + bottom;
92 }
93
94 QSizeF FlowLayout::minSize(const QSizeF &constraint) const
95 {
96 QSizeF size(0, 0);
97 qreal left, top, right, bottom;
98 getContentsMargins(&left, &top, &right, &bottom);
99 if (constraint.width() >= 0) { // height for width
100 const qreal height = doLayout(QRectF(QPointF(0,0), constraint), false);
101 size = QSizeF(constraint.width(), height);
102 } else if (constraint.height() >= 0) { // width for height?
103 // not supported
104 } else {
105 for (const QGraphicsLayoutItem *item : std::as_const(m_items))
106 size = size.expandedTo(item->effectiveSizeHint(Qt::MinimumSize));
107 size += QSizeF(left + right, top + bottom);
108 }
109 return size;
110 }
111
112 QSizeF FlowLayout::prefSize() const
113 {
114 qreal left, right;
115 getContentsMargins(&left, nullptr, &right, nullptr);
116
117 qreal maxh = 0;
118 qreal totalWidth = 0;
119 for (const QGraphicsLayoutItem *item : std::as_const(m_items)) {
120 if (totalWidth > 0)
121 totalWidth += spacing(Qt::Horizontal);
122 QSizeF pref = item->effectiveSizeHint(Qt::PreferredSize);
123 totalWidth += pref.width();
124 maxh = qMax(maxh, pref.height());
125 }
126 maxh += spacing(Qt::Vertical);
127
128 const qreal goldenAspectRatio = 1.61803399;
129 qreal w = qSqrt(totalWidth * maxh * goldenAspectRatio) + left + right;
130
131 return minSize(QSizeF(w, -1));
132 }
133
134 QSizeF FlowLayout::maxSize() const
135 {
136 qreal totalWidth = 0;
137 qreal totalHeight = 0;
138 for (const QGraphicsLayoutItem *item : std::as_const(m_items)) {
139 if (totalWidth > 0)
140 totalWidth += spacing(Qt::Horizontal);
141 if (totalHeight > 0)
142 totalHeight += spacing(Qt::Vertical);
143 QSizeF pref = item->effectiveSizeHint(Qt::PreferredSize);
144 totalWidth += pref.width();
145 totalHeight += pref.height();
146 }
147
148 qreal left, top, right, bottom;
149 getContentsMargins(&left, &top, &right, &bottom);
150 return QSizeF(left + totalWidth + right, top + totalHeight + bottom);
151 }
152
153 QSizeF FlowLayout::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
154 {
155 QSizeF sh = constraint;
156 switch (which) {
157 case Qt::PreferredSize:
158 sh = prefSize();
159 break;
160 case Qt::MinimumSize:
161 sh = minSize(constraint);
162 break;
163 case Qt::MaximumSize:
164 sh = maxSize();
165 break;
166 default:
167 break;
168 }
169 return sh;
170 }