/
MainApplication.py
230 lines (171 loc) · 7.46 KB
/
MainApplication.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
from PyQt5.QtWidgets import *
from PyQt5.QtGui import QTransform, QPainter, QIcon, QColor
from graph_managers import GraphManager
from data_parser import processFrontendData, processBackendData
from modify import Graph
import pickle
import sys
import NPAlgorithms as algo
class MyWindow(QMainWindow):
def __init__(self, width, height):
super().__init__()
self.setWindowTitle("Graph Viewer")
self.resize(width, height)
self.setStyleSheet("QMainWindow {background: 'grey';}")
self.view = GraphViewer()
newFileAction = QAction("New Graph", self)
newFileAction.triggered.connect(self.newGraph)
self.newFileAction = newFileAction
openFileAction = QAction("Open Graph", self)
openFileAction.triggered.connect(self.openGraph)
self.openFileAction = openFileAction
saveFileAction = QAction("Save Graph", self)
saveFileAction.triggered.connect(self.saveGraph)
saveFileAction.setDisabled(True)
self.saveFileAction = saveFileAction
minimumDominantSetAction = QAction("Minimum Dominant Set", self)
minimumDominantSetAction.triggered.connect(lambda x: self.callAlgorithms(1))
maximumIndependentSetAction = QAction("Maximum Independent Set", self)
maximumIndependentSetAction.triggered.connect(lambda x: self.callAlgorithms(2))
maximumCliqueAction = QAction("Maximum Clique", self)
maximumCliqueAction.triggered.connect(lambda x: self.callAlgorithms(3))
menuBar = QMenuBar()
fileMenu = menuBar.addMenu("File")
fileMenu.addAction(newFileAction)
fileMenu.addAction(openFileAction)
fileMenu.addAction(saveFileAction)
algorithmsMenu = menuBar.addMenu("Algorithms")
algorithmsMenu.addAction(minimumDominantSetAction)
algorithmsMenu.addAction(maximumIndependentSetAction)
algorithmsMenu.addAction(maximumCliqueAction)
redColorAction = QAction(QIcon('red_color.png'), "Set Red Pen", self)
redColorAction.triggered.connect(self.setRedPen)
blueColorAction = QAction(QIcon('blue_color.jpg'), "Set Blue Pen", self)
blueColorAction.triggered.connect(self.setBluePen)
blackColorAction = QAction(QIcon('black_color.jpg'), "Set Black Pen", self)
blackColorAction.triggered.connect(self.setBlackPen)
toolBar = self.addToolBar("Pen Color")
toolBar.addAction(redColorAction)
toolBar.addAction(blueColorAction)
toolBar.addAction(blackColorAction)
self.isViewMounted = False
self.setMenuBar(menuBar)
self.show()
def setRedPen(self):
self.view.setPenColor((255, 0, 0))
def setBluePen(self):
self.view.setPenColor((0, 0, 255))
def setBlackPen(self):
self.view.setPenColor((0, 0, 0))
def newGraph(self):
if self.saveFileAction.isEnabled():
choice = QMessageBox.question(self, "Graph Viewer", "Are you sure? All unsaved changes will be lost",
QMessageBox.Yes | QMessageBox.No)
if choice == QMessageBox.No:
return
if not self.isViewMounted:
self.setCentralWidget(self.view)
self.isViewMounted = True
self.view.newData()
self.saveFileAction.setDisabled(False)
def openGraph(self):
filename = QFileDialog.getOpenFileName(self, 'Open file', '/', 'Graph files (*.graph)')
try:
graphFile = open(filename[0], 'rb')
except FileNotFoundError:
QMessageBox.information(self, "Graph Viewer", "No such file found")
else:
graphData = pickle.load(graphFile)
nodes = graphData['nodes']
edges = graphData['edges']
rotation_angle = graphData['angle']
nodeList, adjacencyList = processBackendData(nodes, edges)
self.view.setData(Graph(nodeList, adjacencyList), rotation_angle)
graphFile.close()
self.saveFileAction.setDisabled(False)
if not self.isViewMounted:
self.setCentralWidget(self.view)
def saveGraph(self):
graphData = self.view.getData()
filename = QFileDialog.getSaveFileName(self, "Save File", '/', 'Graph files (*.graph)')
graphFile = open(filename[0], 'wb')
pickle.dump(graphData, graphFile)
graphFile.close()
def closeEvent(self, event):
close = QMessageBox.question(self,
"Quit Graph-Visualization",
"Are you sure you want to quit? All unsaved changes will be lost",
QMessageBox.Yes | QMessageBox.No)
if close == QMessageBox.Yes:
event.accept()
else:
event.ignore()
def callAlgorithms(self, id):
self.view.callAlgorithms(id)
class GraphViewer(QGraphicsView):
def __init__(self):
super().__init__()
self.setRenderHint(QPainter.Antialiasing)
self.container = None
self.angle = 0
def newData(self):
self.setData(Graph([],[]), 0)
def getData(self):
data = self.container.getGraphData()
data['angle'] = self.angle
return data
def setData(self, newGraph, rotation_angle):
self.container = GraphContainer(newGraph)
self.setScene(self.container)
def setPenColor(self, color):
self.container.setPenColor(color)
def callAlgorithms(self, id):
self.container.callAlgorithms(id)
class GraphContainer(QGraphicsScene):
def __init__(self, newGraph):
super().__init__()
self.pressed = False
self.drawing_edge = None
self.graph_manager = GraphManager(self, newGraph)
def mousePressEvent(self, event):
super().mousePressEvent(event)
self.pressed = True
mousePos = event.scenePos()
item = self.itemAt(mousePos.x(), mousePos.y(), QTransform())
self.graph_manager.mousePressEvent(event, item)
self.update()
def mouseMoveEvent(self, event):
super().mouseMoveEvent(event)
if self.pressed:
# Captures Drag events
mousePos = event.scenePos()
item = self.itemAt(mousePos.x(), mousePos.y(), QTransform())
self.graph_manager.mouseMoveEvent(event, item)
def mouseReleaseEvent(self, event):
super().mouseReleaseEvent(event)
self.pressed = False
mousePos = event.scenePos()
item = self.itemAt(mousePos.x(), mousePos.y(), QTransform())
self.graph_manager.mouseReleaseEvent(event, item)
def mouseDoubleClickEvent(self, event):
super().mouseDoubleClickEvent(event)
mousePos = event.scenePos()
item = self.itemAt(mousePos.x(), mousePos.y(), QTransform())
self.graph_manager.mouseDoubleClickEvent(event, item)
def getGraphData(self):
node_objs, edge_matrix = self.graph_manager.getData()
return processFrontendData(node_objs, edge_matrix)
def setGraphData(self, node_objs, edge_matrix):
self.clear()
self.graph_manager = GraphManager(self, node_objs, edge_matrix)
self.update()
def setPenColor(self, color):
self.graph_manager.setPenColor(color)
def callAlgorithms(self, id):
self.graph_manager.callAlgorithms(id)
if __name__ == '__main__':
app = QApplication([])
desktop = QDesktopWidget()
panel = MyWindow(desktop.width(), desktop.height())
ret = app.exec_()
sys.exit(ret)