LeechCraft 0.6.70-14794-g33744ae6ce
Modular cross-platform feature rich live environment.
Loading...
Searching...
No Matches
flowlayout.cpp
Go to the documentation of this file.
1/**********************************************************************
2 * LeechCraft - modular cross-platform feature rich internet client.
3 * Copyright (C) 2006-2014 Georg Rudoy
4 *
5 * Distributed under the Boost Software License, Version 1.0.
6 * (See accompanying file LICENSE or copy at https://www.boost.org/LICENSE_1_0.txt)
7 **********************************************************************/
8
9#include "flowlayout.h"
10#include <QWidget>
11
12namespace LC::Util
13{
14 FlowLayout::FlowLayout (QWidget *parent,
15 int margin, int hspace, int vspace)
16 : QLayout { parent }
17 , HSpace_ { hspace }
18 , VSpace_ { vspace }
19 {
20 setContentsMargins (margin, margin, margin, margin);
21 }
22
23 FlowLayout::FlowLayout (int margin, int hspace, int vspace)
24 : FlowLayout { nullptr, margin, hspace, vspace }
25 {
26 }
27
29 {
30 qDeleteAll (ItemList_);
31 }
32
33 void FlowLayout::addItem (QLayoutItem *item)
34 {
35 ItemList_ << item;
36 }
37
39 {
40 return HSpace_ >= 0 ?
41 HSpace_ :
42 SmartSpacing (QStyle::PM_LayoutHorizontalSpacing);
43 }
44
46 {
47 return VSpace_ >= 0 ?
48 VSpace_ :
49 SmartSpacing (QStyle::PM_LayoutVerticalSpacing);
50 }
51
52 Qt::Orientations FlowLayout::expandingDirections () const
53 {
54 return {};
55 }
56
58 {
59 return true;
60 }
61
62 int FlowLayout::heightForWidth (int width) const
63 {
64 return DoLayout ({ 0, 0, width, 0 }, true);
65 }
66
67 int FlowLayout::count () const
68 {
69 return ItemList_.size ();
70 }
71
72 QLayoutItem* FlowLayout::itemAt (int idx) const
73 {
74 return ItemList_.value (idx);
75 }
76
77 QLayoutItem* FlowLayout::takeAt (int idx)
78 {
79 if (idx < 0 || idx >= ItemList_.size ())
80 return nullptr;
81
82 return ItemList_.takeAt (idx);
83 }
84
86 {
87 QSize size;
88 for (const auto item : ItemList_)
89 size = size.expandedTo (item->minimumSize ());
90
91 size += QSize { margin () * 2, margin () * 2 };
92 return size;
93 }
94
95 void FlowLayout::setGeometry (const QRect& rect)
96 {
97 QLayout::setGeometry (rect);
98 DoLayout (rect, false);
99 }
100
101 QSize FlowLayout::sizeHint () const
102 {
103 return minimumSize ();
104 }
105
106 int FlowLayout::DoLayout (const QRect& rect, bool testOnly) const
107 {
108 int left = 0, top = 0, right = 0, bottom = 0;
109 getContentsMargins (&left, &top, &right, &bottom);
110
111 const auto& effectiveRect = rect.adjusted (left, top, -right, -bottom);
112 int x = effectiveRect.x ();
113 int y = effectiveRect.y ();
114 int lineHeight = 0;
115
116 for (const auto item : ItemList_)
117 {
118 const auto widget = item->widget ();
119
120 int spaceX = horizontalSpacing ();
121 if (spaceX == -1)
122 spaceX = widget->style ()->layoutSpacing (QSizePolicy::PushButton,
123 QSizePolicy::PushButton, Qt::Horizontal);
124 int spaceY = verticalSpacing ();
125 if (spaceY == -1)
126 spaceY = widget->style ()->layoutSpacing (QSizePolicy::PushButton,
127 QSizePolicy::PushButton, Qt::Vertical);
128
129 const auto& sizeHint = item->sizeHint ();
130 const int hintWidth = sizeHint.width ();
131 int nextX = x + hintWidth + spaceX;
132 if (nextX - spaceX > effectiveRect.right () &&
133 lineHeight > 0)
134 {
135 x = effectiveRect.x ();
136 y += lineHeight + spaceY;
137 nextX = x + hintWidth + spaceX;
138 lineHeight = 0;
139 }
140
141 if (!testOnly)
142 item->setGeometry ({ { x, y }, sizeHint });
143
144 x = nextX;
145 lineHeight = std::max (lineHeight, sizeHint.height ());
146 }
147
148 return y + lineHeight - rect.y () + bottom;
149 }
150
151 int FlowLayout::SmartSpacing (QStyle::PixelMetric pm) const
152 {
153 const auto obj = parent ();
154 if (!obj)
155 return -1;
156
157 if (const auto pw = dynamic_cast<QWidget*> (obj))
158 return pw->style ()->pixelMetric (pm, nullptr, pw);
159 if (const auto lay = dynamic_cast<QLayout*> (obj))
160 return lay->spacing ();
161
162 return -1;
163 }
164}
A simple flow layout implementation.
Definition: flowlayout.h:25
FlowLayout(QWidget *, int=-1, int=-1, int=-1)
Definition: flowlayout.cpp:14
QSize minimumSize() const override
Definition: flowlayout.cpp:85
QLayoutItem * takeAt(int) override
Definition: flowlayout.cpp:77
int verticalSpacing() const
Definition: flowlayout.cpp:45
void addItem(QLayoutItem *) override
Definition: flowlayout.cpp:33
int count() const override
Definition: flowlayout.cpp:67
~FlowLayout() override
Definition: flowlayout.cpp:28
bool hasHeightForWidth() const override
Definition: flowlayout.cpp:57
int heightForWidth(int) const override
Definition: flowlayout.cpp:62
QLayoutItem * itemAt(int) const override
Definition: flowlayout.cpp:72
int horizontalSpacing() const
Definition: flowlayout.cpp:38
QSize sizeHint() const override
Definition: flowlayout.cpp:101
void setGeometry(const QRect &) override
Definition: flowlayout.cpp:95
Qt::Orientations expandingDirections() const override
Definition: flowlayout.cpp:52