~ubuntu-branches/ubuntu/wily/qtbase-opensource-src/wily

« back to all changes in this revision

Viewing changes to tests/benchmarks/gui/graphicsview/qgraphicsview/benchapps/chipTest/view.cpp

  • Committer: Package Import Robot
  • Author(s): Timo Jyrinki
  • Date: 2013-02-05 12:46:17 UTC
  • Revision ID: package-import@ubuntu.com-20130205124617-c8jouts182j002fx
Tags: upstream-5.0.1+dfsg
ImportĀ upstreamĀ versionĀ 5.0.1+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
 
4
** Contact: http://www.qt-project.org/legal
 
5
**
 
6
** This file is part of the test suite of the Qt Toolkit.
 
7
**
 
8
** $QT_BEGIN_LICENSE:LGPL$
 
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 Digia.  For licensing terms and
 
14
** conditions see http://qt.digia.com/licensing.  For further information
 
15
** use the contact form at http://qt.digia.com/contact-us.
 
16
**
 
17
** GNU Lesser General Public License Usage
 
18
** Alternatively, this file may be used under the terms of the GNU Lesser
 
19
** General Public License version 2.1 as published by the Free Software
 
20
** Foundation and appearing in the file LICENSE.LGPL included in the
 
21
** packaging of this file.  Please review the following information to
 
22
** ensure the GNU Lesser General Public License version 2.1 requirements
 
23
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
 
24
**
 
25
** In addition, as a special exception, Digia gives you certain additional
 
26
** rights.  These rights are described in the Digia Qt LGPL Exception
 
27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
 
28
**
 
29
** GNU General Public License Usage
 
30
** Alternatively, this file may be used under the terms of the GNU
 
31
** General Public License version 3.0 as published by the Free Software
 
32
** Foundation and appearing in the file LICENSE.GPL included in the
 
33
** packaging of this file.  Please review the following information to
 
34
** ensure the GNU General Public License version 3.0 requirements will be
 
35
** met: http://www.gnu.org/copyleft/gpl.html.
 
36
**
 
37
**
 
38
** $QT_END_LICENSE$
 
39
**
 
40
****************************************************************************/
 
41
 
 
42
#include "view.h"
 
43
 
 
44
#include <QtGui>
 
45
 
 
46
#ifdef Q_WS_WIN
 
47
#define CALLGRIND_START_INSTRUMENTATION  {}
 
48
#define CALLGRIND_STOP_INSTRUMENTATION   {}
 
49
#else
 
50
#include "valgrind/callgrind.h"
 
51
#endif
 
52
 
 
53
#ifndef QT_NO_OPENGL
 
54
#include <QtOpenGL>
 
55
#endif
 
56
 
 
57
#include <qmath.h>
 
58
 
 
59
class CountView : public QGraphicsView
 
60
{
 
61
protected:
 
62
    void paintEvent(QPaintEvent *event)
 
63
    {
 
64
        static int n = 0;
 
65
        if (n)
 
66
            CALLGRIND_START_INSTRUMENTATION
 
67
        QGraphicsView::paintEvent(event);
 
68
        if (n)
 
69
            CALLGRIND_STOP_INSTRUMENTATION
 
70
        if (++n == 500)
 
71
            qApp->quit();
 
72
    }
 
73
};
 
74
 
 
75
View::View(const QString &name, QWidget *parent)
 
76
    : QFrame(parent)
 
77
{
 
78
    setFrameStyle(Sunken | StyledPanel);
 
79
    graphicsView = new CountView;
 
80
    graphicsView->setRenderHint(QPainter::Antialiasing, false);
 
81
    graphicsView->setDragMode(QGraphicsView::RubberBandDrag);
 
82
    graphicsView->setViewportUpdateMode(QGraphicsView::SmartViewportUpdate);
 
83
 
 
84
    int size = style()->pixelMetric(QStyle::PM_ToolBarIconSize);
 
85
    QSize iconSize(size, size);
 
86
 
 
87
    QToolButton *zoomInIcon = new QToolButton;
 
88
    zoomInIcon->setAutoRepeat(true);
 
89
    zoomInIcon->setAutoRepeatInterval(33);
 
90
    zoomInIcon->setAutoRepeatDelay(0);
 
91
    zoomInIcon->setIcon(QPixmap(":/zoomin.png"));
 
92
    zoomInIcon->setIconSize(iconSize);
 
93
    QToolButton *zoomOutIcon = new QToolButton;
 
94
    zoomOutIcon->setAutoRepeat(true);
 
95
    zoomOutIcon->setAutoRepeatInterval(33);
 
96
    zoomOutIcon->setAutoRepeatDelay(0);
 
97
    zoomOutIcon->setIcon(QPixmap(":/zoomout.png"));
 
98
    zoomOutIcon->setIconSize(iconSize);
 
99
    zoomSlider = new QSlider;
 
100
    zoomSlider->setMinimum(0);
 
101
    zoomSlider->setMaximum(500);
 
102
    zoomSlider->setValue(250);
 
103
    zoomSlider->setTickPosition(QSlider::TicksRight);
 
104
 
 
105
    // Zoom slider layout
 
106
    QVBoxLayout *zoomSliderLayout = new QVBoxLayout;
 
107
    zoomSliderLayout->addWidget(zoomInIcon);
 
108
    zoomSliderLayout->addWidget(zoomSlider);
 
109
    zoomSliderLayout->addWidget(zoomOutIcon);
 
110
 
 
111
    QToolButton *rotateLeftIcon = new QToolButton;
 
112
    rotateLeftIcon->setIcon(QPixmap(":/rotateleft.png"));
 
113
    rotateLeftIcon->setIconSize(iconSize);
 
114
    QToolButton *rotateRightIcon = new QToolButton;
 
115
    rotateRightIcon->setIcon(QPixmap(":/rotateright.png"));
 
116
    rotateRightIcon->setIconSize(iconSize);
 
117
    rotateSlider = new QSlider;
 
118
    rotateSlider->setOrientation(Qt::Horizontal);
 
119
    rotateSlider->setMinimum(-360);
 
120
    rotateSlider->setMaximum(360);
 
121
    rotateSlider->setValue(0);
 
122
    rotateSlider->setTickPosition(QSlider::TicksBelow);
 
123
 
 
124
    // Rotate slider layout
 
125
    QHBoxLayout *rotateSliderLayout = new QHBoxLayout;
 
126
    rotateSliderLayout->addWidget(rotateLeftIcon);
 
127
    rotateSliderLayout->addWidget(rotateSlider);
 
128
    rotateSliderLayout->addWidget(rotateRightIcon);
 
129
 
 
130
    resetButton = new QToolButton;
 
131
    resetButton->setText(tr("0"));
 
132
    resetButton->setEnabled(false);
 
133
 
 
134
    // Label layout
 
135
    QHBoxLayout *labelLayout = new QHBoxLayout;
 
136
    label = new QLabel(name);
 
137
    antialiasButton = new QToolButton;
 
138
    antialiasButton->setText(tr("Antialiasing"));
 
139
    antialiasButton->setCheckable(true);
 
140
    antialiasButton->setChecked(false);
 
141
    openGlButton = new QToolButton;
 
142
    openGlButton->setText(tr("OpenGL"));
 
143
    openGlButton->setCheckable(true);
 
144
#ifndef QT_NO_OPENGL
 
145
    openGlButton->setEnabled(QGLFormat::hasOpenGL());
 
146
#else
 
147
    openGlButton->setEnabled(false);
 
148
#endif
 
149
    printButton = new QToolButton;
 
150
    printButton->setIcon(QIcon(QPixmap(":/fileprint.png")));
 
151
 
 
152
    labelLayout->addWidget(label);
 
153
    labelLayout->addStretch();
 
154
    labelLayout->addWidget(antialiasButton);
 
155
    labelLayout->addWidget(openGlButton);
 
156
    labelLayout->addWidget(printButton);
 
157
 
 
158
    QGridLayout *topLayout = new QGridLayout;
 
159
    topLayout->addLayout(labelLayout, 0, 0);
 
160
    topLayout->addWidget(graphicsView, 1, 0);
 
161
    topLayout->addLayout(zoomSliderLayout, 1, 1);
 
162
    topLayout->addLayout(rotateSliderLayout, 2, 0);
 
163
    topLayout->addWidget(resetButton, 2, 1);
 
164
    setLayout(topLayout);
 
165
 
 
166
    connect(resetButton, SIGNAL(clicked()), this, SLOT(resetView()));
 
167
    connect(zoomSlider, SIGNAL(valueChanged(int)), this, SLOT(setupMatrix()));
 
168
    connect(rotateSlider, SIGNAL(valueChanged(int)), this, SLOT(setupMatrix()));
 
169
    connect(graphicsView->verticalScrollBar(), SIGNAL(valueChanged(int)), this, SLOT(setResetButtonEnabled()));
 
170
    connect(graphicsView->horizontalScrollBar(), SIGNAL(valueChanged(int)), this, SLOT(setResetButtonEnabled()));
 
171
    connect(antialiasButton, SIGNAL(toggled(bool)), this, SLOT(toggleAntialiasing()));
 
172
    connect(openGlButton, SIGNAL(toggled(bool)), this, SLOT(toggleOpenGL()));
 
173
    connect(rotateLeftIcon, SIGNAL(clicked()), this, SLOT(rotateLeft()));
 
174
    connect(rotateRightIcon, SIGNAL(clicked()), this, SLOT(rotateRight()));
 
175
    connect(zoomInIcon, SIGNAL(clicked()), this, SLOT(zoomIn()));
 
176
    connect(zoomOutIcon, SIGNAL(clicked()), this, SLOT(zoomOut()));
 
177
    connect(printButton, SIGNAL(clicked()), this, SLOT(print()));
 
178
 
 
179
    setupMatrix();
 
180
 
 
181
    startTimer(0);
 
182
}
 
183
 
 
184
QGraphicsView *View::view() const
 
185
{
 
186
    return graphicsView;
 
187
}
 
188
 
 
189
void View::resetView()
 
190
{
 
191
    zoomSlider->setValue(250);
 
192
    rotateSlider->setValue(0);
 
193
    setupMatrix();
 
194
    graphicsView->ensureVisible(QRectF(0, 0, 0, 0));
 
195
 
 
196
    resetButton->setEnabled(false);
 
197
}
 
198
 
 
199
void View::setResetButtonEnabled()
 
200
{
 
201
    resetButton->setEnabled(true);
 
202
}
 
203
 
 
204
void View::setupMatrix()
 
205
{
 
206
    qreal scale = qPow(qreal(2), (zoomSlider->value() - 250) / qreal(50));
 
207
 
 
208
    QMatrix matrix;
 
209
    matrix.scale(scale, scale);
 
210
    matrix.rotate(rotateSlider->value());
 
211
 
 
212
    graphicsView->setMatrix(matrix);
 
213
    setResetButtonEnabled();
 
214
}
 
215
 
 
216
void View::toggleOpenGL()
 
217
{
 
218
#ifndef QT_NO_OPENGL
 
219
    graphicsView->setViewport(openGlButton->isChecked() ? new QGLWidget(QGLFormat(QGL::SampleBuffers)) : new QWidget);
 
220
#endif
 
221
}
 
222
 
 
223
void View::toggleAntialiasing()
 
224
{
 
225
    graphicsView->setRenderHint(QPainter::Antialiasing, antialiasButton->isChecked());
 
226
}
 
227
 
 
228
void View::print()
 
229
{
 
230
#ifndef QT_NO_PRINTER
 
231
    QPrinter printer;
 
232
    QPrintDialog dialog(&printer, this);
 
233
    if (dialog.exec() == QDialog::Accepted) {
 
234
        QPainter painter(&printer);
 
235
        graphicsView->render(&painter);
 
236
    }
 
237
#endif
 
238
}
 
239
 
 
240
void View::zoomIn()
 
241
{
 
242
    zoomSlider->setValue(zoomSlider->value() + 1);
 
243
}
 
244
 
 
245
void View::zoomOut()
 
246
{
 
247
    zoomSlider->setValue(zoomSlider->value() - 1);
 
248
}
 
249
 
 
250
void View::rotateLeft()
 
251
{
 
252
    rotateSlider->setValue(rotateSlider->value() - 10);
 
253
}
 
254
 
 
255
void View::rotateRight()
 
256
{
 
257
    rotateSlider->setValue(rotateSlider->value() + 10);
 
258
}
 
259
 
 
260
void View::timerEvent(QTimerEvent *)
 
261
{
 
262
    graphicsView->horizontalScrollBar()->setValue(graphicsView->horizontalScrollBar()->value() + 1);
 
263
}
 
264