KanoopGuiQt 1.3.0
Qt GUI utility library
Loading...
Searching...
No Matches
graphicsview.h
1#ifndef GRAPHICSVIEW_H
2#define GRAPHICSVIEW_H
3
4#include <QGraphicsView>
5
6#include <Kanoop/utility/loggingbaseclass.h>
7#include <Kanoop/gui/libkanoopgui.h>
8#include <Kanoop/geometry/point.h>
9
10class Size;
11
12/**
13 * @brief QGraphicsView subclass with built-in mouse pan and wheel zoom support.
14 *
15 * GraphicsView installs itself as an event filter and handles wheel events for
16 * zooming and mouse button press/move/release events for panning. Both features
17 * can be enabled or disabled at runtime. scaleChanged() is emitted whenever the
18 * view transform changes, and resized() is emitted on resize events.
19 */
20class LIBKANOOPGUI_EXPORT GraphicsView : public QGraphicsView,
21 public LoggingBaseClass
22{
23 Q_OBJECT
24public:
25 /**
26 * @brief Construct with an optional parent widget.
27 * @param parent Optional QWidget parent
28 */
29 explicit GraphicsView(QWidget *parent = nullptr);
30
31 /**
32 * @brief Return whether mouse-drag panning is enabled.
33 * @return true if pan is enabled
34 */
35 bool panEnabled() const { return _panEnabled; }
36
37 /**
38 * @brief Enable or disable mouse-drag panning.
39 * @param value true to enable pan
40 */
41 void setPanEnabled(bool value) { _panEnabled = value; }
42
43 /**
44 * @brief Return whether mouse-wheel zooming is enabled.
45 * @return true if zoom is enabled
46 */
47 bool isZoomEnabled() const { return _zoomEnabled; }
48
49 /**
50 * @brief Enable or disable mouse-wheel zooming.
51 * @param value true to enable zoom
52 */
53 void setZoomEnabled(bool value) { _zoomEnabled = value; }
54
55 /**
56 * @brief Set the view scale to an absolute factor.
57 * @param factor Scale factor (1.0 = no zoom)
58 */
59 void setScaleFactor(double factor);
60
61 /**
62 * @brief Return the current horizontal scale of the view transform.
63 * @return Current scale factor
64 */
65 double currentScale() const;
66
67protected:
68 /** @brief Emit resized() with old and new viewport sizes. */
69 virtual void resizeEvent(QResizeEvent* event) override;
70
71private:
72 virtual bool eventFilter(QObject *watched, QEvent *event) override;
73 bool processWheelEvent(QWheelEvent* event);
74 bool processMouseButtonPressEvent(QMouseEvent* event);
75 bool processMouseButtonReleaseEvent(QMouseEvent* event);
76 bool processMouseMoveEvent(QMouseEvent* event);
77
78 enum State { Normal, Pan, Zoom };
79
80 bool _zoomEnabled = true;
81 double _zoomFactor = 1.001;
82
83 bool _panEnabled = true;
84 Point _panOrigin;
85 Point _panStartCenter;
86 Point _lastMousePos;
87
88 State _state = Normal;
89
90signals:
91 /**
92 * @brief Emitted when the view scale changes.
93 * @param scale New scale factor
94 */
95 void scaleChanged(double scale);
96
97 /**
98 * @brief Emitted when the viewport is resized.
99 * @param newSize New viewport size
100 * @param oldSize Previous viewport size
101 */
102 void resized(const Size& newSize, const Size& oldSize);
103};
104
105#endif // GRAPHICSVIEW_H
QGraphicsView subclass with built-in mouse pan and wheel zoom support.
void setPanEnabled(bool value)
Enable or disable mouse-drag panning.
void setScaleFactor(double factor)
Set the view scale to an absolute factor.
GraphicsView(QWidget *parent=nullptr)
Construct with an optional parent widget.
void setZoomEnabled(bool value)
Enable or disable mouse-wheel zooming.
virtual void resizeEvent(QResizeEvent *event) override
Emit resized() with old and new viewport sizes.
bool panEnabled() const
Return whether mouse-drag panning is enabled.
void resized(const Size &newSize, const Size &oldSize)
Emitted when the viewport is resized.
void scaleChanged(double scale)
Emitted when the view scale changes.
double currentScale() const
Return the current horizontal scale of the view transform.
bool isZoomEnabled() const
Return whether mouse-wheel zooming is enabled.