Skip to content

Commit 00ffd81

Browse files
committed
#187 - First prototype of new UI main application, outer layer
1 parent ce6b3c0 commit 00ffd81

File tree

6 files changed

+129
-0
lines changed

6 files changed

+129
-0
lines changed

src/prototype_ui/__init__.py

Whitespace-only changes.

src/prototype_ui/actions.py

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import os
2+
3+
from PyQt6.QtGui import QAction, QIcon
4+
from PyQt6.QtWidgets import QStyle
5+
6+
ICON_DIRECTORY = os.path.join("icons")
7+
ICON_FILE_EXTENSION = ".png"
8+
9+
10+
class Actions:
11+
def __init__(self, style: QStyle):
12+
self._style = style
13+
assert os.path.isdir(ICON_DIRECTORY)
14+
15+
self.file_new = QAction("New")
16+
self.file_new.setShortcut("Ctrl+N")
17+
self.file_new.setIcon(style.standardIcon(QStyle.StandardPixmap.SP_ArrowBack))
18+
19+
self.file_open = QAction("Open")
20+
self.file_open.setShortcut("Ctrl+O")
21+
self.file_open.setIcon(self.get_custom_icon("sample_icon"))
22+
23+
self.file_save = QAction("Save")
24+
self.file_save.setShortcut("Ctrl+S")
25+
self.file_save.setIcon(style.standardIcon(QStyle.StandardPixmap.SP_DirHomeIcon))
26+
27+
self.file_save_as = QAction("Save As")
28+
self.file_save_as.setShortcut("Ctrl+A")
29+
self.file_save_as.setIcon(style.standardIcon(QStyle.StandardPixmap.SP_DialogOpenButton))
30+
31+
@staticmethod
32+
def get_custom_icon(icon_name: str):
33+
file_path = os.path.join(ICON_DIRECTORY, icon_name + ".png")
34+
assert os.path.isfile(file_path)
35+
return QIcon(file_path)
256 Bytes
Loading

src/prototype_ui/main.py

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import sys
2+
import os
3+
4+
from PyQt6.QtCore import Qt
5+
from PyQt6.QtWidgets import QMainWindow, QApplication, QLabel, QProgressBar
6+
7+
from prototype_ui.actions import Actions
8+
from prototype_ui.menubar import MenuBarManager
9+
from prototype_ui.toolbar import ToolBarManager
10+
11+
12+
class MainWindow(QMainWindow):
13+
def __init__(self):
14+
super().__init__()
15+
16+
self.setMinimumSize(200, 200)
17+
self.resize(500, 400)
18+
self.setWindowTitle("Example")
19+
20+
# Status Bar
21+
self._status_bar_label = QLabel("Hello Status Bar")
22+
self._status_bar_progress = QProgressBar()
23+
self._status_bar_progress.setRange(0, 10)
24+
self._status_bar_progress.setValue(7)
25+
self.statusBar().addPermanentWidget(self._status_bar_label)
26+
self.statusBar().addPermanentWidget(self._status_bar_progress)
27+
28+
# Define UI actions
29+
self._actions = Actions(self.style())
30+
31+
# Menu & Tool bars
32+
self._menu_bar_manager = MenuBarManager(self.menuBar(), self._actions)
33+
self._tool_bar_manager = ToolBarManager(self.addToolBar("Main"), self._actions)
34+
35+
# Connect actions with callback methods to implement them
36+
self._actions.file_new.triggered.connect(self._new_file)
37+
self._actions.file_open.triggered.connect(self._open_file)
38+
39+
self.canvas = QLabel("Hello")
40+
self.setCentralWidget(self.canvas)
41+
42+
self.show()
43+
44+
def _new_file(self):
45+
print("New File")
46+
self.statusBar().showMessage("Hello Briefly", 5000) # 5 secs
47+
48+
def _open_file(self):
49+
self.canvas.setCursor(Qt.CursorShape.CrossCursor)
50+
51+
52+
if __name__ == '__main__':
53+
app = QApplication([])
54+
55+
# app.setStyleSheet("QLabel { color: red } QProgressBar::chunk { background-color: blue }")
56+
57+
window = MainWindow()
58+
sys.exit(app.exec())

src/prototype_ui/menubar.py

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from PyQt6.QtWidgets import QMenuBar
2+
3+
from prototype_ui.actions import Actions
4+
5+
6+
class MenuBarManager:
7+
def __init__(self, menu_bar: QMenuBar, actions: Actions):
8+
self._menu_bar = menu_bar
9+
self._actions = actions
10+
self.setup_menu_bar()
11+
12+
def setup_menu_bar(self):
13+
self._create_file_menu()
14+
15+
def _create_file_menu(self):
16+
menu = self._menu_bar.addMenu("File")
17+
menu.addAction(self._actions.file_new)
18+
menu.addAction(self._actions.file_open)
19+
menu.addAction(self._actions.file_save)
20+
menu.addAction(self._actions.file_save_as)

src/prototype_ui/toolbar.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from PyQt6.QtWidgets import QToolBar
2+
3+
from prototype_ui.actions import Actions
4+
5+
6+
class ToolBarManager:
7+
def __init__(self, tool_bar: QToolBar, actions: Actions):
8+
self._toolbar = tool_bar
9+
self._actions = actions
10+
self.setup_tool_bar()
11+
12+
def setup_tool_bar(self):
13+
self._toolbar.addAction(self._actions.file_new)
14+
self._toolbar.addAction(self._actions.file_open)
15+
self._toolbar.addAction(self._actions.file_save)
16+
self._toolbar.addAction(self._actions.file_save_as)

0 commit comments

Comments
 (0)