Remove Q_WS_*, symbian and maemo code in QtDeclarative
[profile/ivi/qtdeclarative.git] / src / plugins / qmltooling / qmldbg_inspector / qtquick1 / zoomtool.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 **
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 **
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
29 **
30 ** Other Usage
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
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
57 ZoomTool::ZoomTool(QDeclarativeViewInspector *view) :
58     AbstractLiveEditTool(view),
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);
66     m_zoomInAction->setShortcut(QKeySequence(Qt::Key_Plus));
67     m_zoomOutAction->setShortcut(QKeySequence(Qt::Key_Minus));
68
69
70     LiveLayerItem *layerItem = QDeclarativeViewInspectorPrivate::get(view)->manipulatorLayer;
71     QGraphicsObject *layerObject = reinterpret_cast<QGraphicsObject *>(layerItem);
72     m_rubberbandManipulator = new LiveRubberBandSelectionManipulator(layerObject, view);
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
80 ZoomTool::~ZoomTool()
81 {
82     delete m_rubberbandManipulator;
83 }
84
85 void ZoomTool::mousePressEvent(QMouseEvent *event)
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
104 void ZoomTool::mouseMoveEvent(QMouseEvent *event)
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()
112                 > Constants::DragStartDistance / 3)
113             && !m_dragStarted)
114     {
115         m_dragStarted = true;
116         m_rubberbandManipulator->begin(m_dragBeginPos);
117         return;
118     }
119
120     if (m_dragStarted)
121         m_rubberbandManipulator->update(scenePos);
122
123 }
124
125 void ZoomTool::mouseReleaseEvent(QMouseEvent *event)
126 {
127     m_mousePos = event->pos();
128     QPointF scenePos = view()->mapToScene(event->pos());
129
130     if (m_dragStarted) {
131         m_rubberbandManipulator->end();
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 {
153         Qt::KeyboardModifier modifierKey = Qt::ControlModifier;
154 #ifdef Q_OS_MAC
155         modifierKey = Qt::AltModifier;
156 #endif
157         if (event->modifiers() & modifierKey) {
158             zoomOut();
159         } else {
160             zoomIn();
161         }
162     }
163 }
164
165 void ZoomTool::zoomIn()
166 {
167     m_currentScale = nextZoomScale(ZoomIn);
168     scaleView(view()->mapToScene(m_mousePos));
169 }
170
171 void ZoomTool::zoomOut()
172 {
173     m_currentScale = nextZoomScale(ZoomOut);
174     scaleView(view()->mapToScene(m_mousePos));
175 }
176
177 void ZoomTool::mouseDoubleClickEvent(QMouseEvent *event)
178 {
179     m_mousePos = event->pos();
180 }
181
182
183 void ZoomTool::hoverMoveEvent(QMouseEvent *event)
184 {
185     m_mousePos = event->pos();
186 }
187
188
189 void ZoomTool::keyPressEvent(QKeyEvent * /*event*/)
190 {
191 }
192
193 void ZoomTool::wheelEvent(QWheelEvent *event)
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) {
207             m_currentScale = nextZoomScale(ZoomIn);
208         } else if (event->delta() < 0) {
209             m_currentScale = nextZoomScale(ZoomOut);
210         }
211         scaleView(view()->mapToScene(m_mousePos));
212     }
213 }
214
215 void ZoomTool::keyReleaseEvent(QKeyEvent *event)
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
245 void ZoomTool::clear()
246 {
247     view()->setCursor(Qt::ArrowCursor);
248 }
249
250 void ZoomTool::scaleView(const QPointF &centerPos)
251 {
252
253     QTransform transform;
254     transform.scale(m_currentScale, m_currentScale);
255     view()->setTransform(transform);
256
257     QPointF adjustedCenterPos = centerPos;
258     QSize rectSize(view()->rect().width() / m_currentScale,
259                    view()->rect().height() / m_currentScale);
260
261     QRectF sceneRect;
262     if (qAbs(m_currentScale - 1.0f) < Constants::ZoomSnapDelta) {
263         adjustedCenterPos.rx() = rectSize.width() / 2;
264         adjustedCenterPos.ry() = rectSize.height() / 2;
265     }
266
267     if (m_currentScale < 1.0f) {
268         adjustedCenterPos.rx() = rectSize.width() / 2;
269         adjustedCenterPos.ry() = rectSize.height() / 2;
270         sceneRect.setRect(view()->rect().width() / 2 -rectSize.width() / 2,
271                           view()->rect().height() / 2 -rectSize.height() / 2,
272                           rectSize.width(),
273                           rectSize.height());
274     } else {
275         sceneRect.setRect(adjustedCenterPos.x() - rectSize.width() / 2,
276                           adjustedCenterPos.y() - rectSize.height() / 2,
277                           rectSize.width(),
278                           rectSize.height());
279     }
280
281     view()->setSceneRect(sceneRect);
282 }
283
284 void ZoomTool::zoomTo100()
285 {
286     m_currentScale = 1.0f;
287     scaleView(view()->mapToScene(view()->rect().center()));
288 }
289
290 qreal ZoomTool::nextZoomScale(ZoomDirection direction) const
291 {
292     static QList<qreal> zoomScales =
293             QList<qreal>()
294             << 0.125f
295             << 1.0f / 6.0f
296             << 0.25f
297             << 1.0f / 3.0f
298             << 0.5f
299             << 2.0f / 3.0f
300             << 1.0f
301             << 2.0f
302             << 3.0f
303             << 4.0f
304             << 5.0f
305             << 6.0f
306             << 7.0f
307             << 8.0f
308             << 12.0f
309             << 16.0f
310             << 32.0f
311             << 48.0f;
312
313     if (direction == ZoomIn) {
314         for (int i = 0; i < zoomScales.length(); ++i) {
315             if (zoomScales[i] > m_currentScale || i == zoomScales.length() - 1)
316                 return zoomScales[i];
317         }
318     } else {
319         for (int i = zoomScales.length() - 1; i >= 0; --i) {
320             if (zoomScales[i] < m_currentScale || i == 0)
321                 return zoomScales[i];
322         }
323     }
324
325     return 1.0f;
326 }
327
328 } // namespace QmlJSDebugger