From d83fd2c1fa1dca61acf00d2ff806d06c756f8524 Mon Sep 17 00:00:00 2001 From: Felix Goding <felgo673@student.liu.se> Date: Tue, 7 Apr 2020 17:12:48 +0200 Subject: [PATCH] made a GUI prototype with functionality examples --- b_asic/GUI/handle_event_window.py | 41 ++++++++ b_asic/GUI/start_window.py | 169 ++++++++++++++++++++++++++++++ b_asic/GUI/test_gui.py | 33 ++++++ b_asic/GUI/toolbar_window.py | 52 +++++++++ b_asic/GUI/widgets_window.py | 52 +++++++++ 5 files changed, 347 insertions(+) create mode 100644 b_asic/GUI/handle_event_window.py create mode 100644 b_asic/GUI/start_window.py create mode 100644 b_asic/GUI/test_gui.py create mode 100644 b_asic/GUI/toolbar_window.py create mode 100644 b_asic/GUI/widgets_window.py diff --git a/b_asic/GUI/handle_event_window.py b/b_asic/GUI/handle_event_window.py new file mode 100644 index 00000000..af31edeb --- /dev/null +++ b/b_asic/GUI/handle_event_window.py @@ -0,0 +1,41 @@ +import sys +from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel +from PyQt5.QtCore import Qt + + +class MainWindow(QMainWindow): + + def __init__(self, *args, **kwargs): + super(MainWindow, self).__init__(*args, **kwargs) + + #self.windowTitleChanged.connect(self.onWindowTitleChange) + + self.windowTitleChanged.connect(lambda x: self.my_custom_fn()) + + #self.windowTitleChanged.connect(lambda x: self.my_custom_fn(x)) + + #self.windowTitleChanged.connect(lambda x: self.my_custom_fn(x, 25)) + + self.setWindowTitle("B-ASIC") + + label = QLabel("Welcome to the our ASIC toolbox, B-ASIC") + label.setAlignment(Qt.AlignCenter) + + self.setCentralWidget(label) + + def onWindowTitleChange(self, s): + print(s) + + def my_custom_fn(self, a="HELLO!", b=5): + print(a, b) + + def contextMenuEvent(self, event): + print("Context meny event!") + super(MainWindow, self).contextMenuEvent(event) + +app = QApplication(sys.argv) + +window = MainWindow() +window.show() + +app.exec_() \ No newline at end of file diff --git a/b_asic/GUI/start_window.py b/b_asic/GUI/start_window.py new file mode 100644 index 00000000..b9b21a31 --- /dev/null +++ b/b_asic/GUI/start_window.py @@ -0,0 +1,169 @@ +import sys + +from PyQt5.QtWidgets import QApplication,QWidget ,QMainWindow, QLabel, QToolBar, QAction, QStatusBar, QHBoxLayout, QVBoxLayout, QCheckBox, QMenu, QMenuBar, QMenuBar, QLineEdit, QPushButton +from PyQt5.QtCore import Qt, QSize +from PyQt5.QtGui import QIcon, QFont, QPainter, QBrush, QPen + +""" +https://stackoverflow.com/questions/12219727/dragging-moving-a-qpushbutton-in-pyqt + +Hur man skapar en dragbar knapp +""" +class DragButton(QPushButton): + + def mousePressEvent(self, event): + self.__mouse_press_pos = None + self.__mouse_move_pos = None + if event.button() == Qt.LeftButton: + self.__mouse_press_pos = event.globalPos() + self.__mouse_move_pos = event.globalPos() + + super(DragButton, self).mousePressEvent(event) + + def mouseMoveEvent(self, event): + if event.buttons() == Qt.LeftButton: + # adjust offset from clicked point to origin of widget + cur_pos = self.mapToGlobal(self.pos()) + global_pos = event.globalPos() + diff = global_pos - self.__mouse_move_pos + new_pos = self.mapFromGlobal(cur_pos + diff) + self.move(new_pos) + + self.__mouse_move_pos = global_pos + + super(DragButton, self).mouseMoveEvent(event) + + def mouseReleaseEvent(self, event): + if self.__mouse_press_pos is not None: + moved = event.globalPos() - self.__mouse_press_pos + if moved.manhattanLength() > 3: + event.ignore() + return + + super(DragButton, self).mouseReleaseEvent(event) + +def clicked(): + print ("Drag button clicked") + +class subwindow(QWidget): + def createWindow(self,WindowWidth,WindowHeight): + parent=None + super(subwindow,self).__init__(parent) + self.setWindowFlags(Qt.WindowStaysOnTopHint) + self.resize(WindowWidth,WindowHeight) + +class MainWindow(QMainWindow): + def __init__(self, *args, **kwargs): + super(MainWindow, self).__init__(*args, **kwargs) + + self.setWindowTitle(" ") + self.setWindowIcon(QIcon('small_logo.png')) + + test_button = QAction("Test", self) + + open_window_button = QPushButton(" Open a new window ", self) + open_window_button.clicked.connect(self.create_sub_window) + open_window_button.resize(150, 40) + open_window_button.move(500, 100) + + exit_button = QAction("Exit", self) + exit_button.setShortcut("Ctrl+Q") + exit_button.triggered.connect(self.exit_app) + + edit_button = QAction("Edit", self) + edit_button.setStatusTip("Open edit menu") + edit_button.triggered.connect(self.on_edit_button_click) + + view_button = QAction("View", self) + view_button.setStatusTip("Open view menu") + view_button.triggered.connect(self.on_view_button_click) + + menu_bar = QMenuBar() + menu_bar.setStyleSheet("background-color:rgb(222, 222, 222)") + self.setMenuBar(menu_bar) + + file_menu = menu_bar.addMenu("&File") + file_menu.addAction(exit_button) + file_menu.addSeparator() + file_menu.addAction(test_button) + + edit_menu = menu_bar.addMenu("&Edit") + edit_menu.addAction(edit_button) + + edit_menu.addSeparator() + + view_menu = menu_bar.addMenu("&View") + view_menu.addAction(view_button) + + addition_button = DragButton("Addition", self) + addition_button.move(10, 130) + addition_button.setFixedSize(70, 20) + addition_button.clicked.connect(clicked) + addition_counter = 1 + + subtraction_button = DragButton("Subtraction", self) + subtraction_button.move(10, 170) + subtraction_button.setFixedSize(70, 20) + #subtraction_button.clicked.connect(clicked) + + multiplication_button = DragButton("Multiplication", self) + multiplication_button.move(10, 210) + multiplication_button.setFixedSize(70, 20) + #multiplication_button.clicked.connect(clicked) + + self.setStatusBar(QStatusBar(self)) + + def on_file_button_click(self): + print("File") + + def on_edit_button_click(self): + print("Edit") + + def on_view_button_click(self): + print("View") + + def exit_app(self, checked): + QApplication.quit() + + def paintEvent(self, e): + painter = QPainter(self) + painter.setPen(QPen(Qt.black, 5, Qt.SolidLine)) + painter.drawRect(0, 110, 100, 400) + + def create_sub_window(self): + self.sub_window = subwindow() + self.sub_window.createWindow(400, 300) + + self.sub_window.properties_label = QLabel(self.sub_window) + self.sub_window.properties_label.setText('Properties') + self.sub_window.properties_label.setFixedWidth(400) + self.sub_window.properties_label.setFont(QFont('SansSerif', 14, QFont.Bold)) + self.sub_window.properties_label.setAlignment(Qt.AlignCenter) + + name_label = QLabel(self.sub_window) + name_label.setText('Name:') + + id_label = QLabel(self.sub_window) + id_label.setText('Id:') + + vertical_box = QVBoxLayout() + vertical_box.addWidget(name_label) + vertical_box.addWidget(id_label) + + self.sub_window.setLayout(vertical_box) + + + self.sub_window.show() + + + + +if __name__ == "__main__": + app = QApplication(sys.argv) + + window = MainWindow() + window.resize(960, 720) + + window.show() + + app.exec_() \ No newline at end of file diff --git a/b_asic/GUI/test_gui.py b/b_asic/GUI/test_gui.py new file mode 100644 index 00000000..45c5a5a1 --- /dev/null +++ b/b_asic/GUI/test_gui.py @@ -0,0 +1,33 @@ +from PyQt5.QtCore import Qt +from PyQt5.QtGui import QPalette +from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QVBoxLayout +from PyQt5.QtWidgets import * + +app = QApplication([]) +app.setStyle('Windows') +app.setStyleSheet("QPushButton { margin: 10ex; }") + +palette = QPalette() +palette.setColor(QPalette.ButtonText, Qt.black) +app.setPalette(palette) + + +window = QWidget() + +first_button = QPushButton('Top') +layout = QVBoxLayout() +layout.addWidget(first_button) +layout.addWidget(QPushButton('Bottom')) + +def on_button_clicked(): + alert = QMessageBox() + alert.setText('You clicked the button!') + alert.exec_() + +first_button.clicked.connect(on_button_clicked) +first_button.show() + + +window.setLayout(layout) +window.show() +app.exec_() \ No newline at end of file diff --git a/b_asic/GUI/toolbar_window.py b/b_asic/GUI/toolbar_window.py new file mode 100644 index 00000000..d1ab661d --- /dev/null +++ b/b_asic/GUI/toolbar_window.py @@ -0,0 +1,52 @@ +import sys +from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel, QToolBar, QAction, QStatusBar, QCheckBox +from PyQt5.QtCore import Qt, QSize +from PyQt5.QtGui import QIcon + + +class MainWindow(QMainWindow): + + def __init__(self, *args, **kwargs): + super(MainWindow, self).__init__(*args, **kwargs) + + self.setWindowTitle("B-ASIC") + + label = QLabel("Welcome to the our ASIC toolbox, B-ASIC") + label.setAlignment(Qt.AlignCenter) + + self.setCentralWidget(label) + + toolbar = QToolBar("My main toolbar") + toolbar.setIconSize(QSize(16, 16)) + self.addToolBar(toolbar) + + button_action = QAction(QIcon("bug.png"), "Your button", self) + button_action.setStatusTip("This is your button") + button_action.triggered.connect(self.onMyToolBarButtonClick) + button_action.setCheckable(True) + toolbar.addAction(button_action) + + toolbar.addSeparator() + + button_action2 = QAction(QIcon("bug.png"), "Your button2", self) + button_action2.setStatusTip("This is your button2") + button_action2.triggered.connect(self.onMyToolBarButtonClick) + button_action2.setCheckable(True) + toolbar.addAction(button_action2) + + toolbar.addWidget(QLabel("Hello")) + toolbar.addWidget(QCheckBox()) + + + self.setStatusBar(QStatusBar(self)) + + def onMyToolBarButtonClick(self, s): + print("click", s) + +app = QApplication(sys.argv) + +window = MainWindow() +window.resize(500, 400) +window.show() + +app.exec_() \ No newline at end of file diff --git a/b_asic/GUI/widgets_window.py b/b_asic/GUI/widgets_window.py new file mode 100644 index 00000000..afa1abfa --- /dev/null +++ b/b_asic/GUI/widgets_window.py @@ -0,0 +1,52 @@ +import sys +#from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel, QToolBar, QAction, QStatusBar, QCheckBox +#from PyQt5.QtCore import Qt, QSize +#from PyQt5.QtGui import QIcon +from PyQt5.QtWidgets import * +from PyQt5.QtCore import * +from PyQt5.QtGui import * + +class MainWindow(QMainWindow): + + def __init__(self, *args, **kwargs): + super(MainWindow, self).__init__(*args, **kwargs) + + self.setWindowTitle("B-ASIC") + + widget = QLineEdit() + widget.setMaxLength(10) + widget.setPlaceholderText("Enter your text") + + #widget.setReadOnly(True) # uncomment this to make readonly + + widget.returnPressed.connect(self.return_pressed) + widget.selectionChanged.connect(self.selection_changed) + widget.textChanged.connect(self.text_changed) + widget.textEdited.connect(self.text_edited) + + self.setCentralWidget(widget) + + + def return_pressed(self): + print("Return pressed!") + self.centralWidget().setText("BOOM!") + + def selection_changed(self): + print("Selection changed") + print(self.centralWidget().selectedText()) + + def text_changed(self, s): + print("Text changed...") + print(s) + + def text_edited(self, s): + print("Text edited...") + print(s) + +app = QApplication(sys.argv) + +window = MainWindow() +window.resize(500, 400) +window.show() + +app.exec_() \ No newline at end of file -- GitLab