Newer
Older

Felix Goding
committed
"""@package docstring
B-ASIC GUI Module.
This python file is the main window of the GUI for B-ASIC.
"""
import sys
from drag_button import DragButton
from gui_interface import Ui_main_window
from arrow import Arrow

Adam Jakobsson
committed
from port_button import PortButton

Felix Goding
committed
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
from b_asic import Constant, Addition, Subtraction, Absolute,\
Multiplication, Division, ConstantMultiplication, SquareRoot, ComplexConjugate,\
Max, Min, Butterfly
from b_asic import Input, Output, Register
from PyQt5.QtWidgets import QApplication, QWidget, QMainWindow, QLabel, QAction,\
QStatusBar, QMenuBar, QLineEdit, QPushButton, QSlider, QScrollArea, QVBoxLayout,\
QHBoxLayout, QDockWidget, QToolBar, QMenu, QLayout, QSizePolicy, QListWidget,\
QListWidgetItem, QGraphicsView, QGraphicsScene
from PyQt5.QtCore import Qt, QSize
from PyQt5.QtGui import QIcon, QFont, QPainter, QPen, QBrush
class MainWindow(QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.ui = Ui_main_window()
self.ui.setupUi(self)
self.setWindowTitle(" ")
self.setWindowIcon(QIcon('small_logo.png'))
self.scene = None
self.init_ui()
self.add_counter = 0
self.sub_counter = 0
self.mul_counter = 0
self.div_counter = 0
self.const_counter = 0
self.cmul_counter = 0
self.sqr_counter = 0
self.cc_counter = 0
self.abs_counter = 0
self.max_counter = 0
self.min_counter = 0
self.butterfly_counter = 0
self.input_counter = 0
self.output_counter = 0
self.reg_counter = 0

Felix Goding
committed
self.operationList = []
self.signalList = []
self.pressed_button = []

Adam Jakobsson
committed
self.portList = []
self.pressed_ports = []

Felix Goding
committed
self.source = None
def init_ui(self):
self.ui.core_operations_list.itemClicked.connect(self.on_list_widget_item_clicked)
self.ui.special_operations_list.itemClicked.connect(self.on_list_widget_item_clicked)
self.ui.exit_menu.triggered.connect(self.exit_app)
self.create_graphics_view()
def create_graphics_view(self):
self.scene = QGraphicsScene()
self.graphic_view = QGraphicsView(self.scene, self)
self.graphic_view.setRenderHint(QPainter.Antialiasing)
self.graphic_view.setGeometry(250, 40, 600, 520)
self.graphic_view.setDragMode(QGraphicsView.ScrollHandDrag)
def wheelEvent(self, event):
old_zoom = self.zoom
self.zoom += event.angleDelta().y()/2500
self.graphic_view.scale(self.zoom, self.zoom)
self.zoom = old_zoom

Felix Goding
committed
def exit_app(self, checked):
QApplication.quit()
def add_ports(self, operation):
if operation.operation.input_count == 2:

Adam Jakobsson
committed
self.input_port_1 = PortButton(">", operation, window)
self.input_port_1.setFixedSize(15, 15)

Felix Goding
committed
self.input_port_1.setStyleSheet("background-color: white")
self.input_port_1.show()

Adam Jakobsson
committed
self.input_port_2 = PortButton(">", operation, window)
self.input_port_2.setFixedSize(15, 15)

Felix Goding
committed
self.input_port_2.setStyleSheet("background-color: white")

Adam Jakobsson
committed
self.input_port_2.move(0, 35)

Felix Goding
committed
self.input_port_2.show()

Adam Jakobsson
committed
self.portList.append(self.input_port_1)
self.portList.append(self.input_port_2)
self.input_port_1.connectionRequested.connect(self.connectButton)
self.input_port_2.connectionRequested.connect(self.connectButton)

Felix Goding
committed
else:

Adam Jakobsson
committed
self.input_port_1 = PortButton(">", operation, window)
self.input_port_1.setFixedSize(15, 15)

Felix Goding
committed
self.input_port_1.setStyleSheet("background-color: white")
self.input_port_1.move(0, 16)
self.input_port_1.show()

Adam Jakobsson
committed
self.portList.append(self.input_port_1)
self.input_port_1.connectionRequested.connect(self.connectButton)

Felix Goding
committed
if operation.operation.output_count == 2:

Adam Jakobsson
committed
self.output_port = PortButton(">", operation, window)
self.output_port.setFixedSize(15, 15)

Felix Goding
committed
self.output_port.setStyleSheet("background-color: white")
self.output_port.move(38, 0)
self.output_port.show()

Adam Jakobsson
committed
self.output_port2 = PortButton(">", operation, window)
self.output_port_2.setFixedSize(15, 15)

Felix Goding
committed
self.output_port2.setStyleSheet("background-color: white")
self.output_port2.move(38, 33)
self.output_port2.show()

Adam Jakobsson
committed
self.portList.append(self.output_port)
self.portList.append(self.output_port_2)
self.output_port.connectionRequested.connect(self.connectButton)
self.output_port_2.connectionRequested.connect(self.connectButton)

Felix Goding
committed
else:

Adam Jakobsson
committed
self.output_port = PortButton(">", operation, window)
self.output_port.setFixedSize(15, 15)

Felix Goding
committed
self.output_port.setStyleSheet("background-color: white")

Adam Jakobsson
committed
self.output_port.move(38, 20)

Felix Goding
committed
self.output_port.show()

Adam Jakobsson
committed
self.portList.append(self.output_port)
self.output_port.connectionRequested.connect(self.connectButton)

Felix Goding
committed
def create_addition_operation(self):
self.add_counter += 1
addition_object = Addition()
self.addition_operation = DragButton("Add" + str(self.add_counter),\
addition_object, "addition", self)
self.addition_operation.move(250, 100)
self.addition_operation.setFixedSize(50, 50)
self.addition_operation.setStyleSheet("background-color: white; border-style: solid;\

Adam Jakobsson
committed
border-color: black; border-width: 2px")

Felix Goding
committed
self.addition_operation.setIcon(QIcon(r"operation_icons\addition.png"))
self.addition_operation.setIconSize(QSize(50, 50))
self.addition_operation.setParent(None)
self.scene.addWidget(self.addition_operation)
self.operationList.append(self.addition_operation)

Adam Jakobsson
committed

Felix Goding
committed
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
self.operationList.append(self.addition_operation)
self.add_ports(self.addition_operation)
def create_subtraction_operation(self):
self.sub_counter += 1
subtraction_object = Subtraction()
self.subtraction_operation = DragButton("Subb" + str(self.sub_counter),\
subtraction_object, "subtraction", self)
self.subtraction_operation.move(250, 100)
self.subtraction_operation.setFixedSize(50, 50)
self.subtraction_operation.setStyleSheet("background-color: white; border-style: solid;\
border-color: black; border-width: 2px; border-radius: 10px")
self.subtraction_operation.setIcon(QIcon(r"operation_icons\subtraction.png"))
self.subtraction_operation.setIconSize(QSize(50, 50))
self.subtraction_operation.setParent(None)
self.scene.addWidget(self.subtraction_operation)
self.operationList.append(self.subtraction_operation)
self.add_ports(self.subtraction_operation)
def create_multiplication_operation(self):
self.mul_counter += 1
multiplication_object = Multiplication()
self.multiplication_operation = DragButton("Mul" + str(self.mul_counter),\
multiplication_object, "multiplication", self)
self.multiplication_operation.move(250, 100)
self.multiplication_operation.setFixedSize(50, 50)
self.multiplication_operation.setStyleSheet("background-color: white; border-style: solid;\
border-color: black; border-width: 2px; border-radius: 10px")
self.multiplication_operation.setIcon(QIcon\
(r"operation_icons\multiplication.png"))
self.multiplication_operation.setIconSize(QSize(50, 50))
self.multiplication_operation.setParent(None)
self.scene.addWidget(self.multiplication_operation)
self.operationList.append(self.multiplication_operation)
self.add_ports(self.multiplication_operation)
def create_division_operation(self):
self.div_counter += 1
division_object = Division()
self.division_operation = DragButton("Div" + str(self.div_counter),\
division_object, "division", self)
self.division_operation.move(250, 100)
self.division_operation.setFixedSize(50, 50)
self.division_operation.setStyleSheet("background-color: white; border-style: solid;\
border-color: black; border-width: 2px; border-radius: 10px")
self.division_operation.setIcon(QIcon(r"operation_icons\division.png"))
self.division_operation.setIconSize(QSize(50, 50))
self.division_operation.setParent(None)
self.scene.addWidget(self.division_operation)
self.operationList.append(self.division_operation)
self.add_ports(self.division_operation)
def create_constant_operation(self):
self.const_counter += 1
constant_object = Constant()
self.constant_operation = DragButton("Const" + str(self.const_counter),\
constant_object, "constant", self)
self.constant_operation.move(250, 100)
self.constant_operation.setFixedSize(50, 50)
self.constant_operation.setStyleSheet("background-color: white; border-style: solid;\
border-color: black; border-width: 2px; border-radius: 10px")
self.constant_operation.setIcon(QIcon(r"operation_icons\constant.png"))
self.constant_operation.setIconSize(QSize(50, 50))
self.constant_operation.setParent(None)
self.scene.addWidget(self.constant_operation)
self.operationList.append(self.constant_operation)
self.add_ports(self.constant_operation)
def create_constant_multiplication_operation(self):
self.cmul_counter += 1
constant_multiplication_object = ConstantMultiplication()
self.constant_multiplication_operation = DragButton("Cmul" + str(self.cmul_counter),\
constant_multiplication_object, "constant_multiplication", self)
self.constant_multiplication_operation.move(250, 100)
self.constant_multiplication_operation.setFixedSize(50, 50)
self.constant_multiplication_operation.setStyleSheet("background-color: white; \
border-style: solid; border-color: black; border-width: 2px; border-radius: 10px")
self.constant_multiplication_operation.setIcon(QIcon\
(r"operation_icons\constant_multiplication.png"))
self.constant_multiplication_operation.setIconSize(QSize(50, 50))
self.constant_multiplication_operation.setParent(None)
self.scene.addWidget(self.constant_multiplication_operation)
self.operationList.append(self.constant_multiplication_operation)
self.add_ports(self.constant_multiplication_operation)
def create_square_root_operation(self):
self.sqr_counter += 1
square_root_object = SquareRoot()
self.square_root_operation = DragButton("Sqr" + str(self.sqr_counter),\
square_root_object, "square_root", self)
self.square_root_operation.move(250, 100)
self.square_root_operation.setFixedSize(50, 50)
self.square_root_operation.setStyleSheet("background-color: white; border-style: solid;\
border-color: black; border-width: 2px; border-radius: 10px")
self.square_root_operation.setIcon(QIcon(r"operation_icons\square_root.png"))
self.square_root_operation.setIconSize(QSize(50, 50))
self.square_root_operation.setParent(None)
self.scene.addWidget(self.square_root_operation)
self.operationList.append(self.square_root_operation)
self.add_ports(self.square_root_operation)
def create_complex_conjugate_operation(self):
self.cc_counter += 1
complex_conjugate_object = ComplexConjugate()
self.complex_conjugate_operation = DragButton("Cc" + str(self.cc_counter),\
complex_conjugate_object, "complex_conjugate", self)
self.complex_conjugate_operation.move(250, 100)
self.complex_conjugate_operation.setFixedSize(50, 50)
self.complex_conjugate_operation.setStyleSheet("background-color: white;\
border-style: solid; border-color: black; border-width: 2px; border-radius: 10px")
self.complex_conjugate_operation.setIcon(QIcon\
(r"operation_icons\complex_conjugate.png"))
self.complex_conjugate_operation.setIconSize(QSize(50, 50))
self.complex_conjugate_operation.setParent(None)
self.scene.addWidget(self.complex_conjugate_operation)
self.operationList.append(self.complex_conjugate_operation)
self.add_ports(self.complex_conjugate_operation)
def create_absolute_operation(self):
self.abs_counter += 1
absolute_object = Absolute()
self.absolute_operation = DragButton("Abs" + str(self.abs_counter),\
absolute_object, "absolute", self)
self.absolute_operation.move(250, 100)
self.absolute_operation.setFixedSize(50, 50)
self.absolute_operation.setStyleSheet("background-color: white; border-style: solid;\
border-color: black; border-width: 2px; border-radius: 10px")
self.absolute_operation.setIcon(QIcon(r"operation_icons\absolute.png"))
self.absolute_operation.setIconSize(QSize(50, 50))
self.absolute_operation.setParent(None)
self.scene.addWidget(self.absolute_operation)
self.operationList.append(self.absolute_operation)
self.add_ports(self.absolute_operation)
def create_max_operation(self):
self.max_counter += 1
max_object = Max()
self.max_operation = DragButton("Max" + str(self.max_counter),\
max_object, "max", self)
self.max_operation.move(250, 100)
self.max_operation.setFixedSize(50, 50)
self.max_operation.setStyleSheet("background-color: white; border-style: solid;\
border-color: black; border-width: 2px; border-radius: 10px")
self.max_operation.setIcon(QIcon(r"operation_icons\max.png"))
self.max_operation.setIconSize(QSize(50, 50))
self.max_operation.setParent(None)
self.scene.addWidget(self.max_operation)
self.operationList.append(self.max_operation)
self.add_ports(self.max_operation)
def create_min_operation(self):
self.min_counter += 1
min_object = Min()
self.min_operation = DragButton("Min" + str(self.min_counter),\
min_object, "min", self)
self.min_operation.move(250, 100)
self.min_operation.setFixedSize(50, 50)
self.min_operation.setStyleSheet("background-color: white; border-style: solid;\
border-color: black; border-width: 2px; border-radius: 10px")
self.min_operation.setIcon(QIcon(r"operation_icons\min.png"))
self.min_operation.setIconSize(QSize(50, 50))
self.min_operation.setParent(None)
self.scene.addWidget(self.min_operation)
self.operationList.append(self.min_operation)
self.add_ports(self.min_operation)
def create_butterfly_operation(self):
self.butterfly_counter += 1
butterfly_object = Butterfly()
self.butterfly_operation = DragButton("Butterfly" + str(self.butterfly_counter),\
butterfly_object, "butterfly", self)
self.butterfly_operation.move(250, 100)
self.butterfly_operation.setFixedSize(50, 50)
self.butterfly_operation.setStyleSheet("background-color: white; border-style: solid;\
border-color: black; border-width: 2px; border-radius: 10px")
self.butterfly_operation.setIcon(QIcon(r"operation_icons\butterfly.png"))
self.butterfly_operation.setIconSize(QSize(50, 50))
self.butterfly_operation.setParent(None)
self.scene.addWidget(self.butterfly_operation)
self.operationList.append(self.butterfly_operation)
self.add_ports(self.butterfly_operation)
def create_input_operation(self):
self.input_counter += 1
input_object = Input()
self.input_operation = DragButton("In" + str(self.input_counter),\
input_object, "input", self)
self.input_operation.move(250, 100)
self.input_operation.setFixedSize(50, 50)
self.input_operation.setStyleSheet("background-color: white; border-style: solid;\
border-color: black; border-width: 2px; border-radius: 10px")
self.input_operation.setIcon(QIcon(r"operation_icons\input.png"))
self.input_operation.setIconSize(QSize(50, 50))
self.input_operation.setParent(None)
self.scene.addWidget(self.input_operation)
self.operationList.append(self.input_operation)
self.add_ports(self.input_operation)
def create_output_operation(self):
self.output_counter += 1
output_object = Output()
self.output_operation = DragButton("Out" + str(self.output_counter),\
output_object, "output", self)
self.output_operation.move(250, 100)
self.output_operation.setFixedSize(50, 50)
self.output_operation.setStyleSheet("background-color: white; border-style: solid;\
border-color: black; border-width: 2px; border-radius: 10px")
self.output_operation.setIcon(QIcon(r"operation_icons\output.png"))
self.output_operation.setIconSize(QSize(50, 50))
self.output_operation.setParent(None)
self.scene.addWidget(self.output_operation)
self.operationList.append(self.output_operation)
self.add_ports(self.output_operation)
def create_register_operation(self):
self.reg_counter += 1
register_object = Register()
self.register_operation = DragButton("Reg" + str(self.reg_counter),\
register_object, "register", self)
self.register_operation.move(250, 100)
self.register_operation.setFixedSize(50, 50)
self.register_operation.setStyleSheet("background-color: white; border-style: solid;\
border-color: black; border-width: 2px; border-radius: 10px")
self.register_operation.setIcon(QIcon(r"operation_icons\register.png"))
self.register_operation.setIconSize(QSize(50, 50))
self.register_operation.setParent(None)
self.scene.addWidget(self.register_operation)
self.operationList.append(self.register_operation)
self.add_ports(self.register_operation)
def create_custom_operation(self):
self.custom_operation = DragButton(self)
self.custom_operation.move(250, 100)
self.custom_operation.setFixedSize(50, 50)
self.custom_operation.setStyleSheet("background-color: white; border-style: solid;\
border-color: black; border-width: 2px; border-radius: 10px")
self.custom_operation.setIcon(QIcon(r"operation_icons\custom_operation.png"))
self.custom_operation.setIconSize(QSize(50, 50))
self.custom_operation.show()
def on_list_widget_item_clicked(self, item):
if item.text() == "Addition":
self.create_addition_operation()
elif item.text() == "Subtraction":
self.create_subtraction_operation()
elif item.text() == "Multiplication":
self.create_multiplication_operation()
elif item.text() == "Division":
self.create_division_operation()
elif item.text() == "Constant":
self.create_constant_operation()
elif item.text() == "Constant multiplication":
self.create_constant_multiplication_operation()
elif item.text() == "Square root":
self.create_square_root_operation()
elif item.text() == "Complex conjugate":
self.create_complex_conjugate_operation()
elif item.text() == "Max":
self.create_max_operation()
elif item.text() == "Min":
self.create_min_operation()
elif item.text() == "Absolute":
self.create_absolute_operation()
elif item.text() == "Butterfly":
self.create_butterfly_operation()
elif item.text() == "Input":
self.create_input_operation()
elif item.text() == "Output":
self.create_output_operation()
elif item.text() == "Register":
self.create_register_operation()
else:
print("Block for this operation is not implemented yet.")
def keyPressEvent(self, event):
pressed_buttons = []
for op in self.operationList:
if op.pressed:
pressed_buttons.append(op)
if event.key() == Qt.Key_Delete:
for pressed_op in pressed_buttons:
self.operationList.remove(pressed_op)
pressed_op.remove()
super().keyPressEvent(event)
def connectButton(self, button):

Adam Jakobsson
committed
if len(self.pressed_ports) < 2:

Felix Goding
committed
return

Adam Jakobsson
committed
for i in range(len(self.pressed_ports) - 1):
line = Arrow(self.pressed_ports[i], self.pressed_ports[i + 1], self)

Felix Goding
committed
self.scene.addItem(line)
self.signalList.append(line)
self.update()
def paintEvent(self, event):
for signal in self.signalList:
signal.moveLine()
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())