forked from napari/napari
-
Notifications
You must be signed in to change notification settings - Fork 0
/
__init__.py
53 lines (43 loc) · 1.91 KB
/
__init__.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
from functools import lru_cache
from itertools import chain
from typing import Optional
# Submodules should be able to import from most modules, so to
# avoid circular imports, don't import submodules at the top level here,
# import them inside the init_qactions function.
@lru_cache # only call once
def init_qactions() -> None:
"""Initialize all Qt-based Actions with app-model
This function will be called in _QtMainWindow.__init__(). It should only
be called once (hence the lru_cache decorator).
It is responsible for:
- injecting Qt-specific names into the application injection_store namespace
(this is what allows functions to be declared with annotations like
`def foo(window: Window)` or `def foo(qt_viewer: QtViewer)`)
- registering provider functions for the names added to the namespace
- registering Qt-dependent actions with app-model (i.e. Q_*_ACTIONS actions).
"""
from napari._app_model import get_app
from napari._qt._qapp_model.qactions._help import Q_HELP_ACTIONS
from napari._qt._qapp_model.qactions._view import Q_VIEW_ACTIONS
from napari._qt.qt_main_window import Window, _QtMainWindow
from napari._qt.qt_viewer import QtViewer
# update the namespace with the Qt-specific types/providers/processors
app = get_app()
store = app.injection_store
store.namespace = {
**store.namespace,
'Window': Window,
'QtViewer': QtViewer,
}
# Qt-specific providers/processors
@store.register_provider
def _provide_window() -> Optional[Window]:
if _qmainwin := _QtMainWindow.current():
return _qmainwin._window
@store.register_provider
def _provide_qt_viewer() -> Optional[QtViewer]:
if _qmainwin := _QtMainWindow.current():
return _qmainwin._qt_viewer
# register actions
for action in chain(Q_VIEW_ACTIONS, Q_HELP_ACTIONS):
app.register_action(action)