QtBase  v6.3.1
tst_qgraphicsitem.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the test suite module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:GPL-EXCEPT$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21 ** included in the packaging of this file. Please review the following
22 ** information to ensure the GNU General Public License requirements will
23 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24 **
25 ** $QT_END_LICENSE$
26 **
27 ****************************************************************************/
28 
29 #include <qtest.h>
30 #include <QGraphicsItem>
31 #include <QGraphicsScene>
32 #include <QGraphicsView>
33 
34 class tst_QGraphicsItem : public QObject
35 {
36  Q_OBJECT
37 
38 public:
40  virtual ~tst_QGraphicsItem();
41 
42 public slots:
43  void initTestCase();
44  void init();
45  void cleanup();
46 
47 private slots:
48  void setParentItem();
49  void setParentItem_deep();
50  void setParentItem_deep_reversed();
51  void deleteItemWithManyChildren();
52  void setPos_data();
53  void setPos();
54  void setTransform_data();
55  void setTransform();
56  void rotate();
57  void scale();
58  void shear();
59  void translate();
60  void createTextItem();
61 };
62 
64 {
65 }
66 
68 {
69 }
70 
71 static inline void processEvents()
72 {
75 }
76 
78 {
79  processEvents();
80  QTest::qWait(1500);
81  processEvents();
82 }
83 
85 {
86  processEvents();
87 }
88 
89 void tst_QGraphicsItem::cleanup()
90 {
91 }
92 
93 void tst_QGraphicsItem::setParentItem()
94 {
95  QBENCHMARK {
98  childRect->setParentItem(&rect);
99  }
100 }
101 
102 void tst_QGraphicsItem::setParentItem_deep()
103 {
104  QBENCHMARK {
106  QGraphicsRectItem *lastRect = &rect;
107  for (int i = 0; i < 10; ++i) {
109  childRect->setParentItem(lastRect);
110  lastRect = childRect;
111  }
112  QGraphicsItem *first = rect.childItems().first();
113  first->setParentItem(0);
114  }
115 }
116 
117 void tst_QGraphicsItem::setParentItem_deep_reversed()
118 {
119  QBENCHMARK {
120  QGraphicsRectItem *lastRect = new QGraphicsRectItem;
121  for (int i = 0; i < 100; ++i) {
122  QGraphicsRectItem *parentRect = new QGraphicsRectItem;
123  lastRect->setParentItem(parentRect);
124  lastRect = parentRect;
125  }
126  delete lastRect;
127  }
128 }
129 
130 void tst_QGraphicsItem::deleteItemWithManyChildren()
131 {
132  QBENCHMARK {
134  for (int i = 0; i < 1000; ++i)
135  new QGraphicsRectItem(rect);
136  delete rect;
137  }
138 }
139 
140 void tst_QGraphicsItem::setPos_data()
141 {
142  QTest::addColumn<QPointF>("pos");
143 
144  QTest::newRow("0, 0") << QPointF(0, 0);
145  QTest::newRow("10, 10") << QPointF(10, 10);
146  QTest::newRow("-10, -10") << QPointF(-10, -10);
147 }
148 
149 void tst_QGraphicsItem::setPos()
150 {
151  QFETCH(QPointF, pos);
152 
153  QGraphicsScene scene;
154  QGraphicsRectItem *rect = scene.addRect(QRectF(0, 0, 100, 100));
155  processEvents();
156 
157  QBENCHMARK {
158  rect->setPos(pos);
159  }
160 }
161 
162 void tst_QGraphicsItem::setTransform_data()
163 {
164  QTest::addColumn<QTransform>("transform");
165 
166  QTest::newRow("rotate 45z") << QTransform().rotate(45);
167  QTest::newRow("scale 2x2") << QTransform().scale(2, 2);
168  QTest::newRow("translate 100, 100") << QTransform().translate(100, 100);
169  QTest::newRow("rotate 45x 45y 45z") << QTransform().rotate(45, Qt::XAxis)
170  .rotate(45, Qt::YAxis).rotate(45, Qt::ZAxis);
171 }
172 
173 void tst_QGraphicsItem::setTransform()
174 {
176 
178  QGraphicsRectItem *item = scene.addRect(QRectF(0, 0, 100, 100));
179  processEvents();
180 
181  QBENCHMARK {
183  }
184 }
185 
186 void tst_QGraphicsItem::rotate()
187 {
189  QGraphicsItem *item = scene.addRect(QRectF(0, 0, 100, 100));
190  processEvents();
191 
192  const QTransform rotate(QTransform().rotate(45));
193  QBENCHMARK {
194  item->setTransform(rotate, true);
195  }
196 }
197 
198 void tst_QGraphicsItem::scale()
199 {
201  QGraphicsItem *item = scene.addRect(QRectF(0, 0, 100, 100));
202  processEvents();
203 
205  QBENCHMARK {
206  item->setTransform(scale, true);
207  }
208 }
209 
210 void tst_QGraphicsItem::shear()
211 {
213  QGraphicsItem *item = scene.addRect(QRectF(0, 0, 100, 100));
214  processEvents();
215 
216  const QTransform shear = QTransform().shear(1.5, 1.5);
217  QBENCHMARK {
218  item->setTransform(shear, true);
219  }
220 }
221 
222 void tst_QGraphicsItem::translate()
223 {
225  QGraphicsItem *item = scene.addRect(QRectF(0, 0, 100, 100));
226  processEvents();
227 
228  const QTransform translate = QTransform::fromTranslate(100, 100);
229  QBENCHMARK {
230  item->setTransform(translate, true);
231  }
232 }
233 
234 void tst_QGraphicsItem::createTextItem()
235 {
236  // Ensure QFontDatabase loaded the font beforehand
237  QFontInfo(qApp->font()).family();
238  const QString text = "This is some text";
239  QBENCHMARK {
241  }
242 }
243 
245 #include "tst_qgraphicsitem.moc"
small capitals from c petite p scientific i
[1]
Definition: afcover.h:80
static void processEvents(QEventLoop::ProcessEventsFlags flags=QEventLoop::AllEvents)
The QFontInfo class provides general information about fonts. \inmodule QtGui.
Definition: qfontinfo.h:51
QString family() const
Definition: qfont.cpp:2616
The QGraphicsItem class is the base class for all graphical items in a QGraphicsScene.
Definition: qgraphicsitem.h:83
void setTransform(const QTransform &matrix, bool combine=false)
void setParentItem(QGraphicsItem *parent)
The QGraphicsRectItem class provides a rectangle item that you can add to a QGraphicsScene.
The QGraphicsScene class provides a surface for managing a large number of 2D graphical items.
QGraphicsRectItem * addRect(const QRectF &rect, const QPen &pen=QPen(), const QBrush &brush=QBrush())
The QGraphicsTextItem class provides a text item that you can add to a QGraphicsScene to display form...
The QObject class is the base class of all Qt objects.
Definition: qobject.h:125
The QPointF class defines a point in the plane using floating point precision.
Definition: qpoint.h:242
The QRectF class defines a finite rectangle in the plane using floating point precision.
Definition: qrect.h:511
The QString class provides a Unicode character string.
Definition: qstring.h:388
The QTransform class specifies 2D transformations of a coordinate system.
Definition: qtransform.h:56
static QTransform fromScale(qreal dx, qreal dy)
Definition: qtransform.cpp:503
QTransform & scale(qreal sx, qreal sy)
Definition: qtransform.cpp:460
QTransform & shear(qreal sh, qreal sv)
Definition: qtransform.cpp:526
static QTransform fromTranslate(qreal dx, qreal dy)
Definition: qtransform.cpp:437
QTransform & rotate(qreal a, Qt::Axis axis=Qt::ZAxis)
Definition: qtransform.cpp:588
QTransform & translate(qreal dx, qreal dy)
Definition: qtransform.cpp:392
rect
[4]
Q_TESTLIB_EXPORT QTestData & newRow(const char *dataTag)
Definition: qtestcase.cpp:2658
Q_CORE_EXPORT void qWait(int ms)
@ ZAxis
Definition: qnamespace.h:1357
@ XAxis
Definition: qnamespace.h:1355
@ YAxis
Definition: qnamespace.h:1356
#define QBENCHMARK
Definition: qbenchmark.h:76
#define qApp
@ text
GLint first
GLuint GLenum GLenum transform
Definition: qopenglext.h:11564
GLenum GLenum GLenum GLenum GLenum scale
Definition: qopenglext.h:10817
#define QTEST_MAIN(TestObject)
Definition: qtest.h:664
#define QFETCH(Type, name)
Definition: qtestcase.h:230
#define Q_OBJECT
Definition: qtmetamacros.h:158
#define slots
Definition: qtmetamacros.h:76
QGraphicsScene scene
[0]
QGraphicsItem * item
QRect childRect(QAccessibleInterface *iface, int index=0)