|
| 1 | +from __future__ import annotations |
| 2 | +import asyncio |
| 3 | +import sys |
| 4 | +from typing import List, Dict, Tuple, Any, Optional |
| 5 | + |
| 6 | +import qasync |
| 7 | +from qasync import QEventLoop # type: ignore |
| 8 | +from PyQt5 import QtWidgets, QtGui |
| 9 | + |
| 10 | +from pyobs.interfaces import IFitsHeaderBefore |
| 11 | +from pyobs.modules import Module |
| 12 | +from .base import BaseWindow |
| 13 | +from .mainwindow import MainWindow, DEFAULT_WIDGETS |
| 14 | + |
| 15 | + |
| 16 | +class ModuleWindow(QtWidgets.QMainWindow, BaseWindow): |
| 17 | + def __init__(self, gui_module: ModuleGUI, **kwargs: Any): |
| 18 | + QtWidgets.QMainWindow.__init__(self) |
| 19 | + BaseWindow.__init__(self) |
| 20 | + self.gui_module = gui_module |
| 21 | + |
| 22 | + async def open(self, module: Optional[Module] = None, **kwargs: Any) -> None: |
| 23 | + """Open module.""" |
| 24 | + |
| 25 | + # what do we have? |
| 26 | + widget, icon = None, None |
| 27 | + for interface, klass in DEFAULT_WIDGETS.items(): |
| 28 | + if isinstance(module, interface): |
| 29 | + widget = self.create_widget(klass) |
| 30 | + self.setCentralWidget(widget) |
| 31 | + break |
| 32 | + |
| 33 | + # open widgets |
| 34 | + await BaseWindow.open(self, module=module, **kwargs) |
| 35 | + |
| 36 | + def closeEvent(self, a0: QtGui.QCloseEvent) -> None: |
| 37 | + self.gui_module.quit() |
| 38 | + |
| 39 | + |
| 40 | +class ModuleGUI(Module, IFitsHeaderBefore): |
| 41 | + __module__ = "pyobs_gui" |
| 42 | + |
| 43 | + app: Optional[QtWidgets.QApplication] = None |
| 44 | + |
| 45 | + def __init__( |
| 46 | + self, |
| 47 | + module: Dict[str, Any], |
| 48 | + *args: Any, |
| 49 | + **kwargs: Any, |
| 50 | + ): |
| 51 | + """Inits a new module GUI. |
| 52 | +
|
| 53 | + Args: |
| 54 | + show_shell: Whether to show the shell page. |
| 55 | + show_events: Whether to show the events page. |
| 56 | + show_modules: If not empty, show only listed modules. |
| 57 | + widgets: List of custom widgets. |
| 58 | + sidebar: List of custom sidebar widgets. |
| 59 | + """ |
| 60 | + |
| 61 | + # init module |
| 62 | + Module.__init__(self, *args, **kwargs) |
| 63 | + self._window: Optional[MainWindow] = None |
| 64 | + self._module = self.add_child_object(module, Module, own_comm=False) |
| 65 | + |
| 66 | + @staticmethod |
| 67 | + def new_event_loop() -> asyncio.AbstractEventLoop: |
| 68 | + ModuleGUI.app = QtWidgets.QApplication(sys.argv) |
| 69 | + return qasync.QEventLoop(ModuleGUI.app) |
| 70 | + |
| 71 | + async def open(self) -> None: |
| 72 | + """Open module.""" |
| 73 | + await Module.open(self) |
| 74 | + |
| 75 | + # open module |
| 76 | + await self._module.open() |
| 77 | + |
| 78 | + # create new mainwindow |
| 79 | + self._window = ModuleWindow(self) |
| 80 | + await self._window.open( |
| 81 | + module=self._module, |
| 82 | + comm=self.comm, |
| 83 | + vfs=self.vfs, |
| 84 | + observer=self.observer, |
| 85 | + ) |
| 86 | + self._window.show() |
| 87 | + |
| 88 | + async def get_fits_header_before( |
| 89 | + self, namespaces: Optional[List[str]] = None, **kwargs: Any |
| 90 | + ) -> Dict[str, Tuple[Any, str]]: |
| 91 | + """Returns FITS header for the current status of this module. |
| 92 | +
|
| 93 | + Args: |
| 94 | + namespaces: If given, only return FITS headers for the given namespaces. |
| 95 | +
|
| 96 | + Returns: |
| 97 | + Dictionary containing FITS headers. |
| 98 | + """ |
| 99 | + if self._window is not None: |
| 100 | + return self._window.get_fits_headers(namespaces) |
| 101 | + else: |
| 102 | + return {} |
0 commit comments