Skip to content
Snippets Groups Projects
Commit 2df0d971 authored by Felix Goding's avatar Felix Goding Committed by Adam Jakobsson
Browse files

Resolve "Change properties of operations in GUI"

parent 6dcab2af
No related branches found
No related tags found
2 merge requests!67WIP: B-ASIC version 1.0.0 hotfix,!65B-ASIC version 1.0.0
......@@ -5,8 +5,10 @@ This class creates a dragbutton which can be clicked, dragged and dropped.
import os.path
from PyQt5.QtWidgets import QPushButton
from PyQt5.QtCore import Qt, QSize
from properties_window import PropertiesWindow
from PyQt5.QtWidgets import QPushButton, QMenu, QAction
from PyQt5.QtCore import Qt, QSize, pyqtSignal
from PyQt5.QtGui import QIcon
from utils import decorate_class, handle_error
......@@ -14,8 +16,11 @@ from utils import decorate_class, handle_error
@decorate_class(handle_error)
class DragButton(QPushButton):
def __init__(self, name, operation, operation_path_name, window, parent = None):
connectionRequested = pyqtSignal(QPushButton)
moved = pyqtSignal()
def __init__(self, name, operation, operation_path_name, is_show_name, window, parent = None):
self.name = name
self.is_show_name = is_show_name
self._window = window
self.operation = operation
self.operation_path_name = operation_path_name
......@@ -23,6 +28,20 @@ class DragButton(QPushButton):
self.pressed = False
super(DragButton, self).__init__(self._window)
def contextMenuEvent(self, event):
menu = QMenu()
properties = QAction("Properties")
menu.addAction(properties)
properties.triggered.connect(self.show_properties_window)
menu.exec_(self.cursor().pos())
def show_properties_window(self):
self.properties_window = PropertiesWindow(self, self.__window)
self.properties_window.show()
def add_label(self, label):
self.label = label
def mousePressEvent(self, event):
self._mouse_press_pos = None
self._mouse_move_pos = None
......@@ -38,20 +57,20 @@ 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-radius: 10px")
border-color: black; border-width: 2px")
path_to_image = os.path.join('operation_icons', self.operation_path_name + '_grey.png')
self.setIcon(QIcon(path_to_image))
self.setIconSize(QSize(50, 50))
self.setIconSize(QSize(55, 55))
self._window.pressed_button.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-radius: 10px")
border-color: black; border-width: 2px")
path_to_image = os.path.join('operation_icons', self.operation_path_name + '.png')
self.setIcon(QIcon(path_to_image))
self.setIconSize(QSize(50, 50))
self.setIconSize(QSize(55, 55))
self._window.pressed_button.remove(self)
super(DragButton, self).mousePressEvent(event)
......
"""@package docstring
B-ASIC GUI Module.
This python file is the main window of the GUI for B-ASIC.
"""
from os import getcwd, path
import sys
from drag_button import DragButton
from gui_interface import Ui_main_window
from arrow import Arrow
from port_button import PortButton
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 numpy import linspace
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, QShortcut
from PyQt5.QtCore import Qt, QSize
from PyQt5.QtGui import QIcon, QFont, QPainter, QPen, QBrush, QKeySequence
@decorate_class(handle_error)
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._operations_from_name = dict()
self.zoom = 1
self.operationList = []
self.signalList = []
self.pressed_button = []
self.portList = []
self.pressed_ports = []
self.source = None
self._window = self
self.init_ui()
self.add_operations_from_namespace(c_oper, self.ui.core_operations_list)
self.add_operations_from_namespace(s_oper, self.ui.special_operations_list)
self.shortcut_core = QShortcut(QKeySequence("Ctrl+R"), self.ui.operation_box)
self.shortcut_core.activated.connect(self._refresh_operations_list_from_namespace)
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
def exit_app(self, checked):
QApplication.quit()
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):
text = ">" if output_port else "<"
button = PortButton(text, operation, self)
button.setStyleSheet("background-color: white")
button.connectionRequested.connect(self.connectButton)
return button
def add_ports(self, operation):
_output_ports_dist = self._determine_port_distance(50 - 15, operation.operation.output_count)
_input_ports_dist = self._determine_port_distance(50 - 15, operation.operation.input_count)
for dist in _input_ports_dist:
port = self._create_port(operation)
port.move(0, dist)
port.show()
for dist in _output_ports_dist:
port = self._create_port(operation)
port.move(50 - 15, dist)
port.show()
def get_operations_from_namespace(self, namespace):
return [comp for comp in dir(namespace) if hasattr(getattr(namespace, comp), "type_name")]
def add_operations_from_namespace(self, namespace, _list):
for attr_name in self.get_operations_from_namespace(namespace):
attr = getattr(namespace, attr_name)
try:
attr.type_name()
item = QListWidgetItem(attr_name)
_list.addItem(item)
self._operations_from_name[attr_name] = attr
except NotImplementedError:
pass
def _create_operation(self, item):
try:
attr_oper = self._operations_from_name[item.text()]()
attr_button = DragButton(attr_oper.graph_id, attr_oper, attr_oper.type_name().lower(), self)
attr_button.move(250, 100)
attr_button.setFixedSize(50, 50)
attr_button.setStyleSheet("background-color: white; border-style: solid;\
border-color: black; border-width: 2px; border-radius: 10px")
self.add_ports(attr_button)
icon_path = path.join("operation_icons", f"{attr_oper.type_name().lower()}.png")
if not path.exists(icon_path):
icon_path = path.join("operation_icons", f"custom_operation.png")
attr_button.setIcon(QIcon(icon_path))
attr_button.setIconSize(QSize(50, 50))
attr_button.setParent(None)
self.scene.addWidget(attr_button)
self.operationList.append(attr_button)
except Exception as e:
print("Unexpected error occured: ", e)
def _refresh_operations_list_from_namespace(self):
self.ui.core_operations_list.clear()
self.ui.special_operations_list.clear()
self.add_operations_from_namespace(c_oper, self.ui.core_operations_list)
self.add_operations_from_namespace(s_oper, self.ui.special_operations_list)
def print_input_port_1(self):
print("Input port 1")
def print_input_port_2(self):
print("Input port 2")
def print_output_port_1(self):
print("Output port 1")
def print_output_port_2(self):
print("Output port 2")
def on_list_widget_item_clicked(self, item):
self._create_operation(item)
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):
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)
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_())
This diff is collapsed.
from PyQt5.QtWidgets import QDialog, QLineEdit, QPushButton, QVBoxLayout, QHBoxLayout,\
QLabel, QCheckBox
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QIntValidator
class PropertiesWindow(QDialog):
def __init__(self, operation, main_window):
super(PropertiesWindow, self).__init__()
self.operation = operation
self.main_window = main_window
self.setWindowFlags(Qt.WindowTitleHint | Qt.WindowCloseButtonHint)
self.setWindowTitle("Properties")
self.name_layout = QHBoxLayout()
self.name_layout.setSpacing(50)
self.name_label = QLabel("Name:")
self.edit_name = QLineEdit(self.operation.operation_path_name)
self.name_layout.addWidget(self.name_label)
self.name_layout.addWidget(self.edit_name)
self.vertical_layout = QVBoxLayout()
self.vertical_layout.addLayout(self.name_layout)
if self.operation.operation_path_name == "c":
self.constant_layout = QHBoxLayout()
self.constant_layout.setSpacing(50)
self.constant_value = QLabel("Constant:")
self.edit_constant = QLineEdit(str(self.operation.operation.value))
self.only_accept_int = QIntValidator()
self.edit_constant.setValidator(self.only_accept_int)
self.constant_layout.addWidget(self.constant_value)
self.constant_layout.addWidget(self.edit_constant)
self.vertical_layout.addLayout(self.constant_layout)
self.show_name_layout = QHBoxLayout()
self.check_show_name = QCheckBox("Show name?")
if self.operation.is_show_name:
self.check_show_name.setChecked(1)
else:
self.check_show_name.setChecked(0)
self.check_show_name.setLayoutDirection(Qt.RightToLeft)
self.check_show_name.setStyleSheet("spacing: 170px")
self.show_name_layout.addWidget(self.check_show_name)
self.vertical_layout.addLayout(self.show_name_layout)
self.ok = QPushButton("OK")
self.ok.clicked.connect(self.save_properties)
self.vertical_layout.addWidget(self.ok)
self.setLayout(self.vertical_layout)
def save_properties(self):
self.operation.name = self.edit_name.text()
self.operation.label.setPlainText(self.operation.name)
if self.operation.operation_path_name == "c":
self.operation.operation.value = self.edit_constant.text()
if self.check_show_name.isChecked():
self.operation.label.setOpacity(1)
self.operation.is_show_name = True
else:
self.operation.label.setOpacity(0)
self.operation.is_show_name = False
self.reject()
\ No newline at end of file
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