Repository navigation
Expand file tree
/
Copy pathsession.py
More file actions
68 lines (49 loc) · 2.07 KB
/
Copy pathsession.py
File metadata and controls
68 lines (49 loc) · 2.07 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
"""
Session persistence between one launch and the next.
Uses QSettings (native to Qt, no extra dependency): Windows registry,
.ini file in ~/.config on Linux, plist on macOS. Requires no special
permissions and no paths to manage by hand.
We save two separate things:
- "language": a plain string, read BEFORE building the UI (needed to
create the labels already in the right language from startup).
- "state_json": a single JSON blob with all slider/checkbox/color
values, applied AFTER the widgets exist. A single blob instead of
many individual keys avoids the type inconsistencies that QSettings
can introduce across different platforms (bool/float saved as string
on some backends).
"""
import json
from PySide6.QtCore import QSettings
_ORG = "BoxMakerHobby"
_APP = "BoxMaker"
def get_settings() -> QSettings:
return QSettings(_ORG, _APP)
def save_window_geometry(window, settings: QSettings | None = None):
settings = settings or get_settings()
settings.setValue("window/geometry", window.saveGeometry())
def restore_window_geometry(window, settings: QSettings | None = None) -> bool:
settings = settings or get_settings()
geo = settings.value("window/geometry")
if geo is None:
return False
window.restoreGeometry(geo)
return True
def load_language(default: str, settings: QSettings | None = None) -> str:
settings = settings or get_settings()
value = settings.value("language", default)
return value if isinstance(value, str) else default
def save_language(lang: str, settings: QSettings | None = None):
settings = settings or get_settings()
settings.setValue("language", lang)
def load_state(settings: QSettings | None = None) -> dict:
settings = settings or get_settings()
raw = settings.value("state_json", "")
if not raw:
return {}
try:
return json.loads(raw)
except (TypeError, ValueError):
return {}
def save_state(state: dict, settings: QSettings | None = None):
settings = settings or get_settings()
settings.setValue("state_json", json.dumps(state))