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 demonstration applications of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:BSD$
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** BSD License Usage
18** Alternatively, you may use this file under the terms of the BSD license
19** as follows:
20**
21** "Redistribution and use in source and binary forms, with or without
22** modification, are permitted provided that the following conditions are
23** met:
24** * Redistributions of source code must retain the above copyright
25** notice, this list of conditions and the following disclaimer.
26** * Redistributions in binary form must reproduce the above copyright
27** notice, this list of conditions and the following disclaimer in
28** the documentation and/or other materials provided with the
29** distribution.
30** * Neither the name of The Qt Company Ltd nor the names of its
31** contributors may be used to endorse or promote products derived
32** from this software without specific prior written permission.
33**
34**
35** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46**
47** $QT_END_LICENSE$
48**
49****************************************************************************/
50
51#include "view.h"
52
53#if defined(QT_PRINTSUPPORT_LIB)
54#include <QtPrintSupport/qtprintsupportglobal.h>
55#if QT_CONFIG(printdialog)
56#include <QPrinter>
57#include <QPrintDialog>
58#endif
59#endif
60#include <QtWidgets>
61#include <QtMath>
62
63#if QT_CONFIG(wheelevent)
64void GraphicsView::wheelEvent(QWheelEvent *e)
65{
66 if (e->modifiers() & Qt::ControlModifier) {
67 if (e->angleDelta().y() > 0)
68 view->zoomIn(6);
69 else
70 view->zoomOut(6);
71 e->accept();
72 } else {
73 QGraphicsView::wheelEvent(e);
74 }
75}
76#endif
77
78View::View(const QString &name, QWidget *parent)
79 : QFrame(parent)
80{
81 setFrameStyle(Sunken | StyledPanel);
82 graphicsView = new GraphicsView(this);
83 graphicsView->setRenderHint(QPainter::Antialiasing, false);
84 graphicsView->setDragMode(QGraphicsView::RubberBandDrag);
85 graphicsView->setOptimizationFlags(QGraphicsView::DontSavePainterState);
86 graphicsView->setViewportUpdateMode(QGraphicsView::SmartViewportUpdate);
87 graphicsView->setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
88
89 int size = style()->pixelMetric(QStyle::PM_ToolBarIconSize);
90 QSize iconSize(size, size);
91
92 QToolButton *zoomInIcon = new QToolButton;
93 zoomInIcon->setAutoRepeat(true);
94 zoomInIcon->setAutoRepeatInterval(33);
95 zoomInIcon->setAutoRepeatDelay(0);
96 zoomInIcon->setIcon(QPixmap(":/zoomin.png"));
97 zoomInIcon->setIconSize(iconSize);
98 QToolButton *zoomOutIcon = new QToolButton;
99 zoomOutIcon->setAutoRepeat(true);
100 zoomOutIcon->setAutoRepeatInterval(33);
101 zoomOutIcon->setAutoRepeatDelay(0);
102 zoomOutIcon->setIcon(QPixmap(":/zoomout.png"));
103 zoomOutIcon->setIconSize(iconSize);
104 zoomSlider = new QSlider;
105 zoomSlider->setMinimum(0);
106 zoomSlider->setMaximum(500);
107 zoomSlider->setValue(250);
108 zoomSlider->setTickPosition(QSlider::TicksRight);
109
110 // Zoom slider layout
111 QVBoxLayout *zoomSliderLayout = new QVBoxLayout;
112 zoomSliderLayout->addWidget(zoomInIcon);
113 zoomSliderLayout->addWidget(zoomSlider);
114 zoomSliderLayout->addWidget(zoomOutIcon);
115
116 QToolButton *rotateLeftIcon = new QToolButton;
117 rotateLeftIcon->setIcon(QPixmap(":/rotateleft.png"));
118 rotateLeftIcon->setIconSize(iconSize);
119 QToolButton *rotateRightIcon = new QToolButton;
120 rotateRightIcon->setIcon(QPixmap(":/rotateright.png"));
121 rotateRightIcon->setIconSize(iconSize);
122 rotateSlider = new QSlider;
123 rotateSlider->setOrientation(Qt::Horizontal);
124 rotateSlider->setMinimum(-360);
125 rotateSlider->setMaximum(360);
126 rotateSlider->setValue(0);
127 rotateSlider->setTickPosition(QSlider::TicksBelow);
128
129 // Rotate slider layout
130 QHBoxLayout *rotateSliderLayout = new QHBoxLayout;
131 rotateSliderLayout->addWidget(rotateLeftIcon);
132 rotateSliderLayout->addWidget(rotateSlider);
133 rotateSliderLayout->addWidget(rotateRightIcon);
134
135 resetButton = new QToolButton;
136 resetButton->setText(tr("0"));
137 resetButton->setEnabled(false);
138
139 // Label layout
140 QHBoxLayout *labelLayout = new QHBoxLayout;
141 label = new QLabel(name);
142 label2 = new QLabel(tr("Pointer Mode"));
143 selectModeButton = new QToolButton;
144 selectModeButton->setText(tr("Select"));
145 selectModeButton->setCheckable(true);
146 selectModeButton->setChecked(true);
147 dragModeButton = new QToolButton;
148 dragModeButton->setText(tr("Drag"));
149 dragModeButton->setCheckable(true);
150 dragModeButton->setChecked(false);
151 antialiasButton = new QToolButton;
152 antialiasButton->setText(tr("Antialiasing"));
153 antialiasButton->setCheckable(true);
154 antialiasButton->setChecked(false);
155 printButton = new QToolButton;
156 printButton->setIcon(QIcon(QPixmap(":/fileprint.png")));
157
158 QButtonGroup *pointerModeGroup = new QButtonGroup(this);
159 pointerModeGroup->setExclusive(true);
160 pointerModeGroup->addButton(selectModeButton);
161 pointerModeGroup->addButton(dragModeButton);
162
163 labelLayout->addWidget(label);
164 labelLayout->addStretch();
165 labelLayout->addWidget(label2);
166 labelLayout->addWidget(selectModeButton);
167 labelLayout->addWidget(dragModeButton);
168 labelLayout->addStretch();
169 labelLayout->addWidget(antialiasButton);
170 labelLayout->addWidget(printButton);
171
172 QGridLayout *topLayout = new QGridLayout;
173 topLayout->addLayout(labelLayout, 0, 0);
174 topLayout->addWidget(graphicsView, 1, 0);
175 topLayout->addLayout(zoomSliderLayout, 1, 1);
176 topLayout->addLayout(rotateSliderLayout, 2, 0);
177 topLayout->addWidget(resetButton, 2, 1);
178 setLayout(topLayout);
179
180 connect(resetButton, &QAbstractButton::clicked, this, &View::resetView);
181 connect(zoomSlider, &QAbstractSlider::valueChanged, this, &View::setupMatrix);
182 connect(rotateSlider, &QAbstractSlider::valueChanged, this, &View::setupMatrix);
183 connect(graphicsView->verticalScrollBar(), &QAbstractSlider::valueChanged,
184 this, &View::setResetButtonEnabled);
185 connect(graphicsView->horizontalScrollBar(), &QAbstractSlider::valueChanged,
186 this, &View::setResetButtonEnabled);
187 connect(selectModeButton, &QAbstractButton::toggled, this, &View::togglePointerMode);
188 connect(dragModeButton, &QAbstractButton::toggled, this, &View::togglePointerMode);
189 connect(antialiasButton, &QAbstractButton::toggled, this, &View::toggleAntialiasing);
190 connect(rotateLeftIcon, &QAbstractButton::clicked, this, &View::rotateLeft);
191 connect(rotateRightIcon, &QAbstractButton::clicked, this, &View::rotateRight);
192 connect(zoomInIcon, &QAbstractButton::clicked, this, &View::zoomIn);
193 connect(zoomOutIcon, &QAbstractButton::clicked, this, &View::zoomOut);
194 connect(printButton, &QAbstractButton::clicked, this, &View::print);
195
196 setupMatrix();
197}
198
199QGraphicsView *View::view() const
200{
201 return static_cast<QGraphicsView *>(graphicsView);
202}
203
204void View::resetView()
205{
206 zoomSlider->setValue(250);
207 rotateSlider->setValue(0);
208 setupMatrix();
209 graphicsView->ensureVisible(QRectF(0, 0, 0, 0));
210
211 resetButton->setEnabled(false);
212}
213
214void View::setResetButtonEnabled()
215{
216 resetButton->setEnabled(true);
217}
218
219void View::setupMatrix()
220{
221 qreal scale = qPow(qreal(2), (zoomSlider->value() - 250) / qreal(50));
222
223 QTransform matrix;
224 matrix.scale(scale, scale);
225 matrix.rotate(rotateSlider->value());
226
227 graphicsView->setTransform(matrix);
228 setResetButtonEnabled();
229}
230
231void View::togglePointerMode()
232{
233 graphicsView->setDragMode(selectModeButton->isChecked()
234 ? QGraphicsView::RubberBandDrag
235 : QGraphicsView::ScrollHandDrag);
236 graphicsView->setInteractive(selectModeButton->isChecked());
237}
238
239void View::toggleAntialiasing()
240{
241 graphicsView->setRenderHint(QPainter::Antialiasing, antialiasButton->isChecked());
242}
243
244void View::print()
245{
246#if defined(QT_PRINTSUPPORT_LIB) && QT_CONFIG(printdialog)
247 QPrinter printer;
248 QPrintDialog dialog(&printer, this);
249 if (dialog.exec() == QDialog::Accepted) {
250 QPainter painter(&printer);
251 graphicsView->render(&painter);
252 }
253#endif
254}
255
256void View::zoomIn(int level)
257{
258 zoomSlider->setValue(zoomSlider->value() + level);
259}
260
261void View::zoomOut(int level)
262{
263 zoomSlider->setValue(zoomSlider->value() - level);
264}
265
266void View::rotateLeft()
267{
268 rotateSlider->setValue(rotateSlider->value() - 10);
269}
270
271void View::rotateRight()
272{
273 rotateSlider->setValue(rotateSlider->value() + 10);
274}
275