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