| 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 |
#ifndef GRAPHICSVIEWZOOM_H |
| 34 |
#define GRAPHICSVIEWZOOM_H |
| 35 |
|
| 36 |
#include <QObject> |
| 37 |
#include <QPointF> |
| 38 |
|
| 39 |
|
| 40 |
/*! |
| 41 |
* This class adds ability to zoom QGraphicsView using mouse wheel. The point under cursor |
| 42 |
* remains motionless while it's possible. |
| 43 |
* |
| 44 |
* Note that it becomes not possible when the scene's |
| 45 |
* size is not large enough comparing to the viewport size. QGraphicsView centers the picture |
| 46 |
* when it's smaller than the view. And QGraphicsView's scrolls boundaries don't allow one to |
| 47 |
* put any picture point at any viewport position. |
| 48 |
* |
| 49 |
* When the user starts scrolling, this class remembers original scene position and |
| 50 |
* keeps it until scrolling is completed. It's better than getting original scene position at |
| 51 |
* each scrolling step because that approach leads to position errors due to before-mentioned |
| 52 |
* positioning restrictions. |
| 53 |
* |
| 54 |
* When zommed using scroll, this class emits zoomed() signal. |
| 55 |
* |
| 56 |
* Usage: |
| 57 |
* |
| 58 |
* new GraphicsViewZoom(view); |
| 59 |
* |
| 60 |
* The object will be deleted automatically when the view is deleted. |
| 61 |
* |
| 62 |
* You can set keyboard modifiers used for zooming using set_modified(). Zooming will be |
| 63 |
* performed only on exact match of modifiers combination. The default modifier is Ctrl. |
| 64 |
* |
| 65 |
* You can change zoom velocity by calling set_zoom_factor_base(). |
| 66 |
* Zoom coefficient is calculated as zoom_factor_base^angle_delta |
| 67 |
* (see QWheelEvent::angleDelta). |
| 68 |
* The default zoom factor base is 1.0015. |
| 69 |
*/ |
| 70 |
|
| 71 |
class QGraphicsView; |
| 72 |
|
| 73 |
class GraphicsViewZoom: public QObject |
| 74 |
{ |
| 75 |
Q_OBJECT |
| 76 |
public: |
| 77 |
GraphicsViewZoom(QGraphicsView* view); |
| 78 |
void gentle_zoom(double factor); |
| 79 |
void set_modifiers(Qt::KeyboardModifiers modifiers); |
| 80 |
void set_zoom_factor_base(double value); |
| 81 |
void set_zoom_inverted(bool on) |
| 82 |
{ |
| 83 |
m_invert_zoom = on; |
| 84 |
} |
| 85 |
|
| 86 |
private: |
| 87 |
QGraphicsView* _view; |
| 88 |
Qt::KeyboardModifiers _modifiers; |
| 89 |
double _zoom_factor_base; |
| 90 |
bool m_invert_zoom; |
| 91 |
QPointF target_scene_pos, target_viewport_pos; |
| 92 |
bool eventFilter(QObject* object, QEvent* event) override; |
| 93 |
}; |
| 94 |
|
| 95 |
#endif // GRAPHICSVIEWZOOM_H |