| 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 |
|
| 29 |
#include "view.h" |
| 30 |
|
| 31 |
#include <QtGui> |
| 32 |
|
| 33 |
#include "valgrind/callgrind.h" |
| 34 |
|
| 35 |
#ifndef QT_NO_OPENGL |
| 36 |
#include <QtOpenGL> |
| 37 |
#endif |
| 38 |
|
| 39 |
#include <qmath.h> |
| 40 |
|
| 41 |
class CountView : public QGraphicsView |
| 42 |
{ |
| 43 |
protected: |
| 44 |
void paintEvent(QPaintEvent *event) |
| 45 |
{ |
| 46 |
static int n = 0; |
| 47 |
if (n) |
| 48 |
CALLGRIND_START_INSTRUMENTATION |
| 49 |
QGraphicsView::paintEvent(event); |
| 50 |
if (n) |
| 51 |
CALLGRIND_STOP_INSTRUMENTATION |
| 52 |
if (++n == 500) |
| 53 |
qApp->quit(); |
| 54 |
} |
| 55 |
}; |
| 56 |
|
| 57 |
View::View(const QString &name, QWidget *parent) |
| 58 |
: QFrame(parent) |
| 59 |
{ |
| 60 |
setFrameStyle(Sunken | StyledPanel); |
| 61 |
graphicsView = new CountView; |
| 62 |
graphicsView->setRenderHint(QPainter::Antialiasing, false); |
| 63 |
graphicsView->setDragMode(QGraphicsView::RubberBandDrag); |
| 64 |
graphicsView->setViewportUpdateMode(QGraphicsView::SmartViewportUpdate); |
| 65 |
|
| 66 |
int size = style()->pixelMetric(QStyle::PM_ToolBarIconSize); |
| 67 |
QSize iconSize(size, size); |
| 68 |
|
| 69 |
QToolButton *zoomInIcon = new QToolButton; |
| 70 |
zoomInIcon->setAutoRepeat(true); |
| 71 |
zoomInIcon->setAutoRepeatInterval(33); |
| 72 |
zoomInIcon->setAutoRepeatDelay(0); |
| 73 |
zoomInIcon->setIcon(QPixmap(":/zoomin.png")); |
| 74 |
zoomInIcon->setIconSize(iconSize); |
| 75 |
QToolButton *zoomOutIcon = new QToolButton; |
| 76 |
zoomOutIcon->setAutoRepeat(true); |
| 77 |
zoomOutIcon->setAutoRepeatInterval(33); |
| 78 |
zoomOutIcon->setAutoRepeatDelay(0); |
| 79 |
zoomOutIcon->setIcon(QPixmap(":/zoomout.png")); |
| 80 |
zoomOutIcon->setIconSize(iconSize); |
| 81 |
zoomSlider = new QSlider; |
| 82 |
zoomSlider->setMinimum(0); |
| 83 |
zoomSlider->setMaximum(500); |
| 84 |
zoomSlider->setValue(250); |
| 85 |
zoomSlider->setTickPosition(QSlider::TicksRight); |
| 86 |
|
| 87 |
// Zoom slider layout |
| 88 |
QVBoxLayout *zoomSliderLayout = new QVBoxLayout; |
| 89 |
zoomSliderLayout->addWidget(zoomInIcon); |
| 90 |
zoomSliderLayout->addWidget(zoomSlider); |
| 91 |
zoomSliderLayout->addWidget(zoomOutIcon); |
| 92 |
|
| 93 |
QToolButton *rotateLeftIcon = new QToolButton; |
| 94 |
rotateLeftIcon->setIcon(QPixmap(":/rotateleft.png")); |
| 95 |
rotateLeftIcon->setIconSize(iconSize); |
| 96 |
QToolButton *rotateRightIcon = new QToolButton; |
| 97 |
rotateRightIcon->setIcon(QPixmap(":/rotateright.png")); |
| 98 |
rotateRightIcon->setIconSize(iconSize); |
| 99 |
rotateSlider = new QSlider; |
| 100 |
rotateSlider->setOrientation(Qt::Horizontal); |
| 101 |
rotateSlider->setMinimum(-360); |
| 102 |
rotateSlider->setMaximum(360); |
| 103 |
rotateSlider->setValue(0); |
| 104 |
rotateSlider->setTickPosition(QSlider::TicksBelow); |
| 105 |
|
| 106 |
// Rotate slider layout |
| 107 |
QHBoxLayout *rotateSliderLayout = new QHBoxLayout; |
| 108 |
rotateSliderLayout->addWidget(rotateLeftIcon); |
| 109 |
rotateSliderLayout->addWidget(rotateSlider); |
| 110 |
rotateSliderLayout->addWidget(rotateRightIcon); |
| 111 |
|
| 112 |
resetButton = new QToolButton; |
| 113 |
resetButton->setText(tr("0")); |
| 114 |
resetButton->setEnabled(false); |
| 115 |
|
| 116 |
// Label layout |
| 117 |
QHBoxLayout *labelLayout = new QHBoxLayout; |
| 118 |
label = new QLabel(name); |
| 119 |
antialiasButton = new QToolButton; |
| 120 |
antialiasButton->setText(tr("Antialiasing")); |
| 121 |
antialiasButton->setCheckable(true); |
| 122 |
antialiasButton->setChecked(false); |
| 123 |
openGlButton = new QToolButton; |
| 124 |
openGlButton->setText(tr("OpenGL")); |
| 125 |
openGlButton->setCheckable(true); |
| 126 |
#ifndef QT_NO_OPENGL |
| 127 |
openGlButton->setEnabled(QGLFormat::hasOpenGL()); |
| 128 |
#else |
| 129 |
openGlButton->setEnabled(false); |
| 130 |
#endif |
| 131 |
printButton = new QToolButton; |
| 132 |
printButton->setIcon(QIcon(QPixmap(":/fileprint.png"))); |
| 133 |
|
| 134 |
labelLayout->addWidget(label); |
| 135 |
labelLayout->addStretch(); |
| 136 |
labelLayout->addWidget(antialiasButton); |
| 137 |
labelLayout->addWidget(openGlButton); |
| 138 |
labelLayout->addWidget(printButton); |
| 139 |
|
| 140 |
QGridLayout *topLayout = new QGridLayout; |
| 141 |
topLayout->addLayout(labelLayout, 0, 0); |
| 142 |
topLayout->addWidget(graphicsView, 1, 0); |
| 143 |
topLayout->addLayout(zoomSliderLayout, 1, 1); |
| 144 |
topLayout->addLayout(rotateSliderLayout, 2, 0); |
| 145 |
topLayout->addWidget(resetButton, 2, 1); |
| 146 |
setLayout(topLayout); |
| 147 |
|
| 148 |
connect(resetButton, SIGNAL(clicked()), this, SLOT(resetView())); |
| 149 |
connect(zoomSlider, SIGNAL(valueChanged(int)), this, SLOT(setupMatrix())); |
| 150 |
connect(rotateSlider, SIGNAL(valueChanged(int)), this, SLOT(setupMatrix())); |
| 151 |
connect(graphicsView->verticalScrollBar(), SIGNAL(valueChanged(int)), this, SLOT(setResetButtonEnabled())); |
| 152 |
connect(graphicsView->horizontalScrollBar(), SIGNAL(valueChanged(int)), this, SLOT(setResetButtonEnabled())); |
| 153 |
connect(antialiasButton, SIGNAL(toggled(bool)), this, SLOT(toggleAntialiasing())); |
| 154 |
connect(openGlButton, SIGNAL(toggled(bool)), this, SLOT(toggleOpenGL())); |
| 155 |
connect(rotateLeftIcon, SIGNAL(clicked()), this, SLOT(rotateLeft())); |
| 156 |
connect(rotateRightIcon, SIGNAL(clicked()), this, SLOT(rotateRight())); |
| 157 |
connect(zoomInIcon, SIGNAL(clicked()), this, SLOT(zoomIn())); |
| 158 |
connect(zoomOutIcon, SIGNAL(clicked()), this, SLOT(zoomOut())); |
| 159 |
connect(printButton, SIGNAL(clicked()), this, SLOT(print())); |
| 160 |
|
| 161 |
setupMatrix(); |
| 162 |
|
| 163 |
startTimer(0); |
| 164 |
} |
| 165 |
|
| 166 |
QGraphicsView *View::view() const |
| 167 |
{ |
| 168 |
return graphicsView; |
| 169 |
} |
| 170 |
|
| 171 |
void View::resetView() |
| 172 |
{ |
| 173 |
zoomSlider->setValue(250); |
| 174 |
rotateSlider->setValue(0); |
| 175 |
setupMatrix(); |
| 176 |
graphicsView->ensureVisible(QRectF(0, 0, 0, 0)); |
| 177 |
|
| 178 |
resetButton->setEnabled(false); |
| 179 |
} |
| 180 |
|
| 181 |
void View::setResetButtonEnabled() |
| 182 |
{ |
| 183 |
resetButton->setEnabled(true); |
| 184 |
} |
| 185 |
|
| 186 |
void View::setupMatrix() |
| 187 |
{ |
| 188 |
qreal scale = qPow(qreal(2), (zoomSlider->value() - 250) / qreal(50)); |
| 189 |
|
| 190 |
QTransform matrix; |
| 191 |
matrix.scale(scale, scale); |
| 192 |
matrix.rotate(rotateSlider->value()); |
| 193 |
|
| 194 |
graphicsView->setMatrix(matrix); |
| 195 |
setResetButtonEnabled(); |
| 196 |
} |
| 197 |
|
| 198 |
void View::toggleOpenGL() |
| 199 |
{ |
| 200 |
#ifndef QT_NO_OPENGL |
| 201 |
graphicsView->setViewport(openGlButton->isChecked() ? new QGLWidget(QGLFormat(QGL::SampleBuffers)) : new QWidget); |
| 202 |
#endif |
| 203 |
} |
| 204 |
|
| 205 |
void View::toggleAntialiasing() |
| 206 |
{ |
| 207 |
graphicsView->setRenderHint(QPainter::Antialiasing, antialiasButton->isChecked()); |
| 208 |
} |
| 209 |
|
| 210 |
void View::print() |
| 211 |
{ |
| 212 |
#ifndef QT_NO_PRINTER |
| 213 |
QPrinter printer; |
| 214 |
QPrintDialog dialog(&printer, this); |
| 215 |
if (dialog.exec() == QDialog::Accepted) { |
| 216 |
QPainter painter(&printer); |
| 217 |
graphicsView->render(&painter); |
| 218 |
} |
| 219 |
#endif |
| 220 |
} |
| 221 |
|
| 222 |
void View::zoomIn() |
| 223 |
{ |
| 224 |
zoomSlider->setValue(zoomSlider->value() + 1); |
| 225 |
} |
| 226 |
|
| 227 |
void View::zoomOut() |
| 228 |
{ |
| 229 |
zoomSlider->setValue(zoomSlider->value() - 1); |
| 230 |
} |
| 231 |
|
| 232 |
void View::rotateLeft() |
| 233 |
{ |
| 234 |
rotateSlider->setValue(rotateSlider->value() - 10); |
| 235 |
} |
| 236 |
|
| 237 |
void View::rotateRight() |
| 238 |
{ |
| 239 |
rotateSlider->setValue(rotateSlider->value() + 10); |
| 240 |
} |
| 241 |
|
| 242 |
void View::timerEvent(QTimerEvent *) |
| 243 |
{ |
| 244 |
graphicsView->horizontalScrollBar()->setValue(graphicsView->horizontalScrollBar()->value() + 1); |
| 245 |
} |
| 246 |
|