Coverage for /usr/local/lib/python3.11/dist-packages/pyrocko/gui/sparrow/elements/hud.py: 99%
113 statements
« prev ^ index » next coverage.py v6.5.0, created at 2023-10-06 15:01 +0000
« prev ^ index » next coverage.py v6.5.0, created at 2023-10-06 15:01 +0000
1# https://pyrocko.org - GPLv3
2#
3# The Pyrocko Developers, 21st Century
4# ---|P------/S----------~Lg----------
6import vtk
8from pyrocko.guts import Bool, String, StringChoice, Float
9from pyrocko.gui.qt_compat import qw, qc
10from pyrocko.gui.talkie import TalkieConnectionOwner, TalkieStringer
13from .base import Element, ElementState
14from .. import common
16guts_prefix = 'sparrow'
19class HudPositionChoice(StringChoice):
20 choices = ['bottom', 'bottom-left', 'bottom-right',
21 'top', 'top-left', 'top-right']
24class HudState(ElementState):
25 visible = Bool.T(default=True)
26 template = String.T()
27 position = HudPositionChoice.T(default='bottom')
28 lightness = Float.T(default=1.0)
29 fontsize = Float.T(default=0.05)
31 def create(self):
32 element = HudElement()
33 return element
36class HudElement(Element):
38 def __init__(self):
39 Element.__init__(self)
40 self._controls = None
41 self._actor = None
42 self._connections2 = TalkieConnectionOwner()
44 def get_name(self):
45 return 'HUD'
47 def bind_state(self, state):
48 Element.bind_state(self, state)
50 self.talkie_connect(
51 state,
52 ['visible', 'lightness', 'fontsize', 'template', 'position'],
53 self.update)
55 self.talkie_connect(state, 'template', self.update_bindings)
57 def unbind_state(self):
58 self._connections2.talkie_disconnect_all()
59 Element.unbind_state(self)
61 def set_parent(self, parent):
62 self._parent = parent
63 self._parent.add_panel(
64 self.get_title_label(),
65 self._get_controls(),
66 visible=True,
67 title_controls=[
68 self.get_title_control_remove(),
69 self.get_title_control_visible()])
71 self.talkie_connect(
72 self._parent.gui_state, 'size', self.update)
74 self.update_bindings()
75 self.update()
77 def unset_parent(self):
78 self.unbind_state()
79 if self._parent:
80 if self._actor is not None:
81 self._parent.remove_actor_2d(self._actor)
83 if self._controls is not None:
84 self._parent.remove_panel(self._controls)
85 self._controls = None
87 self._parent.update_view()
88 self._parent = None
90 def update_bindings(self, *args):
91 self._connections2.talkie_disconnect_all()
92 s = TalkieStringer(self._parent.state)
93 self._state.template.format_map(s)
94 for (state, path) in s.get_paths():
95 self._connections2.talkie_connect(state, path, self.update)
97 def update(self, *args):
98 state = self._state
99 if not self._state:
100 return
101 pstate = self._parent.state
103 if self._actor is None:
104 self._actor = vtk.vtkTextActor()
106 actor = self._actor
108 s = TalkieStringer(pstate)
109 actor.SetInput(self._state.template.format_map(s))
111 sx, sy = self._parent.gui_state.size
112 cx = 0.5 * sx
113 # cy = 0.5 * sy
114 off = 0.1 * sy
115 pos = {
116 'top': (cx, sy - off, 1, 2),
117 'top-left': (off, sy - off, 0, 2),
118 'top-right': (sx - off, sy - off, 2, 2),
119 'bottom': (cx, off, 1, 0),
120 'bottom-left': (off, off, 0, 0),
121 'bottom-right': (sx - off, off, 2, 0)}
122 x, y, hj, vj = pos[state.position]
124 actor.SetPosition(x, y)
125 # actor.SetPosition2(200, 100)
126 prop = actor.GetTextProperty()
127 prop.SetFontSize(int(round(state.fontsize*sy)))
129 lightness = state.lightness
130 prop.SetColor(lightness*0.8, lightness*0.8, lightness*0.7)
131 prop.SetJustification(hj)
132 prop.SetVerticalJustification(vj)
134 if state.visible:
135 self._parent.add_actor_2d(actor)
136 else:
137 self._parent.remove_actor_2d(actor)
139 self._parent.update_view()
141 def _get_controls(self):
142 if not self._controls:
143 from ..state import state_bind_lineedit, \
144 state_bind_combobox, state_bind_slider
146 frame = qw.QFrame()
147 layout = qw.QGridLayout()
148 frame.setLayout(layout)
150 layout.addWidget(qw.QLabel('Template'), 0, 0)
151 le = qw.QLineEdit()
152 layout.addWidget(le, 0, 1)
153 state_bind_lineedit(self, self._state, 'template', le)
155 cb = common.string_choices_to_combobox(HudPositionChoice)
156 layout.addWidget(qw.QLabel('Position'), 1, 0)
157 layout.addWidget(cb, 1, 1)
158 state_bind_combobox(self, self._state, 'position', cb)
160 layout.addWidget(qw.QLabel('Lightness'), 2, 0)
162 slider = qw.QSlider(qc.Qt.Horizontal)
163 slider.setSizePolicy(
164 qw.QSizePolicy(
165 qw.QSizePolicy.Expanding, qw.QSizePolicy.Fixed))
166 slider.setMinimum(0)
167 slider.setMaximum(1000)
168 layout.addWidget(slider, 2, 1)
170 state_bind_slider(
171 self, self._state, 'lightness', slider, factor=0.001)
173 layout.addWidget(qw.QLabel('Fontsize'), 3, 0)
175 slider = qw.QSlider(qc.Qt.Horizontal)
176 slider.setSizePolicy(
177 qw.QSizePolicy(
178 qw.QSizePolicy.Expanding, qw.QSizePolicy.Fixed))
179 slider.setMinimum(0)
180 slider.setMaximum(1000)
181 layout.addWidget(slider, 3, 1)
183 state_bind_slider(
184 self, self._state, 'fontsize', slider, factor=0.001)
186 layout.addWidget(qw.QFrame(), 4, 0)
188 self._controls = frame
190 return self._controls
193__all__ = [
194 'HudElement',
195 'HudState']