diff --git a/superscore/type_hints.py b/superscore/type_hints.py index 75ff074..15d52eb 100644 --- a/superscore/type_hints.py +++ b/superscore/type_hints.py @@ -1,4 +1,8 @@ -from typing import Dict, Protocol, Union +from typing import TYPE_CHECKING, Callable, Dict, Protocol, Union + +if TYPE_CHECKING: + from superscore.model import Entry + from superscore.widgets.core import DataWidget AnyEpicsType = Union[int, str, float, bool] @@ -8,3 +12,6 @@ class AnyDataclass(Protocol): Protocol stub shamelessly lifted from stackoverflow to hint at dataclass """ __dataclass_fields__: Dict + + +OpenPageSlot = Callable[["Entry"], "DataWidget"] diff --git a/superscore/widgets/page/collection_builder.py b/superscore/widgets/page/collection_builder.py index 6b9b261..4d115c1 100644 --- a/superscore/widgets/page/collection_builder.py +++ b/superscore/widgets/page/collection_builder.py @@ -1,11 +1,12 @@ import logging -from typing import Callable, Optional +from typing import Optional from qtpy import QtCore, QtWidgets from qtpy.QtGui import QCloseEvent from superscore.client import Client from superscore.model import Collection, Entry, Parameter +from superscore.type_hints import OpenPageSlot from superscore.widgets.core import DataWidget, Display, NameDescTagsWidget from superscore.widgets.enhanced import FilterComboBox from superscore.widgets.manip_helpers import insert_widget @@ -47,7 +48,7 @@ def __init__( *args, client: Client, data: Optional[Collection] = None, - open_page_slot: Optional[Callable] = None, + open_page_slot: Optional[OpenPageSlot] = None, **kwargs ): if data is None: diff --git a/superscore/widgets/page/search.py b/superscore/widgets/page/search.py index 2b73bb0..7b80ac8 100644 --- a/superscore/widgets/page/search.py +++ b/superscore/widgets/page/search.py @@ -2,7 +2,7 @@ import logging from enum import auto -from typing import Any, Callable, Dict, List, Optional +from typing import Any, Dict, List, Optional import qtawesome as qta from dateutil import tz @@ -11,6 +11,7 @@ from superscore.backends.core import SearchTerm from superscore.client import Client from superscore.model import Collection, Entry, Readback, Setpoint, Snapshot +from superscore.type_hints import OpenPageSlot from superscore.widgets import ICON_MAP from superscore.widgets.core import Display from superscore.widgets.views import (BaseTableEntryModel, ButtonDelegate, @@ -56,7 +57,7 @@ def __init__( self, *args, client: Client, - open_page_slot: Optional[Callable] = None, + open_page_slot: Optional[OpenPageSlot] = None, **kwargs ) -> None: super().__init__(*args, **kwargs) @@ -234,7 +235,7 @@ class ResultFilterProxyModel(QtCore.QSortFilterProxyModel): def __init__( self, *args, - open_page_slot: Optional[Callable] = None, + open_page_slot: Optional[OpenPageSlot] = None, **kwargs ) -> None: super().__init__(*args, **kwargs) diff --git a/superscore/widgets/views.py b/superscore/widgets/views.py index 113598b..3ea0607 100644 --- a/superscore/widgets/views.py +++ b/superscore/widgets/views.py @@ -7,8 +7,7 @@ import logging import time from enum import Enum, IntEnum, auto -from typing import (Any, Callable, ClassVar, Dict, Generator, List, Optional, - Union) +from typing import Any, ClassVar, Dict, Generator, List, Optional, Union from uuid import UUID from weakref import WeakValueDictionary @@ -23,6 +22,7 @@ from superscore.model import (Collection, Entry, Nestable, Parameter, Readback, Root, Setpoint, Severity, Snapshot, Status) from superscore.qt_helpers import QDataclassBridge +from superscore.type_hints import OpenPageSlot from superscore.widgets import ICON_MAP logger = logging.getLogger(__name__) @@ -1047,7 +1047,7 @@ def __init__( *args, client: Optional[Client] = None, data: Optional[Union[Entry, List[Entry]]] = None, - open_page_slot: Optional[Callable] = None, + open_page_slot: Optional[OpenPageSlot] = None, **kwargs, ) -> None: """need to set open_column, close_column in subclass""" diff --git a/superscore/widgets/window.py b/superscore/widgets/window.py index b389d75..964c235 100644 --- a/superscore/widgets/window.py +++ b/superscore/widgets/window.py @@ -14,7 +14,7 @@ from superscore.client import Client from superscore.model import Entry from superscore.widgets import ICON_MAP -from superscore.widgets.core import Display +from superscore.widgets.core import DataWidget, Display from superscore.widgets.page import PAGE_MAP from superscore.widgets.page.collection_builder import CollectionBuilderPage from superscore.widgets.page.search import SearchPage @@ -87,7 +87,7 @@ def open_collection_builder(self): ) self._partial_slots.append(update_slot) - def open_page(self, entry: Entry) -> None: + def open_page(self, entry: Entry) -> DataWidget: """ Open a page for ``entry`` in a new tab. @@ -95,6 +95,11 @@ def open_page(self, entry: Entry) -> None: ---------- entry : Entry Entry subclass to open a new page for + + Returns + ------- + DataWidget + Created widget, for cross references """ logger.debug(f'attempting to open {entry}') if not isinstance(entry, Entry): @@ -119,6 +124,8 @@ def open_page(self, entry: Entry) -> None: idx = self.tab_widget.addTab(page_widget, icon, tab_name) self.tab_widget.setCurrentIndex(idx) + return page_widget + def open_search_page(self) -> None: page = SearchPage(client=self.client, open_page_slot=self.open_page) self.tab_widget.addTab(page, 'search')