-
Notifications
You must be signed in to change notification settings - Fork 1
/
mostragrafo.py
327 lines (252 loc) · 10.6 KB
/
mostragrafo.py
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
import sys
import ast
import os
from PyQt5 import QtCore
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import QIcon, QPainter, QPen
from PyQt5.QtCore import Qt
from enum import Enum
from QGraph import QEdge, QVertex
from Graph import Simulator
import random
try:
from enum import auto
except ImportError:
__my_enum_auto_id = 0
def auto() -> int:
global __my_enum_auto_id
i = __my_enum_auto_id
__my_enum_auto_id += 1
return i
global hasImport
hasImport = False
class App(QMainWindow):
class AppState(Enum):
DEFAULT = auto()
NODE = auto()
EDGE = auto()
DRAWING = auto()
SAVING = auto()
EDITING = auto()
MAX_VERTEXES = 100
def __init__(self):
super().__init__()
scriptDir = os.path.dirname(os.path.realpath(__file__))
self.app_state = App.AppState.DEFAULT
self.shouldExecuteRightClickAction = True
self.title = 'Grafo'
self.left = 100
self.top = 100
self.width = 1600
self.height = 1000
self.v = 0
self.init_interface()
def init_interface(self):
p = self.palette()
p.setColor(self.backgroundRole(), Qt.white)
self.setPalette(p)
self.mouseTrackingPosition = self.pos()
self.SourceEdgeDrawingVertex = None
self.unused_icon_vertexes = set()
self.used_icon_vertexes = set()
self.unused_label_edges = set()
self.used_label_edges = set()
self.createdNodes = 1
self.lambda_ = 0
self.fileName = None
for i in range(self.MAX_VERTEXES):
self.unused_icon_vertexes.add(QVertex.VertexIcon(self))
self.MAX_EDGES = (self.MAX_VERTEXES * (self.MAX_VERTEXES - 1))//2
for i in range(self.MAX_EDGES):
self.unused_label_edges.add(QEdge.EdgeLabel(self))
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
importAct = QAction(QIcon('Images/import.png'),'&Import', self)
importAct.triggered.connect(self.importFile)
nodeAct = QAction(QIcon('Images/vertex.jpeg'), 'Criar vertice', self)
nodeAct.triggered.connect(self.create_vertex_toolbar)
nodeAct.setEnabled(True)
edgeAct = QAction(QIcon('Images/line.jpeg'), 'Criar aresta', self)
edgeAct.setEnabled(True)
edgeAct.triggered.connect(self.create_edge_toolbar)
saveAct = QAction(QIcon('Images/save2.png'), 'Download', self)
saveAct.setEnabled(True)
saveAct.triggered.connect(self.saveFile)
self.routeAct = QAction(QIcon('Images/on.jpeg'), 'Solicitar Chamadas', self)
self.routeAct.setEnabled(True)
self.routeAct.triggered.connect(self.showSimulatorDialog)
waveLentghAct = QAction(QIcon('Images/wave.png'), 'Configurar enlace', self)
waveLentghAct.triggered.connect(self.setWaveLength)
waveLentghAct.setEnabled(True)
self.toolbar = self.addToolBar('Exit')
self.toolbar.addAction(importAct)
self.toolbar.addAction(nodeAct)
self.toolbar.addAction(edgeAct)
self.toolbar.addAction(self.routeAct)
self.toolbar.addAction(saveAct)
self.toolbar.addAction(waveLentghAct)
self.show()
def mousePressEvent(self, event):
if(event.button() == QtCore.Qt.RightButton
and self.app_state is App.AppState.DRAWING
and self.shouldExecuteRightClickAction):
self.onRightClick(event.pos(), event.globalPos())
if(self.shouldExecuteRightClickAction and self.SourceEdgeDrawingVertex is not None):
self.SourceEdgeDrawingVertex = None
self.update()
self.shouldExecuteRightClickAction = True
def showSimulatorDialog(self):
self.dialog = Simulator.SimulatorDialog(self, len(self.used_icon_vertexes), self.used_label_edges)
self.dialog.exec_()
def create_edge(self, u, v, w):
vertex_u = None
vertex_v = None
e = self.unused_label_edges.pop()
for vertex in self.used_icon_vertexes:
if vertex.idVertex==u:
vertex_u = vertex
elif vertex.idVertex==v:
vertex_v = vertex
e.initialize(vertex_u, vertex_v, ast.literal_eval(w))
self.used_label_edges.add(e)
def create_edge_toolbar(self, event):
if (len(self.used_icon_vertexes) < 2):
self.showNewMessageDialog('Não é possível criar! Quantidade de vértices é menor que dois.')
else:
idVertexList = []
labelVertexList = []
for v in self.used_icon_vertexes:
idVertexList.append(v.idVertex)
labelVertexList.append(v.label.text())
self.dialog = QEdge.CreateEdgeDialog(labelVertexList)
self.dialog.exec_()
if self.dialog.status == 0:
u = idVertexList[self.dialog.u.currentIndex()]
v = idVertexList[self.dialog.v.currentIndex()]
if (u == v):
self.showNewMessageDialog('Não é possível criar aresta. Vértices iguais.')
else:
existentEdge = False
for edge in self.used_label_edges:
c1 = edge.u.idVertex == u and edge.v.idVertex == v
c2 = edge.u.idVertex == v and edge.v.idVertex == u
if (c1 or c2):
existentEdge = True
if (existentEdge):
self.showNewMessageDialog('Não é possível criar aresta. Aresta já existe.')
else:
self.create_edge(u, v, self.dialog.label.text())
def create_vertex(self, x, y, label, idVertex):
v = self.unused_icon_vertexes.pop()
self.used_icon_vertexes.add(v)
v.initialize(x, y, label, idVertex)
self.createdNodes += 1
def create_vertex_toolbar(self, event):
aux = self.createVertexDialog(self.createdNodes)
if(aux[0]==0):
self.create_vertex(random.randint(50,self.width-50),random.randint(50,self.height-50),aux[1],self.createdNodes)
def editVertex(self, vertex):
self.dialog = QVertex.VertexDialog(vertex.idVertex)
self.dialog.label.setText(vertex.label.text())
self.dialog.exec_()
if (self.dialog.status==0):
if (self.dialog.label.text()!=vertex.label.text()):
vertex.label.setText(self.dialog.label.text())
def createVertexDialog(self, idField=None):
self.dialog = QVertex.VertexDialog(idField)
self.dialog.exec_()
l = []
l.append(self.dialog.status)
l.append(self.dialog.label.text())
l.append(self.dialog.idField.text())
return(l)
def editEdge(self):
self.dialog = QEdge.EdgeDialog(edge.pos())
self.dialog.label.setText(str(edge.w))
self.dialog.exec_()
if (self.dialog.status==0):
if (ast.literal_eval(self.dialog.label.text())!=edge.w):
edge.w = ast.literal_eval(self.dialog.label.text())
edge.setText(str(edge.w) + ' Km')
def load_topology(self, fileName):
global hasImport
if not hasImport:
file = open(fileName,'r')
content = file.readlines()
nVertex, nEdges = content[0].rstrip().split()
for i in range(1,int(nVertex)+1):
self.create_vertex(random.randint(50,self.width-50),random.randint(50,self.height-50),str(i),str(i))
for j in range(1,int(nEdges)):
vertex,neighbor,weight = content[j].rstrip().split()
self.create_edge(vertex,neighbor,weight)
hasImport = True
def open_file_name_dialog(self):
options = QFileDialog.Options()
options |= QFileDialog.DontUseNativeDialog
fileName, _ = QFileDialog.getOpenFileName(self,"QFileDialog.getOpenFileName()", "","All Files (*);;Python Files (*.py)", options=options)
if fileName:
print(fileName)
self.fileName = fileName
self.load_topology(fileName)
def importFile(self):
global hasImport
if not hasImport:
self.open_file_name_dialog()
else:
self.showNewMessageDialog('Já foi importado uma topologia')
def showNewMessageDialog(self, mensagem):
msg = QMessageBox()
msg.setIcon(QMessageBox.Information)
msg.setText(mensagem)
msg.setWindowTitle("Simulador")
msg.setStandardButtons(QMessageBox.Ok)
msg.exec_()
def paintEvent(self, e):
qp = QPainter()
qp.begin(self)
self.drawEdges(qp)
#if(self.SourceEdgeDrawingVertex is not None):
# self.drawEdgeFollower(qp)
qp.end()
'''
def drawEdgeFollower(self, qp):
pen = QPen(Qt.black, 2, Qt.SolidLine)
pen.setStyle(Qt.SolidLine)
qp.setPen(pen)
qp.drawLine(self.SourceEdgeDrawingVertex.vertexCenter, self.mouseTrackingPosition)
self.update()
'''
def saveFile(self):
fileName, _ = QFileDialog.getSaveFileName(self,"Salvar Topologia","","All Files (*);;Text Files (*.txt)")
if fileName:
print(fileName)
self.fileName = fileName
if self.fileName!=None:
if('.txt' in self.fileName):
f = open(self.fileName, 'w')
else:
f = open(self.fileName + '.txt', 'w')
self.saveGraph(f)
def saveGraph(self,f):
f.write(str(len(self.used_icon_vertexes))+ ' ' +str(len(self.used_label_edges))+'\n')
for e in self.used_label_edges:
f.write(str(e.u.idVertex) + " " + str(e.v.idVertex) + " " + str(e.w) + '\n')
f.close()
def drawEdges(self, qp):
pen = QPen(Qt.black, 2, Qt.SolidLine)
pen.setStyle(Qt.SolidLine)
qp.setPen(pen)
for edge in self.used_label_edges:
qp.drawLine(edge.u.vertexCenter, edge.v.vertexCenter)
edge.updateCenterPosition()
self.update()
def setWaveLength(self,event):
self.dialog = Simulator.SimulatorWavelengthDialog(self)
self.dialog.exec_()
if (int(self.dialog.countLambdasField.text())!=self.lambda_):
self.lambda_ = int(self.dialog.countLambdasField.text())
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
sys.exit(app.exec_())