op13-task5/composer.py
2026-02-27 23:18:40 +03:00

57 lines
No EOL
1.9 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from src.windows import LoginWindow, MainWindow
from src.objects import User
from src.db import DB_CONFIG
from PyQt6.QtWidgets import QApplication
from PyQt6.QtCore import QObject, pyqtSlot, pyqtSignal
from PyQt6.QtSql import QSqlDatabase
class Composer(QObject):
"""Управляет навигацией между окнами и жизненным циклом приложения"""
render_request = pyqtSignal(User)
def __init__(self):
super().__init__()
self._current = None
self._app = QApplication([])
self._init_db()
self.render_request.connect(self._render)
def _init_db(self):
"""Инициализация Qt SQL соединения (используется QSqlTableModel)"""
self._db = QSqlDatabase.addDatabase("QPSQL")
self._db.setDatabaseName(DB_CONFIG['dbname'])
self._db.setPort(DB_CONFIG['port'])
self._db.setHostName(DB_CONFIG['host'])
self._db.setUserName(DB_CONFIG['user'])
self._db.setPassword(DB_CONFIG['password'])
if not self._db.open():
raise Exception(
f"Не удалось подключиться к БД: {self._db.lastError().text()}"
)
@pyqtSlot(User)
def _render(self, user: User):
"""Маршрутизация: все роли идут в MainWindow, права применяются внутри"""
self._main_fabric(user)
def _login_fabric(self):
self.wlogin = LoginWindow(self, self._db)
self._switch_window(self.wlogin)
def _main_fabric(self, user: User):
self.wmain = MainWindow(self, self._db, user)
self._switch_window(self.wmain)
def _switch_window(self, new_window):
if self._current:
self._current.close()
new_window.show()
self._current = new_window
def run(self):
import sys
self._login_fabric()
sys.exit(self._app.exec())