-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsommet.cpp
More file actions
170 lines (135 loc) · 4.69 KB
/
sommet.cpp
File metadata and controls
170 lines (135 loc) · 4.69 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
#include "sommet.h"
Sommet* Sommet::startNode = NULL;
Sommet* Sommet::endNode = NULL;
qint32 Sommet::selectedNb = 0;
Sommet::Sommet(qreal x, qreal y, qreal node_id)
{
this->ItemX = x;
this->ItemY = y;
Id = node_id;
Pressed = false;
isMoving = false;
Selected = false;
colored = false;
setFlag(ItemIsSelectable);
setFlag(ItemSendsGeometryChanges);
this->setZValue(4);
}
QRectF Sommet::boundingRect() const
{
return QRectF(ItemX - 25,ItemY - 25,50,50);
}
void Sommet::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
QRectF rect = boundingRect();
QPen Pen;
QFont font;
Pen.setWidth(3);
Pen.setColor(QColor(72, 171, 96,255));
painter->setPen(Pen);
painter->setBrush(QColor(121, 230, 148, 255));
/*
* Manage Event
*/
if(Pressed == true){
painter->setBrush(Qt::blue);
}
if(isSelected()){
Pen.setColor(QColor(171, 146, 38));
painter->setPen(Pen);
painter->setBrush(QColor(253, 216, 53));
}
if(colored == true && MainWindow::modify==false){
Pen.setColor(QColor(255, 255, 255));
painter->setPen(Pen);
painter->setBrush(QColor(230, 63, 63));
}
painter->drawEllipse(rect);
/*
Reset startNode en desélectionnement
*/
if(!isSelected() && Sommet::startNode == this && MainWindow::modify==true){
Sommet::selectedNb = 0;
Sommet::startNode = NULL;
}
/*
* Control font size and color
*/
font.setPointSize(font.pointSize() * 1.3 );
font.setWeight(font.weight()*1.4);
painter->setFont(font);
Pen.setColor(QColor(21, 121, 45,255));
if(colored == true && MainWindow::modify==false){
Pen.setColor(QColor(255, 255,255));
}
painter->setPen(Pen);
painter->drawText(rect, Qt::AlignCenter,QString::number(Id));
}
void Sommet::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
// Pressed = true;
//update();
//QGraphicsItem::mousePressEvent(event);
// qInfo() << event->pos().x();
}
void Sommet::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
Pressed = false;
isMoving = false;
update();
MainWindow::test->update();
QGraphicsItem::mouseReleaseEvent(event);
}
void Sommet::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
isMoving = true;
update();
QGraphicsItem::mouseMoveEvent(event);
}
QVariant Sommet::itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant &value)
{
if (change == QGraphicsItem::ItemSelectedChange)
{
if (value == true)
{
// Création du graphe (modify==true)
if(Sommet::selectedNb == 1 && Sommet::startNode != NULL && MainWindow::modify==true){
Arrow* arrow = new Arrow(startNode->ItemX,startNode->ItemY,this->ItemX,this->ItemY);
MainWindow::Sommet_arrow[startNode].push_back(arrow);
MainWindow::Sommet_arrow[this].push_back(arrow);
arrow->setZValue(3);
Sommet* hehe= startNode;
double weight = arrow->setWeight();
MainWindow::matrix.insert(QPair<Sommet*,Sommet*>(hehe,this),weight);
if(weight != M_PI){
MainWindow::test->addItem(arrow);
MainWindow::test->update();
}else{
delete arrow;
MainWindow::Sommet_arrow[startNode].pop_back();
MainWindow::Sommet_arrow[this].pop_back();
}
// After choosing a value select this node
this->setSelected(true);
Sommet::startNode = this;
}else if(MainWindow::modify==true){
Sommet::selectedNb = 1;
Sommet::startNode = this;
}
if(Sommet::selectedNb == 1 && Sommet::startNode != NULL && MainWindow::modify==false){
Sommet::endNode = this;
}else if(MainWindow::modify==false){
Sommet::selectedNb = 1;
Sommet::startNode = this;
}
update();
MainWindow::test->update();
}
else
{
update();
MainWindow::test->update();
}
}
return QGraphicsItem::itemChange(change, value);
}