author | Eckhart Koeppen <eckhart.koppen@nokia.com> |
Wed, 21 Apr 2010 20:15:53 +0300 | |
branch | RCL_3 |
changeset 13 | c0432d11811c |
parent 4 | 3b1da2848fc7 |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 4 |
** All rights reserved. |
5 |
** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 |
** |
|
7 |
** This file is part of the test suite of the Qt Toolkit. |
|
8 |
** |
|
9 |
** $QT_BEGIN_LICENSE:LGPL$ |
|
10 |
** No Commercial Usage |
|
11 |
** This file contains pre-release code and may not be distributed. |
|
12 |
** You may use this file in accordance with the terms and conditions |
|
13 |
** contained in the Technology Preview License Agreement accompanying |
|
14 |
** this package. |
|
15 |
** |
|
16 |
** GNU Lesser General Public License Usage |
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 |
** General Public License version 2.1 as published by the Free Software |
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 |
** packaging of this file. Please review the following information to |
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 |
** |
|
24 |
** In addition, as a special exception, Nokia gives you certain additional |
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 |
** |
|
28 |
** If you have questions regarding the use of this file, please contact |
|
29 |
** Nokia at qt-info@nokia.com. |
|
30 |
** |
|
31 |
** |
|
32 |
** |
|
33 |
** |
|
34 |
** |
|
35 |
** |
|
36 |
** |
|
37 |
** |
|
38 |
** $QT_END_LICENSE$ |
|
39 |
** |
|
40 |
****************************************************************************/ |
|
41 |
||
42 |
||
43 |
#include <QtTest/QtTest> |
|
44 |
#include <QtGui> |
|
45 |
#include <math.h> |
|
46 |
||
13
c0432d11811c
eb175c3290cd7ea85da4a590db9461504a4904bc
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
47 |
#include "../../shared/util.h" |
c0432d11811c
eb175c3290cd7ea85da4a590db9461504a4904bc
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
48 |
|
0 | 49 |
//TESTED_CLASS=QGraphicsLayout |
50 |
//TESTED_FILES= |
|
51 |
||
52 |
class tst_QGraphicsLayout : public QObject |
|
53 |
{ |
|
54 |
Q_OBJECT |
|
55 |
||
56 |
public: |
|
57 |
tst_QGraphicsLayout(); |
|
58 |
virtual ~tst_QGraphicsLayout(); |
|
59 |
||
60 |
private slots: |
|
61 |
void sizeHints(); |
|
62 |
void compressLayoutRequest(); |
|
63 |
void automaticReparenting(); |
|
64 |
void verifyActivate(); |
|
65 |
void constructors(); |
|
66 |
void alternativeLayoutItems(); |
|
67 |
void ownership(); |
|
68 |
}; |
|
69 |
||
70 |
tst_QGraphicsLayout::tst_QGraphicsLayout() |
|
71 |
{ |
|
72 |
} |
|
73 |
||
74 |
tst_QGraphicsLayout::~tst_QGraphicsLayout() |
|
75 |
{ |
|
76 |
} |
|
77 |
||
78 |
void tst_QGraphicsLayout::sizeHints() |
|
79 |
{ |
|
80 |
||
81 |
QGraphicsView view; |
|
82 |
QGraphicsScene scene; |
|
83 |
QGraphicsWidget *window = new QGraphicsWidget(); |
|
84 |
scene.addItem(window); |
|
85 |
QGraphicsLinearLayout *lout = new QGraphicsLinearLayout(window); |
|
86 |
lout->setContentsMargins(0,0,0,0); |
|
87 |
QGraphicsWidget *gw = new QGraphicsWidget(window); |
|
88 |
gw->setMinimumSize(QSizeF(10,10)); |
|
89 |
gw->setPreferredSize(QSizeF(100,100)); |
|
90 |
gw->setMaximumSize(QSizeF(500,500)); |
|
91 |
lout->addItem(gw); |
|
92 |
QCOMPARE(lout->effectiveSizeHint(Qt::MinimumSize), gw->effectiveSizeHint(Qt::MinimumSize)); |
|
93 |
QCOMPARE(lout->effectiveSizeHint(Qt::PreferredSize), gw->effectiveSizeHint(Qt::PreferredSize)); |
|
94 |
QCOMPARE(lout->effectiveSizeHint(Qt::MaximumSize), gw->effectiveSizeHint(Qt::MaximumSize)); |
|
95 |
||
96 |
} |
|
97 |
||
98 |
class TestGraphicsWidget : public QGraphicsWidget { |
|
99 |
public: |
|
100 |
TestGraphicsWidget(QGraphicsWidget *parent = 0) : QGraphicsWidget(parent) |
|
101 |
{ } |
|
102 |
||
103 |
bool event(QEvent *e) { |
|
104 |
++(m_eventCount[int(e->type())]); |
|
105 |
return QGraphicsWidget::event(e); |
|
106 |
} |
|
107 |
||
108 |
int eventCount(QEvent::Type type) { |
|
109 |
return m_eventCount.value(int(type)); |
|
110 |
} |
|
111 |
void clearEventCount() { |
|
112 |
m_eventCount.clear(); |
|
113 |
} |
|
114 |
private: |
|
115 |
QMap<int, int> m_eventCount; |
|
116 |
}; |
|
117 |
||
118 |
void tst_QGraphicsLayout::compressLayoutRequest() |
|
119 |
{ |
|
120 |
QGraphicsView view; |
|
121 |
QGraphicsScene scene; |
|
122 |
TestGraphicsWidget *tw = new TestGraphicsWidget(); |
|
123 |
scene.addItem(tw); |
|
124 |
view.show(); |
|
125 |
QGraphicsLinearLayout *lout = new QGraphicsLinearLayout(tw); |
|
126 |
for (int i = 0; i < 4; ++i) { |
|
127 |
QGraphicsWidget *gw = new QGraphicsWidget(tw); |
|
128 |
gw->setPreferredSize(QSizeF(50, 50)); |
|
129 |
lout->addItem(gw); |
|
130 |
} |
|
131 |
QApplication::processEvents(); |
|
132 |
QCOMPARE(tw->eventCount(QEvent::LayoutRequest), 1); |
|
133 |
} |
|
134 |
||
135 |
void tst_QGraphicsLayout::automaticReparenting() |
|
136 |
{ |
|
137 |
QGraphicsView view; |
|
138 |
QGraphicsScene scene; |
|
139 |
{ |
|
140 |
QGraphicsWidget *w = new QGraphicsWidget(); |
|
141 |
QGraphicsLinearLayout *l = new QGraphicsLinearLayout(w); |
|
142 |
QGraphicsWidget *w1 = new QGraphicsWidget; |
|
143 |
l->addItem(w1); |
|
144 |
scene.addItem(w); |
|
145 |
QCOMPARE(w1->parentWidget(), w); |
|
146 |
delete w; |
|
147 |
} |
|
148 |
{ |
|
149 |
QGraphicsWidget *w = new QGraphicsWidget(); |
|
150 |
QGraphicsLinearLayout *l = new QGraphicsLinearLayout(w); |
|
151 |
QGraphicsWidget *w1 = new QGraphicsWidget; |
|
152 |
l->addItem(w1); |
|
153 |
scene.addItem(w); |
|
154 |
QCOMPARE(w1->parentWidget(), w); |
|
155 |
||
156 |
QGraphicsWidget *ww = new QGraphicsWidget(); |
|
157 |
QGraphicsLinearLayout *l1 = new QGraphicsLinearLayout(ww); |
|
158 |
#if !defined(Q_OS_MAC) && defined(QT_DEBUG) |
|
159 |
QTest::ignoreMessage(QtWarningMsg, "QGraphicsLayout::addChildLayoutItem: QGraphicsWidget \"\"" |
|
160 |
" in wrong parent; moved to correct parent"); |
|
161 |
#endif |
|
162 |
l1->addItem(w1); |
|
163 |
QCOMPARE(w1->parentWidget(), ww); |
|
164 |
delete w; |
|
165 |
} |
|
166 |
||
167 |
QGraphicsWidget *window = new QGraphicsWidget(); |
|
168 |
scene.addItem(window); |
|
169 |
view.show(); |
|
170 |
QGraphicsLinearLayout *l1 = new QGraphicsLinearLayout(); |
|
171 |
QGraphicsWidget *w1 = new QGraphicsWidget(); |
|
172 |
l1->addItem(w1); |
|
173 |
QGraphicsWidget *w2 = new QGraphicsWidget(); |
|
174 |
l1->addItem(w2); |
|
175 |
QCOMPARE(w1->parentItem(), static_cast<QGraphicsItem*>(0)); |
|
176 |
QCOMPARE(w2->parentItem(), static_cast<QGraphicsItem*>(0)); |
|
177 |
scene.addItem(w1); |
|
178 |
QCOMPARE(w1->parentItem(), static_cast<QGraphicsItem*>(0)); |
|
179 |
window->setLayout(l1); |
|
180 |
QCOMPARE(w1->parentItem(), static_cast<QGraphicsItem*>(window)); |
|
181 |
QCOMPARE(w2->parentItem(), static_cast<QGraphicsItem*>(window)); |
|
182 |
||
183 |
// Sublayouts |
|
184 |
QGraphicsLinearLayout *l2 = new QGraphicsLinearLayout(); |
|
185 |
QGraphicsWidget *w3 = new QGraphicsWidget(); |
|
186 |
l2->addItem(w3); |
|
187 |
QGraphicsWidget *w4 = new QGraphicsWidget(); |
|
188 |
l2->addItem(w4); |
|
189 |
QGraphicsLinearLayout *l3 = new QGraphicsLinearLayout(); |
|
190 |
l2->addItem(l3); |
|
191 |
QGraphicsWidget *window2 = new QGraphicsWidget(); |
|
192 |
scene.addItem(window2); |
|
193 |
window2->setLayout(l2); |
|
194 |
||
195 |
QCOMPARE(w3->parentItem(), static_cast<QGraphicsItem*>(window2)); |
|
196 |
QCOMPARE(w4->parentItem(), static_cast<QGraphicsItem*>(window2)); |
|
197 |
||
198 |
// graphics item with another parent |
|
199 |
QGraphicsLinearLayout *l5 = new QGraphicsLinearLayout(); |
|
200 |
l5->addItem(w1); |
|
201 |
l5->addItem(w2); |
|
202 |
QCOMPARE(w1->parentItem(), static_cast<QGraphicsItem*>(window)); |
|
203 |
QCOMPARE(w2->parentItem(), static_cast<QGraphicsItem*>(window)); |
|
204 |
QGraphicsLinearLayout *l4 = new QGraphicsLinearLayout(); |
|
205 |
l4->addItem(l5); |
|
206 |
QGraphicsWidget *window3 = new QGraphicsWidget(); |
|
207 |
scene.addItem(window3); |
|
208 |
window3->setLayout(l4); |
|
209 |
||
210 |
QCOMPARE(w1->parentItem(), static_cast<QGraphicsItem*>(window3)); |
|
211 |
QCOMPARE(w2->parentItem(), static_cast<QGraphicsItem*>(window3)); |
|
212 |
} |
|
213 |
||
214 |
class TestLayout : public QGraphicsLinearLayout |
|
215 |
{ |
|
216 |
public: |
|
217 |
TestLayout(QGraphicsLayoutItem *parent = 0) |
|
218 |
: QGraphicsLinearLayout(parent) |
|
219 |
{ |
|
220 |
m_count = 0; |
|
221 |
} |
|
222 |
||
223 |
void setGeometry(const QRectF &rect) { |
|
224 |
||
225 |
++m_count; |
|
226 |
QGraphicsLinearLayout::setGeometry(rect); |
|
227 |
} |
|
228 |
||
229 |
||
230 |
int m_count; |
|
231 |
}; |
|
232 |
||
233 |
void tst_QGraphicsLayout::verifyActivate() |
|
234 |
{ |
|
235 |
QGraphicsScene scene; |
|
236 |
QGraphicsView view(&scene); |
|
237 |
||
238 |
QGraphicsWidget *window = new QGraphicsWidget(); |
|
239 |
scene.addItem(window); |
|
240 |
TestLayout *lout = new TestLayout(window); |
|
241 |
QGraphicsWidget *w = new QGraphicsWidget(); |
|
242 |
lout->addItem(w); |
|
243 |
window->setLayout(lout); |
|
244 |
||
245 |
QCOMPARE(lout->m_count, 0); |
|
246 |
window->setVisible(false); |
|
247 |
QCOMPARE(lout->m_count, 0); |
|
248 |
window->setVisible(true); |
|
249 |
// on polish or the first time a widget is shown, the widget is resized. |
|
250 |
QCOMPARE(lout->m_count, 1); |
|
251 |
||
252 |
} |
|
253 |
||
254 |
class Layout : public QGraphicsLayout |
|
255 |
{ |
|
256 |
public: |
|
257 |
Layout(QGraphicsLayoutItem *parentItem = 0) : QGraphicsLayout(parentItem) {} |
|
258 |
||
259 |
void setGeometry(const QRectF &rect) |
|
260 |
{ |
|
261 |
QGraphicsLayout::setGeometry(rect); |
|
262 |
} |
|
263 |
||
264 |
int count() const { |
|
265 |
return 0; |
|
266 |
} |
|
267 |
||
268 |
QGraphicsLayoutItem *itemAt(int index) const { |
|
269 |
Q_UNUSED(index); |
|
270 |
return 0; |
|
271 |
} |
|
272 |
||
273 |
void removeAt(int index) |
|
274 |
{ |
|
275 |
Q_UNUSED(index); |
|
276 |
} |
|
277 |
||
278 |
protected: |
|
279 |
QSizeF sizeHint(Qt::SizeHint which, const QSizeF &constraint = QSizeF()) const |
|
280 |
{ |
|
281 |
Q_UNUSED(constraint); |
|
282 |
Q_UNUSED(which); |
|
283 |
return QSizeF(100,100); |
|
284 |
} |
|
285 |
||
286 |
}; |
|
287 |
||
288 |
void tst_QGraphicsLayout::constructors() |
|
289 |
{ |
|
290 |
// Strange test, but see the fix that was with this submit |
|
291 |
QVector<Layout*> layouts; |
|
292 |
for (int pass = 0; pass < 5; ++pass) { |
|
293 |
Layout *lay = new Layout(); |
|
294 |
layouts << lay; |
|
295 |
qreal left, top, right, bottom; |
|
296 |
lay->getContentsMargins(&left, &top, &right, &bottom); |
|
297 |
// Test if the style defaults are sane (should always be ints) |
|
298 |
double intpart; |
|
299 |
QVERIFY(modf(left, &intpart) == 0.0); |
|
300 |
QVERIFY(modf(top, &intpart) == 0.0); |
|
301 |
QVERIFY(modf(right, &intpart) == 0.0); |
|
302 |
QVERIFY(modf(bottom, &intpart) == 0.0); |
|
303 |
||
304 |
lay->setContentsMargins(1, 2, 4, 8); |
|
305 |
lay->getContentsMargins(&left, &top, &right, &bottom); |
|
306 |
||
307 |
QCOMPARE(int(left), 1); |
|
308 |
QCOMPARE(int(top), 2); |
|
309 |
QCOMPARE(int(right), 4); |
|
310 |
QCOMPARE(int(bottom), 8); |
|
311 |
} |
|
312 |
||
313 |
qDeleteAll(layouts); |
|
314 |
} |
|
315 |
||
316 |
class AnimatedLayoutItem : public QGraphicsLayoutItem { |
|
317 |
public: |
|
318 |
AnimatedLayoutItem(QGraphicsRectItem *item) |
|
319 |
: QGraphicsLayoutItem() |
|
320 |
{ |
|
321 |
setGraphicsItem(item); |
|
322 |
} |
|
323 |
||
324 |
void setGeometry(const QRectF &geom); |
|
325 |
||
326 |
QSizeF sizeHint(Qt::SizeHint which, const QSizeF & constraint = QSizeF() ) const; |
|
327 |
||
328 |
inline QGraphicsRectItem *rectItem() { |
|
329 |
return static_cast<QGraphicsRectItem *>(graphicsItem()); |
|
330 |
} |
|
331 |
||
332 |
QRectF m_geom; |
|
333 |
private: |
|
334 |
AnimatedLayoutItem() {} |
|
335 |
}; |
|
336 |
||
337 |
void AnimatedLayoutItem::setGeometry(const QRectF &geom) |
|
338 |
{ |
|
339 |
QGraphicsLayoutItem::setGeometry(geom); |
|
340 |
} |
|
341 |
||
342 |
QSizeF AnimatedLayoutItem::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const |
|
343 |
{ |
|
344 |
switch (which) { |
|
345 |
case Qt::MinimumSize: |
|
346 |
return QSizeF(32,32); |
|
347 |
case Qt::PreferredSize: |
|
348 |
return QSizeF(160,90); |
|
349 |
case Qt::MaximumSize: |
|
350 |
return QSizeF(1000,1000); |
|
351 |
default: |
|
352 |
return QSizeF(300, 300); |
|
353 |
} |
|
354 |
} |
|
355 |
||
356 |
class AnimatedLayout : public QObject, public QGraphicsLinearLayout { |
|
357 |
Q_OBJECT |
|
358 |
public: |
|
359 |
AnimatedLayout(QGraphicsWidget *widget) : QGraphicsLinearLayout(widget), m_timeline(500, this) |
|
360 |
{ |
|
361 |
connect(&m_timeline, SIGNAL(valueChanged(qreal)), this, SLOT(valueChanged(qreal))); |
|
362 |
} |
|
363 |
||
364 |
void setGeometry(const QRectF &geom) { |
|
365 |
fromGeoms.clear(); |
|
366 |
toGeoms.clear(); |
|
367 |
for (int i = 0; i < count(); ++i) { |
|
368 |
fromGeoms << itemAt(i)->geometry(); |
|
369 |
} |
|
370 |
||
371 |
QGraphicsLinearLayout::setGeometry(geom); |
|
372 |
||
373 |
for (int i = 0; i < count(); ++i) { |
|
374 |
toGeoms << itemAt(i)->geometry(); |
|
375 |
} |
|
376 |
m_timeline.start(); |
|
377 |
} |
|
378 |
||
379 |
private slots: |
|
380 |
void valueChanged(qreal value) { |
|
381 |
for (int i = 0; i < fromGeoms.count(); ++i) { |
|
382 |
QGraphicsLayoutItem *li = itemAt(i); |
|
383 |
QRectF from = fromGeoms.at(i); |
|
384 |
QRectF to = toGeoms.at(i); |
|
385 |
||
386 |
QRectF geom(from.topLeft() + (to.topLeft() - from.topLeft()) * value, |
|
387 |
from.size() + (to.size() - from.size()) * value); |
|
388 |
static_cast<QGraphicsRectItem*>(li->graphicsItem())->setRect(geom); |
|
389 |
} |
|
390 |
} |
|
391 |
private: |
|
392 |
QTimeLine m_timeline; |
|
393 |
QVector<QRectF> fromGeoms; |
|
394 |
QVector<QRectF> toGeoms; |
|
395 |
}; |
|
396 |
||
397 |
||
398 |
void tst_QGraphicsLayout::alternativeLayoutItems() |
|
399 |
{ |
|
400 |
QGraphicsScene scene; |
|
401 |
QGraphicsView view(&scene); |
|
402 |
||
403 |
QGraphicsWidget *window = new QGraphicsWidget; |
|
404 |
scene.addItem(window); |
|
405 |
AnimatedLayout *lout = new AnimatedLayout(window); |
|
406 |
lout->setContentsMargins(0, 0, 0, 0); |
|
407 |
lout->setSpacing(0); |
|
408 |
||
409 |
QGraphicsRectItem *item1 = new QGraphicsRectItem; |
|
410 |
AnimatedLayoutItem *li1 = new AnimatedLayoutItem(item1); |
|
411 |
lout->addItem(li1); |
|
412 |
||
413 |
QGraphicsRectItem *item2 = new QGraphicsRectItem; |
|
414 |
AnimatedLayoutItem *li2 = new AnimatedLayoutItem(item2); |
|
415 |
lout->addItem(li2); |
|
416 |
||
417 |
QGraphicsRectItem *item3 = new QGraphicsRectItem; |
|
418 |
AnimatedLayoutItem *li3 = new AnimatedLayoutItem(item3); |
|
419 |
lout->addItem(li3); |
|
420 |
||
421 |
window->setLayout(lout); |
|
422 |
||
423 |
window->setGeometry(0, 0, 99, 99); |
|
424 |
view.setSceneRect(QRectF(-10, -10, 110, 110)); |
|
425 |
view.resize(150, 150); |
|
426 |
view.show(); |
|
427 |
||
13
c0432d11811c
eb175c3290cd7ea85da4a590db9461504a4904bc
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
428 |
QTRY_COMPARE(static_cast<QGraphicsRectItem*>(li1->graphicsItem())->rect(), QRectF( 0, 0, 33, 99)); |
c0432d11811c
eb175c3290cd7ea85da4a590db9461504a4904bc
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
429 |
QTRY_COMPARE(static_cast<QGraphicsRectItem*>(li2->graphicsItem())->rect(), QRectF(33, 0, 33, 99)); |
c0432d11811c
eb175c3290cd7ea85da4a590db9461504a4904bc
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
430 |
QTRY_COMPARE(static_cast<QGraphicsRectItem*>(li3->graphicsItem())->rect(), QRectF(66, 0, 33, 99)); |
0 | 431 |
|
432 |
lout->setOrientation(Qt::Vertical); |
|
433 |
||
13
c0432d11811c
eb175c3290cd7ea85da4a590db9461504a4904bc
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
434 |
QTRY_COMPARE(static_cast<QGraphicsRectItem*>(li1->graphicsItem())->rect(), QRectF(0, 0, 99, 33)); |
c0432d11811c
eb175c3290cd7ea85da4a590db9461504a4904bc
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
435 |
QTRY_COMPARE(static_cast<QGraphicsRectItem*>(li2->graphicsItem())->rect(), QRectF(0, 33, 99, 33)); |
c0432d11811c
eb175c3290cd7ea85da4a590db9461504a4904bc
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
436 |
QTRY_COMPARE(static_cast<QGraphicsRectItem*>(li3->graphicsItem())->rect(), QRectF(0, 66, 99, 33)); |
0 | 437 |
|
438 |
} |
|
439 |
||
440 |
class CustomLayoutItem : public QGraphicsLayoutItem { |
|
441 |
public: |
|
442 |
CustomLayoutItem(QSet<QGraphicsLayoutItem*> *destructedSet) |
|
443 |
: QGraphicsLayoutItem() |
|
444 |
{ |
|
445 |
m_destructedSet = destructedSet; |
|
446 |
setOwnedByLayout(true); |
|
447 |
} |
|
448 |
||
449 |
QSizeF sizeHint(Qt::SizeHint which, const QSizeF & constraint = QSizeF() ) const; |
|
450 |
||
451 |
~CustomLayoutItem() { |
|
452 |
m_destructedSet->insert(this); |
|
453 |
} |
|
454 |
private: |
|
455 |
QSet<QGraphicsLayoutItem*> *m_destructedSet; |
|
456 |
}; |
|
457 |
||
458 |
QSizeF CustomLayoutItem::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const |
|
459 |
{ |
|
460 |
switch (which) { |
|
461 |
case Qt::MinimumSize: |
|
462 |
return QSizeF(32,32); |
|
463 |
case Qt::PreferredSize: |
|
464 |
return QSizeF(160,90); |
|
465 |
case Qt::MaximumSize: |
|
466 |
return QSizeF(1000,1000); |
|
467 |
default: |
|
468 |
return QSizeF(300, 300); |
|
469 |
} |
|
470 |
} |
|
471 |
||
472 |
class CustomGraphicsWidget : public QGraphicsWidget { |
|
473 |
public: |
|
474 |
CustomGraphicsWidget(QSet<QGraphicsLayoutItem*> *destructedSet = 0) |
|
475 |
: QGraphicsWidget() |
|
476 |
{ |
|
477 |
m_destructedSet = destructedSet; |
|
478 |
} |
|
479 |
||
480 |
QSizeF sizeHint(Qt::SizeHint which, const QSizeF & constraint = QSizeF() ) const; |
|
481 |
||
482 |
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget * = 0) |
|
483 |
{ |
|
484 |
const QRect r = option->rect.adjusted(0, 0, -1, -1); |
|
485 |
painter->drawLine(r.topLeft(), r.bottomRight()); |
|
486 |
painter->drawLine(r.bottomLeft(), r.topRight()); |
|
487 |
painter->drawRect(r); |
|
488 |
} |
|
489 |
||
490 |
~CustomGraphicsWidget() { |
|
491 |
if (m_destructedSet) |
|
492 |
m_destructedSet->insert(this); |
|
493 |
} |
|
494 |
private: |
|
495 |
QSet<QGraphicsLayoutItem*> *m_destructedSet; |
|
496 |
}; |
|
497 |
||
498 |
QSizeF CustomGraphicsWidget::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const |
|
499 |
{ |
|
500 |
switch (which) { |
|
501 |
case Qt::MinimumSize: |
|
502 |
return QSizeF(32,32); |
|
503 |
case Qt::PreferredSize: |
|
504 |
return QSizeF(160,90); |
|
505 |
case Qt::MaximumSize: |
|
506 |
return QSizeF(1000,1000); |
|
507 |
default: |
|
508 |
return QSizeF(300, 300); |
|
509 |
} |
|
510 |
} |
|
511 |
||
512 |
static bool compareSets(const QSet<QGraphicsLayoutItem*> &actual, const QSet<QGraphicsLayoutItem*> &expected) |
|
513 |
{ |
|
514 |
if (actual != expected) { |
|
515 |
qDebug() << "actual:" << actual << "expected:" << expected; |
|
516 |
return false; |
|
517 |
} |
|
518 |
return true; |
|
519 |
} |
|
520 |
||
521 |
class CustomLayout : public QGraphicsLayout |
|
522 |
{ |
|
523 |
public : |
|
524 |
CustomLayout(QGraphicsLayoutItem *parent) |
|
525 |
: QGraphicsLayout(parent) |
|
526 |
{ |
|
527 |
} |
|
528 |
||
529 |
||
530 |
~CustomLayout() |
|
531 |
{ |
|
532 |
} |
|
533 |
||
534 |
int count() const |
|
535 |
{ |
|
536 |
return items.count(); |
|
537 |
} |
|
538 |
||
539 |
QGraphicsLayoutItem* itemAt(int index) const |
|
540 |
{ |
|
541 |
return items.at(index); |
|
542 |
} |
|
543 |
||
544 |
||
545 |
void removeAt(int index) |
|
546 |
{ |
|
547 |
items.removeAt(index); |
|
548 |
} |
|
549 |
||
550 |
void addItem(QGraphicsLayoutItem *item) |
|
551 |
{ |
|
552 |
insertItem(items.count(), item); |
|
553 |
} |
|
554 |
||
555 |
void insertItem(int index, QGraphicsLayoutItem *item) |
|
556 |
{ |
|
557 |
index = qBound(0, index, items.count()); |
|
558 |
||
559 |
item->setParentLayoutItem(this); |
|
560 |
||
561 |
QGraphicsWidget *widget = static_cast<QGraphicsWidget *>(item); |
|
562 |
updateParentWidget(widget); |
|
563 |
||
564 |
||
565 |
if (index == items.count()) { |
|
566 |
items.append(item); |
|
567 |
} else { |
|
568 |
items.insert(index, item); |
|
569 |
} |
|
570 |
||
571 |
updateGeometry(); |
|
572 |
activate(); |
|
573 |
} |
|
574 |
||
575 |
void updateParentWidget(QGraphicsWidget *item) |
|
576 |
{ |
|
577 |
QGraphicsLayoutItem *parentItem = parentLayoutItem(); |
|
578 |
while (parentItem && parentItem->isLayout()) { |
|
579 |
parentItem = parentItem->parentLayoutItem(); |
|
580 |
} |
|
581 |
||
582 |
if (parentItem) { |
|
583 |
item->setParentItem(static_cast<QGraphicsWidget*>(parentItem)); |
|
584 |
} |
|
585 |
} |
|
586 |
||
587 |
QSizeF sizeHint(Qt::SizeHint which, const QSizeF &constraint) const |
|
588 |
{ |
|
589 |
return QSizeF(50,50); |
|
590 |
} |
|
591 |
||
592 |
QList<QGraphicsLayoutItem*> items; |
|
593 |
||
594 |
}; |
|
595 |
||
596 |
void tst_QGraphicsLayout::ownership() |
|
597 |
{ |
|
598 |
QGraphicsScene scene; |
|
599 |
QGraphicsView view(&scene); |
|
600 |
||
601 |
{ |
|
602 |
QGraphicsLinearLayout *lay = new QGraphicsLinearLayout; |
|
603 |
QSet<QGraphicsLayoutItem*> destructedSet; |
|
604 |
CustomLayoutItem *li1 = new CustomLayoutItem(&destructedSet); |
|
605 |
lay->addItem(li1); |
|
606 |
CustomLayoutItem *li2 = new CustomLayoutItem(&destructedSet); |
|
607 |
lay->addItem(li2); |
|
608 |
CustomLayoutItem *li3 = new CustomLayoutItem(&destructedSet); |
|
609 |
lay->addItem(li3); |
|
610 |
destructedSet.clear(); |
|
611 |
||
612 |
delete lay; |
|
613 |
QSet<QGraphicsLayoutItem*> expected; |
|
614 |
expected << li1 << li2 << li3; |
|
615 |
QVERIFY(compareSets(destructedSet, expected)); |
|
616 |
} |
|
617 |
||
618 |
{ |
|
619 |
QGraphicsWidget *window = new QGraphicsWidget; |
|
620 |
QGraphicsLinearLayout *lay = new QGraphicsLinearLayout; |
|
621 |
QSet<QGraphicsLayoutItem*> destructedSet; |
|
622 |
CustomGraphicsWidget *li1 = new CustomGraphicsWidget(&destructedSet); |
|
623 |
lay->addItem(li1); |
|
624 |
CustomGraphicsWidget *li2 = new CustomGraphicsWidget(&destructedSet); |
|
625 |
lay->addItem(li2); |
|
626 |
CustomGraphicsWidget *li3 = new CustomGraphicsWidget(&destructedSet); |
|
627 |
lay->addItem(li3); |
|
628 |
window->setLayout(lay); |
|
629 |
scene.addItem(window); |
|
630 |
||
631 |
destructedSet.clear(); |
|
632 |
window->setLayout(0); |
|
633 |
QVERIFY(destructedSet.count() == 0); |
|
634 |
delete window; |
|
635 |
} |
|
636 |
||
637 |
{ |
|
638 |
QGraphicsWidget *window = new QGraphicsWidget(0, Qt::Window); |
|
639 |
QGraphicsLinearLayout *lay = new QGraphicsLinearLayout; |
|
640 |
||
641 |
CustomGraphicsWidget *li1 = new CustomGraphicsWidget; |
|
642 |
lay->addItem(li1); |
|
643 |
||
644 |
QGraphicsLinearLayout *li2 = new QGraphicsLinearLayout; |
|
645 |
CustomGraphicsWidget *li2_1 = new CustomGraphicsWidget; |
|
646 |
li2->addItem(li2_1); |
|
647 |
CustomGraphicsWidget *li2_2 = new CustomGraphicsWidget; |
|
648 |
li2->addItem(li2_2); |
|
649 |
CustomGraphicsWidget *li2_3 = new CustomGraphicsWidget; |
|
650 |
li2->addItem(li2_3); |
|
651 |
lay->addItem(li2); |
|
652 |
||
653 |
CustomGraphicsWidget *li3 = new CustomGraphicsWidget; |
|
654 |
lay->addItem(li3); |
|
655 |
||
656 |
window->setLayout(lay); |
|
657 |
scene.addItem(window); |
|
658 |
view.resize(500, 200); |
|
659 |
view.show(); |
|
660 |
||
661 |
for (int i = li2->count(); i > 0; --i) { |
|
662 |
QCOMPARE(li2->count(), i); |
|
663 |
delete li2->itemAt(0); |
|
664 |
} |
|
665 |
||
666 |
for (int i = lay->count(); i > 0; --i) { |
|
667 |
QCOMPARE(lay->count(), i); |
|
668 |
delete lay->itemAt(0); |
|
669 |
} |
|
670 |
||
671 |
delete window; |
|
672 |
} |
|
673 |
||
674 |
{ |
|
675 |
QGraphicsWidget *top = new QGraphicsWidget; |
|
676 |
QGraphicsWidget *w = new QGraphicsWidget; |
|
677 |
QGraphicsWidget *w2 = new QGraphicsWidget; |
|
678 |
CustomLayout *layout = new CustomLayout(top); |
|
679 |
layout->addItem(w); |
|
680 |
layout->addItem(w2); |
|
681 |
top->setLayout(layout); |
|
682 |
delete top; |
|
683 |
//don't crash after that. |
|
684 |
} |
|
685 |
} |
|
686 |
||
687 |
QTEST_MAIN(tst_QGraphicsLayout) |
|
688 |
#include "tst_qgraphicslayout.moc" |