Mantid
Loading...
Searching...
No Matches
FlowLayout.cpp
Go to the documentation of this file.
1/****************************************************************************
2**
3** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation (qt-info@nokia.com)
6**
7** This file is part of the examples of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:BSD$
10** You may use this file under the terms of the BSD license as follows:
11**
12** "Redistribution and use in source and binary forms, with or without
13** modification, are permitted provided that the following conditions are
14** met:
15** * Redistributions of source code must retain the above copyright
16** notice, this list of conditions and the following disclaimer.
17** * Redistributions in binary form must reproduce the above copyright
18** notice, this list of conditions and the following disclaimer in
19** the documentation and/or other materials provided with the
20** distribution.
21** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
22** the names of its contributors may be used to endorse or promote
23** products derived from this software without specific prior written
24** permission.
25**
26** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
37** $QT_END_LICENSE$
38**
39****************************************************************************/
40
42#include <QWidget>
43
44namespace MantidQt::API {
45FlowLayout::FlowLayout(QWidget *parent, int margin, int hSpacing, int vSpacing)
46 : QLayout(parent), m_hSpace(hSpacing), m_vSpace(vSpacing) {
47 setContentsMargins(margin, margin, margin, margin);
48}
49
50FlowLayout::FlowLayout(int margin, int hSpacing, int vSpacing) : m_hSpace(hSpacing), m_vSpace(vSpacing) {
51 setContentsMargins(margin, margin, margin, margin);
52}
53
55 QLayoutItem *item;
56 while ((item = takeAt(0)))
57 delete item;
58}
59
60void FlowLayout::addItem(QLayoutItem *item) { itemList.append(item); }
61
63 if (m_hSpace >= 0) {
64 return m_hSpace;
65 } else {
66 return smartSpacing(QStyle::PM_LayoutHorizontalSpacing);
67 }
68}
69
71 if (m_vSpace >= 0) {
72 return m_vSpace;
73 } else {
74 return smartSpacing(QStyle::PM_LayoutVerticalSpacing);
75 }
76}
77
78int FlowLayout::count() const { return itemList.size(); }
79
80QLayoutItem *FlowLayout::itemAt(int index) const { return itemList.value(index); }
81
82QLayoutItem *FlowLayout::takeAt(int index) {
83 if (index >= 0 && index < itemList.size())
84 return itemList.takeAt(index);
85 else
86 return nullptr;
87}
88
89Qt::Orientations FlowLayout::expandingDirections() const { return Qt::Orientations(); }
90
91bool FlowLayout::hasHeightForWidth() const { return true; }
92
93int FlowLayout::heightForWidth(int width) const {
94 int height = doLayout(QRect(0, 0, width, 0), true);
95 return height;
96}
97
98void FlowLayout::setGeometry(const QRect &rect) {
99 QLayout::setGeometry(rect);
100 doLayout(rect, false);
101}
102
103QSize FlowLayout::sizeHint() const { return minimumSize(); }
104
106 QSize size;
107 foreach (QLayoutItem *item, itemList)
108 size = size.expandedTo(item->minimumSize());
109
110 size += QSize(2 * margin(), 2 * margin());
111 return size;
112}
113
114int FlowLayout::doLayout(const QRect &rect, bool testOnly) const {
115 int left, top, right, bottom;
116 getContentsMargins(&left, &top, &right, &bottom);
117 QRect effectiveRect = rect.adjusted(+left, +top, -right, -bottom);
118 int x = effectiveRect.x();
119 int y = effectiveRect.y();
120 int lineHeight = 0;
121
122 for (const auto &item : itemList) {
123 QWidget *wid = item->widget();
124 int spaceX = horizontalSpacing();
125 if (spaceX == -1)
126 spaceX = wid->style()->layoutSpacing(QSizePolicy::PushButton, QSizePolicy::PushButton, Qt::Horizontal);
127 int spaceY = verticalSpacing();
128 if (spaceY == -1)
129 spaceY = wid->style()->layoutSpacing(QSizePolicy::PushButton, QSizePolicy::PushButton, Qt::Vertical);
130 int nextX = x + item->sizeHint().width() + spaceX;
131 if (nextX - spaceX > effectiveRect.right() && lineHeight > 0) {
132 x = effectiveRect.x();
133 y = y + lineHeight + spaceY;
134 nextX = x + item->sizeHint().width() + spaceX;
135 lineHeight = 0;
136 }
137
138 if (!testOnly)
139 item->setGeometry(QRect(QPoint(x, y), item->sizeHint()));
140
141 x = nextX;
142 lineHeight = qMax(lineHeight, item->sizeHint().height());
143 }
144 return y + lineHeight - rect.y() + bottom;
145}
146int FlowLayout::smartSpacing(QStyle::PixelMetric pm) const {
147 QObject *parent = this->parent();
148 if (!parent) {
149 return -1;
150 } else if (parent->isWidgetType()) {
151 auto *pw = static_cast<QWidget *>(parent);
152 return pw->style()->pixelMetric(pm, nullptr, pw);
153 } else {
154 return static_cast<QLayout *>(parent)->spacing();
155 }
156}
157} // namespace MantidQt::API
double height
Definition: GetAllEi.cpp:155
std::map< DeltaEMode::Type, std::string > index
Definition: DeltaEMode.cpp:19
double bottom
Definition: LineProfile.cpp:79
double top
Definition: LineProfile.cpp:78
double left
Definition: LineProfile.cpp:80
double right
Definition: LineProfile.cpp:81
QSize minimumSize() const override
Definition: FlowLayout.cpp:105
QLayoutItem * takeAt(int index) override
Definition: FlowLayout.cpp:82
int heightForWidth(int) const override
Definition: FlowLayout.cpp:93
int smartSpacing(QStyle::PixelMetric pm) const
Definition: FlowLayout.cpp:146
bool hasHeightForWidth() const override
Definition: FlowLayout.cpp:91
void addItem(QLayoutItem *item) override
Definition: FlowLayout.cpp:60
QLayoutItem * itemAt(int index) const override
Definition: FlowLayout.cpp:80
int count() const override
Definition: FlowLayout.cpp:78
int doLayout(const QRect &rect, bool testOnly) const
Definition: FlowLayout.cpp:114
QSize sizeHint() const override
Definition: FlowLayout.cpp:103
QList< QLayoutItem * > itemList
Definition: FlowLayout.h:74
FlowLayout(QWidget *parent, int margin=-1, int hSpacing=-1, int vSpacing=-1)
Definition: FlowLayout.cpp:45
void setGeometry(const QRect &rect) override
Definition: FlowLayout.cpp:98
Qt::Orientations expandingDirections() const override
Definition: FlowLayout.cpp:89
int horizontalSpacing() const
Definition: FlowLayout.cpp:62