Qt 4.8
zoomtool.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtDeclarative module 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 Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/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 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include "zoomtool.h"
43 
44 #include "../qdeclarativeviewinspector_p.h"
45 
46 #include <QtGui/QMouseEvent>
47 #include <QtGui/QWheelEvent>
48 #include <QtGui/QKeyEvent>
49 #include <QtGui/QMenu>
50 #include <QtGui/QAction>
51 
52 #include <QtCore/QRectF>
53 #include <QtCore/QDebug>
54 
55 namespace QmlJSDebugger {
56 
59  m_rubberbandManipulator(),
60  m_smoothZoomMultiplier(0.05f),
61  m_currentScale(1.0f)
62 {
63  m_zoomTo100Action = new QAction(tr("Zoom to &100%"), this);
64  m_zoomInAction = new QAction(tr("Zoom In"), this);
65  m_zoomOutAction = new QAction(tr("Zoom Out"), this);
68 
69 
71  QGraphicsObject *layerObject = reinterpret_cast<QGraphicsObject *>(layerItem);
73 
74 
75  connect(m_zoomTo100Action, SIGNAL(triggered()), SLOT(zoomTo100()));
76  connect(m_zoomInAction, SIGNAL(triggered()), SLOT(zoomIn()));
77  connect(m_zoomOutAction, SIGNAL(triggered()), SLOT(zoomOut()));
78 }
79 
81 {
83 }
84 
86 {
87  m_mousePos = event->pos();
88 
89  QPointF scenePos = view()->mapToScene(event->pos());
90 
91  if (event->buttons() & Qt::RightButton) {
92  QMenu contextMenu;
93  contextMenu.addAction(m_zoomTo100Action);
94  contextMenu.addSeparator();
95  contextMenu.addAction(m_zoomInAction);
96  contextMenu.addAction(m_zoomOutAction);
97  contextMenu.exec(event->globalPos());
98  } else if (event->buttons() & Qt::LeftButton) {
99  m_dragBeginPos = scenePos;
100  m_dragStarted = false;
101  }
102 }
103 
105 {
106  m_mousePos = event->pos();
107 
108  QPointF scenePos = view()->mapToScene(event->pos());
109 
110  if (event->buttons() & Qt::LeftButton
111  && (QPointF(scenePos - m_dragBeginPos).manhattanLength()
113  && !m_dragStarted)
114  {
115  m_dragStarted = true;
117  return;
118  }
119 
120  if (m_dragStarted)
121  m_rubberbandManipulator->update(scenePos);
122 
123 }
124 
126 {
127  m_mousePos = event->pos();
128  QPointF scenePos = view()->mapToScene(event->pos());
129 
130  if (m_dragStarted) {
132 
133  int x1 = qMin(scenePos.x(), m_rubberbandManipulator->beginPoint().x());
134  int x2 = qMax(scenePos.x(), m_rubberbandManipulator->beginPoint().x());
135  int y1 = qMin(scenePos.y(), m_rubberbandManipulator->beginPoint().y());
136  int y2 = qMax(scenePos.y(), m_rubberbandManipulator->beginPoint().y());
137 
138  QPointF scenePosTopLeft = QPoint(x1, y1);
139  QPointF scenePosBottomRight = QPoint(x2, y2);
140 
141  QRectF sceneArea(scenePosTopLeft, scenePosBottomRight);
142 
143  m_currentScale = qMin(view()->rect().width() / sceneArea.width(),
144  view()->rect().height() / sceneArea.height());
145 
146 
147  QTransform transform;
148  transform.scale(m_currentScale, m_currentScale);
149 
150  view()->setTransform(transform);
151  view()->setSceneRect(sceneArea);
152  } else {
154 #ifdef Q_WS_MAC
155  modifierKey = Qt::AltModifier;
156 #endif
157  if (event->modifiers() & modifierKey) {
158  zoomOut();
159  } else {
160  zoomIn();
161  }
162  }
163 }
164 
166 {
168  scaleView(view()->mapToScene(m_mousePos));
169 }
170 
172 {
174  scaleView(view()->mapToScene(m_mousePos));
175 }
176 
178 {
179  m_mousePos = event->pos();
180 }
181 
182 
184 {
185  m_mousePos = event->pos();
186 }
187 
188 
190 {
191 }
192 
194 {
195  if (event->orientation() != Qt::Vertical)
196  return;
197 
198  Qt::KeyboardModifier smoothZoomModifier = Qt::ControlModifier;
199  if (event->modifiers() & smoothZoomModifier) {
200  int numDegrees = event->delta() / 8;
201  m_currentScale += m_smoothZoomMultiplier * (numDegrees / 15.0f);
202 
203  scaleView(view()->mapToScene(m_mousePos));
204 
205  } else if (!event->modifiers()) {
206  if (event->delta() > 0) {
208  } else if (event->delta() < 0) {
210  }
211  scaleView(view()->mapToScene(m_mousePos));
212  }
213 }
214 
216 {
217  switch (event->key()) {
218  case Qt::Key_Plus:
219  zoomIn();
220  break;
221  case Qt::Key_Minus:
222  zoomOut();
223  break;
224  case Qt::Key_1:
225  case Qt::Key_2:
226  case Qt::Key_3:
227  case Qt::Key_4:
228  case Qt::Key_5:
229  case Qt::Key_6:
230  case Qt::Key_7:
231  case Qt::Key_8:
232  case Qt::Key_9:
233  {
234  m_currentScale = ((event->key() - Qt::Key_0) * 1.0f);
235  scaleView(view()->mapToScene(m_mousePos)); // view()->mapToScene(view()->rect().center())
236  break;
237  }
238 
239  default:
240  break;
241  }
242 
243 }
244 
246 {
247 #ifndef QT_NO_CURSOR
249 #endif
250 }
251 
252 void ZoomTool::scaleView(const QPointF &centerPos)
253 {
254 
255  QTransform transform;
256  transform.scale(m_currentScale, m_currentScale);
257  view()->setTransform(transform);
258 
259  QPointF adjustedCenterPos = centerPos;
260  QSize rectSize(view()->rect().width() / m_currentScale,
261  view()->rect().height() / m_currentScale);
262 
263  QRectF sceneRect;
265  adjustedCenterPos.rx() = rectSize.width() / 2;
266  adjustedCenterPos.ry() = rectSize.height() / 2;
267  }
268 
269  if (m_currentScale < 1.0f) {
270  adjustedCenterPos.rx() = rectSize.width() / 2;
271  adjustedCenterPos.ry() = rectSize.height() / 2;
272  sceneRect.setRect(view()->rect().width() / 2 -rectSize.width() / 2,
273  view()->rect().height() / 2 -rectSize.height() / 2,
274  rectSize.width(),
275  rectSize.height());
276  } else {
277  sceneRect.setRect(adjustedCenterPos.x() - rectSize.width() / 2,
278  adjustedCenterPos.y() - rectSize.height() / 2,
279  rectSize.width(),
280  rectSize.height());
281  }
282 
283  view()->setSceneRect(sceneRect);
284 }
285 
287 {
288  m_currentScale = 1.0f;
289  scaleView(view()->mapToScene(view()->rect().center()));
290 }
291 
293 {
294  static QList<qreal> zoomScales =
295  QList<qreal>()
296  << 0.125f
297  << 1.0f / 6.0f
298  << 0.25f
299  << 1.0f / 3.0f
300  << 0.5f
301  << 2.0f / 3.0f
302  << 1.0f
303  << 2.0f
304  << 3.0f
305  << 4.0f
306  << 5.0f
307  << 6.0f
308  << 7.0f
309  << 8.0f
310  << 12.0f
311  << 16.0f
312  << 32.0f
313  << 48.0f;
314 
315  if (direction == ZoomIn) {
316  for (int i = 0; i < zoomScales.length(); ++i) {
317  if (zoomScales[i] > m_currentScale || i == zoomScales.length() - 1)
318  return zoomScales[i];
319  }
320  } else {
321  for (int i = zoomScales.length() - 1; i >= 0; --i) {
322  if (zoomScales[i] < m_currentScale || i == 0)
323  return zoomScales[i];
324  }
325  }
326 
327  return 1.0f;
328 }
329 
330 } // namespace QmlJSDebugger
The QKeyEvent class describes a key event.
Definition: qevent.h:224
void setShortcut(const QKeySequence &shortcut)
Definition: qaction.cpp:450
double qreal
Definition: qglobal.h:1193
Q_DECL_CONSTEXPR const T & qMin(const T &a, const T &b)
Definition: qglobal.h:1215
QPointF mapToScene(const QPoint &point) const
Returns the viewport coordinate point mapped to scene coordinates.
qreal nextZoomScale(ZoomDirection direction) const
Definition: zoomtool.cpp:292
void mouseDoubleClickEvent(QMouseEvent *event)
Definition: zoomtool.cpp:177
The QWheelEvent class contains parameters that describe a wheel event.
Definition: qevent.h:139
void hoverMoveEvent(QMouseEvent *event)
Definition: zoomtool.cpp:183
QAction * m_zoomTo100Action
Definition: zoomtool.h:96
#define SLOT(a)
Definition: qobjectdefs.h:226
The QPointF class defines a point in the plane using floating point precision.
Definition: qpoint.h:214
static QString tr(const char *sourceText, const char *comment=0, int n=-1)
void mouseMoveEvent(QMouseEvent *event)
Definition: zoomtool.cpp:104
KeyboardModifier
Definition: qnamespace.h:127
int height() const
Returns the height of the rectangle.
Definition: qrect.h:306
Q_DECL_CONSTEXPR T qAbs(const T &t)
Definition: qglobal.h:1201
QAction * addAction(const QString &text)
This convenience function creates a new action with text.
Definition: qmenu.cpp:1453
virtual bool event(QEvent *)
This virtual function receives events to an object and should return true if the event e was recogniz...
Definition: qobject.cpp:1200
Q_DECL_CONSTEXPR const T & qMax(const T &a, const T &b)
Definition: qglobal.h:1217
const QPoint & pos() const
Returns the position of the mouse cursor, relative to the widget that received the event...
Definition: qevent.h:95
qreal x() const
Returns the x-coordinate of this point.
Definition: qpoint.h:282
int key() const
Returns the code of the key that was pressed or released.
Definition: qevent.h:231
#define SIGNAL(a)
Definition: qobjectdefs.h:227
static const int DragStartDistance
int width() const
Returns the width.
Definition: qsize.h:126
qreal & rx()
Returns a reference to the x coordinate of this point.
Definition: qpoint.h:302
The QRectF class defines a rectangle in the plane using floating point precision. ...
Definition: qrect.h:511
void setTransform(const QTransform &matrix, bool combine=false)
Sets the view&#39;s current transformation matrix to matrix.
static bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
Creates a connection of the given type from the signal in the sender object to the method in the rece...
Definition: qobject.cpp:2580
QAction * m_zoomOutAction
Definition: zoomtool.h:98
qreal height() const
Returns the height of the rectangle.
Definition: qrect.h:710
int delta() const
Returns the distance that the wheel is rotated, in eighths of a degree.
Definition: qevent.h:150
QAction * addSeparator()
This convenience function creates a new separator action, i.e.
Definition: qmenu.cpp:1583
void scaleView(const QPointF &centerPos)
Definition: zoomtool.cpp:252
qreal width() const
Returns the width of the rectangle.
Definition: qrect.h:707
void setCursor(const QCursor &)
Definition: qwidget.cpp:5290
void setRect(qreal x, qreal y, qreal w, qreal h)
Sets the coordinates of the rectangle&#39;s top-left corner to (x, y), and its size to the given width an...
Definition: qrect.h:754
void keyReleaseEvent(QKeyEvent *keyEvent)
Definition: zoomtool.cpp:215
void wheelEvent(QWheelEvent *event)
Definition: zoomtool.cpp:193
Qt::Orientation orientation() const
Returns the wheel&#39;s orientation.
Definition: qevent.h:159
static const double ZoomSnapDelta
QRect rect() const
LiveRubberBandSelectionManipulator * m_rubberbandManipulator
Definition: zoomtool.h:99
The QMouseEvent class contains parameters that describe a mouse event.
Definition: qevent.h:85
Q_CORE_EXPORT QTextStream & center(QTextStream &s)
QAction * exec()
Executes this menu synchronously.
Definition: qmenu.cpp:2101
qreal manhattanLength() const
Returns the sum of the absolute values of x() and y(), traditionally known as the "Manhattan length" ...
Definition: qpoint.cpp:603
The QKeySequence class encapsulates a key sequence as used by shortcuts.
Definition: qkeysequence.h:72
Qt::MouseButtons buttons() const
Returns the button state when the event was generated.
Definition: qevent.h:102
The QMenu class provides a menu widget for use in menu bars, context menus, and other popup menus...
Definition: qmenu.h:72
void setSceneRect(const QRectF &rect)
int length() const
This function is identical to count().
Definition: qlist.h:281
The QPoint class defines a point in the plane using integer precision.
Definition: qpoint.h:53
void mouseReleaseEvent(QMouseEvent *event)
Definition: zoomtool.cpp:125
int height() const
Returns the height.
Definition: qsize.h:129
qreal & ry()
Returns a reference to the y coordinate of this point.
Definition: qpoint.h:307
qreal y() const
Returns the y-coordinate of this point.
Definition: qpoint.h:287
The QSize class defines the size of a two-dimensional object using integer point precision.
Definition: qsize.h:53
QTransform & scale(qreal sx, qreal sy)
Scales the coordinate system by sx horizontally and sy vertically, and returns a reference to the mat...
Definition: qtransform.cpp:485
ZoomTool(QDeclarativeViewInspector *view)
Definition: zoomtool.cpp:57
Qt::KeyboardModifiers modifiers() const
Returns the keyboard modifier flags that existed immediately before the event occurred.
Definition: qevent.h:79
The QGraphicsObject class provides a base class for all graphics items that require signals...
const QPoint & globalPos() const
Returns the global position of the mouse cursor at the time of the event.
Definition: qevent.h:96
void mousePressEvent(QMouseEvent *event)
Definition: zoomtool.cpp:85
QAction * m_zoomInAction
Definition: zoomtool.h:97
void keyPressEvent(QKeyEvent *event)
Definition: zoomtool.cpp:189
The QAction class provides an abstract user interface action that can be inserted into widgets...
Definition: qaction.h:64
static QDeclarativeViewInspectorPrivate * get(QDeclarativeViewInspector *v)
The QTransform class specifies 2D transformations of a coordinate system.
Definition: qtransform.h:65
The QList class is a template class that provides lists.
Definition: qdatastream.h:62
Qt::LayoutDirection direction