1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the test suite of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:GPL-EXCEPT$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU
19** General Public License version 3 as published by the Free Software
20** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21** included in the packaging of this file. Please review the following
22** information to ensure the GNU General Public License requirements will
23** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24**
25** $QT_END_LICENSE$
26**
27****************************************************************************/
28
29
30#include <QtTest/QtTest>
31#include <qgraphicsgridlayout.h>
32#include <qgraphicswidget.h>
33#include <qgraphicsscene.h>
34#include <qgraphicsview.h>
35
36class tst_QGraphicsGridLayout : public QObject
37{
38 Q_OBJECT
39
40private slots:
41 void qgraphicsgridlayout_data();
42 void qgraphicsgridlayout();
43 void addItem_data();
44 void addItem();
45 void alignment_data();
46 void alignment();
47 void alignment2();
48 void alignment2_data();
49 void columnAlignment_data();
50 void columnAlignment();
51 void columnCount_data();
52 void columnCount();
53 void columnMaximumWidth_data();
54 void columnMaximumWidth();
55 void columnMinimumWidth_data();
56 void columnMinimumWidth();
57 void columnPreferredWidth_data();
58 void columnPreferredWidth();
59 void setColumnFixedWidth();
60 void columnSpacing();
61 void columnStretchFactor();
62 void count();
63 void contentsMargins();
64 void horizontalSpacing_data();
65 void horizontalSpacing();
66 void itemAt();
67 void removeAt();
68 void removeItem();
69 void rowAlignment_data();
70 void rowAlignment();
71 void rowCount_data();
72 void rowCount();
73 void rowMaximumHeight_data();
74 void rowMaximumHeight();
75 void rowMinimumHeight_data();
76 void rowMinimumHeight();
77 void rowPreferredHeight_data();
78 void rowPreferredHeight();
79 void rowSpacing();
80 void rowStretchFactor_data();
81 void rowStretchFactor();
82 void setColumnSpacing_data();
83 void setColumnSpacing();
84 void setGeometry_data();
85 void setGeometry();
86 void setRowFixedHeight();
87 void setRowSpacing_data();
88 void setRowSpacing();
89 void setSpacing_data();
90 void setSpacing();
91 void sizeHint_data();
92 void sizeHint();
93 void verticalSpacing_data();
94 void verticalSpacing();
95 void layoutDirection_data();
96 void layoutDirection();
97 void removeLayout();
98 void defaultStretchFactors_data();
99 void defaultStretchFactors();
100 void geometries_data();
101 void geometries();
102 void avoidRecursionInInsertItem();
103 void styleInfoLeak();
104 void task236367_maxSizeHint();
105 void spanningItem2x2_data();
106 void spanningItem2x2();
107 void spanningItem2x3_data();
108 void spanningItem2x3();
109 void spanningItem();
110 void spanAcrossEmptyRow();
111 void heightForWidth();
112 void widthForHeight();
113 void heightForWidthWithSpanning();
114 void stretchAndHeightForWidth();
115 void testDefaultAlignment();
116 void hiddenItems();
117};
118
119class RectWidget : public QGraphicsWidget
120{
121public:
122 RectWidget(QGraphicsItem *parent = 0) : QGraphicsWidget(parent), m_fnConstraint(0) {}
123
124 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
125 {
126 Q_UNUSED(option);
127 Q_UNUSED(widget);
128 painter->drawRoundedRect(rect: rect(), xRadius: 25, yRadius: 25, mode: Qt::RelativeSize);
129 painter->drawLine(p1: rect().topLeft(), p2: rect().bottomRight());
130 painter->drawLine(p1: rect().bottomLeft(), p2: rect().topRight());
131 }
132
133 QSizeF sizeHint(Qt::SizeHint which, const QSizeF &constraint = QSizeF()) const
134 {
135 if (constraint.width() < 0 && constraint.height() < 0 && m_sizeHints[which].isValid()) {
136 return m_sizeHints[which];
137 }
138 if (m_fnConstraint) {
139 return m_fnConstraint(which, constraint);
140 }
141 return QGraphicsWidget::sizeHint(which, constraint);
142 }
143
144 void setSizeHint(Qt::SizeHint which, const QSizeF &size) {
145 m_sizeHints[which] = size;
146 updateGeometry();
147 }
148
149 void setConstraintFunction(QSizeF (*fnConstraint)(Qt::SizeHint, const QSizeF &)) {
150 m_fnConstraint = fnConstraint;
151 }
152
153 // Initializer {} is a workaround for gcc bug 68949
154 QSizeF m_sizeHints[Qt::NSizeHints] {};
155 QSizeF (*m_fnConstraint)(Qt::SizeHint, const QSizeF &);
156
157};
158
159struct ItemDesc
160{
161 ItemDesc(int row, int col)
162 : m_pos(qMakePair(x: row, y: col))
163 {
164 }
165
166 ItemDesc &rowSpan(int span) {
167 m_rowSpan = span;
168 return (*this);
169 }
170
171 ItemDesc &colSpan(int span) {
172 m_colSpan = span;
173 return (*this);
174 }
175
176 ItemDesc &sizePolicy(const QSizePolicy &sp) {
177 m_sizePolicy = sp;
178 return (*this);
179 }
180
181 ItemDesc &sizePolicy(QSizePolicy::Policy horAndVer) {
182 m_sizePolicy = QSizePolicy(horAndVer, horAndVer);
183 return (*this);
184 }
185
186 ItemDesc &sizePolicyH(QSizePolicy::Policy hor) {
187 m_sizePolicy.setHorizontalPolicy(hor);
188 return (*this);
189 }
190
191 ItemDesc &sizePolicyV(QSizePolicy::Policy ver) {
192 m_sizePolicy.setVerticalPolicy(ver);
193 return (*this);
194 }
195
196 ItemDesc &sizePolicy(QSizePolicy::Policy hor, QSizePolicy::Policy ver) {
197 m_sizePolicy = QSizePolicy(hor, ver);
198 return (*this);
199 }
200
201 ItemDesc &sizeHint(Qt::SizeHint which, const QSizeF &sh) {
202 m_sizeHints[which] = sh;
203 return (*this);
204 }
205
206 ItemDesc &preferredSizeHint(const QSizeF &sh) {
207 m_sizeHints[Qt::PreferredSize] = sh;
208 return (*this);
209 }
210
211 ItemDesc &minSize(const QSizeF &sz) {
212 m_sizes[Qt::MinimumSize] = sz;
213 return (*this);
214 }
215 ItemDesc &preferredSize(const QSizeF &sz) {
216 m_sizes[Qt::PreferredSize] = sz;
217 return (*this);
218 }
219 ItemDesc &maxSize(const QSizeF &sz) {
220 m_sizes[Qt::MaximumSize] = sz;
221 return (*this);
222 }
223
224 ItemDesc &alignment(Qt::Alignment alignment) {
225 m_align = alignment;
226 return (*this);
227 }
228
229 ItemDesc &dynamicConstraint(QSizeF (*fnConstraint)(Qt::SizeHint, const QSizeF &),
230 Qt::Orientation orientation) {
231 m_fnConstraint = fnConstraint;
232 m_constraintOrientation = orientation;
233 return (*this);
234 }
235
236 void apply(QGraphicsGridLayout *layout, QGraphicsWidget *item) {
237 QSizePolicy sp = m_sizePolicy;
238 if (m_fnConstraint) {
239 sp.setHeightForWidth(m_constraintOrientation == Qt::Vertical);
240 sp.setWidthForHeight(m_constraintOrientation == Qt::Horizontal);
241 }
242
243 item->setSizePolicy(sp);
244 for (int i = 0; i < Qt::NSizeHints; ++i) {
245 if (!m_sizes[i].isValid())
246 continue;
247 switch ((Qt::SizeHint)i) {
248 case Qt::MinimumSize:
249 item->setMinimumSize(m_sizes[i]);
250 break;
251 case Qt::PreferredSize:
252 item->setPreferredSize(m_sizes[i]);
253 break;
254 case Qt::MaximumSize:
255 item->setMaximumSize(m_sizes[i]);
256 break;
257 default:
258 qWarning(msg: "not implemented");
259 break;
260 }
261 }
262
263 layout->addItem(item, row: m_pos.first, column: m_pos.second, rowSpan: m_rowSpan, columnSpan: m_colSpan);
264 layout->setAlignment(item, alignment: m_align);
265 }
266
267 void apply(QGraphicsGridLayout *layout, RectWidget *item) {
268 for (int i = 0; i < Qt::NSizeHints; ++i)
269 item->setSizeHint(which: (Qt::SizeHint)i, size: m_sizeHints[i]);
270 item->setConstraintFunction(m_fnConstraint);
271 apply(layout, item: static_cast<QGraphicsWidget*>(item));
272 }
273
274//private:
275 QPair<int,int> m_pos; // row,col
276 int m_rowSpan = 1;
277 int m_colSpan = 1;
278 QSizePolicy m_sizePolicy{QSizePolicy::Preferred, QSizePolicy::Preferred};
279
280 // Initializer {} is a workaround for gcc bug 68949
281 QSizeF m_sizeHints[Qt::NSizeHints] {};
282 QSizeF m_sizes[Qt::NSizeHints] {};
283 Qt::Alignment m_align;
284
285 Qt::Orientation m_constraintOrientation = Qt::Horizontal;
286 QSizeF (*m_fnConstraint)(Qt::SizeHint, const QSizeF &) = nullptr;
287};
288
289typedef QList<ItemDesc> ItemList;
290Q_DECLARE_METATYPE(ItemList);
291
292typedef QList<QSizeF> SizeList;
293
294void tst_QGraphicsGridLayout::qgraphicsgridlayout_data()
295{
296}
297
298void tst_QGraphicsGridLayout::qgraphicsgridlayout()
299{
300 QGraphicsGridLayout layout;
301 QTest::ignoreMessage(type: QtWarningMsg, message: "QGraphicsGridLayout::addItem: invalid row span/column span: 0");
302 layout.addItem(item: 0, row: 0, column: 0, rowSpan: 0, columnSpan: 0);
303 QTest::ignoreMessage(type: QtWarningMsg, message: "QGraphicsGridLayout::addItem: cannot add null item");
304 layout.addItem(aitem: 0, arow: 0, acolumn: 0);
305 layout.alignment(item: 0);
306 layout.columnAlignment(column: 0);
307 layout.columnCount();
308 layout.columnMaximumWidth(column: 0);
309 layout.columnMinimumWidth(column: 0);
310 layout.columnPreferredWidth(column: 0);
311 layout.columnSpacing(column: 0);
312 layout.columnStretchFactor(column: 0);
313 layout.count();
314 layout.horizontalSpacing();
315 QTest::ignoreMessage(type: QtWarningMsg, message: "QGraphicsGridLayout::itemAt: invalid row, column 0, 0");
316 layout.itemAt(row: 0, column: 0);
317 QTest::ignoreMessage(type: QtWarningMsg, message: "QGraphicsGridLayout::itemAt: invalid index 0");
318 layout.itemAt(index: 0);
319 QTest::ignoreMessage(type: QtWarningMsg, message: "QGraphicsGridLayout::removeAt: invalid index 0");
320 layout.removeAt(index: 0);
321 layout.rowAlignment(row: 0);
322 layout.rowCount();
323 layout.rowMaximumHeight(row: 0);
324 layout.rowMinimumHeight(row: 0);
325 layout.rowPreferredHeight(row: 0);
326 layout.rowSpacing(row: 0);
327 layout.rowStretchFactor(row: 0);
328 layout.setAlignment(item: 0, alignment: Qt::AlignRight);
329 layout.setColumnAlignment(column: 0, alignment: Qt::AlignRight);
330 layout.setColumnFixedWidth(column: 0, width: 0);
331 layout.setColumnMaximumWidth(column: 0, width: 0);
332 layout.setColumnMinimumWidth(column: 0, width: 0);
333 layout.setColumnPreferredWidth(column: 0, width: 0);
334 layout.setColumnSpacing(column: 0, spacing: 0);
335 layout.setColumnStretchFactor(column: 0, stretch: 0);
336 layout.setGeometry(QRectF());
337 layout.setHorizontalSpacing(0);
338 layout.setRowAlignment(row: 0, alignment: { });
339 layout.setRowFixedHeight(row: 0, height: 0);
340 layout.setRowMaximumHeight(row: 0, height: 0);
341 layout.setRowMinimumHeight(row: 0, height: 0);
342 layout.setRowPreferredHeight(row: 0, height: 0);
343 layout.setRowSpacing(row: 0, spacing: 0);
344 layout.setRowStretchFactor(row: 0, stretch: 0);
345 layout.setSpacing(0);
346 layout.setVerticalSpacing(0);
347 layout.sizeHint(which: Qt::MinimumSize);
348 layout.verticalSpacing();
349}
350
351static void populateLayout(QGraphicsGridLayout *gridLayout, int width, int height, bool hasHeightForWidth = false)
352{
353 for (int y = 0; y < height; ++y) {
354 for (int x = 0; x < width; ++x) {
355 QGraphicsWidget *item = new RectWidget();
356 item->setMinimumSize(aw: 10, ah: 10);
357 item->setPreferredSize(aw: 25, ah: 25);
358 item->setMaximumSize(aw: 50, ah: 50);
359 gridLayout->addItem(aitem: item, arow: y, acolumn: x);
360 QSizePolicy policy = item->sizePolicy();
361 policy.setHeightForWidth(hasHeightForWidth);
362 item->setSizePolicy(policy);
363 }
364 }
365}
366
367
368/** populates \a gridLayout with a 3x2 layout:
369 * +----+----+----+
370 * |+---|---+|xxxx|
371 * ||span=2 ||hole|
372 * |+---|---+|xxxx|
373 * +----+----+----+
374 * |xxxx|+---|---+|
375 * |hole||span=2 ||
376 * |xxxx|+---|---+|
377 * +----+----+----+
378 */
379static void populateLayoutWithSpansAndHoles(QGraphicsGridLayout *gridLayout, bool hasHeightForWidth = false)
380{
381 QGraphicsWidget *item = new RectWidget();
382 item->setMinimumSize(aw: 10, ah: 10);
383 item->setPreferredSize(aw: 25, ah: 25);
384 item->setMaximumSize(aw: 50, ah: 50);
385 QSizePolicy sizepolicy = item->sizePolicy();
386 sizepolicy.setHeightForWidth(hasHeightForWidth);
387 item->setSizePolicy(sizepolicy);
388 gridLayout->addItem(item, row: 0, column: 0, rowSpan: 1, columnSpan: 2);
389
390 item = new RectWidget();
391 item->setMinimumSize(aw: 10, ah: 10);
392 item->setPreferredSize(aw: 25, ah: 25);
393 item->setMaximumSize(aw: 50, ah: 50);
394 item->setSizePolicy(sizepolicy);
395 gridLayout->addItem(item, row: 1, column: 1, rowSpan: 1, columnSpan: 2);
396}
397
398Q_DECLARE_METATYPE(Qt::Alignment)
399void tst_QGraphicsGridLayout::addItem_data()
400{
401 QTest::addColumn<int>(name: "row");
402 QTest::addColumn<int>(name: "column");
403 QTest::addColumn<int>(name: "rowSpan");
404 QTest::addColumn<int>(name: "columnSpan");
405 QTest::addColumn<Qt::Alignment>(name: "alignment");
406
407 for (int a = -1; a < 3; ++a) {
408 for (int b = -1; b < 2; ++b) {
409 for (int c = -1; c < 2; ++c) {
410 for (int d = -1; d < 2; ++d) {
411 int row = a;
412 int column = b;
413 int rowSpan = c;
414 int columnSpan = d;
415 const QByteArray name = '(' + QByteArray::number(a) + ',' + QByteArray::number(b)
416 + ',' + QByteArray::number(c) + ',' + QByteArray::number(d);
417 Qt::Alignment alignment = Qt::AlignLeft;
418 QTest::newRow(dataTag: name.constData()) << row << column << rowSpan << columnSpan << alignment;
419 }}}}
420}
421
422// public void addItem(QGraphicsLayoutItem* item, int row, int column, int rowSpan, int columnSpan, Qt::Alignment alignment = 0)
423void tst_QGraphicsGridLayout::addItem()
424{
425 QFETCH(int, row);
426 QFETCH(int, column);
427 QFETCH(int, rowSpan);
428 QFETCH(int, columnSpan);
429 QFETCH(Qt::Alignment, alignment);
430
431 QGraphicsGridLayout *layout = new QGraphicsGridLayout;
432
433 QGraphicsWidget *wid = new QGraphicsWidget;
434 if (row < 0 || column < 0) {
435 QTest::ignoreMessage(type: QtWarningMsg, message: "QGraphicsGridLayout::addItem: invalid row/column: -1");
436 } else if (rowSpan < 1 || columnSpan < 1) {
437 char buf[1024];
438 ::qsnprintf(str: buf, n: sizeof(buf), fmt: "QGraphicsGridLayout::addItem: invalid row span/column span: %d",
439 rowSpan < 1 ? rowSpan : columnSpan);
440 QTest::ignoreMessage(type: QtWarningMsg, message: buf);
441 }
442 layout->addItem(item: wid, row, column, rowSpan, columnSpan, alignment);
443
444 delete layout;
445}
446
447void tst_QGraphicsGridLayout::alignment_data()
448{
449 QTest::addColumn<bool>(name: "hasHeightForWidth");
450
451 QTest::newRow(dataTag: "") << false;
452 QTest::newRow(dataTag: "hasHeightForWidth") << true;
453}
454
455// public Qt::Alignment alignment(QGraphicsLayoutItem* item) const
456void tst_QGraphicsGridLayout::alignment()
457{
458#ifdef Q_OS_MAC
459 QSKIP("Resizing a QGraphicsWidget to effectiveSizeHint(Qt::MaximumSize) is currently not supported on mac");
460#endif
461 QFETCH(bool, hasHeightForWidth);
462 QGraphicsScene scene;
463 QGraphicsView view(&scene);
464 QGraphicsWidget *widget = new QGraphicsWidget(0, Qt::Window);
465 QGraphicsGridLayout *layout = new QGraphicsGridLayout();
466 scene.addItem(item: widget);
467 widget->setLayout(layout);
468 populateLayout(gridLayout: layout, width: 3, height: 2, hasHeightForWidth);
469 layout->setContentsMargins(left: 0, top: 0, right: 0, bottom: 0);
470 layout->setSpacing(0);
471
472 view.show();
473 widget->show();
474 widget->resize(size: widget->effectiveSizeHint(which: Qt::PreferredSize));
475 QApplication::processEvents();
476 // no alignment (the default)
477 QCOMPARE(layout->itemAt(0, 0)->geometry().left(), 0.0);
478 QCOMPARE(layout->itemAt(0, 0)->geometry().right(), layout->itemAt(0, 1)->geometry().left());
479 QCOMPARE(layout->itemAt(0, 1)->geometry().left(), 25.0);
480 QCOMPARE(layout->itemAt(0, 1)->geometry().right(), layout->itemAt(0, 2)->geometry().left());
481 QCOMPARE(layout->itemAt(0, 2)->geometry().left(), 50.0);
482 QCOMPARE(layout->itemAt(0, 2)->geometry().right(), 75.0);
483
484 QCOMPARE(layout->itemAt(1, 0)->geometry().left(), 0.0);
485 QCOMPARE(layout->itemAt(1, 0)->geometry().right(), layout->itemAt(1, 1)->geometry().left());
486 QCOMPARE(layout->itemAt(1, 1)->geometry().left(), 25.0);
487 QCOMPARE(layout->itemAt(1, 1)->geometry().right(), layout->itemAt(1, 2)->geometry().left());
488 QCOMPARE(layout->itemAt(1, 2)->geometry().left(), 50.0);
489 QCOMPARE(layout->itemAt(1, 2)->geometry().right(), 75.0);
490
491 QCOMPARE(layout->itemAt(0, 0)->geometry().top(), 0.0);
492 QCOMPARE(layout->itemAt(0, 0)->geometry().bottom(), layout->itemAt(1, 0)->geometry().top());
493 QCOMPARE(layout->itemAt(1, 0)->geometry().top(), 25.0);
494 QCOMPARE(layout->itemAt(1, 0)->geometry().bottom(), 50.0);
495
496 // align first column left, second hcenter, third right
497 layout->setColumnMinimumWidth(column: 0, width: 100);
498 layout->setAlignment(item: layout->itemAt(row: 0,column: 0), alignment: Qt::AlignLeft);
499 layout->setAlignment(item: layout->itemAt(row: 1,column: 0), alignment: Qt::AlignLeft);
500 layout->setColumnMinimumWidth(column: 1, width: 100);
501 layout->setAlignment(item: layout->itemAt(row: 0,column: 1), alignment: Qt::AlignHCenter);
502 layout->setAlignment(item: layout->itemAt(row: 1,column: 1), alignment: Qt::AlignHCenter);
503 layout->setColumnMinimumWidth(column: 2, width: 100);
504 layout->setAlignment(item: layout->itemAt(row: 0,column: 2), alignment: Qt::AlignRight);
505 layout->setAlignment(item: layout->itemAt(row: 1,column: 2), alignment: Qt::AlignRight);
506
507 widget->resize(size: widget->effectiveSizeHint(which: Qt::MaximumSize));
508 QApplication::processEvents();
509
510 QCOMPARE(layout->itemAt(0,0)->geometry(), QRectF(0, 0, 50, 50));
511 QCOMPARE(layout->itemAt(1,0)->geometry(), QRectF(0, 50, 50, 50));
512 QCOMPARE(layout->itemAt(0,1)->geometry(), QRectF(125, 0, 50, 50));
513 QCOMPARE(layout->itemAt(1,1)->geometry(), QRectF(125, 50, 50, 50));
514 QCOMPARE(layout->itemAt(0,2)->geometry(), QRectF(250, 0, 50, 50));
515 QCOMPARE(layout->itemAt(1,2)->geometry(), QRectF(250, 50, 50, 50));
516
517 delete widget;
518}
519
520void tst_QGraphicsGridLayout::columnAlignment_data()
521{
522 QTest::addColumn<bool>(name: "hasHeightForWidth");
523
524 QTest::newRow(dataTag: "") << false;
525 QTest::newRow(dataTag: "hasHeightForWidth") << true;
526}
527
528// public void setColumnAlignment(int column, Qt::Alignment alignment)
529// public Qt::Alignment columnAlignment(int column) const
530void tst_QGraphicsGridLayout::columnAlignment()
531{
532#ifdef Q_OS_MAC
533 QSKIP("Resizing a QGraphicsWidget to effectiveSizeHint(Qt::MaximumSize) is currently not supported on mac");
534#endif
535 QFETCH(bool, hasHeightForWidth);
536 QGraphicsScene scene;
537 QGraphicsView view(&scene);
538 QGraphicsWidget *widget = new QGraphicsWidget(0, Qt::Window);
539 QGraphicsGridLayout *layout = new QGraphicsGridLayout();
540 scene.addItem(item: widget);
541 widget->setLayout(layout);
542 populateLayout(gridLayout: layout, width: 3, height: 2, hasHeightForWidth);
543 layout->setContentsMargins(left: 0, top: 0, right: 0, bottom: 0);
544 layout->setSpacing(1);
545 widget->setContentsMargins(left: 0, top: 0, right: 0, bottom: 0);
546
547 layout->setColumnMinimumWidth(column: 0, width: 100);
548 layout->setColumnMinimumWidth(column: 1, width: 100);
549 layout->setColumnMinimumWidth(column: 2, width: 100);
550
551 view.resize(w: 450,h: 150);
552 widget->resize(size: widget->effectiveSizeHint(which: Qt::MaximumSize));
553 view.show();
554 widget->show();
555 QApplication::sendPostedEvents(receiver: 0, event_type: 0);
556 // Check default
557 QCOMPARE(layout->columnAlignment(0), 0);
558 QCOMPARE(layout->columnAlignment(1), 0);
559 QCOMPARE(layout->columnAlignment(2), 0);
560
561 layout->setColumnAlignment(column: 0, alignment: Qt::AlignLeft);
562 layout->setColumnAlignment(column: 1, alignment: Qt::AlignHCenter);
563 layout->setColumnAlignment(column: 2, alignment: Qt::AlignRight);
564
565 // see if item alignment takes preference over columnAlignment
566 layout->setAlignment(item: layout->itemAt(row: 1,column: 0), alignment: Qt::AlignHCenter);
567 layout->setAlignment(item: layout->itemAt(row: 1,column: 1), alignment: Qt::AlignRight);
568 layout->setAlignment(item: layout->itemAt(row: 1,column: 2), alignment: Qt::AlignLeft);
569
570 QApplication::sendPostedEvents(receiver: 0, event_type: 0); // process LayoutRequest
571 /*
572 +----------+------------+---------+
573 | Left | HCenter | Right |
574 +----------+------------+---------+
575 | HCenter | Right | Left |
576 +---------------------------------+
577 */
578 QCOMPARE(layout->itemAt(0,0)->geometry(), QRectF(0, 0, 50, 50));
579 QCOMPARE(layout->itemAt(1,0)->geometry(), QRectF(25, 51, 50, 50)); // item is king
580 QCOMPARE(layout->itemAt(0,1)->geometry(), QRectF(126, 0, 50, 50));
581 QCOMPARE(layout->itemAt(1,1)->geometry(), QRectF(151, 51, 50, 50)); // item is king
582 QCOMPARE(layout->itemAt(0,2)->geometry(), QRectF(252, 0, 50, 50));
583 QCOMPARE(layout->itemAt(1,2)->geometry(), QRectF(202, 51, 50, 50)); // item is king
584
585 delete widget;
586}
587
588void tst_QGraphicsGridLayout::columnCount_data()
589{
590 QTest::addColumn<bool>(name: "hasHeightForWidth");
591
592 QTest::newRow(dataTag: "") << false;
593 QTest::newRow(dataTag: "hasHeightForWidth") << true;
594}
595// public int columnCount() const
596void tst_QGraphicsGridLayout::columnCount()
597{
598 QFETCH(bool, hasHeightForWidth);
599 QGraphicsScene scene;
600 QGraphicsView view(&scene);
601 QGraphicsWidget *widget = new QGraphicsWidget(0, Qt::Window);
602 QGraphicsGridLayout *layout = new QGraphicsGridLayout();
603 scene.addItem(item: widget);
604 widget->setLayout(layout);
605 layout->setContentsMargins(left: 0, top: 0, right: 0, bottom: 0);
606 layout->setSpacing(0);
607 widget->setContentsMargins(left: 0, top: 0, right: 0, bottom: 0);
608
609 view.show();
610 widget->show();
611 QApplication::processEvents();
612
613 QCOMPARE(layout->columnCount(), 0);
614 layout->addItem(aitem: new RectWidget(widget), arow: 0, acolumn: 0);
615 QCOMPARE(layout->columnCount(), 1);
616 layout->addItem(aitem: new RectWidget(widget), arow: 1, acolumn: 1);
617 QCOMPARE(layout->columnCount(), 2);
618 layout->addItem(aitem: new RectWidget(widget), arow: 0, acolumn: 2);
619 QCOMPARE(layout->columnCount(), 3);
620 layout->addItem(aitem: new RectWidget(widget), arow: 1, acolumn: 0);
621 QCOMPARE(layout->columnCount(), 3);
622 layout->addItem(aitem: new RectWidget(widget), arow: 0, acolumn: 1);
623 QCOMPARE(layout->columnCount(), 3);
624 layout->addItem(aitem: new RectWidget(widget), arow: 1, acolumn: 2);
625 QCOMPARE(layout->columnCount(), 3);
626
627 // ### Talk with Jasmin. Not sure if removeAt() should adjust columnCount().
628 widget->setLayout(0);
629 layout = new QGraphicsGridLayout();
630 populateLayout(gridLayout: layout, width: 3, height: 2, hasHeightForWidth);
631 QCOMPARE(layout->columnCount(), 3);
632 layout->removeAt(index: 5);
633 layout->removeAt(index: 3);
634 QCOMPARE(layout->columnCount(), 3);
635 layout->removeAt(index: 1);
636 QCOMPARE(layout->columnCount(), 3);
637 layout->removeAt(index: 0);
638 QCOMPARE(layout->columnCount(), 3);
639 layout->removeAt(index: 0);
640 QCOMPARE(layout->columnCount(), 2);
641
642 delete widget;
643}
644
645void tst_QGraphicsGridLayout::columnMaximumWidth_data()
646{
647 QTest::addColumn<bool>(name: "hasHeightForWidth");
648
649 QTest::newRow(dataTag: "") << false;
650 QTest::newRow(dataTag: "hasHeightForWidth") << true;
651}
652// public qreal columnMaximumWidth(int column) const
653void tst_QGraphicsGridLayout::columnMaximumWidth()
654{
655 QFETCH(bool, hasHeightForWidth);
656 QGraphicsScene scene;
657 QGraphicsView view(&scene);
658 QGraphicsWidget *widget = new QGraphicsWidget(0, Qt::Window);
659 QGraphicsGridLayout *layout = new QGraphicsGridLayout();
660 scene.addItem(item: widget);
661 widget->setLayout(layout);
662 populateLayout(gridLayout: layout, width: 3, height: 2, hasHeightForWidth);
663 layout->setContentsMargins(left: 0, top: 0, right: 0, bottom: 0);
664 layout->setSpacing(0);
665
666 QCOMPARE(layout->minimumSize(), QSizeF(10+10+10, 10+10));
667 QCOMPARE(layout->preferredSize(), QSizeF(25+25+25, 25+25));
668 QCOMPARE(layout->maximumSize(), QSizeF(50+50+50, 50+50));
669
670 // should at least be a very large number
671 QVERIFY(layout->columnMaximumWidth(0) >= 10000);
672 QCOMPARE(layout->columnMaximumWidth(0), layout->columnMaximumWidth(1));
673 QCOMPARE(layout->columnMaximumWidth(1), layout->columnMaximumWidth(2));
674 layout->setColumnMaximumWidth(column: 0, width: 20);
675 layout->setColumnMaximumWidth(column: 2, width: 60);
676
677 QCOMPARE(layout->minimumSize(), QSizeF(10+10+10, 10+10));
678 QCOMPARE(layout->preferredSize(), QSizeF(20+25+25, 25+25));
679 QCOMPARE(layout->maximumSize(), QSizeF(20+50+60, 50+50));
680 QCOMPARE(layout->maximumSize(), widget->maximumSize());
681
682 widget->resize(size: widget->effectiveSizeHint(which: Qt::PreferredSize));
683 layout->activate();
684
685 QCOMPARE(layout->itemAt(0,0)->geometry(), QRectF(0, 0, 20, 25));
686 QCOMPARE(layout->itemAt(1,0)->geometry(), QRectF(0, 25, 20, 25));
687 QCOMPARE(layout->itemAt(0,1)->geometry(), QRectF(20, 0, 25, 25));
688 QCOMPARE(layout->itemAt(1,1)->geometry(), QRectF(20, 25, 25, 25));
689 QCOMPARE(layout->itemAt(0,2)->geometry(), QRectF(45, 0, 25, 25));
690 QCOMPARE(layout->itemAt(1,2)->geometry(), QRectF(45, 25, 25, 25));
691
692 layout->setColumnAlignment(column: 2, alignment: Qt::AlignCenter);
693 widget->resize(size: widget->effectiveSizeHint(which: Qt::MaximumSize));
694 layout->activate();
695 QCOMPARE(layout->geometry(), QRectF(0,0,20+50+60, 50+50));
696 QCOMPARE(layout->itemAt(0,0)->geometry(), QRectF(0, 0, 20, 50));
697 QCOMPARE(layout->itemAt(1,0)->geometry(), QRectF(0, 50, 20, 50));
698 QCOMPARE(layout->itemAt(0,1)->geometry(), QRectF(20, 0, 50, 50));
699 QCOMPARE(layout->itemAt(1,1)->geometry(), QRectF(20, 50, 50, 50));
700 QCOMPARE(layout->itemAt(0,2)->geometry(), QRectF(75, 0, 50, 50));
701 QCOMPARE(layout->itemAt(1,2)->geometry(), QRectF(75, 50, 50, 50));
702
703 for (int i = 0; i < layout->count(); i++)
704 layout->setAlignment(item: layout->itemAt(index: i), alignment: Qt::AlignRight | Qt::AlignBottom);
705 layout->activate();
706 QCOMPARE(layout->itemAt(0,0)->geometry(), QRectF(0, 0, 20, 50));
707 QCOMPARE(layout->itemAt(1,0)->geometry(), QRectF(0, 50, 20, 50));
708 QCOMPARE(layout->itemAt(0,1)->geometry(), QRectF(20, 0, 50, 50));
709 QCOMPARE(layout->itemAt(1,1)->geometry(), QRectF(20, 50, 50, 50));
710 QCOMPARE(layout->itemAt(0,2)->geometry(), QRectF(80, 0, 50, 50));
711 QCOMPARE(layout->itemAt(1,2)->geometry(), QRectF(80, 50, 50, 50));
712 for (int i = 0; i < layout->count(); i++)
713 layout->setAlignment(item: layout->itemAt(index: i), alignment: Qt::AlignCenter);
714
715 layout->setMaximumSize(layout->maximumSize() + QSizeF(60,60));
716 widget->resize(size: widget->effectiveSizeHint(which: Qt::MaximumSize));
717 layout->activate();
718
719 QCOMPARE(layout->itemAt(0,0)->geometry(), QRectF(0, 15, 20, 50));
720 QCOMPARE(layout->itemAt(1,0)->geometry(), QRectF(0, 95, 20, 50));
721 QCOMPARE(layout->itemAt(0,1)->geometry(), QRectF(20+30, 15, 50, 50));
722 QCOMPARE(layout->itemAt(1,1)->geometry(), QRectF(20+30, 95, 50, 50));
723 QCOMPARE(layout->itemAt(0,2)->geometry(), QRectF(20+60+50+5, 15, 50, 50));
724 QCOMPARE(layout->itemAt(1,2)->geometry(), QRectF(20+60+50+5, 95, 50, 50));
725
726 layout->setMaximumSize(layout->preferredSize() + QSizeF(20,20));
727 widget->resize(size: widget->effectiveSizeHint(which: Qt::MaximumSize));
728 layout->activate();
729
730 QCOMPARE(layout->itemAt(0,0)->geometry(), QRectF(0, 0, 20, 35));
731 QCOMPARE(layout->itemAt(1,0)->geometry(), QRectF(0, 35, 20, 35));
732 QCOMPARE(layout->itemAt(0,1)->geometry(), QRectF(20, 0, 35, 35));
733 QCOMPARE(layout->itemAt(1,1)->geometry(), QRectF(20, 35, 35, 35));
734 QCOMPARE(layout->itemAt(0,2)->geometry(), QRectF(55, 0, 35, 35));
735 QCOMPARE(layout->itemAt(1,2)->geometry(), QRectF(55, 35, 35, 35));
736
737 delete widget;
738}
739
740void tst_QGraphicsGridLayout::columnMinimumWidth_data()
741{
742 QTest::addColumn<bool>(name: "hasHeightForWidth");
743
744 QTest::newRow(dataTag: "") << false;
745 QTest::newRow(dataTag: "hasHeightForWidth") << true;
746}
747// public qreal columnMinimumWidth(int column) const
748void tst_QGraphicsGridLayout::columnMinimumWidth()
749{
750 QFETCH(bool, hasHeightForWidth);
751 QGraphicsScene scene;
752 QGraphicsView view(&scene);
753 QGraphicsWidget *widget = new QGraphicsWidget(0, Qt::Window);
754 QGraphicsGridLayout *layout = new QGraphicsGridLayout();
755 scene.addItem(item: widget);
756 widget->setLayout(layout);
757 populateLayout(gridLayout: layout, width: 3, height: 2, hasHeightForWidth);
758 layout->setContentsMargins(left: 0, top: 0, right: 0, bottom: 0);
759 layout->setSpacing(0);
760
761 // should at least be a very large number
762 QCOMPARE(layout->columnMinimumWidth(0), 0.0);
763 QCOMPARE(layout->columnMinimumWidth(0), layout->columnMinimumWidth(1));
764 QCOMPARE(layout->columnMinimumWidth(1), layout->columnMinimumWidth(2));
765 layout->setColumnMinimumWidth(column: 0, width: 20);
766 layout->setColumnMinimumWidth(column: 2, width: 40);
767
768 view.show();
769 widget->show();
770 widget->resize(size: widget->effectiveSizeHint(which: Qt::PreferredSize));
771 QApplication::processEvents();
772
773 QCOMPARE(layout->itemAt(0,0)->geometry().width(), 25.0);
774 QCOMPARE(layout->itemAt(1,0)->geometry().width(), 25.0);
775 QCOMPARE(layout->itemAt(0,1)->geometry().width(), 25.0);
776 QCOMPARE(layout->itemAt(1,1)->geometry().width(), 25.0);
777 QCOMPARE(layout->itemAt(0,2)->geometry().width(), 40.0);
778 QCOMPARE(layout->itemAt(1,2)->geometry().width(), 40.0);
779
780 delete widget;
781}
782
783void tst_QGraphicsGridLayout::columnPreferredWidth_data()
784{
785 QTest::addColumn<bool>(name: "hasHeightForWidth");
786
787 QTest::newRow(dataTag: "") << false;
788 QTest::newRow(dataTag: "hasHeightForWidth") << true;
789}
790// public qreal columnPreferredWidth(int column) const
791void tst_QGraphicsGridLayout::columnPreferredWidth()
792{
793 QFETCH(bool, hasHeightForWidth);
794 QGraphicsScene scene;
795 QGraphicsView view(&scene);
796 QGraphicsWidget *widget = new QGraphicsWidget(0, Qt::Window);
797 QGraphicsGridLayout *layout = new QGraphicsGridLayout();
798 scene.addItem(item: widget);
799 widget->setLayout(layout);
800 populateLayout(gridLayout: layout, width: 3, height: 2, hasHeightForWidth);
801 layout->setContentsMargins(left: 0, top: 0, right: 0, bottom: 0);
802 layout->setSpacing(0);
803
804 // default preferred width ??
805 QCOMPARE(layout->columnPreferredWidth(0), 0.0);
806 QCOMPARE(layout->columnPreferredWidth(0), layout->columnPreferredWidth(1));
807 QCOMPARE(layout->columnPreferredWidth(1), layout->columnPreferredWidth(2));
808 layout->setColumnPreferredWidth(column: 0, width: 20);
809 layout->setColumnPreferredWidth(column: 2, width: 40);
810
811 view.show();
812 widget->show();
813 widget->resize(size: widget->effectiveSizeHint(which: Qt::PreferredSize));
814 QApplication::processEvents();
815
816 QCOMPARE(layout->itemAt(0,0)->geometry().width(), 25.0);
817 QCOMPARE(layout->itemAt(1,0)->geometry().width(), 25.0);
818 QCOMPARE(layout->itemAt(0,1)->geometry().width(), 25.0);
819 QCOMPARE(layout->itemAt(1,1)->geometry().width(), 25.0);
820 QCOMPARE(layout->itemAt(0,2)->geometry().width(), 40.0);
821 QCOMPARE(layout->itemAt(1,2)->geometry().width(), 40.0);
822
823 delete widget;
824}
825
826// public void setColumnFixedWidth(int row, qreal height)
827void tst_QGraphicsGridLayout::setColumnFixedWidth()
828{
829 QGraphicsScene scene;
830 QGraphicsView view(&scene);
831 QGraphicsWidget *widget = new QGraphicsWidget(0, Qt::Window);
832 QGraphicsGridLayout *layout = new QGraphicsGridLayout();
833 scene.addItem(item: widget);
834 widget->setLayout(layout);
835 populateLayout(gridLayout: layout, width: 3, height: 2);
836 layout->setContentsMargins(left: 0, top: 0, right: 0, bottom: 0);
837 layout->setSpacing(0);
838
839 layout->setColumnFixedWidth(column: 0, width: 20);
840 layout->setColumnFixedWidth(column: 2, width: 40);
841
842 view.show();
843 widget->show();
844 widget->resize(size: widget->effectiveSizeHint(which: Qt::PreferredSize));
845 QApplication::processEvents();
846
847 QCOMPARE(layout->itemAt(0,0)->geometry().width(), 20.0);
848 QCOMPARE(layout->itemAt(1,0)->geometry().width(), 20.0);
849 QCOMPARE(layout->itemAt(0,1)->geometry().width(), 25.0);
850 QCOMPARE(layout->itemAt(1,1)->geometry().width(), 25.0);
851 QCOMPARE(layout->itemAt(0,2)->geometry().width(), 40.0);
852 QCOMPARE(layout->itemAt(1,2)->geometry().width(), 40.0);
853
854 delete widget;
855}
856
857// public qreal columnSpacing(int column) const
858void tst_QGraphicsGridLayout::columnSpacing()
859{
860 {
861 QGraphicsScene scene;
862 QGraphicsView view(&scene);
863 QGraphicsWidget *widget = new QGraphicsWidget(0, Qt::Window);
864 QGraphicsGridLayout *layout = new QGraphicsGridLayout();
865 scene.addItem(item: widget);
866 widget->setLayout(layout);
867 populateLayout(gridLayout: layout, width: 3, height: 2);
868 layout->setContentsMargins(left: 0, top: 0, right: 0, bottom: 0);
869 layout->setSpacing(0);
870 QCOMPARE(layout->columnSpacing(0), 0.0);
871
872 layout->setColumnSpacing(column: 0, spacing: 20);
873 view.show();
874 widget->show();
875 widget->resize(size: widget->effectiveSizeHint(which: Qt::PreferredSize));
876 QApplication::processEvents();
877
878 QCOMPARE(layout->itemAt(0,0)->geometry().left(), 0.0);
879 QCOMPARE(layout->itemAt(0,0)->geometry().right(), 25.0);
880 QCOMPARE(layout->itemAt(0,1)->geometry().left(), 45.0);
881 QCOMPARE(layout->itemAt(0,1)->geometry().right(), 70.0);
882 QCOMPARE(layout->itemAt(0,2)->geometry().left(), 70.0);
883 QCOMPARE(layout->itemAt(0,2)->geometry().right(), 95.0);
884
885 delete widget;
886 }
887
888 {
889 // don't include items and spacings that was previously part of the layout
890 // (horizontal)
891 QGraphicsGridLayout *layout = new QGraphicsGridLayout;
892 populateLayout(gridLayout: layout, width: 3, height: 1);
893 layout->setContentsMargins(left: 0, top: 0, right: 0, bottom: 0);
894 layout->setSpacing(0);
895 layout->setColumnSpacing(column: 0, spacing: 10);
896 layout->setColumnSpacing(column: 1, spacing: 10);
897 layout->setColumnSpacing(column: 2, spacing: 10);
898 layout->setColumnSpacing(column: 3, spacing: 10);
899 QCOMPARE(layout->preferredSize(), QSizeF(95, 25));
900 layout->removeAt(index: 2);
901 QCOMPARE(layout->preferredSize(), QSizeF(60, 25));
902 layout->removeAt(index: 1);
903 QCOMPARE(layout->preferredSize(), QSizeF(25, 25));
904 delete layout;
905 }
906 {
907 // don't include items and spacings that was previously part of the layout
908 // (vertical)
909 QGraphicsGridLayout *layout = new QGraphicsGridLayout;
910 populateLayout(gridLayout: layout, width: 2, height: 2);
911 layout->setContentsMargins(left: 0, top: 0, right: 0, bottom: 0);
912 layout->setSpacing(0);
913 layout->setColumnSpacing(column: 0, spacing: 10);
914 layout->setColumnSpacing(column: 1, spacing: 10);
915 layout->setRowSpacing(row: 0, spacing: 10);
916 layout->setRowSpacing(row: 1, spacing: 10);
917 QCOMPARE(layout->preferredSize(), QSizeF(60, 60));
918 layout->removeAt(index: 3);
919 QCOMPARE(layout->preferredSize(), QSizeF(60, 60));
920 layout->removeAt(index: 2);
921 QCOMPARE(layout->preferredSize(), QSizeF(60, 25));
922 layout->removeAt(index: 1);
923 QCOMPARE(layout->preferredSize(), QSizeF(25, 25));
924 delete layout;
925 }
926
927}
928
929// public int columnStretchFactor(int column) const
930void tst_QGraphicsGridLayout::columnStretchFactor()
931{
932 QGraphicsScene scene;
933 QGraphicsView view(&scene);
934 QGraphicsWidget *widget = new QGraphicsWidget(0, Qt::Window);
935 QGraphicsGridLayout *layout = new QGraphicsGridLayout();
936 scene.addItem(item: widget);
937 widget->setLayout(layout);
938 populateLayout(gridLayout: layout, width: 3, height: 2);
939 layout->setContentsMargins(left: 0, top: 0, right: 0, bottom: 0);
940 layout->setSpacing(0);
941
942 layout->setColumnStretchFactor(column: 0, stretch: 1);
943 layout->setColumnStretchFactor(column: 1, stretch: 2);
944 layout->setColumnStretchFactor(column: 2, stretch: 3);
945 view.show();
946 widget->show();
947 widget->resize(w: 130, h: 50);
948 QApplication::processEvents();
949
950 QVERIFY(layout->itemAt(0,0)->geometry().width() < layout->itemAt(0,1)->geometry().width());
951 QVERIFY(layout->itemAt(0,1)->geometry().width() < layout->itemAt(0,2)->geometry().width());
952 QVERIFY(layout->itemAt(1,0)->geometry().width() < layout->itemAt(1,1)->geometry().width());
953 QVERIFY(layout->itemAt(1,1)->geometry().width() < layout->itemAt(1,2)->geometry().width());
954
955 delete widget;
956}
957
958
959// public int count() const
960void tst_QGraphicsGridLayout::count()
961{
962 QGraphicsScene scene;
963 QGraphicsView view(&scene);
964 QGraphicsWidget *widget = new QGraphicsWidget(0, Qt::Window);
965 QGraphicsGridLayout *layout = new QGraphicsGridLayout();
966 scene.addItem(item: widget);
967 widget->setLayout(layout);
968 populateLayout(gridLayout: layout, width: 3, height: 2);
969 QCOMPARE(layout->count(), 6);
970 layout->removeAt(index: 5);
971 layout->removeAt(index: 3);
972 QCOMPARE(layout->count(), 4);
973 layout->removeAt(index: 1);
974 QCOMPARE(layout->count(), 3);
975 layout->removeAt(index: 0);
976 QCOMPARE(layout->count(), 2);
977 layout->removeAt(index: 0);
978 QCOMPARE(layout->count(), 1);
979 layout->removeAt(index: 0);
980 QCOMPARE(layout->count(), 0);
981
982 delete widget;
983}
984
985void tst_QGraphicsGridLayout::horizontalSpacing_data()
986{
987 QTest::addColumn<qreal>(name: "horizontalSpacing");
988 QTest::newRow(dataTag: "zero") << qreal(0.0);
989 QTest::newRow(dataTag: "10") << qreal(10.0);
990}
991
992// public qreal horizontalSpacing() const
993void tst_QGraphicsGridLayout::horizontalSpacing()
994{
995 QFETCH(qreal, horizontalSpacing);
996 QGraphicsScene scene;
997 QGraphicsView view(&scene);
998 QGraphicsWidget *widget = new QGraphicsWidget(0, Qt::Window);
999 QGraphicsGridLayout *layout = new QGraphicsGridLayout();
1000 scene.addItem(item: widget);
1001 widget->setLayout(layout);
1002 populateLayout(gridLayout: layout, width: 3, height: 2);
1003 layout->setContentsMargins(left: 0, top: 0, right: 0, bottom: 0);
1004 qreal w = layout->sizeHint(which: Qt::PreferredSize, constraint: QSizeF()).width();
1005 qreal oldSpacing = layout->horizontalSpacing();
1006
1007 // The remainder of this test is only applicable if the current style uses uniform layout spacing
1008 if (oldSpacing != -1) {
1009 layout->setHorizontalSpacing(horizontalSpacing);
1010 QApplication::processEvents();
1011 qreal new_w = layout->sizeHint(which: Qt::PreferredSize, constraint: QSizeF()).width();
1012 QCOMPARE(new_w, w - (3-1)*(oldSpacing - horizontalSpacing));
1013 }
1014 delete widget;
1015}
1016
1017void tst_QGraphicsGridLayout::contentsMargins()
1018{
1019 QGraphicsScene scene;
1020 QGraphicsView view(&scene);
1021 QGraphicsWidget *widget = new QGraphicsWidget(0, Qt::Window);
1022 QGraphicsGridLayout *layout = new QGraphicsGridLayout();
1023 QGraphicsGridLayout *sublayout = new QGraphicsGridLayout();
1024 scene.addItem(item: widget);
1025 widget->setLayout(layout);
1026 layout->addItem(aitem: sublayout,arow: 0, acolumn: 1);
1027
1028 qreal left, top, right, bottom;
1029 // sublayouts have 0 margin
1030 sublayout->getContentsMargins(left: &left, top: &top, right: &right, bottom: &bottom);
1031 QCOMPARE(left, 0.0);
1032 QCOMPARE(top, 0.0);
1033 QCOMPARE(right, 0.0);
1034 QCOMPARE(bottom, 0.0);
1035
1036 // top level layouts have style dependent margins.
1037 // we'll just check if its different from 0. (applies to all our styles)
1038 layout->getContentsMargins(left: &left, top: &top, right: &right, bottom: &bottom);
1039 QVERIFY(left >= 0.0);
1040 QVERIFY(top >= 0.0);
1041 QVERIFY(right >= 0.0);
1042 QVERIFY(bottom >= 0.0);
1043
1044 delete widget;
1045}
1046
1047// public QGraphicsLayoutItem* itemAt(int index) const
1048void tst_QGraphicsGridLayout::itemAt()
1049{
1050 QGraphicsScene scene;
1051 QGraphicsView view(&scene);
1052 QGraphicsWidget *widget = new QGraphicsWidget(0, Qt::Window);
1053 QGraphicsGridLayout *layout = new QGraphicsGridLayout();
1054 scene.addItem(item: widget);
1055 widget->setLayout(layout);
1056 populateLayoutWithSpansAndHoles(gridLayout: layout);
1057
1058 //itemAt(int row, int column)
1059 QVERIFY( layout->itemAt(0,0));
1060 QVERIFY( layout->itemAt(0,1));
1061 QCOMPARE(layout->itemAt(0,2), static_cast<QGraphicsLayoutItem*>(0));
1062 QCOMPARE(layout->itemAt(1,0), static_cast<QGraphicsLayoutItem*>(0));
1063 QVERIFY( layout->itemAt(1,1));
1064 QVERIFY( layout->itemAt(1,2));
1065
1066
1067 //itemAt(int index)
1068 for (int i = -2; i < layout->count() + 2; ++i) {
1069 if (i >= 0 && i < layout->count()) {
1070 QVERIFY(layout->itemAt(i));
1071 } else {
1072 const QByteArray message = "QGraphicsGridLayout::itemAt: invalid index " + QByteArray::number(i);
1073 QTest::ignoreMessage(type: QtWarningMsg, message: message.constData());
1074 QCOMPARE(layout->itemAt(i), nullptr);
1075 }
1076 }
1077 delete widget;
1078}
1079
1080// public void removeAt(int index)
1081void tst_QGraphicsGridLayout::removeAt()
1082{
1083 QGraphicsScene scene;
1084 QGraphicsView view(&scene);
1085 QGraphicsWidget *widget = new QGraphicsWidget(0, Qt::Window);
1086 QGraphicsGridLayout *layout = new QGraphicsGridLayout();
1087 scene.addItem(item: widget);
1088 widget->setLayout(layout);
1089 populateLayout(gridLayout: layout, width: 3, height: 2);
1090 QCOMPARE(layout->count(), 6);
1091 layout->removeAt(index: 5);
1092 layout->removeAt(index: 3);
1093 QCOMPARE(layout->count(), 4);
1094 layout->removeAt(index: 1);
1095 QCOMPARE(layout->count(), 3);
1096 layout->removeAt(index: 0);
1097 QCOMPARE(layout->count(), 2);
1098 layout->removeAt(index: 0);
1099 QCOMPARE(layout->count(), 1);
1100 QGraphicsLayoutItem *item0 = layout->itemAt(index: 0);
1101 QCOMPARE(item0->parentLayoutItem(), static_cast<QGraphicsLayoutItem *>(layout));
1102 layout->removeAt(index: 0);
1103 QCOMPARE(item0->parentLayoutItem(), nullptr);
1104 QCOMPARE(layout->count(), 0);
1105 QTest::ignoreMessage(type: QtWarningMsg, message: QString::fromLatin1(str: "QGraphicsGridLayout::removeAt: invalid index 0").toLatin1().constData());
1106 layout->removeAt(index: 0);
1107 QCOMPARE(layout->count(), 0);
1108 delete widget;
1109}
1110
1111void tst_QGraphicsGridLayout::removeItem()
1112{
1113 QGraphicsScene scene;
1114 QGraphicsView view(&scene);
1115
1116 QGraphicsWidget *widget = new QGraphicsWidget(0, Qt::Window);
1117 scene.addItem(item: widget);
1118 QGraphicsGridLayout *l = new QGraphicsGridLayout();
1119 widget->setLayout(l);
1120
1121 populateLayout(gridLayout: l, width: 3, height: 2);
1122 QCOMPARE(l->count(), 6);
1123 l->removeItem(item: l->itemAt(index: 5));
1124 l->removeItem(item: l->itemAt(index: 4));
1125 QCOMPARE(l->count(), 4);
1126
1127 // Avoid crashing. Note that the warning message might change in the future.
1128 QTest::ignoreMessage(type: QtWarningMsg, message: QString::fromLatin1(str: "QGraphicsGridLayout::removeAt: invalid index -1").toLatin1().constData());
1129 l->removeItem(item: 0);
1130 QCOMPARE(l->count(), 4);
1131
1132 QTest::ignoreMessage(type: QtWarningMsg, message: QString::fromLatin1(str: "QGraphicsGridLayout::removeAt: invalid index -1").toLatin1().constData());
1133 l->removeItem(item: new QGraphicsWidget);
1134 QCOMPARE(l->count(), 4);
1135}
1136
1137void tst_QGraphicsGridLayout::rowAlignment_data()
1138{
1139 QTest::addColumn<bool>(name: "hasHeightForWidth");
1140
1141 QTest::newRow(dataTag: "") << false;
1142 QTest::newRow(dataTag: "hasHeightForWidth") << true;
1143}
1144
1145// public Qt::Alignment rowAlignment(int row) const
1146void tst_QGraphicsGridLayout::rowAlignment()
1147{
1148 QFETCH(bool, hasHeightForWidth);
1149 QGraphicsScene scene;
1150 QGraphicsView view(&scene);
1151 QGraphicsWidget *widget = new QGraphicsWidget(0, Qt::Window);
1152 QGraphicsGridLayout *layout = new QGraphicsGridLayout();
1153 scene.addItem(item: widget);
1154 widget->setLayout(layout);
1155 populateLayout(gridLayout: layout, width: 2, height: 3, hasHeightForWidth);
1156 layout->setContentsMargins(left: 0, top: 0, right: 0, bottom: 0);
1157 layout->setSpacing(1);
1158 widget->setContentsMargins(left: 0, top: 0, right: 0, bottom: 0);
1159
1160 view.resize(w: 330,h: 450);
1161 widget->resize(w: 300, h: 400);
1162 view.show();
1163 widget->show();
1164 QApplication::sendPostedEvents(receiver: 0, event_type: 0);
1165 // Check default
1166 QCOMPARE(layout->rowAlignment(0), 0);
1167 QCOMPARE(layout->rowAlignment(1), 0);
1168 QCOMPARE(layout->rowAlignment(2), 0);
1169
1170 // make the grids larger than the items, so that alignment kicks in
1171 layout->setRowMinimumHeight(row: 0, height: 100.0);
1172 layout->setRowMinimumHeight(row: 1, height: 100.0);
1173 layout->setRowMinimumHeight(row: 2, height: 100.0);
1174 // expand columns also, so we can test combination of horiz and vertical alignment
1175 layout->setColumnMinimumWidth(column: 0, width: 100.0);
1176 layout->setColumnMinimumWidth(column: 1, width: 100.0);
1177
1178 layout->setRowAlignment(row: 0, alignment: Qt::AlignBottom);
1179 layout->setRowAlignment(row: 1, alignment: Qt::AlignVCenter);
1180 layout->setRowAlignment(row: 2, alignment: Qt::AlignTop);
1181
1182 // see if item alignment takes preference over rowAlignment
1183 layout->setAlignment(item: layout->itemAt(row: 0,column: 0), alignment: Qt::AlignRight);
1184 layout->setAlignment(item: layout->itemAt(row: 1,column: 0), alignment: Qt::AlignTop);
1185 layout->setAlignment(item: layout->itemAt(row: 2,column: 0), alignment: Qt::AlignHCenter);
1186
1187 QApplication::sendPostedEvents(receiver: 0, event_type: 0); // process LayoutRequest
1188
1189 QCOMPARE(layout->alignment(layout->itemAt(0,0)), Qt::AlignRight); //Qt::AlignRight | Qt::AlignBottom
1190 QCOMPARE(layout->itemAt(0,0)->geometry(), QRectF(50, 50, 50, 50));
1191 QCOMPARE(layout->rowAlignment(0), Qt::AlignBottom);
1192 QCOMPARE(layout->itemAt(0,1)->geometry(), QRectF(101, 50, 50, 50));
1193 QCOMPARE(layout->alignment(layout->itemAt(1,0)), Qt::AlignTop);
1194 QCOMPARE(layout->itemAt(1,0)->geometry(), QRectF(0, 101, 50, 50));
1195 QCOMPARE(layout->rowAlignment(1), Qt::AlignVCenter);
1196 QCOMPARE(layout->itemAt(1,1)->geometry(), QRectF(101, 126, 50, 50));
1197 QCOMPARE(layout->alignment(layout->itemAt(2,0)), Qt::AlignHCenter);
1198 QCOMPARE(layout->itemAt(2,0)->geometry(), QRectF(25, 202, 50, 50));
1199 QCOMPARE(layout->rowAlignment(2), Qt::AlignTop);
1200 QCOMPARE(layout->itemAt(2,1)->geometry(), QRectF(101,202, 50, 50));
1201
1202 delete widget;
1203}
1204
1205void tst_QGraphicsGridLayout::rowCount_data()
1206{
1207 QTest::addColumn<bool>(name: "hasHeightForWidth");
1208
1209 QTest::newRow(dataTag: "") << false;
1210 QTest::newRow(dataTag: "hasHeightForWidth") << true;
1211}
1212
1213// public int rowCount() const
1214// public int columnCount() const
1215void tst_QGraphicsGridLayout::rowCount()
1216{
1217 QFETCH(bool, hasHeightForWidth);
1218 QGraphicsScene scene;
1219 QGraphicsView view(&scene);
1220 QGraphicsWidget *widget = new QGraphicsWidget(0, Qt::Window);
1221 QGraphicsGridLayout *layout = new QGraphicsGridLayout();
1222 scene.addItem(item: widget);
1223 widget->setLayout(layout);
1224 populateLayout(gridLayout: layout, width: 2, height: 3, hasHeightForWidth);
1225 layout->setContentsMargins(left: 0, top: 0, right: 0, bottom: 0);
1226 layout->setSpacing(0);
1227 widget->setContentsMargins(left: 0, top: 0, right: 0, bottom: 0);
1228 QCOMPARE(layout->rowCount(), 3);
1229 QCOMPARE(layout->columnCount(), 2);
1230
1231 // with spans and holes...
1232 widget->setLayout(0);
1233 layout = new QGraphicsGridLayout();
1234 populateLayoutWithSpansAndHoles(gridLayout: layout, hasHeightForWidth);
1235 QCOMPARE(layout->rowCount(), 2);
1236 QCOMPARE(layout->columnCount(), 3);
1237
1238 delete widget;
1239}
1240
1241void tst_QGraphicsGridLayout::rowMaximumHeight_data()
1242{
1243 QTest::addColumn<bool>(name: "hasHeightForWidth");
1244
1245 QTest::newRow(dataTag: "") << false;
1246 QTest::newRow(dataTag: "hasHeightForWidth") << true;
1247}
1248
1249// public qreal rowMaximumHeight(int row) const
1250void tst_QGraphicsGridLayout::rowMaximumHeight()
1251{
1252 QFETCH(bool, hasHeightForWidth);
1253 QGraphicsScene scene;
1254 QGraphicsView view(&scene);
1255 QGraphicsWidget *widget = new QGraphicsWidget(0, Qt::Window);
1256 QGraphicsGridLayout *layout = new QGraphicsGridLayout;
1257 scene.addItem(item: widget);
1258 widget->setLayout(layout);
1259 populateLayout(gridLayout: layout, width: 2, height: 3, hasHeightForWidth);
1260 layout->setContentsMargins(left: 0, top: 0, right: 0, bottom: 0);
1261 layout->setSpacing(0);
1262
1263 // should at least be a very large number
1264 QVERIFY(layout->rowMaximumHeight(0) >= 10000);
1265 QCOMPARE(layout->rowMaximumHeight(0), layout->rowMaximumHeight(1));
1266 QCOMPARE(layout->rowMaximumHeight(1), layout->rowMaximumHeight(2));
1267 layout->setRowMaximumHeight(row: 0, height: 20);
1268 layout->setRowMaximumHeight(row: 2, height: 60);
1269
1270 view.show();
1271 widget->show();
1272 widget->resize(size: widget->effectiveSizeHint(which: Qt::PreferredSize));
1273 QApplication::processEvents();
1274
1275 QCOMPARE(layout->itemAt(0,0)->geometry().height(), 20.0);
1276 QCOMPARE(layout->itemAt(0,1)->geometry().height(), 20.0);
1277 QCOMPARE(layout->itemAt(1,0)->geometry().height(), 25.0);
1278 QCOMPARE(layout->itemAt(1,1)->geometry().height(), 25.0);
1279 QCOMPARE(layout->itemAt(2,0)->geometry().height(), 25.0);
1280 QCOMPARE(layout->itemAt(2,1)->geometry().height(), 25.0);
1281
1282 delete widget;
1283}
1284
1285void tst_QGraphicsGridLayout::rowMinimumHeight_data()
1286{
1287 QTest::addColumn<bool>(name: "hasHeightForWidth");
1288
1289 QTest::newRow(dataTag: "") << false;
1290 QTest::newRow(dataTag: "hasHeightForWidth") << true;
1291}
1292// public qreal rowMinimumHeight(int row) const
1293void tst_QGraphicsGridLayout::rowMinimumHeight()
1294{
1295 QFETCH(bool, hasHeightForWidth);
1296 QGraphicsScene scene;
1297 QGraphicsView view(&scene);
1298 QGraphicsWidget *widget = new QGraphicsWidget(0, Qt::Window);
1299 QGraphicsGridLayout *layout = new QGraphicsGridLayout();
1300 scene.addItem(item: widget);
1301 widget->setLayout(layout);
1302 populateLayout(gridLayout: layout, width: 2, height: 3, hasHeightForWidth);
1303 layout->setContentsMargins(left: 0, top: 0, right: 0, bottom: 0);
1304 layout->setSpacing(0);
1305
1306 // should at least be a very large number
1307 QCOMPARE(layout->rowMinimumHeight(0), 0.0);
1308 QCOMPARE(layout->rowMinimumHeight(0), layout->rowMinimumHeight(1));
1309 QCOMPARE(layout->rowMinimumHeight(1), layout->rowMinimumHeight(2));
1310 layout->setRowMinimumHeight(row: 0, height: 20);
1311 layout->setRowMinimumHeight(row: 2, height: 40);
1312
1313 view.show();
1314 widget->show();
1315 widget->resize(size: widget->effectiveSizeHint(which: Qt::PreferredSize));
1316 QApplication::processEvents();
1317
1318 QCOMPARE(layout->itemAt(0,0)->geometry().height(), 25.0);
1319 QCOMPARE(layout->itemAt(0,1)->geometry().height(), 25.0);
1320 QCOMPARE(layout->itemAt(1,0)->geometry().height(), 25.0);
1321 QCOMPARE(layout->itemAt(1,1)->geometry().height(), 25.0);
1322 QCOMPARE(layout->itemAt(2,0)->geometry().height(), 40.0);
1323 QCOMPARE(layout->itemAt(2,1)->geometry().height(), 40.0);
1324
1325 delete widget;
1326}
1327
1328void tst_QGraphicsGridLayout::rowPreferredHeight_data()
1329{
1330 QTest::addColumn<bool>(name: "hasHeightForWidth");
1331
1332 QTest::newRow(dataTag: "") << false;
1333 QTest::newRow(dataTag: "hasHeightForWidth") << true;
1334}
1335// public qreal rowPreferredHeight(int row) const
1336void tst_QGraphicsGridLayout::rowPreferredHeight()
1337{
1338 QFETCH(bool, hasHeightForWidth);
1339 QGraphicsScene scene;
1340 QGraphicsView view(&scene);
1341 QGraphicsWidget *widget = new QGraphicsWidget(0, Qt::Window);
1342 QGraphicsGridLayout *layout = new QGraphicsGridLayout();
1343 scene.addItem(item: widget);
1344 widget->setLayout(layout);
1345 populateLayout(gridLayout: layout, width: 2, height: 3, hasHeightForWidth);
1346 layout->setContentsMargins(left: 0, top: 0, right: 0, bottom: 0);
1347 layout->setSpacing(0);
1348
1349 // default preferred height ??
1350 QCOMPARE(layout->rowPreferredHeight(0), 0.0);
1351 QCOMPARE(layout->rowPreferredHeight(0), layout->rowPreferredHeight(1));
1352 QCOMPARE(layout->rowPreferredHeight(1), layout->rowPreferredHeight(2));
1353 layout->setRowPreferredHeight(row: 0, height: 20);
1354 layout->setRowPreferredHeight(row: 2, height: 40);
1355
1356 view.show();
1357 widget->show();
1358 widget->resize(size: widget->effectiveSizeHint(which: Qt::PreferredSize));
1359 QApplication::processEvents();
1360
1361 // ### Jasmin: Should rowPreferredHeight have precedence over sizeHint(Qt::PreferredSize) ?
1362 QCOMPARE(layout->itemAt(0,0)->geometry().height(), 25.0);
1363 QCOMPARE(layout->itemAt(0,1)->geometry().height(), 25.0);
1364 QCOMPARE(layout->itemAt(1,0)->geometry().height(), 25.0);
1365 QCOMPARE(layout->itemAt(1,1)->geometry().height(), 25.0);
1366 QCOMPARE(layout->itemAt(2,0)->geometry().height(), 40.0);
1367 QCOMPARE(layout->itemAt(2,1)->geometry().height(), 40.0);
1368
1369 delete widget;
1370}
1371
1372// public void setRowFixedHeight(int row, qreal height)
1373void tst_QGraphicsGridLayout::setRowFixedHeight()
1374{
1375 QGraphicsScene scene;
1376 QGraphicsView view(&scene);
1377 QGraphicsWidget *widget = new QGraphicsWidget(0, Qt::Window);
1378 QGraphicsGridLayout *layout = new QGraphicsGridLayout();
1379 scene.addItem(item: widget);
1380 widget->setLayout(layout);
1381 populateLayout(gridLayout: layout, width: 2, height: 3);
1382 layout->setContentsMargins(left: 0, top: 0, right: 0, bottom: 0);
1383 layout->setSpacing(0);
1384
1385 layout->setRowFixedHeight(row: 0, height: 20.);
1386 layout->setRowFixedHeight(row: 2, height: 40.);
1387
1388 view.show();
1389 widget->show();
1390 widget->resize(size: widget->effectiveSizeHint(which: Qt::PreferredSize));
1391 QApplication::processEvents();
1392
1393 QCOMPARE(layout->itemAt(0,0)->geometry().height(), 20.0);
1394 QCOMPARE(layout->itemAt(0,1)->geometry().height(), 20.0);
1395 QCOMPARE(layout->itemAt(1,0)->geometry().height(), 25.0);
1396 QCOMPARE(layout->itemAt(1,1)->geometry().height(), 25.0);
1397 QCOMPARE(layout->itemAt(2,0)->geometry().height(), 40.0);
1398 QCOMPARE(layout->itemAt(2,1)->geometry().height(), 40.0);
1399
1400 delete widget;
1401}
1402
1403// public qreal rowSpacing(int row) const
1404void tst_QGraphicsGridLayout::rowSpacing()
1405{
1406 QGraphicsScene scene;
1407 QGraphicsView view(&scene);
1408 QGraphicsWidget *widget = new QGraphicsWidget(0, Qt::Window);
1409 QGraphicsGridLayout *layout = new QGraphicsGridLayout();
1410 scene.addItem(item: widget);
1411 widget->setLayout(layout);
1412 populateLayout(gridLayout: layout, width: 3, height: 2);
1413 layout->setContentsMargins(left: 0, top: 0, right: 0, bottom: 0);
1414 layout->setSpacing(0);
1415 QCOMPARE(layout->columnSpacing(0), 0.0);
1416
1417 layout->setColumnSpacing(column: 0, spacing: 20);
1418 view.show();
1419 widget->show();
1420 widget->resize(size: widget->effectiveSizeHint(which: Qt::PreferredSize));
1421 QApplication::processEvents();
1422
1423 QCOMPARE(layout->itemAt(0,0)->geometry().left(), 0.0);
1424 QCOMPARE(layout->itemAt(0,0)->geometry().right(), 25.0);
1425 QCOMPARE(layout->itemAt(0,1)->geometry().left(), 45.0);
1426 QCOMPARE(layout->itemAt(0,1)->geometry().right(), 70.0);
1427 QCOMPARE(layout->itemAt(0,2)->geometry().left(), 70.0);
1428 QCOMPARE(layout->itemAt(0,2)->geometry().right(), 95.0);
1429
1430 delete widget;
1431
1432}
1433
1434void tst_QGraphicsGridLayout::rowStretchFactor_data()
1435{
1436 QTest::addColumn<bool>(name: "hasHeightForWidth");
1437
1438 QTest::newRow(dataTag: "") << false;
1439 QTest::newRow(dataTag: "hasHeightForWidth") << true;
1440}
1441
1442// public int rowStretchFactor(int row) const
1443void tst_QGraphicsGridLayout::rowStretchFactor()
1444{
1445 QFETCH(bool, hasHeightForWidth);
1446 QGraphicsScene scene;
1447 QGraphicsView view(&scene);
1448 QGraphicsWidget *widget = new QGraphicsWidget(0, Qt::Window);
1449 QGraphicsGridLayout *layout = new QGraphicsGridLayout();
1450 scene.addItem(item: widget);
1451 widget->setLayout(layout);
1452 populateLayout(gridLayout: layout, width: 2, height: 3, hasHeightForWidth);
1453 layout->setContentsMargins(left: 0, top: 0, right: 0, bottom: 0);
1454 layout->setSpacing(0);
1455
1456 layout->setRowStretchFactor(row: 0, stretch: 1);
1457 layout->setRowStretchFactor(row: 1, stretch: 2);
1458 layout->setRowStretchFactor(row: 2, stretch: 3);
1459 view.show();
1460 widget->show();
1461 widget->resize(w: 50, h: 130);
1462 QApplication::processEvents();
1463
1464 QVERIFY(layout->itemAt(0,0)->geometry().height() < layout->itemAt(1,0)->geometry().height());
1465 QVERIFY(layout->itemAt(1,0)->geometry().height() < layout->itemAt(2,0)->geometry().height());
1466 QVERIFY(layout->itemAt(0,1)->geometry().height() < layout->itemAt(1,1)->geometry().height());
1467 QVERIFY(layout->itemAt(1,1)->geometry().height() < layout->itemAt(2,1)->geometry().height());
1468
1469 delete widget;
1470}
1471
1472void tst_QGraphicsGridLayout::setColumnSpacing_data()
1473{
1474 QTest::addColumn<int>(name: "column");
1475 QTest::addColumn<qreal>(name: "spacing");
1476 QTest::addColumn<bool>(name: "hasHeightForWidth");
1477
1478 QTest::newRow(dataTag: "null") << 0 << qreal(0.0) << false;
1479 QTest::newRow(dataTag: "10") << 0 << qreal(10.0) << false;
1480 QTest::newRow(dataTag: "null, hasHeightForWidth") << 0 << qreal(0.0) << true;
1481 QTest::newRow(dataTag: "10, hasHeightForWidth") << 0 << qreal(10.0) << true;
1482}
1483
1484// public void setColumnSpacing(int column, qreal spacing)
1485void tst_QGraphicsGridLayout::setColumnSpacing()
1486{
1487 QFETCH(int, column);
1488 QFETCH(qreal, spacing);
1489 QFETCH(bool, hasHeightForWidth);
1490
1491 QGraphicsScene scene;
1492 QGraphicsView view(&scene);
1493 QGraphicsWidget *widget = new QGraphicsWidget(0, Qt::Window);
1494 QGraphicsGridLayout *layout = new QGraphicsGridLayout();
1495 scene.addItem(item: widget);
1496 widget->setLayout(layout);
1497 populateLayout(gridLayout: layout, width: 3, height: 2, hasHeightForWidth);
1498 layout->setSpacing(0);
1499 layout->setContentsMargins(left: 0, top: 0, right: 0, bottom: 0);
1500 qreal oldSpacing = layout->columnSpacing(column);
1501 QCOMPARE(oldSpacing, 0.0);
1502 qreal w = layout->sizeHint(which: Qt::PreferredSize, constraint: QSizeF()).width();
1503 layout->setColumnSpacing(column, spacing);
1504 QApplication::processEvents();
1505 QCOMPARE(layout->sizeHint(Qt::PreferredSize, QSizeF()).width(), w + spacing);
1506}
1507
1508void tst_QGraphicsGridLayout::setGeometry_data()
1509{
1510 QTest::addColumn<QRectF>(name: "rect");
1511 QTest::newRow(dataTag: "null") << QRectF();
1512 QTest::newRow(dataTag: "normal") << QRectF(0,0, 50, 50);
1513}
1514
1515// public void setGeometry(QRectF const& rect)
1516void tst_QGraphicsGridLayout::setGeometry()
1517{
1518 QFETCH(QRectF, rect);
1519
1520 QGraphicsWidget *window = new QGraphicsWidget;
1521 QGraphicsGridLayout *layout = new QGraphicsGridLayout();
1522 window->setLayout(layout);
1523 QGraphicsGridLayout *layout2 = new QGraphicsGridLayout();
1524 layout2->setMaximumSize(aw: 100, ah: 100);
1525 layout->addItem(aitem: layout2, arow: 0, acolumn: 0);
1526 layout2->setGeometry(rect);
1527 QCOMPARE(layout2->geometry(), rect);
1528}
1529
1530void tst_QGraphicsGridLayout::setRowSpacing_data()
1531{
1532 QTest::addColumn<int>(name: "row");
1533 QTest::addColumn<qreal>(name: "spacing");
1534 QTest::addColumn<bool>(name: "hasHeightForWidth");
1535
1536 QTest::newRow(dataTag: "null") << 0 << qreal(0.0) << false;
1537 QTest::newRow(dataTag: "10") << 0 << qreal(10.0) << false;
1538 QTest::newRow(dataTag: "null, hasHeightForWidth") << 0 << qreal(0.0) << true;
1539 QTest::newRow(dataTag: "10, hasHeightForWidth") << 0 << qreal(10.0) << true;
1540}
1541
1542// public void setRowSpacing(int row, qreal spacing)
1543void tst_QGraphicsGridLayout::setRowSpacing()
1544{
1545 QFETCH(int, row);
1546 QFETCH(qreal, spacing);
1547 QFETCH(bool, hasHeightForWidth);
1548
1549 QGraphicsScene scene;
1550 QGraphicsView view(&scene);
1551 QGraphicsWidget *widget = new QGraphicsWidget(0, Qt::Window);
1552 QGraphicsGridLayout *layout = new QGraphicsGridLayout();
1553 scene.addItem(item: widget);
1554 widget->setLayout(layout);
1555 populateLayout(gridLayout: layout, width: 3, height: 2, hasHeightForWidth);
1556 layout->setSpacing(0);
1557 layout->setContentsMargins(left: 0, top: 0, right: 0, bottom: 0);
1558 qreal oldSpacing = layout->rowSpacing(row);
1559 QCOMPARE(oldSpacing, 0.0);
1560 qreal h = layout->sizeHint(which: Qt::PreferredSize, constraint: QSizeF()).height();
1561 layout->setRowSpacing(row, spacing);
1562 QApplication::processEvents();
1563 QCOMPARE(layout->sizeHint(Qt::PreferredSize, QSizeF()).height(), h + spacing);
1564}
1565
1566void tst_QGraphicsGridLayout::setSpacing_data()
1567{
1568 QTest::addColumn<qreal>(name: "spacing");
1569 QTest::addColumn<bool>(name: "hasHeightForWidth");
1570 QTest::newRow(dataTag: "zero") << qreal(0.0) << false;
1571 QTest::newRow(dataTag: "17") << qreal(17.0) << false;
1572 QTest::newRow(dataTag: "zero, hasHeightForWidth") << qreal(0.0) << true;
1573 QTest::newRow(dataTag: "17, hasHeightForWidth") << qreal(17.0) << true;
1574}
1575
1576// public void setSpacing(qreal spacing)
1577void tst_QGraphicsGridLayout::setSpacing()
1578{
1579 QFETCH(qreal, spacing);
1580 QFETCH(bool, hasHeightForWidth);
1581 QGraphicsScene scene;
1582 QGraphicsView view(&scene);
1583 QGraphicsWidget *widget = new QGraphicsWidget(0, Qt::Window);
1584 QGraphicsGridLayout *layout = new QGraphicsGridLayout();
1585 scene.addItem(item: widget);
1586 widget->setLayout(layout);
1587 populateLayout(gridLayout: layout, width: 3, height: 2, hasHeightForWidth);
1588 layout->setContentsMargins(left: 0, top: 0, right: 0, bottom: 0);
1589 QSizeF sh = layout->sizeHint(which: Qt::PreferredSize, constraint: QSizeF());
1590 qreal oldVSpacing = layout->verticalSpacing();
1591 qreal oldHSpacing = layout->horizontalSpacing();
1592
1593 // The remainder of this test is only applicable if the current style uses uniform layout spacing
1594 if (oldVSpacing != -1) {
1595 layout->setSpacing(spacing);
1596 QApplication::processEvents();
1597 QSizeF newSH = layout->sizeHint(which: Qt::PreferredSize, constraint: QSizeF());
1598 QCOMPARE(newSH.height(), sh.height() - (2-1)*(oldVSpacing - spacing));
1599 QCOMPARE(newSH.width(), sh.width() - (3-1)*(oldHSpacing - spacing));
1600 }
1601 delete widget;
1602}
1603
1604void tst_QGraphicsGridLayout::sizeHint_data()
1605{
1606 QTest::addColumn<ItemList>(name: "itemDescriptions");
1607 QTest::addColumn<QSizeF>(name: "expectedMinimumSizeHint");
1608 QTest::addColumn<QSizeF>(name: "expectedPreferredSizeHint");
1609 QTest::addColumn<QSizeF>(name: "expectedMaximumSizeHint");
1610
1611 QTest::newRow(dataTag: "rowSpan_larger_than_rows") << (ItemList()
1612 << ItemDesc(0,0)
1613 .minSize(sz: QSizeF(50,300))
1614 .maxSize(sz: QSizeF(50,300))
1615 .rowSpan(span: 2)
1616 << ItemDesc(0,1)
1617 .minSize(sz: QSizeF(50,0))
1618 .preferredSize(sz: QSizeF(50,50))
1619 .maxSize(sz: QSize(50, 1000))
1620 << ItemDesc(1,1)
1621 .minSize(sz: QSizeF(50,0))
1622 .preferredSize(sz: QSizeF(50,50))
1623 .maxSize(sz: QSize(50, 1000))
1624 )
1625 << QSizeF(100, 300)
1626 << QSizeF(100, 300)
1627 << QSizeF(100, 2000);
1628
1629 QTest::newRow(dataTag: "rowSpan_smaller_than_rows") << (ItemList()
1630 << ItemDesc(0,0)
1631 .minSize(sz: QSizeF(50, 0))
1632 .preferredSize(sz: QSizeF(50, 50))
1633 .maxSize(sz: QSizeF(50, 300))
1634 .rowSpan(span: 2)
1635 << ItemDesc(0,1)
1636 .minSize(sz: QSizeF(50, 50))
1637 .preferredSize(sz: QSizeF(50, 50))
1638 .maxSize(sz: QSize(50, 50))
1639 << ItemDesc(1,1)
1640 .minSize(sz: QSizeF(50, 50))
1641 .preferredSize(sz: QSizeF(50, 50))
1642 .maxSize(sz: QSize(50, 50))
1643 )
1644 << QSizeF(100, 100)
1645 << QSizeF(100, 100)
1646 << QSizeF(100, 100);
1647
1648 QTest::newRow(dataTag: "colSpan_with_ignored_column") << (ItemList()
1649 << ItemDesc(0,0)
1650 .minSize(sz: QSizeF(40,20))
1651 .maxSize(sz: QSizeF(60,20))
1652 .colSpan(span: 2)
1653 << ItemDesc(0,2)
1654 .minSize(sz: QSizeF(20, 20))
1655 .maxSize(sz: QSizeF(30, 20))
1656 << ItemDesc(1,0)
1657 .minSize(sz: QSizeF(60, 20))
1658 .maxSize(sz: QSizeF(90, 20))
1659 .colSpan(span: 3)
1660 )
1661 << QSizeF(60, 40)
1662 << QSizeF(80, 40)
1663 << QSizeF(90, 40);
1664
1665}
1666
1667// public QSizeF sizeHint(Qt::SizeHint which, QSizeF const& constraint = QSizeF()) const
1668void tst_QGraphicsGridLayout::sizeHint()
1669{
1670 QFETCH(ItemList, itemDescriptions);
1671 QFETCH(QSizeF, expectedMinimumSizeHint);
1672 QFETCH(QSizeF, expectedPreferredSizeHint);
1673 QFETCH(QSizeF, expectedMaximumSizeHint);
1674
1675 QGraphicsScene scene;
1676 QGraphicsView view(&scene);
1677 QGraphicsWidget *widget = new QGraphicsWidget(0, Qt::Window);
1678 QGraphicsGridLayout *layout = new QGraphicsGridLayout;
1679 scene.addItem(item: widget);
1680 widget->setLayout(layout);
1681 layout->setContentsMargins(left: 0, top: 0, right: 0, bottom: 0);
1682 layout->setSpacing(0.0);
1683 widget->setContentsMargins(left: 0, top: 0, right: 0, bottom: 0);
1684
1685 int i;
1686 for (i = 0; i < itemDescriptions.count(); ++i) {
1687 ItemDesc desc = itemDescriptions.at(i);
1688 RectWidget *item = new RectWidget(widget);
1689 desc.apply(layout, item);
1690 }
1691
1692 QApplication::sendPostedEvents(receiver: 0, event_type: 0);
1693
1694 widget->show();
1695 view.show();
1696 view.resize(w: 400,h: 300);
1697 QCOMPARE(layout->sizeHint(Qt::MinimumSize), expectedMinimumSizeHint);
1698 QCOMPARE(layout->sizeHint(Qt::PreferredSize), expectedPreferredSizeHint);
1699 QCOMPARE(layout->sizeHint(Qt::MaximumSize), expectedMaximumSizeHint);
1700
1701}
1702
1703void tst_QGraphicsGridLayout::verticalSpacing_data()
1704{
1705 QTest::addColumn<qreal>(name: "verticalSpacing");
1706 QTest::newRow(dataTag: "zero") << qreal(0.0);
1707 QTest::newRow(dataTag: "17") << qreal(10.0);
1708}
1709
1710// public qreal verticalSpacing() const
1711void tst_QGraphicsGridLayout::verticalSpacing()
1712{
1713 QFETCH(qreal, verticalSpacing);
1714 QGraphicsScene scene;
1715 QGraphicsView view(&scene);
1716 QGraphicsWidget *widget = new QGraphicsWidget(0, Qt::Window);
1717 QGraphicsGridLayout *layout = new QGraphicsGridLayout();
1718 scene.addItem(item: widget);
1719 widget->setLayout(layout);
1720 populateLayout(gridLayout: layout, width: 3, height: 2);
1721 layout->setContentsMargins(left: 0, top: 0, right: 0, bottom: 0);
1722 qreal h = layout->sizeHint(which: Qt::PreferredSize, constraint: QSizeF()).height();
1723 qreal oldSpacing = layout->verticalSpacing();
1724
1725 // The remainder of this test is only applicable if the current style uses uniform layout spacing
1726 if (oldSpacing != -1) {
1727 layout->setVerticalSpacing(verticalSpacing);
1728 QApplication::processEvents();
1729 qreal new_h = layout->sizeHint(which: Qt::PreferredSize, constraint: QSizeF()).height();
1730 QCOMPARE(new_h, h - (2-1)*(oldSpacing - verticalSpacing));
1731 }
1732 delete widget;
1733}
1734
1735void tst_QGraphicsGridLayout::layoutDirection_data()
1736{
1737 QTest::addColumn<bool>(name: "hasHeightForWidth");
1738
1739 QTest::newRow(dataTag: "") << false;
1740 QTest::newRow(dataTag: "hasHeightForWidth") << true;
1741}
1742
1743void tst_QGraphicsGridLayout::layoutDirection()
1744{
1745 QFETCH(bool, hasHeightForWidth);
1746
1747 QGraphicsScene scene;
1748 QGraphicsView view(&scene);
1749
1750 QGraphicsWidget *window = new QGraphicsWidget(0, Qt::Window);
1751 QGraphicsGridLayout *layout = new QGraphicsGridLayout;
1752 layout->setContentsMargins(left: 1, top: 2, right: 3, bottom: 4);
1753 layout->setSpacing(6);
1754 RectWidget *w1 = new RectWidget;
1755 w1->setMinimumSize(aw: 30, ah: 20);
1756 layout->addItem(aitem: w1, arow: 0, acolumn: 0);
1757 RectWidget *w2 = new RectWidget;
1758 w2->setMinimumSize(aw: 20, ah: 20);
1759 w2->setMaximumSize(aw: 20, ah: 20);
1760 layout->addItem(aitem: w2, arow: 0, acolumn: 1);
1761 RectWidget *w3 = new RectWidget;
1762 w3->setMinimumSize(aw: 20, ah: 20);
1763 w3->setMaximumSize(aw: 20, ah: 20);
1764 layout->addItem(aitem: w3, arow: 1, acolumn: 0);
1765 RectWidget *w4 = new RectWidget;
1766 w4->setMinimumSize(aw: 30, ah: 20);
1767 layout->addItem(aitem: w4, arow: 1, acolumn: 1);
1768
1769 QSizePolicy policy = w1->sizePolicy();
1770 policy.setHeightForWidth(hasHeightForWidth);
1771 w1->setSizePolicy(policy);
1772 w2->setSizePolicy(policy);
1773 w4->setSizePolicy(policy);
1774
1775 layout->setAlignment(item: w2, alignment: Qt::AlignRight);
1776 layout->setAlignment(item: w3, alignment: Qt::AlignLeft);
1777
1778 scene.addItem(item: window);
1779 window->setLayout(layout);
1780 view.show();
1781 window->resize(w: 70, h: 52);
1782 QApplication::processEvents();
1783 QCOMPARE(w1->geometry().left(), 1.0);
1784 QCOMPARE(w1->geometry().right(), 31.0);
1785 QCOMPARE(w2->geometry().left(), 47.0);
1786 QCOMPARE(w2->geometry().right(), 67.0);
1787 QCOMPARE(w3->geometry().left(), 1.0);
1788 QCOMPARE(w3->geometry().right(), 21.0);
1789 QCOMPARE(w4->geometry().left(), 37.0);
1790 QCOMPARE(w4->geometry().right(), 67.0);
1791
1792 window->setLayoutDirection(Qt::RightToLeft);
1793 QApplication::processEvents();
1794 QCOMPARE(w1->geometry().left(), 39.0);
1795 QCOMPARE(w1->geometry().right(), 69.0);
1796 QCOMPARE(w2->geometry().left(), 3.0);
1797 QCOMPARE(w2->geometry().right(), 23.0);
1798 QCOMPARE(w3->geometry().left(), 49.0);
1799 QCOMPARE(w3->geometry().right(), 69.0);
1800 QCOMPARE(w4->geometry().left(), 3.0);
1801 QCOMPARE(w4->geometry().right(), 33.0);
1802
1803 delete window;
1804}
1805
1806void tst_QGraphicsGridLayout::removeLayout()
1807{
1808 QGraphicsScene scene;
1809 RectWidget *textEdit = new RectWidget;
1810 RectWidget *pushButton = new RectWidget;
1811 scene.addItem(item: textEdit);
1812 scene.addItem(item: pushButton);
1813
1814 QGraphicsGridLayout *layout = new QGraphicsGridLayout;
1815 layout->addItem(aitem: textEdit, arow: 0, acolumn: 0);
1816 layout->addItem(aitem: pushButton, arow: 1, acolumn: 0);
1817
1818 QGraphicsWidget *form = new QGraphicsWidget;
1819 form->setLayout(layout);
1820 scene.addItem(item: form);
1821
1822 QGraphicsView view(&scene);
1823 view.show();
1824 QVERIFY(QTest::qWaitForWindowExposed(&view));
1825
1826 QRectF r1 = textEdit->geometry();
1827 QRectF r2 = pushButton->geometry();
1828 form->setLayout(0);
1829 //documentation of QGraphicsWidget::setLayout:
1830 //If layout is 0, the widget is left without a layout. Existing subwidgets' geometries will remain unaffected.
1831 QCOMPARE(textEdit->geometry(), r1);
1832 QCOMPARE(pushButton->geometry(), r2);
1833}
1834
1835void tst_QGraphicsGridLayout::defaultStretchFactors_data()
1836{
1837 QTest::addColumn<ItemList>(name: "itemDescriptions");
1838 QTest::addColumn<QSizeF>(name: "newSize");
1839 QTest::addColumn<SizeList>(name: "expectedSizes");
1840
1841 QTest::newRow(dataTag: "usepreferredsize") << (ItemList()
1842 << ItemDesc(0,0)
1843 .preferredSizeHint(sh: QSizeF(10,10))
1844 << ItemDesc(0,1)
1845 .preferredSizeHint(sh: QSizeF(10,10))
1846 << ItemDesc(0,2)
1847 .preferredSizeHint(sh: QSizeF(10,10))
1848 << ItemDesc(1,0)
1849 .preferredSizeHint(sh: QSizeF(10,10))
1850 << ItemDesc(1,1)
1851 .preferredSizeHint(sh: QSizeF(10,10))
1852 << ItemDesc(1,2)
1853 .preferredSizeHint(sh: QSizeF(10,10))
1854 )
1855 << QSizeF()
1856 << (SizeList()
1857 << QSizeF(10,10) << QSizeF(10,10) << QSizeF(10,10)
1858 << QSizeF(10,10) << QSizeF(10,10) << QSizeF(10,10)
1859 );
1860
1861 QTest::newRow(dataTag: "preferredsizeIsZero") << (ItemList()
1862 << ItemDesc(0,0)
1863 .preferredSizeHint(sh: QSizeF(0,10))
1864 << ItemDesc(0,1)
1865 .preferredSizeHint(sh: QSizeF(10,10))
1866 .maxSize(sz: QSizeF(20, 10))
1867 )
1868 << QSizeF(30, 10)
1869 << (SizeList()
1870 << QSizeF(10,10) << QSizeF(20,10)
1871 );
1872
1873 QTest::newRow(dataTag: "ignoreitem01") << (ItemList()
1874 << ItemDesc(0,0)
1875 .preferredSizeHint(sh: QSizeF(10,10))
1876 << ItemDesc(0,1)
1877 .sizePolicy(horAndVer: QSizePolicy::Ignored)
1878 .preferredSizeHint(sh: QSizeF(10,10))
1879 << ItemDesc(0,2)
1880 .preferredSizeHint(sh: QSizeF(10,10))
1881 << ItemDesc(1,0)
1882 .preferredSizeHint(sh: QSizeF(10,10))
1883 << ItemDesc(1,1)
1884 .preferredSizeHint(sh: QSizeF(10,10))
1885 << ItemDesc(1,2)
1886 .preferredSizeHint(sh: QSizeF(10,10))
1887 )
1888 << QSizeF()
1889 << (SizeList()
1890 << QSizeF(10,10) << QSizeF(10,10) << QSizeF(10,10)
1891 << QSizeF(10,10) << QSizeF(10,10) << QSizeF(10,10)
1892 );
1893
1894 QTest::newRow(dataTag: "ignoreitem01_resize120x40") << (ItemList()
1895 << ItemDesc(0,0)
1896 .preferredSizeHint(sh: QSizeF(10,10))
1897 << ItemDesc(0,1)
1898 .sizePolicy(horAndVer: QSizePolicy::Ignored)
1899 .preferredSizeHint(sh: QSizeF(20,10))
1900 << ItemDesc(0,2)
1901 .preferredSizeHint(sh: QSizeF(30,10))
1902 << ItemDesc(1,0)
1903 .preferredSizeHint(sh: QSizeF(10,10))
1904 << ItemDesc(1,1)
1905 .preferredSizeHint(sh: QSizeF(20,10))
1906 << ItemDesc(1,2)
1907 .preferredSizeHint(sh: QSizeF(30,10))
1908 )
1909 << QSizeF(120, 40)
1910 << (SizeList()
1911 << QSizeF(20,20) << QSizeF(40,20) << QSizeF(60,20)
1912 << QSizeF(20,20) << QSizeF(40,20) << QSizeF(60,20)
1913 );
1914
1915 QTest::newRow(dataTag: "ignoreitem11_resize120x40") << (ItemList()
1916 << ItemDesc(0,0)
1917 .preferredSizeHint(sh: QSizeF(10,10))
1918 << ItemDesc(0,1)
1919 .preferredSizeHint(sh: QSizeF(20,10))
1920 << ItemDesc(0,2)
1921 .preferredSizeHint(sh: QSizeF(30,10))
1922 << ItemDesc(1,0)
1923 .preferredSizeHint(sh: QSizeF(10,20))
1924 << ItemDesc(1,1)
1925 .sizePolicy(horAndVer: QSizePolicy::Ignored)
1926 .preferredSizeHint(sh: QSizeF(20,20))
1927 << ItemDesc(1,2)
1928 .preferredSizeHint(sh: QSizeF(30,20))
1929 )
1930 << QSizeF(120, 60)
1931 << (SizeList()
1932 << QSizeF(20,20) << QSizeF(40,20) << QSizeF(60,20)
1933 << QSizeF(20,40) << QSizeF(40,40) << QSizeF(60,40)
1934 );
1935
1936 QTest::newRow(dataTag: "ignoreitem01_span01_resize70x60") << (ItemList()
1937 << ItemDesc(0,0)
1938 .preferredSizeHint(sh: QSizeF(10,10))
1939 << ItemDesc(0,1)
1940 .preferredSizeHint(sh: QSizeF(20,10))
1941 .sizePolicy(horAndVer: QSizePolicy::Ignored)
1942 .rowSpan(span: 2)
1943 << ItemDesc(0,2)
1944 .preferredSizeHint(sh: QSizeF(30,10))
1945 << ItemDesc(1,0)
1946 .preferredSizeHint(sh: QSizeF(10,20))
1947 << ItemDesc(1,2)
1948 .preferredSizeHint(sh: QSizeF(30,20))
1949 )
1950 << QSizeF(70, 60)
1951 << (SizeList()
1952 << QSizeF(20,20) << QSizeF(10,60) << QSizeF(40,20)
1953 << QSizeF(20,40) << QSizeF(40,40)
1954 );
1955
1956 QTest::newRow(dataTag: "ignoreitem10_resize40x120") << (ItemList()
1957 << ItemDesc(0,0)
1958 .preferredSizeHint(sh: QSizeF(10,10))
1959 << ItemDesc(0,1)
1960 .preferredSizeHint(sh: QSizeF(10,10))
1961 << ItemDesc(1,0)
1962 .sizePolicy(horAndVer: QSizePolicy::Ignored)
1963 .preferredSizeHint(sh: QSizeF(10,20))
1964 << ItemDesc(1,1)
1965 .preferredSizeHint(sh: QSizeF(10,20))
1966 << ItemDesc(2,0)
1967 .preferredSizeHint(sh: QSizeF(10,30))
1968 << ItemDesc(2,1)
1969 .preferredSizeHint(sh: QSizeF(10,30))
1970 )
1971 << QSizeF(40, 120)
1972 << (SizeList()
1973 << QSizeF(20,20) << QSizeF(20,20)
1974 << QSizeF(20,40) << QSizeF(20,40)
1975 << QSizeF(20,60) << QSizeF(20,60)
1976 );
1977
1978 QTest::newRow(dataTag: "ignoreitem01_span02") << (ItemList()
1979 << ItemDesc(0,0)
1980 .preferredSizeHint(sh: QSizeF(10,10))
1981 << ItemDesc(0,1)
1982 .sizePolicy(horAndVer: QSizePolicy::Ignored)
1983 .preferredSizeHint(sh: QSizeF(10,20))
1984 .rowSpan(span: 2)
1985 << ItemDesc(0,2)
1986 .preferredSizeHint(sh: QSizeF(10,10))
1987 << ItemDesc(1,0)
1988 .preferredSizeHint(sh: QSizeF(10,10))
1989 << ItemDesc(1,2)
1990 .preferredSizeHint(sh: QSizeF(10,10))
1991 )
1992 << QSizeF()
1993 << (SizeList()
1994 << QSizeF(10,10) << QSizeF(0,20) << QSizeF(10,10)
1995 << QSizeF(10,10) << QSizeF(10,10)
1996 );
1997
1998 QTest::newRow(dataTag: "ignoreitem02_span02") << (ItemList()
1999 << ItemDesc(0,0)
2000 .preferredSizeHint(sh: QSizeF(10,10))
2001 << ItemDesc(0,1)
2002 .preferredSizeHint(sh: QSizeF(10,10))
2003 << ItemDesc(0,2)
2004 .sizePolicy(horAndVer: QSizePolicy::Ignored)
2005 .preferredSizeHint(sh: QSizeF(10,20))
2006 .rowSpan(span: 2)
2007 << ItemDesc(1,0)
2008 .preferredSizeHint(sh: QSizeF(10,10))
2009 << ItemDesc(1,1)
2010 .preferredSizeHint(sh: QSizeF(10,10))
2011 )
2012 << QSizeF()
2013 << (SizeList()
2014 << QSizeF(10,10) << QSizeF(10,10) << QSizeF(0,20)
2015 << QSizeF(10,10) << QSizeF(10,10)
2016 );
2017
2018 QTest::newRow(dataTag: "ignoreitem02_span00_span02") << (ItemList()
2019 << ItemDesc(0,0)
2020 .preferredSizeHint(sh: QSizeF(10,10))
2021 .rowSpan(span: 2)
2022 << ItemDesc(0,1)
2023 .preferredSizeHint(sh: QSizeF(10,10))
2024 << ItemDesc(0,2)
2025 .sizePolicy(horAndVer: QSizePolicy::Ignored)
2026 .preferredSizeHint(sh: QSizeF(10,20))
2027 .rowSpan(span: 2)
2028 << ItemDesc(1,1)
2029 .preferredSizeHint(sh: QSizeF(10,10))
2030 )
2031 << QSizeF()
2032 << (SizeList()
2033 << QSizeF(10,20) << QSizeF(10,10) << QSizeF(0,20)
2034 << QSizeF(10,10)
2035 );
2036
2037 QTest::newRow(dataTag: "ignoreitem00_colspan00") << (ItemList()
2038 << ItemDesc(0,0)
2039 .sizePolicy(horAndVer: QSizePolicy::Ignored)
2040 .preferredSizeHint(sh: QSizeF(10,20))
2041 .colSpan(span: 2)
2042 << ItemDesc(0,2)
2043 .preferredSizeHint(sh: QSizeF(10,10))
2044 << ItemDesc(1,0)
2045 .preferredSizeHint(sh: QSizeF(10,10))
2046 << ItemDesc(1,1)
2047 .preferredSizeHint(sh: QSizeF(10,10))
2048 << ItemDesc(1,2)
2049 .preferredSizeHint(sh: QSizeF(10,10))
2050 )
2051 << QSizeF()
2052 << (SizeList()
2053 << QSizeF(20,10) << QSizeF(10,10) << QSizeF(10,10)
2054 << QSizeF(10,10) << QSizeF(10,10)
2055 );
2056
2057 QTest::newRow(dataTag: "ignoreitem01_colspan01") << (ItemList()
2058 << ItemDesc(0,0)
2059 .preferredSizeHint(sh: QSizeF(10,10))
2060 << ItemDesc(0,1)
2061 .sizePolicy(horAndVer: QSizePolicy::Ignored)
2062 .preferredSizeHint(sh: QSizeF(10,20))
2063 .colSpan(span: 2)
2064 << ItemDesc(1,0)
2065 .preferredSizeHint(sh: QSizeF(10,10))
2066 << ItemDesc(1,1)
2067 .preferredSizeHint(sh: QSizeF(10,10))
2068 << ItemDesc(1,2)
2069 .preferredSizeHint(sh: QSizeF(10,10))
2070 )
2071 << QSizeF()
2072 << (SizeList()
2073 << QSizeF(10,10) << QSizeF(20,10) << QSizeF(10,10)
2074 << QSizeF(10,10) << QSizeF(10,10)
2075 );
2076
2077 QTest::newRow(dataTag: "ignorecolumn1_resize70x60") << (ItemList()
2078 << ItemDesc(0,0)
2079 .preferredSizeHint(sh: QSizeF(10,10))
2080 << ItemDesc(0,1)
2081 .sizePolicy(horAndVer: QSizePolicy::Ignored)
2082 .preferredSizeHint(sh: QSizeF(20,10))
2083 << ItemDesc(0,2)
2084 .preferredSizeHint(sh: QSizeF(30,10))
2085 << ItemDesc(1,0)
2086 .preferredSizeHint(sh: QSizeF(10,20))
2087 << ItemDesc(1,1)
2088 .sizePolicy(horAndVer: QSizePolicy::Ignored)
2089 .preferredSizeHint(sh: QSizeF(20,20))
2090 << ItemDesc(1,2)
2091 .preferredSizeHint(sh: QSizeF(30,20))
2092 )
2093 << QSizeF(70, 60)
2094 << (SizeList()
2095 << QSizeF(20,20) << QSizeF(10,20) << QSizeF(40,20)
2096 << QSizeF(20,40) << QSizeF(10,40) << QSizeF(40,40)
2097 );
2098
2099 QTest::newRow(dataTag: "ignorerow0") << (ItemList()
2100 << ItemDesc(0,0)
2101 .sizePolicy(horAndVer: QSizePolicy::Ignored)
2102 .preferredSizeHint(sh: QSizeF(10,10))
2103 << ItemDesc(0,1)
2104 .sizePolicy(horAndVer: QSizePolicy::Ignored)
2105 .preferredSizeHint(sh: QSizeF(10,10))
2106 << ItemDesc(0,2)
2107 .sizePolicy(horAndVer: QSizePolicy::Ignored)
2108 .preferredSizeHint(sh: QSizeF(10,10))
2109 << ItemDesc(1,0)
2110 .preferredSizeHint(sh: QSizeF(10,10))
2111 << ItemDesc(1,1)
2112 .preferredSizeHint(sh: QSizeF(10,10))
2113 << ItemDesc(1,2)
2114 .preferredSizeHint(sh: QSizeF(10,10))
2115 )
2116 << QSizeF()
2117 << (SizeList()
2118 << QSizeF(10,0) << QSizeF(10,0) << QSizeF(10,0)
2119 << QSizeF(10,10) << QSizeF(10,10) << QSizeF(10,10)
2120 );
2121
2122 QTest::newRow(dataTag: "ignorerow1") << (ItemList()
2123 << ItemDesc(0,0)
2124 .preferredSizeHint(sh: QSizeF(10,10))
2125 << ItemDesc(0,1)
2126 .preferredSizeHint(sh: QSizeF(10,10))
2127 << ItemDesc(0,2)
2128 .preferredSizeHint(sh: QSizeF(10,10))
2129 << ItemDesc(1,0)
2130 .sizePolicy(horAndVer: QSizePolicy::Ignored)
2131 .preferredSizeHint(sh: QSizeF(10,10))
2132 << ItemDesc(1,1)
2133 .sizePolicy(horAndVer: QSizePolicy::Ignored)
2134 .preferredSizeHint(sh: QSizeF(10,10))
2135 << ItemDesc(1,2)
2136 .sizePolicy(horAndVer: QSizePolicy::Ignored)
2137 .preferredSizeHint(sh: QSizeF(10,10))
2138 )
2139 << QSizeF()
2140 << (SizeList()
2141 << QSizeF(10,10) << QSizeF(10,10) << QSizeF(10,10)
2142 << QSizeF(10,0) << QSizeF(10,0) << QSizeF(10,0)
2143 );
2144
2145 QTest::newRow(dataTag: "ignorerow0_resize60x50") << (ItemList()
2146 << ItemDesc(0,0)
2147 .sizePolicy(horAndVer: QSizePolicy::Ignored)
2148 .preferredSizeHint(sh: QSizeF(10,10))
2149 << ItemDesc(0,1)
2150 .sizePolicy(horAndVer: QSizePolicy::Ignored)
2151 .preferredSizeHint(sh: QSizeF(20,10))
2152 << ItemDesc(0,2)
2153 .sizePolicy(horAndVer: QSizePolicy::Ignored)
2154 .preferredSizeHint(sh: QSizeF(30,10))
2155 << ItemDesc(1,0)
2156 .preferredSizeHint(sh: QSizeF(10,30))
2157 << ItemDesc(1,1)
2158 .preferredSizeHint(sh: QSizeF(20,30))
2159 << ItemDesc(1,2)
2160 .preferredSizeHint(sh: QSizeF(30,30))
2161 )
2162 << QSizeF(60, 50)
2163 << (SizeList()
2164 << QSizeF(10,10) << QSizeF(20,10) << QSizeF(30,10)
2165 << QSizeF(10,40) << QSizeF(20,40) << QSizeF(30,40)
2166 );
2167
2168}
2169
2170void tst_QGraphicsGridLayout::defaultStretchFactors()
2171{
2172 QFETCH(ItemList, itemDescriptions);
2173 QFETCH(QSizeF, newSize);
2174 QFETCH(SizeList, expectedSizes);
2175
2176 QGraphicsScene scene;
2177 QGraphicsView view(&scene);
2178 QGraphicsWidget *widget = new QGraphicsWidget(0, Qt::Window);
2179 QGraphicsGridLayout *layout = new QGraphicsGridLayout;
2180 scene.addItem(item: widget);
2181 widget->setLayout(layout);
2182 layout->setContentsMargins(left: 0, top: 0, right: 0, bottom: 0);
2183 layout->setSpacing(0.0);
2184 widget->setContentsMargins(left: 0, top: 0, right: 0, bottom: 0);
2185
2186 int i;
2187 for (i = 0; i < itemDescriptions.count(); ++i) {
2188 ItemDesc desc = itemDescriptions.at(i);
2189 RectWidget *item = new RectWidget(widget);
2190 desc.apply(layout, item);
2191 }
2192
2193 QApplication::sendPostedEvents(receiver: 0, event_type: 0);
2194
2195 widget->show();
2196 view.show();
2197 view.resize(w: 400,h: 300);
2198 if (newSize.isValid())
2199 widget->resize(size: newSize);
2200
2201 QApplication::sendPostedEvents(receiver: 0, event_type: 0);
2202 for (i = 0; i < expectedSizes.count(); ++i) {
2203 QSizeF itemSize = layout->itemAt(index: i)->geometry().size();
2204 QCOMPARE(itemSize, expectedSizes.at(i));
2205 }
2206
2207 delete widget;
2208}
2209
2210typedef QList<QRectF> RectList;
2211
2212void tst_QGraphicsGridLayout::alignment2_data()
2213{
2214 QTest::addColumn<ItemList>(name: "itemDescriptions");
2215 QTest::addColumn<QSizeF>(name: "newSize");
2216 QTest::addColumn<RectList>(name: "expectedGeometries");
2217
2218 QTest::newRow(dataTag: "hor_sizepolicy_fixed") << (ItemList()
2219 << ItemDesc(0,0)
2220 .preferredSizeHint(sh: QSizeF(10,20))
2221 .sizePolicyV(ver: QSizePolicy::Fixed)
2222 << ItemDesc(0,1)
2223 .preferredSizeHint(sh: QSizeF(10,10))
2224 .sizePolicyV(ver: QSizePolicy::Fixed)
2225 )
2226 << QSizeF()
2227 << (RectList()
2228 << QRectF(0, 0, 10,20) << QRectF(10, 0, 10,10)
2229 );
2230
2231 QTest::newRow(dataTag: "hor_sizepolicy_fixed_alignvcenter") << (ItemList()
2232 << ItemDesc(0,0)
2233 .preferredSizeHint(sh: QSizeF(10,20))
2234 .sizePolicyV(ver: QSizePolicy::Fixed)
2235 << ItemDesc(0,1)
2236 .preferredSizeHint(sh: QSizeF(10,10))
2237 .sizePolicyV(ver: QSizePolicy::Fixed)
2238 .alignment(alignment: Qt::AlignVCenter)
2239 )
2240 << QSizeF()
2241 << (RectList()
2242 << QRectF(0, 0, 10,20) << QRectF(10, 5, 10,10)
2243 );
2244
2245 QTest::newRow(dataTag: "hor_sizepolicy_fixed_aligntop") << (ItemList()
2246 << ItemDesc(0,0)
2247 .preferredSizeHint(sh: QSizeF(10,20))
2248 .sizePolicyV(ver: QSizePolicy::Fixed)
2249 << ItemDesc(0,1)
2250 .preferredSizeHint(sh: QSizeF(10,10))
2251 .sizePolicyV(ver: QSizePolicy::Fixed)
2252 .alignment(alignment: Qt::AlignTop)
2253 )
2254 << QSizeF()
2255 << (RectList()
2256 << QRectF(0, 0, 10,20) << QRectF(10, 0, 10,10)
2257 );
2258
2259 QTest::newRow(dataTag: "hor_sizepolicy_fixed_alignbottom") << (ItemList()
2260 << ItemDesc(0,0)
2261 .preferredSizeHint(sh: QSizeF(10,20))
2262 .sizePolicyV(ver: QSizePolicy::Fixed)
2263 << ItemDesc(0,1)
2264 .preferredSizeHint(sh: QSizeF(10,10))
2265 .sizePolicyV(ver: QSizePolicy::Fixed)
2266 .alignment(alignment: Qt::AlignBottom)
2267 )
2268 << QSizeF()
2269 << (RectList()
2270 << QRectF(0, 0, 10,20) << QRectF(10, 10, 10,10)
2271 );
2272
2273 QTest::newRow(dataTag: "ver_sizepolicy_fixed") << (ItemList()
2274 << ItemDesc(0,0)
2275 .preferredSizeHint(sh: QSizeF(20,10))
2276 .sizePolicyH(hor: QSizePolicy::Fixed)
2277 << ItemDesc(1,0)
2278 .preferredSizeHint(sh: QSizeF(10,10))
2279 .sizePolicyH(hor: QSizePolicy::Fixed)
2280 )
2281 << QSizeF()
2282 << (RectList()
2283 << QRectF(0, 0, 20,10) << QRectF(0, 10, 10,10)
2284 );
2285
2286 QTest::newRow(dataTag: "ver_sizepolicy_fixed_alignhcenter") << (ItemList()
2287 << ItemDesc(0,0)
2288 .preferredSizeHint(sh: QSizeF(20,10))
2289 .sizePolicyH(hor: QSizePolicy::Fixed)
2290 << ItemDesc(1,0)
2291 .preferredSizeHint(sh: QSizeF(10,10))
2292 .sizePolicyH(hor: QSizePolicy::Fixed)
2293 .alignment(alignment: Qt::AlignHCenter)
2294 )
2295 << QSizeF()
2296 << (RectList()
2297 << QRectF(0, 0, 20,10) << QRectF(5, 10, 10,10)
2298 );
2299
2300 QTest::newRow(dataTag: "ver_sizepolicy_fixed_alignleft") << (ItemList()
2301 << ItemDesc(0,0)
2302 .preferredSizeHint(sh: QSizeF(20,10))
2303 .sizePolicyH(hor: QSizePolicy::Fixed)
2304 << ItemDesc(1,0)
2305 .preferredSizeHint(sh: QSizeF(10,10))
2306 .sizePolicyH(hor: QSizePolicy::Fixed)
2307 .alignment(alignment: Qt::AlignLeft)
2308 )
2309 << QSizeF()
2310 << (RectList()
2311 << QRectF(0, 0, 20,10) << QRectF(0, 10, 10,10)
2312 );
2313
2314 QTest::newRow(dataTag: "ver_sizepolicy_fixed_alignright") << (ItemList()
2315 << ItemDesc(0,0)
2316 .preferredSizeHint(sh: QSizeF(20,10))
2317 .sizePolicyH(hor: QSizePolicy::Fixed)
2318 << ItemDesc(1,0)
2319 .preferredSizeHint(sh: QSizeF(10,10))
2320 .sizePolicyH(hor: QSizePolicy::Fixed)
2321 .alignment(alignment: Qt::AlignRight)
2322 )
2323 << QSizeF()
2324 << (RectList()
2325 << QRectF(0, 0, 20,10) << QRectF(10, 10, 10,10)
2326 );
2327}
2328
2329void tst_QGraphicsGridLayout::alignment2()
2330{
2331 QFETCH(ItemList, itemDescriptions);
2332 QFETCH(QSizeF, newSize);
2333 QFETCH(RectList, expectedGeometries);
2334
2335 QGraphicsScene scene;
2336 QGraphicsView view(&scene);
2337 QGraphicsWidget *widget = new QGraphicsWidget(0, Qt::Window);
2338 QGraphicsGridLayout *layout = new QGraphicsGridLayout;
2339 scene.addItem(item: widget);
2340 widget->setLayout(layout);
2341 layout->setContentsMargins(left: 0, top: 0, right: 0, bottom: 0);
2342 layout->setSpacing(0.0);
2343 widget->setContentsMargins(left: 0, top: 0, right: 0, bottom: 0);
2344
2345 int i;
2346 for (i = 0; i < itemDescriptions.count(); ++i) {
2347 ItemDesc desc = itemDescriptions.at(i);
2348 RectWidget *item = new RectWidget(widget);
2349 desc.apply(layout, item);
2350 }
2351
2352 QApplication::sendPostedEvents(receiver: 0, event_type: 0);
2353
2354 widget->show();
2355 view.resize(w: 400,h: 300);
2356 view.show();
2357 if (newSize.isValid())
2358 widget->resize(size: newSize);
2359
2360 QApplication::sendPostedEvents(receiver: 0, event_type: 0);
2361 for (i = 0; i < expectedGeometries.count(); ++i) {
2362 QRectF itemRect = layout->itemAt(index: i)->geometry();
2363 QCOMPARE(itemRect, expectedGeometries.at(i));
2364 }
2365
2366 delete widget;
2367}
2368
2369static QSizeF hfw1(Qt::SizeHint, const QSizeF &constraint)
2370{
2371 QSizeF result(constraint);
2372 const qreal ch = constraint.height();
2373 const qreal cw = constraint.width();
2374 if (cw < 0 && ch < 0) {
2375 return QSizeF(50, 400);
2376 } else if (cw > 0) {
2377 result.setHeight(20000./cw);
2378 }
2379 return result;
2380}
2381
2382static QSizeF wfh1(Qt::SizeHint, const QSizeF &constraint)
2383{
2384 QSizeF result(constraint);
2385 const qreal ch = constraint.height();
2386 const qreal cw = constraint.width();
2387 if (cw < 0 && ch < 0) {
2388 return QSizeF(400, 50);
2389 } else if (ch > 0) {
2390 result.setWidth(20000./ch);
2391 }
2392 return result;
2393}
2394
2395static QSizeF wfh2(Qt::SizeHint, const QSizeF &constraint)
2396{
2397 QSizeF result(constraint);
2398 const qreal ch = constraint.height();
2399 const qreal cw = constraint.width();
2400 if (ch < 0 && cw < 0)
2401 return QSizeF(50, 50);
2402 if (ch >= 0)
2403 result.setWidth(ch);
2404 return result;
2405}
2406
2407static QSizeF hfw3(Qt::SizeHint, const QSizeF &constraint)
2408{
2409 QSizeF result(constraint);
2410 const qreal ch = constraint.height();
2411 const qreal cw = constraint.width();
2412 if (cw < 0 && ch < 0) {
2413 return QSizeF(10, 10);
2414 } else if (cw > 0) {
2415 result.setHeight(100./cw);
2416 }
2417 return result;
2418}
2419
2420static QSizeF hfw2(Qt::SizeHint /*which*/, const QSizeF &constraint)
2421{
2422 return QSizeF(constraint.width(), constraint.width());
2423}
2424
2425void tst_QGraphicsGridLayout::geometries_data()
2426{
2427
2428 QTest::addColumn<ItemList>(name: "itemDescriptions");
2429 QTest::addColumn<QSizeF>(name: "newSize");
2430 QTest::addColumn<RectList>(name: "expectedGeometries");
2431
2432 QTest::newRow(dataTag: "combine_max_sizes") << (ItemList()
2433 << ItemDesc(0,0)
2434 .maxSize(sz: QSizeF(50,10))
2435 << ItemDesc(1,0)
2436 .maxSize(sz: QSizeF(10,10))
2437 )
2438 << QSizeF(50, 20)
2439 << (RectList()
2440 << QRectF(0, 0, 50,10) << QRectF(0, 10, 10,10)
2441 );
2442
2443 QTest::newRow(dataTag: "combine_min_sizes") << (ItemList()
2444 << ItemDesc(0,0)
2445 .minSize(sz: QSizeF(50,10))
2446 << ItemDesc(1,0)
2447 .minSize(sz: QSizeF(10,10))
2448 )
2449 << QSizeF(60, 20)
2450 << (RectList()
2451 << QRectF(0, 0, 60,10) << QRectF(0, 10, 60,10)
2452 );
2453
2454 // change layout height and verify
2455 QTest::newRow(dataTag: "hfw-100x401") << (ItemList()
2456 << ItemDesc(0,0)
2457 .minSize(sz: QSizeF(1,1))
2458 .preferredSize(sz: QSizeF(50,10))
2459 .maxSize(sz: QSizeF(100, 100))
2460 << ItemDesc(0,1)
2461 .minSize(sz: QSizeF(1,1))
2462 .preferredSize(sz: QSizeF(50,10))
2463 .maxSize(sz: QSizeF(100, 100))
2464 << ItemDesc(1,0)
2465 .minSize(sz: QSizeF(1,1))
2466 .preferredSize(sz: QSizeF(50,10))
2467 .maxSize(sz: QSizeF(100, 100))
2468 << ItemDesc(1,1)
2469 .minSize(sz: QSizeF(40,-1))
2470 .preferredSize(sz: QSizeF(50,-1))
2471 .maxSize(sz: QSizeF(500, -1))
2472 .dynamicConstraint(fnConstraint: hfw1, orientation: Qt::Vertical)
2473 )
2474 << QSizeF(100, 401)
2475 << (RectList()
2476 << QRectF(0, 0, 50, 1) << QRectF(50, 0, 50, 1)
2477 << QRectF(0, 1, 50,100) << QRectF(50, 1, 50,400)
2478 );
2479 QTest::newRow(dataTag: "hfw-h408") << (ItemList()
2480 << ItemDesc(0,0)
2481 .minSize(sz: QSizeF(1,1))
2482 .preferredSize(sz: QSizeF(50,10))
2483 .maxSize(sz: QSizeF(100, 100))
2484 << ItemDesc(0,1)
2485 .minSize(sz: QSizeF(1,1))
2486 .preferredSize(sz: QSizeF(50,10))
2487 .maxSize(sz: QSizeF(100, 100))
2488 << ItemDesc(1,0)
2489 .minSize(sz: QSizeF(1,1))
2490 .preferredSize(sz: QSizeF(50,10))
2491 .maxSize(sz: QSizeF(100, 100))
2492 << ItemDesc(1,1)
2493 .sizeHint(which: Qt::MinimumSize, sh: QSizeF(40,40))
2494 .sizeHint(which: Qt::PreferredSize, sh: QSizeF(50,400))
2495 .sizeHint(which: Qt::MaximumSize, sh: QSizeF(500, 500))
2496 .dynamicConstraint(fnConstraint: hfw1, orientation: Qt::Vertical)
2497 )
2498 << QSizeF(100, 408)
2499 << (RectList()
2500 << QRectF(0, 0, 50, 8) << QRectF(50, 0, 50, 8)
2501 << QRectF(0, 8, 50,100) << QRectF(50, 8, 50,400)
2502 );
2503 QTest::newRow(dataTag: "hfw-h410") << (ItemList()
2504 << ItemDesc(0,0)
2505 .minSize(sz: QSizeF(1,1))
2506 .preferredSize(sz: QSizeF(50,10))
2507 .maxSize(sz: QSizeF(100, 100))
2508 << ItemDesc(0,1)
2509 .minSize(sz: QSizeF(1,1))
2510 .preferredSize(sz: QSizeF(50,10))
2511 .maxSize(sz: QSizeF(100, 100))
2512 << ItemDesc(1,0)
2513 .minSize(sz: QSizeF(1,1))
2514 .preferredSize(sz: QSizeF(50,10))
2515 .maxSize(sz: QSizeF(100, 100))
2516 << ItemDesc(1,1)
2517 .minSize(sz: QSizeF(40,40))
2518 .preferredSize(sz: QSizeF(50,400))
2519 .maxSize(sz: QSizeF(500, 500))
2520 .dynamicConstraint(fnConstraint: hfw1, orientation: Qt::Vertical)
2521 )
2522 << QSizeF(100, 410)
2523 << (RectList()
2524 << QRectF(0, 0, 50,10) << QRectF(50, 0, 50,10)
2525 << QRectF(0, 10, 50,100) << QRectF(50, 10, 50,400)
2526 );
2527
2528 QTest::newRow(dataTag: "hfw-100x470") << (ItemList()
2529 << ItemDesc(0,0)
2530 .minSize(sz: QSizeF(1,1))
2531 .preferredSize(sz: QSizeF(50,10))
2532 .maxSize(sz: QSizeF(100, 100))
2533 << ItemDesc(0,1)
2534 .minSize(sz: QSizeF(1,1))
2535 .preferredSize(sz: QSizeF(50,10))
2536 .maxSize(sz: QSizeF(100, 100))
2537 << ItemDesc(1,0)
2538 .minSize(sz: QSizeF(1,1))
2539 .preferredSize(sz: QSizeF(50,10))
2540 .maxSize(sz: QSizeF(100, 100))
2541 << ItemDesc(1,1)
2542 .sizeHint(which: Qt::MinimumSize, sh: QSizeF(40,40))
2543 .sizeHint(which: Qt::PreferredSize, sh: QSizeF(50,400))
2544 .sizeHint(which: Qt::MaximumSize, sh: QSizeF(500,500))
2545 .dynamicConstraint(fnConstraint: hfw1, orientation: Qt::Vertical)
2546 )
2547 << QSizeF(100, 470)
2548 << (RectList()
2549 << QRectF(0, 0, 50,70) << QRectF(50, 0, 50,70)
2550 << QRectF(0, 70, 50,100) << QRectF(50, 70, 50,400)
2551 );
2552
2553 // change layout width and verify
2554 QTest::newRow(dataTag: "hfw-100x401") << (ItemList()
2555 << ItemDesc(0,0)
2556 .minSize(sz: QSizeF(1,1))
2557 .preferredSize(sz: QSizeF(50,10))
2558 .maxSize(sz: QSizeF(100, 100))
2559 << ItemDesc(0,1)
2560 .minSize(sz: QSizeF(1,1))
2561 .preferredSize(sz: QSizeF(50,10))
2562 .maxSize(sz: QSizeF(100, 100))
2563 << ItemDesc(1,0)
2564 .minSize(sz: QSizeF(1,1))
2565 .preferredSize(sz: QSizeF(50,10))
2566 .maxSize(sz: QSizeF(100, 100))
2567 << ItemDesc(1,1)
2568 .minSize(sz: QSizeF(-1,-1))
2569 .preferredSize(sz: QSizeF(-1,-1))
2570 .maxSize(sz: QSizeF(-1, -1))
2571 .dynamicConstraint(fnConstraint: hfw1, orientation: Qt::Vertical)
2572 )
2573 << QSizeF(100, 401)
2574 << (RectList()
2575 << QRectF( 0, 0, 50, 1) << QRectF( 50, 0, 50, 1)
2576 << QRectF( 0, 1, 50, 100) << QRectF( 50, 1, 50, 400)
2577 );
2578
2579 QTest::newRow(dataTag: "hfw-160x350") << (ItemList()
2580 << ItemDesc(0,0)
2581 .minSize(sz: QSizeF(1,1))
2582 .preferredSize(sz: QSizeF(50,10))
2583 .maxSize(sz: QSizeF(100, 100))
2584 << ItemDesc(0,1)
2585 .minSize(sz: QSizeF(1,1))
2586 .preferredSize(sz: QSizeF(50,10))
2587 .maxSize(sz: QSizeF(100, 100))
2588 << ItemDesc(1,0)
2589 .minSize(sz: QSizeF(1,1))
2590 .preferredSize(sz: QSizeF(50,10))
2591 .maxSize(sz: QSizeF(100, 100))
2592 << ItemDesc(1,1)
2593 .sizeHint(which: Qt::MinimumSize, sh: QSizeF(40,40))
2594 .sizeHint(which: Qt::PreferredSize, sh: QSizeF(50,400))
2595 .sizeHint(which: Qt::MaximumSize, sh: QSizeF(5000,5000))
2596 .dynamicConstraint(fnConstraint: hfw1, orientation: Qt::Vertical)
2597 )
2598 << QSizeF(160, 350)
2599 << (RectList()
2600 << QRectF( 0, 0, 80, 100) << QRectF( 80, 0, 80, 100)
2601 << QRectF( 0, 100, 80, 100) << QRectF( 80, 100, 80, 250)
2602 );
2603
2604 QTest::newRow(dataTag: "hfw-160x300") << (ItemList()
2605 << ItemDesc(0,0)
2606 .minSize(sz: QSizeF(1,1))
2607 .preferredSize(sz: QSizeF(50,10))
2608 .maxSize(sz: QSizeF(100, 100))
2609 << ItemDesc(0,1)
2610 .minSize(sz: QSizeF(1,1))
2611 .preferredSize(sz: QSizeF(50,10))
2612 .maxSize(sz: QSizeF(100, 100))
2613 << ItemDesc(1,0)
2614 .minSize(sz: QSizeF(1,1))
2615 .preferredSize(sz: QSizeF(50,10))
2616 .maxSize(sz: QSizeF(100, 100))
2617 << ItemDesc(1,1)
2618 .sizeHint(which: Qt::MinimumSize, sh: QSizeF(40,40))
2619 .sizeHint(which: Qt::PreferredSize, sh: QSizeF(50,400))
2620 .sizeHint(which: Qt::MaximumSize, sh: QSizeF(5000, 5000))
2621 .dynamicConstraint(fnConstraint: hfw1, orientation: Qt::Vertical)
2622 )
2623 << QSizeF(160, 300)
2624 << (RectList()
2625 << QRectF( 0, 0, 80, 50) << QRectF( 80, 0, 80, 50)
2626 << QRectF( 0, 50, 80, 100) << QRectF( 80, 50, 80, 250)
2627 );
2628
2629 QTest::newRow(dataTag: "hfw-20x40") << (ItemList()
2630 << ItemDesc(0,0)
2631 .minSize(sz: QSizeF(1,10))
2632 .preferredSize(sz: QSizeF(50,50))
2633 .maxSize(sz: QSizeF(100, 100))
2634 << ItemDesc(0,1)
2635 .minSize(sz: QSizeF(1,1))
2636 .preferredSize(sz: QSizeF(50,50))
2637 .maxSize(sz: QSizeF(100, 100))
2638 << ItemDesc(1,0)
2639 .minSize(sz: QSizeF(1,1))
2640 .preferredSize(sz: QSizeF(50,50))
2641 .maxSize(sz: QSizeF(100, 100))
2642 << ItemDesc(1,1)
2643 .sizeHint(which: Qt::MinimumSize, sh: QSizeF(1, 1))
2644 .sizeHint(which: Qt::PreferredSize, sh: QSizeF(50, 50))
2645 .sizeHint(which: Qt::MaximumSize, sh: QSizeF(100, 100))
2646 .dynamicConstraint(fnConstraint: hfw3, orientation: Qt::Vertical)
2647 )
2648 << QSizeF(20, 40)
2649 << (RectList()
2650 << QRectF(0, 0, 10, 20) << QRectF(10, 0, 10, 20)
2651 << QRectF(0, 20, 10, 20) << QRectF(10, 20, 10, 10)
2652 );
2653
2654 QTest::newRow(dataTag: "wfh-300x160") << (ItemList()
2655 << ItemDesc(0,0)
2656 .minSize(sz: QSizeF(1,1))
2657 .preferredSize(sz: QSizeF(10,50))
2658 .maxSize(sz: QSizeF(100, 100))
2659 << ItemDesc(0,1)
2660 .minSize(sz: QSizeF(1,1))
2661 .preferredSize(sz: QSizeF(10,50))
2662 .maxSize(sz: QSizeF(100, 100))
2663 << ItemDesc(1,0)
2664 .minSize(sz: QSizeF(1,1))
2665 .preferredSize(sz: QSizeF(10,50))
2666 .maxSize(sz: QSizeF(100, 100))
2667 << ItemDesc(1,1)
2668 .sizeHint(which: Qt::MinimumSize, sh: QSizeF(10,10))
2669 .sizeHint(which: Qt::PreferredSize, sh: QSizeF(400,50))
2670 .sizeHint(which: Qt::MaximumSize, sh: QSizeF(5000, 5000))
2671 .dynamicConstraint(fnConstraint: wfh1, orientation: Qt::Horizontal)
2672 )
2673 << QSizeF(300, 160)
2674 << (RectList()
2675 << QRectF( 0, 0, 50, 80) << QRectF( 50, 0, 100, 80)
2676 << QRectF( 0, 80, 50, 80) << QRectF( 50, 80, 250, 80)
2677 );
2678
2679 QTest::newRow(dataTag: "wfh-40x20") << (ItemList()
2680 << ItemDesc(0,0)
2681 .minSize(sz: QSizeF(1,1))
2682 .preferredSize(sz: QSizeF(50,50))
2683 .maxSize(sz: QSizeF(100, 100))
2684 << ItemDesc(0,1)
2685 .minSize(sz: QSizeF(1,1))
2686 .preferredSize(sz: QSizeF(50,50))
2687 .maxSize(sz: QSizeF(100, 100))
2688 << ItemDesc(1,0)
2689 // Note, must be 10 in order to match stretching of wfh item
2690 // below (the same stretch makes it easier to test)
2691 .minSize(sz: QSizeF(10,1))
2692 .preferredSize(sz: QSizeF(50,50))
2693 .maxSize(sz: QSizeF(100, 100))
2694 << ItemDesc(1,1)
2695 .sizeHint(which: Qt::MinimumSize, sh: QSizeF(1,1))
2696 .sizeHint(which: Qt::PreferredSize, sh: QSizeF(50,50))
2697 .sizeHint(which: Qt::MaximumSize, sh: QSizeF(100, 100))
2698 .dynamicConstraint(fnConstraint: wfh2, orientation: Qt::Horizontal)
2699 )
2700 << QSizeF(40, 20)
2701 << (RectList()
2702 << QRectF(0, 0, 20, 10) << QRectF(20, 0, 20, 10)
2703 << QRectF(0, 10, 20, 10) << QRectF(20, 10, 10, 10)
2704 );
2705
2706 QTest::newRow(dataTag: "wfh-400x160") << (ItemList()
2707 << ItemDesc(0,0)
2708 .minSize(sz: QSizeF(1,1))
2709 .preferredSize(sz: QSizeF(50,50))
2710 .maxSize(sz: QSizeF(100, 100))
2711 << ItemDesc(0,1)
2712 .minSize(sz: QSizeF(1,1))
2713 .preferredSize(sz: QSizeF(50,50))
2714 .maxSize(sz: QSizeF(100, 100))
2715 << ItemDesc(1,0)
2716 .minSize(sz: QSizeF(1,1))
2717 .preferredSize(sz: QSizeF(50,50))
2718 .maxSize(sz: QSizeF(100, 100))
2719 << ItemDesc(1,1)
2720 .sizeHint(which: Qt::MinimumSize, sh: QSizeF(1,1))
2721 .sizeHint(which: Qt::PreferredSize, sh: QSizeF(50,50))
2722 .sizeHint(which: Qt::MaximumSize, sh: QSizeF(100, 100))
2723 .dynamicConstraint(fnConstraint: wfh2, orientation: Qt::Horizontal)
2724 )
2725
2726 << QSizeF(400, 160)
2727 << (RectList()
2728 << QRectF(0, 0, 100, 80) << QRectF(100, 0, 100, 80)
2729 << QRectF(0, 80, 100, 80) << QRectF(100, 80, 80, 80)
2730 );
2731
2732 QTest::newRow(dataTag: "wfh-160x100") << (ItemList()
2733 << ItemDesc(0,0)
2734 .minSize(sz: QSizeF(1,1))
2735 // Note, preferred width must be 50 in order to match
2736 // preferred width of wfh item below.
2737 // (The same preferred size makes the stretch the same, and
2738 // makes it easier to test) (The stretch algorithm is a
2739 // blackbox)
2740 .preferredSize(sz: QSizeF(50,50))
2741 .maxSize(sz: QSizeF(100, 100))
2742 << ItemDesc(0,1)
2743 .minSize(sz: QSizeF(1,1))
2744 .preferredSize(sz: QSizeF(10,50))
2745 .maxSize(sz: QSizeF(100, 100))
2746 << ItemDesc(1,0)
2747 .minSize(sz: QSizeF(1,1))
2748 .preferredSize(sz: QSizeF(10,50))
2749 .maxSize(sz: QSizeF(100, 100))
2750 << ItemDesc(1,1)
2751 .sizeHint(which: Qt::MinimumSize, sh: QSizeF(1,1))
2752 .sizeHint(which: Qt::PreferredSize, sh: QSizeF(10,50))
2753 .sizeHint(which: Qt::MaximumSize, sh: QSizeF(500, 500))
2754 .dynamicConstraint(fnConstraint: wfh2, orientation: Qt::Horizontal)
2755 )
2756 << QSizeF(160, 100)
2757 << (RectList()
2758 << QRectF(0, 0, 80, 50) << QRectF( 80, 0, 80, 50)
2759 << QRectF(0, 50, 80, 50) << QRectF( 80, 50, 50, 50)
2760 );
2761
2762 QTest::newRow(dataTag: "hfw-h470") << (ItemList()
2763 << ItemDesc(0,0)
2764 .minSize(sz: QSizeF(1,1))
2765 .preferredSize(sz: QSizeF(50,10))
2766 .maxSize(sz: QSizeF(100, 100))
2767 << ItemDesc(0,1)
2768 .minSize(sz: QSizeF(1,1))
2769 .preferredSize(sz: QSizeF(50,10))
2770 .maxSize(sz: QSizeF(100, 100))
2771 << ItemDesc(1,0)
2772 .minSize(sz: QSizeF(1,1))
2773 .preferredSize(sz: QSizeF(50,10))
2774 .maxSize(sz: QSizeF(100, 100))
2775 << ItemDesc(1,1)
2776 .sizeHint(which: Qt::MinimumSize, sh: QSizeF(40,40))
2777 .sizeHint(which: Qt::PreferredSize, sh: QSizeF(50,400))
2778 .sizeHint(which: Qt::MaximumSize, sh: QSizeF(500,500))
2779 .dynamicConstraint(fnConstraint: hfw1, orientation: Qt::Vertical)
2780 )
2781 << QSizeF(100, 470)
2782 << (RectList()
2783 << QRectF(0, 0, 50,70) << QRectF(50, 0, 50,70)
2784 << QRectF(0, 70, 50,100) << QRectF(50, 70, 50,400)
2785 );
2786
2787 // change layout width and verify
2788 QTest::newRow(dataTag: "hfw-w100") << (ItemList()
2789 << ItemDesc(0,0)
2790 .minSize(sz: QSizeF(1,1))
2791 .preferredSize(sz: QSizeF(50,10))
2792 .maxSize(sz: QSizeF(100, 100))
2793 << ItemDesc(0,1)
2794 .minSize(sz: QSizeF(1,1))
2795 .preferredSize(sz: QSizeF(50,10))
2796 .maxSize(sz: QSizeF(100, 100))
2797 << ItemDesc(1,0)
2798 .minSize(sz: QSizeF(1,1))
2799 .preferredSize(sz: QSizeF(50,10))
2800 .maxSize(sz: QSizeF(100, 100))
2801 << ItemDesc(1,1)
2802 .sizeHint(which: Qt::MinimumSize, sh: QSizeF(40,40))
2803 .sizeHint(which: Qt::PreferredSize, sh: QSizeF(50,400))
2804 .sizeHint(which: Qt::MaximumSize, sh: QSizeF(5000,5000))
2805 .dynamicConstraint(fnConstraint: hfw1, orientation: Qt::Vertical)
2806 )
2807 << QSizeF(100, 401)
2808 << (RectList()
2809 << QRectF( 0, 0, 50, 1) << QRectF( 50, 0, 50, 1)
2810 << QRectF( 0, 1, 50, 100) << QRectF( 50, 1, 50, 400)
2811 );
2812
2813 QTest::newRow(dataTag: "hfw-w160") << (ItemList()
2814 << ItemDesc(0,0)
2815 .minSize(sz: QSizeF(1,1))
2816 .preferredSize(sz: QSizeF(50,10))
2817 .maxSize(sz: QSizeF(100, 100))
2818 << ItemDesc(0,1)
2819 .minSize(sz: QSizeF(1,1))
2820 .preferredSize(sz: QSizeF(50,10))
2821 .maxSize(sz: QSizeF(100, 100))
2822 << ItemDesc(1,0)
2823 .minSize(sz: QSizeF(1,1))
2824 .preferredSize(sz: QSizeF(50,10))
2825 .maxSize(sz: QSizeF(100, 100))
2826 << ItemDesc(1,1)
2827 .sizeHint(which: Qt::MinimumSize, sh: QSizeF(40,40))
2828 .sizeHint(which: Qt::PreferredSize, sh: QSizeF(50,400))
2829 .sizeHint(which: Qt::MaximumSize, sh: QSizeF(5000,5000))
2830 .dynamicConstraint(fnConstraint: hfw1, orientation: Qt::Vertical)
2831 )
2832 << QSizeF(160, 350)
2833 << (RectList()
2834 << QRectF( 0, 0, 80, 100) << QRectF( 80, 0, 80, 100)
2835 << QRectF( 0, 100, 80, 100) << QRectF( 80, 100, 80, 250)
2836 );
2837
2838 QTest::newRow(dataTag: "hfw-w500") << (ItemList()
2839 << ItemDesc(0,0)
2840 .minSize(sz: QSizeF(1,1))
2841 .preferredSize(sz: QSizeF(50,10))
2842 .maxSize(sz: QSizeF(100, 100))
2843 << ItemDesc(0,1)
2844 .minSize(sz: QSizeF(1,1))
2845 .preferredSize(sz: QSizeF(50,10))
2846 .maxSize(sz: QSizeF(100, 100))
2847 << ItemDesc(1,0)
2848 .minSize(sz: QSizeF(1,1))
2849 .preferredSize(sz: QSizeF(50,10))
2850 .maxSize(sz: QSizeF(100, 100))
2851 << ItemDesc(1,1)
2852 .sizeHint(which: Qt::MinimumSize, sh: QSizeF(40,40))
2853 .sizeHint(which: Qt::PreferredSize, sh: QSizeF(50,400))
2854 .sizeHint(which: Qt::MaximumSize, sh: QSizeF(5000,5000))
2855 .dynamicConstraint(fnConstraint: hfw1, orientation: Qt::Vertical)
2856 )
2857 << QSizeF(500, 200)
2858 << (RectList()
2859 << QRectF( 0, 0, 100, 100) << QRectF(100, 0, 100, 100)
2860 << QRectF( 0, 100, 100, 100) << QRectF(100, 100, 400, 50)
2861 );
2862
2863 QTest::newRow(dataTag: "hfw-alignment-defaults") << (ItemList()
2864 << ItemDesc(0,0)
2865 .minSize(sz: QSizeF(100, 100))
2866 .maxSize(sz: QSizeF(100, 100))
2867 .dynamicConstraint(fnConstraint: hfw2, orientation: Qt::Vertical)
2868 << ItemDesc(1,0)
2869 .minSize(sz: QSizeF(200, 200))
2870 .maxSize(sz: QSizeF(200, 200))
2871 .dynamicConstraint(fnConstraint: hfw2, orientation: Qt::Vertical)
2872 << ItemDesc(2,0)
2873 .minSize(sz: QSizeF(300, 300))
2874 .maxSize(sz: QSizeF(300, 300))
2875 )
2876 << QSizeF(300, 600)
2877 << (RectList()
2878 << QRectF(0, 0, 100, 100)
2879 << QRectF(0, 100, 200, 200)
2880 << QRectF(0, 300, 300, 300)
2881 );
2882
2883 QTest::newRow(dataTag: "hfw-alignment2") << (ItemList()
2884 << ItemDesc(0,0)
2885 .minSize(sz: QSizeF(100, 100))
2886 .maxSize(sz: QSizeF(100, 100))
2887 .dynamicConstraint(fnConstraint: hfw2, orientation: Qt::Vertical)
2888 .alignment(alignment: Qt::AlignRight)
2889 << ItemDesc(1,0)
2890 .minSize(sz: QSizeF(200, 200))
2891 .maxSize(sz: QSizeF(200, 200))
2892 .dynamicConstraint(fnConstraint: hfw2, orientation: Qt::Vertical)
2893 .alignment(alignment: Qt::AlignHCenter)
2894 << ItemDesc(2,0)
2895 .minSize(sz: QSizeF(300, 300))
2896 .maxSize(sz: QSizeF(300, 300))
2897 )
2898 << QSizeF(300, 600)
2899 << (RectList()
2900 << QRectF(200, 0, 100, 100)
2901 << QRectF( 50, 100, 200, 200)
2902 << QRectF( 0, 300, 300, 300)
2903 );
2904
2905}
2906
2907void tst_QGraphicsGridLayout::geometries()
2908{
2909 QFETCH(ItemList, itemDescriptions);
2910 QFETCH(QSizeF, newSize);
2911 QFETCH(RectList, expectedGeometries);
2912
2913 QGraphicsScene scene;
2914 QGraphicsView view(&scene);
2915 QGraphicsWidget *widget = new QGraphicsWidget(0, Qt::Window);
2916 QGraphicsGridLayout *layout = new QGraphicsGridLayout;
2917 scene.addItem(item: widget);
2918 widget->setLayout(layout);
2919 layout->setContentsMargins(left: 0, top: 0, right: 0, bottom: 0);
2920 layout->setSpacing(0.0);
2921 widget->setContentsMargins(left: 0, top: 0, right: 0, bottom: 0);
2922
2923 int i;
2924 for (i = 0; i < itemDescriptions.count(); ++i) {
2925 ItemDesc desc = itemDescriptions.at(i);
2926 RectWidget *item = new RectWidget(widget);
2927 desc.apply(layout, item);
2928 }
2929
2930 QApplication::processEvents();
2931
2932 widget->show();
2933 view.resize(w: 400,h: 300);
2934 view.show();
2935 if (newSize.isValid())
2936 widget->resize(size: newSize);
2937
2938 QApplication::processEvents();
2939 for (i = 0; i < expectedGeometries.count(); ++i) {
2940 QRectF itemRect = layout->itemAt(index: i)->geometry();
2941 QCOMPARE(itemRect, expectedGeometries.at(i));
2942 }
2943
2944 delete widget;
2945}
2946
2947void tst_QGraphicsGridLayout::avoidRecursionInInsertItem()
2948{
2949 QGraphicsWidget window(0, Qt::Window);
2950 QGraphicsGridLayout *layout = new QGraphicsGridLayout(&window);
2951 QCOMPARE(layout->count(), 0);
2952 QTest::ignoreMessage(type: QtWarningMsg, message: "QGraphicsGridLayout::addItem: cannot insert itself");
2953 layout->addItem(aitem: layout, arow: 0, acolumn: 0);
2954 QCOMPARE(layout->count(), 0);
2955}
2956
2957void tst_QGraphicsGridLayout::styleInfoLeak()
2958{
2959 QGraphicsGridLayout grid;
2960 grid.horizontalSpacing();
2961}
2962
2963void tst_QGraphicsGridLayout::task236367_maxSizeHint()
2964{
2965 QGraphicsWidget *widget = new QGraphicsWidget;
2966 QGraphicsGridLayout *layout = new QGraphicsGridLayout;
2967 widget->setLayout(layout);
2968 layout->setContentsMargins(left: 0, top: 0, right: 0, bottom: 0);
2969 int w = 203;
2970 int h = 204;
2971 widget->resize(w, h);
2972 QCOMPARE(widget->size(), QSizeF(w, h));
2973}
2974
2975static QSizeF hfw(Qt::SizeHint /*which*/, const QSizeF &constraint)
2976{
2977 QSizeF result(constraint);
2978 const qreal cw = constraint.width();
2979 const qreal ch = constraint.height();
2980 if (cw < 0 && ch < 0) {
2981 return QSizeF(200, 100);
2982 } else if (cw >= 0) {
2983 result.setHeight(20000./cw);
2984 } else if (cw == 0) {
2985 result.setHeight(20000);
2986 } else if (ch >= 0) {
2987 result.setWidth(20000./ch);
2988 } else if (ch == 0) {
2989 result.setWidth(20000);
2990 }
2991 return result;
2992}
2993
2994static QSizeF wfh(Qt::SizeHint /*which*/, const QSizeF &constraint)
2995{
2996 QSizeF result(constraint);
2997 const qreal ch = constraint.height();
2998 if (ch >= 0) {
2999 result.setWidth(ch);
3000 }
3001 return result;
3002}
3003
3004bool qFuzzyCompare(const QSizeF &a, const QSizeF &b)
3005{
3006 return qFuzzyCompare(p1: a.width(), p2: b.width()) && qFuzzyCompare(p1: a.height(), p2: b.height());
3007}
3008
3009void tst_QGraphicsGridLayout::heightForWidth()
3010{
3011 QGraphicsWidget *widget = new QGraphicsWidget;
3012 QGraphicsGridLayout *layout = new QGraphicsGridLayout;
3013 widget->setLayout(layout);
3014 layout->setContentsMargins(left: 0, top: 0, right: 0, bottom: 0);
3015 layout->setSpacing(0);
3016 RectWidget *w00 = new RectWidget;
3017 w00->setSizeHint(which: Qt::MinimumSize, size: QSizeF(1,1));
3018 w00->setSizeHint(which: Qt::PreferredSize, size: QSizeF(10,10));
3019 w00->setSizeHint(which: Qt::MaximumSize, size: QSizeF(100,100));
3020 layout->addItem(aitem: w00, arow: 0, acolumn: 0);
3021
3022 RectWidget *w01 = new RectWidget;
3023 w01->setSizeHint(which: Qt::MinimumSize, size: QSizeF(1,1));
3024 w01->setSizeHint(which: Qt::PreferredSize, size: QSizeF(10,10));
3025 w01->setSizeHint(which: Qt::MaximumSize, size: QSizeF(100,100));
3026 layout->addItem(aitem: w01, arow: 0, acolumn: 1);
3027
3028 RectWidget *w10 = new RectWidget;
3029 w10->setSizeHint(which: Qt::MinimumSize, size: QSizeF(1,1));
3030 w10->setSizeHint(which: Qt::PreferredSize, size: QSizeF(10,10));
3031 w10->setSizeHint(which: Qt::MaximumSize, size: QSizeF(100,100));
3032 layout->addItem(aitem: w10, arow: 1, acolumn: 0);
3033
3034 RectWidget *w11 = new RectWidget;
3035 w11->setSizeHint(which: Qt::MinimumSize, size: QSizeF(1,1));
3036 w11->setSizeHint(which: Qt::MaximumSize, size: QSizeF(30000,30000));
3037 w11->setConstraintFunction(hfw);
3038 QSizePolicy sp(QSizePolicy::Preferred, QSizePolicy::Preferred);
3039 sp.setHeightForWidth(true);
3040 w11->setSizePolicy(sp);
3041 layout->addItem(aitem: w11, arow: 1, acolumn: 1);
3042
3043 QCOMPARE(layout->effectiveSizeHint(Qt::MinimumSize, QSizeF(-1, -1)), QSizeF(2, 2));
3044 QCOMPARE(layout->effectiveSizeHint(Qt::PreferredSize, QSizeF(-1, -1)), QSizeF(210, 110));
3045 QCOMPARE(layout->effectiveSizeHint(Qt::MaximumSize, QSizeF(-1, -1)), QSizeF(30100, 30100));
3046
3047 QCOMPARE(layout->effectiveSizeHint(Qt::MinimumSize, QSizeF(2, -1)), QSizeF(2, 20001));
3048 QCOMPARE(layout->effectiveSizeHint(Qt::PreferredSize, QSizeF(2, -1)), QSizeF(2, 20010));
3049 QCOMPARE(layout->effectiveSizeHint(Qt::MaximumSize, QSizeF(2, -1)), QSizeF(2, 20100));
3050
3051 // Since 20 is somewhere between "minimum width hint" (2) and
3052 // "preferred width hint" (210), it will try to do distribution by
3053 // stretching them with different factors.
3054 // Since column 1 has a "preferred width" of 200 it means that
3055 // column 1 will be a bit wider than column 0. Thus it will also be a bit
3056 // shorter than 2001, (the expected height if all columns had width=10)
3057 QSizeF sh = layout->effectiveSizeHint(which: Qt::MinimumSize, constraint: QSizeF(20, -1));
3058 // column 1 cannot be wider than 19, which means that it must be taller than 20000/19~=1052
3059 QVERIFY(sh.height() < 2000 + 1 && sh.height() > 1052 + 1);
3060
3061 sh = layout->effectiveSizeHint(which: Qt::PreferredSize, constraint: QSizeF(20, -1));
3062 QVERIFY(sh.height() < 2000 + 10 && sh.height() > 1052 + 10);
3063
3064 sh = layout->effectiveSizeHint(which: Qt::MaximumSize, constraint: QSizeF(20, -1));
3065 QVERIFY(sh.height() < 2000 + 100 && sh.height() > 1052 + 100);
3066
3067 // the height of the hfw widget is shorter than the one to the left, which is 100, so
3068 // the total height of the last row is 100 (which leaves the layout height to be 200)
3069 QCOMPARE(layout->effectiveSizeHint(Qt::MaximumSize, QSizeF(500, -1)), QSizeF(500, 100 + 100));
3070
3071}
3072
3073void tst_QGraphicsGridLayout::widthForHeight()
3074{
3075 QGraphicsWidget *widget = new QGraphicsWidget;
3076 QGraphicsGridLayout *layout = new QGraphicsGridLayout;
3077 widget->setLayout(layout);
3078 layout->setContentsMargins(left: 0, top: 0, right: 0, bottom: 0);
3079 layout->setSpacing(0);
3080
3081 RectWidget *w00 = new RectWidget;
3082 w00->setMinimumSize(aw: 1, ah: 1);
3083 w00->setPreferredSize(aw: 50, ah: 50);
3084 w00->setMaximumSize(aw: 100, ah: 100);
3085
3086 layout->addItem(aitem: w00, arow: 0, acolumn: 0);
3087
3088 RectWidget *w01 = new RectWidget;
3089 w01->setMinimumSize(aw: 1,ah: 1);
3090 w01->setPreferredSize(aw: 50,ah: 50);
3091 w01->setMaximumSize(aw: 100,ah: 100);
3092 layout->addItem(aitem: w01, arow: 0, acolumn: 1);
3093
3094 RectWidget *w10 = new RectWidget;
3095 w10->setMinimumSize(aw: 1,ah: 1);
3096 w10->setPreferredSize(aw: 50,ah: 50);
3097 w10->setMaximumSize(aw: 100,ah: 100);
3098 layout->addItem(aitem: w10, arow: 1, acolumn: 0);
3099
3100 RectWidget *w11 = new RectWidget;
3101 w11->setSizeHint(which: Qt::MinimumSize, size: QSizeF(1,1));
3102 w11->setSizeHint(which: Qt::PreferredSize, size: QSizeF(50,50));
3103 w11->setSizeHint(which: Qt::MaximumSize, size: QSizeF(30000,30000));
3104
3105 // This will make sure its always square.
3106 w11->setConstraintFunction(wfh);
3107 QSizePolicy sp(QSizePolicy::Preferred, QSizePolicy::Preferred);
3108 sp.setWidthForHeight(true);
3109 w11->setSizePolicy(sp);
3110 layout->addItem(aitem: w11, arow: 1, acolumn: 1);
3111
3112 /*
3113 | 1, 50, 100 | 1, 50, 100 |
3114 -----+--------------+--------------+
3115 1| | |
3116 50| | |
3117 100| | |
3118 -----|--------------+--------------+
3119 1| | |
3120 50| | WFH |
3121 100| | |
3122 -----------------------------------+
3123 */
3124
3125
3126 QSizeF prefSize = layout->effectiveSizeHint(which: Qt::PreferredSize, constraint: QSizeF(-1, -1));
3127 QCOMPARE(prefSize, QSizeF(50+50, 50+50));
3128
3129 // wfh(1): = 1
3130 QCOMPARE(layout->effectiveSizeHint(Qt::MinimumSize, QSizeF(-1, 2)), QSizeF(1 + 1, 2));
3131 QCOMPARE(layout->effectiveSizeHint(Qt::PreferredSize, QSizeF(-1, 2)), QSizeF(50 + 50, 2));
3132 QCOMPARE(layout->effectiveSizeHint(Qt::MaximumSize, QSizeF(-1, 2)), QSizeF(100 + 100, 2));
3133
3134 // wfh(40) = 40
3135 QCOMPARE(layout->effectiveSizeHint(Qt::MinimumSize, QSizeF(-1, 80)), QSizeF(1 + 40, 80));
3136 QCOMPARE(layout->effectiveSizeHint(Qt::PreferredSize, QSizeF(-1, 80)), QSizeF(50 + 50, 80));
3137 QCOMPARE(layout->effectiveSizeHint(Qt::MaximumSize, QSizeF(-1, 80)), QSizeF(100 + 100, 80));
3138
3139 // wfh(80) = 80
3140 QCOMPARE(layout->effectiveSizeHint(Qt::MinimumSize, QSizeF(-1, 160)), QSizeF(1 + 80, 160));
3141 QCOMPARE(layout->effectiveSizeHint(Qt::PreferredSize, QSizeF(-1, 160)), QSizeF(50 + 80, 160));
3142 QCOMPARE(layout->effectiveSizeHint(Qt::MaximumSize, QSizeF(-1, 160)), QSizeF(100 + 100, 160));
3143
3144 // wfh(200) = 200
3145 QCOMPARE(layout->effectiveSizeHint(Qt::MinimumSize, QSizeF(-1, 300)), QSizeF(1 + 200, 300));
3146 QCOMPARE(layout->effectiveSizeHint(Qt::PreferredSize, QSizeF(-1, 300)), QSizeF(50 + 200, 300));
3147 QCOMPARE(layout->effectiveSizeHint(Qt::MaximumSize, QSizeF(-1, 300)), QSizeF(100 + 200, 300));
3148}
3149
3150void tst_QGraphicsGridLayout::heightForWidthWithSpanning()
3151{
3152 QGraphicsWidget *widget = new QGraphicsWidget;
3153 QGraphicsGridLayout *layout = new QGraphicsGridLayout;
3154 widget->setLayout(layout);
3155 layout->setContentsMargins(left: 0, top: 0, right: 0, bottom: 0);
3156 layout->setSpacing(0);
3157
3158 RectWidget *w = new RectWidget;
3159 w->setSizeHint(which: Qt::MinimumSize, size: QSizeF(1,1));
3160 w->setSizeHint(which: Qt::MaximumSize, size: QSizeF(30000,30000));
3161 w->setConstraintFunction(hfw);
3162 QSizePolicy sp(QSizePolicy::Preferred, QSizePolicy::Preferred);
3163 sp.setHeightForWidth(true);
3164 w->setSizePolicy(sp);
3165 layout->addItem(item: w, row: 0,column: 0,rowSpan: 2,columnSpan: 2);
3166
3167 QCOMPARE(layout->effectiveSizeHint(Qt::MinimumSize, QSizeF(-1, -1)), QSizeF(1, 1));
3168 QCOMPARE(layout->effectiveSizeHint(Qt::PreferredSize, QSizeF(-1, -1)), QSizeF(200, 100));
3169 QCOMPARE(layout->effectiveSizeHint(Qt::MaximumSize, QSizeF(-1, -1)), QSizeF(30000, 30000));
3170
3171 QCOMPARE(layout->effectiveSizeHint(Qt::MinimumSize, QSizeF(200, -1)), QSizeF(200, 100));
3172 QCOMPARE(layout->effectiveSizeHint(Qt::PreferredSize, QSizeF(200, -1)), QSizeF(200, 100));
3173 QCOMPARE(layout->effectiveSizeHint(Qt::MaximumSize, QSizeF(200, -1)), QSizeF(200, 100));
3174
3175 QCOMPARE(layout->effectiveSizeHint(Qt::MinimumSize, QSizeF(2, -1)), QSizeF(2, 10000));
3176 QCOMPARE(layout->effectiveSizeHint(Qt::PreferredSize, QSizeF(2, -1)), QSizeF(2, 10000));
3177 QCOMPARE(layout->effectiveSizeHint(Qt::MaximumSize, QSizeF(2, -1)), QSizeF(2, 10000));
3178
3179 QCOMPARE(layout->effectiveSizeHint(Qt::MinimumSize, QSizeF(200, -1)), QSizeF(200, 100));
3180 QCOMPARE(layout->effectiveSizeHint(Qt::PreferredSize, QSizeF(200, -1)), QSizeF(200, 100));
3181 QCOMPARE(layout->effectiveSizeHint(Qt::MaximumSize, QSizeF(200, -1)), QSizeF(200, 100));
3182}
3183
3184Q_DECLARE_METATYPE(QSizePolicy::Policy)
3185void tst_QGraphicsGridLayout::spanningItem2x2_data()
3186{
3187 QTest::addColumn<QSizePolicy::Policy>(name: "sizePolicy");
3188 QTest::addColumn<int>(name: "itemHeight");
3189 QTest::addColumn<int>(name: "expectedHeight");
3190
3191 QTest::newRow(dataTag: "A larger spanning item with 2 widgets with fixed policy") << QSizePolicy::Fixed << 39 << 80;
3192 QTest::newRow(dataTag: "A larger spanning item with 2 widgets with preferred policy") << QSizePolicy::Preferred << 39 << 80;
3193 QTest::newRow(dataTag: "An equally-sized spanning item with 2 widgets with fixed policy") << QSizePolicy::Fixed << 40 << 80;
3194 QTest::newRow(dataTag: "An equally-sized spanning item with 2 widgets with preferred policy") << QSizePolicy::Preferred << 40 << 80;
3195 QTest::newRow(dataTag: "A smaller spanning item with 2 widgets with fixed policy") << QSizePolicy::Fixed << 41 << 82;
3196 QTest::newRow(dataTag: "A smaller spanning item with 2 widgets with preferred policy") << QSizePolicy::Preferred << 41 << 82;
3197}
3198
3199void tst_QGraphicsGridLayout::spanningItem2x2()
3200{
3201 QFETCH(QSizePolicy::Policy, sizePolicy);
3202 QFETCH(int, itemHeight);
3203 QFETCH(int, expectedHeight);
3204 QGraphicsWidget *form = new QGraphicsWidget(0, Qt::Window);
3205 QGraphicsGridLayout *layout = new QGraphicsGridLayout(form);
3206 layout->setContentsMargins(left: 0, top: 0, right: 0, bottom: 0);
3207 layout->setSpacing(0);
3208
3209 QGraphicsWidget *w1 = new QGraphicsWidget;
3210 w1->setMinimumSize(aw: 80,ah: 80);
3211 w1->setMaximumSize(aw: 80,ah: 80);
3212
3213 QGraphicsWidget *w2 = new QGraphicsWidget;
3214 w2->setMinimumSize(aw: 80,ah: itemHeight);
3215 w2->setPreferredSize(aw: 80,ah: itemHeight);
3216 w2->setSizePolicy(hPolicy: QSizePolicy::Fixed, vPolicy: sizePolicy);
3217
3218 QGraphicsWidget *w3 = new QGraphicsWidget;
3219 w3->setMinimumSize(aw: 80,ah: itemHeight);
3220 w3->setPreferredSize(aw: 80,ah: itemHeight);
3221 w3->setSizePolicy(hPolicy: QSizePolicy::Fixed, vPolicy: sizePolicy);
3222
3223 layout->addItem(item: w1, row: 0, column: 0, rowSpan: 2, columnSpan: 1);
3224 layout->addItem(aitem: w2, arow: 0, acolumn: 1);
3225 layout->addItem(aitem: w3, arow: 1, acolumn: 1);
3226
3227 QCOMPARE(layout->minimumSize(), QSizeF(160,expectedHeight));
3228 if(sizePolicy == QSizePolicy::Fixed)
3229 QCOMPARE(layout->maximumSize(), QSizeF(160,expectedHeight));
3230 else
3231 QCOMPARE(layout->maximumSize(), QSizeF(160,QWIDGETSIZE_MAX));
3232}
3233
3234void tst_QGraphicsGridLayout::spanningItem2x3_data()
3235{
3236 QTest::addColumn<bool>(name: "w1_fixed");
3237 QTest::addColumn<bool>(name: "w2_fixed");
3238 QTest::addColumn<bool>(name: "w3_fixed");
3239 QTest::addColumn<bool>(name: "w4_fixed");
3240 QTest::addColumn<bool>(name: "w5_fixed");
3241
3242 for(int w1 = 0; w1 < 2; w1++)
3243 for(int w2 = 0; w2 < 2; w2++)
3244 for(int w3 = 0; w3 < 2; w3++)
3245 for(int w4 = 0; w4 < 2; w4++)
3246 for(int w5 = 0; w5 < 2; w5++) {
3247 QString description = QString("Fixed sizes:") + (w1?" w1":"") + (w2?" w2":"") + (w3?" w3":"") + (w4?" w4":"") + (w5?" w5":"");
3248 QTest::newRow(dataTag: description.toLatin1()) << (bool)w1 << (bool)w2 << (bool)w3 << (bool)w4 << (bool)w5;
3249 }
3250}
3251
3252void tst_QGraphicsGridLayout::spanningItem2x3()
3253{
3254 QFETCH(bool, w1_fixed);
3255 QFETCH(bool, w2_fixed);
3256 QFETCH(bool, w3_fixed);
3257 QFETCH(bool, w4_fixed);
3258 QFETCH(bool, w5_fixed);
3259 QGraphicsGridLayout *layout = new QGraphicsGridLayout;
3260 layout->setContentsMargins(left: 0, top: 0, right: 0, bottom: 0);
3261 layout->setSpacing(0);
3262
3263 QGraphicsWidget *w1 = new QGraphicsWidget;
3264 w1->setMinimumSize(aw: 80,ah: 80);
3265 w1->setMaximumSize(aw: 80,ah: 80);
3266 if (w1_fixed)
3267 w1->setSizePolicy(hPolicy: QSizePolicy::Preferred, vPolicy: QSizePolicy::Fixed);
3268
3269 QGraphicsWidget *w2 = new QGraphicsWidget;
3270 w2->setMinimumSize(aw: 80,ah: 48);
3271 w2->setPreferredSize(aw: 80,ah: 48);
3272 if (w2_fixed)
3273 w2->setSizePolicy(hPolicy: QSizePolicy::Preferred, vPolicy: QSizePolicy::Fixed);
3274
3275 QGraphicsWidget *w3 = new QGraphicsWidget;
3276 w3->setMinimumSize(aw: 80,ah: 30);
3277 w3->setPreferredSize(aw: 80,ah: 30);
3278 if (w3_fixed)
3279 w3->setSizePolicy(hPolicy: QSizePolicy::Preferred, vPolicy: QSizePolicy::Fixed);
3280
3281 QGraphicsWidget *w4 = new QGraphicsWidget;
3282 w4->setMinimumSize(aw: 80,ah: 30);
3283 w4->setMaximumSize(aw: 80,ah: 30);
3284 if (w4_fixed)
3285 w4->setSizePolicy(hPolicy: QSizePolicy::Preferred, vPolicy: QSizePolicy::Fixed);
3286
3287 QGraphicsWidget *w5 = new QGraphicsWidget;
3288 w5->setMinimumSize(aw: 40,ah: 24);
3289 w5->setMaximumSize(aw: 40,ah: 24);
3290 if (w5_fixed)
3291 w5->setSizePolicy(hPolicy: QSizePolicy::Preferred, vPolicy: QSizePolicy::Fixed);
3292
3293 layout->addItem(item: w1, row: 0, column: 0, rowSpan: 2, columnSpan: 1);
3294 layout->addItem(aitem: w2, arow: 0, acolumn: 1);
3295 layout->addItem(aitem: w3, arow: 1, acolumn: 1);
3296 layout->addItem(aitem: w4, arow: 0, acolumn: 2);
3297 layout->addItem(aitem: w5, arow: 1, acolumn: 2);
3298
3299 QCOMPARE(layout->minimumSize(), QSizeF(240,80));
3300 // Only w2 and w3 grow vertically, so when they have a fixed vertical size policy,
3301 // the whole layout cannot grow vertically.
3302 if (w2_fixed && w3_fixed)
3303 QCOMPARE(layout->maximumSize(), QSizeF(QWIDGETSIZE_MAX,80));
3304 else
3305 QCOMPARE(layout->maximumSize(), QSizeF(QWIDGETSIZE_MAX,QWIDGETSIZE_MAX));
3306}
3307
3308void tst_QGraphicsGridLayout::spanningItem()
3309{
3310 QGraphicsWidget *form = new QGraphicsWidget(0, Qt::Window);
3311 QGraphicsGridLayout *layout = new QGraphicsGridLayout(form);
3312 layout->setContentsMargins(left: 0, top: 0, right: 0, bottom: 0);
3313 layout->setSpacing(0);
3314
3315 QGraphicsWidget *w1 = new QGraphicsWidget;
3316 w1->setMinimumSize(aw: 80,ah: 80);
3317 w1->setMaximumSize(aw: 80,ah: 80);
3318
3319 QGraphicsWidget *w2 = new QGraphicsWidget;
3320 w2->setMinimumSize(aw: 80,ah: 38);
3321 w2->setPreferredSize(aw: 80,ah: 38);
3322 w2->setSizePolicy(hPolicy: QSizePolicy::Fixed, vPolicy: QSizePolicy::Fixed);
3323
3324 QGraphicsWidget *w3 = new QGraphicsWidget;
3325 w3->setMinimumSize(aw: 80,ah: 38);
3326 w3->setPreferredSize(aw: 80,ah: 38);
3327 w3->setSizePolicy(hPolicy: QSizePolicy::Fixed, vPolicy: QSizePolicy::Fixed);
3328
3329 layout->addItem(item: w1, row: 0, column: 0, rowSpan: 2, columnSpan: 1);
3330 layout->addItem(aitem: w2, arow: 0, acolumn: 1);
3331 layout->addItem(aitem: w3, arow: 1, acolumn: 1);
3332
3333 QCOMPARE(layout->minimumSize(), QSizeF(160,80));
3334 QCOMPARE(layout->maximumSize(), QSizeF(160,80));
3335}
3336
3337void tst_QGraphicsGridLayout::spanAcrossEmptyRow()
3338{
3339 QGraphicsWidget *form = new QGraphicsWidget(0, Qt::Window);
3340 QGraphicsGridLayout *layout = new QGraphicsGridLayout(form);
3341 layout->setContentsMargins(left: 0, top: 0, right: 0, bottom: 0);
3342 layout->setSpacing(0);
3343 RectWidget *w1 = new RectWidget;
3344 RectWidget *w2 = new RectWidget;
3345 RectWidget *w3 = new RectWidget;
3346
3347 QSizeF size(10, 10);
3348 for (int i = 0; i < 3; ++i) {
3349 w1->setSizeHint(which: (Qt::SizeHint)i, size);
3350 w2->setSizeHint(which: (Qt::SizeHint)i, size);
3351 w3->setSizeHint(which: (Qt::SizeHint)i, size);
3352 size+=size; //[(10,10), (20,20), (40,40)]
3353 }
3354 layout->addItem(item: w1, row: 0, column: 0, rowSpan: 1, columnSpan: 1);
3355 layout->addItem(item: w2, row: 0, column: 1, rowSpan: 1, columnSpan: 2);
3356 layout->addItem(item: w3, row: 0, column: 99, rowSpan: 1, columnSpan: 1);
3357
3358 form->resize(w: 60,h: 20);
3359 QCOMPARE(w1->geometry(), QRectF( 0, 0, 20, 20));
3360 QCOMPARE(w2->geometry(), QRectF(20, 0, 20, 20));
3361 QCOMPARE(w3->geometry(), QRectF(40, 0, 20, 20));
3362
3363 QCOMPARE(layout->effectiveSizeHint(Qt::MinimumSize), QSizeF(30, 10));
3364 QCOMPARE(layout->effectiveSizeHint(Qt::PreferredSize), QSizeF(60, 20));
3365 QCOMPARE(layout->effectiveSizeHint(Qt::MaximumSize), QSizeF(120, 40));
3366}
3367
3368void tst_QGraphicsGridLayout::stretchAndHeightForWidth()
3369{
3370 QGraphicsWidget *widget = new QGraphicsWidget(0, Qt::Window);
3371 QGraphicsGridLayout *layout = new QGraphicsGridLayout;
3372 widget->setLayout(layout);
3373 layout->setContentsMargins(left: 0, top: 0, right: 0, bottom: 0);
3374 layout->setSpacing(0);
3375
3376 RectWidget *w1 = new RectWidget;
3377 w1->setSizeHint(which: Qt::MinimumSize, size: QSizeF(10, 10));
3378 w1->setSizeHint(which: Qt::PreferredSize, size: QSizeF(100, 100));
3379 w1->setSizeHint(which: Qt::MaximumSize, size: QSizeF(500, 500));
3380 layout->addItem(item: w1, row: 0,column: 0,rowSpan: 1,columnSpan: 1);
3381
3382 RectWidget *w2 = new RectWidget;
3383 w2->setSizeHint(which: Qt::MinimumSize, size: QSizeF(10, 10));
3384 w2->setSizeHint(which: Qt::PreferredSize, size: QSizeF(100, 100));
3385 w2->setSizeHint(which: Qt::MaximumSize, size: QSizeF(500, 500));
3386 layout->addItem(item: w2, row: 0,column: 1,rowSpan: 1,columnSpan: 1);
3387 layout->setColumnStretchFactor(column: 1, stretch: 2);
3388
3389 QApplication::sendPostedEvents();
3390 QGraphicsScene scene;
3391 QGraphicsView *view = new QGraphicsView(&scene);
3392
3393 scene.addItem(item: widget);
3394
3395 view->show();
3396
3397 widget->resize(w: 500, h: 100);
3398 // w1 should stay at its preferred size
3399 QCOMPARE(w1->geometry(), QRectF(0, 0, 100, 100));
3400 QCOMPARE(w2->geometry(), QRectF(100, 0, 400, 100));
3401
3402
3403 // only w1 has hfw
3404 w1->setConstraintFunction(hfw);
3405 QSizePolicy sp(QSizePolicy::Preferred, QSizePolicy::Preferred);
3406 sp.setHeightForWidth(true);
3407 w1->setSizePolicy(sp);
3408 QApplication::sendPostedEvents();
3409
3410 QCOMPARE(w1->geometry(), QRectF(0, 0, 100, 200));
3411 QCOMPARE(w2->geometry(), QRectF(100, 0, 400, 200));
3412
3413 // only w2 has hfw
3414 w2->setConstraintFunction(hfw);
3415 w2->setSizePolicy(sp);
3416
3417 w1->setConstraintFunction(0);
3418 sp.setHeightForWidth(false);
3419 w1->setSizePolicy(sp);
3420 QApplication::sendPostedEvents();
3421
3422 QCOMPARE(w1->geometry(), QRectF(0, 0, 100, 100));
3423 QCOMPARE(w2->geometry(), QRectF(100, 0, 400, 50));
3424
3425}
3426
3427void tst_QGraphicsGridLayout::testDefaultAlignment()
3428{
3429 QGraphicsWidget *widget = new QGraphicsWidget;
3430 QGraphicsGridLayout *layout = new QGraphicsGridLayout(widget);
3431 layout->setContentsMargins(left: 0, top: 0, right: 0, bottom: 0);
3432 layout->setSpacing(0);
3433
3434 QGraphicsWidget *w = new QGraphicsWidget;
3435 w->setMinimumSize(aw: 50,ah: 50);
3436 w->setMaximumSize(aw: 50,ah: 50);
3437 layout->addItem(aitem: w,arow: 0,acolumn: 0);
3438
3439 //Default alignment should be to the top-left
3440
3441 //First, check by forcing the layout to be bigger
3442 layout->setMinimumSize(aw: 100,ah: 100);
3443 layout->activate();
3444 QCOMPARE(layout->geometry(), QRectF(0,0,100,100));
3445 QCOMPARE(w->geometry(), QRectF(0,0,50,50));
3446 layout->setMinimumSize(aw: -1,ah: -1);
3447
3448 //Second, check by forcing the column and row to be bigger instead
3449 layout->setColumnMinimumWidth(column: 0, width: 100);
3450 layout->setRowMinimumHeight(row: 0, height: 100);
3451 layout->activate();
3452 QCOMPARE(layout->geometry(), QRectF(0,0,100,100));
3453 QCOMPARE(w->geometry(), QRectF(0,0,50,50));
3454 layout->setMinimumSize(aw: -1,ah: -1);
3455 layout->setColumnMinimumWidth(column: 0, width: 0);
3456 layout->setRowMinimumHeight(row: 0, height: 0);
3457
3458
3459 //Third, check by adding a larger item in the column
3460 QGraphicsWidget *w2 = new QGraphicsWidget;
3461 w2->setMinimumSize(aw: 100,ah: 100);
3462 w2->setMaximumSize(aw: 100,ah: 100);
3463 layout->addItem(aitem: w2,arow: 1,acolumn: 0);
3464 layout->activate();
3465 QCOMPARE(layout->geometry(), QRectF(0,0,100,150));
3466 QCOMPARE(w->geometry(), QRectF(0,0,50,50));
3467 QCOMPARE(w2->geometry(), QRectF(0,50,100,100));
3468}
3469
3470static RectWidget *addWidget(QGraphicsGridLayout *grid, int row, int column)
3471{
3472 RectWidget *w = new RectWidget;
3473 w->setPreferredSize(aw: 20, ah: 20);
3474 grid->addItem(aitem: w, arow: row, acolumn: column);
3475 return w;
3476}
3477
3478static void setVisible(bool visible, QGraphicsWidget **widgets)
3479{
3480 for (int i = 0; i < 3; ++i)
3481 if (widgets[i]) widgets[i]->setVisible(visible);
3482}
3483
3484static void setRetainSizeWhenHidden(bool retainSize, QGraphicsWidget **widgets)
3485{
3486 QSizePolicy sp = widgets[0]->sizePolicy();
3487 sp.setRetainSizeWhenHidden(retainSize);
3488 for (int i = 0; i < 3; ++i)
3489 if (widgets[i]) widgets[i]->setSizePolicy(sp);
3490}
3491
3492void tst_QGraphicsGridLayout::hiddenItems()
3493{
3494 QGraphicsWidget *widget = new QGraphicsWidget;
3495 QGraphicsGridLayout *layout = new QGraphicsGridLayout(widget);
3496 layout->setContentsMargins(left: 0, top: 0, right: 0, bottom: 0);
3497 layout->setSpacing(2);
3498
3499 // Create a 3x3 layout
3500 addWidget(grid: layout, row: 0, column: 0);
3501 RectWidget *w01 = addWidget(grid: layout, row: 0, column: 1);
3502 addWidget(grid: layout, row: 0, column: 2);
3503 RectWidget *w10 = addWidget(grid: layout, row: 1, column: 0);
3504 RectWidget *w11 = addWidget(grid: layout, row: 1, column: 1);
3505 RectWidget *w12 = addWidget(grid: layout, row: 1, column: 2);
3506 addWidget(grid: layout, row: 2, column: 0);
3507 RectWidget *w21 = addWidget(grid: layout, row: 2, column: 1);
3508 addWidget(grid: layout, row: 2, column: 2);
3509
3510 QGraphicsWidget *middleColumn[] = {w01, w11, w21 };
3511 QGraphicsWidget *topTwoOfMiddleColumn[] = {w01, w11, 0 };
3512
3513 // hide and show middle column
3514 QCOMPARE(layout->preferredWidth(), qreal(64));
3515 setVisible(visible: false, widgets: middleColumn); // hide middle column
3516 QCOMPARE(layout->preferredWidth(), qreal(42));
3517 setVisible(visible: true, widgets: middleColumn); // show middle column
3518 QCOMPARE(layout->preferredWidth(), qreal(64));
3519 setRetainSizeWhenHidden(retainSize: true, widgets: middleColumn);
3520 QCOMPARE(layout->preferredWidth(), qreal(64));
3521 setVisible(visible: false, widgets: middleColumn); // hide middle column
3522 QCOMPARE(layout->preferredWidth(), qreal(64));
3523 setRetainSizeWhenHidden(retainSize: false, widgets: middleColumn);
3524 QCOMPARE(layout->preferredWidth(), qreal(42));
3525 setVisible(visible: true, widgets: middleColumn);
3526 QCOMPARE(layout->preferredWidth(), qreal(64));
3527
3528 // Hide only two items, => column should not collapse
3529 setVisible(visible: false, widgets: topTwoOfMiddleColumn);
3530 QCOMPARE(layout->preferredWidth(), qreal(64));
3531
3532
3533 QGraphicsWidget *middleRow[] = {w10, w11, w12 };
3534 QGraphicsWidget *leftMostTwoOfMiddleRow[] = {w10, w11, 0 };
3535
3536 // hide and show middle row
3537 QCOMPARE(layout->preferredHeight(), qreal(64));
3538 setVisible(visible: false, widgets: middleRow);
3539 QCOMPARE(layout->preferredHeight(), qreal(42));
3540 setVisible(visible: true, widgets: middleRow);
3541 QCOMPARE(layout->preferredHeight(), qreal(64));
3542 setRetainSizeWhenHidden(retainSize: true, widgets: middleColumn);
3543 QCOMPARE(layout->preferredHeight(), qreal(64));
3544 setVisible(visible: false, widgets: middleRow);
3545 QCOMPARE(layout->preferredHeight(), qreal(64));
3546 setRetainSizeWhenHidden(retainSize: false, widgets: middleRow);
3547 QCOMPARE(layout->preferredHeight(), qreal(42));
3548 setVisible(visible: true, widgets: middleRow);
3549 QCOMPARE(layout->preferredHeight(), qreal(64));
3550
3551 // Hide only two items => row should not collapse
3552 setVisible(visible: false, widgets: leftMostTwoOfMiddleRow);
3553 QCOMPARE(layout->preferredHeight(), qreal(64));
3554
3555}
3556
3557QTEST_MAIN(tst_QGraphicsGridLayout)
3558#include "tst_qgraphicsgridlayout.moc"
3559
3560

source code of qtbase/tests/auto/widgets/graphicsview/qgraphicsgridlayout/tst_qgraphicsgridlayout.cpp