|
| 1 | +/****************************************************************************** |
| 2 | + Example provided by Qt |
| 3 | + <https://doc.qt.io/qt-6/qtwidgets-layouts-flowlayout-example.html> |
| 4 | +
|
| 5 | + This program is free software: you can redistribute it and/or modify |
| 6 | + it under the terms of the GNU General Public License as published by |
| 7 | + the Free Software Foundation, either version 2 of the License, or |
| 8 | + (at your option) any later version. |
| 9 | +
|
| 10 | + This program is distributed in the hope that it will be useful, |
| 11 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + GNU General Public License for more details. |
| 14 | +
|
| 15 | + You should have received a copy of the GNU General Public License |
| 16 | + along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | +******************************************************************************/ |
| 18 | + |
| 19 | +#include "FlowLayout.hpp" |
| 20 | + |
| 21 | +#include <QWidget> |
| 22 | + |
| 23 | +FlowLayout::FlowLayout(QWidget *parent, int margin, int hSpacing, int vSpacing) |
| 24 | + : QLayout(parent), |
| 25 | + m_hSpace(hSpacing), |
| 26 | + m_vSpace(vSpacing) |
| 27 | +{ |
| 28 | + setContentsMargins(margin, margin, margin, margin); |
| 29 | +} |
| 30 | + |
| 31 | +FlowLayout::FlowLayout(int margin, int hSpacing, int vSpacing) : m_hSpace(hSpacing), m_vSpace(vSpacing) |
| 32 | +{ |
| 33 | + setContentsMargins(margin, margin, margin, margin); |
| 34 | +} |
| 35 | + |
| 36 | +FlowLayout::~FlowLayout() |
| 37 | +{ |
| 38 | + QLayoutItem *item; |
| 39 | + while ((item = takeAt(0))) |
| 40 | + delete item; |
| 41 | +} |
| 42 | + |
| 43 | +void FlowLayout::addItem(QLayoutItem *item) |
| 44 | +{ |
| 45 | + itemList.append(item); |
| 46 | +} |
| 47 | + |
| 48 | +int FlowLayout::horizontalSpacing() const |
| 49 | +{ |
| 50 | + if (m_hSpace >= 0) { |
| 51 | + return m_hSpace; |
| 52 | + } else { |
| 53 | + return smartSpacing(QStyle::PM_LayoutHorizontalSpacing); |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +int FlowLayout::verticalSpacing() const |
| 58 | +{ |
| 59 | + if (m_vSpace >= 0) { |
| 60 | + return m_vSpace; |
| 61 | + } else { |
| 62 | + return smartSpacing(QStyle::PM_LayoutVerticalSpacing); |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +int FlowLayout::count() const |
| 67 | +{ |
| 68 | + return itemList.size(); |
| 69 | +} |
| 70 | + |
| 71 | +QLayoutItem *FlowLayout::itemAt(int index) const |
| 72 | +{ |
| 73 | + return itemList.value(index); |
| 74 | +} |
| 75 | + |
| 76 | +QLayoutItem *FlowLayout::takeAt(int index) |
| 77 | +{ |
| 78 | + if (index >= 0 && index < itemList.size()) |
| 79 | + return itemList.takeAt(index); |
| 80 | + return nullptr; |
| 81 | +} |
| 82 | + |
| 83 | +Qt::Orientations FlowLayout::expandingDirections() const |
| 84 | +{ |
| 85 | + return {}; |
| 86 | +} |
| 87 | + |
| 88 | +bool FlowLayout::hasHeightForWidth() const |
| 89 | +{ |
| 90 | + return true; |
| 91 | +} |
| 92 | + |
| 93 | +int FlowLayout::heightForWidth(int width) const |
| 94 | +{ |
| 95 | + int height = doLayout(QRect(0, 0, width, 0), true); |
| 96 | + return height; |
| 97 | +} |
| 98 | + |
| 99 | +void FlowLayout::setGeometry(const QRect &rect) |
| 100 | +{ |
| 101 | + QLayout::setGeometry(rect); |
| 102 | + doLayout(rect, false); |
| 103 | +} |
| 104 | + |
| 105 | +QSize FlowLayout::sizeHint() const |
| 106 | +{ |
| 107 | + return minimumSize(); |
| 108 | +} |
| 109 | + |
| 110 | +QSize FlowLayout::minimumSize() const |
| 111 | +{ |
| 112 | + QSize size; |
| 113 | + for (const QLayoutItem *item : std::as_const(itemList)) |
| 114 | + size = size.expandedTo(item->minimumSize()); |
| 115 | + |
| 116 | + const QMargins margins = contentsMargins(); |
| 117 | + size += QSize(margins.left() + margins.right(), margins.top() + margins.bottom()); |
| 118 | + return size; |
| 119 | +} |
| 120 | + |
| 121 | +int FlowLayout::doLayout(const QRect &rect, bool testOnly) const |
| 122 | +{ |
| 123 | + int left, top, right, bottom; |
| 124 | + getContentsMargins(&left, &top, &right, &bottom); |
| 125 | + QRect effectiveRect = rect.adjusted(+left, +top, -right, -bottom); |
| 126 | + int x = effectiveRect.x(); |
| 127 | + int y = effectiveRect.y(); |
| 128 | + int lineHeight = 0; |
| 129 | + |
| 130 | + for (QLayoutItem *item : std::as_const(itemList)) { |
| 131 | + const QWidget *wid = item->widget(); |
| 132 | + int spaceX = horizontalSpacing(); |
| 133 | + if (spaceX == -1) |
| 134 | + spaceX = wid->style()->layoutSpacing(QSizePolicy::PushButton, QSizePolicy::PushButton, |
| 135 | + Qt::Horizontal); |
| 136 | + int spaceY = verticalSpacing(); |
| 137 | + if (spaceY == -1) |
| 138 | + spaceY = wid->style()->layoutSpacing(QSizePolicy::PushButton, QSizePolicy::PushButton, |
| 139 | + Qt::Vertical); |
| 140 | + |
| 141 | + int nextX = x + item->sizeHint().width() + spaceX; |
| 142 | + if (nextX - spaceX > effectiveRect.right() && lineHeight > 0) { |
| 143 | + x = effectiveRect.x(); |
| 144 | + y = y + lineHeight + spaceY; |
| 145 | + nextX = x + item->sizeHint().width() + spaceX; |
| 146 | + lineHeight = 0; |
| 147 | + } |
| 148 | + |
| 149 | + if (!testOnly) |
| 150 | + item->setGeometry(QRect(QPoint(x, y), item->sizeHint())); |
| 151 | + |
| 152 | + x = nextX; |
| 153 | + lineHeight = qMax(lineHeight, item->sizeHint().height()); |
| 154 | + } |
| 155 | + return y + lineHeight - rect.y() + bottom; |
| 156 | +} |
| 157 | + |
| 158 | +int FlowLayout::smartSpacing(QStyle::PixelMetric pm) const |
| 159 | +{ |
| 160 | + QObject *parent = this->parent(); |
| 161 | + if (!parent) { |
| 162 | + return -1; |
| 163 | + } else if (parent->isWidgetType()) { |
| 164 | + QWidget *pw = static_cast<QWidget *>(parent); |
| 165 | + return pw->style()->pixelMetric(pm, nullptr, pw); |
| 166 | + } else { |
| 167 | + return static_cast<QLayout *>(parent)->spacing(); |
| 168 | + } |
| 169 | +} |
0 commit comments