/
flowlayout.cpp
215 lines (182 loc) · 4.59 KB
/
flowlayout.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/*******************************************
*
* Copyright © 2013 Léo Flaventin Hauchecorne
*
* This file is part of Ardoise.
*
* Ardoise is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Ardoise is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Foobar. If not, see <http://www.gnu.org/licenses/>.
********************************************/
#include <QtGui>
#include <limits.h>
#include "flowlayout.h"
FlowLayout::FlowLayout(QWidget *parent, Qt::Orientation o, int hs, int vs, Qt::LayoutDirection d, bool e) :
QLayout(parent), m_o(o), m_hs(hs), m_vs(vs), m_d(d), m_e(e)
{
}
FlowLayout::FlowLayout(Qt::Orientation o, int hs, int vs, Qt::LayoutDirection d, bool e) :
QLayout(), m_o(o), m_hs(hs), m_vs(vs), m_d(d), m_e(e)
{
}
FlowLayout::~FlowLayout()
{
QLayoutItem *item;
while ((item = takeAt(0))) delete item;
}
void FlowLayout::addItem(QLayoutItem *it)
{
items.append(it);
}
QLayoutItem * FlowLayout::itemAt(int i) const
{
return i<items.length() ? items.at(i) : 0;
}
QLayoutItem * FlowLayout::takeAt(int i)
{
return i<items.length() ? items.takeAt(i) : 0;
}
int FlowLayout::count() const
{
return items.size();
}
void FlowLayout::setGeometry(const QRect &r)
{
doLayout(r, true);
}
QSize FlowLayout::sizeHint() const
{
int w=0,h=0;
QSize s;
auto it = items.begin();
auto end = items.end();
for(;it!=end ; ++it)
{
s = (*it)->sizeHint();
w += s.width();
if(h<s.height()) h = s.height();
}
s.setWidth(w/4);
s.setHeight(h);
return s;
}
bool FlowLayout::hasHeightForWidth() const
{
return true;
}
int FlowLayout::heightForWidth(int w) const
{
return doLayout(QRect(0,0,w,0), false).height();
}
QSize FlowLayout::doLayout(QRect rect, bool apply) const
{
//Configuration des marges
int x,y=0; //Coordonnées courrantes
int left, right, top, bottom; //Marge du layout
int h=0; //Hauteur de la ligne courante
int w;//Largeur du layout
int nbe;//Nombre de widget expensible latéralement
int p;//nom de pixel à rajouter aux widget expendable
QSize s;
auto it_b = items.begin(), end = items.end();
auto it_e = it_b;
getContentsMargins(&left, & top, &right, &bottom);
if(m_d == Qt::LeftToRight)
{
w = rect.width() - right;
y = rect.top() + top;
left += rect.left();
while(it_b != end)
{
nbe = (*it_b)->expandingDirections() & Qt::Horizontal;
x = left;
y += h;
s = (*it_b)->sizeHint();
x += s.width();
h = s.height();
it_e = it_b;
while(x < w && (++it_e) != end)
{
nbe += (*it_e)->expandingDirections() & Qt::Horizontal;
s = (*it_e)->sizeHint();
x += s.width() + m_hs;
if(h<s.height()) h = s.height();
}
if(it_e == it_b) ++it_e;
if(it_e != end)
{
x -= s.width();
nbe -= (*it_b)->expandingDirections() & Qt::Horizontal;
}
if(nbe)
{
p = (w-x) / nbe;
nbe = (w-x) % nbe;
}
else p=0;
if(apply)
{
x = left;
while(it_b != it_e)
{
s = (*it_b)->sizeHint();
s.setHeight(h);
if(m_e && ((*it_b)->expandingDirections() & Qt::Horizontal))
{
if(nbe)
{
--nbe;
s.rwidth() += p+1;
}
else s.rwidth() += p;
}
(*it_b)->setGeometry(QRect(QPoint(x,y),s));
++it_b;
x += s.width() + m_hs;
}
}
it_b = it_e;
y += m_vs;
}
}
s.setWidth(rect.width());
s.setHeight(y+h+bottom);
return s;
}
Qt::LayoutDirection FlowLayout::layoutDirection() const
{
return m_d;
}
void FlowLayout::setExpandsWidgets(bool e)
{
m_e = e;
}
bool FlowLayout::expandsWidgets() const
{
return m_e;
}
void FlowLayout::setHSpace(int hs)
{
m_hs = hs;
}
int FlowLayout::HSpace() const
{
return m_hs;
}
void FlowLayout::setVSpace(int vs)
{
m_vs = vs;
}
int FlowLayout::VSpace() const
{
return m_vs;
}