author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Tue, 02 Feb 2010 00:43:10 +0200 | |
changeset 3 | 41300fa6a67c |
parent 0 | 1918ee327afb |
child 4 | 3b1da2848fc7 |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
3 |
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
4 |
** All rights reserved. |
|
5 |
** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 |
** |
|
7 |
** This file is part of the test suite of the Qt Toolkit. |
|
8 |
** |
|
9 |
** $QT_BEGIN_LICENSE:LGPL$ |
|
10 |
** No Commercial Usage |
|
11 |
** This file contains pre-release code and may not be distributed. |
|
12 |
** You may use this file in accordance with the terms and conditions |
|
13 |
** contained in the Technology Preview License Agreement accompanying |
|
14 |
** this package. |
|
15 |
** |
|
16 |
** GNU Lesser General Public License Usage |
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 |
** General Public License version 2.1 as published by the Free Software |
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 |
** packaging of this file. Please review the following information to |
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 |
** |
|
24 |
** In addition, as a special exception, Nokia gives you certain additional |
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 |
** |
|
28 |
** If you have questions regarding the use of this file, please contact |
|
29 |
** Nokia at qt-info@nokia.com. |
|
30 |
** |
|
31 |
** |
|
32 |
** |
|
33 |
** |
|
34 |
** |
|
35 |
** |
|
36 |
** |
|
37 |
** |
|
38 |
** $QT_END_LICENSE$ |
|
39 |
** |
|
40 |
****************************************************************************/ |
|
41 |
||
42 |
||
43 |
#include <QtTest/QtTest> |
|
44 |
||
45 |
#include <qgraphicsitem.h> |
|
46 |
#include <qgraphicsscene.h> |
|
47 |
#include <qgraphicssceneevent.h> |
|
48 |
#include <qgraphicsview.h> |
|
49 |
#include <qgraphicswidget.h> |
|
50 |
#include <qgraphicsproxywidget.h> |
|
51 |
||
52 |
#include <math.h> |
|
53 |
||
54 |
#include <QtGui/QLabel> |
|
55 |
#if !defined(QT_NO_STYLE_MOTIF) |
|
56 |
#include <QtGui/QMotifStyle> |
|
57 |
#endif |
|
58 |
#if !defined(QT_NO_STYLE_WINDOWS) |
|
59 |
#include <QtGui/QWindowsStyle> |
|
60 |
#endif |
|
61 |
#if !defined(QT_NO_STYLE_PLASTIQUE) |
|
62 |
#include <QtGui/QPlastiqueStyle> |
|
63 |
#endif |
|
64 |
#include <QtGui/QPainterPath> |
|
65 |
#include <QtGui/QRubberBand> |
|
66 |
#include <QtGui/QScrollBar> |
|
67 |
#include <QtGui/QStyleOption> |
|
68 |
#include <QtGui/QBoxLayout> |
|
69 |
#include <QtGui/QStyle> |
|
70 |
#include <QtGui/QPushButton> |
|
71 |
#include <QtGui/QInputContext> |
|
72 |
#include <private/qgraphicsview_p.h> |
|
73 |
#include "../../shared/util.h" |
|
74 |
||
75 |
//TESTED_CLASS= |
|
76 |
//TESTED_FILES= |
|
77 |
||
78 |
Q_DECLARE_METATYPE(QList<int>) |
|
79 |
Q_DECLARE_METATYPE(QList<QRectF>) |
|
80 |
Q_DECLARE_METATYPE(QMatrix) |
|
81 |
Q_DECLARE_METATYPE(QPainterPath) |
|
82 |
Q_DECLARE_METATYPE(QPointF) |
|
83 |
Q_DECLARE_METATYPE(QPolygonF) |
|
84 |
Q_DECLARE_METATYPE(QRectF) |
|
85 |
Q_DECLARE_METATYPE(Qt::ScrollBarPolicy) |
|
86 |
||
87 |
#ifdef Q_WS_MAC |
|
88 |
//On mac we get full update. So check that the expected region is contained inside the actual |
|
89 |
#define COMPARE_REGIONS(ACTUAL, EXPECTED) QVERIFY((EXPECTED).subtracted(ACTUAL).isEmpty()) |
|
90 |
#else |
|
91 |
#define COMPARE_REGIONS QCOMPARE |
|
92 |
#endif |
|
93 |
||
94 |
static void sendMousePress(QWidget *widget, const QPoint &point, Qt::MouseButton button = Qt::LeftButton) |
|
95 |
{ |
|
96 |
QMouseEvent event(QEvent::MouseButtonPress, point, widget->mapToGlobal(point), button, 0, 0); |
|
97 |
QApplication::sendEvent(widget, &event); |
|
98 |
} |
|
99 |
||
100 |
static void sendMouseMove(QWidget *widget, const QPoint &point, Qt::MouseButton button = Qt::NoButton, Qt::MouseButtons buttons = 0) |
|
101 |
{ |
|
102 |
QTest::mouseMove(widget, point); |
|
103 |
QMouseEvent event(QEvent::MouseMove, point, button, buttons, 0); |
|
104 |
QApplication::sendEvent(widget, &event); |
|
105 |
} |
|
106 |
||
107 |
static void sendMouseRelease(QWidget *widget, const QPoint &point, Qt::MouseButton button = Qt::LeftButton) |
|
108 |
{ |
|
109 |
QMouseEvent event(QEvent::MouseButtonRelease, point, widget->mapToGlobal(point), button, 0, 0); |
|
110 |
QApplication::sendEvent(widget, &event); |
|
111 |
} |
|
112 |
||
113 |
class EventSpy : public QObject |
|
114 |
{ |
|
115 |
Q_OBJECT |
|
116 |
public: |
|
117 |
EventSpy(QObject *watched, QEvent::Type type) |
|
118 |
: _count(0), spied(type) |
|
119 |
{ |
|
120 |
watched->installEventFilter(this); |
|
121 |
} |
|
122 |
||
123 |
int count() const { return _count; } |
|
124 |
void reset() { _count = 0; } |
|
125 |
||
126 |
protected: |
|
127 |
bool eventFilter(QObject *watched, QEvent *event) |
|
128 |
{ |
|
129 |
Q_UNUSED(watched); |
|
130 |
if (event->type() == spied) |
|
131 |
++_count; |
|
132 |
return false; |
|
133 |
} |
|
134 |
||
135 |
int _count; |
|
136 |
QEvent::Type spied; |
|
137 |
}; |
|
138 |
||
139 |
class tst_QGraphicsView : public QObject |
|
140 |
{ |
|
141 |
Q_OBJECT |
|
142 |
||
143 |
private slots: |
|
144 |
void initTestCase(); |
|
145 |
void construction(); |
|
146 |
void renderHints(); |
|
147 |
void alignment(); |
|
148 |
void interactive(); |
|
149 |
void scene(); |
|
150 |
void setScene(); |
|
151 |
void deleteScene(); |
|
152 |
void sceneRect(); |
|
153 |
void sceneRect_growing(); |
|
154 |
void setSceneRect(); |
|
155 |
void viewport(); |
|
156 |
void dragMode_scrollHand(); |
|
157 |
void dragMode_rubberBand(); |
|
158 |
void rubberBandSelectionMode(); |
|
159 |
void backgroundBrush(); |
|
160 |
void foregroundBrush(); |
|
161 |
void matrix(); |
|
162 |
void matrix_convenience(); |
|
163 |
void matrix_combine(); |
|
164 |
void centerOnPoint(); |
|
165 |
void centerOnItem(); |
|
166 |
void ensureVisibleRect(); |
|
167 |
void fitInView(); |
|
168 |
void itemsAtPoint(); |
|
169 |
void itemsInRect(); |
|
170 |
void itemsInRect_cosmeticAdjust_data(); |
|
171 |
void itemsInRect_cosmeticAdjust(); |
|
172 |
void itemsInPoly(); |
|
173 |
void itemsInPath(); |
|
174 |
void itemAt(); |
|
175 |
void itemAt2(); |
|
176 |
void mapToScene(); |
|
177 |
void mapToScenePoint(); |
|
178 |
void mapToSceneRect_data(); |
|
179 |
void mapToSceneRect(); |
|
180 |
void mapToScenePoly(); |
|
181 |
void mapToScenePath(); |
|
182 |
void mapFromScenePoint(); |
|
183 |
void mapFromSceneRect(); |
|
184 |
void mapFromScenePoly(); |
|
185 |
void mapFromScenePath(); |
|
186 |
void sendEvent(); |
|
187 |
void wheelEvent(); |
|
188 |
void cursor(); |
|
189 |
void cursor2(); |
|
190 |
void transformationAnchor(); |
|
191 |
void resizeAnchor(); |
|
192 |
void viewportUpdateMode(); |
|
193 |
void viewportUpdateMode2(); |
|
194 |
void acceptDrops(); |
|
195 |
void optimizationFlags(); |
|
196 |
void optimizationFlags_dontSavePainterState(); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
197 |
void optimizationFlags_dontSavePainterState2_data(); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
198 |
void optimizationFlags_dontSavePainterState2(); |
0 | 199 |
void levelOfDetail_data(); |
200 |
void levelOfDetail(); |
|
201 |
void scrollBarRanges_data(); |
|
202 |
void scrollBarRanges(); |
|
203 |
void acceptMousePressEvent(); |
|
204 |
void replayMouseMove(); |
|
205 |
void itemsUnderMouse(); |
|
206 |
void embeddedViews(); |
|
207 |
void scrollAfterResize_data(); |
|
208 |
void scrollAfterResize(); |
|
209 |
void moveItemWhileScrolling_data(); |
|
210 |
void moveItemWhileScrolling(); |
|
211 |
void centerOnDirtyItem(); |
|
212 |
void mouseTracking(); |
|
213 |
void mouseTracking2(); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
214 |
void mouseTracking3(); |
0 | 215 |
void render(); |
216 |
void exposeRegion(); |
|
217 |
void update_data(); |
|
218 |
void update(); |
|
219 |
void inputMethodSensitivity(); |
|
220 |
void inputContextReset(); |
|
221 |
||
222 |
// task specific tests below me |
|
223 |
void task172231_untransformableItems(); |
|
224 |
void task180429_mouseReleaseDragMode(); |
|
225 |
void task187791_setSceneCausesUpdate(); |
|
226 |
void task186827_deleteReplayedItem(); |
|
227 |
void task207546_focusCrash(); |
|
228 |
void task210599_unsetDragWhileDragging(); |
|
229 |
void task236394_sendShortcutOverrideEvent(); |
|
230 |
void task239729_noViewUpdate_data(); |
|
231 |
void task239729_noViewUpdate(); |
|
232 |
void task239047_fitInViewSmallViewport(); |
|
233 |
void task245469_itemsAtPointWithClip(); |
|
234 |
void task253415_reconnectUpdateSceneOnSceneChanged(); |
|
235 |
void task255529_transformationAnchorMouseAndViewportMargins(); |
|
236 |
void task259503_scrollingArtifacts(); |
|
237 |
void QTBUG_4151_clipAndIgnore_data(); |
|
238 |
void QTBUG_4151_clipAndIgnore(); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
239 |
void QTBUG_5859_exposedRect(); |
0 | 240 |
}; |
241 |
||
242 |
void tst_QGraphicsView::initTestCase() |
|
243 |
{ |
|
244 |
#ifdef Q_OS_WINCE_WM |
|
245 |
qApp->setAutoMaximizeThreshold(-1); |
|
246 |
#endif |
|
247 |
} |
|
248 |
||
249 |
void tst_QGraphicsView::construction() |
|
250 |
{ |
|
251 |
QGraphicsView view; |
|
252 |
QCOMPARE(view.renderHints(), QPainter::TextAntialiasing); |
|
253 |
QCOMPARE(view.dragMode(), QGraphicsView::NoDrag); |
|
254 |
QVERIFY(view.isInteractive()); |
|
255 |
QVERIFY(!view.scene()); |
|
256 |
QCOMPARE(view.sceneRect(), QRectF()); |
|
257 |
QVERIFY(view.viewport()); |
|
258 |
QCOMPARE(view.viewport()->metaObject()->className(), "QWidget"); |
|
259 |
QCOMPARE(view.matrix(), QMatrix()); |
|
260 |
QVERIFY(view.items().isEmpty()); |
|
261 |
QVERIFY(view.items(QPoint()).isEmpty()); |
|
262 |
QVERIFY(view.items(QRect()).isEmpty()); |
|
263 |
QVERIFY(view.items(QPolygon()).isEmpty()); |
|
264 |
QVERIFY(view.items(QPainterPath()).isEmpty()); |
|
265 |
QVERIFY(!view.itemAt(QPoint())); |
|
266 |
QCOMPARE(view.mapToScene(QPoint()), QPointF()); |
|
267 |
QCOMPARE(view.mapToScene(QRect()), QPolygonF()); |
|
268 |
QCOMPARE(view.mapToScene(QPolygon()), QPolygonF()); |
|
269 |
QCOMPARE(view.mapFromScene(QPointF()), QPoint()); |
|
270 |
QPolygon poly; |
|
271 |
poly << QPoint() << QPoint() << QPoint() << QPoint(); |
|
272 |
QCOMPARE(view.mapFromScene(QRectF()), poly); |
|
273 |
QCOMPARE(view.mapFromScene(QPolygonF()), QPolygon()); |
|
274 |
QCOMPARE(view.transformationAnchor(), QGraphicsView::AnchorViewCenter); |
|
275 |
QCOMPARE(view.resizeAnchor(), QGraphicsView::NoAnchor); |
|
276 |
view.show(); |
|
277 |
QTest::qWaitForWindowShown(&view); |
|
278 |
} |
|
279 |
||
280 |
class TestItem : public QGraphicsItem |
|
281 |
{ |
|
282 |
public: |
|
283 |
QRectF boundingRect() const |
|
284 |
{ return QRectF(-10, -10, 20, 20); } |
|
285 |
||
286 |
void paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *) |
|
287 |
{ hints = painter->renderHints(); painter->drawRect(boundingRect()); } |
|
288 |
||
289 |
bool sceneEvent(QEvent *event) |
|
290 |
{ |
|
291 |
events << event->type(); |
|
292 |
return QGraphicsItem::sceneEvent(event); |
|
293 |
} |
|
294 |
||
295 |
QList<QEvent::Type> events; |
|
296 |
QPainter::RenderHints hints; |
|
297 |
}; |
|
298 |
||
299 |
void tst_QGraphicsView::renderHints() |
|
300 |
{ |
|
301 |
QGraphicsView view; |
|
302 |
QCOMPARE(view.renderHints(), QPainter::TextAntialiasing); |
|
303 |
view.setRenderHint(QPainter::TextAntialiasing, false); |
|
304 |
QCOMPARE(view.renderHints(), 0); |
|
305 |
view.setRenderHint(QPainter::Antialiasing, false); |
|
306 |
QCOMPARE(view.renderHints(), 0); |
|
307 |
view.setRenderHint(QPainter::TextAntialiasing, true); |
|
308 |
QCOMPARE(view.renderHints(), QPainter::TextAntialiasing); |
|
309 |
view.setRenderHint(QPainter::Antialiasing); |
|
310 |
QCOMPARE(view.renderHints(), QPainter::TextAntialiasing | QPainter::Antialiasing); |
|
311 |
view.setRenderHints(0); |
|
312 |
QCOMPARE(view.renderHints(), 0); |
|
313 |
||
314 |
TestItem *item = new TestItem; |
|
315 |
QGraphicsScene scene; |
|
316 |
scene.addItem(item); |
|
317 |
||
318 |
view.setScene(&scene); |
|
319 |
||
320 |
view.setRenderHints(QPainter::TextAntialiasing | QPainter::Antialiasing | QPainter::NonCosmeticDefaultPen); |
|
321 |
QCOMPARE(view.renderHints(), QPainter::TextAntialiasing | QPainter::Antialiasing | QPainter::NonCosmeticDefaultPen); |
|
322 |
||
323 |
QCOMPARE(item->hints, 0); |
|
324 |
view.show(); |
|
325 |
QTest::qWaitForWindowShown(&view); |
|
326 |
view.repaint(); |
|
327 |
QTRY_COMPARE(item->hints, view.renderHints()); |
|
328 |
||
329 |
view.setRenderHints(QPainter::Antialiasing | QPainter::NonCosmeticDefaultPen); |
|
330 |
QCOMPARE(view.renderHints(), QPainter::Antialiasing | QPainter::NonCosmeticDefaultPen); |
|
331 |
||
332 |
view.repaint(); |
|
333 |
QTRY_COMPARE(item->hints, view.renderHints()); |
|
334 |
} |
|
335 |
||
336 |
void tst_QGraphicsView::alignment() |
|
337 |
{ |
|
338 |
QGraphicsScene scene; |
|
339 |
scene.addRect(QRectF(-10, -10, 20, 20)); |
|
340 |
||
341 |
QGraphicsView view(&scene); |
|
342 |
view.show(); |
|
343 |
QTest::qWaitForWindowShown(&view); |
|
344 |
||
345 |
for (int i = 0; i < 3; ++i) { |
|
346 |
for (int j = 0; j < 3; ++j) { |
|
347 |
Qt::Alignment alignment = 0; |
|
348 |
switch (i) { |
|
349 |
case 0: |
|
350 |
alignment |= Qt::AlignLeft; |
|
351 |
break; |
|
352 |
case 1: |
|
353 |
alignment |= Qt::AlignHCenter; |
|
354 |
break; |
|
355 |
case 2: |
|
356 |
default: |
|
357 |
alignment |= Qt::AlignRight; |
|
358 |
break; |
|
359 |
} |
|
360 |
switch (j) { |
|
361 |
case 0: |
|
362 |
alignment |= Qt::AlignTop; |
|
363 |
break; |
|
364 |
case 1: |
|
365 |
alignment |= Qt::AlignVCenter; |
|
366 |
break; |
|
367 |
case 2: |
|
368 |
default: |
|
369 |
alignment |= Qt::AlignBottom; |
|
370 |
break; |
|
371 |
} |
|
372 |
view.setAlignment(alignment); |
|
373 |
QCOMPARE(view.alignment(), alignment); |
|
374 |
||
375 |
for (int k = 0; k < 3; ++k) { |
|
376 |
view.resize(100 + k * 25, 100 + k * 25); |
|
377 |
QApplication::processEvents(); |
|
378 |
} |
|
379 |
} |
|
380 |
} |
|
381 |
} |
|
382 |
||
383 |
void tst_QGraphicsView::interactive() |
|
384 |
{ |
|
385 |
TestItem *item = new TestItem; |
|
386 |
item->setFlags(QGraphicsItem::ItemIsMovable); |
|
387 |
QCOMPARE(item->events.size(), 0); |
|
388 |
||
389 |
QGraphicsScene scene(-200, -200, 400, 400); |
|
390 |
scene.addItem(item); |
|
391 |
||
392 |
QGraphicsView view(&scene); |
|
393 |
view.setFixedSize(300, 300); |
|
394 |
QCOMPARE(item->events.size(), 0); |
|
395 |
view.show(); |
|
396 |
QTest::qWaitForWindowShown(&view); |
|
397 |
||
398 |
QApplication::processEvents(); |
|
399 |
QTRY_COMPARE(item->events.size(), 1); // activate |
|
400 |
||
401 |
QPoint itemPoint = view.mapFromScene(item->scenePos()); |
|
402 |
||
403 |
QVERIFY(view.itemAt(itemPoint)); |
|
404 |
||
405 |
for (int i = 0; i < 100; ++i) { |
|
406 |
sendMousePress(view.viewport(), itemPoint); |
|
407 |
QCOMPARE(item->events.size(), i * 5 + 3); |
|
408 |
QCOMPARE(item->events.at(item->events.size() - 2), QEvent::GrabMouse); |
|
409 |
QCOMPARE(item->events.at(item->events.size() - 1), QEvent::GraphicsSceneMousePress); |
|
410 |
sendMouseRelease(view.viewport(), itemPoint); |
|
411 |
QCOMPARE(item->events.size(), i * 5 + 5); |
|
412 |
QCOMPARE(item->events.at(item->events.size() - 2), QEvent::GraphicsSceneMouseRelease); |
|
413 |
QCOMPARE(item->events.at(item->events.size() - 1), QEvent::UngrabMouse); |
|
414 |
QContextMenuEvent contextEvent(QContextMenuEvent::Mouse, itemPoint, view.mapToGlobal(itemPoint)); |
|
415 |
QApplication::sendEvent(view.viewport(), &contextEvent); |
|
416 |
QCOMPARE(item->events.size(), i * 5 + 6); |
|
417 |
QCOMPARE(item->events.last(), QEvent::GraphicsSceneContextMenu); |
|
418 |
} |
|
419 |
||
420 |
view.setInteractive(false); |
|
421 |
||
422 |
for (int i = 0; i < 100; ++i) { |
|
423 |
sendMousePress(view.viewport(), itemPoint); |
|
424 |
QCOMPARE(item->events.size(), 501); |
|
425 |
QCOMPARE(item->events.last(), QEvent::GraphicsSceneContextMenu); |
|
426 |
sendMouseRelease(view.viewport(), itemPoint); |
|
427 |
QCOMPARE(item->events.size(), 501); |
|
428 |
QCOMPARE(item->events.last(), QEvent::GraphicsSceneContextMenu); |
|
429 |
QContextMenuEvent contextEvent(QContextMenuEvent::Mouse, itemPoint, view.mapToGlobal(itemPoint)); |
|
430 |
QApplication::sendEvent(view.viewport(), &contextEvent); |
|
431 |
QCOMPARE(item->events.size(), 501); |
|
432 |
QCOMPARE(item->events.last(), QEvent::GraphicsSceneContextMenu); |
|
433 |
} |
|
434 |
} |
|
435 |
||
436 |
void tst_QGraphicsView::scene() |
|
437 |
{ |
|
438 |
QGraphicsView view; |
|
439 |
QVERIFY(!view.scene()); |
|
440 |
view.setScene(0); |
|
441 |
QVERIFY(!view.scene()); |
|
442 |
||
443 |
{ |
|
444 |
QGraphicsScene scene; |
|
445 |
view.setScene(&scene); |
|
446 |
QCOMPARE(view.scene(), &scene); |
|
447 |
} |
|
448 |
||
449 |
QCOMPARE(view.scene(), (QGraphicsScene *)0); |
|
450 |
} |
|
451 |
||
452 |
void tst_QGraphicsView::setScene() |
|
453 |
{ |
|
454 |
QGraphicsScene scene(-1000, -1000, 2000, 2000); |
|
455 |
||
456 |
QGraphicsView view(&scene); |
|
457 |
view.show(); |
|
458 |
QTest::qWaitForWindowShown(&view); |
|
459 |
||
460 |
QCOMPARE(view.sceneRect(), scene.sceneRect()); |
|
461 |
||
462 |
QVERIFY(view.horizontalScrollBar()->isVisible()); |
|
463 |
QVERIFY(view.verticalScrollBar()->isVisible()); |
|
464 |
QVERIFY(!view.horizontalScrollBar()->isHidden()); |
|
465 |
QVERIFY(!view.verticalScrollBar()->isHidden()); |
|
466 |
||
467 |
view.setScene(0); |
|
468 |
||
469 |
QTest::qWait(25); |
|
470 |
||
471 |
QVERIFY(!view.horizontalScrollBar()->isVisible()); |
|
472 |
QVERIFY(!view.verticalScrollBar()->isVisible()); |
|
473 |
QVERIFY(!view.horizontalScrollBar()->isHidden()); |
|
474 |
QVERIFY(!view.verticalScrollBar()->isHidden()); |
|
475 |
||
476 |
QCOMPARE(view.sceneRect(), QRectF()); |
|
477 |
} |
|
478 |
||
479 |
void tst_QGraphicsView::deleteScene() |
|
480 |
{ |
|
481 |
QGraphicsScene *scene = new QGraphicsScene; |
|
482 |
QGraphicsView view1(scene); |
|
483 |
view1.show(); |
|
484 |
QGraphicsView view2(scene); |
|
485 |
view2.show(); |
|
486 |
QGraphicsView view3(scene); |
|
487 |
view3.show(); |
|
488 |
delete scene; |
|
489 |
QCOMPARE(view1.scene(), (QGraphicsScene *)0); |
|
490 |
QCOMPARE(view2.scene(), (QGraphicsScene *)0); |
|
491 |
QCOMPARE(view3.scene(), (QGraphicsScene *)0); |
|
492 |
} |
|
493 |
||
494 |
void tst_QGraphicsView::sceneRect() |
|
495 |
{ |
|
496 |
QGraphicsView view; |
|
497 |
QCOMPARE(view.sceneRect(), QRectF()); |
|
498 |
||
499 |
view.setSceneRect(QRectF(-100, -100, 200, 200)); |
|
500 |
QCOMPARE(view.sceneRect(), QRectF(-100, -100, 200, 200)); |
|
501 |
view.setSceneRect(-100, -100, 200, 200); |
|
502 |
QCOMPARE(view.sceneRect(), QRectF(-100, -100, 200, 200)); |
|
503 |
||
504 |
view.setSceneRect(QRectF()); |
|
505 |
QCOMPARE(view.sceneRect(), QRectF()); |
|
506 |
QGraphicsScene scene; |
|
507 |
QGraphicsRectItem *item = scene.addRect(QRectF(-100, -100, 100, 100)); |
|
508 |
||
509 |
view.setScene(&scene); |
|
510 |
||
511 |
QCOMPARE(view.sceneRect(), QRectF(-100, -100, 100, 100)); |
|
512 |
item->moveBy(-100, -100); |
|
513 |
QCOMPARE(view.sceneRect(), QRectF(-200, -200, 200, 200)); |
|
514 |
item->moveBy(100, 100); |
|
515 |
QCOMPARE(view.sceneRect(), QRectF(-200, -200, 200, 200)); |
|
516 |
||
517 |
view.setScene(0); |
|
518 |
view.setSceneRect(QRectF()); |
|
519 |
QCOMPARE(view.sceneRect(), QRectF()); |
|
520 |
} |
|
521 |
||
522 |
void tst_QGraphicsView::sceneRect_growing() |
|
523 |
{ |
|
524 |
QGraphicsScene scene; |
|
525 |
for (int i = 0; i < 100; ++i) |
|
526 |
scene.addText(QString("(0, %1)").arg((i - 50) * 20))->setPos(0, (i - 50) * 20); |
|
527 |
||
528 |
QGraphicsView view(&scene); |
|
529 |
view.setFixedSize(200, 200); |
|
530 |
view.show(); |
|
531 |
||
532 |
int size = 200; |
|
533 |
scene.setSceneRect(-size, -size, size * 2, size * 2); |
|
534 |
QCOMPARE(view.sceneRect(), scene.sceneRect()); |
|
535 |
||
536 |
QTest::qWait(25); |
|
537 |
||
538 |
QPointF topLeft = view.mapToScene(0, 0); |
|
539 |
||
540 |
for (int i = 0; i < 5; ++i) { |
|
541 |
size *= 2; |
|
542 |
scene.setSceneRect(-size, -size, size * 2, size * 2); |
|
543 |
||
544 |
QApplication::processEvents(); |
|
545 |
||
546 |
QCOMPARE(view.sceneRect(), scene.sceneRect()); |
|
547 |
QCOMPARE(view.mapToScene(0, 0), topLeft); |
|
548 |
view.setSceneRect(-size, -size, size * 2, size * 2); |
|
549 |
QCOMPARE(view.mapToScene(0, 0), topLeft); |
|
550 |
view.setSceneRect(QRectF()); |
|
551 |
} |
|
552 |
} |
|
553 |
||
554 |
void tst_QGraphicsView::setSceneRect() |
|
555 |
{ |
|
556 |
QRectF rect1(-100, -100, 200, 200); |
|
557 |
QRectF rect2(-300, -300, 150, 150); |
|
558 |
||
559 |
QGraphicsScene scene; |
|
560 |
QGraphicsView view(&scene); |
|
561 |
||
562 |
scene.setSceneRect(rect1); |
|
563 |
QCOMPARE(scene.sceneRect(), rect1); |
|
564 |
QCOMPARE(view.sceneRect(), rect1); |
|
565 |
||
566 |
scene.setSceneRect(rect2); |
|
567 |
QCOMPARE(scene.sceneRect(), rect2); |
|
568 |
QCOMPARE(view.sceneRect(), rect2); |
|
569 |
||
570 |
view.setSceneRect(rect1); |
|
571 |
QCOMPARE(scene.sceneRect(), rect2); |
|
572 |
QCOMPARE(view.sceneRect(), rect1); |
|
573 |
||
574 |
view.setSceneRect(rect2); |
|
575 |
QCOMPARE(scene.sceneRect(), rect2); |
|
576 |
QCOMPARE(view.sceneRect(), rect2); |
|
577 |
||
578 |
scene.setSceneRect(rect1); |
|
579 |
QCOMPARE(scene.sceneRect(), rect1); |
|
580 |
QCOMPARE(view.sceneRect(), rect2); |
|
581 |
||
582 |
// extreme transformations will max out the scrollbars' ranges. |
|
583 |
view.setSceneRect(-2000000, -2000000, 4000000, 4000000); |
|
584 |
view.scale(9000, 9000); |
|
585 |
QCOMPARE(view.horizontalScrollBar()->minimum(), INT_MIN); |
|
586 |
QCOMPARE(view.horizontalScrollBar()->maximum(), INT_MAX); |
|
587 |
QCOMPARE(view.verticalScrollBar()->minimum(), INT_MIN); |
|
588 |
QCOMPARE(view.verticalScrollBar()->maximum(), INT_MAX); |
|
589 |
} |
|
590 |
||
591 |
void tst_QGraphicsView::viewport() |
|
592 |
{ |
|
593 |
QGraphicsScene scene; |
|
594 |
scene.addText("GraphicsView"); |
|
595 |
||
596 |
QGraphicsView view(&scene); |
|
597 |
QVERIFY(view.viewport() != 0); |
|
598 |
||
599 |
view.show(); |
|
600 |
QTest::qWait(25); |
|
601 |
||
602 |
QPointer<QWidget> widget = new QWidget; |
|
603 |
view.setViewport(widget); |
|
604 |
QCOMPARE(view.viewport(), (QWidget *)widget); |
|
605 |
||
606 |
view.show(); |
|
607 |
QTest::qWait(25); |
|
608 |
||
609 |
view.setViewport(0); |
|
610 |
QVERIFY(widget.isNull()); |
|
611 |
QVERIFY(view.viewport() != 0); |
|
612 |
QVERIFY(view.viewport() != widget); |
|
613 |
||
614 |
view.show(); |
|
615 |
QTest::qWait(25); |
|
616 |
} |
|
617 |
||
618 |
void tst_QGraphicsView::dragMode_scrollHand() |
|
619 |
{ |
|
620 |
for (int j = 0; j < 2; ++j) { |
|
621 |
QGraphicsView view; |
|
622 |
QCOMPARE(view.dragMode(), QGraphicsView::NoDrag); |
|
623 |
||
624 |
view.setSceneRect(-1000, -1000, 2000, 2000); |
|
625 |
view.setFixedSize(100, 100); |
|
626 |
view.show(); |
|
627 |
||
628 |
QTest::qWaitForWindowShown(&view); |
|
629 |
QApplication::processEvents(); |
|
630 |
||
631 |
view.setInteractive(j ? false : true); |
|
632 |
||
633 |
QGraphicsScene scene; |
|
634 |
scene.addRect(QRectF(-100, -100, 5, 5)); |
|
635 |
scene.addRect(QRectF(95, -100, 5, 5)); |
|
636 |
scene.addRect(QRectF(95, 95, 5, 5)); |
|
637 |
QGraphicsItem *item = scene.addRect(QRectF(-100, 95, 5, 5)); |
|
638 |
item->setFlag(QGraphicsItem::ItemIsSelectable); |
|
639 |
item->setSelected(true); |
|
640 |
QVERIFY(item->isSelected()); |
|
641 |
QVERIFY(!view.scene()); |
|
642 |
||
643 |
view.setDragMode(QGraphicsView::ScrollHandDrag); |
|
644 |
||
645 |
for (int i = 0; i < 2; ++i) { |
|
646 |
// ScrollHandDrag |
|
647 |
#ifndef QT_NO_CURSOR |
|
648 |
Qt::CursorShape cursorShape = view.viewport()->cursor().shape(); |
|
649 |
#endif |
|
650 |
int horizontalScrollBarValue = view.horizontalScrollBar()->value(); |
|
651 |
int verticalScrollBarValue = view.verticalScrollBar()->value(); |
|
652 |
{ |
|
653 |
// Press |
|
654 |
QMouseEvent event(QEvent::MouseButtonPress, |
|
655 |
view.viewport()->rect().center(), |
|
656 |
Qt::LeftButton, Qt::LeftButton, 0); |
|
657 |
event.setAccepted(true); |
|
658 |
QApplication::sendEvent(view.viewport(), &event); |
|
659 |
QVERIFY(event.isAccepted()); |
|
660 |
} |
|
661 |
QApplication::processEvents(); |
|
662 |
||
663 |
QTRY_VERIFY(item->isSelected()); |
|
664 |
||
665 |
for (int k = 0; k < 4; ++k) { |
|
666 |
#ifndef QT_NO_CURSOR |
|
667 |
QCOMPARE(view.viewport()->cursor().shape(), Qt::ClosedHandCursor); |
|
668 |
#endif |
|
669 |
{ |
|
670 |
// Move |
|
671 |
QMouseEvent event(QEvent::MouseMove, |
|
672 |
view.viewport()->rect().center() + QPoint(10, 0), |
|
673 |
Qt::LeftButton, Qt::LeftButton, 0); |
|
674 |
event.setAccepted(true); |
|
675 |
QApplication::sendEvent(view.viewport(), &event); |
|
676 |
QVERIFY(event.isAccepted()); |
|
677 |
} |
|
678 |
QVERIFY(item->isSelected()); |
|
679 |
QCOMPARE(view.horizontalScrollBar()->value(), horizontalScrollBarValue - 10); |
|
680 |
QCOMPARE(view.verticalScrollBar()->value(), verticalScrollBarValue); |
|
681 |
{ |
|
682 |
// Move |
|
683 |
QMouseEvent event(QEvent::MouseMove, |
|
684 |
view.viewport()->rect().center() + QPoint(10, 10), |
|
685 |
Qt::LeftButton, Qt::LeftButton, 0); |
|
686 |
event.setAccepted(true); |
|
687 |
QApplication::sendEvent(view.viewport(), &event); |
|
688 |
QVERIFY(event.isAccepted()); |
|
689 |
} |
|
690 |
QVERIFY(item->isSelected()); |
|
691 |
QCOMPARE(view.horizontalScrollBar()->value(), horizontalScrollBarValue - 10); |
|
692 |
QCOMPARE(view.verticalScrollBar()->value(), verticalScrollBarValue - 10); |
|
693 |
} |
|
694 |
||
695 |
{ |
|
696 |
// Release |
|
697 |
QMouseEvent event(QEvent::MouseButtonRelease, |
|
698 |
view.viewport()->rect().center() + QPoint(10, 10), |
|
699 |
Qt::LeftButton, Qt::LeftButton, 0); |
|
700 |
event.setAccepted(true); |
|
701 |
QApplication::sendEvent(view.viewport(), &event); |
|
702 |
QVERIFY(event.isAccepted()); |
|
703 |
} |
|
704 |
QApplication::processEvents(); |
|
705 |
||
706 |
QTRY_VERIFY(item->isSelected()); |
|
707 |
QCOMPARE(view.horizontalScrollBar()->value(), horizontalScrollBarValue - 10); |
|
708 |
QCOMPARE(view.verticalScrollBar()->value(), verticalScrollBarValue - 10); |
|
709 |
#ifndef QT_NO_CURSOR |
|
710 |
QCOMPARE(view.viewport()->cursor().shape(), cursorShape); |
|
711 |
#endif |
|
712 |
||
713 |
// Check that items are not unselected because of a scroll hand drag. |
|
714 |
QVERIFY(item->isSelected()); |
|
715 |
||
716 |
// Check that a click will still unselect the item. |
|
717 |
{ |
|
718 |
// Press |
|
719 |
QMouseEvent event(QEvent::MouseButtonPress, |
|
720 |
view.viewport()->rect().center() + QPoint(10, 10), |
|
721 |
Qt::LeftButton, Qt::LeftButton, 0); |
|
722 |
QApplication::sendEvent(view.viewport(), &event); |
|
723 |
} |
|
724 |
{ |
|
725 |
// Release |
|
726 |
QMouseEvent event(QEvent::MouseButtonRelease, |
|
727 |
view.viewport()->rect().center() + QPoint(10, 10), |
|
728 |
Qt::LeftButton, Qt::LeftButton, 0); |
|
729 |
QApplication::sendEvent(view.viewport(), &event); |
|
730 |
} |
|
731 |
||
732 |
if (view.isInteractive()) { |
|
733 |
if (view.scene()) { |
|
734 |
QVERIFY(!item->isSelected()); |
|
735 |
item->setSelected(true); |
|
736 |
} else { |
|
737 |
QVERIFY(item->isSelected()); |
|
738 |
} |
|
739 |
} else { |
|
740 |
QVERIFY(item->isSelected()); |
|
741 |
} |
|
742 |
||
743 |
view.setScene(&scene); |
|
744 |
} |
|
745 |
} |
|
746 |
} |
|
747 |
||
748 |
void tst_QGraphicsView::dragMode_rubberBand() |
|
749 |
{ |
|
750 |
QGraphicsView view; |
|
751 |
QCOMPARE(view.dragMode(), QGraphicsView::NoDrag); |
|
752 |
||
753 |
view.setSceneRect(-1000, -1000, 2000, 2000); |
|
754 |
view.show(); |
|
755 |
||
756 |
QGraphicsScene scene; |
|
757 |
scene.addRect(QRectF(-100, -100, 25, 25))->setFlag(QGraphicsItem::ItemIsSelectable); |
|
758 |
scene.addRect(QRectF(75, -100, 25, 25))->setFlag(QGraphicsItem::ItemIsSelectable); |
|
759 |
scene.addRect(QRectF(75, 75, 25, 25))->setFlag(QGraphicsItem::ItemIsSelectable); |
|
760 |
scene.addRect(QRectF(-100, 75, 25, 25))->setFlag(QGraphicsItem::ItemIsSelectable); |
|
761 |
||
762 |
view.setDragMode(QGraphicsView::RubberBandDrag); |
|
763 |
||
764 |
QTest::qWaitForWindowShown(&view); |
|
765 |
QApplication::processEvents(); |
|
766 |
||
767 |
for (int i = 0; i < 2; ++i) { |
|
768 |
// RubberBandDrag |
|
769 |
#ifndef QT_NO_CURSOR |
|
770 |
Qt::CursorShape cursorShape = view.viewport()->cursor().shape(); |
|
771 |
#endif |
|
772 |
int horizontalScrollBarValue = view.horizontalScrollBar()->value(); |
|
773 |
int verticalScrollBarValue = view.verticalScrollBar()->value(); |
|
774 |
{ |
|
775 |
// Press |
|
776 |
QMouseEvent event(QEvent::MouseButtonPress, |
|
777 |
view.viewport()->rect().center(), |
|
778 |
Qt::LeftButton, Qt::LeftButton, 0); |
|
779 |
event.setAccepted(true); |
|
780 |
QApplication::sendEvent(view.viewport(), &event); |
|
781 |
QVERIFY(event.isAccepted()); |
|
782 |
} |
|
783 |
#ifndef QT_NO_CURSOR |
|
784 |
QCOMPARE(view.viewport()->cursor().shape(), cursorShape); |
|
785 |
#endif |
|
786 |
||
787 |
QApplication::processEvents(); |
|
788 |
||
789 |
{ |
|
790 |
// Move |
|
791 |
QMouseEvent event(QEvent::MouseMove, |
|
792 |
view.viewport()->rect().center() + QPoint(100, 0), |
|
793 |
Qt::LeftButton, Qt::LeftButton, 0); |
|
794 |
event.setAccepted(true); |
|
795 |
QApplication::sendEvent(view.viewport(), &event); |
|
796 |
QVERIFY(event.isAccepted()); |
|
797 |
} |
|
798 |
QCOMPARE(view.horizontalScrollBar()->value(), horizontalScrollBarValue); |
|
799 |
QCOMPARE(view.verticalScrollBar()->value(), verticalScrollBarValue); |
|
800 |
||
801 |
// We don't use QRubberBand as of 4.3; the band is drawn internally. |
|
802 |
QVERIFY(!qFindChild<QRubberBand *>(&view)); |
|
803 |
||
804 |
QTest::qWait(25); |
|
805 |
||
806 |
{ |
|
807 |
// Move |
|
808 |
QMouseEvent event(QEvent::MouseMove, |
|
809 |
view.viewport()->rect().center() + QPoint(100, 100), |
|
810 |
Qt::LeftButton, Qt::LeftButton, 0); |
|
811 |
event.setAccepted(true); |
|
812 |
QApplication::sendEvent(view.viewport(), &event); |
|
813 |
QVERIFY(event.isAccepted()); |
|
814 |
} |
|
815 |
QCOMPARE(view.horizontalScrollBar()->value(), horizontalScrollBarValue); |
|
816 |
QCOMPARE(view.verticalScrollBar()->value(), verticalScrollBarValue); |
|
817 |
||
818 |
QTest::qWait(25); |
|
819 |
||
820 |
{ |
|
821 |
// Release |
|
822 |
QMouseEvent event(QEvent::MouseButtonRelease, |
|
823 |
view.viewport()->rect().center() + QPoint(100, 100), |
|
824 |
Qt::LeftButton, Qt::LeftButton, 0); |
|
825 |
event.setAccepted(true); |
|
826 |
QApplication::sendEvent(view.viewport(), &event); |
|
827 |
QVERIFY(event.isAccepted()); |
|
828 |
} |
|
829 |
QCOMPARE(view.horizontalScrollBar()->value(), horizontalScrollBarValue); |
|
830 |
QCOMPARE(view.verticalScrollBar()->value(), verticalScrollBarValue); |
|
831 |
#ifndef QT_NO_CURSOR |
|
832 |
QCOMPARE(view.viewport()->cursor().shape(), cursorShape); |
|
833 |
#endif |
|
834 |
||
835 |
QTest::qWait(25); |
|
836 |
||
837 |
if (view.scene()) |
|
838 |
QCOMPARE(scene.selectedItems().size(), 1); |
|
839 |
||
840 |
view.setScene(&scene); |
|
841 |
view.centerOn(0, 0); |
|
842 |
} |
|
843 |
} |
|
844 |
||
845 |
void tst_QGraphicsView::rubberBandSelectionMode() |
|
846 |
{ |
|
847 |
QGraphicsScene scene; |
|
848 |
QGraphicsRectItem *rect = scene.addRect(QRectF(10, 10, 80, 80)); |
|
849 |
rect->setFlag(QGraphicsItem::ItemIsSelectable); |
|
850 |
||
851 |
QGraphicsView view(&scene); |
|
852 |
QCOMPARE(view.rubberBandSelectionMode(), Qt::IntersectsItemShape); |
|
853 |
view.setDragMode(QGraphicsView::RubberBandDrag); |
|
854 |
view.resize(120, 120); |
|
855 |
view.show(); |
|
856 |
||
857 |
// Disable mouse tracking to prevent the window system from sending mouse |
|
858 |
// move events to the viewport while we are synthesizing events. If |
|
859 |
// QGraphicsView gets a mouse move event with no buttons down, it'll |
|
860 |
// terminate the rubber band. |
|
861 |
view.viewport()->setMouseTracking(false); |
|
862 |
||
863 |
QCOMPARE(scene.selectedItems(), QList<QGraphicsItem *>()); |
|
864 |
sendMousePress(view.viewport(), QPoint(), Qt::LeftButton); |
|
865 |
sendMouseMove(view.viewport(), view.viewport()->rect().center(), |
|
866 |
Qt::LeftButton, Qt::LeftButton); |
|
867 |
QCOMPARE(scene.selectedItems(), QList<QGraphicsItem *>() << rect); |
|
868 |
sendMouseRelease(view.viewport(), QPoint(), Qt::LeftButton); |
|
869 |
||
870 |
view.setRubberBandSelectionMode(Qt::ContainsItemShape); |
|
871 |
QCOMPARE(view.rubberBandSelectionMode(), Qt::ContainsItemShape); |
|
872 |
sendMousePress(view.viewport(), QPoint(), Qt::LeftButton); |
|
873 |
QCOMPARE(scene.selectedItems(), QList<QGraphicsItem *>()); |
|
874 |
sendMouseMove(view.viewport(), view.viewport()->rect().center(), |
|
875 |
Qt::LeftButton, Qt::LeftButton); |
|
876 |
QCOMPARE(scene.selectedItems(), QList<QGraphicsItem *>()); |
|
877 |
sendMouseMove(view.viewport(), view.viewport()->rect().bottomRight(), |
|
878 |
Qt::LeftButton, Qt::LeftButton); |
|
879 |
QCOMPARE(scene.selectedItems(), QList<QGraphicsItem *>() << rect); |
|
880 |
} |
|
881 |
||
882 |
void tst_QGraphicsView::backgroundBrush() |
|
883 |
{ |
|
884 |
QGraphicsScene scene; |
|
885 |
QGraphicsView view(&scene); |
|
886 |
scene.setBackgroundBrush(Qt::blue); |
|
887 |
QCOMPARE(scene.backgroundBrush(), QBrush(Qt::blue)); |
|
888 |
||
889 |
view.show(); |
|
890 |
QTest::qWait(25); |
|
891 |
||
892 |
scene.setBackgroundBrush(QBrush()); |
|
893 |
QCOMPARE(scene.backgroundBrush(), QBrush()); |
|
894 |
QTest::qWait(25); |
|
895 |
||
896 |
QRadialGradient gradient(0, 0, 10); |
|
897 |
gradient.setSpread(QGradient::RepeatSpread); |
|
898 |
scene.setBackgroundBrush(gradient); |
|
899 |
||
900 |
QCOMPARE(scene.backgroundBrush(), QBrush(gradient)); |
|
901 |
QTest::qWait(25); |
|
902 |
} |
|
903 |
||
904 |
void tst_QGraphicsView::foregroundBrush() |
|
905 |
{ |
|
906 |
QGraphicsScene scene; |
|
907 |
QGraphicsView view(&scene); |
|
908 |
scene.setForegroundBrush(Qt::blue); |
|
909 |
QCOMPARE(scene.foregroundBrush(), QBrush(Qt::blue)); |
|
910 |
||
911 |
view.show(); |
|
912 |
QTest::qWait(25); |
|
913 |
||
914 |
scene.setForegroundBrush(QBrush()); |
|
915 |
QCOMPARE(scene.foregroundBrush(), QBrush()); |
|
916 |
QTest::qWait(25); |
|
917 |
||
918 |
QRadialGradient gradient(0, 0, 10); |
|
919 |
gradient.setSpread(QGradient::RepeatSpread); |
|
920 |
scene.setForegroundBrush(gradient); |
|
921 |
||
922 |
QCOMPARE(scene.foregroundBrush(), QBrush(gradient)); |
|
923 |
QTest::qWait(25); |
|
924 |
||
925 |
for (int i = 0; i < 50; ++i) { |
|
926 |
QRadialGradient gradient(view.rect().center() + QPoint(int(sin(i / 2.0) * 10), int(cos(i / 2.0) * 10)), 10); |
|
927 |
gradient.setColorAt(0, Qt::transparent); |
|
928 |
gradient.setColorAt(0.5, Qt::black); |
|
929 |
gradient.setColorAt(1, Qt::transparent); |
|
930 |
gradient.setSpread(QGradient::RepeatSpread); |
|
931 |
scene.setForegroundBrush(gradient); |
|
932 |
||
933 |
QRadialGradient gradient2(view.rect().center() + QPoint(int(sin(i / 1.7) * 10), int(cos(i / 1.7) * 10)), 10); |
|
934 |
gradient2.setColorAt(0, Qt::transparent); |
|
935 |
gradient2.setColorAt(0.5, Qt::black); |
|
936 |
gradient2.setColorAt(1, Qt::transparent); |
|
937 |
gradient2.setSpread(QGradient::RepeatSpread); |
|
938 |
scene.setBackgroundBrush(gradient2); |
|
939 |
||
940 |
QRadialGradient gradient3(view.rect().center() + QPoint(int(sin(i / 1.85) * 10), int(cos(i / 1.85) * 10)), 10); |
|
941 |
gradient3.setColorAt(0, Qt::transparent); |
|
942 |
gradient3.setColorAt(0.5, Qt::black); |
|
943 |
gradient3.setColorAt(1, Qt::transparent); |
|
944 |
gradient3.setSpread(QGradient::RepeatSpread); |
|
945 |
scene.setBackgroundBrush(gradient3); |
|
946 |
||
947 |
QApplication::processEvents(); |
|
948 |
} |
|
949 |
||
950 |
view.setSceneRect(-1000, -1000, 2000, 2000); |
|
951 |
for (int i = -500; i < 500; i += 10) { |
|
952 |
view.centerOn(i, 0); |
|
953 |
QApplication::processEvents(); |
|
954 |
QApplication::processEvents(); |
|
955 |
} |
|
956 |
for (int i = -500; i < 500; i += 10) { |
|
957 |
view.centerOn(0, i); |
|
958 |
QApplication::processEvents(); |
|
959 |
QApplication::processEvents(); |
|
960 |
} |
|
961 |
} |
|
962 |
||
963 |
void tst_QGraphicsView::matrix() |
|
964 |
{ |
|
965 |
{ |
|
966 |
QGraphicsScene scene; |
|
967 |
QGraphicsView view(&scene); |
|
968 |
view.show(); |
|
969 |
||
970 |
// Show rendering of background with no scene |
|
971 |
for (int i = 0; i < 50; ++i) { |
|
972 |
view.rotate(5); |
|
973 |
QRadialGradient gradient(view.rect().center() + QPoint(int(sin(i / 2.0) * 10), int(cos(i / 2.0) * 10)), 10); |
|
974 |
gradient.setColorAt(0, Qt::transparent); |
|
975 |
gradient.setColorAt(0.5, Qt::black); |
|
976 |
gradient.setColorAt(1, Qt::transparent); |
|
977 |
gradient.setSpread(QGradient::RepeatSpread); |
|
978 |
scene.setForegroundBrush(gradient); |
|
979 |
QRadialGradient gradient2(view.rect().center() + QPoint(int(sin(i / 1.7) * 10), int(cos(i / 1.7) * 10)), 10); |
|
980 |
gradient2.setColorAt(0, Qt::transparent); |
|
981 |
gradient2.setColorAt(0.5, Qt::black); |
|
982 |
gradient2.setColorAt(1, Qt::transparent); |
|
983 |
gradient2.setSpread(QGradient::RepeatSpread); |
|
984 |
scene.setBackgroundBrush(gradient2); |
|
985 |
QApplication::processEvents(); |
|
986 |
QApplication::processEvents(); |
|
987 |
} |
|
988 |
} |
|
989 |
||
990 |
// Test transformation extremes, see if they cause crashes |
|
991 |
{ |
|
992 |
QGraphicsScene scene; |
|
993 |
scene.addText("GraphicsView rotated clockwise"); |
|
994 |
||
995 |
QGraphicsView view(&scene); |
|
996 |
view.show(); |
|
997 |
for (int i = 0; i < 160; ++i) { |
|
998 |
view.rotate(18); |
|
999 |
QApplication::processEvents(); |
|
1000 |
QApplication::processEvents(); |
|
1001 |
} |
|
1002 |
/* |
|
1003 |
// These cause a crash |
|
1004 |
for (int i = 0; i < 40; ++i) { |
|
1005 |
view.shear(1.2, 1.2); |
|
1006 |
QTest::qWait(20); |
|
1007 |
} |
|
1008 |
for (int i = 0; i < 40; ++i) { |
|
1009 |
view.shear(-1.2, -1.2); |
|
1010 |
QTest::qWait(20); |
|
1011 |
} |
|
1012 |
*/ |
|
1013 |
for (int i = 0; i < 20; ++i) { |
|
1014 |
view.scale(1.2, 1.2); |
|
1015 |
QApplication::processEvents(); |
|
1016 |
QApplication::processEvents(); |
|
1017 |
} |
|
1018 |
for (int i = 0; i < 20; ++i) { |
|
1019 |
view.scale(0.6, 0.6); |
|
1020 |
QApplication::processEvents(); |
|
1021 |
QApplication::processEvents(); |
|
1022 |
} |
|
1023 |
} |
|
1024 |
} |
|
1025 |
||
1026 |
void tst_QGraphicsView::matrix_convenience() |
|
1027 |
{ |
|
1028 |
QGraphicsView view; |
|
1029 |
QCOMPARE(view.matrix(), QMatrix()); |
|
1030 |
||
1031 |
// Check the convenience functions |
|
1032 |
view.rotate(90); |
|
1033 |
QCOMPARE(view.matrix(), QMatrix().rotate(90)); |
|
1034 |
view.scale(2, 2); |
|
1035 |
QCOMPARE(view.matrix(), QMatrix().scale(2, 2) * QMatrix().rotate(90)); |
|
1036 |
view.shear(1.2, 1.2); |
|
1037 |
QCOMPARE(view.matrix(), QMatrix().shear(1.2, 1.2) * QMatrix().scale(2, 2) * QMatrix().rotate(90)); |
|
1038 |
view.translate(1, 1); |
|
1039 |
QCOMPARE(view.matrix(), QMatrix().translate(1, 1) * QMatrix().shear(1.2, 1.2) * QMatrix().scale(2, 2) * QMatrix().rotate(90)); |
|
1040 |
} |
|
1041 |
||
1042 |
void tst_QGraphicsView::matrix_combine() |
|
1043 |
{ |
|
1044 |
// Check matrix combining |
|
1045 |
QGraphicsView view; |
|
1046 |
QCOMPARE(view.matrix(), QMatrix()); |
|
1047 |
view.setMatrix(QMatrix().rotate(90), true); |
|
1048 |
view.setMatrix(QMatrix().rotate(90), true); |
|
1049 |
view.setMatrix(QMatrix().rotate(90), true); |
|
1050 |
view.setMatrix(QMatrix().rotate(90), true); |
|
1051 |
QCOMPARE(view.matrix(), QMatrix()); |
|
1052 |
||
1053 |
view.resetMatrix(); |
|
1054 |
QCOMPARE(view.matrix(), QMatrix()); |
|
1055 |
view.setMatrix(QMatrix().rotate(90), false); |
|
1056 |
view.setMatrix(QMatrix().rotate(90), false); |
|
1057 |
view.setMatrix(QMatrix().rotate(90), false); |
|
1058 |
view.setMatrix(QMatrix().rotate(90), false); |
|
1059 |
QCOMPARE(view.matrix(), QMatrix().rotate(90)); |
|
1060 |
} |
|
1061 |
||
1062 |
void tst_QGraphicsView::centerOnPoint() |
|
1063 |
{ |
|
1064 |
QGraphicsScene scene; |
|
1065 |
scene.addEllipse(QRectF(-100, -100, 50, 50)); |
|
1066 |
scene.addEllipse(QRectF(50, -100, 50, 50)); |
|
1067 |
scene.addEllipse(QRectF(-100, 50, 50, 50)); |
|
1068 |
scene.addEllipse(QRectF(50, 50, 50, 50)); |
|
1069 |
||
1070 |
QGraphicsView view(&scene); |
|
1071 |
view.setSceneRect(-400, -400, 800, 800); |
|
1072 |
view.setFixedSize(100, 100); |
|
1073 |
view.show(); |
|
1074 |
||
1075 |
int tolerance = 5; |
|
1076 |
||
1077 |
for (int i = 0; i < 3; ++i) { |
|
1078 |
for (int y = -100; y < 100; y += 23) { |
|
1079 |
for (int x = -100; x < 100; x += 23) { |
|
1080 |
view.centerOn(x, y); |
|
1081 |
QPoint viewCenter = view.mapToScene(view.viewport()->rect().center()).toPoint(); |
|
1082 |
||
1083 |
// Fuzzy compare |
|
1084 |
if (viewCenter.x() < x - tolerance || viewCenter.x() > x + tolerance |
|
1085 |
|| viewCenter.y() < y - tolerance || viewCenter.y() > y + tolerance) { |
|
1086 |
QString error = QString("Compared values are not the same\n\tActual: (%1, %2)\n\tExpected: (%3, %4)") |
|
1087 |
.arg(viewCenter.x()).arg(viewCenter.y()).arg(x).arg(y); |
|
1088 |
QFAIL(qPrintable(error)); |
|
1089 |
} |
|
1090 |
||
1091 |
QApplication::processEvents(); |
|
1092 |
} |
|
1093 |
} |
|
1094 |
||
1095 |
view.rotate(13); |
|
1096 |
view.scale(1.5, 1.5); |
|
1097 |
view.shear(1.25, 1.25); |
|
1098 |
} |
|
1099 |
} |
|
1100 |
||
1101 |
void tst_QGraphicsView::centerOnItem() |
|
1102 |
{ |
|
1103 |
QGraphicsScene scene; |
|
1104 |
QGraphicsItem *items[4]; |
|
1105 |
items[0] = scene.addEllipse(QRectF(-25, -25, 50, 50)); |
|
1106 |
items[1] = scene.addEllipse(QRectF(-25, -25, 50, 50)); |
|
1107 |
items[2] = scene.addEllipse(QRectF(-25, -25, 50, 50)); |
|
1108 |
items[3] = scene.addEllipse(QRectF(-25, -25, 50, 50)); |
|
1109 |
items[0]->setPos(-100, -100); |
|
1110 |
items[1]->setPos(100, -100); |
|
1111 |
items[2]->setPos(-100, 100); |
|
1112 |
items[3]->setPos(100, 100); |
|
1113 |
||
1114 |
QGraphicsView view(&scene); |
|
1115 |
view.setSceneRect(-1000, -1000, 2000, 2000); |
|
1116 |
view.show(); |
|
1117 |
QTest::qWaitForWindowShown(&view); |
|
1118 |
int tolerance = 7; |
|
1119 |
||
1120 |
for (int x = 0; x < 3; ++x) { |
|
1121 |
for (int i = 0; i < 4; ++i) { |
|
1122 |
QApplication::processEvents(); |
|
1123 |
view.centerOn(items[i]); |
|
1124 |
||
1125 |
QPoint viewCenter = view.mapToScene(view.viewport()->rect().center()).toPoint(); |
|
1126 |
qreal x = items[i]->pos().x(); |
|
1127 |
qreal y = items[i]->pos().y(); |
|
1128 |
||
1129 |
// Fuzzy compare |
|
1130 |
if (viewCenter.x() < x - tolerance || viewCenter.x() > x + tolerance |
|
1131 |
|| viewCenter.y() < y - tolerance || viewCenter.y() > y + tolerance) { |
|
1132 |
QString error = QString("Compared values are not the same\n\tActual: (%1, %2)\n\tExpected: (%3, %4)") |
|
1133 |
.arg(viewCenter.x()).arg(viewCenter.y()).arg(x).arg(y); |
|
1134 |
QFAIL(qPrintable(error)); |
|
1135 |
} |
|
1136 |
||
1137 |
QApplication::processEvents(); |
|
1138 |
} |
|
1139 |
||
1140 |
view.rotate(13); |
|
1141 |
view.scale(1.5, 1.5); |
|
1142 |
view.shear(1.25, 1.25); |
|
1143 |
} |
|
1144 |
} |
|
1145 |
||
1146 |
void tst_QGraphicsView::ensureVisibleRect() |
|
1147 |
{ |
|
1148 |
QGraphicsScene scene; |
|
1149 |
QGraphicsItem *items[4]; |
|
1150 |
items[0] = scene.addEllipse(QRectF(-25, -25, 50, 50), QPen(Qt::black), QBrush(Qt::green)); |
|
1151 |
items[1] = scene.addEllipse(QRectF(-25, -25, 50, 50), QPen(Qt::black), QBrush(Qt::red)); |
|
1152 |
items[2] = scene.addEllipse(QRectF(-25, -25, 50, 50), QPen(Qt::black), QBrush(Qt::blue)); |
|
1153 |
items[3] = scene.addEllipse(QRectF(-25, -25, 50, 50), QPen(Qt::black), QBrush(Qt::yellow)); |
|
1154 |
scene.addLine(QLineF(0, -100, 0, 100), QPen(Qt::blue, 2)); |
|
1155 |
scene.addLine(QLineF(-100, 0, 100, 0), QPen(Qt::blue, 2)); |
|
1156 |
items[0]->setPos(-100, -100); |
|
1157 |
items[1]->setPos(100, -100); |
|
1158 |
items[2]->setPos(-100, 100); |
|
1159 |
items[3]->setPos(100, 100); |
|
1160 |
||
1161 |
QGraphicsItem *icon = scene.addEllipse(QRectF(-10, -10, 20, 20), QPen(Qt::black), QBrush(Qt::gray)); |
|
1162 |
||
1163 |
QGraphicsView view(&scene); |
|
1164 |
view.setSceneRect(-500, -500, 1000, 1000); |
|
1165 |
view.setFixedSize(250, 250); |
|
1166 |
view.show(); |
|
1167 |
QTest::qWaitForWindowShown(&view); |
|
1168 |
||
1169 |
for (int y = -100; y < 100; y += 25) { |
|
1170 |
for (int x = -100; x < 100; x += 13) { |
|
1171 |
||
1172 |
icon->setPos(x, y); |
|
1173 |
||
1174 |
switch (x & 3) { |
|
1175 |
case 0: |
|
1176 |
view.centerOn(-500, -500); |
|
1177 |
break; |
|
1178 |
case 1: |
|
1179 |
view.centerOn(500, -500); |
|
1180 |
break; |
|
1181 |
case 2: |
|
1182 |
view.centerOn(-500, 500); |
|
1183 |
break; |
|
1184 |
case 3: |
|
1185 |
default: |
|
1186 |
view.centerOn(500, 500); |
|
1187 |
break; |
|
1188 |
} |
|
1189 |
||
1190 |
QVERIFY(!view.viewport()->rect().contains(view.mapFromScene(x, y))); |
|
1191 |
||
1192 |
for (int margin = 10; margin < 60; margin += 15) { |
|
1193 |
view.ensureVisible(x, y, 0, 0, margin, margin); |
|
1194 |
||
1195 |
QRect viewRect = view.viewport()->rect(); |
|
1196 |
QPoint viewPoint = view.mapFromScene(x, y); |
|
1197 |
||
1198 |
QVERIFY(viewRect.contains(viewPoint)); |
|
1199 |
QVERIFY(qAbs(viewPoint.x() - viewRect.left()) >= margin -1); |
|
1200 |
QVERIFY(qAbs(viewPoint.x() - viewRect.right()) >= margin -1); |
|
1201 |
QVERIFY(qAbs(viewPoint.y() - viewRect.top()) >= margin -1); |
|
1202 |
QVERIFY(qAbs(viewPoint.y() - viewRect.bottom()) >= margin -1); |
|
1203 |
||
1204 |
QApplication::processEvents(); |
|
1205 |
} |
|
1206 |
} |
|
1207 |
view.rotate(5); |
|
1208 |
view.scale(1.05, 1.05); |
|
1209 |
view.translate(30, -30); |
|
1210 |
} |
|
1211 |
} |
|
1212 |
||
1213 |
void tst_QGraphicsView::fitInView() |
|
1214 |
{ |
|
1215 |
QGraphicsScene scene; |
|
1216 |
QGraphicsItem *items[4]; |
|
1217 |
items[0] = scene.addEllipse(QRectF(-25, -25, 100, 20), QPen(Qt::black), QBrush(Qt::green)); |
|
1218 |
items[1] = scene.addEllipse(QRectF(-25, -25, 20, 100), QPen(Qt::black), QBrush(Qt::red)); |
|
1219 |
items[2] = scene.addEllipse(QRectF(-25, -25, 50, 50), QPen(Qt::black), QBrush(Qt::blue)); |
|
1220 |
items[3] = scene.addEllipse(QRectF(-25, -25, 50, 50), QPen(Qt::black), QBrush(Qt::yellow)); |
|
1221 |
scene.addLine(QLineF(0, -100, 0, 100), QPen(Qt::blue, 2)); |
|
1222 |
scene.addLine(QLineF(-100, 0, 100, 0), QPen(Qt::blue, 2)); |
|
1223 |
items[0]->setPos(-100, -100); |
|
1224 |
items[1]->setPos(100, -100); |
|
1225 |
items[2]->setPos(-100, 100); |
|
1226 |
items[3]->setPos(100, 100); |
|
1227 |
||
1228 |
items[0]->rotate(30); |
|
1229 |
items[1]->rotate(-30); |
|
1230 |
||
1231 |
#if defined(Q_OS_WINCE) |
|
1232 |
//Is the standard scrollbar size |
|
1233 |
int scrollbarSize = qApp->style()->pixelMetric(QStyle::PM_ScrollBarExtent) - 13; |
|
1234 |
#endif |
|
1235 |
||
1236 |
QGraphicsView view(&scene); |
|
1237 |
view.setSceneRect(-400, -400, 800, 800); |
|
1238 |
||
1239 |
#if defined(Q_OS_WINCE) |
|
1240 |
//We need to take in account the scrollbar size for the WindowsMobilStyle |
|
1241 |
view.setFixedSize(400 + scrollbarSize, 200 + scrollbarSize); |
|
1242 |
#else |
|
1243 |
view.setFixedSize(400, 200); |
|
1244 |
#endif |
|
1245 |
||
1246 |
view.show(); |
|
1247 |
view.fitInView(scene.itemsBoundingRect(), Qt::IgnoreAspectRatio); |
|
1248 |
qApp->processEvents(); |
|
1249 |
||
1250 |
// Sampled coordinates. |
|
1251 |
QVERIFY(!view.itemAt(45, 41)); |
|
1252 |
QVERIFY(!view.itemAt(297, 44)); |
|
1253 |
QVERIFY(!view.itemAt(359, 143)); |
|
1254 |
QCOMPARE(view.itemAt(79, 22), items[0]); |
|
1255 |
QCOMPARE(view.itemAt(329, 41), items[1]); |
|
1256 |
QCOMPARE(view.itemAt(38, 158), items[2]); |
|
1257 |
QCOMPARE(view.itemAt(332, 160), items[3]); |
|
1258 |
||
1259 |
view.fitInView(items[0], Qt::IgnoreAspectRatio); |
|
1260 |
qApp->processEvents(); |
|
1261 |
||
1262 |
QCOMPARE(view.itemAt(19, 13), items[0]); |
|
1263 |
QCOMPARE(view.itemAt(91, 47), items[0]); |
|
1264 |
QCOMPARE(view.itemAt(202, 94), items[0]); |
|
1265 |
QCOMPARE(view.itemAt(344, 161), items[0]); |
|
1266 |
QVERIFY(!view.itemAt(236, 54)); |
|
1267 |
QVERIFY(!view.itemAt(144, 11)); |
|
1268 |
QVERIFY(!view.itemAt(29, 69)); |
|
1269 |
QVERIFY(!view.itemAt(251, 167)); |
|
1270 |
||
1271 |
view.fitInView(items[0], Qt::KeepAspectRatio); |
|
1272 |
qApp->processEvents(); |
|
1273 |
||
1274 |
QCOMPARE(view.itemAt(325, 170), items[0]); |
|
1275 |
QCOMPARE(view.itemAt(206, 74), items[0]); |
|
1276 |
QCOMPARE(view.itemAt(190, 115), items[0]); |
|
1277 |
QCOMPARE(view.itemAt(55, 14), items[0]); |
|
1278 |
QVERIFY(!view.itemAt(109, 4)); |
|
1279 |
QVERIFY(!view.itemAt(244, 68)); |
|
1280 |
QVERIFY(!view.itemAt(310, 125)); |
|
1281 |
QVERIFY(!view.itemAt(261, 168)); |
|
1282 |
||
1283 |
view.fitInView(items[0], Qt::KeepAspectRatioByExpanding); |
|
1284 |
qApp->processEvents(); |
|
1285 |
||
1286 |
QCOMPARE(view.itemAt(18, 10), items[0]); |
|
1287 |
QCOMPARE(view.itemAt(95, 4), items[0]); |
|
1288 |
QCOMPARE(view.itemAt(279, 175), items[0]); |
|
1289 |
QCOMPARE(view.itemAt(359, 170), items[0]); |
|
1290 |
QVERIFY(!view.itemAt(370, 166)); |
|
1291 |
QVERIFY(!view.itemAt(136, 7)); |
|
1292 |
QVERIFY(!view.itemAt(31, 44)); |
|
1293 |
QVERIFY(!view.itemAt(203, 153)); |
|
1294 |
} |
|
1295 |
||
1296 |
void tst_QGraphicsView::itemsAtPoint() |
|
1297 |
{ |
|
1298 |
QGraphicsScene scene; |
|
1299 |
scene.addRect(QRectF(-10, -10, 20, 20))->setZValue(1); |
|
1300 |
scene.addRect(QRectF(-10, -10, 20, 20))->setZValue(0); |
|
1301 |
scene.addRect(QRectF(-10, -10, 20, 20))->setZValue(2); |
|
1302 |
scene.addRect(QRectF(-10, -10, 20, 20))->setZValue(-1); |
|
1303 |
scene.addRect(QRectF(-10, -10, 20, 20))->setZValue(3); |
|
1304 |
||
1305 |
QGraphicsView view; |
|
1306 |
QVERIFY(view.items(0, 0).isEmpty()); |
|
1307 |
||
1308 |
view.setScene(&scene); |
|
1309 |
view.setSceneRect(-10000, -10000, 20000, 20000); |
|
1310 |
view.show(); |
|
1311 |
||
1312 |
QList<QGraphicsItem *> items = view.items(view.viewport()->rect().center()); |
|
1313 |
QCOMPARE(items.size(), 5); |
|
1314 |
QCOMPARE(items.takeFirst()->zValue(), qreal(3)); |
|
1315 |
QCOMPARE(items.takeFirst()->zValue(), qreal(2)); |
|
1316 |
QCOMPARE(items.takeFirst()->zValue(), qreal(1)); |
|
1317 |
QCOMPARE(items.takeFirst()->zValue(), qreal(0)); |
|
1318 |
QCOMPARE(items.takeFirst()->zValue(), qreal(-1)); |
|
1319 |
} |
|
1320 |
||
1321 |
void tst_QGraphicsView::itemsInRect() |
|
1322 |
{ |
|
1323 |
QGraphicsScene scene; |
|
1324 |
scene.addRect(QRectF(-30, -10, 20, 20))->setZValue(1); |
|
1325 |
scene.addRect(QRectF(-30, -10, 20, 20))->setZValue(0); |
|
1326 |
scene.addRect(QRectF(-30, -10, 20, 20))->setZValue(2); |
|
1327 |
scene.addRect(QRectF(-30, -10, 20, 20))->setZValue(-1); |
|
1328 |
scene.addRect(QRectF(-30, -10, 20, 20))->setZValue(3); |
|
1329 |
||
1330 |
scene.addRect(QRectF(30, -10, 20, 20))->setZValue(5); |
|
1331 |
scene.addRect(QRectF(30, -10, 20, 20))->setZValue(4); |
|
1332 |
scene.addRect(QRectF(30, -10, 20, 20))->setZValue(6); |
|
1333 |
scene.addRect(QRectF(30, -10, 20, 20))->setZValue(3); |
|
1334 |
scene.addRect(QRectF(30, -10, 20, 20))->setZValue(7); |
|
1335 |
||
1336 |
QGraphicsView view; |
|
1337 |
QVERIFY(view.items(QRect(-100, -100, 200, 200)).isEmpty()); |
|
1338 |
view.setScene(&scene); |
|
1339 |
view.setSceneRect(-10000, -10000, 20000, 20000); |
|
1340 |
view.show(); |
|
1341 |
||
1342 |
QPoint centerPoint = view.viewport()->rect().center(); |
|
1343 |
QRect leftRect = view.mapFromScene(-30, -10, 20, 20).boundingRect(); |
|
1344 |
QRect rightRect = view.mapFromScene(30, -10, 20, 20).boundingRect(); |
|
1345 |
||
1346 |
QList<QGraphicsItem *> items = view.items(leftRect); |
|
1347 |
QCOMPARE(items.size(), 5); |
|
1348 |
QCOMPARE(items.takeFirst()->zValue(), qreal(3)); |
|
1349 |
QCOMPARE(items.takeFirst()->zValue(), qreal(2)); |
|
1350 |
QCOMPARE(items.takeFirst()->zValue(), qreal(1)); |
|
1351 |
QCOMPARE(items.takeFirst()->zValue(), qreal(0)); |
|
1352 |
QCOMPARE(items.takeFirst()->zValue(), qreal(-1)); |
|
1353 |
||
1354 |
items = view.items(rightRect); |
|
1355 |
QCOMPARE(items.size(), 5); |
|
1356 |
QCOMPARE(items.takeFirst()->zValue(), qreal(7)); |
|
1357 |
QCOMPARE(items.takeFirst()->zValue(), qreal(6)); |
|
1358 |
QCOMPARE(items.takeFirst()->zValue(), qreal(5)); |
|
1359 |
QCOMPARE(items.takeFirst()->zValue(), qreal(4)); |
|
1360 |
QCOMPARE(items.takeFirst()->zValue(), qreal(3)); |
|
1361 |
} |
|
1362 |
||
1363 |
class CountPaintItem : public QGraphicsRectItem |
|
1364 |
{ |
|
1365 |
public: |
|
1366 |
int numPaints; |
|
1367 |
||
1368 |
CountPaintItem(const QRectF &rect) |
|
1369 |
: QGraphicsRectItem(rect), numPaints(0) |
|
1370 |
{ } |
|
1371 |
||
1372 |
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0) |
|
1373 |
{ |
|
1374 |
++numPaints; |
|
1375 |
QGraphicsRectItem::paint(painter, option, widget); |
|
1376 |
} |
|
1377 |
}; |
|
1378 |
||
1379 |
void tst_QGraphicsView::itemsInRect_cosmeticAdjust_data() |
|
1380 |
{ |
|
1381 |
QTest::addColumn<QRect>("updateRect"); |
|
1382 |
QTest::addColumn<int>("numPaints"); |
|
1383 |
||
1384 |
QTest::newRow("nil") << QRect() << 1; |
|
1385 |
QTest::newRow("0, 0, 300, 100") << QRect(0, 0, 300, 100) << 1; |
|
1386 |
QTest::newRow("0, 0, 100, 300") << QRect(0, 0, 100, 300) << 1; |
|
1387 |
QTest::newRow("200, 0, 100, 300") << QRect(200, 0, 100, 300) << 1; |
|
1388 |
QTest::newRow("0, 200, 300, 100") << QRect(0, 200, 300, 100) << 1; |
|
1389 |
QTest::newRow("0, 0, 300, 99") << QRect(0, 0, 300, 99) << 0; |
|
1390 |
QTest::newRow("0, 0, 99, 300") << QRect(0, 0, 99, 300) << 0; |
|
1391 |
QTest::newRow("201, 0, 99, 300") << QRect(201, 0, 99, 300) << 0; |
|
1392 |
QTest::newRow("0, 201, 300, 99") << QRect(0, 201, 300, 99) << 0; |
|
1393 |
} |
|
1394 |
||
1395 |
void tst_QGraphicsView::itemsInRect_cosmeticAdjust() |
|
1396 |
{ |
|
1397 |
QFETCH(QRect, updateRect); |
|
1398 |
QFETCH(int, numPaints); |
|
1399 |
||
1400 |
QGraphicsScene scene(-100, -100, 200, 200); |
|
1401 |
CountPaintItem *rect = new CountPaintItem(QRectF(-50, -50, 100, 100)); |
|
1402 |
scene.addItem(rect); |
|
1403 |
||
1404 |
QGraphicsView view(&scene); |
|
1405 |
view.setFrameStyle(0); |
|
1406 |
view.resize(300, 300); |
|
1407 |
view.show(); |
|
1408 |
QTest::qWaitForWindowShown(&view) ; |
|
1409 |
QTRY_VERIFY(rect->numPaints > 0); |
|
1410 |
||
1411 |
rect->numPaints = 0; |
|
1412 |
if (updateRect.isNull()) |
|
1413 |
view.viewport()->update(); |
|
1414 |
else |
|
1415 |
view.viewport()->update(updateRect); |
|
1416 |
qApp->processEvents(); |
|
1417 |
QTRY_COMPARE(rect->numPaints, numPaints); |
|
1418 |
} |
|
1419 |
||
1420 |
void tst_QGraphicsView::itemsInPoly() |
|
1421 |
{ |
|
1422 |
QGraphicsScene scene; |
|
1423 |
scene.addRect(QRectF(-30, -10, 20, 20))->setZValue(1); |
|
1424 |
scene.addRect(QRectF(-30, -10, 20, 20))->setZValue(0); |
|
1425 |
scene.addRect(QRectF(-30, -10, 20, 20))->setZValue(2); |
|
1426 |
scene.addRect(QRectF(-30, -10, 20, 20))->setZValue(-1); |
|
1427 |
scene.addRect(QRectF(-30, -10, 20, 20))->setZValue(3); |
|
1428 |
||
1429 |
scene.addRect(QRectF(30, -10, 20, 20))->setZValue(5); |
|
1430 |
scene.addRect(QRectF(30, -10, 20, 20))->setZValue(4); |
|
1431 |
scene.addRect(QRectF(30, -10, 20, 20))->setZValue(6); |
|
1432 |
scene.addRect(QRectF(30, -10, 20, 20))->setZValue(3); |
|
1433 |
scene.addRect(QRectF(30, -10, 20, 20))->setZValue(7); |
|
1434 |
||
1435 |
QGraphicsView view; |
|
1436 |
QVERIFY(view.items(QPolygon()).isEmpty()); |
|
1437 |
view.setScene(&scene); |
|
1438 |
view.setSceneRect(-10000, -10000, 20000, 20000); |
|
1439 |
view.show(); |
|
1440 |
||
1441 |
QPoint centerPoint = view.viewport()->rect().center(); |
|
1442 |
QPolygon leftPoly = view.mapFromScene(QRectF(-30, -10, 20, 20)); |
|
1443 |
QPolygon rightPoly = view.mapFromScene(QRectF(30, -10, 20, 20)); |
|
1444 |
||
1445 |
QList<QGraphicsItem *> items = view.items(leftPoly); |
|
1446 |
QCOMPARE(items.size(), 5); |
|
1447 |
QCOMPARE(items.takeFirst()->zValue(), qreal(3)); |
|
1448 |
QCOMPARE(items.takeFirst()->zValue(), qreal(2)); |
|
1449 |
QCOMPARE(items.takeFirst()->zValue(), qreal(1)); |
|
1450 |
QCOMPARE(items.takeFirst()->zValue(), qreal(0)); |
|
1451 |
QCOMPARE(items.takeFirst()->zValue(), qreal(-1)); |
|
1452 |
||
1453 |
items = view.items(rightPoly); |
|
1454 |
QCOMPARE(items.size(), 5); |
|
1455 |
QCOMPARE(items.takeFirst()->zValue(), qreal(7)); |
|
1456 |
QCOMPARE(items.takeFirst()->zValue(), qreal(6)); |
|
1457 |
QCOMPARE(items.takeFirst()->zValue(), qreal(5)); |
|
1458 |
QCOMPARE(items.takeFirst()->zValue(), qreal(4)); |
|
1459 |
QCOMPARE(items.takeFirst()->zValue(), qreal(3)); |
|
1460 |
} |
|
1461 |
||
1462 |
void tst_QGraphicsView::itemsInPath() |
|
1463 |
{ |
|
1464 |
QGraphicsScene scene; |
|
1465 |
scene.addRect(QRectF(-30, -10, 20, 20))->setZValue(1); |
|
1466 |
scene.addRect(QRectF(-30, -10, 20, 20))->setZValue(0); |
|
1467 |
scene.addRect(QRectF(-30, -10, 20, 20))->setZValue(2); |
|
1468 |
scene.addRect(QRectF(-30, -10, 20, 20))->setZValue(-1); |
|
1469 |
scene.addRect(QRectF(-30, -10, 20, 20))->setZValue(3); |
|
1470 |
||
1471 |
scene.addRect(QRectF(30, -10, 20, 20))->setZValue(5); |
|
1472 |
scene.addRect(QRectF(30, -10, 20, 20))->setZValue(4); |
|
1473 |
scene.addRect(QRectF(30, -10, 20, 20))->setZValue(6); |
|
1474 |
scene.addRect(QRectF(30, -10, 20, 20))->setZValue(3); |
|
1475 |
scene.addRect(QRectF(30, -10, 20, 20))->setZValue(7); |
|
1476 |
||
1477 |
QGraphicsView view; |
|
1478 |
QVERIFY(view.items(QPainterPath()).isEmpty()); |
|
1479 |
view.setScene(&scene); |
|
1480 |
view.translate(100, 400); |
|
1481 |
view.rotate(22.3); |
|
1482 |
view.setSceneRect(-10000, -10000, 20000, 20000); |
|
1483 |
view.show(); |
|
1484 |
||
1485 |
QPoint centerPoint = view.viewport()->rect().center(); |
|
1486 |
QPainterPath leftPath; |
|
1487 |
leftPath.addEllipse(QRect(view.mapFromScene(-30, -10), QSize(20, 20))); |
|
1488 |
||
1489 |
QPainterPath rightPath; |
|
1490 |
rightPath.addEllipse(QRect(view.mapFromScene(30, -10), QSize(20, 20))); |
|
1491 |
||
1492 |
QList<QGraphicsItem *> items = view.items(leftPath); |
|
1493 |
||
1494 |
QCOMPARE(items.size(), 5); |
|
1495 |
QCOMPARE(items.takeFirst()->zValue(), qreal(3)); |
|
1496 |
QCOMPARE(items.takeFirst()->zValue(), qreal(2)); |
|
1497 |
QCOMPARE(items.takeFirst()->zValue(), qreal(1)); |
|
1498 |
QCOMPARE(items.takeFirst()->zValue(), qreal(0)); |
|
1499 |
QCOMPARE(items.takeFirst()->zValue(), qreal(-1)); |
|
1500 |
||
1501 |
items = view.items(rightPath); |
|
1502 |
QCOMPARE(items.size(), 5); |
|
1503 |
QCOMPARE(items.takeFirst()->zValue(), qreal(7)); |
|
1504 |
QCOMPARE(items.takeFirst()->zValue(), qreal(6)); |
|
1505 |
QCOMPARE(items.takeFirst()->zValue(), qreal(5)); |
|
1506 |
QCOMPARE(items.takeFirst()->zValue(), qreal(4)); |
|
1507 |
QCOMPARE(items.takeFirst()->zValue(), qreal(3)); |
|
1508 |
} |
|
1509 |
||
1510 |
void tst_QGraphicsView::itemAt() |
|
1511 |
{ |
|
1512 |
QGraphicsScene scene; |
|
1513 |
scene.addRect(QRectF(-10, -10, 20, 20))->setZValue(1); |
|
1514 |
scene.addRect(QRectF(-10, -10, 20, 20))->setZValue(0); |
|
1515 |
scene.addRect(QRectF(-10, -10, 20, 20))->setZValue(2); |
|
1516 |
scene.addRect(QRectF(-10, -10, 20, 20))->setZValue(-1); |
|
1517 |
scene.addRect(QRectF(-10, -10, 20, 20))->setZValue(3); |
|
1518 |
||
1519 |
QGraphicsView view; |
|
1520 |
QCOMPARE(view.itemAt(0, 0), (QGraphicsItem *)0); |
|
1521 |
||
1522 |
view.setScene(&scene); |
|
1523 |
view.setSceneRect(-10000, -10000, 20000, 20000); |
|
1524 |
view.show(); |
|
1525 |
||
1526 |
QCOMPARE(view.itemAt(0, 0), (QGraphicsItem *)0); |
|
1527 |
QGraphicsItem* item = view.itemAt(view.viewport()->rect().center()); |
|
1528 |
QVERIFY(item); |
|
1529 |
QCOMPARE(item->zValue(), qreal(3)); |
|
1530 |
} |
|
1531 |
||
1532 |
void tst_QGraphicsView::itemAt2() |
|
1533 |
{ |
|
1534 |
// test precision of the itemAt() function with items that are smaller |
|
1535 |
// than 1 pixel. |
|
1536 |
QGraphicsScene scene(0, 0, 100, 100); |
|
1537 |
||
1538 |
// Add a 0.5x0.5 item at position 0 on the scene, top-left corner at -0.25, -0.25. |
|
1539 |
QGraphicsItem *item = scene.addRect(QRectF(-0.25, -0.25, 0.5, 0.5), QPen(Qt::black, 0.1)); |
|
1540 |
||
1541 |
QGraphicsView view(&scene); |
|
1542 |
view.setFixedSize(200, 200); |
|
1543 |
view.setTransformationAnchor(QGraphicsView::NoAnchor); |
|
1544 |
view.setRenderHint(QPainter::Antialiasing); |
|
1545 |
view.show(); |
|
1546 |
QTest::qWaitForWindowShown(&view); |
|
1547 |
QApplication::processEvents(); |
|
1548 |
||
1549 |
QPoint itemViewPoint = view.mapFromScene(item->scenePos()); |
|
1550 |
||
1551 |
for (int i = 0; i < 3; ++i) { |
|
1552 |
QVERIFY(view.itemAt(itemViewPoint)); |
|
1553 |
QVERIFY(!view.items(itemViewPoint).isEmpty()); |
|
1554 |
QVERIFY(view.itemAt(itemViewPoint + QPoint(-1, 0))); |
|
1555 |
QVERIFY(!view.items(itemViewPoint + QPoint(-1, 0)).isEmpty()); |
|
1556 |
QVERIFY(view.itemAt(itemViewPoint + QPoint(-1, -1))); |
|
1557 |
QVERIFY(!view.items(itemViewPoint + QPoint(-1, -1)).isEmpty()); |
|
1558 |
QVERIFY(view.itemAt(itemViewPoint + QPoint(0, -1))); |
|
1559 |
QVERIFY(!view.items(itemViewPoint + QPoint(0, -1)).isEmpty()); |
|
1560 |
item->moveBy(0.1, 0); |
|
1561 |
} |
|
1562 |
||
1563 |
// Here |
|
1564 |
QVERIFY(view.itemAt(itemViewPoint)); |
|
1565 |
QVERIFY(!view.items(itemViewPoint).isEmpty()); |
|
1566 |
QVERIFY(view.itemAt(itemViewPoint + QPoint(0, -1))); |
|
1567 |
QVERIFY(!view.items(itemViewPoint + QPoint(0, -1)).isEmpty()); |
|
1568 |
||
1569 |
if (sizeof(qreal) != sizeof(double)) { |
|
1570 |
QSKIP("Skipped due to rounding errors", SkipAll); |
|
1571 |
} |
|
1572 |
// Not here |
|
1573 |
QVERIFY(!view.itemAt(itemViewPoint + QPoint(-1, 0))); |
|
1574 |
QVERIFY(view.items(itemViewPoint + QPoint(-1, 0)).isEmpty()); |
|
1575 |
QVERIFY(!view.itemAt(itemViewPoint + QPoint(-1, -1))); |
|
1576 |
QVERIFY(view.items(itemViewPoint + QPoint(-1, -1)).isEmpty()); |
|
1577 |
} |
|
1578 |
||
1579 |
void tst_QGraphicsView::mapToScene() |
|
1580 |
{ |
|
1581 |
// Uncomment the commented-out code to see what's going on. It doesn't |
|
1582 |
// affect the test; it just slows it down. |
|
1583 |
||
1584 |
QGraphicsScene scene; |
|
1585 |
scene.addPixmap(QPixmap("3D-Qt-1-2.png")); |
|
1586 |
||
1587 |
QGraphicsView view; |
|
1588 |
view.setScene(&scene); |
|
1589 |
view.setSceneRect(-500, -500, 1000, 1000); |
|
1590 |
#if defined(Q_OS_WINCE) |
|
1591 |
QSize viewSize(200,200); |
|
1592 |
#else |
|
1593 |
QSize viewSize(300,300); |
|
1594 |
#endif |
|
1595 |
||
1596 |
view.setFixedSize(viewSize); |
|
1597 |
view.show(); |
|
1598 |
QApplication::processEvents(); |
|
1599 |
QVERIFY(view.isVisible()); |
|
1600 |
QCOMPARE(view.size(), viewSize); |
|
1601 |
||
1602 |
// First once without setting the scene rect |
|
1603 |
#ifdef QT_ARCH_ARM |
|
1604 |
const int step = 20; |
|
1605 |
#else |
|
1606 |
const int step = 1; |
|
1607 |
#endif |
|
1608 |
||
1609 |
for (int x = 0; x < view.width(); x += step) { |
|
1610 |
for (int y = 0; y < view.height(); y += step) { |
|
1611 |
QCOMPARE(view.mapToScene(QPoint(x, y)), |
|
1612 |
QPointF(view.horizontalScrollBar()->value() + x, |
|
1613 |
view.verticalScrollBar()->value() + y)); |
|
1614 |
} |
|
1615 |
} |
|
1616 |
||
1617 |
for (int sceneRectHeight = 250; sceneRectHeight < 1000; sceneRectHeight += 250) { |
|
1618 |
for (int sceneRectWidth = 250; sceneRectWidth < 1000; sceneRectWidth += 250) { |
|
1619 |
view.setSceneRect(QRectF(-int(sceneRectWidth / 2), -int(sceneRectHeight / 2), |
|
1620 |
sceneRectWidth, sceneRectHeight)); |
|
1621 |
QApplication::processEvents(); |
|
1622 |
||
1623 |
int hmin = view.horizontalScrollBar()->minimum(); |
|
1624 |
int hmax = view.horizontalScrollBar()->maximum(); |
|
1625 |
int hstep = (hmax - hmin) / 3; |
|
1626 |
int vmin = view.verticalScrollBar()->minimum(); |
|
1627 |
int vmax = view.verticalScrollBar()->maximum(); |
|
1628 |
int vstep = (vmax - vmin) / 3; |
|
1629 |
||
1630 |
for (int hscrollValue = hmin; hscrollValue < hmax; hscrollValue += hstep) { |
|
1631 |
for (int vscrollValue = vmin; vscrollValue < vmax; vscrollValue += vstep) { |
|
1632 |
||
1633 |
view.horizontalScrollBar()->setValue(hscrollValue); |
|
1634 |
view.verticalScrollBar()->setValue(vscrollValue); |
|
1635 |
QApplication::processEvents(); |
|
1636 |
||
1637 |
int h = view.horizontalScrollBar()->value(); |
|
1638 |
int v = view.verticalScrollBar()->value(); |
|
1639 |
||
1640 |
for (int x = 0; x < view.width(); x += step) { |
|
1641 |
for (int y = 0; y < view.height(); y += step) { |
|
1642 |
QCOMPARE(view.mapToScene(QPoint(x, y)), QPointF(h + x, v + y)); |
|
1643 |
QCOMPARE(view.mapFromScene(QPointF(h + x, v + y)), QPoint(x, y)); |
|
1644 |
} |
|
1645 |
} |
|
1646 |
} |
|
1647 |
} |
|
1648 |
} |
|
1649 |
} |
|
1650 |
} |
|
1651 |
||
1652 |
void tst_QGraphicsView::mapToScenePoint() |
|
1653 |
{ |
|
1654 |
QGraphicsScene scene; |
|
1655 |
QGraphicsView view(&scene); |
|
1656 |
view.rotate(90); |
|
1657 |
view.setFixedSize(117, 117); |
|
1658 |
view.show(); |
|
1659 |
QPoint center = view.viewport()->rect().center(); |
|
1660 |
QCOMPARE(view.mapToScene(center + QPoint(10, 0)), |
|
1661 |
view.mapToScene(center) + QPointF(0, -10)); |
|
1662 |
} |
|
1663 |
||
1664 |
void tst_QGraphicsView::mapToSceneRect_data() |
|
1665 |
{ |
|
1666 |
QTest::addColumn<QRect>("viewRect"); |
|
1667 |
QTest::addColumn<QPolygonF>("scenePoly"); |
|
1668 |
QTest::addColumn<qreal>("rotation"); |
|
1669 |
||
1670 |
QTest::newRow("nil") << QRect() << QPolygonF() << qreal(0); |
|
1671 |
QTest::newRow("0, 0, 1, 1") << QRect(0, 0, 1, 1) << QPolygonF(QRectF(0, 0, 1, 1)) << qreal(0); |
|
1672 |
QTest::newRow("0, 0, 10, 10") << QRect(0, 0, 10, 10) << QPolygonF(QRectF(0, 0, 10, 10)) << qreal(0); |
|
1673 |
QTest::newRow("nil") << QRect() << QPolygonF() << qreal(90); |
|
1674 |
QPolygonF p; |
|
1675 |
p << QPointF(0, 0) << QPointF(0, -1) << QPointF(1, -1) << QPointF(1, 0) << QPointF(0, 0); |
|
1676 |
QTest::newRow("0, 0, 1, 1") << QRect(0, 0, 1, 1) |
|
1677 |
<< p |
|
1678 |
<< qreal(90); |
|
1679 |
p.clear(); |
|
1680 |
p << QPointF(0, 0) << QPointF(0, -10) << QPointF(10, -10) << QPointF(10, 0) << QPointF(0, 0); |
|
1681 |
QTest::newRow("0, 0, 10, 10") << QRect(0, 0, 10, 10) |
|
1682 |
<< p |
|
1683 |
<< qreal(90); |
|
1684 |
} |
|
1685 |
||
1686 |
void tst_QGraphicsView::mapToSceneRect() |
|
1687 |
{ |
|
1688 |
QFETCH(QRect, viewRect); |
|
1689 |
QFETCH(QPolygonF, scenePoly); |
|
1690 |
QFETCH(qreal, rotation); |
|
1691 |
||
1692 |
QGraphicsScene scene(-1000, -1000, 2000, 2000); |
|
1693 |
scene.addRect(25, -25, 50, 50); |
|
1694 |
QGraphicsView view(&scene); |
|
1695 |
view.setFrameStyle(0); |
|
1696 |
view.setAlignment(Qt::AlignTop | Qt::AlignLeft); |
|
1697 |
view.setFixedSize(200, 200); |
|
1698 |
view.setTransformationAnchor(QGraphicsView::NoAnchor); |
|
1699 |
view.setResizeAnchor(QGraphicsView::NoAnchor); |
|
1700 |
view.show(); |
|
1701 |
||
1702 |
view.rotate(rotation); |
|
1703 |
||
1704 |
QPolygonF poly = view.mapToScene(viewRect); |
|
1705 |
if (!poly.isEmpty()) |
|
1706 |
poly << poly[0]; |
|
1707 |
||
1708 |
QCOMPARE(poly, scenePoly); |
|
1709 |
} |
|
1710 |
||
1711 |
void tst_QGraphicsView::mapToScenePoly() |
|
1712 |
{ |
|
1713 |
QGraphicsScene scene; |
|
1714 |
QGraphicsView view(&scene); |
|
1715 |
view.translate(100, 100); |
|
1716 |
view.setFixedSize(117, 117); |
|
1717 |
view.show(); |
|
1718 |
QPoint center = view.viewport()->rect().center(); |
|
1719 |
QRect rect(center + QPoint(10, 0), QSize(10, 10)); |
|
1720 |
||
1721 |
QPolygon poly; |
|
1722 |
poly << rect.topLeft(); |
|
1723 |
poly << rect.topRight(); |
|
1724 |
poly << rect.bottomRight(); |
|
1725 |
poly << rect.bottomLeft(); |
|
1726 |
||
1727 |
QPolygonF poly2; |
|
1728 |
poly2 << view.mapToScene(rect.topLeft()); |
|
1729 |
poly2 << view.mapToScene(rect.topRight()); |
|
1730 |
poly2 << view.mapToScene(rect.bottomRight()); |
|
1731 |
poly2 << view.mapToScene(rect.bottomLeft()); |
|
1732 |
||
1733 |
QCOMPARE(view.mapToScene(poly), poly2); |
|
1734 |
} |
|
1735 |
||
1736 |
void tst_QGraphicsView::mapToScenePath() |
|
1737 |
{ |
|
1738 |
QGraphicsScene scene; |
|
1739 |
QGraphicsView view(&scene); |
|
1740 |
view.setSceneRect(-300, -300, 600, 600); |
|
1741 |
view.translate(10, 10); |
|
1742 |
view.setFixedSize(300, 300); |
|
1743 |
view.show(); |
|
1744 |
QPoint center = view.viewport()->rect().center(); |
|
1745 |
QRect rect(QPoint(10, 0), QSize(10, 10)); |
|
1746 |
||
1747 |
QPainterPath path; |
|
1748 |
path.addRect(rect); |
|
1749 |
||
1750 |
QPainterPath path2; |
|
1751 |
path2.addRect(rect.translated(view.horizontalScrollBar()->value() - 10, |
|
1752 |
view.verticalScrollBar()->value() - 10)); |
|
1753 |
QCOMPARE(view.mapToScene(path), path2); |
|
1754 |
} |
|
1755 |
||
1756 |
void tst_QGraphicsView::mapFromScenePoint() |
|
1757 |
{ |
|
1758 |
{ |
|
1759 |
QGraphicsScene scene; |
|
1760 |
QGraphicsView view(&scene); |
|
1761 |
view.rotate(90); |
|
1762 |
view.scale(10, 10); |
|
1763 |
view.setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); |
|
1764 |
view.setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); |
|
1765 |
view.show(); |
|
1766 |
||
1767 |
QPoint mapped = view.mapFromScene(0, 0); |
|
1768 |
QPoint center = view.viewport()->rect().center(); |
|
1769 |
if (qAbs(mapped.x() - center.x()) >= 2 |
|
1770 |
|| qAbs(mapped.y() - center.y()) >= 2) { |
|
1771 |
QString error = QString("Compared values are not the same\n\tActual: (%1, %2)\n\tExpected: (%3, %4)") |
|
1772 |
.arg(mapped.x()).arg(mapped.y()).arg(center.x()).arg(center.y()); |
|
1773 |
QFAIL(qPrintable(error)); |
|
1774 |
} |
|
1775 |
} |
|
1776 |
{ |
|
1777 |
QGraphicsScene scene(0, 0, 200, 200); |
|
1778 |
scene.addRect(QRectF(0, 0, 200, 200), QPen(Qt::black, 1)); |
|
1779 |
QGraphicsView view(&scene); |
|
1780 |
view.resize(view.sizeHint()); |
|
1781 |
view.show(); |
|
1782 |
||
1783 |
QCOMPARE(view.mapFromScene(0, 0), QPoint(0, 0)); |
|
1784 |
QCOMPARE(view.mapFromScene(0.4, 0.4), QPoint(0, 0)); |
|
1785 |
QCOMPARE(view.mapFromScene(0.5, 0.5), QPoint(1, 1)); |
|
1786 |
QCOMPARE(view.mapFromScene(0.9, 0.9), QPoint(1, 1)); |
|
1787 |
QCOMPARE(view.mapFromScene(1.0, 1.0), QPoint(1, 1)); |
|
1788 |
QCOMPARE(view.mapFromScene(100, 100), QPoint(100, 100)); |
|
1789 |
QCOMPARE(view.mapFromScene(100.5, 100.5), QPoint(101, 101)); |
|
1790 |
QCOMPARE(view.mapToScene(0, 0), QPointF(0, 0)); |
|
1791 |
QCOMPARE(view.mapToScene(1, 1), QPointF(1, 1)); |
|
1792 |
QCOMPARE(view.mapToScene(100, 100), QPointF(100, 100)); |
|
1793 |
} |
|
1794 |
} |
|
1795 |
||
1796 |
void tst_QGraphicsView::mapFromSceneRect() |
|
1797 |
{ |
|
1798 |
QGraphicsScene scene; |
|
1799 |
QGraphicsView view(&scene); |
|
1800 |
view.rotate(90); |
|
1801 |
view.setFixedSize(200, 200); |
|
1802 |
view.setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); |
|
1803 |
view.setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); |
|
1804 |
view.show(); |
|
1805 |
QTest::qWait(25); |
|
1806 |
||
1807 |
QPolygon polygon; |
|
1808 |
polygon << QPoint(98, 98); |
|
1809 |
polygon << QPoint(98, 108); |
|
1810 |
polygon << QPoint(88, 108); |
|
1811 |
polygon << QPoint(88, 98); |
|
1812 |
||
1813 |
||
1814 |
QPolygon viewPolygon = view.mapFromScene(0, 0, 10, 10); |
|
1815 |
for (int i = 0; i < 4; ++i) { |
|
1816 |
QVERIFY(qAbs(viewPolygon[i].x() - polygon[i].x()) < 3); |
|
1817 |
QVERIFY(qAbs(viewPolygon[i].y() - polygon[i].y()) < 3); |
|
1818 |
} |
|
1819 |
||
1820 |
QPoint pt = view.mapFromScene(QPointF()); |
|
1821 |
QPolygon p; |
|
1822 |
p << pt << pt << pt << pt; |
|
1823 |
QCOMPARE(view.mapFromScene(QRectF()), p); |
|
1824 |
} |
|
1825 |
||
1826 |
void tst_QGraphicsView::mapFromScenePoly() |
|
1827 |
{ |
|
1828 |
QGraphicsScene scene; |
|
1829 |
QGraphicsView view(&scene); |
|
1830 |
view.rotate(90); |
|
1831 |
view.setFixedSize(200, 200); |
|
1832 |
view.setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); |
|
1833 |
view.setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); |
|
1834 |
view.show(); |
|
1835 |
||
1836 |
QPolygonF polygon; |
|
1837 |
polygon << QPoint(0, 0); |
|
1838 |
polygon << QPoint(10, 0); |
|
1839 |
polygon << QPoint(10, 10); |
|
1840 |
polygon << QPoint(0, 10); |
|
1841 |
||
1842 |
QPolygon polygon2; |
|
1843 |
polygon2 << QPoint(98, 98); |
|
1844 |
polygon2 << QPoint(98, 108); |
|
1845 |
polygon2 << QPoint(88, 108); |
|
1846 |
polygon2 << QPoint(88, 98); |
|
1847 |
||
1848 |
QPolygon viewPolygon = view.mapFromScene(polygon); |
|
1849 |
for (int i = 0; i < 4; ++i) { |
|
1850 |
QVERIFY(qAbs(viewPolygon[i].x() - polygon2[i].x()) < 3); |
|
1851 |
QVERIFY(qAbs(viewPolygon[i].y() - polygon2[i].y()) < 3); |
|
1852 |
} |
|
1853 |
} |
|
1854 |
||
1855 |
void tst_QGraphicsView::mapFromScenePath() |
|
1856 |
{ |
|
1857 |
QGraphicsScene scene; |
|
1858 |
QGraphicsView view(&scene); |
|
1859 |
view.rotate(90); |
|
1860 |
view.setFixedSize(200, 200); |
|
1861 |
view.setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); |
|
1862 |
view.setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); |
|
1863 |
view.show(); |
|
1864 |
||
1865 |
QPolygonF polygon; |
|
1866 |
polygon << QPoint(0, 0); |
|
1867 |
polygon << QPoint(10, 0); |
|
1868 |
polygon << QPoint(10, 10); |
|
1869 |
polygon << QPoint(0, 10); |
|
1870 |
QPainterPath path; |
|
1871 |
path.addPolygon(polygon); |
|
1872 |
||
1873 |
QPolygon polygon2; |
|
1874 |
polygon2 << QPoint(98, 98); |
|
1875 |
polygon2 << QPoint(98, 108); |
|
1876 |
polygon2 << QPoint(88, 108); |
|
1877 |
polygon2 << QPoint(88, 98); |
|
1878 |
QPainterPath path2; |
|
1879 |
path2.addPolygon(polygon2); |
|
1880 |
||
1881 |
QPolygonF pathPoly = view.mapFromScene(path).toFillPolygon(); |
|
1882 |
QPolygonF path2Poly = path2.toFillPolygon(); |
|
1883 |
||
1884 |
for (int i = 0; i < pathPoly.size(); ++i) { |
|
1885 |
QVERIFY(qAbs(pathPoly[i].x() - path2Poly[i].x()) < 3); |
|
1886 |
QVERIFY(qAbs(pathPoly[i].y() - path2Poly[i].y()) < 3); |
|
1887 |
} |
|
1888 |
} |
|
1889 |
||
1890 |
void tst_QGraphicsView::sendEvent() |
|
1891 |
{ |
|
1892 |
QGraphicsScene scene; |
|
1893 |
||
1894 |
TestItem *item = new TestItem; |
|
1895 |
scene.addItem(item); |
|
1896 |
item->setFlag(QGraphicsItem::ItemIsFocusable); |
|
1897 |
item->setFlag(QGraphicsItem::ItemIsMovable); |
|
1898 |
||
1899 |
QGraphicsView view(&scene); |
|
1900 |
view.show(); |
|
1901 |
QTest::qWaitForWindowShown(&view); |
|
1902 |
QApplication::setActiveWindow(&view); |
|
1903 |
QTest::qWait(20); |
|
1904 |
QTRY_COMPARE(QApplication::activeWindow(), static_cast<QWidget *>(&view)); |
|
1905 |
||
1906 |
item->setFocus(); |
|
1907 |
||
1908 |
QCOMPARE(scene.focusItem(), (QGraphicsItem *)item); |
|
1909 |
QCOMPARE(item->events.size(), 2); |
|
1910 |
QCOMPARE(item->events.last(), QEvent::FocusIn); |
|
1911 |
||
1912 |
QPoint itemPoint = view.mapFromScene(item->scenePos()); |
|
1913 |
sendMousePress(view.viewport(), itemPoint); |
|
1914 |
QCOMPARE(item->events.size(), 4); |
|
1915 |
QCOMPARE(item->events.at(item->events.size() - 2), QEvent::GrabMouse); |
|
1916 |
QCOMPARE(item->events.at(item->events.size() - 1), QEvent::GraphicsSceneMousePress); |
|
1917 |
||
1918 |
QMouseEvent mouseMoveEvent(QEvent::MouseMove, itemPoint, view.viewport()->mapToGlobal(itemPoint), |
|
1919 |
Qt::LeftButton, Qt::LeftButton, 0); |
|
1920 |
QApplication::sendEvent(view.viewport(), &mouseMoveEvent); |
|
1921 |
QCOMPARE(item->events.size(), 5); |
|
1922 |
QCOMPARE(item->events.last(), QEvent::GraphicsSceneMouseMove); |
|
1923 |
||
1924 |
QMouseEvent mouseReleaseEvent(QEvent::MouseButtonRelease, itemPoint, |
|
1925 |
view.viewport()->mapToGlobal(itemPoint), |
|
1926 |
Qt::LeftButton, 0, 0); |
|
1927 |
QApplication::sendEvent(view.viewport(), &mouseReleaseEvent); |
|
1928 |
QCOMPARE(item->events.size(), 7); |
|
1929 |
QCOMPARE(item->events.at(item->events.size() - 2), QEvent::GraphicsSceneMouseRelease); |
|
1930 |
QCOMPARE(item->events.at(item->events.size() - 1), QEvent::UngrabMouse); |
|
1931 |
||
1932 |
QKeyEvent keyPress(QEvent::KeyPress, Qt::Key_Space, 0); |
|
1933 |
QApplication::sendEvent(view.viewport(), &keyPress); |
|
1934 |
QCOMPARE(item->events.size(), 9); |
|
1935 |
QCOMPARE(item->events.at(item->events.size() - 2), QEvent::ShortcutOverride); |
|
1936 |
QCOMPARE(item->events.last(), QEvent::KeyPress); |
|
1937 |
} |
|
1938 |
||
1939 |
class MouseWheelScene : public QGraphicsScene |
|
1940 |
{ |
|
1941 |
public: |
|
1942 |
Qt::Orientation orientation; |
|
1943 |
||
1944 |
void wheelEvent(QGraphicsSceneWheelEvent *event) |
|
1945 |
{ |
|
1946 |
orientation = event->orientation(); |
|
1947 |
QGraphicsScene::wheelEvent(event); |
|
1948 |
} |
|
1949 |
}; |
|
1950 |
||
1951 |
void tst_QGraphicsView::wheelEvent() |
|
1952 |
{ |
|
1953 |
// Create a scene with an invalid orientation. |
|
1954 |
MouseWheelScene scene; |
|
1955 |
scene.orientation = Qt::Orientation(-1); |
|
1956 |
||
1957 |
QGraphicsWidget *widget = new QGraphicsWidget; |
|
1958 |
widget->setGeometry(0, 0, 400, 400); |
|
1959 |
widget->setFocusPolicy(Qt::WheelFocus); |
|
1960 |
||
1961 |
EventSpy spy(widget, QEvent::GraphicsSceneWheel); |
|
1962 |
QCOMPARE(spy.count(), 0); |
|
1963 |
||
1964 |
scene.addItem(widget); |
|
1965 |
||
1966 |
// Assign a view. |
|
1967 |
QGraphicsView view(&scene); |
|
1968 |
view.show(); |
|
1969 |
QTest::qWaitForWindowShown(&view); |
|
1970 |
QApplication::setActiveWindow(&view); |
|
1971 |
QTest::qWait(20); |
|
1972 |
QTRY_COMPARE(QApplication::activeWindow(), static_cast<QWidget *>(&view)); |
|
1973 |
||
1974 |
||
1975 |
// Send a wheel event with horizontal orientation. |
|
1976 |
{ |
|
1977 |
QWheelEvent event(view.mapFromScene(widget->boundingRect().center()), |
|
1978 |
view.mapToGlobal(view.mapFromScene(widget->boundingRect().center())), |
|
1979 |
120, 0, 0, Qt::Horizontal); |
|
1980 |
QApplication::sendEvent(view.viewport(), &event); |
|
1981 |
QCOMPARE(scene.orientation, Qt::Horizontal); |
|
1982 |
} |
|
1983 |
||
1984 |
// Send a wheel event with vertical orientation. |
|
1985 |
{ |
|
1986 |
QWheelEvent event(view.mapFromScene(widget->boundingRect().center()), |
|
1987 |
view.mapToGlobal(view.mapFromScene(widget->boundingRect().center())), |
|
1988 |
120, 0, 0, Qt::Vertical); |
|
1989 |
QApplication::sendEvent(view.viewport(), &event); |
|
1990 |
QCOMPARE(scene.orientation, Qt::Vertical); |
|
1991 |
} |
|
1992 |
||
1993 |
QCOMPARE(spy.count(), 2); |
|
1994 |
QVERIFY(widget->hasFocus()); |
|
1995 |
} |
|
1996 |
||
1997 |
void tst_QGraphicsView::cursor() |
|
1998 |
{ |
|
1999 |
#ifndef QT_NO_CURSOR |
|
2000 |
#if defined(Q_OS_WINCE) |
|
2001 |
QSKIP("Qt/CE does not have regular cursor support", SkipAll); |
|
2002 |
#endif |
|
2003 |
QGraphicsScene scene; |
|
2004 |
QGraphicsItem *item = scene.addRect(QRectF(-10, -10, 20, 20)); |
|
2005 |
item->setCursor(Qt::IBeamCursor); |
|
2006 |
||
2007 |
QGraphicsView view(&scene); |
|
2008 |
view.setFixedSize(400, 400); |
|
2009 |
view.show(); |
|
2010 |
QTest::qWaitForWindowShown(&view); |
|
2011 |
||
2012 |
QCOMPARE(view.viewport()->cursor().shape(), QCursor().shape()); |
|
2013 |
view.viewport()->setCursor(Qt::PointingHandCursor); |
|
2014 |
QCOMPARE(view.viewport()->cursor().shape(), Qt::PointingHandCursor); |
|
2015 |
||
2016 |
sendMouseMove(view.viewport(), view.mapFromScene(0, 0)); |
|
2017 |
QCOMPARE(view.viewport()->cursor().shape(), Qt::IBeamCursor); |
|
2018 |
||
2019 |
sendMouseMove(view.viewport(), QPoint(5, 5)); |
|
2020 |
QCOMPARE(view.viewport()->cursor().shape(), Qt::PointingHandCursor); |
|
2021 |
#endif |
|
2022 |
} |
|
2023 |
||
2024 |
void tst_QGraphicsView::cursor2() |
|
2025 |
{ |
|
2026 |
#ifndef QT_NO_CURSOR |
|
2027 |
#if defined(Q_OS_WINCE) |
|
2028 |
QSKIP("Qt/CE does not have regular cursor support", SkipAll); |
|
2029 |
#endif |
|
2030 |
QGraphicsScene scene; |
|
2031 |
QGraphicsItem *item = scene.addRect(QRectF(-10, -10, 20, 20)); |
|
2032 |
item->setCursor(Qt::IBeamCursor); |
|
2033 |
item->setZValue(1); |
|
2034 |
||
2035 |
QGraphicsItem *item2 = scene.addRect(QRectF(-20, -20, 40, 40)); |
|
2036 |
item2->setZValue(0); |
|
2037 |
||
2038 |
QGraphicsView view(&scene); |
|
2039 |
view.viewport()->setCursor(Qt::PointingHandCursor); |
|
2040 |
view.setFixedSize(400, 400); |
|
2041 |
view.show(); |
|
2042 |
QTest::qWaitForWindowShown(&view); |
|
2043 |
||
2044 |
sendMouseMove(view.viewport(), view.mapFromScene(-30, -30)); |
|
2045 |
QCOMPARE(view.viewport()->cursor().shape(), Qt::PointingHandCursor); |
|
2046 |
sendMouseMove(view.viewport(), view.mapFromScene(0, 0)); |
|
2047 |
QCOMPARE(view.viewport()->cursor().shape(), Qt::IBeamCursor); |
|
2048 |
sendMouseMove(view.viewport(), view.mapFromScene(-30, -30)); |
|
2049 |
QCOMPARE(view.viewport()->cursor().shape(), Qt::PointingHandCursor); |
|
2050 |
sendMouseMove(view.viewport(), view.mapFromScene(0, 0)); |
|
2051 |
QCOMPARE(view.viewport()->cursor().shape(), Qt::IBeamCursor); |
|
2052 |
sendMouseMove(view.viewport(), view.mapFromScene(-15, 0)); |
|
2053 |
QCOMPARE(view.viewport()->cursor().shape(), Qt::PointingHandCursor); |
|
2054 |
||
2055 |
view.setDragMode(QGraphicsView::ScrollHandDrag); |
|
2056 |
||
2057 |
sendMouseMove(view.viewport(), view.mapFromScene(-30, -30)); |
|
2058 |
QCOMPARE(view.viewport()->cursor().shape(), Qt::OpenHandCursor); |
|
2059 |
sendMouseMove(view.viewport(), view.mapFromScene(0, 0)); |
|
2060 |
QCOMPARE(view.viewport()->cursor().shape(), Qt::IBeamCursor); |
|
2061 |
sendMouseMove(view.viewport(), view.mapFromScene(-15, -15)); |
|
2062 |
QCOMPARE(view.viewport()->cursor().shape(), Qt::OpenHandCursor); |
|
2063 |
||
2064 |
view.setDragMode(QGraphicsView::NoDrag); |
|
2065 |
QCOMPARE(view.viewport()->cursor().shape(), Qt::ArrowCursor); |
|
2066 |
view.viewport()->setCursor(Qt::PointingHandCursor); |
|
2067 |
QCOMPARE(view.viewport()->cursor().shape(), Qt::PointingHandCursor); |
|
2068 |
||
2069 |
item2->setCursor(Qt::SizeAllCursor); |
|
2070 |
||
2071 |
sendMouseMove(view.viewport(), view.mapFromScene(-30, -30)); |
|
2072 |
QCOMPARE(view.viewport()->cursor().shape(), Qt::PointingHandCursor); |
|
2073 |
sendMouseMove(view.viewport(), view.mapFromScene(-15, -15)); |
|
2074 |
QCOMPARE(view.viewport()->cursor().shape(), Qt::SizeAllCursor); |
|
2075 |
sendMouseMove(view.viewport(), view.mapFromScene(0, 0)); |
|
2076 |
QCOMPARE(view.viewport()->cursor().shape(), Qt::IBeamCursor); |
|
2077 |
sendMouseMove(view.viewport(), view.mapFromScene(-15, -15)); |
|
2078 |
QCOMPARE(view.viewport()->cursor().shape(), Qt::SizeAllCursor); |
|
2079 |
sendMouseMove(view.viewport(), view.mapFromScene(0, 0)); |
|
2080 |
QCOMPARE(view.viewport()->cursor().shape(), Qt::IBeamCursor); |
|
2081 |
sendMouseMove(view.viewport(), view.mapFromScene(-30, -30)); |
|
2082 |
QCOMPARE(view.viewport()->cursor().shape(), Qt::PointingHandCursor); |
|
2083 |
||
2084 |
view.setDragMode(QGraphicsView::ScrollHandDrag); |
|
2085 |
||
2086 |
sendMouseMove(view.viewport(), view.mapFromScene(-30, -30)); |
|
2087 |
QCOMPARE(view.viewport()->cursor().shape(), Qt::OpenHandCursor); |
|
2088 |
sendMouseMove(view.viewport(), view.mapFromScene(0, 0)); |
|
2089 |
QCOMPARE(view.viewport()->cursor().shape(), Qt::IBeamCursor); |
|
2090 |
sendMouseMove(view.viewport(), view.mapFromScene(-15, -15)); |
|
2091 |
QCOMPARE(view.viewport()->cursor().shape(), Qt::SizeAllCursor); |
|
2092 |
#endif |
|
2093 |
} |
|
2094 |
||
2095 |
void tst_QGraphicsView::transformationAnchor() |
|
2096 |
{ |
|
2097 |
QGraphicsScene scene(-1000, -1000, 2000, 2000); |
|
2098 |
scene.addRect(QRectF(-50, -50, 100, 100), QPen(Qt::black), QBrush(Qt::blue)); |
|
2099 |
||
2100 |
QGraphicsView view(&scene); |
|
2101 |
||
2102 |
for (int i = 0; i < 2; ++i) { |
|
2103 |
view.resize(100, 100); |
|
2104 |
view.show(); |
|
2105 |
||
2106 |
if (i == 0) { |
|
2107 |
QCOMPARE(view.transformationAnchor(), QGraphicsView::AnchorViewCenter); |
|
2108 |
} else { |
|
2109 |
view.setTransformationAnchor(QGraphicsView::NoAnchor); |
|
2110 |
} |
|
2111 |
view.centerOn(0, 0); |
|
2112 |
view.horizontalScrollBar()->setValue(100); |
|
2113 |
QApplication::processEvents(); |
|
2114 |
||
2115 |
QPointF center = view.mapToScene(view.viewport()->rect().center()); |
|
2116 |
||
2117 |
view.scale(10, 10); |
|
2118 |
||
2119 |
QPointF newCenter = view.mapToScene(view.viewport()->rect().center()); |
|
2120 |
||
2121 |
if (i == 0) { |
|
2122 |
qreal slack = 3; |
|
2123 |
QVERIFY(qAbs(newCenter.x() - center.x()) < slack); |
|
2124 |
QVERIFY(qAbs(newCenter.y() - center.y()) < slack); |
|
2125 |
} else { |
|
2126 |
qreal slack = qreal(0.3); |
|
2127 |
QVERIFY(qAbs(newCenter.x() - center.x() / 10) < slack); |
|
2128 |
QVERIFY(qAbs(newCenter.y() - center.y() / 10) < slack); |
|
2129 |
} |
|
2130 |
} |
|
2131 |
} |
|
2132 |
||
2133 |
void tst_QGraphicsView::resizeAnchor() |
|
2134 |
{ |
|
2135 |
QGraphicsScene scene(-1000, -1000, 2000, 2000); |
|
2136 |
scene.addRect(QRectF(-50, -50, 100, 100), QPen(Qt::black), QBrush(Qt::blue)); |
|
2137 |
||
2138 |
QGraphicsView view(&scene); |
|
2139 |
||
2140 |
for (int i = 0; i < 2; ++i) { |
|
2141 |
view.resize(100, 100); |
|
2142 |
view.show(); |
|
2143 |
QTest::qWaitForWindowShown(&view); |
|
2144 |
QApplication::processEvents(); |
|
2145 |
||
2146 |
if (i == 0) { |
|
2147 |
QCOMPARE(view.resizeAnchor(), QGraphicsView::NoAnchor); |
|
2148 |
} else { |
|
2149 |
view.setResizeAnchor(QGraphicsView::AnchorViewCenter); |
|
2150 |
} |
|
2151 |
view.centerOn(0, 0); |
|
2152 |
QTest::qWait(25); |
|
2153 |
||
2154 |
QPointF f = view.mapToScene(50, 50); |
|
2155 |
QPointF center = view.mapToScene(view.viewport()->rect().center()); |
|
2156 |
||
2157 |
QApplication::processEvents(); |
|
2158 |
||
2159 |
for (int size = 200; size <= 400; size += 25) { |
|
2160 |
view.resize(size, size); |
|
2161 |
if (i == 0) { |
|
2162 |
QTRY_COMPARE(view.mapToScene(50, 50), f); |
|
2163 |
QTRY_VERIFY(view.mapToScene(view.viewport()->rect().center()) != center); |
|
2164 |
} else { |
|
2165 |
QTRY_VERIFY(view.mapToScene(50, 50) != f); |
|
2166 |
||
2167 |
QPointF newCenter = view.mapToScene(view.viewport()->rect().center()); |
|
2168 |
int slack = 3; |
|
2169 |
QVERIFY(qAbs(newCenter.x() - center.x()) < slack); |
|
2170 |
QVERIFY(qAbs(newCenter.y() - center.y()) < slack); |
|
2171 |
} |
|
2172 |
QApplication::processEvents(); |
|
2173 |
} |
|
2174 |
} |
|
2175 |
} |
|
2176 |
||
2177 |
class CustomView : public QGraphicsView |
|
2178 |
{ |
|
2179 |
Q_OBJECT |
|
2180 |
public: |
|
2181 |
CustomView(QGraphicsScene *s = 0) : QGraphicsView(s) {} |
|
2182 |
QList<QRegion> lastUpdateRegions; |
|
2183 |
bool painted; |
|
2184 |
||
2185 |
protected: |
|
2186 |
void paintEvent(QPaintEvent *event) |
|
2187 |
{ |
|
2188 |
lastUpdateRegions << event->region(); |
|
2189 |
painted = true; |
|
2190 |
QGraphicsView::paintEvent(event); |
|
2191 |
} |
|
2192 |
}; |
|
2193 |
||
2194 |
void tst_QGraphicsView::viewportUpdateMode() |
|
2195 |
{ |
|
2196 |
QGraphicsScene scene(0, 0, 100, 100); |
|
2197 |
scene.setBackgroundBrush(Qt::red); |
|
2198 |
||
2199 |
CustomView view; |
|
2200 |
view.setFixedSize(500, 500); |
|
2201 |
view.setScene(&scene); |
|
2202 |
QCOMPARE(view.viewportUpdateMode(), QGraphicsView::MinimalViewportUpdate); |
|
2203 |
||
2204 |
// Show the view, and initialize our test. |
|
2205 |
view.show(); |
|
2206 |
QTest::qWaitForWindowShown(&view); |
|
2207 |
QTRY_VERIFY(!view.lastUpdateRegions.isEmpty()); |
|
2208 |
view.lastUpdateRegions.clear(); |
|
2209 |
||
2210 |
// Issue two scene updates. |
|
2211 |
scene.update(QRectF(0, 0, 10, 10)); |
|
2212 |
scene.update(QRectF(20, 0, 10, 10)); |
|
2213 |
QTest::qWait(50); |
|
2214 |
||
2215 |
// The view gets two updates for the update scene updates. |
|
2216 |
QTRY_VERIFY(!view.lastUpdateRegions.isEmpty()); |
|
2217 |
#ifndef QT_MAC_USE_COCOA //cocoa doesn't support drawing regions |
|
2218 |
QCOMPARE(view.lastUpdateRegions.last().rects().size(), 2); |
|
2219 |
QCOMPARE(view.lastUpdateRegions.last().rects().at(0).size(), QSize(15, 15)); |
|
2220 |
QCOMPARE(view.lastUpdateRegions.last().rects().at(1).size(), QSize(15, 15)); |
|
2221 |
#endif |
|
2222 |
||
2223 |
// Set full update mode. |
|
2224 |
view.setViewportUpdateMode(QGraphicsView::FullViewportUpdate); |
|
2225 |
QCOMPARE(view.viewportUpdateMode(), QGraphicsView::FullViewportUpdate); |
|
2226 |
view.lastUpdateRegions.clear(); |
|
2227 |
||
2228 |
// Issue two scene updates. |
|
2229 |
scene.update(QRectF(0, 0, 10, 10)); |
|
2230 |
scene.update(QRectF(20, 0, 10, 10)); |
|
2231 |
qApp->processEvents(); |
|
2232 |
qApp->processEvents(); |
|
2233 |
||
2234 |
// The view gets one full viewport update for the update scene updates. |
|
2235 |
QCOMPARE(view.lastUpdateRegions.last().rects().size(), 1); |
|
2236 |
QCOMPARE(view.lastUpdateRegions.last().rects().at(0).size(), view.viewport()->size()); |
|
2237 |
view.lastUpdateRegions.clear(); |
|
2238 |
||
2239 |
// Set smart update mode |
|
2240 |
view.setViewportUpdateMode(QGraphicsView::SmartViewportUpdate); |
|
2241 |
QCOMPARE(view.viewportUpdateMode(), QGraphicsView::SmartViewportUpdate); |
|
2242 |
||
2243 |
// Issue 100 mini-updates |
|
2244 |
for (int i = 0; i < 10; ++i) { |
|
2245 |
for (int j = 0; j < 10; ++j) { |
|
2246 |
scene.update(QRectF(i * 3, j * 3, 1, 1)); |
|
2247 |
} |
|
2248 |
} |
|
2249 |
qApp->processEvents(); |
|
2250 |
qApp->processEvents(); |
|
2251 |
||
2252 |
// The view gets one bounding rect update. |
|
2253 |
QCOMPARE(view.lastUpdateRegions.last().rects().size(), 1); |
|
2254 |
QCOMPARE(view.lastUpdateRegions.last().rects().at(0).size(), QSize(33, 33)); |
|
2255 |
||
2256 |
// Set no update mode |
|
2257 |
view.setViewportUpdateMode(QGraphicsView::NoViewportUpdate); |
|
2258 |
QCOMPARE(view.viewportUpdateMode(), QGraphicsView::NoViewportUpdate); |
|
2259 |
||
2260 |
// Issue two scene updates. |
|
2261 |
view.lastUpdateRegions.clear(); |
|
2262 |
TestItem item; |
|
2263 |
scene.addItem(&item); |
|
2264 |
item.moveBy(10, 10); |
|
2265 |
scene.update(QRectF(0, 0, 10, 10)); |
|
2266 |
scene.update(QRectF(20, 0, 10, 10)); |
|
2267 |
qApp->processEvents(); |
|
2268 |
qApp->processEvents(); |
|
2269 |
||
2270 |
// The view should not get any painting calls from the scene updates |
|
2271 |
QCOMPARE(view.lastUpdateRegions.size(), 0); |
|
2272 |
} |
|
2273 |
||
2274 |
void tst_QGraphicsView::viewportUpdateMode2() |
|
2275 |
{ |
|
2276 |
// Create a view with viewport rect equal to QRect(0, 0, 200, 200). |
|
2277 |
QGraphicsScene dummyScene; |
|
2278 |
CustomView view; |
|
2279 |
view.painted = false; |
|
2280 |
view.setViewportUpdateMode(QGraphicsView::BoundingRectViewportUpdate); |
|
2281 |
view.setScene(&dummyScene); |
|
2282 |
int left, top, right, bottom; |
|
2283 |
view.getContentsMargins(&left, &top, &right, &bottom); |
|
2284 |
view.resize(200 + left + right, 200 + top + bottom); |
|
2285 |
view.show(); |
|
2286 |
QTest::qWaitForWindowShown(&view); |
|
2287 |
QTest::qWait(50); |
|
2288 |
QTRY_VERIFY(view.painted); |
|
2289 |
const QRect viewportRect = view.viewport()->rect(); |
|
2290 |
QCOMPARE(viewportRect, QRect(0, 0, 200, 200)); |
|
2291 |
||
2292 |
#if defined QT_BUILD_INTERNAL |
|
2293 |
QGraphicsViewPrivate *viewPrivate = static_cast<QGraphicsViewPrivate *>(qt_widget_private(&view)); |
|
2294 |
||
2295 |
QRect boundingRect; |
|
2296 |
const QRect rect1(0, 0, 10, 10); |
|
2297 |
QVERIFY(viewPrivate->updateRect(rect1)); |
|
2298 |
QVERIFY(!viewPrivate->fullUpdatePending); |
|
2299 |
boundingRect |= rect1; |
|
2300 |
QCOMPARE(viewPrivate->dirtyBoundingRect, boundingRect); |
|
2301 |
||
2302 |
const QRect rect2(50, 50, 10, 10); |
|
2303 |
QVERIFY(viewPrivate->updateRect(rect2)); |
|
2304 |
QVERIFY(!viewPrivate->fullUpdatePending); |
|
2305 |
boundingRect |= rect2; |
|
2306 |
QCOMPARE(viewPrivate->dirtyBoundingRect, boundingRect); |
|
2307 |
||
2308 |
const QRect rect3(190, 190, 10, 10); |
|
2309 |
QVERIFY(viewPrivate->updateRect(rect3)); |
|
2310 |
QVERIFY(viewPrivate->fullUpdatePending); |
|
2311 |
boundingRect |= rect3; |
|
2312 |
QCOMPARE(viewPrivate->dirtyBoundingRect, boundingRect); |
|
2313 |
||
2314 |
view.lastUpdateRegions.clear(); |
|
2315 |
viewPrivate->processPendingUpdates(); |
|
2316 |
QTest::qWait(50); |
|
2317 |
QCOMPARE(view.lastUpdateRegions.size(), 1); |
|
2318 |
// Note that we adjust by 2 for antialiasing. |
|
2319 |
QCOMPARE(view.lastUpdateRegions.at(0), QRegion(boundingRect.adjusted(-2, -2, 2, 2) & viewportRect)); |
|
2320 |
#endif |
|
2321 |
} |
|
2322 |
||
2323 |
void tst_QGraphicsView::acceptDrops() |
|
2324 |
{ |
|
2325 |
#ifdef QT_NO_DRAGANDDROP |
|
2326 |
QSKIP("Drag'n drop disabled in this build", SkipAll); |
|
2327 |
#else |
|
2328 |
QGraphicsView view; |
|
2329 |
||
2330 |
// Excepted default behavior. |
|
2331 |
QVERIFY(view.acceptDrops()); |
|
2332 |
QVERIFY(view.viewport()->acceptDrops()); |
|
2333 |
||
2334 |
// Excepted behavior with no drops. |
|
2335 |
view.setAcceptDrops(false); |
|
2336 |
QVERIFY(!view.acceptDrops()); |
|
2337 |
QVERIFY(!view.viewport()->acceptDrops()); |
|
2338 |
||
2339 |
// Setting a widget with drops on a QGraphicsView without drops. |
|
2340 |
QWidget *widget = new QWidget; |
|
2341 |
widget->setAcceptDrops(true); |
|
2342 |
view.setViewport(widget); |
|
2343 |
QVERIFY(!view.acceptDrops()); |
|
2344 |
QVERIFY(!view.viewport()->acceptDrops()); |
|
2345 |
||
2346 |
// Switching the view to accept drops. |
|
2347 |
view.setAcceptDrops(true); |
|
2348 |
QVERIFY(view.acceptDrops()); |
|
2349 |
QVERIFY(view.viewport()->acceptDrops()); |
|
2350 |
||
2351 |
// Setting a widget with no drops on a QGraphicsView with drops. |
|
2352 |
widget = new QWidget; |
|
2353 |
widget->setAcceptDrops(false); |
|
2354 |
view.setViewport(widget); |
|
2355 |
QVERIFY(view.viewport()->acceptDrops()); |
|
2356 |
QVERIFY(view.acceptDrops()); |
|
2357 |
||
2358 |
// Switching the view to not accept drops. |
|
2359 |
view.setAcceptDrops(false); |
|
2360 |
QVERIFY(!view.viewport()->acceptDrops()); |
|
2361 |
#endif |
|
2362 |
} |
|
2363 |
||
2364 |
void tst_QGraphicsView::optimizationFlags() |
|
2365 |
{ |
|
2366 |
QGraphicsView view; |
|
2367 |
QVERIFY(!view.optimizationFlags()); |
|
2368 |
||
2369 |
view.setOptimizationFlag(QGraphicsView::DontClipPainter); |
|
2370 |
QVERIFY(view.optimizationFlags() & QGraphicsView::DontClipPainter); |
|
2371 |
view.setOptimizationFlag(QGraphicsView::DontClipPainter, false); |
|
2372 |
QVERIFY(!view.optimizationFlags()); |
|
2373 |
||
2374 |
view.setOptimizationFlag(QGraphicsView::DontSavePainterState); |
|
2375 |
QVERIFY(view.optimizationFlags() & QGraphicsView::DontSavePainterState); |
|
2376 |
view.setOptimizationFlag(QGraphicsView::DontSavePainterState, false); |
|
2377 |
QVERIFY(!view.optimizationFlags()); |
|
2378 |
||
2379 |
view.setOptimizationFlag(QGraphicsView::DontAdjustForAntialiasing); |
|
2380 |
QVERIFY(view.optimizationFlags() & QGraphicsView::DontAdjustForAntialiasing); |
|
2381 |
view.setOptimizationFlag(QGraphicsView::DontAdjustForAntialiasing, false); |
|
2382 |
QVERIFY(!view.optimizationFlags()); |
|
2383 |
||
2384 |
view.setOptimizationFlags(QGraphicsView::DontAdjustForAntialiasing |
|
2385 |
| QGraphicsView::DontClipPainter); |
|
2386 |
QCOMPARE(view.optimizationFlags(), QGraphicsView::OptimizationFlags(QGraphicsView::DontAdjustForAntialiasing |
|
2387 |
| QGraphicsView::DontClipPainter)); |
|
2388 |
} |
|
2389 |
||
2390 |
class MessUpPainterItem : public QGraphicsRectItem |
|
2391 |
{ |
|
2392 |
public: |
|
2393 |
MessUpPainterItem(const QRectF &rect) : QGraphicsRectItem(rect), dirtyPainter(false) |
|
2394 |
{ } |
|
2395 |
||
2396 |
bool dirtyPainter; |
|
2397 |
||
2398 |
void paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *) |
|
2399 |
{ |
|
2400 |
dirtyPainter = (painter->pen().width() != 0); |
|
2401 |
painter->setPen(QPen(Qt::black, 1.0)); |
|
2402 |
} |
|
2403 |
}; |
|
2404 |
||
2405 |
class MyGraphicsView : public QGraphicsView |
|
2406 |
{ |
|
2407 |
public: |
|
2408 |
MyGraphicsView(QGraphicsScene * scene) : QGraphicsView(scene) |
|
2409 |
{ } |
|
2410 |
||
2411 |
void drawBackground(QPainter * painter, const QRectF & rect) { |
|
2412 |
painter->setCompositionMode(QPainter::CompositionMode_Source); |
|
2413 |
painter->drawRect(rect); |
|
2414 |
} |
|
2415 |
||
2416 |
void drawItems (QPainter * painter, int numItems, QGraphicsItem *items[], const QStyleOptionGraphicsItem options[]) { |
|
2417 |
if (!(optimizationFlags() & QGraphicsView::DontSavePainterState)) |
|
2418 |
QCOMPARE(painter->compositionMode(),QPainter::CompositionMode_SourceOver); |
|
2419 |
else |
|
2420 |
QCOMPARE(painter->compositionMode(),QPainter::CompositionMode_Source); |
|
2421 |
QGraphicsView::drawItems(painter,numItems,items,options); |
|
2422 |
} |
|
2423 |
}; |
|
2424 |
||
2425 |
void tst_QGraphicsView::optimizationFlags_dontSavePainterState() |
|
2426 |
{ |
|
2427 |
MessUpPainterItem *parent = new MessUpPainterItem(QRectF(0, 0, 100, 100)); |
|
2428 |
MessUpPainterItem *child = new MessUpPainterItem(QRectF(0, 0, 100, 100)); |
|
2429 |
child->setParentItem(parent); |
|
2430 |
||
2431 |
QGraphicsScene scene; |
|
2432 |
scene.addItem(parent); |
|
2433 |
||
2434 |
QGraphicsView view(&scene); |
|
2435 |
view.show(); |
|
2436 |
QTest::qWaitForWindowShown(&view); |
|
2437 |
view.viewport()->repaint(); |
|
2438 |
||
2439 |
QVERIFY(!parent->dirtyPainter); |
|
2440 |
QVERIFY(!child->dirtyPainter); |
|
2441 |
||
2442 |
view.setOptimizationFlags(QGraphicsView::DontSavePainterState); |
|
2443 |
view.viewport()->repaint(); |
|
2444 |
||
2445 |
#ifdef Q_WS_MAC |
|
2446 |
// Repaint on Mac OS X actually does require spinning the event loop. |
|
2447 |
QTest::qWait(100); |
|
2448 |
#endif |
|
2449 |
QVERIFY(!parent->dirtyPainter); |
|
2450 |
QVERIFY(child->dirtyPainter); |
|
2451 |
||
2452 |
MyGraphicsView painter(&scene); |
|
2453 |
painter.show(); |
|
2454 |
QTest::qWaitForWindowShown(&painter); |
|
2455 |
||
2456 |
MyGraphicsView painter2(&scene); |
|
2457 |
painter2.setOptimizationFlag(QGraphicsView::DontSavePainterState,true); |
|
2458 |
painter2.show(); |
|
2459 |
QTest::qWaitForWindowShown(&painter2); |
|
2460 |
} |
|
2461 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2462 |
void tst_QGraphicsView::optimizationFlags_dontSavePainterState2_data() |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2463 |
{ |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2464 |
QTest::addColumn<bool>("savePainter"); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2465 |
QTest::newRow("With painter state protection") << true; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2466 |
QTest::newRow("Without painter state protection") << false; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2467 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2468 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2469 |
void tst_QGraphicsView::optimizationFlags_dontSavePainterState2() |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2470 |
{ |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2471 |
QFETCH(bool, savePainter); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2472 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2473 |
class MyScene : public QGraphicsScene |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2474 |
{ |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2475 |
public: |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2476 |
void drawBackground(QPainter *p, const QRectF &) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2477 |
{ transformInDrawBackground = p->worldTransform(); } |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2478 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2479 |
void drawForeground(QPainter *p, const QRectF &) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2480 |
{ transformInDrawForeground = p->worldTransform(); } |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2481 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2482 |
QTransform transformInDrawBackground; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2483 |
QTransform transformInDrawForeground; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2484 |
}; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2485 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2486 |
MyScene scene; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2487 |
// Add transformed dummy items to make sure the painter's worldTransform() is changed in drawItems. |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2488 |
scene.addRect(0, 0, 20, 20)->setTransform(QTransform::fromScale(2, 2)); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2489 |
scene.addRect(50, 50, 20, 20)->setTransform(QTransform::fromTranslate(200, 200)); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2490 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2491 |
CustomView view(&scene); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2492 |
if (!savePainter) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2493 |
view.setOptimizationFlag(QGraphicsView::DontSavePainterState); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2494 |
view.rotate(45); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2495 |
view.scale(1.5, 1.5); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2496 |
view.show(); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2497 |
#ifdef Q_WS_X11 |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2498 |
qt_x11_wait_for_window_manager(&view); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2499 |
#endif |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2500 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2501 |
// Make sure the view is repainted; otherwise the tests below will fail. |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2502 |
view.viewport()->repaint(); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2503 |
QTest::qWait(200); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2504 |
QVERIFY(view.painted); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2505 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2506 |
// Make sure the painter's world transform is preserved after drawItems. |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2507 |
const QTransform expectedTransform = view.viewportTransform(); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2508 |
QVERIFY(!expectedTransform.isIdentity()); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2509 |
QCOMPARE(scene.transformInDrawForeground, expectedTransform); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2510 |
QCOMPARE(scene.transformInDrawBackground, expectedTransform); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2511 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2512 |
|
0 | 2513 |
class LodItem : public QGraphicsRectItem |
2514 |
{ |
|
2515 |
public: |
|
2516 |
LodItem(const QRectF &rect) : QGraphicsRectItem(rect), lastLod(-42) |
|
2517 |
{ } |
|
2518 |
||
2519 |
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *viewport) |
|
2520 |
{ |
|
2521 |
lastLod = option->levelOfDetailFromTransform(painter->worldTransform()); |
|
2522 |
QGraphicsRectItem::paint(painter, option, viewport); |
|
2523 |
} |
|
2524 |
||
2525 |
qreal lastLod; |
|
2526 |
}; |
|
2527 |
||
2528 |
void tst_QGraphicsView::levelOfDetail_data() |
|
2529 |
{ |
|
2530 |
QTest::addColumn<QTransform>("transform"); |
|
2531 |
QTest::addColumn<qreal>("lod"); |
|
2532 |
||
2533 |
QTest::newRow("1:4, 1:4") << QTransform().scale(0.25, 0.25) << qreal(0.25); |
|
2534 |
QTest::newRow("1:2, 1:4") << QTransform().scale(0.5, 0.25) << qreal(::sqrt(0.125)); |
|
2535 |
QTest::newRow("4:1, 1:2") << QTransform().scale(0.25, 0.5) << qreal(::sqrt(0.125)); |
|
2536 |
||
2537 |
QTest::newRow("1:2, 1:2") << QTransform().scale(0.5, 0.5) << qreal(0.5); |
|
2538 |
QTest::newRow("1:1, 1:2") << QTransform().scale(1, 0.5) << qreal(::sqrt(0.5)); |
|
2539 |
QTest::newRow("2:1, 1:1") << QTransform().scale(0.5, 1) << qreal(::sqrt(0.5)); |
|
2540 |
||
2541 |
QTest::newRow("1:1, 1:1") << QTransform().scale(1, 1) << qreal(1.0); |
|
2542 |
QTest::newRow("2:1, 1:1") << QTransform().scale(2, 1) << qreal(::sqrt(2.0)); |
|
2543 |
QTest::newRow("1:1, 2:1") << QTransform().scale(2, 1) << qreal(::sqrt(2.0)); |
|
2544 |
QTest::newRow("2:1, 2:1") << QTransform().scale(2, 2) << qreal(2.0); |
|
2545 |
QTest::newRow("2:1, 4:1") << QTransform().scale(2, 4) << qreal(::sqrt(8.0)); |
|
2546 |
QTest::newRow("4:1, 2:1") << QTransform().scale(4, 2) << qreal(::sqrt(8.0)); |
|
2547 |
QTest::newRow("4:1, 4:1") << QTransform().scale(4, 4) << qreal(4.0); |
|
2548 |
} |
|
2549 |
||
2550 |
void tst_QGraphicsView::levelOfDetail() |
|
2551 |
{ |
|
2552 |
QFETCH(QTransform, transform); |
|
2553 |
QFETCH(qreal, lod); |
|
2554 |
||
2555 |
LodItem *item = new LodItem(QRectF(0, 0, 100, 100)); |
|
2556 |
||
2557 |
QGraphicsScene scene; |
|
2558 |
scene.addItem(item); |
|
2559 |
||
2560 |
QGraphicsView view(&scene); |
|
2561 |
view.show(); |
|
2562 |
QTest::qWaitForWindowShown(&view); |
|
2563 |
||
2564 |
QTRY_COMPARE(item->lastLod, qreal(1)); |
|
2565 |
||
2566 |
view.setTransform(transform); |
|
2567 |
||
2568 |
QTRY_COMPARE(item->lastLod, lod); |
|
2569 |
} |
|
2570 |
||
2571 |
// Moved to tst_qgraphicsview_2.cpp |
|
2572 |
extern void _scrollBarRanges_data(); |
|
2573 |
||
2574 |
void tst_QGraphicsView::scrollBarRanges_data() |
|
2575 |
{ |
|
2576 |
_scrollBarRanges_data(); |
|
2577 |
} |
|
2578 |
||
2579 |
void tst_QGraphicsView::scrollBarRanges() |
|
2580 |
{ |
|
2581 |
QFETCH(QSize, viewportSize); |
|
2582 |
QFETCH(QRectF, sceneRect); |
|
2583 |
QFETCH(QTransform, transform); |
|
2584 |
QFETCH(Qt::ScrollBarPolicy, hbarpolicy); |
|
2585 |
QFETCH(Qt::ScrollBarPolicy, vbarpolicy); |
|
2586 |
QFETCH(int, hmin); |
|
2587 |
QFETCH(int, hmax); |
|
2588 |
QFETCH(int, vmin); |
|
2589 |
QFETCH(int, vmax); |
|
2590 |
QFETCH(bool, useMotif); |
|
2591 |
QFETCH(bool, useStyledPanel); |
|
2592 |
||
2593 |
QGraphicsScene scene(sceneRect); |
|
2594 |
scene.addRect(sceneRect, QPen(Qt::blue), QBrush(QColor(Qt::green))); |
|
2595 |
QGraphicsView view(&scene); |
|
2596 |
view.setRenderHint(QPainter::Antialiasing); |
|
2597 |
view.setTransform(transform); |
|
2598 |
view.setFrameStyle(useStyledPanel ? QFrame::StyledPanel : QFrame::NoFrame); |
|
2599 |
||
2600 |
if (useMotif) { |
|
2601 |
#if !defined(QT_NO_STYLE_MOTIF) |
|
2602 |
view.setStyle(new QMotifStyle); |
|
2603 |
#else |
|
2604 |
QSKIP("No Motif style compiled.", SkipSingle); |
|
2605 |
#endif |
|
2606 |
} else { |
|
2607 |
#if defined(Q_OS_WINCE) || defined(Q_OS_SYMBIAN) |
|
2608 |
view.setStyle(new QWindowsStyle); |
|
2609 |
#elif !defined(QT_NO_STYLE_PLASTIQUE) |
|
2610 |
view.setStyle(new QPlastiqueStyle); |
|
2611 |
#endif |
|
2612 |
} |
|
2613 |
view.setStyleSheet(" "); // enables style propagation ;-) |
|
2614 |
||
2615 |
int adjust = 0; |
|
2616 |
if (useStyledPanel) |
|
2617 |
adjust = view.style()->pixelMetric(QStyle::PM_DefaultFrameWidth) * 2; |
|
2618 |
view.resize(viewportSize + QSize(adjust, adjust)); |
|
2619 |
||
2620 |
view.setHorizontalScrollBarPolicy(hbarpolicy); |
|
2621 |
view.setVerticalScrollBarPolicy(vbarpolicy); |
|
2622 |
||
2623 |
view.show(); |
|
2624 |
||
2625 |
QCOMPARE(view.horizontalScrollBar()->minimum(), hmin); |
|
2626 |
QCOMPARE(view.verticalScrollBar()->minimum(), vmin); |
|
2627 |
QCOMPARE(view.horizontalScrollBar()->maximum(), hmax); |
|
2628 |
QCOMPARE(view.verticalScrollBar()->maximum(), vmax); |
|
2629 |
} |
|
2630 |
||
2631 |
class TestView : public QGraphicsView |
|
2632 |
{ |
|
2633 |
public: |
|
2634 |
TestView(QGraphicsScene *scene) |
|
2635 |
: QGraphicsView(scene), accepted(false) |
|
2636 |
{ } |
|
2637 |
||
2638 |
bool accepted; |
|
2639 |
||
2640 |
protected: |
|
2641 |
void mousePressEvent(QMouseEvent *event) |
|
2642 |
{ |
|
2643 |
QGraphicsView::mousePressEvent(event); |
|
2644 |
accepted = event->isAccepted(); |
|
2645 |
} |
|
2646 |
}; |
|
2647 |
||
2648 |
void tst_QGraphicsView::acceptMousePressEvent() |
|
2649 |
{ |
|
2650 |
QGraphicsScene scene; |
|
2651 |
||
2652 |
TestView view(&scene); |
|
2653 |
view.show(); |
|
2654 |
QTest::qWaitForWindowShown(&view); |
|
2655 |
||
2656 |
QMouseEvent event(QEvent::MouseButtonPress, |
|
2657 |
view.viewport()->rect().center(), |
|
2658 |
view.viewport()->mapToGlobal(view.viewport()->rect().center()), |
|
2659 |
Qt::LeftButton, 0, 0); |
|
2660 |
event.setAccepted(false); |
|
2661 |
QApplication::sendEvent(view.viewport(), &event); |
|
2662 |
QVERIFY(!view.accepted); |
|
2663 |
||
2664 |
scene.addRect(0, 0, 2000, 2000)->setFlag(QGraphicsItem::ItemIsMovable); |
|
2665 |
||
2666 |
qApp->processEvents(); // ensure scene rect is updated |
|
2667 |
||
2668 |
QApplication::sendEvent(view.viewport(), &event); |
|
2669 |
QVERIFY(view.accepted); |
|
2670 |
} |
|
2671 |
||
2672 |
void tst_QGraphicsView::replayMouseMove() |
|
2673 |
{ |
|
2674 |
// An empty scene in a view. The view will send the events to the scene in |
|
2675 |
// any case. Note that the view doesn't have to be shown - the mouse event |
|
2676 |
// sending functions below send the events directly to the viewport. |
|
2677 |
QGraphicsScene scene(-10000, -10000, 20000, 20000); |
|
2678 |
QGraphicsView view(&scene); |
|
2679 |
||
2680 |
EventSpy sceneSpy(&scene, QEvent::GraphicsSceneMouseMove); |
|
2681 |
EventSpy viewSpy(view.viewport(), QEvent::MouseMove); |
|
2682 |
||
2683 |
sendMousePress(view.viewport(), view.viewport()->rect().center()); |
|
2684 |
||
2685 |
// One mouse event should be translated into one scene event. |
|
2686 |
for (int i = 0; i < 3; ++i) { |
|
2687 |
sendMouseMove(view.viewport(), view.viewport()->rect().center(), |
|
2688 |
Qt::LeftButton, Qt::MouseButtons(Qt::LeftButton)); |
|
2689 |
QCOMPARE(viewSpy.count(), i + 1); |
|
2690 |
QCOMPARE(sceneSpy.count(), i + 1); |
|
2691 |
} |
|
2692 |
||
2693 |
// When the view is transformed, the view should get no more events. But |
|
2694 |
// the scene should get replays. |
|
2695 |
for (int i = 0; i < 3; ++i) { |
|
2696 |
view.rotate(10); |
|
2697 |
QCOMPARE(viewSpy.count(), 3); |
|
2698 |
QCOMPARE(sceneSpy.count(), 3 + i + 1); |
|
2699 |
} |
|
2700 |
||
2701 |
// When the view is scrolled, the view should get no more events. But the |
|
2702 |
// scene should get replays. |
|
2703 |
for (int i = 0; i < 3; ++i) { |
|
2704 |
view.horizontalScrollBar()->setValue((i + 1) * 10); |
|
2705 |
QCOMPARE(viewSpy.count(), 3); |
|
2706 |
QCOMPARE(sceneSpy.count(), 6 + i + 1); |
|
2707 |
} |
|
2708 |
} |
|
2709 |
||
2710 |
void tst_QGraphicsView::itemsUnderMouse() |
|
2711 |
{ |
|
2712 |
QGraphicsScene scene; |
|
2713 |
QGraphicsProxyWidget w; |
|
2714 |
w.setWidget(new QPushButton("W")); |
|
2715 |
w.resize(50,50); |
|
2716 |
QGraphicsProxyWidget w2(&w); |
|
2717 |
w2.setWidget(new QPushButton("W2")); |
|
2718 |
w2.resize(50,50); |
|
2719 |
QGraphicsProxyWidget w3(&w2); |
|
2720 |
w3.setWidget(new QPushButton("W3")); |
|
2721 |
w3.resize(50,50); |
|
2722 |
w.setZValue(150); |
|
2723 |
w2.setZValue(50); |
|
2724 |
w3.setZValue(0); |
|
2725 |
scene.addItem(&w); |
|
2726 |
||
2727 |
QGraphicsView view(&scene); |
|
2728 |
view.show(); |
|
2729 |
QTest::qWaitForWindowShown(&view); |
|
2730 |
||
2731 |
QCOMPARE(view.items(view.mapFromScene(w3.boundingRect().center())).first(), |
|
2732 |
static_cast<QGraphicsItem *>(&w3)); |
|
2733 |
w2.setFlag(QGraphicsItem::ItemIgnoresTransformations, true); |
|
2734 |
QCOMPARE(view.items(view.mapFromScene(w3.boundingRect().center())).first(), |
|
2735 |
static_cast<QGraphicsItem *>(&w3)); |
|
2736 |
} |
|
2737 |
||
2738 |
class QGraphicsTextItem_task172231 : public QGraphicsTextItem |
|
2739 |
{ |
|
2740 |
public: |
|
2741 |
QGraphicsTextItem_task172231(const QString & text, QGraphicsItem * parent = 0) |
|
2742 |
: QGraphicsTextItem(text, parent) {} |
|
2743 |
QRectF exposedRect; |
|
2744 |
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) |
|
2745 |
{ |
|
2746 |
exposedRect = option->exposedRect; |
|
2747 |
QGraphicsTextItem::paint(painter, option, widget); |
|
2748 |
} |
|
2749 |
}; |
|
2750 |
||
2751 |
void tst_QGraphicsView::task172231_untransformableItems() |
|
2752 |
{ |
|
2753 |
// check fix in QGraphicsView::paintEvent() |
|
2754 |
||
2755 |
QGraphicsScene scene; |
|
2756 |
||
2757 |
QGraphicsTextItem_task172231 *text = |
|
2758 |
new QGraphicsTextItem_task172231("abcdefghijklmnopqrstuvwxyz"); |
|
2759 |
text->setFlag(QGraphicsItem::ItemIgnoresTransformations); |
|
2760 |
scene.addItem(text); |
|
2761 |
||
2762 |
QGraphicsView view(&scene); |
|
2763 |
||
2764 |
view.scale(2, 1); |
|
2765 |
view.show(); |
|
2766 |
QTest::qWaitForWindowShown(&view); |
|
2767 |
QApplication::setActiveWindow(&view); |
|
2768 |
QTRY_COMPARE(QApplication::activeWindow(), static_cast<QWidget *>(&view)); |
|
2769 |
||
2770 |
QRectF origExposedRect = text->exposedRect; |
|
2771 |
||
2772 |
view.resize(int(0.75 * view.width()), view.height()); |
|
2773 |
qApp->processEvents(); |
|
2774 |
||
2775 |
QCOMPARE(text->exposedRect, origExposedRect); |
|
2776 |
||
2777 |
// notice that the fix also goes into QGraphicsView::render() |
|
2778 |
// and QGraphicsScene::render(), but in duplicated code that |
|
2779 |
// is pending a refactoring, so for now we omit autotesting |
|
2780 |
// these functions separately |
|
2781 |
} |
|
2782 |
||
2783 |
class MousePressReleaseScene : public QGraphicsScene |
|
2784 |
{ |
|
2785 |
public: |
|
2786 |
MousePressReleaseScene() |
|
2787 |
: presses(0), releases(0) |
|
2788 |
{ } |
|
2789 |
int presses; |
|
2790 |
int releases; |
|
2791 |
||
2792 |
protected: |
|
2793 |
void mousePressEvent(QGraphicsSceneMouseEvent *event) |
|
2794 |
{ ++presses; QGraphicsScene::mousePressEvent(event); } |
|
2795 |
void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) |
|
2796 |
{ ++releases; QGraphicsScene::mouseReleaseEvent(event); } |
|
2797 |
}; |
|
2798 |
||
2799 |
void tst_QGraphicsView::task180429_mouseReleaseDragMode() |
|
2800 |
{ |
|
2801 |
MousePressReleaseScene scene; |
|
2802 |
||
2803 |
QGraphicsView view(&scene); |
|
2804 |
view.show(); |
|
2805 |
||
2806 |
sendMousePress(view.viewport(), view.viewport()->rect().center()); |
|
2807 |
QCOMPARE(scene.presses, 1); |
|
2808 |
QCOMPARE(scene.releases, 0); |
|
2809 |
sendMouseRelease(view.viewport(), view.viewport()->rect().center()); |
|
2810 |
QCOMPARE(scene.presses, 1); |
|
2811 |
QCOMPARE(scene.releases, 1); |
|
2812 |
||
2813 |
view.setDragMode(QGraphicsView::RubberBandDrag); |
|
2814 |
sendMousePress(view.viewport(), view.viewport()->rect().center()); |
|
2815 |
QCOMPARE(scene.presses, 2); |
|
2816 |
QCOMPARE(scene.releases, 1); |
|
2817 |
sendMouseRelease(view.viewport(), view.viewport()->rect().center()); |
|
2818 |
QCOMPARE(scene.presses, 2); |
|
2819 |
QCOMPARE(scene.releases, 2); |
|
2820 |
} |
|
2821 |
||
2822 |
void tst_QGraphicsView::task187791_setSceneCausesUpdate() |
|
2823 |
{ |
|
2824 |
QGraphicsScene scene(0, 0, 200, 200); |
|
2825 |
QGraphicsView view(&scene); |
|
2826 |
view.show(); |
|
2827 |
QTest::qWaitForWindowShown(&view); |
|
2828 |
||
2829 |
EventSpy updateSpy(view.viewport(), QEvent::Paint); |
|
2830 |
QCOMPARE(updateSpy.count(), 0); |
|
2831 |
||
2832 |
view.setScene(0); |
|
2833 |
QApplication::processEvents(); |
|
2834 |
QTRY_COMPARE(updateSpy.count(), 1); |
|
2835 |
view.setScene(&scene); |
|
2836 |
QApplication::processEvents(); |
|
2837 |
QTRY_COMPARE(updateSpy.count(), 2); |
|
2838 |
} |
|
2839 |
||
2840 |
class MouseMoveCounter : public QGraphicsView |
|
2841 |
{ |
|
2842 |
public: |
|
2843 |
MouseMoveCounter() : mouseMoves(0) |
|
2844 |
{ } |
|
2845 |
int mouseMoves; |
|
2846 |
protected: |
|
2847 |
void mouseMoveEvent(QMouseEvent *event) |
|
2848 |
{ |
|
2849 |
++mouseMoves; |
|
2850 |
QGraphicsView::mouseMoveEvent(event); |
|
2851 |
foreach (QGraphicsItem *item, scene()->items()) { |
|
2852 |
scene()->removeItem(item); |
|
2853 |
delete item; |
|
2854 |
} |
|
2855 |
scene()->addRect(0, 0, 50, 50); |
|
2856 |
scene()->addRect(0, 0, 100, 100); |
|
2857 |
} |
|
2858 |
}; |
|
2859 |
||
2860 |
void tst_QGraphicsView::task186827_deleteReplayedItem() |
|
2861 |
{ |
|
2862 |
// make sure the mouse is not over the window, causing spontaneous mouse moves |
|
2863 |
QCursor::setPos(0, 0); |
|
2864 |
||
2865 |
QGraphicsScene scene; |
|
2866 |
scene.addRect(0, 0, 50, 50); |
|
2867 |
scene.addRect(0, 0, 100, 100); |
|
2868 |
||
2869 |
MouseMoveCounter view; |
|
2870 |
view.setScene(&scene); |
|
2871 |
view.show(); |
|
2872 |
QTest::qWaitForWindowShown(&view); |
|
2873 |
view.viewport()->setMouseTracking(true); |
|
2874 |
||
2875 |
QCOMPARE(view.mouseMoves, 0); |
|
2876 |
{ |
|
2877 |
QMouseEvent event(QEvent::MouseMove, view.mapFromScene(25, 25), Qt::NoButton, 0, 0); |
|
2878 |
QApplication::sendEvent(view.viewport(), &event); |
|
2879 |
} |
|
2880 |
QCOMPARE(view.mouseMoves, 1); |
|
2881 |
QTest::qWait(25); |
|
2882 |
QTRY_COMPARE(view.mouseMoves, 1); |
|
2883 |
QTest::qWait(25); |
|
2884 |
{ |
|
2885 |
QMouseEvent event(QEvent::MouseMove, view.mapFromScene(25, 25), Qt::NoButton, 0, 0); |
|
2886 |
QApplication::sendEvent(view.viewport(), &event); |
|
2887 |
} |
|
2888 |
QCOMPARE(view.mouseMoves, 2); |
|
2889 |
QTest::qWait(15); |
|
2890 |
} |
|
2891 |
||
2892 |
void tst_QGraphicsView::task207546_focusCrash() |
|
2893 |
{ |
|
2894 |
class _Widget : public QWidget |
|
2895 |
{ |
|
2896 |
public: |
|
2897 |
bool focusNextPrevChild(bool next) { return QWidget::focusNextPrevChild(next); } |
|
2898 |
} widget; |
|
2899 |
||
2900 |
widget.setLayout(new QVBoxLayout()); |
|
2901 |
QGraphicsView *gr1 = new QGraphicsView(&widget); |
|
2902 |
QGraphicsView *gr2 = new QGraphicsView(&widget); |
|
2903 |
widget.layout()->addWidget(gr1); |
|
2904 |
widget.layout()->addWidget(gr2); |
|
2905 |
widget.show(); |
|
2906 |
QTest::qWaitForWindowShown(&widget); |
|
2907 |
widget.activateWindow(); |
|
2908 |
QApplication::setActiveWindow(&widget); |
|
2909 |
QTRY_COMPARE(QApplication::activeWindow(), static_cast<QWidget *>(&widget)); |
|
2910 |
widget.focusNextPrevChild(true); |
|
2911 |
QCOMPARE(static_cast<QWidget *>(gr2), widget.focusWidget()); |
|
2912 |
} |
|
2913 |
||
2914 |
void tst_QGraphicsView::task210599_unsetDragWhileDragging() |
|
2915 |
{ |
|
2916 |
QGraphicsScene scene(0, 0, 400, 400); |
|
2917 |
QGraphicsView view(&scene); |
|
2918 |
view.setGeometry(0, 0, 200, 200); |
|
2919 |
view.show(); |
|
2920 |
||
2921 |
QPoint origPos = QPoint(100, 100); |
|
2922 |
QPoint step1Pos = QPoint(100, 110); |
|
2923 |
QPoint step2Pos = QPoint(100, 120); |
|
2924 |
||
2925 |
// Enable and do a drag |
|
2926 |
{ |
|
2927 |
view.setDragMode(QGraphicsView::ScrollHandDrag); |
|
2928 |
QMouseEvent press(QEvent::MouseButtonPress, origPos, Qt::LeftButton, 0, 0); |
|
2929 |
QMouseEvent move(QEvent::MouseMove, step1Pos, Qt::LeftButton, 0, 0); |
|
2930 |
QApplication::sendEvent(view.viewport(), &press); |
|
2931 |
QApplication::sendEvent(view.viewport(), &move); |
|
2932 |
} |
|
2933 |
||
2934 |
// unset drag and release mouse, inverse order |
|
2935 |
{ |
|
2936 |
view.setDragMode(QGraphicsView::NoDrag); |
|
2937 |
QMouseEvent release(QEvent::MouseButtonRelease, step1Pos, Qt::LeftButton, 0, 0); |
|
2938 |
QApplication::sendEvent(view.viewport(), &release); |
|
2939 |
} |
|
2940 |
||
2941 |
QPoint basePos = view.mapFromScene(0, 0); |
|
2942 |
||
2943 |
// reset drag, and move mouse without holding button down. |
|
2944 |
{ |
|
2945 |
view.setDragMode(QGraphicsView::ScrollHandDrag); |
|
2946 |
QMouseEvent move(QEvent::MouseMove, step2Pos, Qt::LeftButton, 0, 0); |
|
2947 |
QApplication::sendEvent(view.viewport(), &move); |
|
2948 |
} |
|
2949 |
||
2950 |
// Check that no draggin has occured... |
|
2951 |
QCOMPARE(basePos, view.mapFromScene(0, 0)); |
|
2952 |
} |
|
2953 |
||
2954 |
void tst_QGraphicsView::task236394_sendShortcutOverrideEvent() |
|
2955 |
{ |
|
2956 |
QGraphicsView view; |
|
2957 |
view.show(); |
|
2958 |
QKeyEvent event(QEvent::ShortcutOverride, Qt::Key_A, 0, QString("A")); |
|
2959 |
QApplication::sendEvent(&view, &event); |
|
2960 |
} |
|
2961 |
||
2962 |
class ChangedListener : public QObject |
|
2963 |
{ |
|
2964 |
Q_OBJECT |
|
2965 |
public: |
|
2966 |
QList<QList<QRectF> > changes; |
|
2967 |
||
2968 |
public slots: |
|
2969 |
void changed(const QList<QRectF> &dirty) |
|
2970 |
{ |
|
2971 |
changes << dirty; |
|
2972 |
} |
|
2973 |
}; |
|
2974 |
||
2975 |
void tst_QGraphicsView::task239729_noViewUpdate_data() |
|
2976 |
{ |
|
2977 |
QTest::addColumn<bool>("a"); |
|
2978 |
||
2979 |
QTest::newRow("a") << false; |
|
2980 |
QTest::newRow("b") << true; |
|
2981 |
} |
|
2982 |
||
2983 |
void tst_QGraphicsView::task239729_noViewUpdate() |
|
2984 |
{ |
|
2985 |
QFETCH(bool, a); |
|
2986 |
// The scene's changed signal is connected to something that isn't a view. |
|
2987 |
QGraphicsScene scene; |
|
2988 |
ChangedListener cl; |
|
2989 |
QGraphicsView *view = 0; |
|
2990 |
||
2991 |
if (a) { |
|
2992 |
view = new QGraphicsView(&scene); |
|
2993 |
connect(&scene, SIGNAL(changed(const QList<QRectF> &)), &cl, SLOT(changed(const QList<QRectF> &))); |
|
2994 |
} else { |
|
2995 |
connect(&scene, SIGNAL(changed(const QList<QRectF> &)), &cl, SLOT(changed(const QList<QRectF> &))); |
|
2996 |
view = new QGraphicsView(&scene); |
|
2997 |
} |
|
2998 |
||
2999 |
EventSpy spy(view->viewport(), QEvent::Paint); |
|
3000 |
QCOMPARE(spy.count(), 0); |
|
3001 |
||
3002 |
view->show(); |
|
3003 |
QTest::qWaitForWindowShown(view); |
|
3004 |
||
3005 |
QTRY_VERIFY(spy.count() >= 1); |
|
3006 |
spy.reset(); |
|
3007 |
scene.update(); |
|
3008 |
QApplication::processEvents(); |
|
3009 |
QTRY_COMPARE(spy.count(), 1); |
|
3010 |
||
3011 |
delete view; |
|
3012 |
} |
|
3013 |
||
3014 |
void tst_QGraphicsView::task239047_fitInViewSmallViewport() |
|
3015 |
{ |
|
3016 |
// Ensure that with a small viewport, fitInView doesn't mirror the |
|
3017 |
// scene. |
|
3018 |
QWidget widget; |
|
3019 |
QGraphicsScene scene; |
|
3020 |
QGraphicsView *view = new QGraphicsView(&scene, &widget); |
|
3021 |
view->resize(3, 3); |
|
3022 |
QCOMPARE(view->size(), QSize(3, 3)); |
|
3023 |
widget.show(); |
|
3024 |
view->fitInView(0, 0, 100, 100); |
|
3025 |
QPointF topLeft = view->mapToScene(0, 0); |
|
3026 |
QPointF bottomRight = view->mapToScene(100, 100); |
|
3027 |
QVERIFY(bottomRight.x() > topLeft.x()); |
|
3028 |
QVERIFY(bottomRight.y() > topLeft.y()); |
|
3029 |
||
3030 |
view->fitInView(0, 0, 0, 100); |
|
3031 |
||
3032 |
// Don't crash |
|
3033 |
view->scale(0, 0); |
|
3034 |
view->fitInView(0, 0, 100, 100); |
|
3035 |
} |
|
3036 |
||
3037 |
void tst_QGraphicsView::task245469_itemsAtPointWithClip() |
|
3038 |
{ |
|
3039 |
QGraphicsScene scene; |
|
3040 |
QGraphicsItem *parent = scene.addRect(0, 0, 100, 100); |
|
3041 |
QGraphicsItem *child = new QGraphicsRectItem(40, 40, 20, 20, parent); |
|
3042 |
parent->setFlag(QGraphicsItem::ItemClipsChildrenToShape); |
|
3043 |
||
3044 |
QGraphicsView view(&scene); |
|
3045 |
view.resize(150,150); |
|
3046 |
view.rotate(90); |
|
3047 |
view.show(); |
|
3048 |
QTest::qWaitForWindowShown(&view); |
|
3049 |
||
3050 |
QList<QGraphicsItem *> itemsAtCenter = view.items(view.viewport()->rect().center()); |
|
3051 |
QCOMPARE(itemsAtCenter, (QList<QGraphicsItem *>() << child << parent)); |
|
3052 |
||
3053 |
QPolygonF p = view.mapToScene(QRect(view.viewport()->rect().center(), QSize(1, 1))); |
|
3054 |
QList<QGraphicsItem *> itemsAtCenter2 = scene.items(p); |
|
3055 |
QCOMPARE(itemsAtCenter2, itemsAtCenter); |
|
3056 |
} |
|
3057 |
||
3058 |
static QGraphicsView *createSimpleViewAndScene() |
|
3059 |
{ |
|
3060 |
QGraphicsView *view = new QGraphicsView; |
|
3061 |
QGraphicsScene *scene = new QGraphicsScene; |
|
3062 |
view->setScene(scene); |
|
3063 |
||
3064 |
view->setBackgroundBrush(Qt::blue); |
|
3065 |
||
3066 |
QGraphicsRectItem *rect = scene->addRect(0, 0, 10, 10); |
|
3067 |
rect->setBrush(Qt::red); |
|
3068 |
rect->setPen(Qt::NoPen); |
|
3069 |
return view; |
|
3070 |
} |
|
3071 |
||
3072 |
class SpyItem : public QGraphicsRectItem |
|
3073 |
{ |
|
3074 |
public: |
|
3075 |
SpyItem() |
|
3076 |
: QGraphicsRectItem(QRectF(0, 0, 100, 100)) |
|
3077 |
{ |
|
3078 |
} |
|
3079 |
||
3080 |
void paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *) |
|
3081 |
{ |
|
3082 |
transform = painter->transform(); |
|
3083 |
} |
|
3084 |
||
3085 |
QTransform transform; |
|
3086 |
}; |
|
3087 |
||
3088 |
void tst_QGraphicsView::embeddedViews() |
|
3089 |
{ |
|
3090 |
QGraphicsView *v1 = createSimpleViewAndScene(); |
|
3091 |
QGraphicsView *v2 = createSimpleViewAndScene(); |
|
3092 |
||
3093 |
QGraphicsProxyWidget *proxy = v1->scene()->addWidget(v2); |
|
3094 |
||
3095 |
SpyItem *item = new SpyItem; |
|
3096 |
v2->scene()->addItem(item); |
|
3097 |
||
3098 |
proxy->translate(5, 5); |
|
3099 |
||
3100 |
QImage actual(64, 64, QImage::Format_ARGB32_Premultiplied); |
|
3101 |
actual.fill(0); |
|
3102 |
v1->QWidget::render(&actual); |
|
3103 |
QTransform a = item->transform; |
|
3104 |
||
3105 |
v2->QWidget::render(&actual); |
|
3106 |
QTransform b = item->transform; |
|
3107 |
||
3108 |
QVERIFY(a == b); |
|
3109 |
delete v1; |
|
3110 |
} |
|
3111 |
||
3112 |
void tst_QGraphicsView::scrollAfterResize_data() |
|
3113 |
{ |
|
3114 |
QTest::addColumn<bool>("reverse"); |
|
3115 |
QTest::addColumn<QTransform>("x1"); |
|
3116 |
QTest::addColumn<QTransform>("x2"); |
|
3117 |
QTest::addColumn<QTransform>("x3"); |
|
3118 |
||
3119 |
#if !defined(QT_NO_STYLE_PLASTIQUE) |
|
3120 |
QPlastiqueStyle style; |
|
3121 |
#elif !defined(QT_NO_STYLE_WINDOWS) |
|
3122 |
QWindowsStyle style; |
|
3123 |
#else |
|
3124 |
QCommonStyle style; |
|
3125 |
#endif |
|
3126 |
||
3127 |
int frameWidth = style.pixelMetric(QStyle::PM_DefaultFrameWidth); |
|
3128 |
int extent = style.pixelMetric(QStyle::PM_ScrollBarExtent); |
|
3129 |
int inside = style.styleHint(QStyle::SH_ScrollView_FrameOnlyAroundContents); |
|
3130 |
int viewportWidth = 300; |
|
3131 |
int scrollBarIndent = viewportWidth - extent - (inside ? 4 : 2)*frameWidth; |
|
3132 |
||
3133 |
QTest::newRow("normal") << false |
|
3134 |
<< QTransform() |
|
3135 |
<< QTransform() |
|
3136 |
<< QTransform().translate(-10, 0); |
|
3137 |
QTest::newRow("reverse") << true |
|
3138 |
<< QTransform().translate(scrollBarIndent, 0) |
|
3139 |
<< QTransform().translate(scrollBarIndent + 100, 0) |
|
3140 |
<< QTransform().translate(scrollBarIndent + 110, 0); |
|
3141 |
} |
|
3142 |
||
3143 |
void tst_QGraphicsView::scrollAfterResize() |
|
3144 |
{ |
|
3145 |
QFETCH(bool, reverse); |
|
3146 |
QFETCH(QTransform, x1); |
|
3147 |
QFETCH(QTransform, x2); |
|
3148 |
QFETCH(QTransform, x3); |
|
3149 |
||
3150 |
#if !defined(QT_NO_STYLE_PLASTIQUE) |
|
3151 |
QPlastiqueStyle style; |
|
3152 |
#elif !defined(QT_NO_STYLE_WINDOWS) |
|
3153 |
QWindowsStyle style; |
|
3154 |
#else |
|
3155 |
QCommonStyle style; |
|
3156 |
#endif |
|
3157 |
QGraphicsView view; |
|
3158 |
view.setStyle(&style); |
|
3159 |
if (reverse) |
|
3160 |
view.setLayoutDirection(Qt::RightToLeft); |
|
3161 |
||
3162 |
view.setSceneRect(-1000, -1000, 2000, 2000); |
|
3163 |
view.resize(300, 300); |
|
3164 |
view.show(); |
|
3165 |
QTest::qWaitForWindowShown(&view); |
|
3166 |
view.horizontalScrollBar()->setValue(0); |
|
3167 |
view.verticalScrollBar()->setValue(0); |
|
3168 |
QCOMPARE(view.viewportTransform(), x1); |
|
3169 |
view.resize(400, 300); |
|
3170 |
QCOMPARE(view.viewportTransform(), x2); |
|
3171 |
view.horizontalScrollBar()->setValue(10); |
|
3172 |
QCOMPARE(view.viewportTransform(), x3); |
|
3173 |
} |
|
3174 |
||
3175 |
void tst_QGraphicsView::moveItemWhileScrolling_data() |
|
3176 |
{ |
|
3177 |
QTest::addColumn<bool>("adjustForAntialiasing"); |
|
3178 |
||
3179 |
QTest::newRow("no adjust") << false; |
|
3180 |
QTest::newRow("adjust") << true; |
|
3181 |
} |
|
3182 |
||
3183 |
void tst_QGraphicsView::moveItemWhileScrolling() |
|
3184 |
{ |
|
3185 |
QFETCH(bool, adjustForAntialiasing); |
|
3186 |
||
3187 |
class MoveItemScrollView : public QGraphicsView |
|
3188 |
{ |
|
3189 |
public: |
|
3190 |
MoveItemScrollView() |
|
3191 |
{ |
|
3192 |
setWindowFlags(Qt::X11BypassWindowManagerHint); |
|
3193 |
setScene(new QGraphicsScene(0, 0, 1000, 1000)); |
|
3194 |
rect = scene()->addRect(0, 0, 10, 10); |
|
3195 |
rect->setPos(50, 50); |
|
3196 |
painted = false; |
|
3197 |
} |
|
3198 |
QRegion lastPaintedRegion; |
|
3199 |
QGraphicsItem *rect; |
|
3200 |
bool painted; |
|
3201 |
void waitForPaintEvent() |
|
3202 |
{ |
|
3203 |
QTimer::singleShot(2000, &eventLoop, SLOT(quit())); |
|
3204 |
eventLoop.exec(); |
|
3205 |
} |
|
3206 |
protected: |
|
3207 |
QEventLoop eventLoop; |
|
3208 |
void paintEvent(QPaintEvent *event) |
|
3209 |
{ |
|
3210 |
painted = true; |
|
3211 |
lastPaintedRegion = event->region(); |
|
3212 |
QGraphicsView::paintEvent(event); |
|
3213 |
if (eventLoop.isRunning()) |
|
3214 |
eventLoop.quit(); |
|
3215 |
} |
|
3216 |
}; |
|
3217 |
||
3218 |
MoveItemScrollView view; |
|
3219 |
view.setFrameStyle(0); |
|
3220 |
view.setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); |
|
3221 |
view.setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); |
|
3222 |
view.setResizeAnchor(QGraphicsView::NoAnchor); |
|
3223 |
view.setTransformationAnchor(QGraphicsView::NoAnchor); |
|
3224 |
if (!adjustForAntialiasing) |
|
3225 |
view.setOptimizationFlag(QGraphicsView::DontAdjustForAntialiasing); |
|
3226 |
view.resize(200, 200); |
|
3227 |
view.painted = false; |
|
3228 |
view.show(); |
|
3229 |
QTest::qWaitForWindowShown(&view); |
|
3230 |
QApplication::processEvents(); |
|
3231 |
QTRY_VERIFY(view.painted); |
|
3232 |
view.painted = false; |
|
3233 |
view.lastPaintedRegion = QRegion(); |
|
3234 |
view.horizontalScrollBar()->setValue(view.horizontalScrollBar()->value() + 10); |
|
3235 |
view.rect->moveBy(0, 10); |
|
3236 |
view.waitForPaintEvent(); |
|
3237 |
QTRY_VERIFY(view.painted); |
|
3238 |
||
3239 |
QRegion expectedRegion; |
|
3240 |
expectedRegion += QRect(0, 0, 200, 200); |
|
3241 |
expectedRegion -= QRect(0, 0, 190, 200); |
|
3242 |
int a = adjustForAntialiasing ? 2 : 1; |
|
3243 |
expectedRegion += QRect(40, 50, 10, 10).adjusted(-a, -a, a, a); |
|
3244 |
expectedRegion += QRect(40, 60, 10, 10).adjusted(-a, -a, a, a); |
|
3245 |
COMPARE_REGIONS(view.lastPaintedRegion, expectedRegion); |
|
3246 |
} |
|
3247 |
||
3248 |
void tst_QGraphicsView::centerOnDirtyItem() |
|
3249 |
{ |
|
3250 |
QGraphicsView view; |
|
3251 |
view.setWindowFlags(view.windowFlags() | Qt::WindowStaysOnTopHint); |
|
3252 |
view.resize(200, 200); |
|
3253 |
||
3254 |
QGraphicsScene *scene = new QGraphicsScene; |
|
3255 |
view.setScene(scene); |
|
3256 |
view.setSceneRect(-1000, -1000, 2000, 2000); |
|
3257 |
||
3258 |
QGraphicsRectItem *item = new QGraphicsRectItem(0, 0, 10, 10); |
|
3259 |
item->setBrush(Qt::red); |
|
3260 |
scene->addItem(item); |
|
3261 |
view.centerOn(item); |
|
3262 |
||
3263 |
view.show(); |
|
3264 |
QTest::qWaitForWindowShown(&view); |
|
3265 |
||
3266 |
QImage before(view.viewport()->size(), QImage::Format_ARGB32); |
|
3267 |
view.viewport()->render(&before); |
|
3268 |
||
3269 |
item->setPos(20, 0); |
|
3270 |
view.centerOn(item); |
|
3271 |
||
3272 |
QTest::qWait(50); |
|
3273 |
||
3274 |
QImage after(view.viewport()->size(), QImage::Format_ARGB32); |
|
3275 |
view.viewport()->render(&after); |
|
3276 |
||
3277 |
QCOMPARE(before, after); |
|
3278 |
} |
|
3279 |
||
3280 |
void tst_QGraphicsView::mouseTracking() |
|
3281 |
{ |
|
3282 |
// Mouse tracking should only be automatically enabled if items either accept hover events |
|
3283 |
// or have a cursor set. We never disable mouse tracking if it is already enabled. |
|
3284 |
||
3285 |
{ // Make sure mouse tracking is disabled by default. |
|
3286 |
QGraphicsScene scene(-10000, -10000, 20000, 20000); |
|
3287 |
QGraphicsView view(&scene); |
|
3288 |
QVERIFY(!view.viewport()->hasMouseTracking()); |
|
3289 |
} |
|
3290 |
||
3291 |
{ // Make sure we don't disable mouse tracking in setupViewport/setScene. |
|
3292 |
QGraphicsView view; |
|
3293 |
QWidget *viewport = new QWidget; |
|
3294 |
viewport->setMouseTracking(true); |
|
3295 |
view.setViewport(viewport); |
|
3296 |
QVERIFY(viewport->hasMouseTracking()); |
|
3297 |
||
3298 |
QGraphicsScene scene(-10000, -10000, 20000, 20000); |
|
3299 |
view.setScene(&scene); |
|
3300 |
QVERIFY(viewport->hasMouseTracking()); |
|
3301 |
} |
|
3302 |
||
3303 |
// Make sure we enable mouse tracking when having items that accept hover events. |
|
3304 |
{ |
|
3305 |
// Adding an item to the scene after the scene is set on the view. |
|
3306 |
QGraphicsScene scene(-10000, -10000, 20000, 20000); |
|
3307 |
QGraphicsView view(&scene); |
|
3308 |
||
3309 |
QGraphicsRectItem *item = new QGraphicsRectItem(10, 10, 10, 10); |
|
3310 |
item->setAcceptHoverEvents(true); |
|
3311 |
scene.addItem(item); |
|
3312 |
QVERIFY(view.viewport()->hasMouseTracking()); |
|
3313 |
} |
|
3314 |
{ |
|
3315 |
// Adding an item to the scene before the scene is set on the view. |
|
3316 |
QGraphicsScene scene(-10000, -10000, 20000, 20000); |
|
3317 |
QGraphicsRectItem *item = new QGraphicsRectItem(10, 10, 10, 10); |
|
3318 |
item->setAcceptHoverEvents(true); |
|
3319 |
scene.addItem(item); |
|
3320 |
||
3321 |
QGraphicsView view(&scene); |
|
3322 |
QVERIFY(view.viewport()->hasMouseTracking()); |
|
3323 |
} |
|
3324 |
{ |
|
3325 |
// QGraphicsWidget implicitly accepts hover if it has window decoration. |
|
3326 |
QGraphicsScene scene(-10000, -10000, 20000, 20000); |
|
3327 |
QGraphicsView view(&scene); |
|
3328 |
||
3329 |
QGraphicsWidget *widget = new QGraphicsWidget; |
|
3330 |
scene.addItem(widget); |
|
3331 |
QVERIFY(!view.viewport()->hasMouseTracking()); |
|
3332 |
// Enable window decoraton. |
|
3333 |
widget->setWindowFlags(Qt::Window | Qt::WindowTitleHint); |
|
3334 |
QVERIFY(view.viewport()->hasMouseTracking()); |
|
3335 |
} |
|
3336 |
||
3337 |
// Make sure we enable mouse tracking when having items with a cursor set. |
|
3338 |
{ |
|
3339 |
// Adding an item to the scene after the scene is set on the view. |
|
3340 |
QGraphicsScene scene(-10000, -10000, 20000, 20000); |
|
3341 |
QGraphicsView view(&scene); |
|
3342 |
||
3343 |
QGraphicsRectItem *item = new QGraphicsRectItem(10, 10, 10, 10); |
|
3344 |
#ifndef QT_NO_CURSOR |
|
3345 |
item->setCursor(Qt::CrossCursor); |
|
3346 |
#endif |
|
3347 |
scene.addItem(item); |
|
3348 |
QVERIFY(view.viewport()->hasMouseTracking()); |
|
3349 |
} |
|
3350 |
{ |
|
3351 |
// Adding an item to the scene before the scene is set on the view. |
|
3352 |
QGraphicsScene scene(-10000, -10000, 20000, 20000); |
|
3353 |
QGraphicsRectItem *item = new QGraphicsRectItem(10, 10, 10, 10); |
|
3354 |
#ifndef QT_NO_CURSOR |
|
3355 |
item->setCursor(Qt::CrossCursor); |
|
3356 |
#endif |
|
3357 |
scene.addItem(item); |
|
3358 |
||
3359 |
QGraphicsView view(&scene); |
|
3360 |
QVERIFY(view.viewport()->hasMouseTracking()); |
|
3361 |
} |
|
3362 |
||
3363 |
// Make sure we propagate mouse tracking to all views. |
|
3364 |
{ |
|
3365 |
QGraphicsScene scene(-10000, -10000, 20000, 20000); |
|
3366 |
QGraphicsView view1(&scene); |
|
3367 |
QGraphicsView view2(&scene); |
|
3368 |
QGraphicsView view3(&scene); |
|
3369 |
||
3370 |
QGraphicsRectItem *item = new QGraphicsRectItem(10, 10, 10, 10); |
|
3371 |
#ifndef QT_NO_CURSOR |
|
3372 |
item->setCursor(Qt::CrossCursor); |
|
3373 |
#endif |
|
3374 |
scene.addItem(item); |
|
3375 |
||
3376 |
QVERIFY(view1.viewport()->hasMouseTracking()); |
|
3377 |
QVERIFY(view2.viewport()->hasMouseTracking()); |
|
3378 |
QVERIFY(view3.viewport()->hasMouseTracking()); |
|
3379 |
} |
|
3380 |
} |
|
3381 |
||
3382 |
void tst_QGraphicsView::mouseTracking2() |
|
3383 |
{ |
|
3384 |
// Make sure mouse move events propagates to the scene when |
|
3385 |
// mouse tracking is explicitly enabled on the view, |
|
3386 |
// even when all items ignore hover events / use default cursor. |
|
3387 |
||
3388 |
QGraphicsScene scene; |
|
3389 |
scene.addRect(0, 0, 100, 100); |
|
3390 |
||
3391 |
QGraphicsView view(&scene); |
|
3392 |
view.show(); |
|
3393 |
QTest::qWaitForWindowShown(&view); |
|
3394 |
||
3395 |
QVERIFY(!view.viewport()->hasMouseTracking()); |
|
3396 |
view.viewport()->setMouseTracking(true); // Explicitly enable mouse tracking. |
|
3397 |
QVERIFY(view.viewport()->hasMouseTracking()); |
|
3398 |
||
3399 |
EventSpy spy(&scene, QEvent::GraphicsSceneMouseMove); |
|
3400 |
QCOMPARE(spy.count(), 0); |
|
3401 |
QMouseEvent event(QEvent::MouseMove,view.viewport()->rect().center(), Qt::NoButton, |
|
3402 |
Qt::MouseButtons(Qt::NoButton), 0); |
|
3403 |
QApplication::sendEvent(view.viewport(), &event); |
|
3404 |
QCOMPARE(spy.count(), 1); |
|
3405 |
} |
|
3406 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3407 |
void tst_QGraphicsView::mouseTracking3() |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3408 |
{ |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3409 |
// Mouse tracking should be automatically enabled if AnchorUnderMouse is used for |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3410 |
// view transform or resize. We never disable mouse tracking if it is already enabled. |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3411 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3412 |
{ // Make sure we enable mouse tracking when using AnchorUnderMouse for view transformation. |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3413 |
QGraphicsScene scene(-10000, -10000, 20000, 20000); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3414 |
QGraphicsView view(&scene); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3415 |
QVERIFY(!view.viewport()->hasMouseTracking()); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3416 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3417 |
view.setTransformationAnchor(QGraphicsView::AnchorUnderMouse); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3418 |
QVERIFY(view.viewport()->hasMouseTracking()); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3419 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3420 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3421 |
{ // Make sure we enable mouse tracking when using AnchorUnderMouse for view resizing. |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3422 |
QGraphicsScene scene(-10000, -10000, 20000, 20000); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3423 |
QGraphicsView view(&scene); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3424 |
QVERIFY(!view.viewport()->hasMouseTracking()); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3425 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3426 |
view.setResizeAnchor(QGraphicsView::AnchorUnderMouse); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3427 |
QVERIFY(view.viewport()->hasMouseTracking()); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3428 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3429 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3430 |
{ // Make sure we don't disable mouse tracking in setViewport/setScene (transformation anchor). |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3431 |
QGraphicsView view; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3432 |
view.setTransformationAnchor(QGraphicsView::AnchorUnderMouse); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3433 |
QVERIFY(view.viewport()->hasMouseTracking()); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3434 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3435 |
QWidget *viewport = new QWidget; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3436 |
view.setViewport(viewport); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3437 |
QVERIFY(viewport->hasMouseTracking()); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3438 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3439 |
QGraphicsScene scene(-10000, -10000, 20000, 20000); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3440 |
view.setScene(&scene); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3441 |
QVERIFY(viewport->hasMouseTracking()); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3442 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3443 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3444 |
{ // Make sure we don't disable mouse tracking in setViewport/setScene (resize anchor). |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3445 |
QGraphicsView view; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3446 |
view.setResizeAnchor(QGraphicsView::AnchorUnderMouse); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3447 |
QVERIFY(view.viewport()->hasMouseTracking()); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3448 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3449 |
QWidget *viewport = new QWidget; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3450 |
view.setViewport(viewport); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3451 |
QVERIFY(viewport->hasMouseTracking()); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3452 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3453 |
QGraphicsScene scene(-10000, -10000, 20000, 20000); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3454 |
view.setScene(&scene); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3455 |
QVERIFY(viewport->hasMouseTracking()); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3456 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3457 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3458 |
// Make sure we don't disable mouse tracking when adding an item (transformation anchor). |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3459 |
{ // Adding an item to the scene before the scene is set on the view. |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3460 |
QGraphicsScene scene(-10000, -10000, 20000, 20000); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3461 |
QGraphicsRectItem *item = new QGraphicsRectItem(10, 10, 10, 10); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3462 |
scene.addItem(item); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3463 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3464 |
QGraphicsView view; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3465 |
view.setTransformationAnchor(QGraphicsView::AnchorUnderMouse); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3466 |
view.setScene(&scene); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3467 |
QVERIFY(view.viewport()->hasMouseTracking()); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3468 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3469 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3470 |
{ // Adding an item to the scene after the scene is set on the view. |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3471 |
QGraphicsScene scene(-10000, -10000, 20000, 20000); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3472 |
QGraphicsView view(&scene); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3473 |
view.setTransformationAnchor(QGraphicsView::AnchorUnderMouse); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3474 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3475 |
QGraphicsRectItem *item = new QGraphicsRectItem(10, 10, 10, 10); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3476 |
scene.addItem(item); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3477 |
QVERIFY(view.viewport()->hasMouseTracking()); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3478 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3479 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3480 |
// Make sure we don't disable mouse tracking when adding an item (resize anchor). |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3481 |
{ // Adding an item to the scene before the scene is set on the view. |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3482 |
QGraphicsScene scene(-10000, -10000, 20000, 20000); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3483 |
QGraphicsRectItem *item = new QGraphicsRectItem(10, 10, 10, 10); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3484 |
scene.addItem(item); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3485 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3486 |
QGraphicsView view; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3487 |
view.setResizeAnchor(QGraphicsView::AnchorUnderMouse); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3488 |
view.setScene(&scene); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3489 |
QVERIFY(view.viewport()->hasMouseTracking()); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3490 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3491 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3492 |
{ // Adding an item to the scene after the scene is set on the view. |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3493 |
QGraphicsScene scene(-10000, -10000, 20000, 20000); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3494 |
QGraphicsView view(&scene); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3495 |
view.setResizeAnchor(QGraphicsView::AnchorUnderMouse); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3496 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3497 |
QGraphicsRectItem *item = new QGraphicsRectItem(10, 10, 10, 10); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3498 |
scene.addItem(item); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3499 |
QVERIFY(view.viewport()->hasMouseTracking()); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3500 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3501 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3502 |
|
0 | 3503 |
class RenderTester : public QGraphicsRectItem |
3504 |
{ |
|
3505 |
public: |
|
3506 |
RenderTester(const QRectF &rect) |
|
3507 |
: QGraphicsRectItem(rect), paints(0) |
|
3508 |
{ } |
|
3509 |
||
3510 |
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, |
|
3511 |
QWidget *widget) |
|
3512 |
{ |
|
3513 |
QGraphicsRectItem::paint(painter, option, widget); |
|
3514 |
++paints; |
|
3515 |
} |
|
3516 |
||
3517 |
int paints; |
|
3518 |
}; |
|
3519 |
||
3520 |
void tst_QGraphicsView::render() |
|
3521 |
{ |
|
3522 |
// ### This test can be much more thorough - see QGraphicsScene::render. |
|
3523 |
QGraphicsScene scene; |
|
3524 |
CustomView view(&scene); |
|
3525 |
view.setFrameStyle(0); |
|
3526 |
view.resize(200, 200); |
|
3527 |
view.painted = false; |
|
3528 |
view.show(); |
|
3529 |
QTest::qWaitForWindowShown(&view); |
|
3530 |
QApplication::processEvents(); |
|
3531 |
QTRY_VERIFY(view.painted > 0); |
|
3532 |
||
3533 |
RenderTester *r1 = new RenderTester(QRectF(0, 0, 50, 50)); |
|
3534 |
RenderTester *r2 = new RenderTester(QRectF(50, 50, 50, 50)); |
|
3535 |
RenderTester *r3 = new RenderTester(QRectF(0, 50, 50, 50)); |
|
3536 |
RenderTester *r4 = new RenderTester(QRectF(50, 0, 50, 50)); |
|
3537 |
scene.addItem(r1); |
|
3538 |
scene.addItem(r2); |
|
3539 |
scene.addItem(r3); |
|
3540 |
scene.addItem(r4); |
|
3541 |
||
3542 |
qApp->processEvents(); |
|
3543 |
||
3544 |
QTRY_COMPARE(r1->paints, 1); |
|
3545 |
QCOMPARE(r2->paints, 1); |
|
3546 |
QCOMPARE(r3->paints, 1); |
|
3547 |
QCOMPARE(r4->paints, 1); |
|
3548 |
||
3549 |
QPixmap pix(200, 200); |
|
3550 |
pix.fill(Qt::transparent); |
|
3551 |
QPainter painter(&pix); |
|
3552 |
view.render(&painter); |
|
3553 |
painter.end(); |
|
3554 |
||
3555 |
QCOMPARE(r1->paints, 2); |
|
3556 |
QCOMPARE(r2->paints, 2); |
|
3557 |
QCOMPARE(r3->paints, 2); |
|
3558 |
QCOMPARE(r4->paints, 2); |
|
3559 |
} |
|
3560 |
||
3561 |
void tst_QGraphicsView::exposeRegion() |
|
3562 |
{ |
|
3563 |
RenderTester *item = new RenderTester(QRectF(0, 0, 20, 20)); |
|
3564 |
QGraphicsScene scene; |
|
3565 |
scene.addItem(item); |
|
3566 |
||
3567 |
item->paints = 0; |
|
3568 |
CustomView view; |
|
3569 |
view.setScene(&scene); |
|
3570 |
view.show(); |
|
3571 |
QTest::qWaitForWindowShown(&view); |
|
3572 |
QTRY_VERIFY(item->paints > 0); |
|
3573 |
||
3574 |
item->paints = 0; |
|
3575 |
view.lastUpdateRegions.clear(); |
|
3576 |
||
3577 |
// Update a small area in the viewport's topLeft() and bottomRight(). |
|
3578 |
// (the boundingRect() of this area covers the entire viewport). |
|
3579 |
QWidget *viewport = view.viewport(); |
|
3580 |
QRegion expectedExposeRegion = QRect(0, 0, 5, 5); |
|
3581 |
expectedExposeRegion += QRect(viewport->rect().bottomRight() - QPoint(5, 5), QSize(5, 5)); |
|
3582 |
viewport->update(expectedExposeRegion); |
|
3583 |
QApplication::processEvents(); |
|
3584 |
||
3585 |
// Make sure it triggers correct repaint on the view. |
|
3586 |
QTRY_COMPARE(view.lastUpdateRegions.size(), 1); |
|
3587 |
COMPARE_REGIONS(view.lastUpdateRegions.at(0), expectedExposeRegion); |
|
3588 |
||
3589 |
// Make sure the item didn't get any repaints. |
|
3590 |
#ifndef QT_MAC_USE_COCOA |
|
3591 |
QCOMPARE(item->paints, 0); |
|
3592 |
#endif |
|
3593 |
} |
|
3594 |
||
3595 |
void tst_QGraphicsView::update_data() |
|
3596 |
{ |
|
3597 |
// In view.viewport() coordinates. (viewport rect: QRect(0, 0, 200, 200)) |
|
3598 |
QTest::addColumn<QRect>("updateRect"); |
|
3599 |
QTest::newRow("empty") << QRect(); |
|
3600 |
QTest::newRow("outside left") << QRect(-200, 0, 100, 100); |
|
3601 |
QTest::newRow("outside right") << QRect(400, 0 ,100, 100); |
|
3602 |
QTest::newRow("outside top") << QRect(0, -200, 100, 100); |
|
3603 |
QTest::newRow("outside bottom") << QRect(0, 400, 100, 100); |
|
3604 |
QTest::newRow("partially inside left") << QRect(-50, 0, 100, 100); |
|
3605 |
QTest::newRow("partially inside right") << QRect(-150, 0, 100, 100); |
|
3606 |
QTest::newRow("partially inside top") << QRect(0, -150, 100, 100); |
|
3607 |
QTest::newRow("partially inside bottom") << QRect(0, 150, 100, 100); |
|
3608 |
QTest::newRow("on topLeft edge") << QRect(-100, -100, 100, 100); |
|
3609 |
QTest::newRow("on topRight edge") << QRect(200, -100, 100, 100); |
|
3610 |
QTest::newRow("on bottomRight edge") << QRect(200, 200, 100, 100); |
|
3611 |
QTest::newRow("on bottomLeft edge") << QRect(-200, 200, 100, 100); |
|
3612 |
QTest::newRow("inside topLeft") << QRect(-99, -99, 100, 100); |
|
3613 |
QTest::newRow("inside topRight") << QRect(199, -99, 100, 100); |
|
3614 |
QTest::newRow("inside bottomRight") << QRect(199, 199, 100, 100); |
|
3615 |
QTest::newRow("inside bottomLeft") << QRect(-199, 199, 100, 100); |
|
3616 |
QTest::newRow("large1") << QRect(50, -100, 100, 400); |
|
3617 |
QTest::newRow("large2") << QRect(-100, 50, 400, 100); |
|
3618 |
QTest::newRow("large3") << QRect(-100, -100, 400, 400); |
|
3619 |
QTest::newRow("viewport rect") << QRect(0, 0, 200, 200); |
|
3620 |
} |
|
3621 |
||
3622 |
void tst_QGraphicsView::update() |
|
3623 |
{ |
|
3624 |
QFETCH(QRect, updateRect); |
|
3625 |
||
3626 |
// Create a view with viewport rect equal to QRect(0, 0, 200, 200). |
|
3627 |
QGraphicsScene dummyScene; |
|
3628 |
CustomView view; |
|
3629 |
view.setScene(&dummyScene); |
|
3630 |
int left, top, right, bottom; |
|
3631 |
view.getContentsMargins(&left, &top, &right, &bottom); |
|
3632 |
view.resize(200 + left + right, 200 + top + bottom); |
|
3633 |
view.show(); |
|
3634 |
QTest::qWaitForWindowShown(&view); |
|
3635 |
||
3636 |
QApplication::setActiveWindow(&view); |
|
3637 |
QApplication::processEvents(); |
|
3638 |
QTRY_COMPARE(QApplication::activeWindow(), static_cast<QWidget *>(&view)); |
|
3639 |
||
3640 |
const QRect viewportRect = view.viewport()->rect(); |
|
3641 |
QCOMPARE(viewportRect, QRect(0, 0, 200, 200)); |
|
3642 |
||
3643 |
#if defined QT_BUILD_INTERNAL |
|
3644 |
const bool intersects = updateRect.intersects(viewportRect); |
|
3645 |
QGraphicsViewPrivate *viewPrivate = static_cast<QGraphicsViewPrivate *>(qt_widget_private(&view)); |
|
3646 |
QTRY_COMPARE(viewPrivate->updateRect(updateRect), intersects); |
|
3647 |
QCOMPARE(viewPrivate->updateRegion(updateRect), intersects); |
|
3648 |
||
3649 |
view.lastUpdateRegions.clear(); |
|
3650 |
viewPrivate->processPendingUpdates(); |
|
3651 |
QVERIFY(viewPrivate->dirtyRegion.isEmpty()); |
|
3652 |
QVERIFY(viewPrivate->dirtyBoundingRect.isEmpty()); |
|
3653 |
QApplication::processEvents(); |
|
3654 |
if (!intersects) { |
|
3655 |
QTRY_VERIFY(view.lastUpdateRegions.isEmpty()); |
|
3656 |
} else { |
|
3657 |
QTRY_COMPARE(view.lastUpdateRegions.size(), 1); |
|
3658 |
// Note that we adjust by 2 for antialiasing. |
|
3659 |
QTRY_COMPARE(view.lastUpdateRegions.at(0), QRegion(updateRect.adjusted(-2, -2, 2, 2) & viewportRect)); |
|
3660 |
} |
|
3661 |
QTRY_VERIFY(!viewPrivate->fullUpdatePending); |
|
3662 |
#endif |
|
3663 |
} |
|
3664 |
||
3665 |
void tst_QGraphicsView::inputMethodSensitivity() |
|
3666 |
{ |
|
3667 |
QGraphicsScene scene; |
|
3668 |
QGraphicsView view(&scene); |
|
3669 |
view.show(); |
|
3670 |
QTest::qWaitForWindowShown(&view); |
|
3671 |
QApplication::setActiveWindow(&view); |
|
3672 |
QTRY_COMPARE(QApplication::activeWindow(), static_cast<QWidget *>(&view)); |
|
3673 |
||
3674 |
QGraphicsRectItem *item = new QGraphicsRectItem; |
|
3675 |
||
3676 |
view.setAttribute(Qt::WA_InputMethodEnabled, true); |
|
3677 |
||
3678 |
scene.addItem(item); |
|
3679 |
QCOMPARE(view.testAttribute(Qt::WA_InputMethodEnabled), false); |
|
3680 |
||
3681 |
scene.removeItem(item); |
|
3682 |
QCOMPARE(view.testAttribute(Qt::WA_InputMethodEnabled), false); |
|
3683 |
||
3684 |
item->setFlag(QGraphicsItem::ItemAcceptsInputMethod); |
|
3685 |
scene.addItem(item); |
|
3686 |
QCOMPARE(view.testAttribute(Qt::WA_InputMethodEnabled), false); |
|
3687 |
||
3688 |
scene.removeItem(item); |
|
3689 |
QCOMPARE(view.testAttribute(Qt::WA_InputMethodEnabled), false); |
|
3690 |
||
3691 |
scene.addItem(item); |
|
3692 |
scene.setFocusItem(item); |
|
3693 |
QCOMPARE(view.testAttribute(Qt::WA_InputMethodEnabled), false); |
|
3694 |
||
3695 |
scene.removeItem(item); |
|
3696 |
QCOMPARE(view.testAttribute(Qt::WA_InputMethodEnabled), false); |
|
3697 |
||
3698 |
item->setFlag(QGraphicsItem::ItemIsFocusable); |
|
3699 |
scene.addItem(item); |
|
3700 |
scene.setFocusItem(item); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3701 |
QCOMPARE(scene.focusItem(), static_cast<QGraphicsItem *>(item)); |
0 | 3702 |
QCOMPARE(view.testAttribute(Qt::WA_InputMethodEnabled), true); |
3703 |
||
3704 |
item->setFlag(QGraphicsItem::ItemAcceptsInputMethod, false); |
|
3705 |
QCOMPARE(view.testAttribute(Qt::WA_InputMethodEnabled), false); |
|
3706 |
||
3707 |
item->setFlag(QGraphicsItem::ItemAcceptsInputMethod, true); |
|
3708 |
QCOMPARE(view.testAttribute(Qt::WA_InputMethodEnabled), true); |
|
3709 |
||
3710 |
// introduce another item that is focusable but does not accept input methods |
|
3711 |
QGraphicsRectItem *item2 = new QGraphicsRectItem; |
|
3712 |
item2->setFlag(QGraphicsItem::ItemIsFocusable); |
|
3713 |
scene.addItem(item2); |
|
3714 |
scene.setFocusItem(item2); |
|
3715 |
QCOMPARE(view.testAttribute(Qt::WA_InputMethodEnabled), false); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3716 |
QCOMPARE(scene.focusItem(), static_cast<QGraphicsItem *>(item2)); |
0 | 3717 |
|
3718 |
scene.setFocusItem(item); |
|
3719 |
QCOMPARE(view.testAttribute(Qt::WA_InputMethodEnabled), true); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3720 |
QCOMPARE(scene.focusItem(), static_cast<QGraphicsItem *>(item)); |
0 | 3721 |
|
3722 |
view.setScene(0); |
|
3723 |
QCOMPARE(view.testAttribute(Qt::WA_InputMethodEnabled), false); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3724 |
QCOMPARE(scene.focusItem(), static_cast<QGraphicsItem *>(item)); |
0 | 3725 |
|
3726 |
view.setScene(&scene); |
|
3727 |
QCOMPARE(view.testAttribute(Qt::WA_InputMethodEnabled), true); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3728 |
QCOMPARE(scene.focusItem(), static_cast<QGraphicsItem *>(item)); |
0 | 3729 |
|
3730 |
scene.setFocusItem(item2); |
|
3731 |
QCOMPARE(view.testAttribute(Qt::WA_InputMethodEnabled), false); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3732 |
QCOMPARE(scene.focusItem(), static_cast<QGraphicsItem *>(item2)); |
0 | 3733 |
|
3734 |
view.setScene(0); |
|
3735 |
QCOMPARE(view.testAttribute(Qt::WA_InputMethodEnabled), false); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3736 |
QCOMPARE(scene.focusItem(), static_cast<QGraphicsItem *>(item2)); |
0 | 3737 |
|
3738 |
scene.setFocusItem(item); |
|
3739 |
QCOMPARE(view.testAttribute(Qt::WA_InputMethodEnabled), false); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3740 |
QCOMPARE(scene.focusItem(), static_cast<QGraphicsItem *>(item)); |
0 | 3741 |
|
3742 |
view.setScene(&scene); |
|
3743 |
QCOMPARE(view.testAttribute(Qt::WA_InputMethodEnabled), true); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3744 |
QCOMPARE(scene.focusItem(), static_cast<QGraphicsItem *>(item)); |
0 | 3745 |
} |
3746 |
||
3747 |
class InputContextTester : public QInputContext |
|
3748 |
{ |
|
3749 |
Q_OBJECT |
|
3750 |
public: |
|
3751 |
QString identifierName() { return QString(); } |
|
3752 |
bool isComposing() const { return false; } |
|
3753 |
QString language() { return QString(); } |
|
3754 |
void reset() { ++resets; } |
|
3755 |
int resets; |
|
3756 |
}; |
|
3757 |
||
3758 |
void tst_QGraphicsView::inputContextReset() |
|
3759 |
{ |
|
3760 |
QGraphicsScene scene; |
|
3761 |
QGraphicsView view(&scene); |
|
3762 |
QVERIFY(view.testAttribute(Qt::WA_InputMethodEnabled)); |
|
3763 |
||
3764 |
InputContextTester inputContext; |
|
3765 |
view.setInputContext(&inputContext); |
|
3766 |
||
3767 |
view.show(); |
|
3768 |
QTest::qWaitForWindowShown(&view); |
|
3769 |
QApplication::setActiveWindow(&view); |
|
3770 |
QTRY_COMPARE(QApplication::activeWindow(), static_cast<QWidget *>(&view)); |
|
3771 |
||
3772 |
QGraphicsItem *item1 = new QGraphicsRectItem; |
|
3773 |
item1->setFlags(QGraphicsItem::ItemIsFocusable | QGraphicsItem::ItemAcceptsInputMethod); |
|
3774 |
||
3775 |
inputContext.resets = 0; |
|
3776 |
scene.addItem(item1); |
|
3777 |
QCOMPARE(inputContext.resets, 0); |
|
3778 |
||
3779 |
inputContext.resets = 0; |
|
3780 |
scene.setFocusItem(item1); |
|
3781 |
QCOMPARE(scene.focusItem(), (QGraphicsItem *)item1); |
|
3782 |
QVERIFY(view.testAttribute(Qt::WA_InputMethodEnabled)); |
|
3783 |
QCOMPARE(inputContext.resets, 0); |
|
3784 |
||
3785 |
inputContext.resets = 0; |
|
3786 |
scene.setFocusItem(0); |
|
3787 |
QCOMPARE(inputContext.resets, 1); |
|
3788 |
||
3789 |
// introduce another item that is focusable but does not accept input methods |
|
3790 |
QGraphicsItem *item2 = new QGraphicsRectItem; |
|
3791 |
item1->setFlags(QGraphicsItem::ItemIsFocusable); |
|
3792 |
||
3793 |
inputContext.resets = 0; |
|
3794 |
scene.setFocusItem(item2); |
|
3795 |
QCOMPARE(inputContext.resets, 0); |
|
3796 |
||
3797 |
inputContext.resets = 0; |
|
3798 |
scene.setFocusItem(item1); |
|
3799 |
QCOMPARE(inputContext.resets, 0); |
|
3800 |
} |
|
3801 |
||
3802 |
void tst_QGraphicsView::task253415_reconnectUpdateSceneOnSceneChanged() |
|
3803 |
{ |
|
3804 |
QGraphicsView view; |
|
3805 |
QGraphicsView dummyView; |
|
3806 |
view.setWindowFlags(view.windowFlags() | Qt::WindowStaysOnTopHint); |
|
3807 |
view.resize(200, 200); |
|
3808 |
||
3809 |
QGraphicsScene scene1; |
|
3810 |
QObject::connect(&scene1, SIGNAL(changed(QList<QRectF>)), &dummyView, SLOT(updateScene(QList<QRectF>))); |
|
3811 |
view.setScene(&scene1); |
|
3812 |
||
3813 |
QTest::qWait(12); |
|
3814 |
||
3815 |
QGraphicsScene scene2; |
|
3816 |
QObject::connect(&scene2, SIGNAL(changed(QList<QRectF>)), &dummyView, SLOT(updateScene(QList<QRectF>))); |
|
3817 |
view.setScene(&scene2); |
|
3818 |
||
3819 |
QTest::qWait(12); |
|
3820 |
||
3821 |
bool wasConnected2 = QObject::disconnect(&scene2, SIGNAL(changed(QList<QRectF>)), &view, 0); |
|
3822 |
QVERIFY(wasConnected2); |
|
3823 |
} |
|
3824 |
||
3825 |
void tst_QGraphicsView::task255529_transformationAnchorMouseAndViewportMargins() |
|
3826 |
{ |
|
3827 |
#if defined(Q_OS_WINCE) |
|
3828 |
QSKIP("Qt/CE does not implement mouse tracking at this point", SkipAll); |
|
3829 |
#endif |
|
3830 |
||
3831 |
QGraphicsScene scene(-100, -100, 200, 200); |
|
3832 |
scene.addRect(QRectF(-50, -50, 100, 100), QPen(Qt::black), QBrush(Qt::blue)); |
|
3833 |
||
3834 |
class VpGraphicsView: public QGraphicsView |
|
3835 |
{ |
|
3836 |
public: |
|
3837 |
VpGraphicsView(QGraphicsScene *scene) |
|
3838 |
: QGraphicsView(scene) |
|
3839 |
{ |
|
3840 |
setViewportMargins(8, 16, 12, 20); |
|
3841 |
setTransformationAnchor(QGraphicsView::AnchorUnderMouse); |
|
3842 |
setMouseTracking(true); |
|
3843 |
} |
|
3844 |
}; |
|
3845 |
||
3846 |
VpGraphicsView view(&scene); |
|
3847 |
view.setWindowFlags(Qt::X11BypassWindowManagerHint); |
|
3848 |
view.show(); |
|
3849 |
QTest::qWaitForWindowShown(&view); |
|
3850 |
QTest::qWait(50); |
|
3851 |
QPoint mouseViewPos(20, 20); |
|
3852 |
sendMouseMove(view.viewport(), mouseViewPos); |
|
3853 |
||
3854 |
QPointF mouseScenePos = view.mapToScene(mouseViewPos); |
|
3855 |
view.setTransform(QTransform().scale(5, 5).rotate(5, Qt::ZAxis), true); |
|
3856 |
||
3857 |
QPointF newMouseScenePos = view.mapToScene(mouseViewPos); |
|
3858 |
||
3859 |
qreal slack = 1; |
|
3860 |
QVERIFY(qAbs(newMouseScenePos.x() - mouseScenePos.x()) < slack); |
|
3861 |
QVERIFY(qAbs(newMouseScenePos.y() - mouseScenePos.y()) < slack); |
|
3862 |
} |
|
3863 |
||
3864 |
void tst_QGraphicsView::task259503_scrollingArtifacts() |
|
3865 |
{ |
|
3866 |
QGraphicsScene scene(0, 0, 800, 600); |
|
3867 |
||
3868 |
QGraphicsRectItem card; |
|
3869 |
card.setRect(0, 0, 50, 50); |
|
3870 |
card.setPen(QPen(Qt::darkRed)); |
|
3871 |
card.setBrush(QBrush(Qt::cyan)); |
|
3872 |
card.setZValue(2.0); |
|
3873 |
card.setPos(300, 300); |
|
3874 |
scene.addItem(&card); |
|
3875 |
||
3876 |
class SAGraphicsView: public QGraphicsView |
|
3877 |
{ |
|
3878 |
public: |
|
3879 |
SAGraphicsView(QGraphicsScene *scene) |
|
3880 |
: QGraphicsView(scene) |
|
3881 |
, itSTimeToTest(false) |
|
3882 |
{ |
|
3883 |
setViewportUpdateMode( QGraphicsView::MinimalViewportUpdate ); |
|
3884 |
resize(QSize(640, 480)); |
|
3885 |
} |
|
3886 |
||
3887 |
QRegion updateRegion; |
|
3888 |
bool itSTimeToTest; |
|
3889 |
||
3890 |
void paintEvent(QPaintEvent *event) |
|
3891 |
{ |
|
3892 |
QGraphicsView::paintEvent(event); |
|
3893 |
||
3894 |
if (itSTimeToTest) |
|
3895 |
{ |
|
3896 |
// qDebug() << event->region(); |
|
3897 |
// qDebug() << updateRegion; |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3898 |
QEXPECT_FAIL("", "The event region doesn't include the original item position region. See QTBUG-4416", Continue); |
0 | 3899 |
QCOMPARE(event->region(), updateRegion); |
3900 |
} |
|
3901 |
} |
|
3902 |
}; |
|
3903 |
||
3904 |
SAGraphicsView view(&scene); |
|
3905 |
view.show(); |
|
3906 |
QTest::qWaitForWindowShown(&view); |
|
3907 |
||
3908 |
int hsbValue = view.horizontalScrollBar()->value(); |
|
3909 |
view.horizontalScrollBar()->setValue(hsbValue / 2); |
|
3910 |
QTest::qWait(10); |
|
3911 |
view.horizontalScrollBar()->setValue(0); |
|
3912 |
QTest::qWait(10); |
|
3913 |
||
3914 |
QRect itemDeviceBoundingRect = card.deviceTransform(view.viewportTransform()).mapRect(card.boundingRect()).toRect(); |
|
3915 |
itemDeviceBoundingRect.adjust(-2, -2, 2, 2); |
|
3916 |
view.updateRegion = itemDeviceBoundingRect; |
|
3917 |
view.updateRegion += itemDeviceBoundingRect.translated(-100, 0); |
|
3918 |
view.itSTimeToTest = true; |
|
3919 |
card.setPos(200, 300); |
|
3920 |
QTest::qWait(10); |
|
3921 |
} |
|
3922 |
||
3923 |
void tst_QGraphicsView::QTBUG_4151_clipAndIgnore_data() |
|
3924 |
{ |
|
3925 |
QTest::addColumn<bool>("clip"); |
|
3926 |
QTest::addColumn<bool>("ignoreTransformations"); |
|
3927 |
QTest::addColumn<int>("numItems"); |
|
3928 |
||
3929 |
QTest::newRow("none") << false << false << 3; |
|
3930 |
QTest::newRow("clip") << true << false << 3; |
|
3931 |
QTest::newRow("ignore") << false << true << 3; |
|
3932 |
QTest::newRow("clip+ignore") << true << true << 3; |
|
3933 |
} |
|
3934 |
||
3935 |
void tst_QGraphicsView::QTBUG_4151_clipAndIgnore() |
|
3936 |
{ |
|
3937 |
QFETCH(bool, clip); |
|
3938 |
QFETCH(bool, ignoreTransformations); |
|
3939 |
QFETCH(int, numItems); |
|
3940 |
||
3941 |
QGraphicsScene scene; |
|
3942 |
||
3943 |
QGraphicsRectItem *parent = new QGraphicsRectItem(QRectF(0, 0, 50, 50), 0); |
|
3944 |
QGraphicsRectItem *child = new QGraphicsRectItem(QRectF(-10, -10, 40, 40), parent); |
|
3945 |
QGraphicsRectItem *ignore = new QGraphicsRectItem(QRectF(60, 60, 50, 50), 0); |
|
3946 |
||
3947 |
if (clip) |
|
3948 |
parent->setFlags(QGraphicsItem::ItemClipsChildrenToShape); |
|
3949 |
if (ignoreTransformations) |
|
3950 |
ignore->setFlag(QGraphicsItem::ItemIgnoresTransformations); |
|
3951 |
||
3952 |
parent->setBrush(Qt::red); |
|
3953 |
child->setBrush(QColor(0, 0, 255, 128)); |
|
3954 |
ignore->setBrush(Qt::green); |
|
3955 |
||
3956 |
scene.addItem(parent); |
|
3957 |
scene.addItem(ignore); |
|
3958 |
||
3959 |
QGraphicsView view(&scene); |
|
3960 |
view.setFrameStyle(0); |
|
3961 |
view.resize(75, 75); |
|
3962 |
view.show(); |
|
3963 |
QTRY_COMPARE(QApplication::activeWindow(), (QWidget *)&view); |
|
3964 |
||
3965 |
QCOMPARE(view.items(view.rect()).size(), numItems); |
|
3966 |
} |
|
3967 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3968 |
void tst_QGraphicsView::QTBUG_5859_exposedRect() |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3969 |
{ |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3970 |
class CustomScene : public QGraphicsScene |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3971 |
{ |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3972 |
public: |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3973 |
CustomScene(const QRectF &rect) : QGraphicsScene(rect) { } |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3974 |
void drawBackground(QPainter *painter, const QRectF &rect) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3975 |
{ lastBackgroundExposedRect = rect; } |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3976 |
QRectF lastBackgroundExposedRect; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3977 |
}; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3978 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3979 |
class CustomRectItem : public QGraphicsRectItem |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3980 |
{ |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3981 |
public: |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3982 |
CustomRectItem(const QRectF &rect) : QGraphicsRectItem(rect) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3983 |
{ setFlag(QGraphicsItem::ItemUsesExtendedStyleOption); } |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3984 |
void paint(QPainter * painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3985 |
{ lastExposedRect = option->exposedRect; } |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3986 |
QRectF lastExposedRect; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3987 |
}; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3988 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3989 |
CustomScene scene(QRectF(0,0,50,50)); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3990 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3991 |
CustomRectItem item(scene.sceneRect()); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3992 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3993 |
scene.addItem(&item); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3994 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3995 |
QGraphicsView view(&scene); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3996 |
view.scale(4.15, 4.15); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3997 |
view.show(); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3998 |
QTest::qWaitForWindowShown(&view); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3999 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4000 |
view.viewport()->repaint(10,10,20,20); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4001 |
QApplication::processEvents(); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4002 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4003 |
QCOMPARE(item.lastExposedRect, scene.lastBackgroundExposedRect); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4004 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4005 |
|
0 | 4006 |
QTEST_MAIN(tst_QGraphicsView) |
4007 |
#include "tst_qgraphicsview.moc" |