author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Fri, 19 Feb 2010 23:40:16 +0200 | |
branch | RCL_3 |
changeset 4 | 3b1da2848fc7 |
parent 3 | 41300fa6a67c |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 4 |
** All rights reserved. |
5 |
** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 |
** |
|
7 |
** This file is part of the test suite of the Qt Toolkit. |
|
8 |
** |
|
9 |
** $QT_BEGIN_LICENSE:LGPL$ |
|
10 |
** No Commercial Usage |
|
11 |
** This file contains pre-release code and may not be distributed. |
|
12 |
** You may use this file in accordance with the terms and conditions |
|
13 |
** contained in the Technology Preview License Agreement accompanying |
|
14 |
** this package. |
|
15 |
** |
|
16 |
** GNU Lesser General Public License Usage |
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 |
** General Public License version 2.1 as published by the Free Software |
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 |
** packaging of this file. Please review the following information to |
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 |
** |
|
24 |
** In addition, as a special exception, Nokia gives you certain additional |
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 |
** |
|
28 |
** If you have questions regarding the use of this file, please contact |
|
29 |
** Nokia at qt-info@nokia.com. |
|
30 |
** |
|
31 |
** |
|
32 |
** |
|
33 |
** |
|
34 |
** |
|
35 |
** |
|
36 |
** |
|
37 |
** |
|
38 |
** $QT_END_LICENSE$ |
|
39 |
** |
|
40 |
****************************************************************************/ |
|
41 |
||
42 |
||
43 |
#include <QtTest/QtTest> |
|
44 |
#include <qlayout.h> |
|
45 |
#include <qapplication.h> |
|
46 |
#include <qwidget.h> |
|
47 |
#include <qwindowsstyle.h> |
|
48 |
#include <qsizepolicy.h> |
|
49 |
#include <QtGui> |
|
50 |
||
51 |
#include <QtGui/QWindowsStyle> |
|
52 |
#include <QStyleFactory> |
|
53 |
||
54 |
#include "../../shared/util.h" |
|
55 |
||
56 |
//TESTED_CLASS= |
|
57 |
//TESTED_FILES=gui/kernel/qlayout.cpp gui/kernel/qlayout.h |
|
58 |
||
59 |
||
60 |
class tst_QGridLayout : public QObject |
|
61 |
{ |
|
62 |
Q_OBJECT |
|
63 |
||
64 |
public: |
|
65 |
tst_QGridLayout(); |
|
66 |
virtual ~tst_QGridLayout(); |
|
67 |
||
68 |
||
69 |
public slots: |
|
70 |
void initTestCase(); |
|
71 |
void cleanupTestCase(); |
|
72 |
void init(); |
|
73 |
void cleanup(); |
|
74 |
||
75 |
private slots: |
|
76 |
void getItemPosition(); |
|
77 |
void itemAtPosition(); |
|
78 |
void badDistributionBug(); |
|
79 |
void setMinAndMaxSize(); |
|
80 |
void spacingAndSpacers(); |
|
81 |
||
82 |
void spacingsAndMargins(); |
|
83 |
void spacingsAndMargins_data(); |
|
84 |
void minMaxSize_data(); |
|
85 |
void minMaxSize(); |
|
86 |
||
87 |
void styleDependentSpacingsAndMargins_data(); |
|
88 |
void styleDependentSpacingsAndMargins(); |
|
89 |
void layoutSpacingImplementation_data(); |
|
90 |
void layoutSpacingImplementation(); |
|
91 |
void spacing(); |
|
92 |
void spacerWithSpacing(); |
|
93 |
void contentsRect(); |
|
94 |
void distributeMultiCell(); |
|
95 |
||
96 |
private: |
|
97 |
QWidget *testWidget; |
|
98 |
QGridLayout *testLayout; |
|
99 |
QWidget *w1; |
|
100 |
QWidget *w2; |
|
101 |
QWidget *w3; |
|
102 |
QSpacerItem *sp; |
|
103 |
||
104 |
QGridLayout *m_grid; |
|
105 |
QWidget *m_toplevel; |
|
106 |
}; |
|
107 |
||
108 |
||
109 |
tst_QGridLayout::tst_QGridLayout() |
|
110 |
{ |
|
111 |
m_grid = 0; |
|
112 |
m_toplevel = 0; |
|
113 |
} |
|
114 |
||
115 |
tst_QGridLayout::~tst_QGridLayout() |
|
116 |
{ |
|
117 |
delete m_toplevel; |
|
118 |
} |
|
119 |
||
120 |
void tst_QGridLayout::initTestCase() |
|
121 |
{ |
|
122 |
#ifdef Q_OS_WINCE //disable magic for WindowsCE |
|
123 |
qApp->setAutoMaximizeThreshold(-1); |
|
124 |
#endif |
|
125 |
// Create the test class |
|
126 |
testWidget = new QWidget(0); |
|
127 |
||
128 |
testLayout = new QGridLayout(testWidget); |
|
129 |
||
130 |
w1 = new QWidget(testWidget); |
|
131 |
w1->setPalette(QPalette(Qt::red)); |
|
132 |
testLayout->addWidget(w1, 0, 0); |
|
133 |
||
134 |
w2 = new QWidget(testWidget); |
|
135 |
testLayout->addWidget(w2, 1, 1, 2, 2); |
|
136 |
w2->setPalette(QPalette(Qt::green)); |
|
137 |
||
138 |
w3 = new QWidget(testWidget); |
|
139 |
testLayout->addWidget(w3, 0, 1, 1, 2); |
|
140 |
w3->setPalette(QPalette(Qt::blue)); |
|
141 |
||
142 |
sp = new QSpacerItem(4, 4); |
|
143 |
testLayout->addItem(sp, 1, 3, 2, 1); |
|
144 |
||
145 |
testWidget->resize( 200, 200 ); |
|
146 |
testWidget->show(); |
|
147 |
} |
|
148 |
||
149 |
void tst_QGridLayout::cleanupTestCase() |
|
150 |
{ |
|
151 |
delete testWidget; |
|
152 |
testWidget = 0; |
|
153 |
} |
|
154 |
||
155 |
void tst_QGridLayout::init() |
|
156 |
{ |
|
157 |
} |
|
158 |
||
159 |
void tst_QGridLayout::cleanup() |
|
160 |
{ |
|
161 |
} |
|
162 |
||
163 |
void tst_QGridLayout::getItemPosition() |
|
164 |
{ |
|
165 |
QLayoutItem *item; |
|
166 |
int counter = 0; |
|
167 |
||
168 |
bool seenW1 = false; |
|
169 |
bool seenW2 = false; |
|
170 |
bool seenW3 = false; |
|
171 |
bool seenSpacer = false; |
|
172 |
||
173 |
while ((item = testLayout->itemAt(counter))) { |
|
174 |
QWidget *w = item->widget(); |
|
175 |
int r,c,rs,cs; |
|
176 |
testLayout->getItemPosition(counter, &r, &c, &rs, &cs); |
|
177 |
||
178 |
// qDebug() << "item" << counter << "has" <<r << c << rs << cs; |
|
179 |
||
180 |
if (w == w1) { |
|
181 |
QVERIFY(!seenW1); |
|
182 |
seenW1 = true; |
|
183 |
QCOMPARE(r, 0); |
|
184 |
QCOMPARE(c, 0); |
|
185 |
QCOMPARE(rs, 1); |
|
186 |
QCOMPARE(cs, 1); |
|
187 |
} else if (w == w2) { |
|
188 |
QVERIFY(!seenW2); |
|
189 |
seenW2 = true; |
|
190 |
QCOMPARE(r, 1); |
|
191 |
QCOMPARE(c, 1); |
|
192 |
QCOMPARE(rs, 2); |
|
193 |
QCOMPARE(cs, 2); |
|
194 |
} else if (w == w3) { |
|
195 |
QVERIFY(!seenW3); |
|
196 |
seenW3 = true; |
|
197 |
QCOMPARE(r, 0); |
|
198 |
QCOMPARE(c, 1); |
|
199 |
QCOMPARE(rs, 1); |
|
200 |
QCOMPARE(cs, 2); |
|
201 |
} else { |
|
202 |
QVERIFY(!w); |
|
203 |
QVERIFY(!seenSpacer); |
|
204 |
seenSpacer = true; |
|
205 |
QCOMPARE(r, 1); |
|
206 |
QCOMPARE(c, 3); |
|
207 |
QCOMPARE(rs, 2); |
|
208 |
QCOMPARE(cs, 1); |
|
209 |
} |
|
210 |
++counter; |
|
211 |
} |
|
212 |
QCOMPARE(counter, 4); |
|
213 |
QVERIFY(seenW1); |
|
214 |
QVERIFY(seenW2); |
|
215 |
QVERIFY(seenW3); |
|
216 |
QVERIFY(seenSpacer); |
|
217 |
} |
|
218 |
||
219 |
void tst_QGridLayout::itemAtPosition() |
|
220 |
{ |
|
221 |
void *table[4][5] = { |
|
222 |
{ w1, w3, w3, 0, 0 }, |
|
223 |
{ 0, w2, w2, sp, 0 }, |
|
224 |
{ 0, w2, w2, sp, 0 }, |
|
225 |
{ 0, 0, 0, 0, 0 } |
|
226 |
}; |
|
227 |
||
228 |
for (int row = 0; row < 4; ++row) { |
|
229 |
for (int col = 0; col < 5; ++col) { |
|
230 |
QLayoutItem *item = testLayout->itemAtPosition(row, col); |
|
231 |
QVERIFY(item == table[row][col] |
|
232 |
|| (item && item->widget() == table[row][col])); |
|
233 |
} |
|
234 |
} |
|
235 |
} |
|
236 |
||
237 |
#include "ui_sortdialog.h" |
|
238 |
||
239 |
void tst_QGridLayout::badDistributionBug() |
|
240 |
{ |
|
241 |
QDialog dialog; |
|
242 |
Ui::SortDialog ui; |
|
243 |
ui.setupUi(&dialog); |
|
244 |
ui.gridLayout->setMargin(0); |
|
245 |
ui.gridLayout->setSpacing(0); |
|
246 |
ui.vboxLayout->setMargin(0); |
|
247 |
ui.vboxLayout->setSpacing(0); |
|
248 |
ui.okButton->setFixedHeight(20); |
|
249 |
ui.moreButton->setFixedHeight(20); |
|
250 |
ui.primaryGroupBox->setAttribute(Qt::WA_LayoutUsesWidgetRect); |
|
251 |
ui.primaryGroupBox->setFixedHeight(200); |
|
252 |
||
253 |
QSize minSize = dialog.layout()->minimumSize(); |
|
254 |
QCOMPARE(minSize.height(), 200); |
|
255 |
} |
|
256 |
||
257 |
void tst_QGridLayout::setMinAndMaxSize() |
|
258 |
{ |
|
259 |
QWidget widget; |
|
260 |
QGridLayout layout(&widget); |
|
261 |
layout.setMargin(0); |
|
262 |
layout.setSpacing(0); |
|
263 |
layout.setSizeConstraint(QLayout::SetMinAndMaxSize); |
|
264 |
widget.show(); |
|
265 |
||
266 |
QWidget leftChild; |
|
267 |
leftChild.setPalette(QPalette(Qt::red)); |
|
268 |
leftChild.setMinimumSize(100, 100); |
|
269 |
leftChild.setMaximumSize(200, 200); |
|
270 |
layout.addWidget(&leftChild, 0, 0); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
271 |
QApplication::sendPostedEvents(0, 0); |
0 | 272 |
QCOMPARE(widget.minimumSize(), leftChild.minimumSize()); |
273 |
QCOMPARE(widget.maximumSize(), leftChild.maximumSize()); |
|
274 |
||
275 |
QWidget rightChild; |
|
276 |
rightChild.setPalette(QPalette(Qt::green)); |
|
277 |
rightChild.setMinimumSize(100, 100); |
|
278 |
rightChild.setMaximumSize(200, 200); |
|
279 |
layout.addWidget(&rightChild, 0, 2); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
280 |
QApplication::sendPostedEvents(0, 0); |
0 | 281 |
|
282 |
QCOMPARE(widget.minimumWidth(), |
|
283 |
leftChild.minimumWidth() + rightChild.minimumWidth()); |
|
284 |
QCOMPARE(widget.minimumHeight(), |
|
285 |
qMax(leftChild.minimumHeight(), rightChild.minimumHeight())); |
|
286 |
QCOMPARE(widget.maximumWidth(), |
|
287 |
leftChild.maximumWidth() + rightChild.maximumWidth()); |
|
288 |
QCOMPARE(widget.maximumHeight(), |
|
289 |
qMax(leftChild.maximumHeight(), rightChild.maximumHeight())); |
|
290 |
||
291 |
||
292 |
static const int colMin = 100; |
|
293 |
layout.setColumnMinimumWidth(1, colMin); |
|
294 |
QCOMPARE(layout.columnMinimumWidth(1), colMin); |
|
295 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
296 |
QApplication::sendPostedEvents(0, 0); |
0 | 297 |
QCOMPARE(widget.minimumWidth(), |
298 |
leftChild.minimumWidth() + rightChild.minimumWidth() + colMin); |
|
299 |
QCOMPARE(widget.maximumWidth(), |
|
300 |
leftChild.maximumWidth() + rightChild.maximumWidth() + colMin); |
|
301 |
QCOMPARE(widget.minimumHeight(), |
|
302 |
qMax(leftChild.minimumHeight(), rightChild.minimumHeight())); |
|
303 |
QCOMPARE(widget.maximumHeight(), |
|
304 |
qMax(leftChild.maximumHeight(), rightChild.maximumHeight())); |
|
305 |
||
306 |
||
307 |
||
308 |
layout.setColumnStretch(1,1); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
309 |
QApplication::sendPostedEvents(0, 0); |
0 | 310 |
QCOMPARE(widget.minimumWidth(), |
311 |
leftChild.minimumWidth() + rightChild.minimumWidth() + colMin); |
|
312 |
QCOMPARE(widget.maximumWidth(), QLAYOUTSIZE_MAX); |
|
313 |
QCOMPARE(widget.minimumHeight(), |
|
314 |
qMax(leftChild.minimumHeight(), rightChild.minimumHeight())); |
|
315 |
QCOMPARE(widget.maximumHeight(), |
|
316 |
qMax(leftChild.maximumHeight(), rightChild.maximumHeight())); |
|
317 |
||
318 |
||
319 |
||
320 |
layout.setColumnStretch(1,0); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
321 |
QApplication::sendPostedEvents(0, 0); |
0 | 322 |
QCOMPARE(widget.minimumWidth(), |
323 |
leftChild.minimumWidth() + rightChild.minimumWidth() + colMin); |
|
324 |
QCOMPARE(widget.maximumWidth(), |
|
325 |
leftChild.maximumWidth() + rightChild.maximumWidth() + colMin); |
|
326 |
QCOMPARE(widget.minimumHeight(), |
|
327 |
qMax(leftChild.minimumHeight(), rightChild.minimumHeight())); |
|
328 |
QCOMPARE(widget.maximumHeight(), |
|
329 |
qMax(leftChild.maximumHeight(), rightChild.maximumHeight())); |
|
330 |
||
331 |
||
332 |
||
333 |
layout.setColumnMinimumWidth(1, 0); |
|
334 |
||
335 |
static const int spacerS = 250; |
|
336 |
QSpacerItem *spacer = new QSpacerItem(spacerS, spacerS); |
|
337 |
layout.addItem(spacer, 0, 1); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
338 |
QApplication::sendPostedEvents(0, 0); |
0 | 339 |
|
340 |
QCOMPARE(widget.minimumWidth(), |
|
341 |
leftChild.minimumWidth() + rightChild.minimumWidth() + spacerS); |
|
342 |
QCOMPARE(widget.maximumWidth(), QLAYOUTSIZE_MAX); |
|
343 |
QCOMPARE(widget.minimumHeight(), |
|
344 |
qMax(qMax(leftChild.minimumHeight(), rightChild.minimumHeight()), spacerS)); |
|
345 |
QCOMPARE(widget.maximumHeight(), |
|
346 |
qMax(leftChild.maximumHeight(), rightChild.maximumHeight())); |
|
347 |
||
348 |
||
349 |
spacer->changeSize(spacerS, spacerS, QSizePolicy::Fixed, QSizePolicy::Minimum); |
|
350 |
layout.invalidate(); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
351 |
QApplication::sendPostedEvents(0, 0); |
0 | 352 |
QCOMPARE(widget.minimumWidth(), |
353 |
leftChild.minimumWidth() + rightChild.minimumWidth() + spacerS); |
|
354 |
QCOMPARE(widget.maximumWidth(), |
|
355 |
leftChild.maximumWidth() + rightChild.maximumWidth() + spacerS); |
|
356 |
||
357 |
||
358 |
layout.removeItem(spacer); |
|
359 |
||
360 |
rightChild.hide(); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
361 |
QApplication::sendPostedEvents(0, 0); |
0 | 362 |
QCOMPARE(widget.minimumSize(), leftChild.minimumSize()); |
363 |
QCOMPARE(widget.maximumSize(), leftChild.maximumSize()); |
|
364 |
||
365 |
rightChild.show(); |
|
366 |
layout.removeWidget(&rightChild); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
367 |
QApplication::sendPostedEvents(0, 0); |
0 | 368 |
QCOMPARE(widget.minimumSize(), leftChild.minimumSize()); |
369 |
QCOMPARE(widget.maximumSize(), leftChild.maximumSize()); |
|
370 |
||
371 |
QWidget bottomChild(&widget); |
|
372 |
bottomChild.setPalette(QPalette(Qt::green)); |
|
373 |
bottomChild.setMinimumSize(100, 100); |
|
374 |
bottomChild.setMaximumSize(200, 200); |
|
375 |
layout.addWidget(&bottomChild, 1, 0); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
376 |
QApplication::sendPostedEvents(0, 0); |
0 | 377 |
|
378 |
QCOMPARE(widget.minimumHeight(), |
|
379 |
leftChild.minimumHeight() + bottomChild.minimumHeight()); |
|
380 |
QCOMPARE(widget.minimumWidth(), |
|
381 |
qMax(leftChild.minimumWidth(), bottomChild.minimumWidth())); |
|
382 |
QCOMPARE(widget.maximumHeight(), |
|
383 |
leftChild.maximumHeight() + bottomChild.maximumHeight()); |
|
384 |
QCOMPARE(widget.maximumWidth(), |
|
385 |
qMax(leftChild.maximumWidth(), bottomChild.maximumWidth())); |
|
386 |
||
387 |
bottomChild.hide(); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
388 |
QApplication::sendPostedEvents(0, 0); |
0 | 389 |
QCOMPARE(widget.minimumSize(), leftChild.minimumSize()); |
390 |
QCOMPARE(widget.maximumSize(), leftChild.maximumSize()); |
|
391 |
||
392 |
bottomChild.show(); |
|
393 |
layout.removeWidget(&bottomChild); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
394 |
QApplication::sendPostedEvents(0, 0); |
0 | 395 |
QCOMPARE(widget.minimumSize(), leftChild.minimumSize()); |
396 |
QCOMPARE(widget.maximumSize(), leftChild.maximumSize()); |
|
397 |
} |
|
398 |
||
399 |
||
400 |
class SizeHinter : public QWidget |
|
401 |
{ |
|
402 |
public: |
|
403 |
SizeHinter(const QSize &s, QWidget *parent = 0) |
|
404 |
: QWidget(parent), sh(s) { } |
|
405 |
SizeHinter(int w, int h, QWidget *parent = 0) |
|
406 |
: QWidget(parent), sh(QSize(w,h)) {} |
|
407 |
void setSizeHint(QSize s) { sh = s; } |
|
408 |
QSize sizeHint() const { return sh; } |
|
409 |
private: |
|
410 |
QSize sh; |
|
411 |
}; |
|
412 |
||
413 |
void tst_QGridLayout::spacingAndSpacers() |
|
414 |
{ |
|
415 |
QWidget widget; |
|
416 |
QGridLayout layout(&widget); |
|
417 |
layout.setMargin(0); |
|
418 |
layout.setSpacing(0); |
|
419 |
widget.show(); |
|
420 |
||
421 |
QSize expectedSizeHint; |
|
422 |
||
423 |
SizeHinter leftChild(100,100); |
|
424 |
leftChild.setPalette(QPalette(Qt::red)); |
|
425 |
layout.addWidget(&leftChild, 0, 0); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
426 |
QApplication::sendPostedEvents(0, 0); |
0 | 427 |
expectedSizeHint = leftChild.sizeHint(); |
428 |
QCOMPARE(widget.sizeHint(), expectedSizeHint); |
|
429 |
||
430 |
||
431 |
SizeHinter rightChild(200,100); |
|
432 |
rightChild.setPalette(QPalette(Qt::green)); |
|
433 |
layout.addWidget(&rightChild, 0, 2); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
434 |
QApplication::sendPostedEvents(0, 0); |
0 | 435 |
QCOMPARE(rightChild.sizeHint(), QSize(200,100)); |
436 |
||
437 |
expectedSizeHint += QSize(rightChild.sizeHint().width(), 0); |
|
438 |
QCOMPARE(widget.sizeHint(), expectedSizeHint); |
|
439 |
||
440 |
layout.setColumnMinimumWidth(1, 100); |
|
441 |
widget.adjustSize(); |
|
442 |
expectedSizeHint += QSize(100,0); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
443 |
QApplication::sendPostedEvents(0, 0); |
0 | 444 |
QCOMPARE(widget.sizeHint(), expectedSizeHint); |
445 |
||
446 |
rightChild.hide(); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
447 |
QApplication::sendPostedEvents(0, 0); |
0 | 448 |
expectedSizeHint -= QSize(rightChild.sizeHint().width(), 0); |
449 |
QCOMPARE(widget.sizeHint(), expectedSizeHint); |
|
450 |
||
451 |
||
452 |
layout.setColumnMinimumWidth(1, 0); |
|
453 |
expectedSizeHint -= QSize(100, 0); |
|
454 |
QCOMPARE(widget.sizeHint(), expectedSizeHint); |
|
455 |
||
456 |
rightChild.show(); |
|
457 |
||
458 |
#if 0 |
|
459 |
leftChild.setMaximumWidth(200); |
|
460 |
rightChild.setMaximumWidth(200); |
|
461 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
462 |
QApplication::sendPostedEvents(0, 0); |
0 | 463 |
QCOMPARE(widget.maximumWidth(), leftChild.maximumWidth() + rightChild.maximumWidth()); |
464 |
#endif |
|
465 |
||
466 |
layout.removeWidget(&rightChild); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
467 |
QApplication::sendPostedEvents(0, 0); |
0 | 468 |
QCOMPARE(widget.sizeHint(), expectedSizeHint); |
469 |
||
470 |
||
471 |
} |
|
472 |
||
473 |
||
474 |
class Qt42Style : public QWindowsStyle |
|
475 |
{ |
|
476 |
Q_OBJECT |
|
477 |
public: |
|
478 |
Qt42Style() : QWindowsStyle() |
|
479 |
{ |
|
480 |
spacing = 6; |
|
481 |
margin = 9; |
|
482 |
margin_toplevel = 11; |
|
483 |
} |
|
484 |
||
485 |
virtual int pixelMetric(PixelMetric metric, const QStyleOption * option = 0, |
|
486 |
const QWidget * widget = 0 ) const; |
|
487 |
||
488 |
int spacing; |
|
489 |
int margin; |
|
490 |
int margin_toplevel; |
|
491 |
||
492 |
}; |
|
493 |
||
494 |
int Qt42Style::pixelMetric(PixelMetric metric, const QStyleOption * option /*= 0*/, |
|
495 |
const QWidget * widget /*= 0*/ ) const |
|
496 |
{ |
|
497 |
switch (metric) { |
|
498 |
case PM_DefaultLayoutSpacing: |
|
499 |
return spacing; |
|
500 |
break; |
|
501 |
case PM_DefaultTopLevelMargin: |
|
502 |
return margin_toplevel; |
|
503 |
break; |
|
504 |
case PM_DefaultChildMargin: |
|
505 |
return margin; |
|
506 |
break; |
|
507 |
default: |
|
508 |
break; |
|
509 |
} |
|
510 |
return QWindowsStyle::pixelMetric(metric, option, widget); |
|
511 |
} |
|
512 |
||
513 |
||
514 |
typedef QList<QPoint> PointList; |
|
515 |
Q_DECLARE_METATYPE(PointList) |
|
516 |
||
517 |
||
518 |
class SizeHinterFrame : public QLabel |
|
519 |
{ |
|
520 |
public: |
|
521 |
SizeHinterFrame(QWidget *parent = 0) |
|
522 |
: QLabel(parent) |
|
523 |
{ |
|
524 |
init(-1); |
|
525 |
} |
|
526 |
||
527 |
SizeHinterFrame(const QSize &s, int numPixels = -1) |
|
528 |
: QLabel(0), sh(s) { |
|
529 |
init(numPixels); |
|
530 |
} |
|
531 |
||
532 |
||
533 |
SizeHinterFrame(int w, int h) |
|
534 |
: QLabel(0), sh(QSize(w,h)) |
|
535 |
{ |
|
536 |
init(-1); |
|
537 |
} |
|
538 |
||
539 |
void setSizeHint(const QSize &s) { sh = s; } |
|
540 |
QSize sizeHint() const { return sh; } |
|
541 |
void setMinimumSizeHint(const QSize &s) { msh = s; } |
|
542 |
QSize minimumSizeHint() const { return msh; } |
|
543 |
||
544 |
virtual int heightForWidth(int width) const; |
|
545 |
||
546 |
void setNumberOfPixels(int numPixels) { |
|
547 |
m_numPixels = numPixels; |
|
548 |
QSizePolicy sp = sizePolicy(); |
|
549 |
sp.setHeightForWidth(m_numPixels != -1); |
|
550 |
setSizePolicy(sp); |
|
551 |
} |
|
552 |
private: |
|
553 |
void init(int numPixels = -1){ |
|
554 |
setText(QString::fromAscii("(%1,%2)").arg(sh.width()).arg(sh.height())); |
|
555 |
setFrameStyle(QFrame::Box | QFrame::Plain); |
|
556 |
setNumberOfPixels(numPixels); |
|
557 |
} |
|
558 |
private: |
|
559 |
QSize sh; |
|
560 |
QSize msh; |
|
561 |
int m_numPixels; |
|
562 |
}; |
|
563 |
||
564 |
int SizeHinterFrame::heightForWidth(int width) const |
|
565 |
{ |
|
566 |
// Special hack if m_numPixels == -2 then we report that we are heightForWidth aware, but we |
|
567 |
// return sizeHint().width() so that it should be laid out as if we had't any hfw. |
|
568 |
// This enables us to run the tests twice and to see if we get the same results with hfw as without hfw. |
|
569 |
if (m_numPixels == -2) { |
|
570 |
return sizeHint().height(); |
|
571 |
} |
|
572 |
if (m_numPixels == -1 || width == 0) return -1; |
|
573 |
// this widget should always cover the same amount of pixels (provided that we don't get any rounding errors) |
|
574 |
return (m_numPixels)/width; |
|
575 |
} |
|
576 |
||
577 |
void tst_QGridLayout::spacingsAndMargins_data() |
|
578 |
{ |
|
579 |
// input |
|
580 |
QTest::addColumn<int>("columns"); |
|
581 |
QTest::addColumn<int>("rows"); |
|
582 |
QTest::addColumn<QSize>("sizehint"); |
|
583 |
// expected |
|
584 |
QTest::addColumn<PointList>("expectedpositions"); |
|
585 |
||
586 |
int child_offset_y = 11 + 100 + 6 + 9 ; |
|
587 |
QTest::newRow("1x1 grid") << 1 << 1 << QSize(100, 100) |
|
588 |
<< (PointList() // toplevel |
|
589 |
<< QPoint( 11, 11) |
|
590 |
// children |
|
591 |
<< QPoint( 20, child_offset_y) |
|
592 |
); |
|
593 |
||
594 |
QTest::newRow("2x1 grid") << 2 << 1 << QSize(100, 100) |
|
595 |
<< (PointList() // toplevel |
|
596 |
<< QPoint( 11, 11) |
|
597 |
<< QPoint( 11 + 100 + 6, 11) |
|
598 |
// children |
|
599 |
<< QPoint( 20, child_offset_y) |
|
600 |
<< QPoint( 20 + 100 + 6, child_offset_y) |
|
601 |
); |
|
602 |
||
603 |
QTest::newRow("3x1 grid") << 3 << 1 << QSize(100, 100) |
|
604 |
<< (PointList() // toplevel |
|
605 |
<< QPoint( 11, 11) |
|
606 |
<< QPoint( 11 + 100 + 6, 11) |
|
607 |
<< QPoint( 11 + 100 + 6 + 100 + 6, 11) |
|
608 |
// children |
|
609 |
<< QPoint( 20, child_offset_y) |
|
610 |
<< QPoint( 20 + 100 + 6, child_offset_y) |
|
611 |
<< QPoint( 20 + 100 + 6 + 100 + 6, child_offset_y) |
|
612 |
); |
|
613 |
||
614 |
child_offset_y = 11 + 9 + 100 + 6 + 100 + 6; |
|
615 |
QTest::newRow("1x2 grid") << 1 << 2 << QSize(100, 100) |
|
616 |
<< (PointList() // toplevel |
|
617 |
<< QPoint( 11, 11) |
|
618 |
<< QPoint( 11, 11 + 100 + 6) |
|
619 |
// children |
|
620 |
<< QPoint( 20, child_offset_y) |
|
621 |
<< QPoint( 20, child_offset_y + 100 + 6) |
|
622 |
); |
|
623 |
#if defined (Q_OS_WINCE) //There is not enough screenspace to run the test in original size on Windows CE. We use smaller widgets. |
|
624 |
child_offset_y = 11 + 9 + 50 + 6 + 50 + 6 + 50 + 6; |
|
625 |
QTest::newRow("1x3 grid") << 1 << 3 << QSize(50, 50) |
|
626 |
<< (PointList() // toplevel |
|
627 |
<< QPoint( 11, 11) |
|
628 |
<< QPoint( 11, 11 + 50 + 6) |
|
629 |
<< QPoint( 11, 11 + 50 + 6 + 50 + 6) |
|
630 |
// children |
|
631 |
<< QPoint( 20, child_offset_y) |
|
632 |
<< QPoint( 20, child_offset_y + 50 + 6) |
|
633 |
<< QPoint( 20, child_offset_y + 50 + 6 + 50 + 6) |
|
634 |
); |
|
635 |
#else |
|
636 |
child_offset_y = 11 + 9 + 100 + 6 + 100 + 6 + 100 + 6; |
|
637 |
QTest::newRow("1x3 grid") << 1 << 3 << QSize(100, 100) |
|
638 |
<< (PointList() // toplevel |
|
639 |
<< QPoint( 11, 11) |
|
640 |
<< QPoint( 11, 11 + 100 + 6) |
|
641 |
<< QPoint( 11, 11 + 100 + 6 + 100 + 6) |
|
642 |
// children |
|
643 |
<< QPoint( 20, child_offset_y) |
|
644 |
<< QPoint( 20, child_offset_y + 100 + 6) |
|
645 |
<< QPoint( 20, child_offset_y + 100 + 6 + 100 + 6) |
|
646 |
); |
|
647 |
#endif |
|
648 |
||
649 |
child_offset_y = 11 + 9 + 100 + 6 + 100 + 6; |
|
650 |
QTest::newRow("2x2 grid") << 2 << 2 << QSize(100, 100) |
|
651 |
<< (PointList() // toplevel |
|
652 |
<< QPoint( 11, 11) |
|
653 |
<< QPoint( 11 + 100 + 6, 11) |
|
654 |
<< QPoint( 11, 11 + 100 + 6) |
|
655 |
<< QPoint( 11 + 100 + 6, 11 + 100 + 6) |
|
656 |
// children |
|
657 |
<< QPoint( 20, child_offset_y) |
|
658 |
<< QPoint( 20 + 100 + 6, child_offset_y) |
|
659 |
<< QPoint( 20, child_offset_y + 100 + 6) |
|
660 |
<< QPoint( 20 + 100 + 6, child_offset_y + 100 + 6) |
|
661 |
); |
|
662 |
} |
|
663 |
||
664 |
void tst_QGridLayout::spacingsAndMargins() |
|
665 |
{ |
|
666 |
/* |
|
667 |
The test tests a gridlayout as a child of a top-level widget, |
|
668 |
and then a gridlayout as a child of a non-toplevel widget. |
|
669 |
||
670 |
The expectedpositions should then contain the list of widget positions in the |
|
671 |
first gridlayout, then followed by a list of widget positions in the second gridlayout. |
|
672 |
*/ |
|
673 |
QFETCH(int, columns); |
|
674 |
QFETCH(int, rows); |
|
675 |
QFETCH(QSize, sizehint); |
|
676 |
QFETCH(PointList, expectedpositions); |
|
677 |
||
678 |
||
679 |
QApplication::setStyle(new Qt42Style); |
|
680 |
QWidget toplevel; |
|
681 |
QVBoxLayout vbox(&toplevel); |
|
682 |
QGridLayout grid1; |
|
683 |
vbox.addLayout(&grid1); |
|
684 |
||
685 |
// a layout with a top-level parent widget |
|
686 |
QList<QPointer<SizeHinterFrame> > sizehinters; |
|
687 |
for (int i = 0; i < rows; ++i) { |
|
688 |
for (int j = 0; j < columns; ++j) { |
|
689 |
SizeHinterFrame *sh = new SizeHinterFrame(sizehint); |
|
690 |
sh->setMinimumSizeHint(sizehint); |
|
691 |
sizehinters.append(sh); |
|
692 |
grid1.addWidget(sh, i, j); |
|
693 |
} |
|
694 |
} |
|
695 |
||
696 |
// Add the child widget |
|
697 |
QWidget widget; |
|
698 |
vbox.addWidget(&widget); |
|
699 |
QGridLayout grid2; |
|
700 |
widget.setLayout(&grid2); |
|
701 |
// add a layout to the child widget |
|
702 |
for (int i = 0; i < rows; ++i) { |
|
703 |
for (int j = 0; j < columns; ++j) { |
|
704 |
SizeHinterFrame *sh = new SizeHinterFrame(sizehint); |
|
705 |
sh->setMinimumSizeHint(sizehint); |
|
706 |
sizehinters.append(sh); |
|
707 |
grid2.addWidget(sh, i, j); |
|
708 |
} |
|
709 |
} |
|
710 |
||
711 |
grid1.setColumnStretch(columns-1, 1); |
|
712 |
grid1.setRowStretch(rows-1, 1); |
|
713 |
toplevel.show(); |
|
714 |
toplevel.adjustSize(); |
|
715 |
QApplication::processEvents(); |
|
716 |
||
717 |
QSize topsize = toplevel.size(); |
|
718 |
QSize minimumsize = vbox.totalMinimumSize(); |
|
719 |
||
720 |
#ifdef Q_WS_QWS |
|
721 |
if (topsize.width() < minimumsize.width() || topsize.height() < minimumsize.height()) |
|
722 |
QSKIP("The screen is too small to run this test case", SkipSingle); |
|
723 |
#endif |
|
724 |
||
725 |
// We are relying on the order here... |
|
726 |
for (int pi = 0; pi < sizehinters.count(); ++pi) { |
|
727 |
QPoint pt = sizehinters.at(pi)->mapTo(&toplevel, QPoint(0, 0)); |
|
728 |
QCOMPARE(pt, expectedpositions.at(pi)); |
|
729 |
} |
|
730 |
} |
|
731 |
||
732 |
||
733 |
||
734 |
||
735 |
struct SizeInfo { |
|
736 |
SizeInfo(const QPoint &expected, const QSize &sh, const QSize &minimumSize = QSize(), |
|
737 |
const QSize &maximumSize = QSize(), int numPixelsToCover = -1) |
|
738 |
{ |
|
739 |
expectedPos = expected; |
|
740 |
sizeHint = sh; |
|
741 |
minSize = minimumSize; |
|
742 |
maxSize = maximumSize; |
|
743 |
hfwNumPixels = numPixelsToCover; |
|
744 |
} |
|
745 |
||
746 |
SizeInfo(const QRect &expected, const QSize &sh, const QSize &minimumSize = QSize(), |
|
747 |
const QSize &maximumSize = QSize(), int numPixelsToCover = -1) |
|
748 |
{ |
|
749 |
expectedPos = expected.topLeft(); |
|
750 |
expectedSize = expected.size(); |
|
751 |
sizeHint = sh; |
|
752 |
minSize = minimumSize; |
|
753 |
maxSize = maximumSize; |
|
754 |
hfwNumPixels = numPixelsToCover; |
|
755 |
} |
|
756 |
SizeInfo(const SizeInfo& other) { |
|
757 |
(*this)=other; |
|
758 |
} |
|
759 |
||
760 |
SizeInfo &operator=(const SizeInfo& other) { |
|
761 |
expectedPos = other.expectedPos; |
|
762 |
expectedSize = other.expectedSize; |
|
763 |
sizeHint = other.sizeHint; |
|
764 |
minSize = other.minSize; |
|
765 |
maxSize = other.maxSize; |
|
766 |
hfwNumPixels = other.hfwNumPixels; |
|
767 |
return (*this); |
|
768 |
} |
|
769 |
||
770 |
QPoint expectedPos; |
|
771 |
QSize expectedSize; |
|
772 |
QSize sizeHint; |
|
773 |
QSize minSize; |
|
774 |
QSize maxSize; |
|
775 |
int hfwNumPixels; |
|
776 |
}; |
|
777 |
||
778 |
||
779 |
typedef QList<SizeInfo> SizeInfoList; |
|
780 |
Q_DECLARE_METATYPE(SizeInfoList) |
|
781 |
||
782 |
||
783 |
void tst_QGridLayout::minMaxSize_data() |
|
784 |
{ |
|
785 |
// input |
|
786 |
QTest::addColumn<QString>("stylename"); |
|
787 |
QTest::addColumn<int>("columns"); |
|
788 |
QTest::addColumn<int>("rows"); |
|
789 |
QTest::addColumn<int>("sizePolicy"); |
|
790 |
QTest::addColumn<QSize>("fixedSize"); |
|
791 |
//input and expected output |
|
792 |
QTest::addColumn<SizeInfoList>("sizeinfos"); |
|
793 |
||
794 |
QTest::newRow("3x1 grid, extend to minimumSize") << QString() << 3 << 1 |
|
795 |
<< int(QSizePolicy::Minimum) << QSize(152, 50) << (SizeInfoList() |
|
796 |
<< SizeInfo(QRect(10, 10, 43, 30), QSize( 75, 75), QSize(0,0)) |
|
797 |
<< SizeInfo(QRect(10 + 45, 10, 43, 30), QSize(75, 75), QSize( 0, 0)) |
|
798 |
<< SizeInfo(QRect(10 + 45 + 44, 10, 42, 30), QSize(75, 75), QSize( 0, 0)) |
|
799 |
); |
|
800 |
||
801 |
QTest::newRow("1x1 grid, extend to minimumSize") << QString() << 1 << 1 |
|
802 |
<< int(QSizePolicy::Preferred) << QSize() << (SizeInfoList() |
|
803 |
<< SizeInfo(QPoint(10, 10), QSize( 90, 90), QSize(100,100)) |
|
804 |
); |
|
805 |
QTest::newRow("2x1 grid, extend to minimumSize") << QString() << 2 << 1 |
|
806 |
<< int(QSizePolicy::Preferred) << QSize() << (SizeInfoList() |
|
807 |
<< SizeInfo(QPoint(10, 10), QSize( 90, 90), QSize(100,100)) |
|
808 |
<< SizeInfo(QPoint(10 + 100 + 1, 10), QSize( 90, 90)) |
|
809 |
); |
|
810 |
QTest::newRow("1x2 grid, extend to minimumSize") << QString() << 1 << 2 |
|
811 |
<< int(QSizePolicy::Preferred) << QSize() << (SizeInfoList() |
|
812 |
<< SizeInfo(QPoint(10, 10), QSize( 90, 90), QSize(100,100)) |
|
813 |
<< SizeInfo(QPoint(10, 10 + 100 + 1), QSize( 90, 90)) |
|
814 |
); |
|
815 |
QTest::newRow("2x1 grid, crop to maximumSize") << QString() << 2 << 1 |
|
816 |
<< int(QSizePolicy::Preferred) << QSize() << (SizeInfoList() |
|
817 |
<< SizeInfo(QPoint(10, 10), QSize(110,110), QSize(), QSize(100, 100)) |
|
818 |
<< SizeInfo(QPoint(10 + 100 + 1, 10), QSize( 90, 90)) |
|
819 |
); |
|
820 |
QTest::newRow("1x2 grid, crop to maximumSize") << QString() << 1 << 2 |
|
821 |
<< int(QSizePolicy::Preferred) << QSize() << (SizeInfoList() |
|
822 |
<< SizeInfo(QPoint(10, 10), QSize(110,110), QSize(), QSize(100, 100)) |
|
823 |
<< SizeInfo(QPoint(10, 10 + 100 + 1), QSize( 90, 90)) |
|
824 |
); |
|
825 |
QTest::newRow("1x3 grid, heightForWidth") << QString() << 1 << 3 |
|
826 |
<< int(QSizePolicy::Preferred) << QSize() << (SizeInfoList() |
|
827 |
<< SizeInfo(QPoint(10, 10), QSize(), QSize(200,100), QSize()) |
|
828 |
<< SizeInfo(QPoint(10, 10 + 100 + 1), QSize(100,100), QSize(), QSize(), 100*100) |
|
829 |
<< SizeInfo(QPoint(10, 10 + 100 + 1 + 50 + 1), QSize(100,100), QSize(), QSize(100, 100)) |
|
830 |
); |
|
831 |
QTest::newRow("2x1 grid, extend to minimumSize") << QString::fromAscii("motif") << 2 << 1 |
|
832 |
<< int(QSizePolicy::Preferred) << QSize() << (SizeInfoList() |
|
833 |
<< SizeInfo(QPoint(11, 11), QSize( 90, 90), QSize(100,100)) |
|
834 |
<< SizeInfo(QPoint(11 + 100 + 6, 11), QSize( 90, 90)) |
|
835 |
); |
|
836 |
QTest::newRow("2x1 grid, extend to minimumSize") << QString::fromAscii("windows") << 2 << 1 |
|
837 |
<< int(QSizePolicy::Preferred) << QSize() << (SizeInfoList() |
|
838 |
<< SizeInfo(QPoint(11, 11), QSize( 90, 90), QSize(100,100)) |
|
839 |
<< SizeInfo(QPoint(11 + 100 + 6, 11), QSize( 90, 90)) |
|
840 |
); |
|
841 |
||
842 |
} |
|
843 |
||
844 |
void tst_QGridLayout::minMaxSize() |
|
845 |
{ |
|
846 |
/* |
|
847 |
The test tests a gridlayout as a child of a top-level widget |
|
848 |
*/ |
|
849 |
// input |
|
850 |
QFETCH(QString, stylename); |
|
851 |
QFETCH(int, columns); |
|
852 |
QFETCH(int, rows); |
|
853 |
QFETCH(int, sizePolicy); |
|
854 |
QFETCH(QSize, fixedSize); |
|
855 |
//input and expected output |
|
856 |
QFETCH(SizeInfoList, sizeinfos); |
|
857 |
||
858 |
QStyle *style = 0; |
|
859 |
if (stylename.isEmpty()) { |
|
860 |
Qt42Style *s = new Qt42Style; |
|
861 |
s->margin_toplevel = 10; |
|
862 |
s->margin = 5; |
|
863 |
s->spacing = 1; |
|
864 |
style = static_cast<QStyle *>(s); |
|
865 |
}else{ |
|
866 |
style = QStyleFactory::create(stylename); |
|
867 |
if (!style) { |
|
868 |
QSKIP( qPrintable(QString::fromLatin1("Qt has been compiled without style: %1").arg(stylename)), SkipSingle); |
|
869 |
} |
|
870 |
} |
|
871 |
QApplication::setStyle(style); |
|
872 |
if (!m_grid) |
|
873 |
m_grid = new QGridLayout(); |
|
874 |
if (!m_toplevel) |
|
875 |
m_toplevel = new QWidget(); |
|
876 |
if (fixedSize.isValid()) { |
|
877 |
m_toplevel->setFixedSize(fixedSize); |
|
878 |
} else { |
|
879 |
m_toplevel->setMinimumSize(QSize(0,0)); |
|
880 |
m_toplevel->setMaximumSize(QSize(QWIDGETSIZE_MAX,QWIDGETSIZE_MAX)); |
|
881 |
} |
|
882 |
// Do a two-pass one using the real testdata, the other pass enables heightForWidth |
|
883 |
// on the widget, but the heightForWidth() function just return sizeHint().width() |
|
884 |
for (int pass = 0; pass < 2; ++pass) { |
|
885 |
m_toplevel->hide(); |
|
886 |
QApplication::processEvents(); |
|
887 |
QTest::qWait(20); |
|
888 |
// Test if removeItem uninitializes data properly |
|
889 |
while (m_grid->count()) { |
|
890 |
QLayoutItem *item = m_grid->itemAt(0); |
|
891 |
m_grid->removeItem(item); |
|
892 |
delete item->widget(); |
|
893 |
delete item; |
|
894 |
} |
|
895 |
m_toplevel->setLayout(m_grid); |
|
896 |
||
897 |
// a layout with a top-level parent widget |
|
898 |
QList<QPointer<SizeHinterFrame> > sizehinters; |
|
899 |
for (int i = 0; i < rows; ++i) { |
|
900 |
for (int j = 0; j < columns; ++j) { |
|
901 |
SizeInfo si = sizeinfos.at(sizehinters.count()); |
|
902 |
int numpixels = si.hfwNumPixels; |
|
903 |
if (pass == 1 && numpixels == -1) |
|
904 |
numpixels = -2; //### yuk, (and don't fake it if it already tests sizehint) |
|
905 |
SizeHinterFrame *sh = new SizeHinterFrame(si.sizeHint, numpixels); |
|
906 |
QSizePolicy sp = sh->sizePolicy(); |
|
907 |
sp.setHorizontalPolicy((QSizePolicy::Policy)sizePolicy); |
|
908 |
sh->setSizePolicy(sp); |
|
909 |
sh->setParent(m_toplevel); |
|
910 |
if (si.minSize.isValid()) |
|
911 |
sh->setMinimumSize(si.minSize); |
|
912 |
if (si.maxSize.isValid()) |
|
913 |
sh->setMaximumSize(si.maxSize); |
|
914 |
sizehinters.append(sh); |
|
915 |
m_grid->addWidget(sh, i, j); |
|
916 |
} |
|
917 |
} |
|
918 |
||
919 |
m_toplevel->show(); |
|
920 |
#if defined(Q_WS_X11) |
|
921 |
qt_x11_wait_for_window_manager(m_toplevel); // wait for the show |
|
922 |
#endif |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
923 |
QTest::qWait(40); |
0 | 924 |
m_toplevel->adjustSize(); |
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
925 |
QTest::qWait(240); // wait for the implicit adjustSize |
0 | 926 |
// If the following fails we might have to wait longer. |
927 |
// If that does not help there is likely a problem with the implicit adjustSize in show() |
|
928 |
if (!fixedSize.isValid()) { |
|
929 |
// Note that this can fail if the desktop has large fonts on windows. |
|
930 |
QTRY_COMPARE(m_toplevel->size(), m_toplevel->sizeHint()); |
|
931 |
} |
|
932 |
// We are relying on the order here... |
|
933 |
for (int pi = 0; pi < sizehinters.count(); ++pi) { |
|
934 |
QPoint pt = sizehinters.at(pi)->mapTo(m_toplevel, QPoint(0, 0)); |
|
935 |
QCOMPARE(pt, sizeinfos.at(pi).expectedPos); |
|
936 |
} |
|
937 |
} |
|
938 |
} |
|
939 |
||
940 |
||
941 |
class CustomLayoutStyle : public QWindowsStyle |
|
942 |
{ |
|
943 |
Q_OBJECT |
|
944 |
public: |
|
945 |
CustomLayoutStyle() : QWindowsStyle() |
|
946 |
{ |
|
947 |
hspacing = 5; |
|
948 |
vspacing = 10; |
|
949 |
reimplementSubelementRect = false; |
|
950 |
} |
|
951 |
||
952 |
virtual int pixelMetric(PixelMetric metric, const QStyleOption * option = 0, |
|
953 |
const QWidget * widget = 0 ) const; |
|
954 |
virtual QRect subElementRect(SubElement sr, const QStyleOption *opt, |
|
955 |
const QWidget *widget) const; |
|
956 |
||
957 |
int hspacing; |
|
958 |
int vspacing; |
|
959 |
bool reimplementSubelementRect; |
|
960 |
||
961 |
protected slots: |
|
962 |
int layoutSpacingImplementation(QSizePolicy::ControlType control1, |
|
963 |
QSizePolicy::ControlType control2, |
|
964 |
Qt::Orientation orientation, |
|
965 |
const QStyleOption *option = 0, |
|
966 |
const QWidget *widget = 0) const; |
|
967 |
||
968 |
}; |
|
969 |
||
970 |
QRect CustomLayoutStyle::subElementRect(SubElement sr, const QStyleOption *opt, |
|
971 |
const QWidget *widget) const |
|
972 |
{ |
|
973 |
QRect rect; |
|
974 |
if (reimplementSubelementRect) { |
|
975 |
switch (sr) { |
|
976 |
case SE_FrameLayoutItem: |
|
977 |
rect = opt->rect; |
|
978 |
rect.adjust(+4, +9, -4, 0); // The hspacing=5 and vspacing=10, so we keep it safe. |
|
979 |
break; |
|
980 |
case SE_GroupBoxLayoutItem: |
|
981 |
rect = opt->rect.adjusted(0, +10, 0, 0); |
|
982 |
break; |
|
983 |
default: |
|
984 |
break; |
|
985 |
} |
|
986 |
} |
|
987 |
if (rect.isNull()) |
|
988 |
rect = QWindowsStyle::subElementRect(sr, opt, widget); |
|
989 |
return rect; |
|
990 |
} |
|
991 |
||
992 |
#define CT1(c) CT2(c, c) |
|
993 |
#define CT2(c1, c2) ((uint)c1 << 16) | (uint)c2 |
|
994 |
||
995 |
int CustomLayoutStyle::layoutSpacingImplementation(QSizePolicy::ControlType control1, |
|
996 |
QSizePolicy::ControlType control2, |
|
997 |
Qt::Orientation orientation, |
|
998 |
const QStyleOption * /*option = 0*/, |
|
999 |
const QWidget * /*widget = 0*/) const |
|
1000 |
{ |
|
1001 |
if (orientation == Qt::Horizontal) { |
|
1002 |
switch (CT2(control1, control2)) { |
|
1003 |
case CT1(QSizePolicy::PushButton): |
|
1004 |
return 2; |
|
1005 |
break; |
|
1006 |
} |
|
1007 |
return 5; |
|
1008 |
} else { |
|
1009 |
switch (CT2(control1, control2)) { |
|
1010 |
case CT1(QSizePolicy::RadioButton): |
|
1011 |
return 2; |
|
1012 |
break; |
|
1013 |
||
1014 |
} |
|
1015 |
return 10; |
|
1016 |
} |
|
1017 |
} |
|
1018 |
||
1019 |
int CustomLayoutStyle::pixelMetric(PixelMetric metric, const QStyleOption * option /*= 0*/, |
|
1020 |
const QWidget * widget /*= 0*/ ) const |
|
1021 |
{ |
|
1022 |
switch (metric) { |
|
1023 |
case PM_LayoutLeftMargin: |
|
1024 |
return 0; |
|
1025 |
break; |
|
1026 |
case PM_LayoutTopMargin: |
|
1027 |
return 3; |
|
1028 |
break; |
|
1029 |
case PM_LayoutRightMargin: |
|
1030 |
return 6; |
|
1031 |
break; |
|
1032 |
case PM_LayoutBottomMargin: |
|
1033 |
return 9; |
|
1034 |
break; |
|
1035 |
case PM_LayoutHorizontalSpacing: |
|
1036 |
return hspacing; |
|
1037 |
case PM_LayoutVerticalSpacing: |
|
1038 |
return vspacing; |
|
1039 |
break; |
|
1040 |
default: |
|
1041 |
break; |
|
1042 |
} |
|
1043 |
return QWindowsStyle::pixelMetric(metric, option, widget); |
|
1044 |
} |
|
1045 |
||
1046 |
void tst_QGridLayout::styleDependentSpacingsAndMargins_data() |
|
1047 |
{ |
|
1048 |
// input |
|
1049 |
QTest::addColumn<int>("columns"); |
|
1050 |
QTest::addColumn<int>("rows"); |
|
1051 |
QTest::addColumn<QSize>("sizehint"); |
|
1052 |
// expected |
|
1053 |
QTest::addColumn<PointList>("expectedpositions"); |
|
1054 |
||
1055 |
QTest::newRow("1x1 grid") << 1 << 1 << QSize(100, 100) |
|
1056 |
<< (PointList() << QPoint(0, 3) ); |
|
1057 |
QTest::newRow("2x1 grid") << 2 << 1 << QSize(100, 100) |
|
1058 |
<< (PointList() << QPoint(0, 3) |
|
1059 |
<< QPoint(0+100+5, 3)); |
|
1060 |
QTest::newRow("3x1 grid") << 3 << 1 << QSize(100, 100) |
|
1061 |
<< (PointList() << QPoint(0, 3) |
|
1062 |
<< QPoint(0+100+5, 3) |
|
1063 |
<< QPoint(0 + 2*105, 3)); |
|
1064 |
QTest::newRow("1x2 grid") << 1 << 2 << QSize(100, 100) |
|
1065 |
<< (PointList() << QPoint(0, 3) |
|
1066 |
<< QPoint(0, 3+100+10)); |
|
1067 |
QTest::newRow("1x3 grid") << 1 << 3 << QSize(100, 100) |
|
1068 |
<< (PointList() << QPoint(0, 3) |
|
1069 |
<< QPoint(0, 3+100+10) |
|
1070 |
<< QPoint(0, 3+2*110)); |
|
1071 |
QTest::newRow("2x2 grid") << 2 << 2 << QSize(100, 100) |
|
1072 |
<< (PointList() << QPoint(0, 3) << QPoint(0+100+5, 3) |
|
1073 |
<< QPoint(0, 3+100+10) << QPoint(0+100+5, 3+100+10)); |
|
1074 |
} |
|
1075 |
||
1076 |
||
1077 |
void tst_QGridLayout::styleDependentSpacingsAndMargins() |
|
1078 |
{ |
|
1079 |
QFETCH(int, columns); |
|
1080 |
QFETCH(int, rows); |
|
1081 |
QFETCH(QSize, sizehint); |
|
1082 |
QFETCH(PointList, expectedpositions); |
|
1083 |
||
1084 |
QApplication::setStyle(new CustomLayoutStyle()); |
|
1085 |
QWidget widget; |
|
1086 |
QGridLayout layout(&widget); |
|
1087 |
QList<QPointer<SizeHinterFrame> > sizehinters; |
|
1088 |
for (int i = 0; i < rows; ++i) { |
|
1089 |
for (int j = 0; j < columns; ++j) { |
|
1090 |
SizeHinterFrame *sh = new SizeHinterFrame(sizehint); |
|
1091 |
sh->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed)); |
|
1092 |
sh->setParent(&widget); |
|
1093 |
sizehinters.append(sh); |
|
1094 |
layout.addWidget(sh, i, j); |
|
1095 |
} |
|
1096 |
} |
|
1097 |
layout.setColumnStretch(columns, 1); |
|
1098 |
layout.setRowStretch(rows, 1); |
|
1099 |
widget.show(); |
|
1100 |
widget.adjustSize(); |
|
1101 |
QApplication::processEvents(); |
|
1102 |
||
1103 |
for (int pi = 0; pi < expectedpositions.count(); ++pi) { |
|
1104 |
QCOMPARE(sizehinters.at(pi)->pos(), expectedpositions.at(pi)); |
|
1105 |
} |
|
1106 |
} |
|
1107 |
||
1108 |
void tst_QGridLayout::layoutSpacingImplementation_data() |
|
1109 |
{ |
|
1110 |
QTest::addColumn<QWidget*>("widget"); |
|
1111 |
// expected |
|
1112 |
QTest::addColumn<PointList>("expectedpositions"); |
|
1113 |
QTest::addColumn<int>("hSpacing"); |
|
1114 |
QTest::addColumn<int>("vSpacing"); |
|
1115 |
QTest::addColumn<bool>("customSubElementRect"); |
|
1116 |
||
1117 |
CustomLayoutStyle *style = new CustomLayoutStyle(); |
|
1118 |
{ |
|
1119 |
// If the layoutSpacing is negative, the layouting code will call |
|
1120 |
// layoutSpacingImplementation() |
|
1121 |
style->hspacing = -1; |
|
1122 |
style->vspacing = -1; |
|
1123 |
style->reimplementSubelementRect = false; |
|
1124 |
QApplication::setStyle(style); |
|
1125 |
QWidget *w = new QWidget(); |
|
1126 |
QVBoxLayout *layout = new QVBoxLayout(); |
|
1127 |
QRadioButton *rb1 = new QRadioButton(QLatin1String("Radio 1"), w); |
|
1128 |
QRadioButton *rb2 = new QRadioButton(QLatin1String("Radio 2"), w); |
|
1129 |
QRadioButton *rb3 = new QRadioButton(QLatin1String("Radio 3"), w); |
|
1130 |
layout->addWidget(rb1, 0, Qt::AlignTop | Qt::AlignLeft); |
|
1131 |
layout->addWidget(rb2, 0, Qt::AlignTop | Qt::AlignLeft); |
|
1132 |
layout->addWidget(rb3, 0, Qt::AlignTop | Qt::AlignLeft); |
|
1133 |
||
1134 |
QPushButton *b1 = new QPushButton(QLatin1String("Push 1"), w); |
|
1135 |
QPushButton *b2 = new QPushButton(QLatin1String("Push 2"), w); |
|
1136 |
QPushButton *b3 = new QPushButton(QLatin1String("Push 3"), w); |
|
1137 |
layout->addWidget(b1, 0, Qt::AlignTop | Qt::AlignLeft); |
|
1138 |
layout->addWidget(b2, 0, Qt::AlignTop | Qt::AlignLeft); |
|
1139 |
layout->addWidget(b3, 0, Qt::AlignTop | Qt::AlignLeft); |
|
1140 |
||
1141 |
layout->addStretch(1); |
|
1142 |
w->setLayout(layout); |
|
1143 |
int rh = rb1->sizeHint().height(); |
|
1144 |
int ph = b1->sizeHint().height(); |
|
1145 |
QTest::newRow("1x6, radio + push buttons") |
|
1146 |
<< w << (PointList() |
|
1147 |
<< QPoint(0, 3) |
|
1148 |
<< QPoint(0, 3 + rh + 2) |
|
1149 |
<< QPoint(0, 3 + 2*(rh + 2)) |
|
1150 |
<< QPoint(0, 3 + 2*(rh + 2) + (rh + 10)) |
|
1151 |
<< QPoint(0, 3 + 2*(rh + 2) + (rh + 10 + ph + 10)) |
|
1152 |
<< QPoint(0, 3 + 2*(rh + 2) + rh + 10 + 2*(ph + 10))) |
|
1153 |
<< style->hspacing << style->vspacing << style->reimplementSubelementRect; |
|
1154 |
} |
|
1155 |
||
1156 |
{ |
|
1157 |
style->hspacing = -1; |
|
1158 |
style->vspacing = -1; |
|
1159 |
style->reimplementSubelementRect = false; |
|
1160 |
QApplication::setStyle(style); |
|
1161 |
QWidget *w = new QWidget(); |
|
1162 |
QHBoxLayout *layout = new QHBoxLayout(); |
|
1163 |
QLineEdit *le1 = new QLineEdit(w); |
|
1164 |
QLineEdit *le2 = new QLineEdit(w); |
|
1165 |
QLineEdit *le3 = new QLineEdit(w); |
|
1166 |
layout->addWidget(le1, 0, Qt::AlignTop | Qt::AlignLeft); |
|
1167 |
layout->addWidget(le2, 0, Qt::AlignTop | Qt::AlignLeft); |
|
1168 |
layout->addWidget(le3, 0, Qt::AlignTop | Qt::AlignLeft); |
|
1169 |
||
1170 |
QPushButton *b1 = new QPushButton(QLatin1String("Push 1"), w); |
|
1171 |
QPushButton *b2 = new QPushButton(QLatin1String("Push 2"), w); |
|
1172 |
QPushButton *b3 = new QPushButton(QLatin1String("Push 3"), w); |
|
1173 |
layout->addWidget(b1, 0, Qt::AlignTop | Qt::AlignLeft); |
|
1174 |
layout->addWidget(b2, 0, Qt::AlignTop | Qt::AlignLeft); |
|
1175 |
layout->addWidget(b3, 0, Qt::AlignTop | Qt::AlignLeft); |
|
1176 |
||
1177 |
layout->addStretch(1); |
|
1178 |
w->setLayout(layout); |
|
1179 |
int lw = le1->sizeHint().width(); |
|
1180 |
int pw = b1->sizeHint().width(); |
|
1181 |
QTest::newRow("6x1, line edit + push buttons") |
|
1182 |
<< w << (PointList() |
|
1183 |
<< QPoint(0, 3) |
|
1184 |
<< QPoint(0 + lw + 5, 3) |
|
1185 |
<< QPoint(0 + 2*(lw + 5), 3) |
|
1186 |
<< QPoint(0 + 3*(lw + 5), 3) |
|
1187 |
<< QPoint(0 + 3*(lw + 5) + 1*(pw + 2), 3) |
|
1188 |
<< QPoint(0 + 3*(lw + 5) + 2*(pw + 2), 3)) |
|
1189 |
<< style->hspacing << style->vspacing << style->reimplementSubelementRect; |
|
1190 |
} |
|
1191 |
||
1192 |
||
1193 |
{ |
|
1194 |
style->hspacing = 5; |
|
1195 |
style->vspacing = 10; |
|
1196 |
style->reimplementSubelementRect = true; |
|
1197 |
QApplication::setStyle(style); |
|
1198 |
QWidget *w = new QWidget(); |
|
1199 |
QVBoxLayout *layout = new QVBoxLayout(); |
|
1200 |
QPushButton *pb1 = new QPushButton(QLatin1String("Push 1"), w); |
|
1201 |
||
1202 |
QGroupBox *g1 = new QGroupBox(QLatin1String("GroupBox 1"), w); |
|
1203 |
||
1204 |
QRadioButton *rb = new QRadioButton(QLatin1String("Radio 1"), g1); |
|
1205 |
QVBoxLayout *g1layout = new QVBoxLayout(); |
|
1206 |
g1layout->addWidget(rb); |
|
1207 |
g1->setLayout(g1layout); |
|
1208 |
||
1209 |
QPushButton *pb3 = new QPushButton(QLatin1String("Push 3"), w); |
|
1210 |
||
1211 |
layout->addWidget(pb1); |
|
1212 |
layout->addWidget(g1 ); |
|
1213 |
layout->addWidget(pb3); |
|
1214 |
||
1215 |
w->setLayout(layout); |
|
1216 |
QSize psh = pb1->sizeHint(); |
|
1217 |
QSize gsh = g1->sizeHint(); |
|
1218 |
||
1219 |
QTest::newRow("subElementRect1") |
|
1220 |
<< w << (PointList() |
|
1221 |
<< QPoint(0, 3) |
|
1222 |
<< QPoint(0, 3 + psh.height() + 10 - 10) |
|
1223 |
<< QPoint(0, 3 + psh.height() + 10 - 10 + gsh.height() + 10) |
|
1224 |
) |
|
1225 |
<< style->hspacing << style->vspacing << style->reimplementSubelementRect; |
|
1226 |
} |
|
1227 |
||
1228 |
||
1229 |
{ |
|
1230 |
style->hspacing = 5; |
|
1231 |
style->vspacing = 10; |
|
1232 |
style->reimplementSubelementRect = true; |
|
1233 |
QApplication::setStyle(style); |
|
1234 |
QWidget *w = new QWidget(); |
|
1235 |
QGridLayout *layout = new QGridLayout(); |
|
1236 |
QPushButton *pb1 = new QPushButton(QLatin1String("Push 1"), w); |
|
1237 |
QPushButton *pb2 = new QPushButton(QLatin1String("Push 2"), w); |
|
1238 |
QPushButton *pb3 = new QPushButton(QLatin1String("Push 3"), w); |
|
1239 |
QPushButton *pb4 = new QPushButton(QLatin1String("Push 4"), w); |
|
1240 |
||
1241 |
layout->addWidget(pb1, 0, 0); |
|
1242 |
layout->addWidget(pb2, 0, 1); |
|
1243 |
layout->addWidget(pb3, 0, 2); |
|
1244 |
layout->addWidget(pb4, 1, 0, Qt::AlignTop); |
|
1245 |
||
1246 |
||
1247 |
QFrame *f1 = new QFrame(w); |
|
1248 |
f1->setFrameStyle(QFrame::Box | QFrame::Plain); |
|
1249 |
f1->setMinimumSize(100, 20); |
|
1250 |
f1->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed); |
|
1251 |
||
1252 |
layout->addWidget(f1, 1, 1, Qt::AlignTop); |
|
1253 |
||
1254 |
||
1255 |
QPushButton *pb6 = new QPushButton(QLatin1String("Push 6"), w); |
|
1256 |
QPushButton *pb7 = new QPushButton(QLatin1String("Push 7"), w); |
|
1257 |
QPushButton *pb8 = new QPushButton(QLatin1String("Push 8"), w); |
|
1258 |
QPushButton *pb9 = new QPushButton(QLatin1String("Push 9"), w); |
|
1259 |
layout->addWidget(pb6, 1, 2, Qt::AlignTop); |
|
1260 |
layout->addWidget(pb7, 2, 0, Qt::AlignTop); |
|
1261 |
layout->addWidget(pb8, 2, 1, Qt::AlignTop); |
|
1262 |
layout->addWidget(pb9, 2, 2, Qt::AlignTop); |
|
1263 |
||
1264 |
layout->setColumnStretch(2, 1); |
|
1265 |
layout->setRowStretch(2, 1); |
|
1266 |
w->setLayout(layout); |
|
1267 |
int c[3]; |
|
1268 |
c[0] = pb1->sizeHint().width(); |
|
1269 |
c[1] = f1->minimumSize().width() - 2*4; |
|
1270 |
c[2] = pb3->sizeHint().width(); |
|
1271 |
||
1272 |
int r[3]; |
|
1273 |
r[0] = pb1->sizeHint().height(); |
|
1274 |
r[1] = pb4->sizeHint().height(); |
|
1275 |
r[2] = pb7->sizeHint().height(); |
|
1276 |
||
1277 |
||
1278 |
QTest::newRow("subElementRect2") |
|
1279 |
<< w << (PointList() |
|
1280 |
<< QPoint(0, 3) |
|
1281 |
<< QPoint(0 + c[0] + 5, 3) |
|
1282 |
<< QPoint(0 + c[0] + 5 + c[1] + 5, 3) |
|
1283 |
||
1284 |
<< QPoint(0, 3 + r[0] + 10) |
|
1285 |
<< QPoint(0 + c[0] + 5 - 4, 3 + r[0] + 10 - 9) |
|
1286 |
<< QPoint(0 + c[0] + 5 + c[1] + 5, 3 + r[0] + 10) |
|
1287 |
||
1288 |
<< QPoint(0, 3 + r[0] + 10 + r[1] + 10) |
|
1289 |
<< QPoint(0 + c[0] + 5, 3 + r[0] + 10 + r[1] + 10) |
|
1290 |
<< QPoint(0 + c[0] + 5 + c[1] + 5, 3 + r[0] + 10 + r[1] + 10) |
|
1291 |
||
1292 |
) |
|
1293 |
<< style->hspacing << style->vspacing << style->reimplementSubelementRect; |
|
1294 |
} |
|
1295 |
||
1296 |
{ |
|
1297 |
style->hspacing = 5; |
|
1298 |
style->vspacing = 10; |
|
1299 |
style->reimplementSubelementRect = true; |
|
1300 |
QApplication::setStyle(style); |
|
1301 |
QWidget *w = new QWidget(); |
|
1302 |
QVBoxLayout *layout = new QVBoxLayout(); |
|
1303 |
QPushButton *pb1 = new QPushButton(QLatin1String("Push 1"), w); |
|
1304 |
||
1305 |
QGroupBox *g1 = new QGroupBox(QLatin1String("GroupBox 1"), w); |
|
1306 |
||
1307 |
QRadioButton *rb = new QRadioButton(QLatin1String("Radio 1"), g1); |
|
1308 |
QVBoxLayout *g1layout = new QVBoxLayout(); |
|
1309 |
g1layout->addWidget(rb); |
|
1310 |
g1->setLayout(g1layout); |
|
1311 |
||
1312 |
QPushButton *pb3 = new QPushButton(QLatin1String("Push 3"), w); |
|
1313 |
||
1314 |
pb1->setAttribute(Qt::WA_LayoutUsesWidgetRect, true); |
|
1315 |
g1->setAttribute(Qt::WA_LayoutUsesWidgetRect, true); |
|
1316 |
pb3->setAttribute(Qt::WA_LayoutUsesWidgetRect, true); |
|
1317 |
layout->addWidget(pb1); |
|
1318 |
layout->addWidget(g1 ); |
|
1319 |
layout->addWidget(pb3); |
|
1320 |
||
1321 |
w->setLayout(layout); |
|
1322 |
QSize psh = pb1->sizeHint(); |
|
1323 |
QSize gsh = g1->sizeHint(); |
|
1324 |
||
1325 |
QTest::newRow("subElementRect1, use widgetRect") |
|
1326 |
<< w << (PointList() |
|
1327 |
<< QPoint(0, 3) |
|
1328 |
<< QPoint(0, 3 + psh.height() + 10) |
|
1329 |
<< QPoint(0, 3 + psh.height() + 10 + gsh.height() + 10) |
|
1330 |
) |
|
1331 |
<< style->hspacing << style->vspacing << style->reimplementSubelementRect; |
|
1332 |
} |
|
1333 |
||
1334 |
||
1335 |
{ |
|
1336 |
style->hspacing = 5; |
|
1337 |
style->vspacing = 10; |
|
1338 |
style->reimplementSubelementRect = true; |
|
1339 |
QApplication::setStyle(style); |
|
1340 |
QWidget *w = new QWidget(); |
|
1341 |
QVBoxLayout *layout = new QVBoxLayout(); |
|
1342 |
QPushButton *pb1 = new QPushButton(QLatin1String("Push 1"), w); |
|
1343 |
||
1344 |
QGroupBox *g1 = new QGroupBox(QLatin1String("GroupBox 1"), w); |
|
1345 |
||
1346 |
QRadioButton *rb = new QRadioButton(QLatin1String("Radio 1"), g1); |
|
1347 |
QVBoxLayout *g1layout = new QVBoxLayout(); |
|
1348 |
g1layout->addWidget(rb); |
|
1349 |
g1->setLayout(g1layout); |
|
1350 |
||
1351 |
QPushButton *pb3 = new QPushButton(QLatin1String("Push 3"), w); |
|
1352 |
||
1353 |
pb1->setAttribute(Qt::WA_LayoutUsesWidgetRect, false); |
|
1354 |
g1->setAttribute(Qt::WA_LayoutUsesWidgetRect, false); |
|
1355 |
pb3->setAttribute(Qt::WA_LayoutUsesWidgetRect, false); |
|
1356 |
layout->addWidget(pb1); |
|
1357 |
layout->addWidget(g1 ); |
|
1358 |
layout->addWidget(pb3); |
|
1359 |
||
1360 |
w->setLayout(layout); |
|
1361 |
QSize psh = pb1->sizeHint(); |
|
1362 |
QSize gsh = g1->sizeHint(); |
|
1363 |
||
1364 |
QTest::newRow("subElementRect1, use layoutItemRect") |
|
1365 |
<< w << (PointList() |
|
1366 |
<< QPoint(0, 3) |
|
1367 |
<< QPoint(0, 3 + psh.height() + 10 - 10) |
|
1368 |
<< QPoint(0, 3 + psh.height() + 10 - 10 + gsh.height() + 10) |
|
1369 |
) |
|
1370 |
<< style->hspacing << style->vspacing << style->reimplementSubelementRect; |
|
1371 |
} |
|
1372 |
||
1373 |
||
1374 |
{ |
|
1375 |
/* A 3x4 gridlayout, modified arrowpad example: |
|
1376 |
* [PB] |
|
1377 |
* [PB] [PB] |
|
1378 |
* |PB| |
|
1379 |
* | | |
|
1380 |
* | | |
|
1381 |
* |
|
1382 |
* Here the bottom pushbutton has a span |
|
1383 |
*/ |
|
1384 |
style->hspacing = -1; |
|
1385 |
style->vspacing = -1; |
|
1386 |
style->reimplementSubelementRect = false; |
|
1387 |
QApplication::setStyle(style); |
|
1388 |
QWidget *w = new QWidget(); |
|
1389 |
QGridLayout *layout = new QGridLayout(); |
|
1390 |
QPushButton *left = new QPushButton(w); |
|
1391 |
QPushButton *up = new QPushButton(w); |
|
1392 |
QPushButton *right = new QPushButton(w); |
|
1393 |
QPushButton *down = new QPushButton(w); |
|
1394 |
||
1395 |
layout->addWidget(up, 0, 1); |
|
1396 |
layout->addWidget(left, 1, 0); |
|
1397 |
layout->addWidget(right, 1, 2); |
|
1398 |
layout->addWidget(down, 2, 1, 3, 1); |
|
1399 |
||
1400 |
w->setLayout(layout); |
|
1401 |
int pw = up->sizeHint().width(); |
|
1402 |
int ph = up->sizeHint().height(); |
|
1403 |
QTest::newRow("arrowpad with span") |
|
1404 |
<< w << (PointList() |
|
1405 |
<< QPoint(0 + pw + 5, 3) |
|
1406 |
<< QPoint(0, 3 + ph + 10) |
|
1407 |
<< QPoint(0 + pw + 5 + pw + 5, 3 + ph + 10) |
|
1408 |
<< QPoint(0 + pw + 5, 3 + ph + 10 + ph + 10) |
|
1409 |
) |
|
1410 |
<< style->hspacing << style->vspacing << style->reimplementSubelementRect; |
|
1411 |
} |
|
1412 |
||
1413 |
||
1414 |
for (int yoff = 0; yoff < 5; ++yoff) |
|
1415 |
{ |
|
1416 |
for (int xoff = 0; xoff < 5; ++xoff) { |
|
1417 |
/* A 3x4 gridlayout, modified arrowpad example: |
|
1418 |
* [empty cells] |
|
1419 |
* [PB] |
|
1420 |
* [PB] [PB] |
|
1421 |
* [PB] |
|
1422 |
* |
|
1423 |
* It has 0-4 empty rows at the top and 0-4 empty columns to the left. |
|
1424 |
*/ |
|
1425 |
style->hspacing = -1; |
|
1426 |
style->vspacing = -1; |
|
1427 |
style->reimplementSubelementRect = false; |
|
1428 |
QApplication::setStyle(style); |
|
1429 |
QWidget *w = new QWidget(); |
|
1430 |
QGridLayout *layout = new QGridLayout(); |
|
1431 |
QPushButton *left = new QPushButton(w); |
|
1432 |
QPushButton *up = new QPushButton(w); |
|
1433 |
QPushButton *right = new QPushButton(w); |
|
1434 |
QPushButton *down = new QPushButton(w); |
|
1435 |
||
1436 |
layout->addWidget(up, yoff + 0, xoff + 1); |
|
1437 |
layout->addWidget(left, yoff + 1, xoff + 0); |
|
1438 |
layout->addWidget(right, yoff + 1, xoff + 2); |
|
1439 |
layout->addWidget(down, yoff + 2, xoff + 1, 3, 1); |
|
1440 |
||
1441 |
w->setLayout(layout); |
|
1442 |
int pw = up->sizeHint().width(); |
|
1443 |
int ph = up->sizeHint().height(); |
|
1444 |
QByteArray testName = QString::fromAscii("arrowpad with %1 empty rows, %2 empty columns").arg(yoff).arg(xoff).toLatin1(); |
|
1445 |
QTest::newRow(testName.data()) |
|
1446 |
<< w << (PointList() |
|
1447 |
<< QPoint(0 + pw + 5, 3) |
|
1448 |
<< QPoint(0, 3 + ph + 10) |
|
1449 |
<< QPoint(0 + pw + 5 + pw + 5, 3 + ph + 10) |
|
1450 |
<< QPoint(0 + pw + 5, 3 + ph + 10 + ph + 10) |
|
1451 |
) |
|
1452 |
<< style->hspacing << style->vspacing << style->reimplementSubelementRect; |
|
1453 |
} |
|
1454 |
} |
|
1455 |
||
1456 |
} |
|
1457 |
||
1458 |
void tst_QGridLayout::layoutSpacingImplementation() |
|
1459 |
{ |
|
1460 |
QFETCH(QWidget *, widget); |
|
1461 |
QFETCH(PointList, expectedpositions); |
|
1462 |
QFETCH(int, hSpacing); |
|
1463 |
QFETCH(int, vSpacing); |
|
1464 |
QFETCH(bool, customSubElementRect); |
|
1465 |
||
1466 |
CustomLayoutStyle *style = new CustomLayoutStyle(); |
|
1467 |
style->hspacing = hSpacing; |
|
1468 |
style->vspacing = vSpacing; |
|
1469 |
style->reimplementSubelementRect = customSubElementRect; |
|
1470 |
QApplication::setStyle(style); |
|
1471 |
widget->resize(widget->sizeHint()); |
|
1472 |
widget->show(); |
|
1473 |
#if defined(Q_WS_X11) |
|
1474 |
qt_x11_wait_for_window_manager(widget); // wait for the show |
|
1475 |
#endif |
|
1476 |
||
1477 |
QLayout *layout = widget->layout(); |
|
1478 |
QVERIFY(layout); |
|
1479 |
//QTest::qWait(2000); |
|
1480 |
for (int pi = 0; pi < expectedpositions.count(); ++pi) { |
|
1481 |
QLayoutItem *item = layout->itemAt(pi); |
|
1482 |
//qDebug() << item->widget()->pos(); |
|
1483 |
QCOMPARE(item->widget()->pos(), expectedpositions.at(pi)); |
|
1484 |
} |
|
1485 |
delete widget; |
|
1486 |
||
1487 |
} |
|
1488 |
||
1489 |
void tst_QGridLayout::spacing() |
|
1490 |
{ |
|
1491 |
QWidget w; |
|
1492 |
CustomLayoutStyle *style = new CustomLayoutStyle(); |
|
1493 |
style->hspacing = 5; |
|
1494 |
style->vspacing = 10; |
|
1495 |
w.setStyle(style); |
|
1496 |
QGridLayout grid(&w); |
|
1497 |
QCOMPARE(style->hspacing, grid.horizontalSpacing()); |
|
1498 |
QCOMPARE(style->vspacing, grid.verticalSpacing()); |
|
1499 |
||
1500 |
QCOMPARE(grid.spacing(), -1); |
|
1501 |
grid.setVerticalSpacing(5); |
|
1502 |
QCOMPARE(5, grid.horizontalSpacing()); |
|
1503 |
QCOMPARE(5, grid.verticalSpacing()); |
|
1504 |
QCOMPARE(grid.spacing(), 5); |
|
1505 |
grid.setVerticalSpacing(-1); |
|
1506 |
QCOMPARE(style->hspacing, grid.horizontalSpacing()); |
|
1507 |
QCOMPARE(style->vspacing, grid.verticalSpacing()); |
|
1508 |
||
1509 |
style->hspacing = 5; |
|
1510 |
style->vspacing = 5; |
|
1511 |
QCOMPARE(grid.spacing(), 5); |
|
1512 |
||
1513 |
||
1514 |
grid.setHorizontalSpacing(20); |
|
1515 |
QCOMPARE(grid.spacing(), -1); |
|
1516 |
style->vspacing = 20; |
|
1517 |
QCOMPARE(grid.horizontalSpacing(), 20); |
|
1518 |
QCOMPARE(grid.verticalSpacing(), 20); |
|
1519 |
QCOMPARE(grid.spacing(), 20); |
|
1520 |
grid.setHorizontalSpacing(-1); |
|
1521 |
QCOMPARE(grid.spacing(), -1); |
|
1522 |
style->hspacing = 20; |
|
1523 |
QCOMPARE(grid.spacing(), 20); |
|
1524 |
||
1525 |
||
1526 |
delete style; |
|
1527 |
} |
|
1528 |
||
1529 |
void populate(QGridLayout *layout, int row, int kind) |
|
1530 |
{ |
|
1531 |
if (kind == 0) { |
|
1532 |
QWidget *widget = new QWidget; |
|
1533 |
widget->setFixedSize(100, 100); |
|
1534 |
layout->addWidget(widget, row, 0); |
|
1535 |
} else if (kind == 1) { |
|
1536 |
layout->addItem(new QSpacerItem(10, 10), row, 0); |
|
1537 |
} |
|
1538 |
} |
|
1539 |
||
1540 |
void tst_QGridLayout::spacerWithSpacing() |
|
1541 |
{ |
|
1542 |
// Tests all combinations of widget (w), spacer (s) and no layoutitem (-) |
|
1543 |
// to see if they are laid out correctly. |
|
1544 |
// Note that a combination of "s-" or "-s" should only give the height of "s" |
|
1545 |
const int expectedHeight[] = { |
|
1546 |
302,// www |
|
1547 |
211,// wws |
|
1548 |
201,// ww- |
|
1549 |
211,// wsw |
|
1550 |
120,// wss |
|
1551 |
110,// ws- |
|
1552 |
201,// w-w |
|
1553 |
110,// w-s |
|
1554 |
100,// w-- |
|
1555 |
211,// sww |
|
1556 |
120,// sws |
|
1557 |
110,// sw- |
|
1558 |
120,// ssw |
|
1559 |
30,// sss |
|
1560 |
20,// ss- |
|
1561 |
110,// s-w |
|
1562 |
20,// s-s |
|
1563 |
10,// s-- |
|
1564 |
201,// -ww |
|
1565 |
110,// -ws |
|
1566 |
100,// -w- |
|
1567 |
110,// -sw |
|
1568 |
20,// -ss |
|
1569 |
10,// -s- |
|
1570 |
100,// --w |
|
1571 |
10,// --s |
|
1572 |
000 // --- |
|
1573 |
}; |
|
1574 |
int ii = 0; |
|
1575 |
for (int i = 0; i < 3; ++i) { |
|
1576 |
for (int j = 0; j < 3; ++j) { |
|
1577 |
for (int k = 0; k < 3; ++k) { |
|
1578 |
QWidget window; |
|
1579 |
QGridLayout layout(&window); |
|
1580 |
layout.setSpacing(1); |
|
1581 |
layout.setMargin(0); |
|
1582 |
populate(&layout, 0, i); |
|
1583 |
populate(&layout, 1, j); |
|
1584 |
populate(&layout, 2, k); |
|
1585 |
QCOMPARE(window.sizeHint().height(), expectedHeight[ii]); |
|
1586 |
#if 0 |
|
1587 |
const char T[] = "ws-"; |
|
1588 |
qWarning("%c%c%c: %.3d", i[T], j[T], k[T], window.sizeHint().height()); |
|
1589 |
#endif |
|
1590 |
++ii; |
|
1591 |
} |
|
1592 |
} |
|
1593 |
} |
|
1594 |
} |
|
1595 |
||
1596 |
void tst_QGridLayout::contentsRect() |
|
1597 |
{ |
|
1598 |
QWidget w; |
|
1599 |
QGridLayout grid; |
|
1600 |
w.setLayout(&grid); |
|
1601 |
grid.addWidget(new QPushButton(&w)); |
|
1602 |
w.show(); |
|
1603 |
#if defined(Q_WS_X11) |
|
1604 |
qt_x11_wait_for_window_manager(&w); // wait for the show |
|
1605 |
#endif |
|
1606 |
int l, t, r, b; |
|
1607 |
grid.getContentsMargins(&l, &t, &r, &b); |
|
1608 |
QRect geom = grid.geometry(); |
|
1609 |
||
1610 |
QCOMPARE(geom.adjusted(+l, +t, -r, -b), grid.contentsRect()); |
|
1611 |
||
1612 |
} |
|
1613 |
||
1614 |
void tst_QGridLayout::distributeMultiCell() |
|
1615 |
{ |
|
1616 |
QWidget w; |
|
1617 |
Qt42Style *style = new Qt42Style(); |
|
1618 |
style->spacing = 9; |
|
1619 |
||
1620 |
w.setStyle(style); |
|
1621 |
QGridLayout grid; |
|
1622 |
w.setLayout(&grid); |
|
1623 |
||
1624 |
SizeHinter le1(200, 20, &w); |
|
1625 |
le1.setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed)); |
|
1626 |
SizeHinter le2(200, 20, &w); |
|
1627 |
le2.setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed)); |
|
1628 |
SizeHinter box(80, 57, &w); |
|
1629 |
box.setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding)); |
|
1630 |
box.setMinimumSize(80, 57); |
|
1631 |
||
1632 |
grid.addWidget(&le1, 0, 0, 1, 1); |
|
1633 |
grid.addWidget(&le2, 1, 0, 1, 1); |
|
1634 |
grid.addWidget(&box, 0, 1, 2, 1); |
|
1635 |
||
1636 |
QCOMPARE(box.sizeHint().height(), 57); |
|
1637 |
QCOMPARE(w.sizeHint().height(), 11 + 57 + 11); |
|
1638 |
} |
|
1639 |
||
1640 |
QTEST_MAIN(tst_QGridLayout) |
|
1641 |
#include "tst_qgridlayout.moc" |