-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
122 lines (98 loc) · 4.36 KB
/
Copy pathmain.py
File metadata and controls
122 lines (98 loc) · 4.36 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
"""
PortusSIM — entry point.
Run this file to start the application: python main.py
"""
import os
import sys
from PySide6.QtWidgets import QApplication
from PySide6.QtCore import QCoreApplication
from PySide6.QtGui import QFontDatabase, QIcon
# Set the application name BEFORE QApplication is constructed.
# On macOS, Qt reads these names when initializing the menu bar; setting them
# afterwards leaves the menu showing "Python" or "main.py".
QCoreApplication.setApplicationName("PortusSIM")
QCoreApplication.setOrganizationName("Archaeological ABM")
QCoreApplication.setApplicationVersion("1.0.0")
# On macOS, also try to override the process name so the menu bar's
# application menu reads "PortusSIM" rather than "Python". This uses
# AppKit via pyobjc if available; falls back silently otherwise.
if sys.platform == "darwin":
try:
from Foundation import NSBundle
bundle = NSBundle.mainBundle()
info = bundle.localizedInfoDictionary() or bundle.infoDictionary()
if info is not None:
info["CFBundleName"] = "PortusSIM"
info["CFBundleDisplayName"] = "PortusSIM"
except ImportError:
# pyobjc not installed — menu bar will still say "Python" but the
# in-window UI is unaffected. Suggest: pip install pyobjc-framework-Cocoa
pass
from ui.v2.main_window import MainWindow
def resource_path(*parts):
"""Resolve a path to a bundled resource, working both when running from
source and when frozen by PyInstaller.
PyInstaller unpacks bundled data files into a temporary directory exposed
as sys._MEIPASS; from source we use this file's directory. All bundled
asset access should go through here so the packaged app finds its fonts,
icons, and presets.
"""
base = getattr(sys, "_MEIPASS", os.path.dirname(os.path.abspath(__file__)))
return os.path.join(base, *parts)
def _load_bundled_fonts():
"""Load Inter from the bundled assets so the UI looks consistent
regardless of what fonts the user's system has installed.
Returns the family name that should be used, or a fallback name."""
font_path = resource_path("assets", "fonts", "Inter-Variable.ttf")
if not os.path.exists(font_path):
return None # graceful fallback — stylesheet will use its font stack
font_id = QFontDatabase.addApplicationFont(font_path)
if font_id == -1:
return None
families = QFontDatabase.applicationFontFamilies(font_id)
return families[0] if families else None
def _build_app_icon():
"""Build a multi-resolution QIcon from the bundled PortusSIM logo PNGs.
Qt picks the closest size when the OS asks for an icon."""
logo_dir = resource_path("assets", "logo")
icon = QIcon()
added_any = False
for size in (16, 24, 32, 48, 64, 128, 256, 512):
path = os.path.join(logo_dir, f"portussim_mark_{size}.png")
if os.path.exists(path):
icon.addFile(path)
added_any = True
return icon if added_any else None
def main():
app = QApplication(sys.argv)
# Re-affirm (some platforms reset these during QApplication construction)
app.setApplicationName("PortusSIM")
app.setApplicationDisplayName("PortusSIM")
app.setOrganizationName("Archaeological ABM")
# Install an uncaught-exception hook so silent failures still appear in
# the debug log. Without this, exceptions raised inside Qt event handlers
# are sometimes printed only to stderr (and lost if launched from Finder).
from ui.v2.debug_log import LOG
import traceback
def _excepthook(exc_type, exc_value, tb):
formatted = "".join(traceback.format_exception(exc_type, exc_value, tb))
LOG.error(f"Uncaught {exc_type.__name__}: {exc_value}",
traceback=formatted)
# Still write to stderr for terminal users
sys.__excepthook__(exc_type, exc_value, tb)
sys.excepthook = _excepthook
LOG.info("PortusSIM started",
platform=sys.platform, python=sys.version.split()[0])
# Load Inter font before any windows are constructed
_load_bundled_fonts()
# Set the application icon (taskbar, Alt+Tab, dock when bundled)
icon = _build_app_icon()
if icon is not None:
app.setWindowIcon(icon)
window = MainWindow()
if icon is not None:
window.setWindowIcon(icon)
window.show()
sys.exit(app.exec())
if __name__ == "__main__":
main()