Remove "All rights reserved" line from license headers.
[profile/ivi/qtdeclarative.git] / src / plugins / qmltooling / qmldbg_qtquick1 / zoomtool.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the QtDeclarative module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
16 **
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
20 **
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
28 **
29 ** Other Usage
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
32 **
33 **
34 **
35 **
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 <QtWidgets/QMenu>
50 #include <QtWidgets/QAction>
51
52 #include <QtCore/QRectF>
53 #include <QtCore/QDebug>
54
55 namespace QmlJSDebugger {
56 namespace QtQuick1 {
57
58 ZoomTool::ZoomTool(QDeclarativeViewInspector *view) :
59     AbstractLiveEditTool(view),
60     m_rubberbandManipulator(),
61     m_smoothZoomMultiplier(0.05f),
62     m_currentScale(1.0f)
63 {
64     m_zoomTo100Action = new QAction(tr("Zoom to &100%"), this);
65     m_zoomInAction = new QAction(tr("Zoom In"), this);
66     m_zoomOutAction = new QAction(tr("Zoom Out"), this);
67     m_zoomInAction->setShortcut(QKeySequence(Qt::Key_Plus));
68     m_zoomOutAction->setShortcut(QKeySequence(Qt::Key_Minus));
69
70
71     LiveLayerItem *layerItem = QDeclarativeViewInspectorPrivate::get(view)->manipulatorLayer;
72     QGraphicsObject *layerObject = reinterpret_cast<QGraphicsObject *>(layerItem);
73     m_rubberbandManipulator = new LiveRubberBandSelectionManipulator(layerObject, view);
74
75
76     connect(m_zoomTo100Action, SIGNAL(triggered()), SLOT(zoomTo100()));
77     connect(m_zoomInAction, SIGNAL(triggered()), SLOT(zoomIn()));
78     connect(m_zoomOutAction, SIGNAL(triggered()), SLOT(zoomOut()));
79 }
80
81 ZoomTool::~ZoomTool()
82 {
83     delete m_rubberbandManipulator;
84 }
85
86 void ZoomTool::mousePressEvent(QMouseEvent *event)
87 {
88     m_mousePos = event->pos();
89
90     QPointF scenePos = view()->mapToScene(event->pos());
91
92     if (event->buttons() & Qt::RightButton) {
93         QMenu contextMenu;
94         contextMenu.addAction(m_zoomTo100Action);
95         contextMenu.addSeparator();
96         contextMenu.addAction(m_zoomInAction);
97         contextMenu.addAction(m_zoomOutAction);
98         contextMenu.exec(event->globalPos());
99     } else if (event->buttons() & Qt::LeftButton) {
100         m_dragBeginPos = scenePos;
101         m_dragStarted = false;
102     }
103 }
104
105 void ZoomTool::mouseMoveEvent(QMouseEvent *event)
106 {
107     m_mousePos = event->pos();
108
109     QPointF scenePos = view()->mapToScene(event->pos());
110
111     if (event->buttons() & Qt::LeftButton
112             && (QPointF(scenePos - m_dragBeginPos).manhattanLength()
113                 > Constants::DragStartDistance / 3)
114             && !m_dragStarted)
115     {
116         m_dragStarted = true;
117         m_rubberbandManipulator->begin(m_dragBeginPos);
118         return;
119     }
120
121     if (m_dragStarted)
122         m_rubberbandManipulator->update(scenePos);
123
124 }
125
126 void ZoomTool::mouseReleaseEvent(QMouseEvent *event)
127 {
128     m_mousePos = event->pos();
129     QPointF scenePos = view()->mapToScene(event->pos());
130
131     if (m_dragStarted) {
132         m_rubberbandManipulator->end();
133
134         int x1 = qMin(scenePos.x(), m_rubberbandManipulator->beginPoint().x());
135         int x2 = qMax(scenePos.x(), m_rubberbandManipulator->beginPoint().x());
136         int y1 = qMin(scenePos.y(), m_rubberbandManipulator->beginPoint().y());
137         int y2 = qMax(scenePos.y(), m_rubberbandManipulator->beginPoint().y());
138
139         QPointF scenePosTopLeft = QPoint(x1, y1);
140         QPointF scenePosBottomRight = QPoint(x2, y2);
141
142         QRectF sceneArea(scenePosTopLeft, scenePosBottomRight);
143
144         m_currentScale = qMin(view()->rect().width() / sceneArea.width(),
145                               view()->rect().height() / sceneArea.height());
146
147
148         QTransform transform;
149         transform.scale(m_currentScale, m_currentScale);
150
151         view()->setTransform(transform);
152         view()->setSceneRect(sceneArea);
153     } else {
154         Qt::KeyboardModifier modifierKey = Qt::ControlModifier;
155 #ifdef Q_OS_MAC
156         modifierKey = Qt::AltModifier;
157 #endif
158         if (event->modifiers() & modifierKey) {
159             zoomOut();
160         } else {
161             zoomIn();
162         }
163     }
164 }
165
166 void ZoomTool::zoomIn()
167 {
168     m_currentScale = nextZoomScale(ZoomIn);
169     scaleView(view()->mapToScene(m_mousePos));
170 }
171
172 void ZoomTool::zoomOut()
173 {
174     m_currentScale = nextZoomScale(ZoomOut);
175     scaleView(view()->mapToScene(m_mousePos));
176 }
177
178 void ZoomTool::mouseDoubleClickEvent(QMouseEvent *event)
179 {
180     m_mousePos = event->pos();
181 }
182
183
184 void ZoomTool::hoverMoveEvent(QMouseEvent *event)
185 {
186     m_mousePos = event->pos();
187 }
188
189
190 void ZoomTool::keyPressEvent(QKeyEvent * /*event*/)
191 {
192 }
193
194 void ZoomTool::wheelEvent(QWheelEvent *event)
195 {
196     if (event->orientation() != Qt::Vertical)
197         return;
198
199     Qt::KeyboardModifier smoothZoomModifier = Qt::ControlModifier;
200     if (event->modifiers() & smoothZoomModifier) {
201         int numDegrees = event->delta() / 8;
202         m_currentScale += m_smoothZoomMultiplier * (numDegrees / 15.0f);
203
204         scaleView(view()->mapToScene(m_mousePos));
205
206     } else if (!event->modifiers()) {
207         if (event->delta() > 0) {
208             m_currentScale = nextZoomScale(ZoomIn);
209         } else if (event->delta() < 0) {
210             m_currentScale = nextZoomScale(ZoomOut);
211         }
212         scaleView(view()->mapToScene(m_mousePos));
213     }
214 }
215
216 void ZoomTool::keyReleaseEvent(QKeyEvent *event)
217 {
218     switch (event->key()) {
219     case Qt::Key_Plus:
220         zoomIn();
221         break;
222     case Qt::Key_Minus:
223         zoomOut();
224         break;
225     case Qt::Key_1:
226     case Qt::Key_2:
227     case Qt::Key_3:
228     case Qt::Key_4:
229     case Qt::Key_5:
230     case Qt::Key_6:
231     case Qt::Key_7:
232     case Qt::Key_8:
233     case Qt::Key_9:
234     {
235         m_currentScale = ((event->key() - Qt::Key_0) * 1.0f);
236         scaleView(view()->mapToScene(m_mousePos)); // view()->mapToScene(view()->rect().center())
237         break;
238     }
239
240     default:
241         break;
242     }
243
244 }
245
246 void ZoomTool::clear()
247 {
248     view()->setCursor(Qt::ArrowCursor);
249 }
250
251 void ZoomTool::scaleView(const QPointF &centerPos)
252 {
253
254     QTransform transform;
255     transform.scale(m_currentScale, m_currentScale);
256     view()->setTransform(transform);
257
258     QPointF adjustedCenterPos = centerPos;
259     QSize rectSize(view()->rect().width() / m_currentScale,
260                    view()->rect().height() / m_currentScale);
261
262     QRectF sceneRect;
263     if (qAbs(m_currentScale - 1.0f) < Constants::ZoomSnapDelta) {
264         adjustedCenterPos.rx() = rectSize.width() / 2;
265         adjustedCenterPos.ry() = rectSize.height() / 2;
266     }
267
268     if (m_currentScale < 1.0f) {
269         adjustedCenterPos.rx() = rectSize.width() / 2;
270         adjustedCenterPos.ry() = rectSize.height() / 2;
271         sceneRect.setRect(view()->rect().width() / 2 -rectSize.width() / 2,
272                           view()->rect().height() / 2 -rectSize.height() / 2,
273                           rectSize.width(),
274                           rectSize.height());
275     } else {
276         sceneRect.setRect(adjustedCenterPos.x() - rectSize.width() / 2,
277                           adjustedCenterPos.y() - rectSize.height() / 2,
278                           rectSize.width(),
279                           rectSize.height());
280     }
281
282     view()->setSceneRect(sceneRect);
283 }
284
285 void ZoomTool::zoomTo100()
286 {
287     m_currentScale = 1.0f;
288     scaleView(view()->mapToScene(view()->rect().center()));
289 }
290
291 qreal ZoomTool::nextZoomScale(ZoomDirection direction) const
292 {
293     static QList<qreal> zoomScales =
294             QList<qreal>()
295             << 0.125f
296             << 1.0f / 6.0f
297             << 0.25f
298             << 1.0f / 3.0f
299             << 0.5f
300             << 2.0f / 3.0f
301             << 1.0f
302             << 2.0f
303             << 3.0f
304             << 4.0f
305             << 5.0f
306             << 6.0f
307             << 7.0f
308             << 8.0f
309             << 12.0f
310             << 16.0f
311             << 32.0f
312             << 48.0f;
313
314     if (direction == ZoomIn) {
315         for (int i = 0; i < zoomScales.length(); ++i) {
316             if (zoomScales[i] > m_currentScale || i == zoomScales.length() - 1)
317                 return zoomScales[i];
318         }
319     } else {
320         for (int i = zoomScales.length() - 1; i >= 0; --i) {
321             if (zoomScales[i] < m_currentScale || i == 0)
322                 return zoomScales[i];
323         }
324     }
325
326     return 1.0f;
327 }
328
329 } // namespace QtQuick1
330 } // namespace QmlJSDebugger