Mercurial > minori
comparison src/gui/layouts/flow_layout.cc @ 253:b3549da699a6
*: ooooh! stupid big commit!
oops
| author | Paper <paper@paper.us.eu.org> |
|---|---|
| date | Tue, 06 Feb 2024 16:56:32 -0500 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 252:a0eeb2cc7e6d | 253:b3549da699a6 |
|---|---|
| 1 /* | |
| 2 * Copyright (C) 2016 The Qt Company Ltd. | |
| 3 * Contact: https://www.qt.io/licensing/ | |
| 4 * | |
| 5 * This file is part of the QtCore module of the Qt Toolkit. | |
| 6 * | |
| 7 * "Redistribution and use in source and binary forms, with or without | |
| 8 * modification, are permitted provided that the following conditions are | |
| 9 * met: | |
| 10 * * Redistributions of source code must retain the above copyright | |
| 11 * notice, this list of conditions and the following disclaimer. | |
| 12 * * Redistributions in binary form must reproduce the above copyright | |
| 13 * notice, this list of conditions and the following disclaimer in | |
| 14 * the documentation and/or other materials provided with the | |
| 15 * distribution. | |
| 16 * * Neither the name of The Qt Company Ltd nor the names of its | |
| 17 * contributors may be used to endorse or promote products derived | |
| 18 * from this software without specific prior written permission. | |
| 19 * | |
| 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| 21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
| 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
| 23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
| 24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
| 25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
| 26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
| 27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
| 28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
| 29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
| 30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." | |
| 31 */ | |
| 32 #include "gui/layouts/flow_layout.h" | |
| 33 | |
| 34 #include <QWidget> | |
| 35 | |
| 36 #include <algorithm> | |
| 37 | |
| 38 FlowLayout::FlowLayout(QWidget *parent, int margin, int hSpacing, int vSpacing) | |
| 39 : QLayout(parent), _horiz_space(hSpacing), _vert_space(vSpacing) { | |
| 40 setContentsMargins(margin, margin, margin, margin); | |
| 41 } | |
| 42 | |
| 43 FlowLayout::FlowLayout(int margin, int hSpacing, int vSpacing) | |
| 44 : _horiz_space(hSpacing), _vert_space(vSpacing) { | |
| 45 setContentsMargins(margin, margin, margin, margin); | |
| 46 } | |
| 47 | |
| 48 FlowLayout::~FlowLayout() { | |
| 49 while (count()) | |
| 50 delete takeAt(0); | |
| 51 } | |
| 52 | |
| 53 void FlowLayout::addItem(QLayoutItem *item) { | |
| 54 item_list.append(item); | |
| 55 } | |
| 56 | |
| 57 int FlowLayout::horizontalSpacing() const { | |
| 58 return (_horiz_space >= 0) ? _horiz_space : smartSpacing(QStyle::PM_LayoutHorizontalSpacing); | |
| 59 } | |
| 60 | |
| 61 int FlowLayout::verticalSpacing() const { | |
| 62 return (_vert_space >= 0) ? _vert_space : smartSpacing(QStyle::PM_LayoutVerticalSpacing); | |
| 63 } | |
| 64 | |
| 65 int FlowLayout::count() const { | |
| 66 return item_list.size(); | |
| 67 } | |
| 68 | |
| 69 QLayoutItem* FlowLayout::itemAt(int index) const { | |
| 70 return item_list.value(index); | |
| 71 } | |
| 72 | |
| 73 QLayoutItem* FlowLayout::takeAt(int index) { | |
| 74 return (index >= 0 && index < item_list.size()) ? item_list.takeAt(index) : nullptr; | |
| 75 } | |
| 76 | |
| 77 Qt::Orientations FlowLayout::expandingDirections() const | |
| 78 { | |
| 79 return {}; | |
| 80 } | |
| 81 | |
| 82 bool FlowLayout::hasHeightForWidth() const { | |
| 83 return true; | |
| 84 } | |
| 85 | |
| 86 int FlowLayout::heightForWidth(int width) const { | |
| 87 return doLayout(QRect(0, 0, width, 0), true); | |
| 88 } | |
| 89 | |
| 90 void FlowLayout::setGeometry(const QRect &rect) { | |
| 91 QLayout::setGeometry(rect); | |
| 92 doLayout(rect, false); | |
| 93 } | |
| 94 | |
| 95 QSize FlowLayout::sizeHint() const { | |
| 96 return minimumSize(); | |
| 97 } | |
| 98 | |
| 99 QSize FlowLayout::minimumSize() const { | |
| 100 QSize size; | |
| 101 for (QLayoutItem* item : item_list) | |
| 102 size = size.expandedTo(item->minimumSize()); | |
| 103 | |
| 104 const QMargins margins = contentsMargins(); | |
| 105 size += QSize(margins.left() + margins.right(), margins.top() + margins.bottom()); | |
| 106 return size; | |
| 107 } | |
| 108 | |
| 109 int FlowLayout::doLayout(const QRect &rect, bool test) const { | |
| 110 int left, top, right, bottom; | |
| 111 getContentsMargins(&left, &top, &right, &bottom); | |
| 112 QRect effectiveRect = rect.adjusted(+left, +top, -right, -bottom); | |
| 113 | |
| 114 int x = effectiveRect.x(); | |
| 115 int y = effectiveRect.y(); | |
| 116 int line_height = 0; | |
| 117 | |
| 118 for (QLayoutItem* item : item_list) { | |
| 119 const QWidget* wid = item->widget(); | |
| 120 int horiz_space = horizontalSpacing(); | |
| 121 if (horiz_space == -1) | |
| 122 horiz_space = wid->style()->layoutSpacing(QSizePolicy::PushButton, QSizePolicy::PushButton, Qt::Horizontal); | |
| 123 | |
| 124 int vert_space = verticalSpacing(); | |
| 125 if (vert_space == -1) | |
| 126 vert_space = wid->style()->layoutSpacing(QSizePolicy::PushButton, QSizePolicy::PushButton, Qt::Vertical); | |
| 127 | |
| 128 int next_x = x + item->sizeHint().width() + horiz_space; | |
| 129 if ((next_x - horiz_space > effectiveRect.right()) && (line_height > 0)) { | |
| 130 x = effectiveRect.x(); | |
| 131 y = y + line_height + vert_space; | |
| 132 next_x = x + item->sizeHint().width() + horiz_space; | |
| 133 line_height = 0; | |
| 134 } | |
| 135 | |
| 136 if (!test) | |
| 137 item->setGeometry(QRect(QPoint(x, y), item->sizeHint())); | |
| 138 | |
| 139 x = next_x; | |
| 140 line_height = std::max(line_height, item->sizeHint().height()); | |
| 141 } | |
| 142 | |
| 143 return y + line_height - rect.y() + bottom; | |
| 144 } | |
| 145 | |
| 146 int FlowLayout::smartSpacing(QStyle::PixelMetric pm) const { | |
| 147 QObject *parent = this->parent(); | |
| 148 if (!parent) { | |
| 149 return -1; | |
| 150 } else if (parent->isWidgetType()) { | |
| 151 QWidget *pw = static_cast<QWidget *>(parent); | |
| 152 return pw->style()->pixelMetric(pm, nullptr, pw); | |
| 153 } else { | |
| 154 return static_cast<QLayout *>(parent)->spacing(); | |
| 155 } | |
| 156 } |
