-
Notifications
You must be signed in to change notification settings - Fork 0
/
navigation.py
57 lines (47 loc) · 1.97 KB
/
navigation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
from collections import deque
from controller import Controller
from screen import Screen
from PyQt5.QtWidgets import QWidget, QMainWindow
from PyQt5.QtCore import QTimer
class NavigationPath:
def __init__(self, widget: type[Screen], widgetArgs: list, controller: type[Controller]=None, controllerArgs: list=None):
self.widget = widget
self.widgetArgs = widgetArgs
self.controllerClass = controller
self.controllerArgs = controllerArgs
self.instantiated = False
def getScreen(self):
if not self.instantiated: raise Exception("Path not instantiated")
if self.controller != None:
return self.widget(self.params, controller=self.controller, *self.widgetArgs)
else:
return self.widget(self.params, *self.widgetArgs)
def instantiate(self, params: dict={}) -> QWidget:
self.params = params
self.controller = None
if self.controllerClass != None:
self.controller = self.controllerClass(*self.controllerArgs)
self.instantiated = True
class NavigationController:
def __init__(self, mainWidget: QMainWindow):
self.mainWidget = mainWidget
self.paths = {}
self.navStack = deque()
self.currentScreen= None
def setPath(self, path: str, navigationPath: NavigationPath):
self.paths[path] = navigationPath
def navigate(self, path: str, params: dict={}):
navPath = self.paths.get(path)
if navPath == None:
raise Exception("invalid path")
navPath.instantiate(params)
self.navStack.append(navPath)
self.__navigate()
def __navigate(self):
if self.currentScreen is not None: self.currentScreen.end()
self.currentScreen = self.navStack[-1].getScreen()
self.mainWidget.setCentralWidget(self.currentScreen)
QTimer.singleShot(5, self.currentScreen.start)
def popBackStack(self):
self.navStack.pop()
self.__navigate()