-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEditor.py
More file actions
executable file
·123 lines (100 loc) · 3.18 KB
/
Editor.py
File metadata and controls
executable file
·123 lines (100 loc) · 3.18 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
#!/usr/bin/env -S uv run --script
#
import json
import os
import sys
from PySide6.QtCore import Property, QObject, QUrl, Signal, Slot
from PySide6.QtQml import QQmlApplicationEngine
from PySide6.QtWidgets import QApplication, QFileDialog
class AppModel(QObject):
appsChanged = Signal()
filePathChanged = Signal()
def __init__(self):
super().__init__()
self._apps = {} # {name: {fields...}}
self._file_path = ""
@Property("QVariant", notify=appsChanged)
def apps(self):
return self._apps
@Property(str, notify=filePathChanged)
def filePath(self):
return self._file_path
@Slot(str)
def loadJson(self, path):
if not path:
return
try:
with open(path, "r", encoding="utf-8") as f:
self._apps = json.load(f)
self._file_path = path
self.appsChanged.emit()
self.filePathChanged.emit()
except Exception as e:
print(f"Failed to load: {e}")
@Slot()
def openJsonDialog(self):
dialog = QFileDialog()
dialog.setFileMode(QFileDialog.ExistingFile)
dialog.setNameFilter("JSON Files (*.json)")
if dialog.exec():
files = dialog.selectedFiles()
if files:
self.loadJson(files[0])
@Slot()
def saveJson(self):
if not self._file_path:
self.saveJsonAs()
return
try:
with open(self._file_path, "w", encoding="utf-8") as f:
json.dump(self._apps, f, indent=2)
print(f"Saved to {self._file_path}")
except Exception as e:
print(f"Failed to save: {e}")
@Slot()
def saveJsonAs(self):
dialog = QFileDialog()
dialog.setAcceptMode(QFileDialog.AcceptSave)
dialog.setNameFilter("JSON Files (*.json)")
if dialog.exec():
files = dialog.selectedFiles()
if files:
self._file_path = files[0]
self.filePathChanged.emit()
self.saveJson()
@Slot(str, "QVariant")
def updateApp(self, name, appdata):
self._apps[name] = appdata
self.appsChanged.emit()
@Slot(str)
def removeApp(self, name):
if name in self._apps:
del self._apps[name]
self.appsChanged.emit()
@Slot("QVariant")
def addApp(self, appdata):
name = appdata.get("name", "")
if name:
self._apps[name] = appdata
self.appsChanged.emit()
@Slot(result="QVariant")
def getAppNames(self):
return list(self._apps.keys())
@Slot(str, result="QVariant")
def getApp(self, name):
return self._apps.get(name, {})
def main():
app = QApplication(sys.argv)
engine = QQmlApplicationEngine()
model = AppModel()
# Load from command line if provided
if len(sys.argv) > 1 and os.path.exists(sys.argv[1]):
model.loadJson(sys.argv[1])
engine.rootContext().setContextProperty("appModel", model)
engine.load(QUrl.fromLocalFile(("Editor.qml")))
if not engine.rootObjects():
sys.exit(-1)
sys.exit(app.exec())
print("finished")
if __name__ == "__main__":
main()