Skip to content

Commit

Permalink
#187 - First prototype of new UI main application, outer layer
Browse files Browse the repository at this point in the history
  • Loading branch information
dmh23 committed Feb 26, 2023
1 parent ce6b3c0 commit 00ffd81
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 0 deletions.
Empty file added src/prototype_ui/__init__.py
Empty file.
35 changes: 35 additions & 0 deletions src/prototype_ui/actions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import os

from PyQt6.QtGui import QAction, QIcon
from PyQt6.QtWidgets import QStyle

ICON_DIRECTORY = os.path.join("icons")
ICON_FILE_EXTENSION = ".png"


class Actions:
def __init__(self, style: QStyle):
self._style = style
assert os.path.isdir(ICON_DIRECTORY)

self.file_new = QAction("New")
self.file_new.setShortcut("Ctrl+N")
self.file_new.setIcon(style.standardIcon(QStyle.StandardPixmap.SP_ArrowBack))

self.file_open = QAction("Open")
self.file_open.setShortcut("Ctrl+O")
self.file_open.setIcon(self.get_custom_icon("sample_icon"))

self.file_save = QAction("Save")
self.file_save.setShortcut("Ctrl+S")
self.file_save.setIcon(style.standardIcon(QStyle.StandardPixmap.SP_DirHomeIcon))

self.file_save_as = QAction("Save As")
self.file_save_as.setShortcut("Ctrl+A")
self.file_save_as.setIcon(style.standardIcon(QStyle.StandardPixmap.SP_DialogOpenButton))

@staticmethod
def get_custom_icon(icon_name: str):
file_path = os.path.join(ICON_DIRECTORY, icon_name + ".png")
assert os.path.isfile(file_path)
return QIcon(file_path)
Binary file added src/prototype_ui/icons/sample_icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
58 changes: 58 additions & 0 deletions src/prototype_ui/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import sys
import os

from PyQt6.QtCore import Qt
from PyQt6.QtWidgets import QMainWindow, QApplication, QLabel, QProgressBar

from prototype_ui.actions import Actions
from prototype_ui.menubar import MenuBarManager
from prototype_ui.toolbar import ToolBarManager


class MainWindow(QMainWindow):
def __init__(self):
super().__init__()

self.setMinimumSize(200, 200)
self.resize(500, 400)
self.setWindowTitle("Example")

# Status Bar
self._status_bar_label = QLabel("Hello Status Bar")
self._status_bar_progress = QProgressBar()
self._status_bar_progress.setRange(0, 10)
self._status_bar_progress.setValue(7)
self.statusBar().addPermanentWidget(self._status_bar_label)
self.statusBar().addPermanentWidget(self._status_bar_progress)

# Define UI actions
self._actions = Actions(self.style())

# Menu & Tool bars
self._menu_bar_manager = MenuBarManager(self.menuBar(), self._actions)
self._tool_bar_manager = ToolBarManager(self.addToolBar("Main"), self._actions)

# Connect actions with callback methods to implement them
self._actions.file_new.triggered.connect(self._new_file)
self._actions.file_open.triggered.connect(self._open_file)

self.canvas = QLabel("Hello")
self.setCentralWidget(self.canvas)

self.show()

def _new_file(self):
print("New File")
self.statusBar().showMessage("Hello Briefly", 5000) # 5 secs

def _open_file(self):
self.canvas.setCursor(Qt.CursorShape.CrossCursor)


if __name__ == '__main__':
app = QApplication([])

# app.setStyleSheet("QLabel { color: red } QProgressBar::chunk { background-color: blue }")

window = MainWindow()
sys.exit(app.exec())
20 changes: 20 additions & 0 deletions src/prototype_ui/menubar.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from PyQt6.QtWidgets import QMenuBar

from prototype_ui.actions import Actions


class MenuBarManager:
def __init__(self, menu_bar: QMenuBar, actions: Actions):
self._menu_bar = menu_bar
self._actions = actions
self.setup_menu_bar()

def setup_menu_bar(self):
self._create_file_menu()

def _create_file_menu(self):
menu = self._menu_bar.addMenu("File")
menu.addAction(self._actions.file_new)
menu.addAction(self._actions.file_open)
menu.addAction(self._actions.file_save)
menu.addAction(self._actions.file_save_as)
16 changes: 16 additions & 0 deletions src/prototype_ui/toolbar.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from PyQt6.QtWidgets import QToolBar

from prototype_ui.actions import Actions


class ToolBarManager:
def __init__(self, tool_bar: QToolBar, actions: Actions):
self._toolbar = tool_bar
self._actions = actions
self.setup_tool_bar()

def setup_tool_bar(self):
self._toolbar.addAction(self._actions.file_new)
self._toolbar.addAction(self._actions.file_open)
self._toolbar.addAction(self._actions.file_save)
self._toolbar.addAction(self._actions.file_save_as)

0 comments on commit 00ffd81

Please sign in to comment.