-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
140 lines (106 loc) · 3.92 KB
/
app.py
File metadata and controls
140 lines (106 loc) · 3.92 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import argparse
import os
import sys
from importlib.metadata import PackageNotFoundError, version
from importlib.resources import files
from pathlib import Path
from PySide6.QtCore import QEvent
from PySide6.QtGui import QFileOpenEvent, QIcon
from PySide6.QtWidgets import QApplication
from .controllers import MainController
from .models import PEtabModel
from .views import MainWindow
def find_example(path: Path) -> Path:
"""Find the example directory by traversing up from the given path.
Args:
path: The starting path to search from
Returns:
Path: The path to the example directory
Raises:
FileNotFoundError: If the example directory cannot be found
"""
while path.parent != path:
if (path / "example").is_dir():
return path / "example"
path = path.parent
raise FileNotFoundError("Could not find examples directory")
def get_icon() -> QIcon:
"""Get the Icon for the Window."""
icon_path = files("petab_gui.assets").joinpath("PEtab.png")
if not icon_path.is_file():
raise FileNotFoundError(f"Icon file not found: {icon_path}")
return QIcon(str(icon_path))
class PEtabGuiApp(QApplication):
"""Main application class for PEtab GUI.
Inherits from QApplication and sets up the MVC components.
"""
def __init__(self, file: str | Path = None):
"""Initialize the PEtab GUI application.
Sets up the model, view, and controller components.
Handles command line arguments for opening files.
Args:
file: Path to a PEtab YAML file to open on startup.
"""
super().__init__(sys.argv)
self.setWindowIcon(get_icon())
self.model = PEtabModel()
self.view = MainWindow()
self.view.setWindowIcon(get_icon())
self.controller = MainController(self.view, self.model)
# Connect the view to the controller
self.view.controller = self.controller
if file and os.path.isfile(file):
self.controller.open_file(file, mode="overwrite")
self.view.show()
def event(self, event):
"""Handle application events.
Args:
event: The Qt event to handle
Returns:
bool: Result of the event handling from the parent class
Notes:
Currently handles FileOpen events to open files dropped on
the application.
"""
if event.type() == QEvent.FileOpen:
openEvent = QFileOpenEvent(event)
self.controller.open_file(openEvent.file(), mode="overwrite")
return super().event(event)
def apply_stylesheet(self):
"""Load and apply the QSS stylesheet to the application.
Reads the stylesheet.css file from the same directory as this module
and applies it to the application. If the file doesn't exist,
no stylesheet is applied.
"""
stylesheet_path = os.path.join(
os.path.dirname(__file__), "stylesheet.css"
)
if os.path.exists(stylesheet_path):
with open(stylesheet_path) as f:
self.setStyleSheet(f.read())
else:
pass
def main():
"""Entry point for the PEtab GUI application.
Creates the application instance and starts the event loop.
The function exits with the return code from the application.
"""
try:
pkg_version = version("petab_gui")
except PackageNotFoundError:
pkg_version = "unknown"
parser = argparse.ArgumentParser(description="PEtabGUI: A PEtab editor")
parser.add_argument(
"--version",
action="version",
version=f"%(prog)s {pkg_version}",
help="Show version number and exit",
)
parser.add_argument(
"petab_yaml", nargs="?", help="Path to the PEtab YAML file"
)
args = parser.parse_args()
app = PEtabGuiApp(args.petab_yaml)
sys.exit(app.exec())
if __name__ == "__main__":
main()