Debian Codegraph @

tests/benchmarks/widgets/graphicsview/qgraphicsview/benchapps/scrolltest/main.cpp

raw
1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the test suite of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:GPL-EXCEPT$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21 ** included in the packaging of this file. Please review the following
22 ** information to ensure the GNU General Public License requirements will
23 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24 **
25 ** $QT_END_LICENSE$
26 **
27 ****************************************************************************/
28 #include <QtGui>
29
30 #include "valgrind/callgrind.h"
31
32 class ItemMover : public QObject
33 {
34 Q_OBJECT
35 public:
36 ItemMover(QGraphicsItem *item)
37 : _item(item)
38 {
39 startTimer(0);
40 }
41
42 protected:
43 void timerEvent(QTimerEvent *event)
44 {
45 _item->moveBy(-1, 0);
46 }
47
48 private:
49 QGraphicsItem *_item;
50 };
51
52 class ClipItem : public QGraphicsRectItem
53 {
54 public:
55 ClipItem(qreal x, qreal y, qreal w, qreal h, const QPen &pen, const QBrush &brush)
56 : QGraphicsRectItem(x, y, w, h)
57 {
58 setPen(pen);
59 setBrush(brush);
60 }
61
62 QPainterPath shape() const
63 {
64 QPainterPath path;
65 path.addRect(rect());
66 return path;
67 }
68 };
69
70 class CountView : public QGraphicsView
71 {
72 protected:
73 void paintEvent(QPaintEvent *event)
74 {
75 static int n = 0;
76 if (n)
77 CALLGRIND_START_INSTRUMENTATION
78 QGraphicsView::paintEvent(event);
79 if (n)
80 CALLGRIND_STOP_INSTRUMENTATION
81 if (++n == 500)
82 qApp->quit();
83 }
84 };
85
86 int main(int argc, char *argv[])
87 {
88 QApplication app(argc, argv);
89
90 QGraphicsScene scene;
91 scene.setItemIndexMethod(QGraphicsScene::NoIndex);
92
93 ClipItem *clipItem = new ClipItem(0, 0, 100, 100, QPen(), QBrush(Qt::blue));
94 clipItem->setFlag(QGraphicsItem::ItemClipsChildrenToShape);
95 clipItem->setData(0, "clipItem");
96 scene.addItem(clipItem);
97
98 QGraphicsRectItem *scrollItem = scene.addRect(0, 0, 10, 10, QPen(Qt::NoPen), QBrush(Qt::NoBrush));
99 scrollItem->setParentItem(clipItem);
100 scrollItem->setFlag(QGraphicsItem::ItemIsMovable);
101 scrollItem->setData(0, "scrollItem");
102
103 for (int y = 0; y < 25; ++y) {
104 for (int x = 0; x < 25; ++x) {
105 ClipItem *rect = new ClipItem(0, 0, 90, 20, QPen(Qt::NoPen), QBrush(Qt::green));
106 rect->setParentItem(scrollItem);
107 rect->setPos(x * 95, y * 25);
108 rect->setData(0, qPrintable(QString("rect %1 %2").arg(x).arg(y)));
109 rect->setFlag(QGraphicsItem::ItemClipsChildrenToShape);
110
111 QGraphicsEllipseItem *ellipse = new QGraphicsEllipseItem(-5, -5, 10, 10);
112 ellipse->setPen(QPen(Qt::NoPen));
113 ellipse->setBrush(QBrush(Qt::yellow));
114 ellipse->setParentItem(rect);
115 ellipse->setData(0, qPrintable(QString("ellipse %1 %2").arg(x).arg(y)));
116 }
117 }
118
119 scrollItem->setRect(scrollItem->childrenBoundingRect());
120
121 #if 0
122 ItemMover mover(scrollItem);
123 #endif
124
125 CountView view;
126 view.setScene(&scene);
127 view.setSceneRect(-25, -25, 150, 150);
128 view.resize(300, 300);
129 view.show();
130
131 return app.exec();
132 }
133
134 #include "main.moc"