0
0
mirror of https://gitlab.nic.cz/labs/bird.git synced 2024-12-22 17:51:53 +00:00

Python GUI: Switching layouts

This commit is contained in:
Maria Matejka 2023-05-22 16:27:47 +02:00
parent fff44269b9
commit 19274f4301

View File

@ -8,7 +8,13 @@ import signal
import sys import sys
# Async worker thread # Async worker thread
class AsyncWorker(QRunnable): class AsyncWorker(QRunnable, QObject):
def __init__(self):
QRunnable.__init__(self)
QObject.__init__(self)
exception_signal = Signal(Exception)
@Slot() @Slot()
def run(self): def run(self):
self.loop = asyncio.new_event_loop() self.loop = asyncio.new_event_loop()
@ -20,48 +26,83 @@ class AsyncWorker(QRunnable):
async def _stop_internal(self): async def _stop_internal(self):
self.loop.stop() self.loop.stop()
async def dispatch_and_check_exception(coro):
try:
await coro
except Exception as e:
AsyncWorker.worker.exception_happened.emit(e)
def dispatch(coro): def dispatch(coro):
asyncio.run_coroutine_threadsafe(coro, AsyncWorker.worker.loop) asyncio.run_coroutine_threadsafe(AsyncWorker.dispatch_and_check_exception(coro), AsyncWorker.worker.loop)
if not hasattr(AsyncWorker, "worker"): if not hasattr(AsyncWorker, "worker"):
AsyncWorker.worker = AsyncWorker() AsyncWorker.worker = AsyncWorker()
class MainWindow(QMainWindow): class InitialLayout(QWidget):
connected_signal = Signal(BIRD)
start_signal = Signal()
done_signal = Signal()
def __init__(self): def __init__(self):
super().__init__() super().__init__()
self.bird = BIRD("/run/bird/bird.ctl") self.layout = QVBoxLayout(self)
self.initial_layout()
def initial_layout(self):
widget = QWidget()
self.setCentralWidget(widget)
layout = QVBoxLayout(widget)
self.text = QLabel("No connection to BIRD.") self.text = QLabel("No connection to BIRD.")
layout.addWidget(self.text, alignment=Qt.AlignmentFlag.AlignCenter) self.layout.addWidget(self.text, alignment=Qt.AlignmentFlag.AlignCenter)
async_trigger = QPushButton(text="Connect") async_trigger = QPushButton(text="Connect")
async_trigger.clicked.connect(self.connect) async_trigger.clicked.connect(self.connect_slot)
layout.addWidget(async_trigger, alignment=Qt.AlignmentFlag.AlignCenter) self.layout.addWidget(async_trigger, alignment=Qt.AlignmentFlag.AlignCenter)
@Slot() @Slot()
def connect(self): def connect_slot(self):
self.bird = BIRD("/run/bird/bird.ctl")
async def f(): async def f():
async with self.bird as b: async with self.bird as b:
await b.version.update() await b.version.update()
await b.status.update() await b.status.update()
self.text.setText(f"Connected to {b.version.name} {b.version.version}") self.connected_signal.emit(b)
AsyncWorker.dispatch(f()) AsyncWorker.dispatch(f())
class ConnectedLayout(QWidget):
def __init__(self, bird):
super().__init__()
self.bird = bird
self.layout = QVBoxLayout(self)
self.main_info = QLabel(f"Connected to {bird.version.name} {bird.version.version}")
self.layout.addWidget(self.main_info)
self.status = QLabel(f"Status: {bird.status.status}")
self.layout.addWidget(self.status)
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
AsyncWorker.worker.exception_signal.connect(self.exception_slot)
self.set_layout(InitialLayout())
self.layout.connected_signal.connect(self.connected_slot)
def set_layout(self, layout):
self.setCentralWidget(layout)
self.layout = layout
@Slot(Exception)
def exception_slot(self, e):
print("got exception")
raise Exception() from e
@Slot(BIRD)
def connected_slot(self, bird):
self.set_layout(ConnectedLayout(bird))
if __name__ == "__main__": if __name__ == "__main__":
app = QApplication(sys.argv) app = QApplication(sys.argv)
threadpool = QThreadPool() threadpool = QThreadPool()