/
main.py
140 lines (113 loc) · 4.35 KB
/
main.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
from PySide2.QtWidgets import QApplication, QWidget, QListWidget, QVBoxLayout, QHBoxLayout,\
QAbstractItemView, QPushButton, QGraphicsView, QLabel, QFileDialog
from PySide2.QtGui import QBrush, QPen
from PySide2.QtCore import Qt
import sys
from graphics import GraphicsEllipse, GraphicsScene, GraphicsLine
import json
NODE_R = 30
NODE_D = NODE_R * 2
class Window(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("Network Designer")
self.setGeometry(300, 200, 840, 720)
self.current_id = 1
self.createLayout()
self.couple = []
self.show()
def makeButtonsLayout(self):
self.button = QPushButton("Add Node", self)
self.button.clicked.connect(self.addNode)
self.btn_connection = QPushButton("Add Connection:", self)
self.btn_connection.clicked.connect(self.addConnection)
self.button3 = QPushButton("Export Network", self)
self.button3.clicked.connect(self.file_save)
self.lbl_connection = QLabel("Connection")
self.l_btns = QHBoxLayout()
self.l_btns.addWidget(self.button3)
self.l_btns.addStretch()
self.l_btns.addWidget(self.btn_connection)
self.l_btns.addWidget(self.lbl_connection)
self.l_btns.addStretch()
self.l_btns.addWidget(self.button)
self.btns = QWidget()
self.btns.setLayout(self.l_btns)
self.btns.setFixedHeight(40)
def initializeGview(self):
self.scene = GraphicsScene()
self.scene.setSceneRect(0, 0, 480, 480)
self.scene.click.connect(self.keepNode)
self.addNode()
self.view = QGraphicsView(self.scene)
self.view.setGeometry(0, 0, 500, 500)
self.view.scale(1, 1)
def createOrderList(self):
self.orderList = QListWidget()
# Enable drag & drop ordering of items.
self.orderList.setDragDropMode(QAbstractItemView.InternalMove)
def createLayout(self):
self.makeButtonsLayout()
self.initializeGview()
self.createOrderList()
l_network = QHBoxLayout()
l_network.addWidget(self.view)
l_network.addStretch()
l_network.addWidget(self.orderList)
self.l_root = QVBoxLayout()
self.l_root.addLayout(l_network)
self.l_root.addStretch()
self.l_root.addWidget(self.btns)
# self.l_root.addWidget(self.view)
self.setLayout(self.l_root)
def addNode(self):
greenBrush = QBrush(Qt.green)
blackPen = QPen(Qt.black)
# blueBrush = QBrush(Qt.blue)
blackPen.setWidth(5)
ellipse = GraphicsEllipse(str(self.current_id), blackPen, greenBrush, 100, 100, NODE_D, NODE_D)
self.scene.addItem(ellipse)
self.current_id += 1
def keepNode(self, node):
if len(self.couple) < 2:
self.couple.append(node)
else:
self.couple.pop(0)
self.couple.append(node)
if len(self.couple) == 2:
self.lbl_connection.setText(self.couple[0].label + " --> " + self.couple[1].label)
def addConnection(self):
line = GraphicsLine(self.couple[0], self.couple[1], NODE_R)
self.scene.addItem(line)
for v in self.couple:
v.add_connection(line)
self.orderList.addItem(self.lbl_connection.text())
def getNodes(self):
positions = {}
for i in self.scene.items():
if type(i) is GraphicsEllipse:
positions[int(i.label)] = i.getPosition()
return positions
def getConnections(self):
connections = []
for i in range(self.orderList.count()):
conn = self.orderList.item(i).text().split(" --> ")
connections.append((int(conn[0]), int(conn[1]), {"label": str(i + 1)}))
return connections
def file_save(self):
positions = self.getNodes()
connections = self.getConnections()
network = {}
network["labels"] = {i: i for i in sorted(list(positions.keys()))}
network["edges"] = connections
network["pos"] = positions
text = json.dumps(network)
name = QFileDialog.getSaveFileName(self, 'Save File')
file = open(name[0], 'w')
# text = "something"
file.write(text)
file.close()
if __name__ == "__main__":
app = QApplication(sys.argv)
window = Window()
sys.exit(app.exec_())