0
|
1 |
/****************************************************************************
|
|
2 |
**
|
|
3 |
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
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 <qformlayout.h>
|
|
55 |
|
|
56 |
//TESTED_CLASS=
|
|
57 |
//TESTED_FILES=
|
|
58 |
|
|
59 |
class tst_QFormLayout : public QObject
|
|
60 |
{
|
|
61 |
Q_OBJECT
|
|
62 |
|
|
63 |
public:
|
|
64 |
tst_QFormLayout();
|
|
65 |
~tst_QFormLayout();
|
|
66 |
|
|
67 |
public slots:
|
|
68 |
void initTestCase();
|
|
69 |
void cleanupTestCase();
|
|
70 |
void init();
|
|
71 |
void cleanup();
|
|
72 |
|
|
73 |
private slots:
|
|
74 |
void rowCount();
|
|
75 |
void buddies();
|
|
76 |
void getItemPosition();
|
|
77 |
void wrapping();
|
|
78 |
void spacing();
|
|
79 |
void contentsRect();
|
|
80 |
|
|
81 |
void setFormStyle();
|
|
82 |
void setFieldGrowthPolicy();
|
|
83 |
void setRowWrapPolicy();
|
|
84 |
void setLabelAlignment();
|
|
85 |
void setFormAlignment();
|
|
86 |
|
|
87 |
/*
|
|
88 |
void setHorizontalSpacing(int spacing);
|
|
89 |
int horizontalSpacing() const;
|
|
90 |
void setVerticalSpacing(int spacing);
|
|
91 |
int verticalSpacing() const;
|
|
92 |
*/
|
|
93 |
|
|
94 |
void addRow();
|
|
95 |
void insertRow_QWidget_QWidget();
|
|
96 |
void insertRow_QWidget_QLayout();
|
|
97 |
void insertRow_QString_QWidget();
|
|
98 |
void insertRow_QString_QLayout();
|
|
99 |
void insertRow_QWidget();
|
|
100 |
void insertRow_QLayout();
|
|
101 |
void setWidget();
|
|
102 |
void setLayout();
|
|
103 |
|
|
104 |
/*
|
|
105 |
QLayoutItem *itemAt(int row, ItemRole role) const;
|
|
106 |
void getItemPosition(int index, int *rowPtr, ItemRole *rolePtr) const;
|
|
107 |
void getLayoutPosition(QWidget *widget, int *rowPtr, ItemRole *rolePtr) const;
|
|
108 |
void getItemPosition(QLayoutItem *item, int *rowPtr, ItemRole *rolePtr) const;
|
|
109 |
QWidget *labelForField(QWidget *widget) const;
|
|
110 |
QWidget *labelForField(QLayoutItem *item) const;
|
|
111 |
|
|
112 |
void addItem(QLayoutItem *item);
|
|
113 |
*/
|
|
114 |
|
|
115 |
void itemAt();
|
|
116 |
void takeAt();
|
|
117 |
void layoutAlone();
|
|
118 |
/*
|
|
119 |
void setGeometry(const QRect &rect);
|
|
120 |
QSize minimumSize() const;
|
|
121 |
QSize sizeHint() const;
|
|
122 |
|
|
123 |
bool hasHeightForWidth() const;
|
|
124 |
int heightForWidth(int width) const;
|
|
125 |
Qt::Orientations expandingDirections() const;
|
|
126 |
*/
|
|
127 |
|
|
128 |
};
|
|
129 |
|
|
130 |
tst_QFormLayout::tst_QFormLayout()
|
|
131 |
{
|
|
132 |
}
|
|
133 |
|
|
134 |
tst_QFormLayout::~tst_QFormLayout()
|
|
135 |
{
|
|
136 |
}
|
|
137 |
|
|
138 |
void tst_QFormLayout::initTestCase()
|
|
139 |
{
|
|
140 |
}
|
|
141 |
|
|
142 |
void tst_QFormLayout::cleanupTestCase()
|
|
143 |
{
|
|
144 |
}
|
|
145 |
|
|
146 |
void tst_QFormLayout::init()
|
|
147 |
{
|
|
148 |
}
|
|
149 |
|
|
150 |
void tst_QFormLayout::cleanup()
|
|
151 |
{
|
|
152 |
}
|
|
153 |
|
|
154 |
void tst_QFormLayout::rowCount()
|
|
155 |
{
|
|
156 |
QWidget *w = new QWidget;
|
|
157 |
QFormLayout *fl = new QFormLayout(w);
|
|
158 |
|
|
159 |
fl->addRow(tr("Label 1"), new QLineEdit);
|
|
160 |
fl->addRow(tr("Label 2"), new QLineEdit);
|
|
161 |
fl->addRow(tr("Label 3"), new QLineEdit);
|
|
162 |
QCOMPARE(fl->rowCount(), 3);
|
|
163 |
|
|
164 |
fl->addRow(new QWidget);
|
|
165 |
fl->addRow(new QHBoxLayout);
|
|
166 |
QCOMPARE(fl->rowCount(), 5);
|
|
167 |
|
|
168 |
fl->insertRow(1, tr("Label 0.5"), new QLineEdit);
|
|
169 |
QCOMPARE(fl->rowCount(), 6);
|
|
170 |
|
|
171 |
//TODO: remove items
|
|
172 |
|
|
173 |
delete w;
|
|
174 |
}
|
|
175 |
|
|
176 |
void tst_QFormLayout::buddies()
|
|
177 |
{
|
|
178 |
QWidget *w = new QWidget;
|
|
179 |
QFormLayout *fl = new QFormLayout(w);
|
|
180 |
|
|
181 |
//normal buddy case
|
|
182 |
QLineEdit *le = new QLineEdit;
|
|
183 |
fl->addRow(tr("Label 1"), le);
|
|
184 |
QLabel *label = qobject_cast<QLabel *>(fl->labelForField(le));
|
|
185 |
QVERIFY(label);
|
|
186 |
QWidget *lew = le;
|
|
187 |
QCOMPARE(label->buddy(), lew);
|
|
188 |
|
|
189 |
//null label
|
|
190 |
QLineEdit *le2 = new QLineEdit;
|
|
191 |
fl->addRow(0, le2);
|
|
192 |
QWidget *label2 = fl->labelForField(le2);
|
|
193 |
QVERIFY(label2 == 0);
|
|
194 |
|
|
195 |
//no label
|
|
196 |
QLineEdit *le3 = new QLineEdit;
|
|
197 |
fl->addRow(le3);
|
|
198 |
QWidget *label3 = fl->labelForField(le3);
|
|
199 |
QVERIFY(label3 == 0);
|
|
200 |
|
|
201 |
//TODO: empty label?
|
|
202 |
|
|
203 |
delete w;
|
|
204 |
}
|
|
205 |
|
|
206 |
void tst_QFormLayout::getItemPosition()
|
|
207 |
{
|
|
208 |
QWidget *w = new QWidget;
|
|
209 |
QFormLayout *fl = new QFormLayout(w);
|
|
210 |
|
|
211 |
QList<QLabel*> labels;
|
|
212 |
QList<QLineEdit*> fields;
|
|
213 |
for (int i = 0; i < 5; ++i) {
|
|
214 |
labels.append(new QLabel(QString("Label %1").arg(i+1)));
|
|
215 |
fields.append(new QLineEdit);
|
|
216 |
fl->addRow(labels[i], fields[i]);
|
|
217 |
}
|
|
218 |
|
|
219 |
//a field
|
|
220 |
{
|
|
221 |
int row;
|
|
222 |
QFormLayout::ItemRole role;
|
|
223 |
fl->getWidgetPosition(fields[3], &row, &role);
|
|
224 |
QCOMPARE(row, 3);
|
|
225 |
QCOMPARE(role, QFormLayout::FieldRole);
|
|
226 |
}
|
|
227 |
|
|
228 |
//a label
|
|
229 |
{
|
|
230 |
int row;
|
|
231 |
QFormLayout::ItemRole role;
|
|
232 |
fl->getWidgetPosition(labels[2], &row, &role);
|
|
233 |
QCOMPARE(row, 2);
|
|
234 |
QCOMPARE(role, QFormLayout::LabelRole);
|
|
235 |
}
|
|
236 |
|
|
237 |
//a layout that's been inserted
|
|
238 |
{
|
|
239 |
QVBoxLayout *vbl = new QVBoxLayout;
|
|
240 |
fl->insertRow(2, "Label 1.5", vbl);
|
|
241 |
int row;
|
|
242 |
QFormLayout::ItemRole role;
|
|
243 |
fl->getLayoutPosition(vbl, &row, &role);
|
|
244 |
QCOMPARE(row, 2);
|
|
245 |
QCOMPARE(role, QFormLayout::FieldRole);
|
|
246 |
}
|
|
247 |
|
|
248 |
delete w;
|
|
249 |
}
|
|
250 |
|
|
251 |
void tst_QFormLayout::wrapping()
|
|
252 |
{
|
|
253 |
QWidget *w = new QWidget;
|
|
254 |
QFormLayout *fl = new QFormLayout(w);
|
|
255 |
fl->setRowWrapPolicy(QFormLayout::WrapLongRows);
|
|
256 |
|
|
257 |
QLineEdit *le = new QLineEdit;
|
|
258 |
QLabel *lbl = new QLabel("A long label");
|
|
259 |
le->setMinimumWidth(200);
|
|
260 |
fl->addRow(lbl, le);
|
|
261 |
|
|
262 |
w->setFixedWidth(240);
|
|
263 |
w->show();
|
|
264 |
|
|
265 |
QCOMPARE(le->geometry().y() > lbl->geometry().y(), true);
|
|
266 |
|
|
267 |
//TODO: additional tests covering different wrapping cases
|
|
268 |
|
|
269 |
delete w;
|
|
270 |
}
|
|
271 |
|
|
272 |
class CustomLayoutStyle : public QWindowsStyle
|
|
273 |
{
|
|
274 |
Q_OBJECT
|
|
275 |
public:
|
|
276 |
CustomLayoutStyle()
|
|
277 |
{
|
|
278 |
hspacing = 5;
|
|
279 |
vspacing = 10;
|
|
280 |
}
|
|
281 |
|
|
282 |
virtual int pixelMetric(PixelMetric metric, const QStyleOption * option = 0,
|
|
283 |
const QWidget * widget = 0 ) const;
|
|
284 |
|
|
285 |
int hspacing;
|
|
286 |
int vspacing;
|
|
287 |
};
|
|
288 |
|
|
289 |
int CustomLayoutStyle::pixelMetric(PixelMetric metric, const QStyleOption * option /*= 0*/,
|
|
290 |
const QWidget * widget /*= 0*/ ) const
|
|
291 |
{
|
|
292 |
switch (metric) {
|
|
293 |
case PM_LayoutHorizontalSpacing:
|
|
294 |
return hspacing;
|
|
295 |
case PM_LayoutVerticalSpacing:
|
|
296 |
return vspacing;
|
|
297 |
break;
|
|
298 |
default:
|
|
299 |
break;
|
|
300 |
}
|
|
301 |
return QWindowsStyle::pixelMetric(metric, option, widget);
|
|
302 |
}
|
|
303 |
|
|
304 |
void tst_QFormLayout::spacing()
|
|
305 |
{
|
|
306 |
//TODO: confirm spacing behavior
|
|
307 |
QWidget *w = new QWidget;
|
|
308 |
CustomLayoutStyle *style = new CustomLayoutStyle;
|
|
309 |
style->hspacing = 5;
|
|
310 |
style->vspacing = 10;
|
|
311 |
w->setStyle(style);
|
|
312 |
QFormLayout *fl = new QFormLayout(w);
|
|
313 |
QCOMPARE(style->hspacing, fl->horizontalSpacing());
|
|
314 |
QCOMPARE(style->vspacing, fl->verticalSpacing());
|
|
315 |
|
|
316 |
//QCOMPARE(fl->spacing(), -1);
|
|
317 |
fl->setVerticalSpacing(5);
|
|
318 |
QCOMPARE(5, fl->horizontalSpacing());
|
|
319 |
QCOMPARE(5, fl->verticalSpacing());
|
|
320 |
//QCOMPARE(fl->spacing(), 5);
|
|
321 |
fl->setVerticalSpacing(-1);
|
|
322 |
QCOMPARE(style->hspacing, fl->horizontalSpacing());
|
|
323 |
QCOMPARE(style->vspacing, fl->verticalSpacing());
|
|
324 |
|
|
325 |
style->hspacing = 5;
|
|
326 |
style->vspacing = 5;
|
|
327 |
//QCOMPARE(fl->spacing(), 5);
|
|
328 |
|
|
329 |
fl->setHorizontalSpacing(20);
|
|
330 |
//QCOMPARE(fl->spacing(), -1);
|
|
331 |
style->vspacing = 20;
|
|
332 |
QCOMPARE(fl->horizontalSpacing(), 20);
|
|
333 |
QCOMPARE(fl->verticalSpacing(), 20);
|
|
334 |
//QCOMPARE(fl->spacing(), 20);
|
|
335 |
fl->setHorizontalSpacing(-1);
|
|
336 |
//QCOMPARE(fl->spacing(), -1);
|
|
337 |
style->hspacing = 20;
|
|
338 |
//QCOMPARE(fl->spacing(), 20);
|
|
339 |
|
|
340 |
delete w;
|
|
341 |
delete style;
|
|
342 |
}
|
|
343 |
|
|
344 |
void tst_QFormLayout::contentsRect()
|
|
345 |
{
|
|
346 |
QWidget w;
|
|
347 |
QFormLayout form;
|
|
348 |
w.setLayout(&form);
|
|
349 |
form.addRow("Label", new QPushButton(&w));
|
|
350 |
w.show();
|
|
351 |
/*#if defined(Q_WS_X11)
|
|
352 |
qt_x11_wait_for_window_manager(&w); // wait for the show
|
|
353 |
#endif*/
|
|
354 |
int l, t, r, b;
|
|
355 |
form.getContentsMargins(&l, &t, &r, &b);
|
|
356 |
QRect geom = form.geometry();
|
|
357 |
|
|
358 |
QCOMPARE(geom.adjusted(+l, +t, -r, -b), form.contentsRect());
|
|
359 |
}
|
|
360 |
|
|
361 |
|
|
362 |
class DummyMacStyle : public QCommonStyle
|
|
363 |
{
|
|
364 |
public:
|
|
365 |
virtual int styleHint ( StyleHint hint, const QStyleOption * option = 0, const QWidget * widget = 0, QStyleHintReturn * returnData = 0 ) const
|
|
366 |
{
|
|
367 |
switch(hint) {
|
|
368 |
case SH_FormLayoutFormAlignment:
|
|
369 |
return Qt::AlignHCenter | Qt::AlignTop;
|
|
370 |
case SH_FormLayoutLabelAlignment:
|
|
371 |
return Qt::AlignRight;
|
|
372 |
case SH_FormLayoutWrapPolicy:
|
|
373 |
return QFormLayout::DontWrapRows;
|
|
374 |
case SH_FormLayoutFieldGrowthPolicy:
|
|
375 |
return QFormLayout::FieldsStayAtSizeHint;
|
|
376 |
default:
|
|
377 |
return QCommonStyle::styleHint(hint, option, widget, returnData);
|
|
378 |
}
|
|
379 |
}
|
|
380 |
};
|
|
381 |
|
|
382 |
class DummyQtopiaStyle : public QCommonStyle
|
|
383 |
{
|
|
384 |
public:
|
|
385 |
virtual int styleHint ( StyleHint hint, const QStyleOption * option = 0, const QWidget * widget = 0, QStyleHintReturn * returnData = 0 ) const
|
|
386 |
{
|
|
387 |
switch(hint) {
|
|
388 |
case SH_FormLayoutFormAlignment:
|
|
389 |
return Qt::AlignLeft | Qt::AlignTop;
|
|
390 |
case SH_FormLayoutLabelAlignment:
|
|
391 |
return Qt::AlignRight;
|
|
392 |
case SH_FormLayoutWrapPolicy:
|
|
393 |
return QFormLayout::WrapLongRows;
|
|
394 |
case SH_FormLayoutFieldGrowthPolicy:
|
|
395 |
return QFormLayout::AllNonFixedFieldsGrow;
|
|
396 |
default:
|
|
397 |
return QCommonStyle::styleHint(hint, option, widget, returnData);
|
|
398 |
}
|
|
399 |
}
|
|
400 |
};
|
|
401 |
|
|
402 |
void tst_QFormLayout::setFormStyle()
|
|
403 |
{
|
|
404 |
QWidget widget;
|
|
405 |
QFormLayout layout;
|
|
406 |
widget.setLayout(&layout);
|
|
407 |
|
|
408 |
#ifndef QT_NO_STYLE_PLASTIQUE
|
|
409 |
widget.setStyle(new QPlastiqueStyle());
|
|
410 |
|
|
411 |
QVERIFY(layout.labelAlignment() == Qt::AlignRight);
|
|
412 |
QVERIFY(layout.formAlignment() == (Qt::AlignLeft | Qt::AlignTop));
|
|
413 |
QVERIFY(layout.fieldGrowthPolicy() == QFormLayout::ExpandingFieldsGrow);
|
|
414 |
QVERIFY(layout.rowWrapPolicy() == QFormLayout::DontWrapRows);
|
|
415 |
#endif
|
|
416 |
|
|
417 |
widget.setStyle(new QWindowsStyle());
|
|
418 |
|
|
419 |
QVERIFY(layout.labelAlignment() == Qt::AlignLeft);
|
|
420 |
QVERIFY(layout.formAlignment() == (Qt::AlignLeft | Qt::AlignTop));
|
|
421 |
QVERIFY(layout.fieldGrowthPolicy() == QFormLayout::AllNonFixedFieldsGrow);
|
|
422 |
QVERIFY(layout.rowWrapPolicy() == QFormLayout::DontWrapRows);
|
|
423 |
|
|
424 |
/* can't directly create mac style or qtopia style, since
|
|
425 |
this test is cross platform.. so create dummy styles that
|
|
426 |
return all the right stylehints.
|
|
427 |
*/
|
|
428 |
widget.setStyle(new DummyMacStyle());
|
|
429 |
|
|
430 |
QVERIFY(layout.labelAlignment() == Qt::AlignRight);
|
|
431 |
QVERIFY(layout.formAlignment() == (Qt::AlignHCenter | Qt::AlignTop));
|
|
432 |
QVERIFY(layout.fieldGrowthPolicy() == QFormLayout::FieldsStayAtSizeHint);
|
|
433 |
QVERIFY(layout.rowWrapPolicy() == QFormLayout::DontWrapRows);
|
|
434 |
|
|
435 |
widget.setStyle(new DummyQtopiaStyle());
|
|
436 |
|
|
437 |
QVERIFY(layout.labelAlignment() == Qt::AlignRight);
|
|
438 |
QVERIFY(layout.formAlignment() == (Qt::AlignLeft | Qt::AlignTop));
|
|
439 |
QVERIFY(layout.fieldGrowthPolicy() == QFormLayout::AllNonFixedFieldsGrow);
|
|
440 |
QVERIFY(layout.rowWrapPolicy() == QFormLayout::WrapLongRows);
|
|
441 |
}
|
|
442 |
|
|
443 |
void tst_QFormLayout::setFieldGrowthPolicy()
|
|
444 |
{
|
|
445 |
QWidget window;
|
|
446 |
QLineEdit fld1, fld2, fld3;
|
|
447 |
fld1.setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
|
|
448 |
fld2.setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
|
|
449 |
fld3.setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
|
|
450 |
|
|
451 |
QFormLayout layout;
|
|
452 |
layout.addRow("One:", &fld1);
|
|
453 |
layout.addRow("Two:", &fld2);
|
|
454 |
layout.addRow("Three:", &fld3);
|
|
455 |
window.setLayout(&layout);
|
|
456 |
window.resize(1000, 200);
|
|
457 |
|
|
458 |
for (int i = 0; i < 3; ++i) {
|
|
459 |
layout.setFieldGrowthPolicy(i == 0 ? QFormLayout::FieldsStayAtSizeHint :
|
|
460 |
i == 1 ? QFormLayout::ExpandingFieldsGrow :
|
|
461 |
QFormLayout::AllNonFixedFieldsGrow);
|
|
462 |
layout.activate();
|
|
463 |
|
|
464 |
if (i == 0) {
|
|
465 |
QVERIFY(fld1.width() == fld2.width());
|
|
466 |
QVERIFY(fld2.width() == fld3.width());
|
|
467 |
} else if (i == 1) {
|
|
468 |
QVERIFY(fld1.width() == fld2.width());
|
|
469 |
QVERIFY(fld2.width() < fld3.width());
|
|
470 |
} else {
|
|
471 |
QVERIFY(fld1.width() < fld2.width());
|
|
472 |
QVERIFY(fld2.width() == fld3.width());
|
|
473 |
}
|
|
474 |
}
|
|
475 |
}
|
|
476 |
|
|
477 |
void tst_QFormLayout::setRowWrapPolicy()
|
|
478 |
{
|
|
479 |
}
|
|
480 |
|
|
481 |
void tst_QFormLayout::setLabelAlignment()
|
|
482 |
{
|
|
483 |
}
|
|
484 |
|
|
485 |
void tst_QFormLayout::setFormAlignment()
|
|
486 |
{
|
|
487 |
}
|
|
488 |
|
|
489 |
void tst_QFormLayout::addRow()
|
|
490 |
{
|
|
491 |
QFormLayout layout;
|
|
492 |
QWidget w1, w2, w3;
|
|
493 |
QHBoxLayout l1, l2, l3;
|
|
494 |
QLabel lbl1, lbl2;
|
|
495 |
|
|
496 |
QCOMPARE(layout.rowCount(), 0);
|
|
497 |
|
|
498 |
layout.addRow(&lbl1, &w1);
|
|
499 |
layout.addRow(&lbl2, &l1);
|
|
500 |
layout.addRow("Foo:", &w2);
|
|
501 |
layout.addRow("Bar:", &l2);
|
|
502 |
layout.addRow(&w3);
|
|
503 |
layout.addRow(&l3);
|
|
504 |
|
|
505 |
QCOMPARE(layout.rowCount(), 6);
|
|
506 |
|
|
507 |
QVERIFY(layout.itemAt(0, QFormLayout::LabelRole)->widget() == &lbl1);
|
|
508 |
QVERIFY(layout.itemAt(1, QFormLayout::LabelRole)->widget() == &lbl2);
|
|
509 |
QVERIFY(layout.itemAt(2, QFormLayout::LabelRole)->widget()->property("text") == "Foo:");
|
|
510 |
QVERIFY(layout.itemAt(3, QFormLayout::LabelRole)->widget()->property("text") == "Bar:");
|
|
511 |
QVERIFY(layout.itemAt(4, QFormLayout::LabelRole) == 0);
|
|
512 |
QVERIFY(layout.itemAt(5, QFormLayout::LabelRole) == 0);
|
|
513 |
|
|
514 |
QVERIFY(layout.itemAt(0, QFormLayout::FieldRole)->widget() == &w1);
|
|
515 |
QVERIFY(layout.itemAt(1, QFormLayout::FieldRole)->layout() == &l1);
|
|
516 |
QVERIFY(layout.itemAt(2, QFormLayout::FieldRole)->widget() == &w2);
|
|
517 |
QVERIFY(layout.itemAt(3, QFormLayout::FieldRole)->layout() == &l2);
|
|
518 |
// ### should have a third role, FullRowRole?
|
|
519 |
// QVERIFY(layout.itemAt(4, QFormLayout::FieldRole) == 0);
|
|
520 |
// QVERIFY(layout.itemAt(5, QFormLayout::FieldRole) == 0);
|
|
521 |
}
|
|
522 |
|
|
523 |
void tst_QFormLayout::insertRow_QWidget_QWidget()
|
|
524 |
{
|
|
525 |
QFormLayout layout;
|
|
526 |
QLabel lbl1, lbl2, lbl3, lbl4;
|
|
527 |
QLineEdit fld1, fld2, fld3, fld4;
|
|
528 |
|
|
529 |
layout.insertRow(0, &lbl1, &fld1);
|
|
530 |
QCOMPARE(layout.rowCount(), 1);
|
|
531 |
|
|
532 |
{
|
|
533 |
int row = -1;
|
|
534 |
QFormLayout::ItemRole role = QFormLayout::ItemRole(-123);
|
|
535 |
layout.getWidgetPosition(&lbl1, &row, &role);
|
|
536 |
QCOMPARE(row, 0);
|
|
537 |
QCOMPARE(int(role), int(QFormLayout::LabelRole));
|
|
538 |
}
|
|
539 |
|
|
540 |
{
|
|
541 |
int row = -1;
|
|
542 |
QFormLayout::ItemRole role = QFormLayout::ItemRole(-123);
|
|
543 |
layout.getWidgetPosition(&fld1, &row, &role);
|
|
544 |
QCOMPARE(row, 0);
|
|
545 |
QCOMPARE(int(role), int(QFormLayout::FieldRole));
|
|
546 |
}
|
|
547 |
|
|
548 |
// check that negative values append
|
|
549 |
layout.insertRow(-2, &lbl2, &fld2);
|
|
550 |
QCOMPARE(layout.rowCount(), 2);
|
|
551 |
|
|
552 |
QVERIFY(layout.itemAt(0, QFormLayout::LabelRole)->widget() == &lbl1);
|
|
553 |
QVERIFY(layout.itemAt(1, QFormLayout::LabelRole)->widget() == &lbl2);
|
|
554 |
|
|
555 |
// check that too large values append
|
|
556 |
layout.insertRow(100, &lbl3, &fld3);
|
|
557 |
QCOMPARE(layout.rowCount(), 3);
|
|
558 |
QCOMPARE(layout.count(), 6);
|
|
559 |
|
|
560 |
layout.insertRow(3, (QWidget *)0, (QWidget *)0);
|
|
561 |
QCOMPARE(layout.rowCount(), 4);
|
|
562 |
QCOMPARE(layout.count(), 6);
|
|
563 |
|
|
564 |
layout.insertRow(4, (QWidget *)0, &fld4);
|
|
565 |
QCOMPARE(layout.rowCount(), 5);
|
|
566 |
QCOMPARE(layout.count(), 7);
|
|
567 |
|
|
568 |
layout.insertRow(5, &lbl4, (QWidget *)0);
|
|
569 |
QCOMPARE(layout.rowCount(), 6);
|
|
570 |
QCOMPARE(layout.count(), 8);
|
|
571 |
|
|
572 |
QVERIFY(layout.itemAt(0, QFormLayout::LabelRole)->widget() == &lbl1);
|
|
573 |
QVERIFY(layout.itemAt(1, QFormLayout::LabelRole)->widget() == &lbl2);
|
|
574 |
QVERIFY(layout.itemAt(2, QFormLayout::LabelRole)->widget() == &lbl3);
|
|
575 |
QVERIFY(layout.itemAt(3, QFormLayout::LabelRole) == 0);
|
|
576 |
QVERIFY(layout.itemAt(4, QFormLayout::LabelRole) == 0);
|
|
577 |
QVERIFY(layout.itemAt(5, QFormLayout::LabelRole)->widget() == &lbl4);
|
|
578 |
|
|
579 |
QVERIFY(layout.itemAt(0, QFormLayout::FieldRole)->widget() == &fld1);
|
|
580 |
QVERIFY(layout.itemAt(1, QFormLayout::FieldRole)->widget() == &fld2);
|
|
581 |
QVERIFY(layout.itemAt(2, QFormLayout::FieldRole)->widget() == &fld3);
|
|
582 |
QVERIFY(layout.itemAt(3, QFormLayout::FieldRole) == 0);
|
|
583 |
QVERIFY(layout.itemAt(4, QFormLayout::FieldRole)->widget() == &fld4);
|
|
584 |
QVERIFY(layout.itemAt(5, QFormLayout::FieldRole) == 0);
|
|
585 |
}
|
|
586 |
|
|
587 |
void tst_QFormLayout::insertRow_QWidget_QLayout()
|
|
588 |
{
|
|
589 |
QFormLayout layout;
|
|
590 |
QLabel lbl1, lbl2, lbl3, lbl4;
|
|
591 |
QHBoxLayout fld1, fld2, fld3, fld4;
|
|
592 |
|
|
593 |
layout.insertRow(0, &lbl1, &fld1);
|
|
594 |
QCOMPARE(layout.rowCount(), 1);
|
|
595 |
|
|
596 |
{
|
|
597 |
int row = -1;
|
|
598 |
QFormLayout::ItemRole role = QFormLayout::ItemRole(-123);
|
|
599 |
layout.getWidgetPosition(&lbl1, &row, &role);
|
|
600 |
QCOMPARE(row, 0);
|
|
601 |
QCOMPARE(int(role), int(QFormLayout::LabelRole));
|
|
602 |
}
|
|
603 |
|
|
604 |
{
|
|
605 |
int row = -1;
|
|
606 |
QFormLayout::ItemRole role = QFormLayout::ItemRole(-123);
|
|
607 |
layout.getLayoutPosition(&fld1, &row, &role);
|
|
608 |
QCOMPARE(row, 0);
|
|
609 |
QCOMPARE(int(role), int(QFormLayout::FieldRole));
|
|
610 |
}
|
|
611 |
|
|
612 |
// check that negative values append
|
|
613 |
layout.insertRow(-2, &lbl2, &fld2);
|
|
614 |
QCOMPARE(layout.rowCount(), 2);
|
|
615 |
|
|
616 |
QVERIFY(layout.itemAt(0, QFormLayout::LabelRole)->widget() == &lbl1);
|
|
617 |
QVERIFY(layout.itemAt(1, QFormLayout::LabelRole)->widget() == &lbl2);
|
|
618 |
|
|
619 |
// check that too large values append
|
|
620 |
layout.insertRow(100, &lbl3, &fld3);
|
|
621 |
QCOMPARE(layout.rowCount(), 3);
|
|
622 |
|
|
623 |
QVERIFY(layout.itemAt(0, QFormLayout::LabelRole)->widget() == &lbl1);
|
|
624 |
QVERIFY(layout.itemAt(1, QFormLayout::LabelRole)->widget() == &lbl2);
|
|
625 |
QVERIFY(layout.itemAt(2, QFormLayout::LabelRole)->widget() == &lbl3);
|
|
626 |
|
|
627 |
QVERIFY(layout.itemAt(0, QFormLayout::FieldRole)->layout() == &fld1);
|
|
628 |
QVERIFY(layout.itemAt(1, QFormLayout::FieldRole)->layout() == &fld2);
|
|
629 |
QVERIFY(layout.itemAt(2, QFormLayout::FieldRole)->layout() == &fld3);
|
|
630 |
}
|
|
631 |
|
|
632 |
void tst_QFormLayout::insertRow_QString_QWidget()
|
|
633 |
{
|
|
634 |
QFormLayout layout;
|
|
635 |
QLineEdit fld1, fld2, fld3;
|
|
636 |
|
|
637 |
layout.insertRow(-5, "&Name:", &fld1);
|
|
638 |
QLabel *label1 = qobject_cast<QLabel *>(layout.itemAt(0, QFormLayout::LabelRole)->widget());
|
|
639 |
QVERIFY(label1 != 0);
|
|
640 |
QVERIFY(label1->buddy() == &fld1);
|
|
641 |
|
|
642 |
layout.insertRow(0, "&Email:", &fld2);
|
|
643 |
QLabel *label2 = qobject_cast<QLabel *>(layout.itemAt(0, QFormLayout::LabelRole)->widget());
|
|
644 |
QVERIFY(label2 != 0);
|
|
645 |
QVERIFY(label2->buddy() == &fld2);
|
|
646 |
|
|
647 |
layout.insertRow(5, "&Age:", &fld3);
|
|
648 |
QLabel *label3 = qobject_cast<QLabel *>(layout.itemAt(2, QFormLayout::LabelRole)->widget());
|
|
649 |
QVERIFY(label3 != 0);
|
|
650 |
QVERIFY(label3->buddy() == &fld3);
|
|
651 |
}
|
|
652 |
|
|
653 |
void tst_QFormLayout::insertRow_QString_QLayout()
|
|
654 |
{
|
|
655 |
QFormLayout layout;
|
|
656 |
QHBoxLayout fld1, fld2, fld3;
|
|
657 |
|
|
658 |
layout.insertRow(-5, "&Name:", &fld1);
|
|
659 |
QLabel *label1 = qobject_cast<QLabel *>(layout.itemAt(0, QFormLayout::LabelRole)->widget());
|
|
660 |
QVERIFY(label1 != 0);
|
|
661 |
QVERIFY(label1->buddy() == 0);
|
|
662 |
|
|
663 |
QCOMPARE(layout.rowCount(), 1);
|
|
664 |
|
|
665 |
layout.insertRow(0, "&Email:", &fld2);
|
|
666 |
QLabel *label2 = qobject_cast<QLabel *>(layout.itemAt(0, QFormLayout::LabelRole)->widget());
|
|
667 |
QVERIFY(label2 != 0);
|
|
668 |
QVERIFY(label2->buddy() == 0);
|
|
669 |
|
|
670 |
QCOMPARE(layout.rowCount(), 2);
|
|
671 |
|
|
672 |
layout.insertRow(5, "&Age:", &fld3);
|
|
673 |
QLabel *label3 = qobject_cast<QLabel *>(layout.itemAt(2, QFormLayout::LabelRole)->widget());
|
|
674 |
QVERIFY(label3 != 0);
|
|
675 |
QVERIFY(label3->buddy() == 0);
|
|
676 |
|
|
677 |
QCOMPARE(layout.rowCount(), 3);
|
|
678 |
}
|
|
679 |
|
|
680 |
void tst_QFormLayout::insertRow_QWidget()
|
|
681 |
{
|
|
682 |
// ### come back to this later
|
|
683 |
}
|
|
684 |
|
|
685 |
void tst_QFormLayout::insertRow_QLayout()
|
|
686 |
{
|
|
687 |
// ### come back to this later
|
|
688 |
}
|
|
689 |
|
|
690 |
void tst_QFormLayout::setWidget()
|
|
691 |
{
|
|
692 |
QFormLayout layout;
|
|
693 |
|
|
694 |
QWidget w1;
|
|
695 |
QWidget w2;
|
|
696 |
QWidget w3;
|
|
697 |
QWidget w4;
|
|
698 |
|
|
699 |
QCOMPARE(layout.count(), 0);
|
|
700 |
QCOMPARE(layout.rowCount(), 0);
|
|
701 |
|
|
702 |
layout.setWidget(5, QFormLayout::LabelRole, &w1);
|
|
703 |
QCOMPARE(layout.count(), 1);
|
|
704 |
QCOMPARE(layout.rowCount(), 6);
|
|
705 |
|
|
706 |
layout.setWidget(3, QFormLayout::FieldRole, &w2);
|
|
707 |
layout.setWidget(3, QFormLayout::LabelRole, &w3);
|
|
708 |
QCOMPARE(layout.count(), 3);
|
|
709 |
QCOMPARE(layout.rowCount(), 6);
|
|
710 |
|
|
711 |
// should be ignored and generate warnings
|
|
712 |
layout.setWidget(3, QFormLayout::FieldRole, &w4);
|
|
713 |
layout.setWidget(-1, QFormLayout::FieldRole, &w4);
|
|
714 |
|
|
715 |
{
|
|
716 |
int row = -1;
|
|
717 |
QFormLayout::ItemRole role = QFormLayout::ItemRole(-123);
|
|
718 |
layout.getWidgetPosition(&w1, &row, &role);
|
|
719 |
QCOMPARE(row, 5);
|
|
720 |
QCOMPARE(int(role), int(QFormLayout::LabelRole));
|
|
721 |
}
|
|
722 |
|
|
723 |
{
|
|
724 |
int row = -1;
|
|
725 |
QFormLayout::ItemRole role = QFormLayout::ItemRole(-123);
|
|
726 |
layout.getWidgetPosition(&w2, &row, &role);
|
|
727 |
QCOMPARE(row, 3);
|
|
728 |
QCOMPARE(int(role), int(QFormLayout::FieldRole));
|
|
729 |
}
|
|
730 |
|
|
731 |
{
|
|
732 |
int row = -1;
|
|
733 |
QFormLayout::ItemRole role = QFormLayout::ItemRole(-123);
|
|
734 |
layout.getWidgetPosition(&w3, &row, &role);
|
|
735 |
QCOMPARE(row, 3);
|
|
736 |
QCOMPARE(int(role), int(QFormLayout::LabelRole));
|
|
737 |
}
|
|
738 |
|
|
739 |
{
|
|
740 |
int row = -1;
|
|
741 |
QFormLayout::ItemRole role = QFormLayout::ItemRole(-123);
|
|
742 |
layout.getWidgetPosition(&w4, &row, &role);
|
|
743 |
QCOMPARE(row, -1);
|
|
744 |
QCOMPARE(int(role), -123);
|
|
745 |
}
|
|
746 |
|
|
747 |
{
|
|
748 |
int row = -1;
|
|
749 |
QFormLayout::ItemRole role = QFormLayout::ItemRole(-123);
|
|
750 |
layout.getWidgetPosition(0, &row, &role);
|
|
751 |
QCOMPARE(row, -1);
|
|
752 |
QCOMPARE(int(role), -123);
|
|
753 |
}
|
|
754 |
}
|
|
755 |
|
|
756 |
void tst_QFormLayout::setLayout()
|
|
757 |
{
|
|
758 |
QFormLayout layout;
|
|
759 |
|
|
760 |
QHBoxLayout l1;
|
|
761 |
QHBoxLayout l2;
|
|
762 |
QHBoxLayout l3;
|
|
763 |
QHBoxLayout l4;
|
|
764 |
|
|
765 |
QCOMPARE(layout.count(), 0);
|
|
766 |
QCOMPARE(layout.rowCount(), 0);
|
|
767 |
|
|
768 |
layout.setLayout(5, QFormLayout::LabelRole, &l1);
|
|
769 |
QCOMPARE(layout.count(), 1);
|
|
770 |
QCOMPARE(layout.rowCount(), 6);
|
|
771 |
|
|
772 |
layout.setLayout(3, QFormLayout::FieldRole, &l2);
|
|
773 |
layout.setLayout(3, QFormLayout::LabelRole, &l3);
|
|
774 |
QCOMPARE(layout.count(), 3);
|
|
775 |
QCOMPARE(layout.rowCount(), 6);
|
|
776 |
|
|
777 |
// should be ignored and generate warnings
|
|
778 |
layout.setLayout(3, QFormLayout::FieldRole, &l4);
|
|
779 |
layout.setLayout(-1, QFormLayout::FieldRole, &l4);
|
|
780 |
QCOMPARE(layout.count(), 3);
|
|
781 |
QCOMPARE(layout.rowCount(), 6);
|
|
782 |
|
|
783 |
{
|
|
784 |
int row = -1;
|
|
785 |
QFormLayout::ItemRole role = QFormLayout::ItemRole(-123);
|
|
786 |
layout.getLayoutPosition(&l1, &row, &role);
|
|
787 |
QCOMPARE(row, 5);
|
|
788 |
QCOMPARE(int(role), int(QFormLayout::LabelRole));
|
|
789 |
}
|
|
790 |
|
|
791 |
{
|
|
792 |
int row = -1;
|
|
793 |
QFormLayout::ItemRole role = QFormLayout::ItemRole(-123);
|
|
794 |
layout.getLayoutPosition(&l2, &row, &role);
|
|
795 |
QCOMPARE(row, 3);
|
|
796 |
QCOMPARE(int(role), int(QFormLayout::FieldRole));
|
|
797 |
}
|
|
798 |
|
|
799 |
{
|
|
800 |
int row = -1;
|
|
801 |
QFormLayout::ItemRole role = QFormLayout::ItemRole(-123);
|
|
802 |
layout.getLayoutPosition(&l3, &row, &role);
|
|
803 |
QCOMPARE(row, 3);
|
|
804 |
QCOMPARE(int(role), int(QFormLayout::LabelRole));
|
|
805 |
}
|
|
806 |
|
|
807 |
{
|
|
808 |
int row = -1;
|
|
809 |
QFormLayout::ItemRole role = QFormLayout::ItemRole(-123);
|
|
810 |
layout.getLayoutPosition(&l4, &row, &role);
|
|
811 |
QCOMPARE(row, -1);
|
|
812 |
QCOMPARE(int(role), -123);
|
|
813 |
}
|
|
814 |
|
|
815 |
{
|
|
816 |
int row = -1;
|
|
817 |
QFormLayout::ItemRole role = QFormLayout::ItemRole(-123);
|
|
818 |
layout.getLayoutPosition(0, &row, &role);
|
|
819 |
QCOMPARE(row, -1);
|
|
820 |
QCOMPARE(int(role), -123);
|
|
821 |
}
|
|
822 |
}
|
|
823 |
|
|
824 |
void tst_QFormLayout::itemAt()
|
|
825 |
{
|
|
826 |
QFormLayout layout;
|
|
827 |
|
|
828 |
QWidget w1;
|
|
829 |
QWidget w2;
|
|
830 |
QWidget w3;
|
|
831 |
QWidget w4;
|
|
832 |
QWidget w5;
|
|
833 |
QHBoxLayout l6;
|
|
834 |
|
|
835 |
layout.setWidget(5, QFormLayout::LabelRole, &w1);
|
|
836 |
layout.setWidget(3, QFormLayout::FieldRole, &w2);
|
|
837 |
layout.setWidget(3, QFormLayout::LabelRole, &w3);
|
|
838 |
layout.addRow(&w4, &w5);
|
|
839 |
layout.addRow("Foo:", &l6);
|
|
840 |
|
|
841 |
QCOMPARE(layout.count(), 7);
|
|
842 |
|
|
843 |
QBitArray scoreBoard(7);
|
|
844 |
for (int i = 0; i < 7; ++i) {
|
|
845 |
QLayoutItem *item = layout.itemAt(i);
|
|
846 |
QVERIFY(item != 0);
|
|
847 |
|
|
848 |
if (item->widget() == &w1) {
|
|
849 |
scoreBoard[0] = true;
|
|
850 |
} else if (item->widget() == &w2) {
|
|
851 |
scoreBoard[1] = true;
|
|
852 |
} else if (item->widget() == &w3) {
|
|
853 |
scoreBoard[2] = true;
|
|
854 |
} else if (item->widget() == &w4) {
|
|
855 |
scoreBoard[3] = true;
|
|
856 |
} else if (item->widget() == &w5) {
|
|
857 |
scoreBoard[4] = true;
|
|
858 |
} else if (item->layout() == &l6) {
|
|
859 |
scoreBoard[5] = true;
|
|
860 |
} else if (qobject_cast<QLabel *>(item->widget())) {
|
|
861 |
scoreBoard[6] = true;
|
|
862 |
}
|
|
863 |
}
|
|
864 |
QCOMPARE(scoreBoard.count(false), 0);
|
|
865 |
}
|
|
866 |
|
|
867 |
void tst_QFormLayout::takeAt()
|
|
868 |
{
|
|
869 |
QFormLayout layout;
|
|
870 |
|
|
871 |
QWidget w1;
|
|
872 |
QWidget w2;
|
|
873 |
QWidget w3;
|
|
874 |
QWidget w4;
|
|
875 |
QWidget w5;
|
|
876 |
QHBoxLayout l6;
|
|
877 |
|
|
878 |
layout.setWidget(5, QFormLayout::LabelRole, &w1);
|
|
879 |
layout.setWidget(3, QFormLayout::FieldRole, &w2);
|
|
880 |
layout.setWidget(3, QFormLayout::LabelRole, &w3);
|
|
881 |
layout.addRow(&w4, &w5);
|
|
882 |
layout.addRow("Foo:", &l6);
|
|
883 |
|
|
884 |
QCOMPARE(layout.count(), 7);
|
|
885 |
|
|
886 |
for (int i = 6; i >= 0; --i) {
|
|
887 |
layout.takeAt(0);
|
|
888 |
QCOMPARE(layout.count(), i);
|
|
889 |
}
|
|
890 |
}
|
|
891 |
|
|
892 |
void tst_QFormLayout::layoutAlone()
|
|
893 |
{
|
|
894 |
QWidget w;
|
|
895 |
QFormLayout layout;
|
|
896 |
layout.setFieldGrowthPolicy(QFormLayout::AllNonFixedFieldsGrow);
|
|
897 |
w.setLayout(&layout);
|
|
898 |
QLabel label("Here is a strange test case");
|
|
899 |
layout.setWidget(0, QFormLayout::LabelRole, &label);
|
|
900 |
QHBoxLayout hlay;
|
|
901 |
layout.setLayout(1, QFormLayout::LabelRole, &hlay);
|
|
902 |
QCOMPARE(layout.count(), 2);
|
|
903 |
w.show();
|
|
904 |
layout.activate();
|
|
905 |
QTest::qWait(500);
|
|
906 |
}
|
|
907 |
|
|
908 |
QTEST_MAIN(tst_QFormLayout)
|
|
909 |
|
|
910 |
#include "tst_qformlayout.moc"
|