-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphpainter.cpp
111 lines (85 loc) · 3.62 KB
/
graphpainter.cpp
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
#include "graphpainter.h"
GraphPainter::GraphPainter(QWidget *parent) : QWidget(parent)
{
pointerToGraphMatrix=NULL;
pointColor=QColor(Qt::red);
lineColor=QColor(Qt::gray);
pathColor=QColor(Qt::green);
showLabels=true;
}
void GraphPainter::update(QObject * sender){
Q_UNUSED (sender);
pointerToGraphMatrix=Engine::getInstance()->getGraph();
repaint();
}
void GraphPainter::paintEvent(QPaintEvent * event){
Q_UNUSED(event);
if(pointerToGraphMatrix==NULL) return;
QPoint widgetCenter(this->width()/2,this->height()/2);
QVector<QPoint> pointsCordinates;
int numberOfElements=pointerToGraphMatrix->getVerticesNumber();
float angleStep=2.0*PI/numberOfElements;
float radius=width()>height()?height():width();
for(int index=0;index<numberOfElements;index++){
QPoint point;
point.setX(sin(angleStep*index)*radius/2.5);
point.setY(cos(angleStep*index)*radius/2.5);
pointsCordinates.push_back(point+widgetCenter);
}
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
painter.setPen(QPen(lineColor,2));
for(int index=0;index<numberOfElements;index++){
for(int index2=index;index2<numberOfElements;index2++){
if((*pointerToGraphMatrix)._adjacencyMatrix[index][index2] || (*pointerToGraphMatrix)._adjacencyMatrix[index2][index]){
if((*pointerToGraphMatrix)._adjacencyMatrix[index][index2] && (*pointerToGraphMatrix)._adjacencyMatrix[index2][index]){
painter.setPen(Qt::red);
}
else if((*pointerToGraphMatrix)._adjacencyMatrix[index][index2]){
painter.setPen(Qt::blue);
}
else{
painter.setPen(Qt::green);
}
painter.drawLine(pointsCordinates[index],pointsCordinates[index2]);
}
}
}
painter.setPen(QPen(pathColor,2));
painter.setPen(Qt::white);
int ID=1;
for(int index=0;index<numberOfElements;index++){
// idzie po górnym trójkącie
for(int index2=index;index2<numberOfElements;index2++){
if((*pointerToGraphMatrix)._adjacencyMatrix[index][index2] || (*pointerToGraphMatrix)._adjacencyMatrix[index2][index]){
QPoint textCord((pointsCordinates[index].x()+pointsCordinates[index2].x())/2,(pointsCordinates[index].y()+pointsCordinates[index2].y())/2);
QString text;
if((*pointerToGraphMatrix)._adjacencyMatrix[index][index2]){
text=QString::number(index+1)+"=>"+QString::number(index2+1)+":"+QString::number((*pointerToGraphMatrix)._weightMatrix[index][index2] );
painter.drawText(textCord,text);
textCord.setY(textCord.y()+10);
}
if((*pointerToGraphMatrix)._adjacencyMatrix[index2][index]){
text=QString::number(index2+1)+"=>"+QString::number(index+1)+":"+QString::number((*pointerToGraphMatrix)._weightMatrix[index2][index] );
painter.drawText(textCord,text);
}
ID++;
}
}
}
for(int index=0;index<numberOfElements;index++){
QPoint point=pointsCordinates[index];
painter.setPen(QPen(pointColor,2));
painter.drawEllipse(point,3,3);
if(showLabels){
painter.setPen(Qt::black);
painter.drawText(point+QPoint(4,-4),QString::number(index+1));
}
}
}
void GraphPainter::setPointColor(QColor pointC){
pointColor=pointC;
}
void GraphPainter::setLineColor(QColor lineC){
lineColor=lineC;
}