QtBase  v6.3.1
view.cpp
Go to the documentation of this file.
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)
64 void GraphicsView::wheelEvent(QWheelEvent *e)
65 {
66  if (e->modifiers() & Qt::ControlModifier) {
67  if (e->angleDelta().y() > 0)
68  view->zoomInBy(6);
69  else
70  view->zoomOutBy(6);
71  e->accept();
72  } else {
73  QGraphicsView::wheelEvent(e);
74  }
75 }
76 #endif
77 
79  : QFrame(parent)
80 {
82  graphicsView = new GraphicsView(this);
83  graphicsView->setRenderHint(QPainter::Antialiasing, false);
88 
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);
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 
200 {
201  return static_cast<QGraphicsView *>(graphicsView);
202 }
203 
204 void 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 
214 void View::setResetButtonEnabled()
215 {
216  resetButton->setEnabled(true);
217 }
218 
219 void View::setupMatrix()
220 {
221  qreal scale = qPow(qreal(2), (zoomSlider->value() - 250) / qreal(50));
222 
224  matrix.scale(scale, scale);
225  matrix.rotate(rotateSlider->value());
226 
227  graphicsView->setTransform(matrix);
228  setResetButtonEnabled();
229 }
230 
231 void View::togglePointerMode()
232 {
233  graphicsView->setDragMode(selectModeButton->isChecked()
236  graphicsView->setInteractive(selectModeButton->isChecked());
237 }
238 
239 void View::toggleAntialiasing()
240 {
241  graphicsView->setRenderHint(QPainter::Antialiasing, antialiasButton->isChecked());
242 }
243 
244 void 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 
257 {
258  zoomSlider->setValue(zoomSlider->value() + 1);
259 }
260 
262 {
263  zoomSlider->setValue(zoomSlider->value() - 1);
264 }
265 
267 {
268  zoomSlider->setValue(zoomSlider->value() + level);
269 }
270 
272 {
273  zoomSlider->setValue(zoomSlider->value() - level);
274 }
275 
276 void View::rotateLeft()
277 {
278  rotateSlider->setValue(rotateSlider->value() - 10);
279 }
280 
281 void View::rotateRight()
282 {
283  rotateSlider->setValue(rotateSlider->value() + 10);
284 }
void setAutoRepeatDelay(int)
void setIcon(const QIcon &icon)
void setAutoRepeat(bool)
void setAutoRepeatInterval(int)
void clicked(bool checked=false)
void toggled(bool checked)
void setCheckable(bool)
void setIconSize(const QSize &size)
void setText(const QString &text)
bool isChecked() const
int value
the slider's current value
void valueChanged(int value)
void setOrientation(Qt::Orientation)
void addWidget(QWidget *, int stretch=0, Qt::Alignment alignment=Qt::Alignment())
void addStretch(int stretch=0)
The QButtonGroup class provides a container to organize groups of button widgets.
Definition: qbuttongroup.h:55
void setExclusive(bool)
void addButton(QAbstractButton *, int id=-1)
virtual int exec()
Definition: qdialog.cpp:595
@ Accepted
Definition: qdialog.h:66
The QFrame class is the base class of widgets that can have a frame.
Definition: qframe.h:53
@ Sunken
Definition: qframe.h:87
void setFrameStyle(int)
Definition: qframe.cpp:335
@ StyledPanel
Definition: qframe.h:81
The QGraphicsView class provides a widget for displaying the contents of a QGraphicsScene.
Definition: qgraphicsview.h:60
void setInteractive(bool allowed)
void setTransform(const QTransform &matrix, bool combine=false)
void setDragMode(DragMode mode)
void ensureVisible(const QRectF &rect, int xmargin=50, int ymargin=50)
void setRenderHint(QPainter::RenderHint hint, bool enabled=true)
void render(QPainter *painter, const QRectF &target=QRectF(), const QRect &source=QRect(), Qt::AspectRatioMode aspectRatioMode=Qt::KeepAspectRatio)
void setTransformationAnchor(ViewportAnchor anchor)
void setOptimizationFlags(OptimizationFlags flags)
void setViewportUpdateMode(ViewportUpdateMode mode)
The QGridLayout class lays out widgets in a grid.
Definition: qgridlayout.h:57
void addWidget(QWidget *w)
Definition: qgridlayout.h:100
void addLayout(QLayout *, int row, int column, Qt::Alignment=Qt::Alignment())
The QHBoxLayout class lines up widgets horizontally.
Definition: qboxlayout.h:114
The QIcon class provides scalable icons in different modes and states.
Definition: qicon.h:56
The QLabel widget provides a text or image display.
Definition: qlabel.h:56
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
Definition: qobject.cpp:2772
The QPainter class performs low-level painting on widgets and other paint devices.
Definition: qpainter.h:82
@ Antialiasing
Definition: qpainter.h:88
The QPrintDialog class provides a dialog for specifying the printer's configuration.
Definition: qprintdialog.h:56
The QPrinter class is a paint device that paints on a printer.
Definition: qprinter.h:64
The QRectF class defines a finite rectangle in the plane using floating point precision.
Definition: qrect.h:511
The QSize class defines the size of a two-dimensional object using integer point precision.
Definition: qsize.h:55
The QSlider widget provides a vertical or horizontal slider.
Definition: qslider.h:54
@ TicksRight
Definition: qslider.h:66
@ TicksBelow
Definition: qslider.h:65
void setTickPosition(TickPosition position)
Definition: qslider.cpp:509
The QString class provides a Unicode character string.
Definition: qstring.h:388
@ PM_ToolBarIconSize
Definition: qstyle.h:526
virtual int pixelMetric(PixelMetric metric, const QStyleOption *option=nullptr, const QWidget *widget=nullptr) const =0
The QToolButton class provides a quick-access button to commands or options, usually used inside a QT...
Definition: qtoolbutton.h:56
The QTransform class specifies 2D transformations of a coordinate system.
Definition: qtransform.h:56
The QVBoxLayout class lines up widgets vertically.
Definition: qboxlayout.h:127
The QWidget class is the base class of all user interface objects.
Definition: qwidget.h:133
void setLayout(QLayout *)
Definition: qwidget.cpp:10146
void setEnabled(bool)
Definition: qwidget.cpp:3368
friend class QPixmap
Definition: qwidget.h:778
QStyle * style() const
Definition: qwidget.cpp:2612
void zoomOut()
Definition: view.cpp:261
QGraphicsView * view() const
Definition: view.cpp:199
void zoomOutBy(int level)
Definition: view.cpp:271
View(const QString &items, const QString &images, QWidget *parent=nullptr)
[0]
Definition: view.cpp:56
void zoomIn()
Definition: view.cpp:256
void zoomInBy(int level)
Definition: view.cpp:266
double e
opt iconSize
@ Horizontal
Definition: qnamespace.h:124
@ ControlModifier
Definition: qnamespace.h:1076
QT_END_INCLUDE_NAMESPACE typedef double qreal
Definition: qglobal.h:341
auto qPow(T1 x, T2 y)
Definition: qmath.h:216
GLenum GLuint GLint level
GLenum GLuint GLintptr GLsizeiptr size
[1]
GLuint GLsizei const GLchar * label
[43]
GLuint name
GLuint GLenum matrix
Definition: qopenglext.h:11564
GLenum GLenum GLenum GLenum GLenum scale
Definition: qopenglext.h:10817
#define tr(X)
QFileDialog dialog(this)
[1]
QPainter painter(this)
[7]
IUIAutomationTreeWalker __RPC__deref_out_opt IUIAutomationElement ** parent