-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathgraph_view.h
More file actions
382 lines (308 loc) · 10.4 KB
/
graph_view.h
File metadata and controls
382 lines (308 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
#ifndef GRAPH_VIEW_H
#define GRAPH_VIEW_H
#include <QGraphicsView>
#include <QGraphicsScene>
#include <QMouseEvent>
#include <QWheelEvent>
#include <QResizeEvent>
#include <QQueue>
#include <QFile>
#include <QTextStream>
#include <QScrollBar>
#include <QLabel>
//Header for MyVex class
#include <QGraphicsEllipseItem>
#include <QVector>
//Headers for lines
#include <QPainter>
#include <QColor>
#include <QGraphicsPathItem>
#include <QTimeLine>
#include <QEasingCurve>
#if (QT_VERSION < QT_VERSION_CHECK(6,0,0))
#include <QtMath>
#endif
class viewLog : public QLabel{
Q_OBJECT
private:
QFont logFont = QFont("Corbel", 12);
QString logText;
void resizeEvent(QResizeEvent *event);
public:
viewLog(QString log, QWidget *parent = nullptr);
};
class MyGraphicsView;
class MyGraphicsVexItem;
class MyGraphicsLineItem;
// Summary:
// MyGraphicsView class is customized for visualizing graph
//
// Functions:
// -Left click to add vex
// -Left click on vex to add arc
// -Right click on vex or arc to view information
// -Drag vex to adjust place (adjust connected arcs either)
class MyGraphicsView : public QGraphicsView{
Q_OBJECT
private:
enum mouseStates{
NORMAL = 0b00000000,
ON_HOVER = 0b00010000,
ON_SELECTED = 0b00100000,
ON_MOVING = 0b01000000
};
enum itemStates{
NON = 0b00000000,
SEL = 0b00010000,
ADD = 0b00100000,
VEX = 0b00000001,
LINE = 0b00000010
};
QGraphicsScene* myGraphicsScene;
int type;
int vexID = 0;
int getIdOf(MyGraphicsVexItem* vex){return vexes.indexOf(vex);}
int mouseState = NORMAL;
int itemState = NON;
bool onRightPress = false;
//For dragging and scaling
bool onMiddlePress = false;
QPointF lastPos;
QGraphicsItem *selItem = nullptr;
MyGraphicsVexItem *strtVex = nullptr;
QGraphicsItem *sketchItem = nullptr;
qreal zValue = -1;
/* Animation loop */
QQueue<QTimeLine*> aniQueue;
bool onAni = false;
QTimeLine *curAni = nullptr;
qreal speedRate = 1;
void nextAni();
void mousePressEvent(QMouseEvent* event);
void mouseReleaseEvent(QMouseEvent* event);
void mouseMoveEvent(QMouseEvent* event);
void wheelEvent(QWheelEvent *event);
void resizeEvent(QResizeEvent *event){this->setSceneRect(this->rect());}
void changeCursor();
MyGraphicsVexItem* addVex(QPointF center, qreal radius = 10);
void clearSketch();
void sketchLine(QPointF start, QPointF end);
void addLine(MyGraphicsVexItem* start, MyGraphicsVexItem* end);
public:
enum { UDG = 128, DG = 256 };
int vexNum = 0;
int arcNum = 0;
/* For Saving */
QVector<MyGraphicsVexItem*> vexes;
QVector<MyGraphicsLineItem*> lines;
/* for visit flag */
bool hasVisitedItem = false;
MyGraphicsView(int _type = UDG, QWidget *parent = nullptr);
MyGraphicsVexItem* selectedVex();
MyGraphicsLineItem* selectedArc();
void RemoveVex(MyGraphicsVexItem *vex);
void RemoveArc(MyGraphicsLineItem *line);
void HideUnvisited();
void ShowUnvisited();
void setAniRate(qreal rate){speedRate = rate;}
void setType(int _type){type = _type;}
void unSelect(){itemState = NON;selItem = nullptr;emit unselected();}
void SaveToFile(QTextStream &ts);
void ReadFromFile(QTextStream &ts);
signals:
void mouseMoved(QPointF position);
void mouseLeftClicked(QPointF position);
void mouseRightClicked(QPointF position);
void mouseReleased();
void vexAdded(MyGraphicsVexItem *vex);
void arcAdded(MyGraphicsLineItem *arc);
void logAdded(viewLog *log);
void selected(QGraphicsItem *item);
void unselected();
void visitClear();
public slots:
void setHover(bool in = true);
void setSel(QGraphicsItem *sel);
void startLine(MyGraphicsVexItem* startVex);
void setMenu(QGraphicsItem *target, bool display = true);
void addLog(viewLog *log){emit logAdded(log);}
void vexRemoved(MyGraphicsVexItem* vex){vexes.erase(vexes.begin() + vexes.indexOf(vex));vexNum--;}
void arcRemoved(MyGraphicsLineItem* line){lines.erase(lines.begin() + lines.indexOf(line));arcNum--;}
void addAnimation(QTimeLine *ani);
};
// Summary:
// MyGraphicsVexItem realize the interactions with vex
class MyGraphicsVexItem : public QObject, public QGraphicsEllipseItem{
Q_OBJECT
private:
enum {
PREPARING = 0b10000000,
UNDEFINED = 0b00000000,
ON_HOVER = 0b00000001,
ON_LEFT_CLICK = 0b00000010,
ON_RIGHT_CLICK = 0b00000100,
ON_SELECTED = 0b00001000,
ON_LINING = 0b00010000,
ON_MENU = 0b00100000,
ON_VISIT = 0b01000000,
ON_ACCESS = 0b10000000
};
private:
static unsigned int internalID;
QBrush regBrush = QBrush(QColor(58, 143, 192));
QBrush selBrush = QBrush(QColor(208, 90, 110));
QBrush visitedBrush = QBrush(QColor(0, 137, 108));
QBrush accessBrush = QBrush(QColor(152, 109, 178));
QPointF center;
qreal radius;
int state = UNDEFINED;
QTimeLine* curAnimation = nullptr;
QVector<MyGraphicsLineItem*> linesStartWith;
QVector<MyGraphicsLineItem*> linesEndWith;
/* For display temporary tag */
QGraphicsSimpleTextItem *nameTag = nullptr;
QString nameText = "";
QFont nameFont = QFont("Corbel", 13, QFont::Normal, true);
QGraphicsSimpleTextItem *tag = nullptr;
QString hintText = "";
QFont hintFont = QFont("Corbel", 12);
void displayText();
void hoverInEffect();
void hoverOutEffect();
void onClickEffect();
void onReleaseEffect();
void startAnimation();
void stopAnimation();
void move(QPointF position);
public:
enum { Type = UserType + 1 };
int id;
MyGraphicsVexItem(QPointF _center, qreal _r, int nameID = 0, QGraphicsItem *parent = nullptr);
/* initializing */
void estConnection(MyGraphicsView* view);
void showAnimation();
void select();
void visit(bool visited = true);
void access(const QString &hint = "", bool isAccess = true);
void itemHide();
void itemShow();
QString Text(){return nameText;}
void setText(const QString & text){nameTag->setText(text);nameText = text;}
void addStartLine(MyGraphicsLineItem *line){linesStartWith.push_back(line);}
void removeStartLine(MyGraphicsLineItem *line){linesStartWith.remove(linesStartWith.indexOf(line));}
void addEndLine(MyGraphicsLineItem *line){linesEndWith.push_back(line);}
void removeEndLine(MyGraphicsLineItem *line){linesEndWith.remove(linesEndWith.indexOf(line));}
void remove();
bool equalTo(MyGraphicsVexItem *v){return id == v->id;}
int type() const override {return Type;}
bool isVisited(){return state & ON_VISIT;}
qreal getRadius() {return radius;}
QString getData(){return QString::asprintf("%g %g %g\n", center.x(), center.y(), radius)+nameText;}
signals:
void setHover(bool in = true);
void selected(QGraphicsItem *sel);
void lineFrom(MyGraphicsVexItem *start);
void menuStateChanged(QGraphicsItem *item, bool display = true);
void logAdded(viewLog *log);
void removed(MyGraphicsVexItem *vex);
void addAnimation(QTimeLine *ani);
public slots:
void onMouseMove(QPointF position);
void onLeftClick(QPointF position);
void onRightClick(QPointF position);
void onMouseRelease();
};
class MyGraphicsLineItem : public QObject, public QGraphicsLineItem{
Q_OBJECT
private:
enum {
UNDEFINED = 0b00000000,
ON_HOVER = 0b00000001,
ON_LEFT_CLICK = 0b00000010,
ON_RIGHT_CLICK = 0b00000100,
ON_SELECTED = 0b00001000,
ON_MENU = 0b00100000,
ON_VISIT = 0b01000000
};
/* basic data */
bool hasDirection;
MyGraphicsVexItem *startVex;
MyGraphicsVexItem *endVex;
QGraphicsLineItem *line1 = nullptr;
QGraphicsLineItem *line2 = nullptr;
QGraphicsPathItem *arrow = nullptr;
QGraphicsSimpleTextItem *textItem = nullptr;
QString text = "";
int state = UNDEFINED;
/* about animation */
QTimeLine *curAnimation;
/* detail of the line */
qreal lineWidth = 3;
qreal arrowLength = 10;
Qt::PenStyle lineStyle = Qt::SolidLine;
Qt::PenCapStyle capStyle = Qt::RoundCap;
QColor defaultColor = QColor(125, 185, 222);
QColor hoverColor = QColor(0, 98, 132);
QColor selColor = QColor(208, 90, 110);
QColor visitColor = QColor(0, 137, 108);
QColor accessColor = QColor(178, 143, 206);
QPen defaultPen;
QPen curPen;
QFont textFont = QFont("Corbel", 12);
QColor textColor = QColor(0, 0, 0);
/* for calculation and line rendering */
qreal angle = 0;
QPointF center;
QPointF sP, eP, dP;
void setLengthRate(qreal r=1);
void drawLine();
void drawText();
void drawArrow();
void delArrow();
/* effects */
void hoverInEffect();
void hoverOutEffect();
void onClickEffect();
void onReleaseEffect();
void onSelectEffect();
void deSelectEffect();
public:
enum { Type = UserType + 2 };
MyGraphicsLineItem(MyGraphicsVexItem *start, MyGraphicsVexItem *end, bool hasDir = false, QGraphicsItem *parent = nullptr);
/* initialize functions */
void estConnection(MyGraphicsView *view);
void refrshLine();
/* adjust functions */
void reverseDirection();
void moveStart(MyGraphicsVexItem *start);
void moveEnd(MyGraphicsVexItem *end);
void setText(const QString & _text);
void setDirection(bool hasDir = true);
/* effects */
//void startAnimation(){}
//void stopAnimation(){}
/* retrieve */
MyGraphicsVexItem* stVex(){return startVex;}
MyGraphicsVexItem* edVex(){return endVex;}
QString weightText(){return text;}
void visit(bool visit = true);
void itemHide();
void itemShow();
void remove();
void access();
int type() const override {return Type;}
bool isVisited(){return state & ON_VISIT;}
signals:
void setHover(bool in = true);
void selected(QGraphicsItem *sel);
void menuStateChanged(QGraphicsItem *item, bool display = true);
void logAdded(viewLog *log);
void removed(MyGraphicsLineItem *line);
void addAnimation(QTimeLine *ani);
private slots:
void onMouseMove(QPointF position);
void onLeftClick(QPointF position);
void onRightClick(QPointF position);
void onMouseRelease();
};
#endif // GRAPH_VIEW_H