LibrePCB Developers Documentation
graphicsview.h
Go to the documentation of this file.
1/*
2 * LibrePCB - Professional EDA for everyone!
3 * Copyright (C) 2013 LibrePCB Developers, see AUTHORS.md for contributors.
4 * https://librepcb.org/
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#ifndef LIBREPCB_EDITOR_GRAPHICSVIEW_H
21#define LIBREPCB_EDITOR_GRAPHICSVIEW_H
22
23/*******************************************************************************
24 * Includes
25 ******************************************************************************/
29#include <optional/tl/optional.hpp>
30
31#include <QtCore>
32#include <QtWidgets>
33
34/*******************************************************************************
35 * Namespace / Forward Declarations
36 ******************************************************************************/
37namespace librepcb {
38namespace editor {
39
40class GraphicsScene;
41class IF_GraphicsViewEventHandler;
42class WaitingSpinnerWidget;
43
44/*******************************************************************************
45 * Class GraphicsView
46 ******************************************************************************/
47
51class GraphicsView final : public QGraphicsView {
52 Q_OBJECT
53
54public:
55 // Types
56 enum class CursorOption {
57 Cross = (1 << 0),
58 Circle = (1 << 1),
59 };
60 Q_DECLARE_FLAGS(CursorOptions, CursorOption)
61
62 // Constructors / Destructor
63 GraphicsView(const GraphicsView& other) = delete;
64 explicit GraphicsView(
65 QWidget* parent = nullptr,
66 IF_GraphicsViewEventHandler* eventHandler = nullptr) noexcept;
67 ~GraphicsView() noexcept;
68
69 // Getters
70 GraphicsScene* getScene() const noexcept { return mScene; }
71 QRectF getVisibleSceneRect() const noexcept;
72 bool getUseOpenGl() const noexcept { return mUseOpenGl; }
73 const PositiveLength& getGridInterval() const noexcept {
74 return mGridInterval;
75 }
76 Theme::GridStyle getGridStyle() const noexcept { return mGridStyle; }
77 bool isMouseButtonPressed(Qt::MouseButtons btn) const noexcept {
78 return mPressedMouseButtons & btn;
79 }
80 qint64 getIdleTimeMs() const noexcept { return mIdleTimeMs; }
81
82 // Setters
83 void setBackgroundColors(const QColor& fill, const QColor& grid) noexcept;
84 void setOverlayColors(const QColor& fill, const QColor& content) noexcept;
85 void setInfoBoxColors(const QColor& fill, const QColor& text) noexcept;
86 void setUseOpenGl(bool useOpenGl) noexcept;
87 void setGrayOut(bool grayOut) noexcept;
88 void setGridStyle(Theme::GridStyle style) noexcept;
89 void setGridInterval(const PositiveLength& interval) noexcept;
90 void setScene(GraphicsScene* scene) noexcept;
91 void setVisibleSceneRect(const QRectF& rect) noexcept;
92
102 void setSceneRectMarker(const QRectF& rect) noexcept;
103 void setSceneCursor(
104 const tl::optional<std::pair<Point, CursorOptions>>& cursor) noexcept;
106 const tl::optional<std::pair<Point, Point>>& pos) noexcept;
107 void setInfoBoxText(const QString& text) noexcept;
108 void setOriginCrossVisible(bool visible) noexcept;
110 IF_GraphicsViewEventHandler* eventHandler) noexcept;
111
112 // General Methods
113 Point mapGlobalPosToScenePos(const QPoint& globalPosPx, bool boundToView,
114 bool mapToGrid) const noexcept;
115 QPainterPath calcPosWithTolerance(const Point& pos,
116 qreal multiplier = 1) const noexcept;
117 void handleMouseWheelEvent(QGraphicsSceneWheelEvent* event) noexcept;
118
119 // Operator Overloadings
120 GraphicsView& operator=(const GraphicsView& rhs) = delete;
121
122public slots:
123
124 // Public Slots
125 void zoomIn() noexcept;
126 void zoomOut() noexcept;
127 void zoomAll() noexcept;
128 void zoomToRect(const QRectF& rect) noexcept;
129 void showWaitingSpinner() noexcept;
130 void hideWaitingSpinner() noexcept;
131
132signals:
139
140private slots:
141
142 // Private Slots
143 void zoomAnimationValueChanged(const QVariant& value) noexcept;
144
145private:
146 // Inherited Methods
147 void wheelEvent(QWheelEvent* event);
148 bool eventFilter(QObject* obj, QEvent* event);
149 void drawBackground(QPainter* painter, const QRectF& rect);
150 void drawForeground(QPainter* painter, const QRectF& rect);
151
152 // General Attributes
154 QScopedPointer<QLabel> mInfoBoxLabel;
157 QVariantAnimation* mZoomAnimation;
158 Theme::GridStyle mGridStyle;
168
170 tl::optional<std::pair<Point, CursorOptions>> mSceneCursor;
171
172 // Configuration for the ruler overlay
173 struct RulerGauge {
179 };
180 QVector<RulerGauge> mRulerGauges;
181 tl::optional<std::pair<Point, Point>> mRulerPositions;
182
183 // State
184 volatile bool mPanningActive;
185 Qt::MouseButton mPanningButton;
186 Qt::MouseButtons mPressedMouseButtons;
189
190 // Static Variables
191 static constexpr qreal sZoomStepFactor = 1.3;
192};
193
194/*******************************************************************************
195 * End of File
196 ******************************************************************************/
197
198} // namespace editor
199} // namespace librepcb
200
201#endif
The Circle class.
Definition: circle.h:46
The Length class is used to represent a length (for example 12.75 millimeters)
Definition: length.h:83
The LengthUnit class represents a length unit (millimeters, inches,...) and provides some useful meth...
Definition: lengthunit.h:60
The Point class is used to represent a point/coordinate/vector, for example (1.2mm; 5....
Definition: point.h:79
Theme class as used by librepcb::WorkspaceSettingsItem_Themes.
Definition: theme.h:44
GridStyle
Definition: theme.h:49
The GraphicsScene class.
Definition: graphicsscene.h:45
The GraphicsView class.
Definition: graphicsview.h:51
void zoomToRect(const QRectF &rect) noexcept
Definition: graphicsview.cpp:319
void setSceneCursor(const tl::optional< std::pair< Point, CursorOptions > > &cursor) noexcept
Definition: graphicsview.cpp:209
QCursor mCursorBeforePanning
Definition: graphicsview.h:187
Theme::GridStyle getGridStyle() const noexcept
Definition: graphicsview.h:76
bool mUseOpenGl
Definition: graphicsview.h:166
void zoomAll() noexcept
Definition: graphicsview.cpp:308
QColor mOverlayFillColor
Definition: graphicsview.h:162
QColor mGridColor
Definition: graphicsview.h:161
QVector< RulerGauge > mRulerGauges
Definition: graphicsview.h:180
PositiveLength mGridInterval
Definition: graphicsview.h:159
const PositiveLength & getGridInterval() const noexcept
Definition: graphicsview.h:73
void setGridStyle(Theme::GridStyle style) noexcept
Definition: graphicsview.cpp:182
void setRulerPositions(const tl::optional< std::pair< Point, Point > > &pos) noexcept
Definition: graphicsview.cpp:215
QPainterPath calcPosWithTolerance(const Point &pos, qreal multiplier=1) const noexcept
Definition: graphicsview.cpp:256
tl::optional< std::pair< Point, Point > > mRulerPositions
Definition: graphicsview.h:181
IF_GraphicsViewEventHandler * mEventHandlerObject
Definition: graphicsview.h:155
void setUseOpenGl(bool useOpenGl) noexcept
Definition: graphicsview.cpp:156
void setOriginCrossVisible(bool visible) noexcept
Definition: graphicsview.cpp:227
void hideWaitingSpinner() noexcept
Definition: graphicsview.cpp:332
void zoomOut() noexcept
Definition: graphicsview.cpp:302
void zoomAnimationValueChanged(const QVariant &value) noexcept
Definition: graphicsview.cpp:340
Theme::GridStyle mGridStyle
Definition: graphicsview.h:158
void setSceneRectMarker(const QRectF &rect) noexcept
Setup the marker for a specific scene rect.
Definition: graphicsview.cpp:204
void handleMouseWheelEvent(QGraphicsSceneWheelEvent *event) noexcept
Definition: graphicsview.cpp:268
QScopedPointer< WaitingSpinnerWidget > mWaitingSpinnerWidget
Definition: graphicsview.h:153
QColor mOverlayContentColor
Definition: graphicsview.h:163
void setOverlayColors(const QColor &fill, const QColor &content) noexcept
Definition: graphicsview.cpp:136
QRectF getVisibleSceneRect() const noexcept
Definition: graphicsview.cpp:120
Qt::MouseButton mPanningButton
Definition: graphicsview.h:185
void setGrayOut(bool grayOut) noexcept
Definition: graphicsview.cpp:177
void cursorScenePositionChanged(const Point &pos)
Cursor scene position changed signal.
bool mGrayOut
Definition: graphicsview.h:167
void drawBackground(QPainter *painter, const QRectF &rect)
Definition: graphicsview.cpp:452
bool mOriginCrossVisible
Definition: graphicsview.h:165
qint64 mIdleTimeMs
Definition: graphicsview.h:188
void zoomIn() noexcept
Definition: graphicsview.cpp:296
tl::optional< std::pair< Point, CursorOptions > > mSceneCursor
If not nullopt, a cursor will be shown at the given position.
Definition: graphicsview.h:170
static constexpr qreal sZoomStepFactor
Definition: graphicsview.h:191
QVariantAnimation * mZoomAnimation
Definition: graphicsview.h:157
volatile bool mPanningActive
Definition: graphicsview.h:184
void setBackgroundColors(const QColor &fill, const QColor &grid) noexcept
Definition: graphicsview.cpp:128
void drawForeground(QPainter *painter, const QRectF &rect)
Definition: graphicsview.cpp:501
void setInfoBoxColors(const QColor &fill, const QColor &text) noexcept
Definition: graphicsview.cpp:143
GraphicsScene * getScene() const noexcept
Definition: graphicsview.h:70
QRectF mSceneRectMarker
Definition: graphicsview.h:164
void wheelEvent(QWheelEvent *event)
Definition: graphicsview.cpp:351
bool getUseOpenGl() const noexcept
Definition: graphicsview.h:72
CursorOption
Definition: graphicsview.h:56
void showWaitingSpinner() noexcept
Definition: graphicsview.cpp:328
void setInfoBoxText(const QString &text) noexcept
Definition: graphicsview.cpp:221
bool eventFilter(QObject *obj, QEvent *event)
Definition: graphicsview.cpp:359
QScopedPointer< QLabel > mInfoBoxLabel
Definition: graphicsview.h:154
void setVisibleSceneRect(const QRectF &rect) noexcept
Definition: graphicsview.cpp:200
qint64 getIdleTimeMs() const noexcept
Definition: graphicsview.h:80
Point mapGlobalPosToScenePos(const QPoint &globalPosPx, bool boundToView, bool mapToGrid) const noexcept
Definition: graphicsview.cpp:241
void setGridInterval(const PositiveLength &interval) noexcept
Definition: graphicsview.cpp:187
QColor mBackgroundColor
Definition: graphicsview.h:160
void setScene(GraphicsScene *scene) noexcept
Definition: graphicsview.cpp:192
GraphicsScene * mScene
Definition: graphicsview.h:156
Qt::MouseButtons mPressedMouseButtons
Definition: graphicsview.h:186
bool isMouseButtonPressed(Qt::MouseButtons btn) const noexcept
Definition: graphicsview.h:77
void setEventHandlerObject(IF_GraphicsViewEventHandler *eventHandler) noexcept
Definition: graphicsview.cpp:232
The IF_GraphicsViewEventHandler class.
Definition: if_graphicsvieweventhandler.h:41
A widget drawing a rotating spinner to indicate an ongoing operation.
Definition: waitingspinnerwidget.h:52
Definition: occmodel.cpp:77
type_safe::constrained_type< Length, PositiveLengthConstraint, PositiveLengthVerifier > PositiveLength
Definition: length.h:812
Definition: uuid.h:183
Definition: graphicsview.h:173
QString unitSeparator
Definition: graphicsview.h:176
LengthUnit unit
Definition: graphicsview.h:175
Length currentTickInterval
Definition: graphicsview.h:178
int xScale
Definition: graphicsview.h:174
Length minTickInterval
Definition: graphicsview.h:177