|
| 1 | +/****************************************************************************** |
| 2 | + Example provided by Qt |
| 3 | + <https://doc.qt.io/qt-6/qtwidgets-layouts-flowlayout-example.html> |
| 4 | +
|
| 5 | + Copyright (C) 2016 The Qt Company Ltd. |
| 6 | + SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause |
| 7 | +******************************************************************************/ |
| 8 | + |
| 9 | +#include "FlowLayout.hpp" |
| 10 | + |
| 11 | +#include <QWidget> |
| 12 | + |
| 13 | +FlowLayout::FlowLayout(QWidget *parent, int margin, int hSpacing, int vSpacing) |
| 14 | + : QLayout(parent), |
| 15 | + m_hSpace(hSpacing), |
| 16 | + m_vSpace(vSpacing) |
| 17 | +{ |
| 18 | + setContentsMargins(margin, margin, margin, margin); |
| 19 | +} |
| 20 | + |
| 21 | +FlowLayout::FlowLayout(int margin, int hSpacing, int vSpacing) : m_hSpace(hSpacing), m_vSpace(vSpacing) |
| 22 | +{ |
| 23 | + setContentsMargins(margin, margin, margin, margin); |
| 24 | +} |
| 25 | + |
| 26 | +FlowLayout::~FlowLayout() |
| 27 | +{ |
| 28 | + QLayoutItem *item; |
| 29 | + while ((item = takeAt(0))) { |
| 30 | + delete item; |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +void FlowLayout::addItem(QLayoutItem *item) |
| 35 | +{ |
| 36 | + itemList.append(item); |
| 37 | +} |
| 38 | + |
| 39 | +int FlowLayout::horizontalSpacing() const |
| 40 | +{ |
| 41 | + if (m_hSpace >= 0) { |
| 42 | + return m_hSpace; |
| 43 | + } else { |
| 44 | + return smartSpacing(QStyle::PM_LayoutHorizontalSpacing); |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +int FlowLayout::verticalSpacing() const |
| 49 | +{ |
| 50 | + if (m_vSpace >= 0) { |
| 51 | + return m_vSpace; |
| 52 | + } else { |
| 53 | + return smartSpacing(QStyle::PM_LayoutVerticalSpacing); |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +int FlowLayout::count() const |
| 58 | +{ |
| 59 | + return itemList.size(); |
| 60 | +} |
| 61 | + |
| 62 | +QLayoutItem *FlowLayout::itemAt(int index) const |
| 63 | +{ |
| 64 | + return itemList.value(index); |
| 65 | +} |
| 66 | + |
| 67 | +QLayoutItem *FlowLayout::takeAt(int index) |
| 68 | +{ |
| 69 | + if (index >= 0 && index < itemList.size()) { |
| 70 | + return itemList.takeAt(index); |
| 71 | + } |
| 72 | + return nullptr; |
| 73 | +} |
| 74 | + |
| 75 | +Qt::Orientations FlowLayout::expandingDirections() const |
| 76 | +{ |
| 77 | + return {}; |
| 78 | +} |
| 79 | + |
| 80 | +bool FlowLayout::hasHeightForWidth() const |
| 81 | +{ |
| 82 | + return true; |
| 83 | +} |
| 84 | + |
| 85 | +int FlowLayout::heightForWidth(int width) const |
| 86 | +{ |
| 87 | + int height = doLayout(QRect(0, 0, width, 0), true); |
| 88 | + return height; |
| 89 | +} |
| 90 | + |
| 91 | +void FlowLayout::setGeometry(const QRect &rect) |
| 92 | +{ |
| 93 | + QLayout::setGeometry(rect); |
| 94 | + doLayout(rect, false); |
| 95 | +} |
| 96 | + |
| 97 | +QSize FlowLayout::sizeHint() const |
| 98 | +{ |
| 99 | + return minimumSize(); |
| 100 | +} |
| 101 | + |
| 102 | +QSize FlowLayout::minimumSize() const |
| 103 | +{ |
| 104 | + QSize size; |
| 105 | + for (const QLayoutItem *item : std::as_const(itemList)) { |
| 106 | + size = size.expandedTo(item->minimumSize()); |
| 107 | + } |
| 108 | + |
| 109 | + const QMargins margins = contentsMargins(); |
| 110 | + size += QSize(margins.left() + margins.right(), margins.top() + margins.bottom()); |
| 111 | + return size; |
| 112 | +} |
| 113 | + |
| 114 | +int FlowLayout::doLayout(const QRect &rect, bool testOnly) const |
| 115 | +{ |
| 116 | + int left, top, right, bottom; |
| 117 | + getContentsMargins(&left, &top, &right, &bottom); |
| 118 | + QRect effectiveRect = rect.adjusted(+left, +top, -right, -bottom); |
| 119 | + int x = effectiveRect.x(); |
| 120 | + int y = effectiveRect.y(); |
| 121 | + int lineHeight = 0; |
| 122 | + |
| 123 | + for (QLayoutItem *item : std::as_const(itemList)) { |
| 124 | + const QWidget *wid = item->widget(); |
| 125 | + int spaceX = horizontalSpacing(); |
| 126 | + if (spaceX == -1) { |
| 127 | + spaceX = wid->style()->layoutSpacing(QSizePolicy::PushButton, QSizePolicy::PushButton, |
| 128 | + Qt::Horizontal); |
| 129 | + } |
| 130 | + |
| 131 | + int spaceY = verticalSpacing(); |
| 132 | + if (spaceY == -1) { |
| 133 | + spaceY = wid->style()->layoutSpacing(QSizePolicy::PushButton, QSizePolicy::PushButton, |
| 134 | + Qt::Vertical); |
| 135 | + } |
| 136 | + |
| 137 | + int nextX = x + item->sizeHint().width() + spaceX; |
| 138 | + if (nextX - spaceX > effectiveRect.right() && lineHeight > 0) { |
| 139 | + x = effectiveRect.x(); |
| 140 | + y = y + lineHeight + spaceY; |
| 141 | + nextX = x + item->sizeHint().width() + spaceX; |
| 142 | + lineHeight = 0; |
| 143 | + } |
| 144 | + |
| 145 | + if (!testOnly) { |
| 146 | + item->setGeometry(QRect(QPoint(x, y), item->sizeHint())); |
| 147 | + } |
| 148 | + |
| 149 | + x = nextX; |
| 150 | + lineHeight = qMax(lineHeight, item->sizeHint().height()); |
| 151 | + } |
| 152 | + return y + lineHeight - rect.y() + bottom; |
| 153 | +} |
| 154 | + |
| 155 | +int FlowLayout::smartSpacing(QStyle::PixelMetric pm) const |
| 156 | +{ |
| 157 | + QObject *parent = this->parent(); |
| 158 | + if (!parent) { |
| 159 | + return -1; |
| 160 | + } else if (parent->isWidgetType()) { |
| 161 | + QWidget *pw = static_cast<QWidget *>(parent); |
| 162 | + return pw->style()->pixelMetric(pm, nullptr, pw); |
| 163 | + } else { |
| 164 | + return static_cast<QLayout *>(parent)->spacing(); |
| 165 | + } |
| 166 | +} |
0 commit comments