-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqgraphicsnodeitem.h
68 lines (61 loc) · 2.18 KB
/
qgraphicsnodeitem.h
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
/*
MIT License
Copyright (c) 2021 Ramesh Choudhary
*/
#ifndef QGRAPHICSNODEITEM_H
#define QGRAPHICSNODEITEM_H
#include<QGraphicsObject>
#include<QPainter>
#include<QVector2D>
class QGraphicsEdgeItem;
class Spring;
class QGraphicsNodeItem:public QGraphicsObject
{
friend QGraphicsEdgeItem;
public:
QGraphicsNodeItem(QGraphicsScene * scene, QPointF pos, int radius,
int NodeIndex, float * drag, int * maxSpeed,
QGraphicsItem *parent = nullptr, QString nodeName=QString());
virtual ~QGraphicsNodeItem();
void connectEdgeWith(QGraphicsNodeItem * node2,int edgePenWidth,bool directed=true);
void disconnectEdgeWith(QGraphicsNodeItem * node2);
bool hasEdgeWith(QGraphicsNodeItem * node2);
bool hasDirectedEdgeWith(QGraphicsNodeItem * node2);
void connectMechanicalSpringWith(QGraphicsNodeItem * node2,float * restingLength);
void connectElectricalSpringWith(QGraphicsNodeItem * node2, int *charge);
void disconnectSpringWith(QGraphicsNodeItem * node2);
void setPen(QPen pen);
QPen & pen();
void setNodeRadius(uint radius);
void setPenWidthOfConnectedEdges(uint width);
void accelerate(QVector2D acc);
void balanceSprings();
void updatePosition();
QString getNodeName();
//interface
public:
QRectF boundingRect() const override;
QPainterPath shape() const override;
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override;
private:
QMap<QGraphicsNodeItem *,QPair<QGraphicsEdgeItem *,int>> connectedEdges;//connected with(node2),edge
QMap<QGraphicsNodeItem *,Spring *> connectedSprings; // node2 , spring in between
int nodeIndex;
QPen m_pen;
QColor nodeColor;
QFont font;
QRectF nodeRect;
QVector2D acceleration=QVector2D(0,0);
QVector2D velocity=QVector2D(0,0);
int * maxSpeed;
float * drag;
QGraphicsScene * m_scene;
void updateConnectedEdgePositons();
int m_zValue=2;
QString m_nodeName;
// QGraphicsItem interface
protected:
void hoverEnterEvent(QGraphicsSceneHoverEvent *event) override;
void hoverLeaveEvent(QGraphicsSceneHoverEvent *event) override;
};
#endif // QGRAPHICSNODEITEM_H