Files
kontor/gui/kontor.py
T
2025-01-08 22:25:44 +01:00

132 lines
4.4 KiB
Python

"""
PyQT6 GUI for Kontor
"""
import sys
from pathlib import Path
import yaml
from PySide6.QtGui import QAction, QIcon
from PySide6.QtSql import QSqlDatabase
from PySide6.QtWidgets import QWidget, QVBoxLayout, QMenu, QMessageBox, QTabWidget, QTableView
from PySide6.QtWidgets import QApplication, QLabel, QMainWindow
from platformdirs import PlatformDirs
from comic_model import ComicTableModel
from media_file_model import MediaFileTableModel
class MainWindow(QMainWindow):
def __init__(self, config):
super().__init__()
self.tick = QIcon('tick.png')
self.cross = QIcon('cross.png')
self.import_icon = QIcon("application-import.png")
self.export_icon = QIcon("application-export.png")
self.circle_icon = QIcon("arrow-circle-double.png")
self.setWindowTitle("Kontor")
self.setMinimumSize(800, 500)
self._createActions()
self._createMenuBar()
self._createToolBars()
self._createStatusBar()
self.data = []
self.central_widget = QWidget()
parent_layout = QVBoxLayout()
self.central_widget.setLayout(parent_layout)
self.tabs = QTabWidget()
self.tabs.addTab(self.generate_tab_comics(config), "Comics")
self.tabs.addTab(self.generate_tab_media_file(config), "MediaFile")
self.tabs.currentChanged.connect(self._tab_changed)
#label.setAlignment(Qt.AlignmentFlag.AlignCenter)
parent_layout.addWidget(self.tabs)
self.setCentralWidget(self.central_widget)
def _createActions(self):
self.newAction = QAction("&New", self)
self.aboutAction = QAction("&Über...", self)
self.aboutAction.triggered.connect(self.about)
self.importAction = QAction(self.import_icon, "&Import", self)
self.exportAction = QAction(self.export_icon, "&Export", self)
self.refreshAction = QAction(self.circle_icon, "&Refresh", self)
self.refreshAction.triggered.connect(self.refresh)
self.exitAction = QAction("&Beenden", self)
self.exitAction.setShortcut("Alt+F4")
self.exitAction.triggered.connect(self.close)
def _createMenuBar(self):
menu_bar = self.menuBar()
# File menu
file_menu = QMenu("&Datei")
menu_bar.addMenu(file_menu)
file_menu.addAction(self.exitAction)
# Kontor menu
kontor_menu = QMenu("&Kontor")
menu_bar.addMenu(kontor_menu)
kontor_menu.addAction(self.importAction)
kontor_menu.addAction(self.exportAction)
# Help menu
help_menu = QMenu("&Hilfe")
menu_bar.addMenu(help_menu)
help_menu.addAction(self.aboutAction)
def _createToolBars(self):
# Kontor toolbar
kontor_tool_bar = self.addToolBar("Kontor")
kontor_tool_bar.addAction(self.importAction)
kontor_tool_bar.addAction(self.exportAction)
kontor_tool_bar.addAction(self.refreshAction)
def _createStatusBar(self):
self.statusBar = self.statusBar()
self.statusBar.showMessage("Kontor ready", 6000)
self.status_label = QLabel("")
self.statusBar.addPermanentWidget(self.status_label)
def about(self):
QMessageBox.about(self.central_widget, "Über Kontor", f"Python: 3.11\nKontor: 0.1.0")
def refresh(self):
self.data[self.tabs.currentIndex()].refresh()
def _tab_changed(self, tab_index):
self.data[tab_index].refresh()
def generate_tab_comics(self, db_configuration):
comic_tab = QWidget()
layout = QVBoxLayout()
comic_tab.setLayout(layout)
model = ComicTableModel(db_configuration, self)
self.data.append(model)
table_view = QTableView()
table_view.setModel(model)
layout.addWidget(table_view)
return comic_tab
def generate_tab_media_file(self, db_configuration):
media_file_tab = QWidget()
layout = QVBoxLayout()
model = MediaFileTableModel(db_configuration, self)
self.data.append(model)
media_file_tab.setLayout(layout)
table_view = QTableView()
table_view.setModel(model)
layout.addWidget(table_view)
return media_file_tab
if __name__ == '__main__':
app = QApplication(sys.argv)
dirs = PlatformDirs("kontor")
database_config = Path(dirs.user_config_dir, 'database-config.yaml')
with open(database_config, 'rt') as f:
db_config = yaml.safe_load(f.read())
window = MainWindow(db_config)
window.show()
app.exec()