Skip to content
Snippets Groups Projects
Commit 70977725 authored by Adam Jakobsson's avatar Adam Jakobsson
Browse files

Merge branch '86-create-sfg-in-gui' into 'develop'

Resolve "Create SFG in GUI"

See merge request PUM_TDDD96/B-ASIC!52
parents c4d0db07 6e1a13ee
No related branches found
No related tags found
3 merge requests!67WIP: B-ASIC version 1.0.0 hotfix,!65B-ASIC version 1.0.0,!52Resolve "Create SFG in GUI"
Pipeline #15478 passed
......@@ -5,12 +5,14 @@ QGraphicsLineItem, QGraphicsWidget
from PySide2.QtCore import Qt, QSize, QLineF, QPoint, QRectF
from PySide2.QtGui import QIcon, QFont, QPainter, QPen
from b_asic import Signal
class Arrow(QGraphicsLineItem):
def __init__(self, source, destination, window, parent=None):
super(Arrow, self).__init__(parent)
self.source = source
self.signal = Signal(source.port, destination.port)
self.destination = destination
self._window = window
self.moveLine()
......
......@@ -57,21 +57,25 @@ class DragButton(QPushButton):
if self.clicked == 1:
self.pressed = True
self.setStyleSheet("background-color: grey; border-style: solid;\
border-color: black; border-width: 2px")
border-color: black; border-width: 2px; border-radius: 10px")
self.setStyleSheet(""" QToolTip { background-color: white;
color: black }""")
path_to_image = os.path.join('operation_icons', self.operation_path_name + '_grey.png')
self.setIcon(QIcon(path_to_image))
self.setIconSize(QSize(55, 55))
self._window.pressed_button.append(self)
self._window.pressed_operations.append(self)
elif self.clicked == 2:
self.clicked = 0
self.pressed = False
self.setStyleSheet("background-color: white; border-style: solid;\
border-color: black; border-width: 2px")
border-color: black; border-width: 2px; border-radius: 10px")
self.setStyleSheet(""" QToolTip { background-color: white;
color: black}""")
path_to_image = os.path.join('operation_icons', self.operation_path_name + '.png')
self.setIcon(QIcon(path_to_image))
self.setIconSize(QSize(55, 55))
self._window.pressed_button.remove(self)
self._window.pressed_operations.remove(self)
super(DragButton, self).mousePressEvent(event)
......
......@@ -15,6 +15,8 @@ from b_asic import Operation
import b_asic.core_operations as c_oper
import b_asic.special_operations as s_oper
from utils import decorate_class, handle_error
from b_asic import SFG
from b_asic import InputPort, OutputPort
from numpy import linspace
......@@ -41,11 +43,13 @@ class MainWindow(QMainWindow):
self.scene = None
self._operations_from_name = dict()
self.zoom = 1
self.sfg_name_i = 0
self.operationList = []
self.signalList = []
self.pressed_button = []
self.pressed_operations = []
self.portList = []
self.pressed_ports = []
self.sfg_list = []
self.source = None
self._window = self
......@@ -69,6 +73,7 @@ class MainWindow(QMainWindow):
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_toolbar_view()
self.create_graphics_view()
def create_graphics_view(self):
......@@ -78,6 +83,10 @@ class MainWindow(QMainWindow):
self.graphic_view.setGeometry(self.ui.operation_box.width(), 0, self.width(), self.height())
self.graphic_view.setDragMode(QGraphicsView.ScrollHandDrag)
def create_toolbar_view(self):
self.toolbar = self.addToolBar("Toolbar")
self.toolbar.addAction("Create SFG", self.create_SFG_from_toolbar)
def resizeEvent(self, event):
self.ui.operation_box.setGeometry(10, 10, self.ui.operation_box.width(), self.height())
self.graphic_view.setGeometry(self.ui.operation_box.width() + 20, 0, self.width() - self.ui.operation_box.width() - 20, self.height())
......@@ -102,15 +111,30 @@ class MainWindow(QMainWindow):
def exit_app(self, checked):
QApplication.quit()
def create_SFG_from_toolbar(self):
inputs = []
outputs = []
for op in self.pressed_operations:
if isinstance(op.operation, s_oper.Input):
inputs.append(op.operation)
elif isinstance(op.operation, s_oper.Output):
outputs.append(op.operation)
self.sfg_name_i += 1
sfg = SFG(inputs=inputs, outputs=outputs, name="sfg" + str(self.sfg_name_i))
for op in self.pressed_operations:
op.setToolTip(sfg.name)
self.sfg_list.append(sfg)
def _determine_port_distance(self, length, ports):
"""Determine the distance between each port on the side of an operation.
The method returns the distance that each port should have from 0.
"""
return [length / 2] if ports == 1 else linspace(0, length, ports)
def _create_port(self, operation, output_port=True):
def _create_port(self, operation, port, output_port=True):
text = ">" if output_port else "<"
button = PortButton(text, operation, self)
button = PortButton(text, operation, port, self)
button.setStyleSheet("background-color: white")
button.connectionRequested.connect(self.connectButton)
return button
......@@ -119,13 +143,13 @@ class MainWindow(QMainWindow):
_output_ports_dist = self._determine_port_distance(55 - 17, operation.operation.output_count)
_input_ports_dist = self._determine_port_distance(55 - 17, operation.operation.input_count)
for dist in _input_ports_dist:
port = self._create_port(operation)
for i, dist in enumerate(_input_ports_dist):
port = self._create_port(operation, operation.operation.input(i))
port.move(0, dist)
port.show()
for dist in _output_ports_dist:
port = self._create_port(operation)
for i, dist in enumerate(_output_ports_dist):
port = self._create_port(operation, operation.operation.output(i))
port.move(55 - 12, dist)
port.show()
......@@ -158,6 +182,9 @@ class MainWindow(QMainWindow):
icon_path = path.join("operation_icons", f"custom_operation.png")
attr_button.setIcon(QIcon(icon_path))
attr_button.setIconSize(QSize(55, 55))
attr_button.setToolTip("No sfg")
attr_button.setStyleSheet(""" QToolTip { background-color: white;
color: black }""")
attr_button.setParent(None)
attr_button_scene = self.scene.addWidget(attr_button)
attr_button_scene.moveBy(self.move_button_index * 100, 0)
......@@ -183,12 +210,12 @@ class MainWindow(QMainWindow):
self._create_operation(item)
def keyPressEvent(self, event):
pressed_buttons = []
pressed_operations = []
for op in self.operationList:
if op.pressed:
pressed_buttons.append(op)
pressed_operations.append(op)
if event.key() == Qt.Key_Delete:
for pressed_op in pressed_buttons:
for pressed_op in pressed_operations:
self.operationList.remove(pressed_op)
pressed_op.remove()
self.move_button_index -= 1
......@@ -198,9 +225,11 @@ class MainWindow(QMainWindow):
if len(self.pressed_ports) < 2:
return
for i in range(len(self.pressed_ports) - 1):
line = Arrow(self.pressed_ports[i], self.pressed_ports[i + 1], self)
self.scene.addItem(line)
self.signalList.append(line)
if isinstance(self.pressed_ports[i].port, OutputPort) and \
isinstance(self.pressed_ports[i+1].port, InputPort):
line = Arrow(self.pressed_ports[i], self.pressed_ports[i + 1], self)
self.scene.addItem(line)
self.signalList.append(line)
self.update()
......
......@@ -7,9 +7,10 @@ from PySide2.QtCore import Qt, Signal
class PortButton(QPushButton):
connectionRequested = Signal(QPushButton)
moved = Signal()
def __init__(self, name, operation, window, parent=None):
def __init__(self, name, operation, port, window, parent=None):
self.pressed = False
self.window = window
self.port = port
self.operation = operation
self.clicked = 0
super(PortButton, self).__init__(name, operation)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment