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  ******************************************************************************/
37 namespace librepcb {
38 namespace editor {
39 
40 class GraphicsScene;
41 class IF_GraphicsViewEventHandler;
42 class WaitingSpinnerWidget;
43 
44 /*******************************************************************************
45  * Class GraphicsView
46  ******************************************************************************/
47 
51 class GraphicsView final : public QGraphicsView {
52  Q_OBJECT
53 
54 public:
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;
105  void setRulerPositions(
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 
122 public 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 
132 signals:
138  void cursorScenePositionChanged(const Point& pos);
139 
140 private slots:
141 
142  // Private Slots
143  void zoomAnimationValueChanged(const QVariant& value) noexcept;
144 
145 private:
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
153  QScopedPointer<WaitingSpinnerWidget> mWaitingSpinnerWidget;
154  QScopedPointer<QLabel> mInfoBoxLabel;
157  QVariantAnimation* mZoomAnimation;
161  QColor mGridColor;
167  bool mGrayOut;
168 
170  tl::optional<std::pair<Point, CursorOptions>> mSceneCursor;
171 
172  // Configuration for the ruler overlay
173  struct RulerGauge {
174  int xScale;
176  QString unitSeparator;
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;
188  qint64 mIdleTimeMs;
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
qint64 mIdleTimeMs
Definition: graphicsview.h:188
bool isMouseButtonPressed(Qt::MouseButtons btn) const noexcept
Definition: graphicsview.h:77
void setEventHandlerObject(IF_GraphicsViewEventHandler *eventHandler) noexcept
Definition: graphicsview.cpp:232
QRectF getVisibleSceneRect() const noexcept
Definition: graphicsview.cpp:120
bool getUseOpenGl() const noexcept
Definition: graphicsview.h:72
GraphicsView & operator=(const GraphicsView &rhs)=delete
The GraphicsView class.
Definition: graphicsview.h:51
const PositiveLength & getGridInterval() const noexcept
Definition: graphicsview.h:73
QVariantAnimation * mZoomAnimation
Definition: graphicsview.h:157
void setOverlayColors(const QColor &fill, const QColor &content) noexcept
Definition: graphicsview.cpp:136
Definition: occmodel.cpp:77
QVector< RulerGauge > mRulerGauges
Definition: graphicsview.h:180
QRectF mSceneRectMarker
Definition: graphicsview.h:164
void zoomToRect(const QRectF &rect) noexcept
Definition: graphicsview.cpp:319
QColor mOverlayContentColor
Definition: graphicsview.h:163
void setUseOpenGl(bool useOpenGl) noexcept
Definition: graphicsview.cpp:156
tl::optional< std::pair< Point, Point > > mRulerPositions
Definition: graphicsview.h:181
void setRulerPositions(const tl::optional< std::pair< Point, Point >> &pos) noexcept
Definition: graphicsview.cpp:215
Definition: graphicsview.h:173
QString unitSeparator
Definition: graphicsview.h:176
qint64 getIdleTimeMs() const noexcept
Definition: graphicsview.h:80
void setGridInterval(const PositiveLength &interval) noexcept
Definition: graphicsview.cpp:187
static constexpr qreal sZoomStepFactor
Definition: graphicsview.h:191
void setGridStyle(Theme::GridStyle style) noexcept
Definition: graphicsview.cpp:182
Point mapGlobalPosToScenePos(const QPoint &globalPosPx, bool boundToView, bool mapToGrid) const noexcept
Definition: graphicsview.cpp:241
void wheelEvent(QWheelEvent *event)
Definition: graphicsview.cpp:351
Length minTickInterval
Definition: graphicsview.h:177
void hideWaitingSpinner() noexcept
Definition: graphicsview.cpp:332
Qt::MouseButtons mPressedMouseButtons
Definition: graphicsview.h:186
bool mGrayOut
Definition: graphicsview.h:167
void drawBackground(QPainter *painter, const QRectF &rect)
Definition: graphicsview.cpp:452
The Point class is used to represent a point/coordinate/vector, for example (1.2mm; 5...
Definition: point.h:79
void zoomAnimationValueChanged(const QVariant &value) noexcept
Definition: graphicsview.cpp:340
void setSceneCursor(const tl::optional< std::pair< Point, CursorOptions >> &cursor) noexcept
Definition: graphicsview.cpp:209
bool eventFilter(QObject *obj, QEvent *event)
Definition: graphicsview.cpp:359
void setBackgroundColors(const QColor &fill, const QColor &grid) noexcept
Definition: graphicsview.cpp:128
void setGrayOut(bool grayOut) noexcept
Definition: graphicsview.cpp:177
QColor mBackgroundColor
Definition: graphicsview.h:160
void setScene(GraphicsScene *scene) noexcept
Definition: graphicsview.cpp:192
void setSceneRectMarker(const QRectF &rect) noexcept
Setup the marker for a specific scene rect.
Definition: graphicsview.cpp:204
QCursor mCursorBeforePanning
Definition: graphicsview.h:187
IF_GraphicsViewEventHandler * mEventHandlerObject
Definition: graphicsview.h:155
Qt::MouseButton mPanningButton
Definition: graphicsview.h:185
tl::optional< std::pair< Point, CursorOptions > > mSceneCursor
If not nullopt, a cursor will be shown at the given position.
Definition: graphicsview.h:170
void setInfoBoxColors(const QColor &fill, const QColor &text) noexcept
Definition: graphicsview.cpp:143
Theme::GridStyle getGridStyle() const noexcept
Definition: graphicsview.h:76
The IF_GraphicsViewEventHandler class.
Definition: if_graphicsvieweventhandler.h:41
bool mOriginCrossVisible
Definition: graphicsview.h:165
~GraphicsView() noexcept
Definition: graphicsview.cpp:111
void setOriginCrossVisible(bool visible) noexcept
Definition: graphicsview.cpp:227
QScopedPointer< WaitingSpinnerWidget > mWaitingSpinnerWidget
Definition: graphicsview.h:153
void showWaitingSpinner() noexcept
Definition: graphicsview.cpp:328
QColor mGridColor
Definition: graphicsview.h:161
The Circle class.
Definition: circle.h:46
The GraphicsScene class.
Definition: graphicsscene.h:45
void cursorScenePositionChanged(const Point &pos)
Cursor scene position changed signal.
bool mUseOpenGl
Definition: graphicsview.h:166
GridStyle
Definition: theme.h:49
int xScale
Definition: graphicsview.h:174
CursorOption
Definition: graphicsview.h:56
Theme::GridStyle mGridStyle
Definition: graphicsview.h:158
PositiveLength mGridInterval
Definition: graphicsview.h:159
void zoomOut() noexcept
Definition: graphicsview.cpp:302
GraphicsView(const GraphicsView &other)=delete
GraphicsScene * mScene
Definition: graphicsview.h:156
GraphicsScene * getScene() const noexcept
Definition: graphicsview.h:70
type_safe::constrained_type< Length, PositiveLengthConstraint, PositiveLengthVerifier > PositiveLength
Definition: length.h:812
void drawForeground(QPainter *painter, const QRectF &rect)
Definition: graphicsview.cpp:501
void zoomAll() noexcept
Definition: graphicsview.cpp:308
QColor mOverlayFillColor
Definition: graphicsview.h:162
Length currentTickInterval
Definition: graphicsview.h:178
void setInfoBoxText(const QString &text) noexcept
Definition: graphicsview.cpp:221
void handleMouseWheelEvent(QGraphicsSceneWheelEvent *event) noexcept
Definition: graphicsview.cpp:268
The Length class is used to represent a length (for example 12.75 millimeters)
Definition: length.h:83
LengthUnit unit
Definition: graphicsview.h:175
volatile bool mPanningActive
Definition: graphicsview.h:184
void setVisibleSceneRect(const QRectF &rect) noexcept
Definition: graphicsview.cpp:200
The LengthUnit class represents a length unit (millimeters, inches,...) and provides some useful meth...
Definition: lengthunit.h:60
QScopedPointer< QLabel > mInfoBoxLabel
Definition: graphicsview.h:154
void zoomIn() noexcept
Definition: graphicsview.cpp:296
QPainterPath calcPosWithTolerance(const Point &pos, qreal multiplier=1) const noexcept
Definition: graphicsview.cpp:256