-
Notifications
You must be signed in to change notification settings - Fork 2
/
gridlayoutex.cpp
311 lines (241 loc) · 8.12 KB
/
gridlayoutex.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#include "gridlayoutex.h"
#include <QDebug>
#include <iterator>
#include <QMapIterator>
GLEPageButton::GLEPageButton(int pid, QWidget* parent)
: POSToolButtonEx(parent)
, _pageId(pid)
{
this->connect(this, SIGNAL(pageChanged(int)), parent, SLOT(pageChanged(int)));
}
GLEPageButton::~GLEPageButton() {
}
GLENextPageButton::GLENextPageButton(int pid, QWidget* parent)
: GLEPageButton(pid, parent)
{
this->setText(tr("下一页"));
this->connect(this, SIGNAL(released()), this, SLOT(onClicked()));
this->setStyleSheet("background-color: rgb(128, 64, 128)");
}
GLENextPageButton::~GLENextPageButton() {
}
void GLENextPageButton::onClicked() {
emit(pageChanged(_pageId + 1));
}
GLEPrevPageButton::GLEPrevPageButton(int pid, QWidget* parent)
: GLEPageButton(pid, parent)
{
this->setText(tr("上一页"));
this->connect(this, SIGNAL(released()), this, SLOT(onClicked()));
this->setStyleSheet("background-color: rgb(64, 128, 64)");
}
GLEPrevPageButton::~GLEPrevPageButton() {
}
void GLEPrevPageButton::onClicked() {
emit(pageChanged(_pageId - 1));
}
///////////////////////////////////////////////////////////////
GridLayoutEx::GridLayoutEx(int columns,
QWidget *parent) :
QFrame(parent)
, _rowHeight(50)
, _columnWidth(80)
, _columns(columns)
, _border(1)
, _pageId(0)
, _lastPageId(0)
{
_items = new QList<LayoutItem>();
//默每个page都会有两个按钮
this->_prevPage = new GLEPrevPageButton(_pageId, this);
this->_nextPage = new GLENextPageButton(_pageId, this);
this->_prevPage->hide();
this->_nextPage->hide();
}
GridLayoutEx::~GridLayoutEx()
{
}
void GridLayoutEx::resizeEvent(QResizeEvent* event) {
QFrame::resizeEvent(event);
resetPages();
}
bool GridLayoutEx::existsItem(QList<LayoutItem>::iterator& itbefor, QWidget* obj, QRect& rect) {
QList<LayoutItem>::iterator it = _items->begin();
for(; it != _items->end() && it != itbefor; it++) {
//
int pid = it->_pageId;
if( pid != _pageId) {
//只更新当前页的大小
it->_obj->hide();
continue;
}
if( it->_obj->isHidden() ) {
continue;
}
QWidget* _obj = it->_obj;
if(_obj == obj) {
continue;
}
QRect _ir = _obj->geometry();
// rect 的左上角或是右下角,在 _ir的区间内
// _ir 的左上角或是右下角在rect的区间内
if( rect.left() >= _ir.left() && rect.top() >= _ir.top()
&& rect.left() < _ir.right() && rect.top() < _ir.bottom()) {
return true;
} else if( rect.right() >= _ir.left() && rect.bottom() >= _ir.top()
&& rect.right() <= _ir.right() && rect.bottom() <= _ir.bottom()) {
return true;
} else if( _ir.left() >= rect.left() && _ir.top() >= rect.top()
&& _ir.left() <= rect.right() && _ir.top() <= rect.bottom()) {
return true;
} else if( _ir.right() >= rect.left() && _ir.bottom() >= rect.top()
&& _ir.right() <= rect.right() && _ir.bottom() <= rect.bottom()) {
return true;
}
}
return false;
}
void GridLayoutEx::resetPages() {
QRect rect = this->frameGeometry();
if(5 > this->_rowHeight ) {
this->_rowHeight = 5;
}
double rows = rect.height() / this->_rowHeight;
// qDebug() << this->objectName() << " resize event be called in gridlayoutex ...";
// qDebug() << this->objectName() << " rows: " << rows << ", row height: " << this->_rowHeight;
if(rows == 0.0) {
return;
}
QList<LayoutItem>::iterator it = _items->begin();
int size = _items->size();
int curPageItemCount = 0;
for(; it != _items->end(); it++, curPageItemCount++) {
int totalItemsInPage = rows * this->_columns;
if(totalItemsInPage == 0) {
return;
}
if( totalItemsInPage >= size) {
_lastPageId = curPageItemCount / totalItemsInPage;
} else if( totalItemsInPage > 2 ) {
_lastPageId = curPageItemCount / ( totalItemsInPage - 2);
} else {
_lastPageId = curPageItemCount / totalItemsInPage;
}
it->_pageId = _lastPageId;
// qDebug() << this->objectName() << ", obj: " << it->_obj->objectName() << ", pageID: " << it->_pageId;
}
resizeItems();
}
void GridLayoutEx::resizeItems() {
// qDebug() << this->objectName() << " resize items in this layout be called ...";
QRect rect = this->geometry();
if(5 > this->_rowHeight ) {
this->_rowHeight = 5;
}
double rows = rect.height() / this->_rowHeight;
this->_columnWidth = rect.width() / this->_columns;
int w = this->_columnWidth, h = this->_rowHeight;
int r = 0, c = 0 ;
int i = 0;
QList<LayoutItem>::iterator it = _items->begin();
for(; it != _items->end(); it++) {
//
int pid = it->_pageId;
if( pid != _pageId) {
//只更新当前页的大小
it->_obj->hide();
continue;
}
// qDebug() << it->_obj->objectName() << ", pageid: " << it->_pageId;
QWidget* obj = it->_obj;
QRect _ir;
do {
c = i % (this->_columns);
r = (c == 0 ? ++r : r);
_ir = QRect (c * w + _border, (r - 1) * h + _border,
it->_colMerge * w - _border,
it->_rowMerge * h - _border);
if( existsItem(it, obj, _ir) == true) {
i += 1;
} else {
break;
}
}while(true);
//如果这区间有其它组件,则向下一个尝试
obj->setGeometry(_ir);
obj->show();
i += it->_colMerge;
}
//显示页控制按钮
//如果 qwidget 是GLEPageButton, 则放到最后,
//如果一页能显示完所有的 qwidget,则不显示 GLEPageButton
if( this->_columns >= 2) {
this->_prevPage->setGeometry(( this->_columns - 2) * w, (rows - 1) * h, w, h );
} else if(this->_columns == 1) {
this->_prevPage->setGeometry(( this->_columns - 1) * w, (rows - 2) * h, w, h );
}
this->_nextPage->setGeometry(( this->_columns - 1) * w, (rows - 1) * h, w, h );
if( this->_pageId == 0 ) {
this->_prevPage->hide();
} else {
this->_prevPage->show();
}
if( this->_pageId == this->_lastPageId) {
this->_nextPage->hide();
this->_prevPage->setGeometry(( this->_columns - 1) * w, (rows - 1) * h, w, h );
} else if( this->_lastPageId > 0) {
this->_nextPage->show();
}
}
void GridLayoutEx::showEvent(QShowEvent* event) {
QFrame::showEvent(event);
resizeItems();
}
void GridLayoutEx::pageChanged(int toPage) {
qDebug() << " page chage to: " << toPage;
this->_pageId = toPage;
//更新所有的控制页 page id
this->_prevPage->updatePageId(toPage);
this->_nextPage->updatePageId(toPage);
this->resizeItems();
}
void GridLayoutEx::addWidget( QWidget* item, int colMerge, int rowMerge, int id) {
//
LayoutItem li;
li._obj = item;
li._pageId = 0;
li._rowMerge = rowMerge;
li._colMerge = colMerge;
if( id == 0) {
li._id = this->_items->length();
} else {
li._id = id;
}
this->_items->push_back(li);
}
QWidget* GridLayoutEx::getItembyID(int id) {
foreach (LayoutItem li, *this->_items) {
if( li._id == id) {
return li._obj;
}
}
qDebug() << "not found object in gridlayout[" << this->objectName() << "] with id: " << id;
return nullptr;
}
QWidget* GridLayoutEx::getItembyObjectName(const QString& oname) {
foreach (LayoutItem li, *this->_items) {
if( li._obj->objectName() == oname) {
return li._obj;
}
}
qDebug() << "not found object in gridlayout[" << this->objectName() << "] with object name: " << oname;
return nullptr;
}
void GridLayoutEx::clean() {
QList<LayoutItem>::iterator it = _items->begin();
for(; it != _items->end(); it++) {
it->_obj->hide();
}
_items->erase(_items->begin(), it);
this->_pageId = 0;
}