Code Documentation 3.7
Social Network Visualizer
Loading...
Searching...
No Matches
graphicswidget.h
Go to the documentation of this file.
1
15
16
17#ifndef GRAPHICSWIDGET_H
18#define GRAPHICSWIDGET_H
19
20
21#include <QGraphicsView>
22#include <QHash>
23#include <QTimer>
24#include <QLoggingCategory>
25#include "global.h"
26
28
29// WS14: shared across graphicswidget.cpp and the small canvas item files (graphicsnode.cpp,
30// graphicsedgelabel.cpp, graphicsedgeweight.cpp, graphicsnodelabel.cpp, graphicsnodenumber.cpp) --
31// promoted from graphicswidget.cpp-local since those files are all part of the same WS10 canvas
32// subsystem and each has too few qDebug() calls to justify a category of its own.
33Q_DECLARE_LOGGING_CATEGORY(lcGW)
34
35class MainWindow;
36class GraphicsNode;
37class GraphicsEdge;
40class GraphicsGuide;
43
44typedef QHash<quint64, GraphicsEdge*> H_KeyToEdge;
45typedef QHash <int, GraphicsNode*> H_NumToNode;
46
47
48
49class GraphicsWidget : public QGraphicsView {
50 Q_OBJECT
51
52public:
53
54 GraphicsWidget(QGraphicsScene *m_scene, MainWindow *m_parent);
56
57 void clear();
58
68 bool isClearing() const { return m_isClearing; }
69
70 quint64 edgeKey(const int &v1,
71 const int &v2,
72 const int &relation=-1);
73
74 void setInitNodeSize(int);
75
76 void setInitZoomIndex (const int &);
77 void setMaxZoomIndex (const int &);
78
79 void setSelectedNodes(const QList<int> &list);
80
81 QList<QGraphicsItem *> selectedItems();
82 QList<int> selectedNodes();
83 QList<SelectedEdge> selectedEdges();
84
85 void selectAll();
86 void selectNone();
87
89 void removeItem(GraphicsEdgeWeight *edgeWeight);
90 void removeItem(GraphicsEdgeLabel *edgeLabel);
94
95 void setNumbersInsideNodes(const bool &toggle);
96
97 void setAllItemsVisibility(int, bool);
98
99protected:
100
101 void wheelEvent(QWheelEvent *event);
102 void mouseDoubleClickEvent ( QMouseEvent * e );
103 void mousePressEvent ( QMouseEvent * e );
104 void mouseReleaseEvent(QMouseEvent * e );
105 void resizeEvent( QResizeEvent *e );
106
107public slots:
108
110
111 void relationSet(int relation);
112
113 void drawNode(const QPointF &p,
114 const int &num,
115 const int &nodeSize,
116 const QString &nodeShape,
117 const QString &nodeIconPath,
118 const QString &nodeColor,
119 const QString &numberColor,
120 const int &numberSize,
121 const int &numberDistance,
122 const QString &nodeLabel,
123 const QString &labelColor,
124 const int &labelSize,
125 const int &labelDistance);
126 void removeNode(const int &nodeNum);
127 void setNodeVisibility(const int &nodeNum, const bool &toggle ); //Called from Graph via MW
129 void moveNode(const int &num, const qreal &x, const qreal &y);
130
131 bool setNodeSize(const int &nodeNumber, const int &size=0);
132
133 bool setNodeShape(const int &nodeNum,
134 const QString &shape,
135 const QString &iconPath=QString());
136 bool setNodeColor(const int &nodeNum, const QString &color);
137
138 void setNodeNumberColor(const int &nodeNum, const QString &color);
139 void setNodeNumberVisibility(const bool &toggle);
140 bool setNodeNumberSize(const int &nodeNum, const int &size=0);
141 bool setNodeNumberDistance(const int &, const int &distance=0);
142
143 void setNodeLabelsVisibility(const bool &toggle);
144 bool setNodeLabelColor(const int &nodeNum, const QString &color="green");
145 bool setNodeLabelSize(const int &, const int &size=0);
146 bool setNodeLabel(const int & , const QString &label);
147 bool setNodeLabelDistance(const int &, const int &distance=0);
148
149 void drawEdge(const int &source,
150 const int &target,
151 const qreal &weight,
152 const QString &label="",
153 const QString &color="black",
154 const int &type=0,
155 const bool &drawArrows=true,
156 const bool &bezier=false,
157 const bool &weightNumbers=false);
158
159 void removeEdge(const int &source,
160 const int &target,
161 const bool &removeOpposite=false);
162
163 void setEdgeVisibility (const int &relation,
164 const int &sourceNum,
165 const int &targetNum,
166 const bool &visible,
167 const bool &preserveReverseEdge=false,
168 const int &edgeWeight=1,
169 const int &reverseEdgeWeight=1);
170
171 // Bulk counterpart of setEdgeVisibility (WS3 M2): applies a whole batch in one queued
172 // dispatch instead of one per edge - see Graph::signalSetEdgesVisibilityBatch.
173 void setEdgesVisibilityBatch(const QList<EdgeVisibilityChange> &changes);
174
175 bool setEdgeDirectionType(const int &source,
176 const int &target,
177 const int &dirType=false);
178
179 bool setEdgeWeight(const int &, const int &, const qreal &);
180
181 void setEdgeLabel(const int &source, const int &target, const QString &label);
182
183 void setEdgeColor(const int &, const int&, const QString &);
184
185 void setEdgeClicked(GraphicsEdge *, const bool &openMenu=false);
186
187 void setEdgeOffsetFromNode(const int &source,
188 const int &target,
189 const int &offset);
190 void setEdgeArrowsVisibility(const bool &toggle);
191 void setEdgeArrowSize(const int &size);
192 void setEdgeWeightNumbersVisibility (const bool &toggle);
193 void setEdgeLabelsVisibility(const bool &toggle);
194
195 void setEdgesBezier(const bool &toggle);
196
197 void setEdgeHighlighting(const bool &toggle);
198
199 void selectPath(const QList<int> &path);
200
202
203 void clearGuides();
204 void addGuideCircle( const double&x0, const double&y0, const double&radius);
205 void addGuideHLine(const double &y0);
206
207 void zoomIn(const int step = 1);
208 void zoomOut(const int step = 1);
209 void zoomToFit();
210 void rotateLeft();
211 void rotateRight();
212 void changeMatrixScale(const int value);
213 void changeMatrixRotation(int angle);
214 void reset();
215
216 void setOptionsOpenGL(const bool &enabled=false);
217 void setOptionsAntialiasing(const bool &toggle);
218 void setOptionsNoAntialiasingAutoAdjust(const bool &toggle);
219
220protected:
221 void scrollContentsBy(int dx, int dy) override;
222
223signals:
225 void userDoubleClickNewNode(const QPointF &);
226
229 void userMiddleClicked(const int &sourceNum, const int &targetNum, const qreal &weight=1);
230
232 void userClickOnEmptySpace(const QPointF &p);
233
236
238 void openContextMenu(const QPointF p);
239
242 void userNodeMoved(const int &, const int &, const int &);
243
246 void userSelectedItems(const QList<int> selectedNodes,
247 const QList<SelectedEdge> selectedEdges);
248
251 void userClickedNode(const int &nodeNumber, const QPointF &p);
252
254 void userClickedEdge(const int &source, const int &target, const bool &openMenu=false);
255
257 void zoomChanged(const int);
258
260 void rotationChanged(const int);
261
263 void resized(const int, const int);
264
266 void setCursor(Qt::CursorShape);
267
268
269private:
270
271 H_NumToNode nodeHash; //Our basic hash table for node items
272 H_KeyToEdge edgesHash; // Our basic hash table for edge items
273 QList<int> m_selectedNodes;
274 QList<SelectedEdge> m_selectedEdges;
282 qreal fX,fY, factor;
284 quint64 m_edgeKey;
295 QList<GraphicsGuide *> m_guides;
300};
301
302#endif
Definition graphicsedge.h:51
Definition graphicsedgelabel.h:28
Definition graphicsedgeweight.h:28
Definition graphicsguide.h:30
Definition graphicsnode.h:49
Definition graphicsnodelabel.h:26
Definition graphicsnodenumber.h:27
int m_edgeMinOffsetFromNode
Definition graphicswidget.h:279
void moveNode(const int &num, const qreal &x, const qreal &y)
Moves the node with the given number to the new coordinates.
Definition graphicswidget.cpp:480
void drawEdge(const int &source, const int &target, const qreal &weight, const QString &label="", const QString &color="black", const int &type=0, const bool &drawArrows=true, const bool &bezier=false, const bool &weightNumbers=false)
Draws a new edge in the scene, from node with sourceNum to node with targetNum.
Definition graphicswidget.cpp:345
H_KeyToEdge edgesHash
Definition graphicswidget.h:272
int m_zoomIndex
Definition graphicswidget.h:277
QString m_labelColor
Definition graphicswidget.h:283
bool setNodeLabelSize(const int &, const int &size=0)
Changes the label size of a node to 'size'.
Definition graphicswidget.cpp:884
void setInitNodeSize(int)
Sets initial node size from MW.
Definition graphicswidget.cpp:734
void selectNone()
Clears any item selection from the scene. Also signals that no node is clicked.
Definition graphicswidget.cpp:1387
void setNodeNumberColor(const int &nodeNum, const QString &color)
Changes the color of a node number.
Definition graphicswidget.cpp:819
QString m_numberColor
Definition graphicswidget.h:283
QList< QGraphicsItem * > selectedItems()
Returns a list of all selected QGraphicsItem(s).
Definition graphicswidget.cpp:1421
void setEdgeColor(const int &, const int &, const QString &)
Sets the color of an edge.
Definition graphicswidget.cpp:961
bool setEdgeDirectionType(const int &source, const int &target, const int &dirType=false)
Changes the direction type of an existing edge.
Definition graphicswidget.cpp:982
void userDoubleClickNewNode(const QPointF &)
Double-click on empty canvas space; MW creates a new node at this scene position.
int m_arrowSize
Definition graphicswidget.h:280
void userMiddleClicked(const int &sourceNum, const int &targetNum, const qreal &weight=1)
Two consecutive middle-clicks (or double-clicks) on two different nodes; MW/Graph creates a new edge ...
QList< SelectedEdge > m_selectedEdges
Definition graphicswidget.h:274
void userNodeMoved(const int &, const int &, const int &)
One node from the current selection finished being dragged to a new position (nodeNumber,...
~GraphicsWidget()
Destructor. Calls the method to clear the data.
Definition graphicswidget.cpp:1959
GraphicsNode * secondDoubleClickedNode
Definition graphicswidget.h:296
void resized(const int, const int)
Canvas viewport was resized (debounced to fire once, 150ms after the last resize event).
void userClickedEdge(const int &source, const int &target, const bool &openMenu=false)
An edge was clicked (source, target); openMenu requests MW open its context menu.
bool hasDoubleClickedNode
Definition graphicswidget.h:290
void setNodeLabelsVisibility(const bool &toggle)
Toggles the visibility of all node labels.
Definition graphicswidget.cpp:684
bool setNodeNumberSize(const int &nodeNum, const int &size=0)
Changes the size of the number of a node.
Definition graphicswidget.cpp:833
void changeMatrixRotation(int angle)
Rotates the view transformation by angle degrees clockwise, while preserving the current scale.
Definition graphicswidget.cpp:1810
int markedEdgeSourceOrigSize
Definition graphicswidget.h:278
GraphicsWidget(QGraphicsScene *m_scene, MainWindow *m_parent)
Constructs a GraphicsWidget object.
Definition graphicswidget.cpp:52
void zoomToFit()
Scales and centres the view so that the full network is visible.
Definition graphicswidget.cpp:1844
bool setEdgeWeight(const int &, const int &, const qreal &)
Sets the weight of an edge.
Definition graphicswidget.cpp:1008
void reset()
Resets to default rotation, zoom and scale.
Definition graphicswidget.cpp:1897
void userClickedNode(const int &nodeNumber, const QPointF &p)
A node was clicked (nodeNumber, scene position); also used with nodeNumber=0 as a generic "selection ...
bool setNodeLabelColor(const int &nodeNum, const QString &color="green")
Changes the label color of a node to 'color'.
Definition graphicswidget.cpp:868
int m_zoomIndexInit
Definition graphicswidget.h:277
void selectAll()
Forces the scene to select all items. Also signals that no node is clicked.
Definition graphicswidget.cpp:1372
void drawNode(const QPointF &p, const int &num, const int &nodeSize, const QString &nodeShape, const QString &nodeIconPath, const QString &nodeColor, const QString &numberColor, const int &numberSize, const int &numberDistance, const QString &nodeLabel, const QString &labelColor, const int &labelSize, const int &labelDistance)
Draws a new node in the scene.
Definition graphicswidget.cpp:288
void userClickOnEmptySpace(const QPointF &p)
Left-click on empty canvas space, at scene position p.
void setEdgeArrowSize(const int &size)
Sets the arrow size for all edges and stores it as the default for new edges.
Definition graphicswidget.cpp:1056
H_NumToNode nodeHash
Definition graphicswidget.h:271
qreal fY
Definition graphicswidget.h:282
bool m_isTransformationActive
Definition graphicswidget.h:288
QList< SelectedEdge > selectedEdges()
Returns a QList of selected directed edges structs in the form of v1,v2.
Definition graphicswidget.cpp:1448
QTimer m_resizeTimer
Definition graphicswidget.h:294
void selectPath(const QList< int > &path)
Selects the edges that form a shortest path, exactly as if the user had Shift-clicked each edge in tu...
Definition graphicswidget.cpp:1162
void setEdgeClicked(GraphicsEdge *, const bool &openMenu=false)
Sets the clicked edge.
Definition graphicswidget.cpp:452
void setNumbersInsideNodes(const bool &toggle)
Toggles displaying node numbers in or out of nodes.
Definition graphicswidget.cpp:714
void setOptionsOpenGL(const bool &enabled=false)
Toggles openGL.
Definition graphicswidget.cpp:121
QPointF m_viewCenter
Definition graphicswidget.h:285
void zoomIn(const int step=1)
Increases the numerical zoom index of the scene by the given step.
Definition graphicswidget.cpp:1704
quint64 edgeKey(const int &v1, const int &v2, const int &relation=-1)
Packs (relation, v1, v2) into a single quint64 key for edgesHash.
Definition graphicswidget.cpp:203
void setNodeClicked(GraphicsNode *)
Clears clickedEdge and emits a signal to Graph.
Definition graphicswidget.cpp:428
void setMaxZoomIndex(const int &)
Sets the max zoom allowed (and the initial zoom as half of it). Called from MW on startup.
Definition graphicswidget.cpp:755
void removeNode(const int &nodeNum)
Deletes the node with the given number from the scene, if exists.
Definition graphicswidget.cpp:493
bool setNodeShape(const int &nodeNum, const QString &shape, const QString &iconPath=QString())
Sets the shape of an node.
Definition graphicswidget.cpp:668
bool setNodeLabelDistance(const int &, const int &distance=0)
Changes the distance of the label of the given node.
Definition graphicswidget.cpp:904
bool setNodeNumberDistance(const int &, const int &distance=0)
Changes the distance of the number of a node.
Definition graphicswidget.cpp:850
void relationSet(int relation)
Changes the current relation.
Definition graphicswidget.cpp:261
void userSelectedItems(const QList< int > selectedNodes, const QList< SelectedEdge > selectedEdges)
The full current node/edge selection, emitted whenever it changes (single emission per change,...
void mousePressEvent(QMouseEvent *e)
Handles the mouse press event.
Definition graphicswidget.cpp:1505
QList< int > m_selectedNodes
Definition graphicswidget.h:273
bool isClearing() const
True while a bulk clear() is in progress; false otherwise.
Definition graphicswidget.h:68
void addGuideCircle(const double &x0, const double &y0, const double &radius)
Adds a circular guideline.
Definition graphicswidget.cpp:1331
bool clickedEdgeExists
Definition graphicswidget.h:290
bool m_edgesBezier
Definition graphicswidget.h:293
void zoomOut(const int step=1)
Decreases the numerical zoom index of the scene by the given step.
Definition graphicswidget.cpp:1683
QList< int > selectedNodes()
Returns a List of selected node numbers.
Definition graphicswidget.cpp:1431
void handleSelectionChanged()
Handles the event of selection change in the scene.
Definition graphicswidget.cpp:1401
int m_nodeSize
Definition graphicswidget.h:275
QList< GraphicsGuide * > m_guides
Definition graphicswidget.h:295
double m_currentScaleFactor
Definition graphicswidget.h:281
void setEdgeOffsetFromNode(const int &source, const int &target, const int &offset)
Changes the offset of an edge (or all edges) from source and target nodes.
Definition graphicswidget.cpp:1072
void resizeEvent(QResizeEvent *e)
Handles the canvas resize event.
Definition graphicswidget.cpp:1920
void scrollContentsBy(int dx, int dy) override
Tracks the user's pan position so zoom can anchor to it.
Definition graphicswidget.cpp:1774
void setAllItemsVisibility(int, bool)
Toggles the visibility of all items of the given type.
Definition graphicswidget.cpp:1302
void clearGuides()
Removes and deletes all guide items previously added via addGuideCircle()/addGuideHLine(),...
Definition graphicswidget.cpp:1359
bool setNodeSize(const int &nodeNumber, const int &size=0)
Changes the size of a node.
Definition graphicswidget.cpp:784
void setEdgesBezier(const bool &toggle)
Toggles bezier-curve rendering for all edges currently in the scene.
Definition graphicswidget.cpp:1190
bool m_isZooming
Definition graphicswidget.h:287
void setEdgeLabel(const int &source, const int &target, const QString &label)
Sets the label of an edge.
Definition graphicswidget.cpp:941
GraphicsNode * markedEdgeSource
Definition graphicswidget.h:297
void rotateRight()
Rotates the view to the right, by a fixed angle.
Definition graphicswidget.cpp:1798
void setOptionsAntialiasing(const bool &toggle)
Toggles QPainter render hints for primitive edges and text antialiasing.
Definition graphicswidget.cpp:147
void removeEdge(const int &source, const int &target, const bool &removeOpposite=false)
Removes the edge from node sourceNum to node targetNum from the scene.
Definition graphicswidget.cpp:515
int markedEdgeTargetOrigSize
Definition graphicswidget.h:278
bool m_isClearing
Definition graphicswidget.h:289
void zoomChanged(const int)
Current zoom index changed; keeps the MW zoom slider in sync.
void setOptionsNoAntialiasingAutoAdjust(const bool &toggle)
Toggles QGraphicsView's antialiasing auto-adjustment of exposed areas.
Definition graphicswidget.cpp:165
void clear()
Clears the scene and all hashes, lists, var etc.
Definition graphicswidget.cpp:228
void mouseReleaseEvent(QMouseEvent *e)
Handles the mouse release events.
Definition graphicswidget.cpp:1615
void setSelectedNodes(const QList< int > &list)
Selects all nodes in the given list.
Definition graphicswidget.cpp:923
qreal fX
Definition graphicswidget.h:282
void openContextMenu(const QPointF p)
Right-click on empty canvas space, at scene position p; MW opens the general context menu.
void addGuideHLine(const double &y0)
Adds a horizonal guideline.
Definition graphicswidget.cpp:1344
void setEdgeArrowsVisibility(const bool &toggle)
Toggles the visibility of all edge arrows.
Definition graphicswidget.cpp:1037
void setEdgeVisibility(const int &relation, const int &sourceNum, const int &targetNum, const bool &visible, const bool &preserveReverseEdge=false, const int &edgeWeight=1, const int &reverseEdgeWeight=1)
Toggles the visibility of the given edge.
Definition graphicswidget.cpp:1209
bool m_nodeLabelVisibility
Definition graphicswidget.h:291
void setCursor(Qt::CursorShape)
Requests MW change the application cursor shape (e.g. while creating an edge).
void setEdgeLabelsVisibility(const bool &toggle)
Toggles all edge labels visibility.
Definition graphicswidget.cpp:1114
void setNodeVisibility(const int &nodeNum, const bool &toggle)
Changes the visibility of a Node.
Definition graphicswidget.cpp:769
int m_curRelation
Definition graphicswidget.h:275
void mouseDoubleClickEvent(QMouseEvent *e)
Handles user double-clicks.
Definition graphicswidget.cpp:1471
void rotateLeft()
Decreases the numerical rotation Rotates the view to the left, by a fixed angle.
Definition graphicswidget.cpp:1785
void openNodeMenu()
Right-click on a node; MW opens the node context menu for the currently clicked node.
QString m_nodeLabel
Definition graphicswidget.h:283
void rotationChanged(const int)
Current rotation angle changed; keeps the MW rotation slider in sync.
void setNodeNumberVisibility(const bool &toggle)
Toggles the visibility of node numbers.
Definition graphicswidget.cpp:801
void changeMatrixScale(const int value)
Does the actual zoom in or out while preserving current rotation.
Definition graphicswidget.cpp:1726
int m_zoomIndexMax
Definition graphicswidget.h:277
void setEdgesVisibilityBatch(const QList< EdgeVisibilityChange > &changes)
Applies a batch of edge visibility changes in one call (WS3 M2).
Definition graphicswidget.cpp:1287
GraphicsEdge * clickedEdge
Definition graphicswidget.h:299
GraphicsNode * firstDoubleClickedNode
Definition graphicswidget.h:296
bool m_edgeHighlighting
Definition graphicswidget.h:292
bool m_viewCenterValid
Definition graphicswidget.h:286
void setEdgeWeightNumbersVisibility(const bool &toggle)
Toggles all edge weight numbers visibility.
Definition graphicswidget.cpp:1100
void handleDoubleClickOnNode(GraphicsNode *node)
Handles double-clicks (or middle-clicks) on the given node, creating a new edge if needed.
Definition graphicswidget.cpp:401
bool setNodeLabel(const int &, const QString &label)
Sets the label of an node.
Definition graphicswidget.cpp:701
bool m_nodeNumberVisibility
Definition graphicswidget.h:291
bool m_nodeNumbersInside
Definition graphicswidget.h:291
quint64 m_edgeKey
Definition graphicswidget.h:284
GraphicsNode * markedEdgeTarget
Definition graphicswidget.h:298
void setInitZoomIndex(const int &)
Sets the initial zoom setting. Use setMaxZoomIndex() instead.
Definition graphicswidget.cpp:745
void removeItem(GraphicsEdge *)
Removes an edge item from the scene.
Definition graphicswidget.cpp:586
void setEdgeHighlighting(const bool &toggle)
Enables or disables edge highlighting.
Definition graphicswidget.cpp:1133
bool setNodeColor(const int &nodeNum, const QString &color)
Sets the color of an node.
Definition graphicswidget.cpp:653
qreal factor
Definition graphicswidget.h:282
void wheelEvent(QWheelEvent *event)
Handles the mouse wheel event. If CTRL is pressed, zooms in or out.
Definition graphicswidget.cpp:1662
int m_currentRotationAngle
Definition graphicswidget.h:276
The base window of SocNetV contains all widgets and functionality.
Definition mainwindow.h:118
Global definitions, constants, enumerations, and utility types for SocNetV.
#define SOCNETV_USE_NAMESPACE
Definition global.h:28
QHash< int, GraphicsNode * > H_NumToNode
Definition graphicswidget.h:45
QHash< quint64, GraphicsEdge * > H_KeyToEdge
Definition graphicswidget.h:44