Debian Codegraph @

tests/benchmarks/widgets/graphicsview/qgraphicsview/chiptester/chiptester.cpp

raw
1 /****************************************************************************
2 **
3 ** Copyright (C) 2020 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the test suite of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:GPL-EXCEPT$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21 ** included in the packaging of this file. Please review the following
22 ** information to ensure the GNU General Public License requirements will
23 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24 **
25 ** $QT_END_LICENSE$
26 **
27 ****************************************************************************/
28
29 #include "chiptester.h"
30 #include "chip.h"
31
32 #include <QtGui>
33 #include <QScrollBar>
34 #ifndef QT_NO_OPENGL
35 #include <QtOpenGL>
36 #endif
37
38 ChipTester::ChipTester(QWidget *parent)
39 : QGraphicsView(parent),
40 npaints(0)
41 {
42 resize(400, 300);
43 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
44 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
45 setFrameStyle(0);
46 setTransformationAnchor(NoAnchor);
47
48 populateScene();
49 setScene(scene);
50
51 setWindowTitle(tr("Chip Example"));
52 }
53
54 void ChipTester::setAntialias(bool enabled)
55 {
56 setRenderHint(QPainter::Antialiasing, enabled);
57 }
58
59 void ChipTester::setOpenGL(bool enabled)
60 {
61 #ifndef QT_NO_OPENGL
62 setViewport(enabled ? new QGLWidget(QGLFormat(QGL::SampleBuffers)) : 0);
63 #endif
64 }
65
66 void ChipTester::setOperation(Operation operation)
67 {
68 this->operation = operation;
69 }
70
71 void ChipTester::runBenchmark()
72 {
73 npaints = 0;
74 timerId = startTimer(0);
75 eventLoop.exec();
76 killTimer(timerId);
77 }
78
79 void ChipTester::paintEvent(QPaintEvent *event)
80 {
81 QGraphicsView::paintEvent(event);
82 if (++npaints == 50)
83 eventLoop.quit();
84 }
85
86 void ChipTester::timerEvent(QTimerEvent *)
87 {
88 switch (operation) {
89 case Rotate360:
90 rotate(1);
91 break;
92 case ZoomInOut: {
93 qreal s = 0.05 + (npaints / 20.0);
94 setTransform(QTransform().scale(s, s));
95 break;
96 }
97 case Translate: {
98 int offset = horizontalScrollBar()->minimum()
99 + (npaints % (horizontalScrollBar()->maximum() - horizontalScrollBar()->minimum()));
100 horizontalScrollBar()->setValue(offset);
101 break;
102 }
103 }
104 }
105
106 void ChipTester::populateScene()
107 {
108 scene = new QGraphicsScene;
109
110 QImage image(":/qt4logo.png");
111
112 // Populate scene
113 int xx = 0;
114 int nitems = 0;
115 for (int i = -1100; i < 1100; i += 110) {
116 ++xx;
117 int yy = 0;
118 for (int j = -700; j < 700; j += 70) {
119 ++yy;
120 qreal x = (i + 1100) / 2200.0;
121 qreal y = (j + 700) / 1400.0;
122
123 QColor color(image.pixel(int(image.width() * x), int(image.height() * y)));
124 QGraphicsItem *item = new Chip(color, xx, yy);
125 item->setPos(QPointF(i, j));
126 scene->addItem(item);
127
128 ++nitems;
129 }
130 }
131 }