Update copyright headers
[qt:qt.git] / demos / chip / view.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the demonstration applications 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 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.
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 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.
25 **
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.
29 **
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.
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "view.h"
43
44 #include <QtGui>
45 #ifndef QT_NO_OPENGL
46 #include <QtOpenGL>
47 #endif
48
49 #include <qmath.h>
50
51 void GraphicsView::wheelEvent(QWheelEvent *e)
52 {
53     if (e->modifiers() & Qt::ControlModifier) {
54         if (e->delta() > 0)
55             view->zoomIn(6);
56         else
57             view->zoomOut(6);
58         e->accept();
59     } else {
60         QGraphicsView::wheelEvent(e);
61     }
62 }
63
64 View::View(const QString &name, QWidget *parent)
65     : QFrame(parent)
66 {
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);
74
75     int size = style()->pixelMetric(QStyle::PM_ToolBarIconSize);
76     QSize iconSize(size, size);
77
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);
95
96     // Zoom slider layout
97     QVBoxLayout *zoomSliderLayout = new QVBoxLayout;
98     zoomSliderLayout->addWidget(zoomInIcon);
99     zoomSliderLayout->addWidget(zoomSlider);
100     zoomSliderLayout->addWidget(zoomOutIcon);
101
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);
114
115     // Rotate slider layout
116     QHBoxLayout *rotateSliderLayout = new QHBoxLayout;
117     rotateSliderLayout->addWidget(rotateLeftIcon);
118     rotateSliderLayout->addWidget(rotateSlider);
119     rotateSliderLayout->addWidget(rotateRightIcon);
120
121     resetButton = new QToolButton;
122     resetButton->setText(tr("0"));
123     resetButton->setEnabled(false);
124
125     // Label layout
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);
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     QButtonGroup *pointerModeGroup = new QButtonGroup;
153     pointerModeGroup->setExclusive(true);
154     pointerModeGroup->addButton(selectModeButton);
155     pointerModeGroup->addButton(dragModeButton);
156
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);
166
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);
174
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()));
189
190     setupMatrix();
191 }
192
193 QGraphicsView *View::view() const
194 {
195     return static_cast<QGraphicsView *>(graphicsView);
196 }
197
198 void View::resetView()
199 {
200     zoomSlider->setValue(250);
201     rotateSlider->setValue(0);
202     setupMatrix();
203     graphicsView->ensureVisible(QRectF(0, 0, 0, 0));
204
205     resetButton->setEnabled(false);
206 }
207
208 void View::setResetButtonEnabled()
209 {
210     resetButton->setEnabled(true);
211 }
212
213 void View::setupMatrix()
214 {
215     qreal scale = qPow(qreal(2), (zoomSlider->value() - 250) / qreal(50));
216
217     QMatrix matrix;
218     matrix.scale(scale, scale);
219     matrix.rotate(rotateSlider->value());
220
221     graphicsView->setMatrix(matrix);
222     setResetButtonEnabled();
223 }
224
225 void View::togglePointerMode()
226 {
227     graphicsView->setDragMode(selectModeButton->isChecked()
228                               ? QGraphicsView::RubberBandDrag
229                               : QGraphicsView::ScrollHandDrag);
230     graphicsView->setInteractive(selectModeButton->isChecked());
231 }
232
233 void View::toggleOpenGL()
234 {
235 #ifndef QT_NO_OPENGL
236     graphicsView->setViewport(openGlButton->isChecked() ? new QGLWidget(QGLFormat(QGL::SampleBuffers)) : new QWidget);
237 #endif
238 }
239
240 void View::toggleAntialiasing()
241 {
242     graphicsView->setRenderHint(QPainter::Antialiasing, antialiasButton->isChecked());
243 }
244
245 void View::print()
246 {
247 #ifndef QT_NO_PRINTER
248     QPrinter printer;
249     QPrintDialog dialog(&printer, this);
250     if (dialog.exec() == QDialog::Accepted) {
251         QPainter painter(&printer);
252         graphicsView->render(&painter);
253     }
254 #endif
255 }
256
257 void View::zoomIn(int level)
258 {
259     zoomSlider->setValue(zoomSlider->value() + level);
260 }
261
262 void View::zoomOut(int level)
263 {
264     zoomSlider->setValue(zoomSlider->value() - level);
265 }
266
267 void View::rotateLeft()
268 {
269     rotateSlider->setValue(rotateSlider->value() - 10);
270 }
271
272 void View::rotateRight()
273 {
274     rotateSlider->setValue(rotateSlider->value() + 10);
275 }
276