Messagelib

searchlinecommandflowlayout.cpp
1/*
2 SPDX-FileCopyrightText: 2020-2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "searchlinecommandflowlayout.h"
8
9#include <QStyle>
10#include <QWidget>
11using namespace MessageList::Core;
12static int smartSpacing(QObject *parent, QStyle::PixelMetric pm)
13{
14 if (!parent) {
15 return -1;
16 } else if (parent->isWidgetType()) {
17 auto pw = static_cast<QWidget *>(parent);
18 return pw->style()->pixelMetric(pm, nullptr, pw);
19 } else {
20 return static_cast<QLayout *>(parent)->spacing();
21 }
22}
23
24SearchLineCommandFlowLayout::SearchLineCommandFlowLayout(QWidget *parent)
25 : QLayout(parent)
26{
27}
28
29SearchLineCommandFlowLayout::~SearchLineCommandFlowLayout()
30{
31 clear();
32}
33
34int SearchLineCommandFlowLayout::horizontalSpacing() const
35{
36 if (mHorizontalSpacing >= 0) {
37 return mHorizontalSpacing;
38 } else {
39 return smartSpacing(parent(), QStyle::PM_LayoutHorizontalSpacing);
40 }
41}
42
43void SearchLineCommandFlowLayout::setHorizontalSpacing(int horizontalSpacing)
44{
45 if (mHorizontalSpacing != horizontalSpacing) {
46 mHorizontalSpacing = horizontalSpacing;
47 invalidate();
48 }
49}
50
51int SearchLineCommandFlowLayout::verticalSpacing() const
52{
53 if (mVerticalSpacing >= 0) {
54 return mVerticalSpacing;
55 } else {
56 return smartSpacing(parent(), QStyle::PM_LayoutVerticalSpacing);
57 }
58}
59
60void SearchLineCommandFlowLayout::setVerticalSpacing(int verticalSpacing)
61{
62 if (mVerticalSpacing != verticalSpacing) {
63 mVerticalSpacing = verticalSpacing;
64 invalidate();
65 }
66}
67
68QSize SearchLineCommandFlowLayout::sizeHint() const
69{
70 return minimumSize();
71}
72
73QSize SearchLineCommandFlowLayout::minimumSize() const
74{
75 const QMargins margins = contentsMargins();
76 QSize size;
77
78 for (const QLayoutItem *item : mItems) {
79 size = size.expandedTo(item->minimumSize());
80 }
81
82 size += QSize(margins.left() + margins.right(), margins.top() + margins.bottom());
83 return size;
84}
85
86void SearchLineCommandFlowLayout::addItem(QLayoutItem *item)
87{
88 Q_ASSERT(!mItems.contains(item));
89 mItems.append(item);
90 invalidate();
91}
92
93QLayoutItem *SearchLineCommandFlowLayout::itemAt(int index) const
94{
95 if (index >= 0 && index < mItems.count()) {
96 return mItems[index];
97 }
98
99 return nullptr;
100}
101
102QLayoutItem *SearchLineCommandFlowLayout::takeAt(int index)
103{
104 if (index >= 0 && index < mItems.count()) {
105 auto *it = mItems.takeAt(index);
106 invalidate();
107 return it;
108 }
109
110 return nullptr;
111}
112
113int SearchLineCommandFlowLayout::count() const
114{
115 return mItems.count();
116}
117
118Qt::Orientations SearchLineCommandFlowLayout::expandingDirections() const
119{
120 return {};
121}
122
123bool SearchLineCommandFlowLayout::hasHeightForWidth() const
124{
125 return true;
126}
127
128int SearchLineCommandFlowLayout::heightForWidth(int width) const
129{
130 return doFlow(QRect(0, 0, width, 0), false);
131}
132
133void SearchLineCommandFlowLayout::setGeometry(const QRect &rect)
134{
136 doFlow(rect, true);
137}
138
139void SearchLineCommandFlowLayout::clear()
140{
141 while (QLayoutItem *item = takeAt(0)) {
142 delete item;
143 }
144}
145
146void SearchLineCommandFlowLayout::clearAndDeleteWidgets()
147{
148 while (QLayoutItem *item = takeAt(0)) {
149 item->widget()->deleteLater();
150 delete item;
151 }
152}
153
154int SearchLineCommandFlowLayout::doFlow(QRect rect, bool effective) const
155{
156 const QMargins margins = contentsMargins();
157 const QRect effectiveRect = rect.adjusted(margins.left(), margins.top(), -margins.right(), -margins.bottom());
158 int x = effectiveRect.x();
159 int y = effectiveRect.y();
160 int highest = 0;
161
162 for (QLayoutItem *item : mItems) {
163 const QWidget *widget = item->widget();
164
166 continue;
167 }
168
169 int hSpacing = horizontalSpacing();
170 int vSpacing = verticalSpacing();
171
172 if (hSpacing == -1) {
174 }
175
176 if (vSpacing == -1) {
178 }
179
180 int widgetXPos = x + item->sizeHint().width() + hSpacing;
181
182 if (widgetXPos - hSpacing > effectiveRect.right() && highest > 0) {
183 x = effectiveRect.x();
184 y += highest + vSpacing;
185 widgetXPos = x + item->sizeHint().width() + hSpacing;
186 highest = 0;
187 }
188
189 if (effective) {
190 item->setGeometry(QRect(QPoint(x, y), item->sizeHint()));
191 }
192
193 x = widgetXPos;
194 highest = qMax(highest, item->sizeHint().height());
195 }
196
197 return y + highest - rect.y() + margins.bottom();
198}
199
200#include "moc_searchlinecommandflowlayout.cpp"
The implementation independent part of the MessageList library.
Definition aggregation.h:22
QMargins contentsMargins() const const
virtual void invalidate() override
QWidget * parentWidget() const const
virtual void setGeometry(const QRect &r) override
virtual void setGeometry(const QRect &r)=0
virtual QSize sizeHint() const const=0
virtual QWidget * widget() const const
int bottom() const const
int left() const const
int right() const const
int top() const const
void deleteLater()
bool isWidgetType() const const
QObject * parent() const const
QRect adjusted(int dx1, int dy1, int dx2, int dy2) const const
int right() const const
int x() const const
int y() const const
QSize expandedTo(const QSize &otherSize) const const
int height() const const
int width() const const
virtual int layoutSpacing(QSizePolicy::ControlType control1, QSizePolicy::ControlType control2, Qt::Orientation orientation, const QStyleOption *option, const QWidget *widget) const const=0
virtual int pixelMetric(PixelMetric metric, const QStyleOption *option, const QWidget *widget) const const=0
typedef Orientations
bool isVisibleTo(const QWidget *ancestor) const const
QStyle * style() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:07:25 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.