]> sigrok.org Git - pulseview.git/blame - pv/widgets/flowlayout.cpp
Session: Fix issue #67 by improving error handling
[pulseview.git] / pv / widgets / flowlayout.cpp
CommitLineData
adf9e022
SA
1/****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the examples of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** You may use this file under the terms of the BSD license as follows:
10 **
11 ** "Redistribution and use in source and binary forms, with or without
12 ** modification, are permitted provided that the following conditions are
13 ** met:
14 ** * Redistributions of source code must retain the above copyright
15 ** notice, this list of conditions and the following disclaimer.
16 ** * Redistributions in binary form must reproduce the above copyright
17 ** notice, this list of conditions and the following disclaimer in
18 ** the documentation and/or other materials provided with the
19 ** distribution.
20 ** * Neither the name of The Qt Company Ltd nor the names of its
21 ** contributors may be used to endorse or promote products derived
22 ** from this software without specific prior written permission.
23 **
24 **
25 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
36 **
37 ** $QT_END_LICENSE$
38 **
39 ****************************************************************************/
40
41#include <QWidget>
42
43#include "flowlayout.hpp"
44
45FlowLayout::FlowLayout(QWidget *parent, int margin, int hSpacing, int vSpacing) :
46 QLayout(parent),
47 m_parent(parent),
48 m_hSpace(hSpacing),
49 m_vSpace(vSpacing)
50{
51 setContentsMargins(margin, margin, margin, margin);
52}
53
54FlowLayout::FlowLayout(int margin, int hSpacing, int vSpacing) :
55 m_parent(nullptr),
56 m_hSpace(hSpacing),
57 m_vSpace(vSpacing)
58{
59 setContentsMargins(margin, margin, margin, margin);
60}
61
62FlowLayout::~FlowLayout()
63{
64 QLayoutItem *item;
65 while ((item = takeAt(0)))
66 delete item;
67}
68
69void FlowLayout::addItem(QLayoutItem *item)
70{
71 itemList.append(item);
72}
73
74int FlowLayout::horizontalSpacing() const
75{
76 if (m_hSpace >= 0)
77 return m_hSpace;
78 else
79 return smartSpacing(QStyle::PM_LayoutHorizontalSpacing);
80}
81
82int FlowLayout::verticalSpacing() const
83{
84 if (m_vSpace >= 0)
85 return m_vSpace;
86 else
87 return smartSpacing(QStyle::PM_LayoutVerticalSpacing);
88}
89
90int FlowLayout::count() const
91{
92 return itemList.size();
93}
94
95QLayoutItem *FlowLayout::itemAt(int index) const
96{
97 return itemList.value(index);
98}
99
100QLayoutItem *FlowLayout::takeAt(int index)
101{
102 if ((index >= 0) && (index < itemList.size()))
103 return itemList.takeAt(index);
104 else
feda6c6b 105 return nullptr;
adf9e022
SA
106}
107
108Qt::Orientations FlowLayout::expandingDirections() const
109{
110 return Qt::Horizontal | Qt::Vertical;
111}
112
113bool FlowLayout::hasHeightForWidth() const
114{
115 return true;
116}
117
118int FlowLayout::heightForWidth(int width) const
119{
120 int height = doLayout(QRect(0, 0, width, 0), true);
121 return height;
122}
123
124void FlowLayout::setGeometry(const QRect &rect)
125{
126 QLayout::setGeometry(rect);
127 doLayout(rect, false);
128}
129
130QSize FlowLayout::sizeHint() const
131{
132 return minimumSize();
133}
134
135QSize FlowLayout::minimumSize() const
136{
137 QSize size(0, 0);
138
139 for (QLayoutItem* item : itemList) {
140 int w = item->geometry().x() + item->geometry().width();
141 if (w > size.width())
142 size.setWidth(w);
143
65bde9b3 144 int h = item->geometry().y() + item->geometry().height();
adf9e022
SA
145 if (h > size.height())
146 size.setHeight(h);
147 }
148
1ed73ebd
VPP
149#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
150 int left, top, right, bottom;
151 getContentsMargins(&left, &top, &right, &bottom);
152 size += QSize(left + right, top + bottom);
153#else
adf9e022 154 size += QSize(2 * margin(), 2 * margin());
1ed73ebd 155#endif
adf9e022
SA
156
157 return size;
158}
159
160int FlowLayout::doLayout(const QRect &rect, bool testOnly) const
161{
162 int left, top, right, bottom;
163 getContentsMargins(&left, &top, &right, &bottom);
164
165 QRect effectiveRect = rect.adjusted(left, top, -right, -bottom);
166 int x = effectiveRect.x();
167 int y = effectiveRect.y();
168
169 int lineHeight = 0;
170 for (QLayoutItem* item : itemList) {
171 QWidget* w = item->widget();
172
173 int spaceX = horizontalSpacing();
174 if (spaceX == -1)
175 spaceX = w->style()->layoutSpacing(
176 QSizePolicy::PushButton, QSizePolicy::PushButton, Qt::Horizontal);
177
178 int spaceY = verticalSpacing();
179 if (spaceY == -1)
180 spaceY = w->style()->layoutSpacing(
181 QSizePolicy::PushButton, QSizePolicy::PushButton, Qt::Vertical);
182
183 int nextX = x + item->sizeHint().width() + spaceX;
184 if (((nextX - spaceX) > effectiveRect.right()) && (lineHeight > 0)) {
185 x = effectiveRect.x();
186 y = y + lineHeight + spaceY;
187 nextX = x + item->sizeHint().width() + spaceX;
188 lineHeight = 0;
189 }
190
191 if (!testOnly)
192 item->setGeometry(QRect(QPoint(x, y), item->sizeHint()));
193
194 x = nextX;
195 lineHeight = qMax(lineHeight, item->sizeHint().height());
196 }
197
198 int height = y + lineHeight - rect.y() + bottom;
199
200 if (m_parent)
201 m_parent->setMinimumHeight(height);
202
203 return height;
204}
205
206int FlowLayout::smartSpacing(QStyle::PixelMetric pm) const
207{
208 QObject *parent = this->parent();
209
210 if (!parent)
211 return -1;
212
213 if (parent->isWidgetType()) {
214 QWidget *pw = qobject_cast<QWidget*>(parent);
feda6c6b 215 return pw->style()->pixelMetric(pm, nullptr, pw);
adf9e022
SA
216 } else
217 return static_cast<QLayout*>(parent)->spacing();
218}