Seamly2D
Code documentation
vmaingraphicsview.h
Go to the documentation of this file.
1 /***************************************************************************
2  * *
3  * Copyright (C) 2017 Seamly, LLC *
4  * *
5  * https://github.com/fashionfreedom/seamly2d *
6  * *
7  ***************************************************************************
8  **
9  ** Seamly2D is free software: you can redistribute it and/or modify
10  ** it under the terms of the GNU General Public License as published by
11  ** the Free Software Foundation, either version 3 of the License, or
12  ** (at your option) any later version.
13  **
14  ** Seamly2D is distributed in the hope that it will be useful,
15  ** but WITHOUT ANY WARRANTY; without even the implied warranty of
16  ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  ** GNU General Public License for more details.
18  **
19  ** You should have received a copy of the GNU General Public License
20  ** along with Seamly2D. If not, see <http://www.gnu.org/licenses/>.
21  **
22  **************************************************************************
23 
24  ************************************************************************
25  **
26  ** @file vmaingraphicsview.h
27  ** @author Roman Telezhynskyi <dismine(at)gmail.com>
28  ** @date November 15, 2013
29  **
30  ** @brief
31  ** @copyright
32  ** This source code is part of the Valentine project, a pattern making
33  ** program, whose allow create and modeling patterns of clothing.
34  ** Copyright (C) 2013-2015 Seamly2D project
35  ** <https://github.com/fashionfreedom/seamly2d> All Rights Reserved.
36  **
37  ** Seamly2D is free software: you can redistribute it and/or modify
38  ** it under the terms of the GNU General Public License as published by
39  ** the Free Software Foundation, either version 3 of the License, or
40  ** (at your option) any later version.
41  **
42  ** Seamly2D is distributed in the hope that it will be useful,
43  ** but WITHOUT ANY WARRANTY; without even the implied warranty of
44  ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
45  ** GNU General Public License for more details.
46  **
47  ** You should have received a copy of the GNU General Public License
48  ** along with Seamly2D. If not, see <http://www.gnu.org/licenses/>.
49  **
50  *************************************************************************/
51 
52 #ifndef VMAINGRAPHICSVIEW_H
53 #define VMAINGRAPHICSVIEW_H
54 
55 #include <qcompilerdetection.h>
56 #include <QGraphicsView>
57 #include <QMetaObject>
58 #include <QObject>
59 #include <QPointF>
60 #include <QRectF>
61 #include <QString>
62 #include <Qt>
63 #include <QtGlobal>
64 #include <QRubberBand>
65 #include <QColor>
66 #include <Qt>
67 #include <QSharedPointer>
68 
69 /*!
70  * This class adds ability to zoom QGraphicsView using mouse wheel. The point under cursor
71  * remains motionless while it's possible.
72  *
73  * When the user starts scrolling, this class remembers original scene position and
74  * keeps it until scrolling is completed. It's better than getting original scene position at
75  * each scrolling step because that approach leads to position errors due to before-mentioned
76  * positioning restrictions.
77  *
78  * When zoomed using scroll, this class emits zoomed() signal.
79  *
80  * Usage:
81  *
82  * new Graphics_view_zoom(view);
83  *
84  * The object will be deleted automatically when the view is deleted.
85  *
86  * You can set keyboard modifiers used for zooming using set_modified(). Zooming will be
87  * performed only on exact match of modifiers combination. The default modifier is Ctrl.
88  *
89  * You can change zoom velocity by calling set_zoom_factor_base().
90  * Zoom coefficient is calculated as zoom_factor_base^angle_delta
91  * (see QWheelEvent::angleDelta).
92  * The default zoom factor base is 1.0015.
93  */
94 
95 class QTimeLine;
96 class QTransform;
97 class QGesture;
98 class QGestureEvent;
99 class QPanGesture;
100 class QPinchGesture;
101 
102 class GraphicsViewZoom : public QObject
103 {
104  Q_OBJECT
105 public:
106  explicit GraphicsViewZoom(QGraphicsView* view);
107  void gentleZoom(qreal factor);
108  void setModifiers(Qt::KeyboardModifiers modifiers);
109  void setZoomSpeedFactor(qreal value);
110  void initScrollAnimations();
111 
112 signals:
113  void zoomed();
114 
115 public slots:
116  void verticalScrollingTime(qreal x);
117  void horizontalScrollingTime(qreal x);
118  void animFinished();
119 
120 protected:
121  virtual bool eventFilter(QObject* object, QEvent* event) Q_DECL_OVERRIDE;
122 
123 private:
124  Q_DISABLE_COPY(GraphicsViewZoom)
125  QGraphicsView *m_view;
126  Qt::KeyboardModifiers m_modifiers;
128  QPointF targetScenePos;
129  QPointF targetViewPos;
132  QTimeLine *verticalScrollAnim;
133  /** @brief _numScheduledVerticalScrollings keep number scheduled vertical scrollings. */
136  /** @brief _numScheduledHorizontalScrollings keep number scheduled horizontal scrollings. */
138 
139  void fictiveSceneRect(QGraphicsScene *sc, QGraphicsView *view);
140 
141  bool startVerticalScrollings(QWheelEvent* wheel_event);
142  bool startHorizontalScrollings(QWheelEvent* wheel_event);
143  bool gestureEvent(QGestureEvent* event);
144  void panTriggered(QPanGesture* gesture);
145  void pinchTriggered(QPinchGesture* gesture);
146 
147  QGesture *pan;
148  QGesture *pinch;
151  qreal scaleFactor;
153 };
154 
155 /**
156  * @brief The VMainGraphicsView class main scene view.
157  */
158 class VMainGraphicsView : public QGraphicsView
159 {
160  Q_OBJECT
161 public:
162 
163  explicit VMainGraphicsView(QWidget *parent = nullptr);
164  void setShowToolOptions(bool value);
165  void allowRubberBand(bool value);
166  void zoomPanEnabled(bool value);
167  void zoomToAreaEnabled(bool value);
168 
169  static void NewSceneRect(QGraphicsScene *sc, QGraphicsView *view, QGraphicsItem *item = nullptr);
170  static QRectF SceneVisibleArea(QGraphicsView *view);
171 
172  static qreal MinScale();
173  static qreal MaxScale();
174 
175  void setRubberBandColor(QRubberBand *band, const QColor &color);
176 
177  void initScrollBars();
178 
179 signals:
180  /**
181  * @brief mouseRelease help catch mouse release event.
182  *
183  * Usefull when you need show dialog after working with tool visualization.
184  */
185  void mouseRelease();
186  void itemClicked(QGraphicsItem *item);
187  void signalZoomScaleChanged(qreal scale);
188 
189 
190 public slots:
191  void zoomByScale(qreal scale);
192  void zoomIn();
193  void zoomOut();
194  void zoom100Percent();
195  void zoomToFit();
196  void zoomToRect(const QRectF &rect);
197  void updateView(const QTransform &transform);
198  void resetScrollBars();
199  void resetScrollAnimations();
200 
201 protected:
202  virtual void mousePressEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
203  virtual void mouseMoveEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
204  virtual void mouseReleaseEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
205  virtual void mouseDoubleClickEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
206 
208 
209 private:
210  Q_DISABLE_COPY(VMainGraphicsView)
212  bool showToolProperties;
213  bool showScrollBars;
214  bool isallowRubberBand;
215  bool isZoomToAreaActive;
216  bool isRubberBandActive;
217  bool isRubberBandColorSet;
218  bool isZoomPanActive;
219  bool isPanDragActive;
220  QRubberBand *rubberBand;
221  QRect *rubberBandRect;
222  QPoint startPoint;
223  QPoint endPoint;
224  QPoint m_ptStartPos;
225  QPoint cursorPos;
226 };
227 
228 #endif // VMAINGRAPHICSVIEW_H
GraphicsViewZoom(QGraphicsView *view)
QTimeLine * verticalScrollAnim
void panTriggered(QPanGesture *gesture)
void horizontalScrollingTime(qreal x)
Qt::KeyboardModifiers m_modifiers
bool startHorizontalScrollings(QWheelEvent *wheel_event)
qint32 m_numScheduledVerticalScrollings
_numScheduledVerticalScrollings keep number scheduled vertical scrollings.
virtual bool eventFilter(QObject *object, QEvent *event) Q_DECL_OVERRIDE
bool gestureEvent(QGestureEvent *event)
void setModifiers(Qt::KeyboardModifiers modifiers)
void pinchTriggered(QPinchGesture *gesture)
QGraphicsView * m_view
void setZoomSpeedFactor(qreal value)
void verticalScrollingTime(qreal x)
void fictiveSceneRect(QGraphicsScene *sc, QGraphicsView *view)
bool startVerticalScrollings(QWheelEvent *wheel_event)
void gentleZoom(qreal factor)
QTimeLine * horizontalScrollAnim
qint32 m_numScheduledHorizontalScrollings
_numScheduledHorizontalScrollings keep number scheduled horizontal scrollings.
The VMainGraphicsView class main scene view.
void itemClicked(QGraphicsItem *item)
void signalZoomScaleChanged(qreal scale)
QSharedPointer< QCursor > curMagnifier
void mouseRelease()
mouseRelease help catch mouse release event.