| 1 |
/* |
| 2 |
* |
| 3 |
* |
| 4 |
* Copyright (C) 2023, KylinSoft Co., Ltd. |
| 5 |
* |
| 6 |
* This library is free software; you can redistribute it and/or |
| 7 |
* modify it under the terms of the GNU Lesser General Public |
| 8 |
* License as published by the Free Software Foundation; either |
| 9 |
* version 3 of the License, or (at your option) any later version. |
| 10 |
* |
| 11 |
* This library is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
* Lesser General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU General Public License |
| 17 |
* along with this library. If not, see <https://www.gnu.org/licenses/>. |
| 18 |
* |
| 19 |
* Authors: Sisheng He <[email protected]> |
| 20 |
* |
| 21 |
*/ |
| 22 |
#include "kflowlayout.h" |
| 23 |
|
| 24 |
#include <QStyle> |
| 25 |
#include <QtMath> |
| 26 |
|
| 27 |
namespace kdk |
| 28 |
{ |
| 29 |
|
| 30 |
class KFlowLayoutPrivate : public QObject |
| 31 |
{ |
| 32 |
Q_OBJECT |
| 33 |
Q_DECLARE_PUBLIC(KFlowLayout) |
| 34 |
public: |
| 35 |
KFlowLayoutPrivate(KFlowLayout *parent); |
| 36 |
|
| 37 |
private: |
| 38 |
KFlowLayout *q_ptr; |
| 39 |
QList<QLayoutItem *> m_itemList; |
| 40 |
int m_hSpace; |
| 41 |
int m_vSpace; |
| 42 |
bool m_home = false; |
| 43 |
|
| 44 |
private: |
| 45 |
int doLayout(const QRect &rect, bool testOnly) const; |
| 46 |
int smartSpacing(QStyle::PixelMetric pm) const; |
| 47 |
int fillSpaceX(QWidget *wid) const; |
| 48 |
}; |
| 49 |
|
| 50 |
KFlowLayoutPrivate::KFlowLayoutPrivate(KFlowLayout *parent) |
| 51 |
: q_ptr(parent) |
| 52 |
{ |
| 53 |
} |
| 54 |
|
| 55 |
int KFlowLayoutPrivate::smartSpacing(QStyle::PixelMetric pm) const |
| 56 |
{ |
| 57 |
Q_Q(const KFlowLayout); |
| 58 |
QObject *parent = q->parent(); |
| 59 |
if (!parent) { |
| 60 |
return -1; |
| 61 |
} else if (parent->isWidgetType()) { |
| 62 |
QWidget *pw = static_cast<QWidget *>(parent); |
| 63 |
return pw->style()->pixelMetric(pm, 0, pw); |
| 64 |
} else { |
| 65 |
return static_cast<QLayout *>(parent)->spacing(); |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
int KFlowLayoutPrivate::doLayout(const QRect &rect, bool testOnly) const |
| 70 |
{ |
| 71 |
Q_Q(const KFlowLayout); |
| 72 |
int left, top, right, bottom; |
| 73 |
q->getContentsMargins(&left, &top, &right, &bottom); |
| 74 |
QRect effectiveRect = rect.adjusted(+left, +top, -right, -bottom); |
| 75 |
int x = effectiveRect.x(); |
| 76 |
int y = effectiveRect.y(); |
| 77 |
int lineHeight = 0; |
| 78 |
|
| 79 |
int fillX = 0; |
| 80 |
bool bFillX = false; |
| 81 |
|
| 82 |
QLayoutItem *item; |
| 83 |
foreach (item, m_itemList) { |
| 84 |
QWidget *wid = item->widget(); |
| 85 |
int spaceX = q->horizontalSpacing(); |
| 86 |
if (spaceX == -1) { |
| 87 |
if (!bFillX) { |
| 88 |
bFillX = true; |
| 89 |
fillX = fillSpaceX(wid); |
| 90 |
} |
| 91 |
spaceX = fillX; |
| 92 |
} |
| 93 |
|
| 94 |
int spaceY = q->verticalSpacing(); |
| 95 |
if (spaceY == -1 && fillX >= 0) { |
| 96 |
spaceY = fillX; |
| 97 |
} /*else { |
| 98 |
spaceY = wid->style()->layoutSpacing( |
| 99 |
QSizePolicy::PushButton, QSizePolicy::PushButton, Qt::Vertical); |
| 100 |
}*/ |
| 101 |
if (m_home) { |
| 102 |
spaceY = 32; |
| 103 |
} |
| 104 |
|
| 105 |
int nextX = x + item->sizeHint().width() + spaceX; |
| 106 |
if (nextX - spaceX > effectiveRect.right() && lineHeight > 0) { |
| 107 |
x = effectiveRect.x(); |
| 108 |
y = y + lineHeight + spaceY; |
| 109 |
nextX = x + item->sizeHint().width() + spaceX; |
| 110 |
lineHeight = 0; |
| 111 |
} |
| 112 |
|
| 113 |
if (!testOnly) |
| 114 |
item->setGeometry(QRect(QPoint(x, y), item->sizeHint())); |
| 115 |
|
| 116 |
x = nextX; |
| 117 |
lineHeight = qMax(lineHeight, item->sizeHint().height()); |
| 118 |
} |
| 119 |
return y + lineHeight - rect.y() + bottom; |
| 120 |
} |
| 121 |
|
| 122 |
int KFlowLayoutPrivate::fillSpaceX(QWidget *wid) const |
| 123 |
{ |
| 124 |
Q_Q(const KFlowLayout); |
| 125 |
int num = 0; |
| 126 |
int x = 0; |
| 127 |
int numH = 0; |
| 128 |
int space = 4; |
| 129 |
if (m_home) { |
| 130 |
space = 24; |
| 131 |
} |
| 132 |
int len = q->parentWidget()->width() - q->contentsMargins().left() - q->contentsMargins().right(); |
| 133 |
while (true) { |
| 134 |
num++; |
| 135 |
if (num * (wid->width() + space) - space >= len) { // 最小间距space |
| 136 |
break; |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
num = num - 1; |
| 141 |
if (num <= 1) { |
| 142 |
numH = m_itemList.size(); |
| 143 |
return 32; |
| 144 |
} |
| 145 |
int height = wid->height(); |
| 146 |
numH = ceil(double(m_itemList.size()) / num); |
| 147 |
x = len + space - num * (wid->width() + space); |
| 148 |
x = ceil(double(x) / (num - 1)) + space; |
| 149 |
x = x - 1; // 考虑边框等因素影响 |
| 150 |
|
| 151 |
int maxY = numH * (height + x) + 32 - x; |
| 152 |
if (m_home) { |
| 153 |
maxY = numH * (height + 32); |
| 154 |
q->parentWidget()->parentWidget()->setFixedHeight(maxY); |
| 155 |
} |
| 156 |
q->parentWidget()->setFixedHeight(maxY); |
| 157 |
|
| 158 |
return x; |
| 159 |
} |
| 160 |
|
| 161 |
KFlowLayout::KFlowLayout(QWidget *parent, bool home, int margin, int hSpacing, int vSpacing) |
| 162 |
: QLayout(parent) |
| 163 |
, d_ptr(new KFlowLayoutPrivate(this)) |
| 164 |
{ |
| 165 |
Q_D(KFlowLayout); |
| 166 |
d->m_hSpace = hSpacing; |
| 167 |
d->m_vSpace = vSpacing; |
| 168 |
d->m_home = home; |
| 169 |
setContentsMargins(margin, margin, margin, margin); |
| 170 |
} |
| 171 |
|
| 172 |
KFlowLayout::KFlowLayout(int margin, int hSpacing, int vSpacing) |
| 173 |
: d_ptr(new KFlowLayoutPrivate(this)) |
| 174 |
{ |
| 175 |
Q_D(KFlowLayout); |
| 176 |
d->m_hSpace = hSpacing; |
| 177 |
d->m_vSpace = vSpacing; |
| 178 |
|
| 179 |
setContentsMargins(margin, margin, margin, margin); |
| 180 |
} |
| 181 |
|
| 182 |
KFlowLayout::~KFlowLayout() |
| 183 |
{ |
| 184 |
QLayoutItem *item; |
| 185 |
while ((item = takeAt(0))) { |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
void KFlowLayout::addItem(QLayoutItem *item) |
| 190 |
{ |
| 191 |
Q_D(KFlowLayout); |
| 192 |
d->m_itemList.append(item); |
| 193 |
} |
| 194 |
|
| 195 |
int KFlowLayout::horizontalSpacing() const |
| 196 |
{ |
| 197 |
Q_D(const KFlowLayout); |
| 198 |
if (d->m_hSpace >= 0 || d->m_hSpace == -1) { |
| 199 |
return d->m_hSpace; |
| 200 |
} else { |
| 201 |
return d->smartSpacing(QStyle::PM_LayoutHorizontalSpacing); |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
int KFlowLayout::verticalSpacing() const |
| 206 |
{ |
| 207 |
Q_D(const KFlowLayout); |
| 208 |
if (d->m_vSpace >= 0 || d->m_vSpace == -1) { |
| 209 |
return d->m_vSpace; |
| 210 |
} else { |
| 211 |
return d->smartSpacing(QStyle::PM_LayoutVerticalSpacing); |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
int KFlowLayout::count() const |
| 216 |
{ |
| 217 |
Q_D(const KFlowLayout); |
| 218 |
return d->m_itemList.size(); |
| 219 |
} |
| 220 |
|
| 221 |
QLayoutItem *KFlowLayout::itemAt(int index) const |
| 222 |
{ |
| 223 |
Q_D(const KFlowLayout); |
| 224 |
return d->m_itemList.value(index); |
| 225 |
} |
| 226 |
|
| 227 |
QLayoutItem *KFlowLayout::takeAt(int index) |
| 228 |
{ |
| 229 |
Q_D(KFlowLayout); |
| 230 |
if (index >= 0 && index < d->m_itemList.size()) |
| 231 |
return d->m_itemList.takeAt(index); |
| 232 |
else |
| 233 |
return 0; |
| 234 |
} |
| 235 |
|
| 236 |
Qt::Orientations KFlowLayout::expandingDirections() const |
| 237 |
{ |
| 238 |
Q_D(const KFlowLayout); |
| 239 |
return Qt::Orientations(); |
| 240 |
} |
| 241 |
|
| 242 |
bool KFlowLayout::hasHeightForWidth() const |
| 243 |
{ |
| 244 |
Q_D(const KFlowLayout); |
| 245 |
return true; |
| 246 |
} |
| 247 |
|
| 248 |
int KFlowLayout::heightForWidth(int width) const |
| 249 |
{ |
| 250 |
Q_D(const KFlowLayout); |
| 251 |
int height = d->doLayout(QRect(0, 0, width, 0), true); |
| 252 |
return height; |
| 253 |
} |
| 254 |
|
| 255 |
void KFlowLayout::setGeometry(const QRect &rect) |
| 256 |
{ |
| 257 |
Q_D(KFlowLayout); |
| 258 |
QLayout::setGeometry(rect); |
| 259 |
d->doLayout(rect, false); |
| 260 |
} |
| 261 |
|
| 262 |
QSize KFlowLayout::sizeHint() const |
| 263 |
{ |
| 264 |
Q_D(const KFlowLayout); |
| 265 |
return minimumSize(); |
| 266 |
} |
| 267 |
|
| 268 |
QSize KFlowLayout::minimumSize() const |
| 269 |
{ |
| 270 |
Q_D(const KFlowLayout); |
| 271 |
QSize size; |
| 272 |
QLayoutItem *item; |
| 273 |
foreach (item, d->m_itemList) |
| 274 |
size = size.expandedTo(item->minimumSize()); |
| 275 |
|
| 276 |
// size += QSize(2 * margin(), 2 * margin()); |
| 277 |
QMargins margins = contentsMargins(); |
| 278 |
size += QSize(margins.left() + margins.right(), margins.top() + margins.bottom()); |
| 279 |
return size; |
| 280 |
} |
| 281 |
|
| 282 |
} |
| 283 |
|
| 284 |
#include "kflowlayout.moc" |
| 285 |
#include "moc_kflowlayout.cpp" |