OpenTwin 0.1
OpenTwin
 
Loading...
Searching...
No Matches
GraphicsScene.h
Go to the documentation of this file.
1
4// ###########################################################################################################################################################################################################################################################################################################################
5
6#pragma once
7
8// OpenTwin header
9#include "OTCore/Flags.h"
10#include "OTGui/Grid.h"
11#include "OTGui/Margins.h"
14
15// Qt header
16#include <QtWidgets/qgraphicsscene.h>
17
18// std header
19#include <list>
20
21namespace ot {
22
23 class GraphicsView;
24 class GraphicsItem;
25 class GraphicsElement;
26 class GraphicsConnectionItem;
27 class GraphicsConnectionPreviewItem;
28
31 class OT_WIDGETS_API_EXPORT GraphicsScene : public QGraphicsScene {
32 Q_OBJECT
33 public:
35 GraphicsScene(const QRectF& _sceneRect, GraphicsView* _view);
36 virtual ~GraphicsScene();
37
38 // ###########################################################################################################################################################################################################################################################################################################################
39
40 // Connection handling
41
44 void startConnection(ot::GraphicsItem* _item);
45
50 void startConnectionToConnection(ot::GraphicsConnectionItem* _targetedConnection, const Point2DD& _pos);
51
52 void stopConnection(void);
53
54 // ###########################################################################################################################################################################################################################################################################################################################
55
56 // Setter / Getter
57
58 void setGrid(const Grid& _grid) { m_grid = _grid; };
59 const Grid& getGrid(void) const { return m_grid; };
60
61 void setGridStep(int _step) { m_grid.setGridStep(_step); };
62 void setGridStep(const Point2D& _step) { m_grid.setGridStep(_step); };
63 Point2D getGridStep(void) const { return m_grid.getGridStep(); };
64
65 void setWideGridLineCounter(int _counter) { m_grid.setWideGridLineCounter(_counter); };
66 void setWideGridLineCounter(const Point2D& _counter) { m_grid.setWideGridLineCounter(_counter); };
67 Point2D getWideGridLineCounter(void) const { return m_grid.getWideGridLineCounter(); };
68
69 void setGridFlag(Grid::GridFlag _flag, bool _active = true) { m_grid.setGridFlag(_flag, _active); };
70 void setGridFlags(const Grid::GridFlags& _flags) { m_grid.setGridFlags(_flags); };
71 const Grid::GridFlags& getGridFlags(void) const { return m_grid.getGridFlags(); };
72
73 void setGridSnapMode(Grid::GridSnapMode _mode) { m_grid.setGridSnapMode(_mode); };
74 Grid::GridSnapMode getGridSnapMode(void) const { return m_grid.getGridSnapMode(); };
75
76 void setGridLineStyle(const OutlineF& _outline) { m_grid.setGridLineStyle(_outline); };
77 const OutlineF& getGridLineStyle(void) const { return m_grid.getGridLineStyle(); };
78
79 void setMultiselectionEnabled(bool _enabled) { m_multiselectionEnabled = _enabled; };
80 bool getMultiselectionEnabled(void) const { return m_multiselectionEnabled; };
81
82 void checkMaxTriggerDistance(double _triggerDistance);
83 void checkMaxTriggerDistance(const MarginsD& _triggerDistance);
84
85 GraphicsView* getGraphicsView(void) { return m_view; };
86 const GraphicsView* getGraphicsView(void) const { return m_view; };
87
88 void setConnectionPreviewShape(GraphicsConnectionCfg::ConnectionShape _shape) { m_connectionPreviewShape = _shape; };
89 GraphicsConnectionCfg::ConnectionShape getConnectionPreviewShape(void) const { return m_connectionPreviewShape; };
90
91 void setIgnoreEvents(bool _ignore) { m_ignoreEvents = _ignore; };
92 bool getIgnoreEvents(void) const { return m_ignoreEvents; };
93
94 QPointF snapToGrid(const QPointF& _pt) const;
95
97 QPointF snapToGrid(const GraphicsItem* _item) const;
98
99 void deselectAll(void);
100
101 void moveAllSelectedItems(const Point2DD& _delta);
102
103 void rotateAllSelectedItems(double _relativeAngle);
104
105 void flipAllSelectedItems(Qt::Orientation _flipAxis);
106
107 void itemAboutToBeRemoved(GraphicsItem* _item);
108
109 void connectionAboutToBeRemoved(GraphicsConnectionItem* _connection);
110
111 void elementAboutToBeRemoved(GraphicsElement* _element);
112
113 void handleSelectionChanged(void);
114
115 Q_SIGNALS:
118
119 // ###########################################################################################################################################################################################################################################################################################################################
120
121 // Public: Slots
122
123 public Q_SLOTS:
124 void slotSelectionChanged(void);
125
126 // ###########################################################################################################################################################################################################################################################################################################################
127
128 // Protected: Event handling
129 protected:
130 virtual void mouseDoubleClickEvent(QGraphicsSceneMouseEvent* _event) override;
131 virtual void mouseMoveEvent(QGraphicsSceneMouseEvent* _event) override;
132 virtual void mousePressEvent(QGraphicsSceneMouseEvent* _event) override;
133 virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent* _event) override;
134
135 // ###########################################################################################################################################################################################################################################################################################################################
136
137 // Protected: Helper
138
139 virtual void drawBackground(QPainter* _painter, const QRectF& _rect) override;
140
141 void drawGrid(QPainter* _painter, const QRectF& _rect);
142
143 qreal calculateScaledGridLineWidth(QPainter* _painter, qreal _normalWidth) const;
144
145 void calculateGridLines(const QRectF& _painterRect, QList<QLineF>& _normalLines, QList<QLineF>& _wideLines, QList<QLineF>& _centerLines) const;
146
147 Point2D calculateScaledGridStepSize(const QRectF& _rect) const;
148
149 GraphicsElement* findClosestConnectableElement(const QPointF& _pos) const;
150
151 QList<QGraphicsItem*> findItemsInTriggerDistance(const QPointF& _pos) const;
152
153 private:
154 void handleMultiSelectionChanged(void);
155 void handleSingleSelectionChanged(void);
156
157
158 std::list<GraphicsElement *> m_lastHoverElements;
159
160 bool m_ignoreEvents;
161 double m_maxTriggerDistance;
162 bool m_multiselectionEnabled;
163 Grid m_grid;
164 GraphicsView* m_view;
165 GraphicsItem* m_connectionOrigin;
166 GraphicsConnectionPreviewItem* m_connectionPreview;
167 ot::GraphicsConnectionCfg::ConnectionShape m_connectionPreviewShape;
168 QList<QGraphicsItem*> m_lastSelection;
169 bool m_mouseIsPressed;
170
171 GraphicsScene() = delete;
172 };
173
174}
#define OT_WIDGETS_API_EXPORT
Definition OTWidgetsAPIExport.h:12
ConnectionShape
Definition GraphicsConnectionCfg.h:24
Definition GraphicsConnectionItem.h:25
Definition GraphicsConnectionPreviewItem.h:23
Definition GraphicsElement.h:28
Base class for all OpenTwin GraphicsItems GraphicsItems should be created by the GraphicsFactory and ...
Definition GraphicsItem.h:35
Graphics Scene for ot::GraphicsItem Adding QGraphicsItems to the scene that do not inherit ot::Graphi...
Definition GraphicsScene.h:31
GraphicsConnectionCfg::ConnectionShape getConnectionPreviewShape(void) const
Definition GraphicsScene.h:89
void setGridStep(int _step)
Definition GraphicsScene.h:61
Grid::GridSnapMode getGridSnapMode(void) const
Definition GraphicsScene.h:74
void setGridFlag(Grid::GridFlag _flag, bool _active=true)
Definition GraphicsScene.h:69
void setConnectionPreviewShape(GraphicsConnectionCfg::ConnectionShape _shape)
Definition GraphicsScene.h:88
void setGrid(const Grid &_grid)
Definition GraphicsScene.h:58
void setGridFlags(const Grid::GridFlags &_flags)
Definition GraphicsScene.h:70
void setGridSnapMode(Grid::GridSnapMode _mode)
Definition GraphicsScene.h:73
void setWideGridLineCounter(const Point2D &_counter)
Definition GraphicsScene.h:66
const Grid::GridFlags & getGridFlags(void) const
Definition GraphicsScene.h:71
void graphicsItemDoubleClicked(const ot::GraphicsItem *_item)
const OutlineF & getGridLineStyle(void) const
Definition GraphicsScene.h:77
Point2D getGridStep(void) const
Definition GraphicsScene.h:63
void setIgnoreEvents(bool _ignore)
Definition GraphicsScene.h:91
GraphicsView * getGraphicsView(void)
Definition GraphicsScene.h:85
bool getIgnoreEvents(void) const
Definition GraphicsScene.h:92
void setGridStep(const Point2D &_step)
Definition GraphicsScene.h:62
void setWideGridLineCounter(int _counter)
Definition GraphicsScene.h:65
void setMultiselectionEnabled(bool _enabled)
Definition GraphicsScene.h:79
const GraphicsView * getGraphicsView(void) const
Definition GraphicsScene.h:86
bool getMultiselectionEnabled(void) const
Definition GraphicsScene.h:80
Point2D getWideGridLineCounter(void) const
Definition GraphicsScene.h:67
const Grid & getGrid(void) const
Definition GraphicsScene.h:59
void setGridLineStyle(const OutlineF &_outline)
Definition GraphicsScene.h:76
void selectionChangeFinished(void)
View widget used to display GraphicsItems Note that the View creates its own scene.
Definition GraphicsView.h:37
The Grid describes how a 2D Grid should look and behave like.
Definition Grid.h:25
GridSnapMode
The GridSnapMode describe if and how the grid snapping should be performed.
Definition Grid.h:44
GridFlag
A GridFlag describe the grid appearance.
Definition Grid.h:29
Definition Margins.h:98
The OutlineF class is used to describe how a outline should look like.
Definition Outline.h:161
2D Point with double values
Definition Point2D.h:144
2D Point with integer values
Definition Point2D.h:20
Definition Connector.h:8