-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
97 lines (72 loc) · 2.36 KB
/
main.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
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
import signal
import sys
import os
import inotify.adapters
from PySide6.QtCore import QThread, QObject, Signal, Slot
from PySide6.QtGui import QGuiApplication
from PySide6.QtQml import QQmlApplicationEngine
main = os.path.join(os.path.dirname(__file__), 'qml', "main.qml")
class Master(QObject):
command = Signal()
@Slot()
def reload(self):
print("Loading UI")
windows = engine.rootObjects()
if len(windows) > 0:
pos = windows[-1].position()
width = windows[-1].width()
height = windows[-1].height()
state = windows[-1].windowState()
for window in windows:
window.close()
window.destroy()
engine.clearComponentCache()
engine.load(main)
if not engine.rootObjects():
sys.exit(-1)
# NOTE: Every load creates new rootObject
window = engine.rootObjects()[-1]
window.setPosition(pos); window.setWindowState(state)
window.resize(width, height)
else:
engine.load(main)
if not engine.rootObjects():
sys.exit(-1)
class Worker(QObject):
requestReload = Signal()
isRunning = True
@Slot()
def stop(self):
self.isRunning = False
@Slot()
def run(self):
self.requestReload.emit()
i = inotify.adapters.InotifyTree('./qml')
while self.isRunning:
events = i.event_gen(yield_nones=False, timeout_s=1)
reload = False
for event in list(events):
(_, type_names, path, filename) = event
if filename[-3:] == 'qml' and 'IN_MODIFY' in type_names:
reload = True
break
if reload:
self.requestReload.emit()
if __name__ == "__main__":
app = QGuiApplication(sys.argv)
engine = QQmlApplicationEngine()
workerThread = QThread()
workerThread.start()
worker = Worker()
worker.moveToThread(workerThread)
master = Master()
master.command.connect(worker.run)
worker.requestReload.connect(master.reload)
master.command.emit()
# Stop application gracefully:
signal.signal(signal.SIGINT, signal.SIG_DFL)
status = app.exec_()
worker.stop()
workerThread.quit()
workerThread.wait()
sys.exit(status)