| 1 |
/*************************************************************************** |
| 2 |
* Copyright (c) 2015 Pavel Strakhov <[email protected]> * |
| 3 |
* Copyright (c) 2015 Eivind Kvedalen <[email protected]> * |
| 4 |
* * |
| 5 |
* This file is part of the FreeCAD CAx development system. * |
| 6 |
* * |
| 7 |
* This library is free software; you can redistribute it and/or * |
| 8 |
* modify it under the terms of the GNU Library General Public * |
| 9 |
* License as published by the Free Software Foundation; either * |
| 10 |
* version 2 of the License, or (at your option) any later version. * |
| 11 |
* * |
| 12 |
* This library is distributed in the hope that it will be useful, * |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 15 |
* GNU Library General Public License for more details. * |
| 16 |
* * |
| 17 |
* You should have received a copy of the GNU Library General Public * |
| 18 |
* License along with this library; see the file COPYING.LIB. If not, * |
| 19 |
* write to the Free Software Foundation, Inc., 59 Temple Place, * |
| 20 |
* Suite 330, Boston, MA 02111-1307, USA * |
| 21 |
* * |
| 22 |
***************************************************************************/ |
| 23 |
|
| 24 |
/* |
| 25 |
* Based on |
| 26 |
* |
| 27 |
* http://stackoverflow.com/questions/19113532/qgraphicsview-zooming-in-and-out-under-mouse-position-using-mouse-wheel |
| 28 |
* |
| 29 |
* Re-licensed to LGPL after having contacted original author by e-mail. |
| 30 |
* |
| 31 |
*/ |
| 32 |
|
| 33 |
|
| 34 |
#include <QApplication> |
| 35 |
#include <QGraphicsView> |
| 36 |
#include <QMouseEvent> |
| 37 |
#include <qmath.h> |
| 38 |
|
| 39 |
|
| 40 |
#include <GraphicsViewZoom.h> |
| 41 |
|
| 42 |
GraphicsViewZoom::GraphicsViewZoom(QGraphicsView* view) |
| 43 |
: QObject(view) |
| 44 |
, _view(view) |
| 45 |
, m_invert_zoom(false) |
| 46 |
{ |
| 47 |
_view->viewport()->installEventFilter(this); |
| 48 |
_view->setMouseTracking(true); |
| 49 |
_modifiers = Qt::ControlModifier; |
| 50 |
_zoom_factor_base = 1.0015; |
| 51 |
} |
| 52 |
|
| 53 |
void GraphicsViewZoom::gentle_zoom(double factor) |
| 54 |
{ |
| 55 |
_view->scale(factor, factor); |
| 56 |
_view->centerOn(target_scene_pos); |
| 57 |
QPointF delta_viewport_pos = target_viewport_pos |
| 58 |
- QPointF(_view->viewport()->width() / 2.0, _view->viewport()->height() / 2.0); |
| 59 |
QPointF viewport_center = _view->mapFromScene(target_scene_pos) - delta_viewport_pos; |
| 60 |
_view->centerOn(_view->mapToScene(viewport_center.toPoint())); |
| 61 |
} |
| 62 |
|
| 63 |
void GraphicsViewZoom::set_modifiers(Qt::KeyboardModifiers modifiers) |
| 64 |
{ |
| 65 |
_modifiers = modifiers; |
| 66 |
} |
| 67 |
|
| 68 |
void GraphicsViewZoom::set_zoom_factor_base(double value) |
| 69 |
{ |
| 70 |
_zoom_factor_base = value; |
| 71 |
} |
| 72 |
|
| 73 |
bool GraphicsViewZoom::eventFilter(QObject* object, QEvent* event) |
| 74 |
{ |
| 75 |
if (event->type() == QEvent::MouseMove) { |
| 76 |
auto mouse_event = static_cast<QMouseEvent*>(event); |
| 77 |
QPointF delta = target_viewport_pos - mouse_event->pos(); |
| 78 |
if (qAbs(delta.x()) > 5 || qAbs(delta.y()) > 5) { |
| 79 |
target_viewport_pos = mouse_event->pos(); |
| 80 |
target_scene_pos = _view->mapToScene(mouse_event->pos()); |
| 81 |
} |
| 82 |
} |
| 83 |
else if (event->type() == QEvent::Wheel) { |
| 84 |
auto wheel_event = static_cast<QWheelEvent*>(event); |
| 85 |
if (QApplication::keyboardModifiers() == _modifiers) { |
| 86 |
QPoint delta = wheel_event->angleDelta(); |
| 87 |
if (qAbs(delta.y()) > qAbs(delta.x())) { // vertical |
| 88 |
double angle = -delta.y(); |
| 89 |
if (m_invert_zoom) { |
| 90 |
angle = -angle; |
| 91 |
} |
| 92 |
double factor = qPow(_zoom_factor_base, angle); |
| 93 |
gentle_zoom(factor); |
| 94 |
return true; |
| 95 |
} |
| 96 |
} |
| 97 |
} |
| 98 |
Q_UNUSED(object); |
| 99 |
return false; |
| 100 |
} |
| 101 |
|
| 102 |
#include "moc_GraphicsViewZoom.cpp" |