1 /****************************************************************************
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
6 ** This file is part of the demonstration applications of the Qt Toolkit.
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 The Qt Company. For licensing terms
14 ** and conditions see http://www.qt.io/terms-conditions. For further
15 ** information use the contact form at http://www.qt.io/contact-us.
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 or version 3 as published by the Free
20 ** Software Foundation and appearing in the file LICENSE.LGPLv21 and
21 ** LICENSE.LGPLv3 included in the packaging of this file. Please review the
22 ** following information to ensure the GNU Lesser General Public License
23 ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
24 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
26 ** As a special exception, The Qt Company gives you certain additional
27 ** rights. These rights are described in The Qt Company LGPL Exception
28 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
30 ** GNU General Public License Usage
31 ** Alternatively, this file may be used under the terms of the GNU
32 ** General Public License version 3.0 as published by the Free Software
33 ** Foundation and appearing in the file LICENSE.GPL included in the
34 ** packaging of this file. Please review the following information to
35 ** ensure the GNU General Public License version 3.0 requirements will be
36 ** met: http://www.gnu.org/copyleft/gpl.html.
40 ****************************************************************************/
51 void GraphicsView::wheelEvent(QWheelEvent *e)
53 if (e->modifiers() & Qt::ControlModifier) {
60 QGraphicsView::wheelEvent(e);
64 View::View(const QString &name, QWidget *parent)
67 setFrameStyle(Sunken | StyledPanel);
68 graphicsView = new GraphicsView(this);
69 graphicsView->setRenderHint(QPainter::Antialiasing, false);
70 graphicsView->setDragMode(QGraphicsView::RubberBandDrag);
71 graphicsView->setOptimizationFlags(QGraphicsView::DontSavePainterState);
72 graphicsView->setViewportUpdateMode(QGraphicsView::SmartViewportUpdate);
73 graphicsView->setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
75 int size = style()->pixelMetric(QStyle::PM_ToolBarIconSize);
76 QSize iconSize(size, size);
78 QToolButton *zoomInIcon = new QToolButton;
79 zoomInIcon->setAutoRepeat(true);
80 zoomInIcon->setAutoRepeatInterval(33);
81 zoomInIcon->setAutoRepeatDelay(0);
82 zoomInIcon->setIcon(QPixmap(":/zoomin.png"));
83 zoomInIcon->setIconSize(iconSize);
84 QToolButton *zoomOutIcon = new QToolButton;
85 zoomOutIcon->setAutoRepeat(true);
86 zoomOutIcon->setAutoRepeatInterval(33);
87 zoomOutIcon->setAutoRepeatDelay(0);
88 zoomOutIcon->setIcon(QPixmap(":/zoomout.png"));
89 zoomOutIcon->setIconSize(iconSize);
90 zoomSlider = new QSlider;
91 zoomSlider->setMinimum(0);
92 zoomSlider->setMaximum(500);
93 zoomSlider->setValue(250);
94 zoomSlider->setTickPosition(QSlider::TicksRight);
97 QVBoxLayout *zoomSliderLayout = new QVBoxLayout;
98 zoomSliderLayout->addWidget(zoomInIcon);
99 zoomSliderLayout->addWidget(zoomSlider);
100 zoomSliderLayout->addWidget(zoomOutIcon);
102 QToolButton *rotateLeftIcon = new QToolButton;
103 rotateLeftIcon->setIcon(QPixmap(":/rotateleft.png"));
104 rotateLeftIcon->setIconSize(iconSize);
105 QToolButton *rotateRightIcon = new QToolButton;
106 rotateRightIcon->setIcon(QPixmap(":/rotateright.png"));
107 rotateRightIcon->setIconSize(iconSize);
108 rotateSlider = new QSlider;
109 rotateSlider->setOrientation(Qt::Horizontal);
110 rotateSlider->setMinimum(-360);
111 rotateSlider->setMaximum(360);
112 rotateSlider->setValue(0);
113 rotateSlider->setTickPosition(QSlider::TicksBelow);
115 // Rotate slider layout
116 QHBoxLayout *rotateSliderLayout = new QHBoxLayout;
117 rotateSliderLayout->addWidget(rotateLeftIcon);
118 rotateSliderLayout->addWidget(rotateSlider);
119 rotateSliderLayout->addWidget(rotateRightIcon);
121 resetButton = new QToolButton;
122 resetButton->setText(tr("0"));
123 resetButton->setEnabled(false);
126 QHBoxLayout *labelLayout = new QHBoxLayout;
127 label = new QLabel(name);
128 label2 = new QLabel(tr("Pointer Mode"));
129 selectModeButton = new QToolButton;
130 selectModeButton->setText(tr("Select"));
131 selectModeButton->setCheckable(true);
132 selectModeButton->setChecked(true);
133 dragModeButton = new QToolButton;
134 dragModeButton->setText(tr("Drag"));
135 dragModeButton->setCheckable(true);
136 dragModeButton->setChecked(false);
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);
145 openGlButton->setEnabled(QGLFormat::hasOpenGL());
147 openGlButton->setEnabled(false);
149 printButton = new QToolButton;
150 printButton->setIcon(QIcon(QPixmap(":/fileprint.png")));
152 QButtonGroup *pointerModeGroup = new QButtonGroup;
153 pointerModeGroup->setExclusive(true);
154 pointerModeGroup->addButton(selectModeButton);
155 pointerModeGroup->addButton(dragModeButton);
157 labelLayout->addWidget(label);
158 labelLayout->addStretch();
159 labelLayout->addWidget(label2);
160 labelLayout->addWidget(selectModeButton);
161 labelLayout->addWidget(dragModeButton);
162 labelLayout->addStretch();
163 labelLayout->addWidget(antialiasButton);
164 labelLayout->addWidget(openGlButton);
165 labelLayout->addWidget(printButton);
167 QGridLayout *topLayout = new QGridLayout;
168 topLayout->addLayout(labelLayout, 0, 0);
169 topLayout->addWidget(graphicsView, 1, 0);
170 topLayout->addLayout(zoomSliderLayout, 1, 1);
171 topLayout->addLayout(rotateSliderLayout, 2, 0);
172 topLayout->addWidget(resetButton, 2, 1);
173 setLayout(topLayout);
175 connect(resetButton, SIGNAL(clicked()), this, SLOT(resetView()));
176 connect(zoomSlider, SIGNAL(valueChanged(int)), this, SLOT(setupMatrix()));
177 connect(rotateSlider, SIGNAL(valueChanged(int)), this, SLOT(setupMatrix()));
178 connect(graphicsView->verticalScrollBar(), SIGNAL(valueChanged(int)), this, SLOT(setResetButtonEnabled()));
179 connect(graphicsView->horizontalScrollBar(), SIGNAL(valueChanged(int)), this, SLOT(setResetButtonEnabled()));
180 connect(selectModeButton, SIGNAL(toggled(bool)), this, SLOT(togglePointerMode()));
181 connect(dragModeButton, SIGNAL(toggled(bool)), this, SLOT(togglePointerMode()));
182 connect(antialiasButton, SIGNAL(toggled(bool)), this, SLOT(toggleAntialiasing()));
183 connect(openGlButton, SIGNAL(toggled(bool)), this, SLOT(toggleOpenGL()));
184 connect(rotateLeftIcon, SIGNAL(clicked()), this, SLOT(rotateLeft()));
185 connect(rotateRightIcon, SIGNAL(clicked()), this, SLOT(rotateRight()));
186 connect(zoomInIcon, SIGNAL(clicked()), this, SLOT(zoomIn()));
187 connect(zoomOutIcon, SIGNAL(clicked()), this, SLOT(zoomOut()));
188 connect(printButton, SIGNAL(clicked()), this, SLOT(print()));
193 QGraphicsView *View::view() const
195 return static_cast<QGraphicsView *>(graphicsView);
198 void View::resetView()
200 zoomSlider->setValue(250);
201 rotateSlider->setValue(0);
203 graphicsView->ensureVisible(QRectF(0, 0, 0, 0));
205 resetButton->setEnabled(false);
208 void View::setResetButtonEnabled()
210 resetButton->setEnabled(true);
213 void View::setupMatrix()
215 qreal scale = qPow(qreal(2), (zoomSlider->value() - 250) / qreal(50));
218 matrix.scale(scale, scale);
219 matrix.rotate(rotateSlider->value());
221 graphicsView->setMatrix(matrix);
222 setResetButtonEnabled();
225 void View::togglePointerMode()
227 graphicsView->setDragMode(selectModeButton->isChecked()
228 ? QGraphicsView::RubberBandDrag
229 : QGraphicsView::ScrollHandDrag);
230 graphicsView->setInteractive(selectModeButton->isChecked());
233 void View::toggleOpenGL()
236 graphicsView->setViewport(openGlButton->isChecked() ? new QGLWidget(QGLFormat(QGL::SampleBuffers)) : new QWidget);
240 void View::toggleAntialiasing()
242 graphicsView->setRenderHint(QPainter::Antialiasing, antialiasButton->isChecked());
247 #ifndef QT_NO_PRINTER
249 QPrintDialog dialog(&printer, this);
250 if (dialog.exec() == QDialog::Accepted) {
251 QPainter painter(&printer);
252 graphicsView->render(&painter);
257 void View::zoomIn(int level)
259 zoomSlider->setValue(zoomSlider->value() + level);
262 void View::zoomOut(int level)
264 zoomSlider->setValue(zoomSlider->value() - level);
267 void View::rotateLeft()
269 rotateSlider->setValue(rotateSlider->value() - 10);
272 void View::rotateRight()
274 rotateSlider->setValue(rotateSlider->value() + 10);