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 06:59 +0000

1# https://pyrocko.org - GPLv3 

2# 

3# The Pyrocko Developers, 21st Century 

4# ---|P------/S----------~Lg---------- 

5 

6import vtk 

7 

8from pyrocko.guts import Bool, String, StringChoice, Float 

9from pyrocko.gui.qt_compat import qw, qc 

10from pyrocko.gui.talkie import TalkieConnectionOwner, TalkieStringer 

11 

12 

13from .base import Element, ElementState 

14from .. import common 

15 

16guts_prefix = 'sparrow' 

17 

18 

19class HudPositionChoice(StringChoice): 

20 choices = ['bottom', 'bottom-left', 'bottom-right', 

21 'top', 'top-left', 'top-right'] 

22 

23 

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) 

30 

31 def create(self): 

32 element = HudElement() 

33 return element 

34 

35 

36class HudElement(Element): 

37 

38 def __init__(self): 

39 Element.__init__(self) 

40 self._controls = None 

41 self._actor = None 

42 self._connections2 = TalkieConnectionOwner() 

43 

44 def get_name(self): 

45 return 'HUD' 

46 

47 def bind_state(self, state): 

48 Element.bind_state(self, state) 

49 

50 self.talkie_connect( 

51 state, 

52 ['visible', 'lightness', 'fontsize', 'template', 'position'], 

53 self.update) 

54 

55 self.talkie_connect(state, 'template', self.update_bindings) 

56 

57 def unbind_state(self): 

58 self._connections2.talkie_disconnect_all() 

59 Element.unbind_state(self) 

60 

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()]) 

70 

71 self.talkie_connect( 

72 self._parent.gui_state, 'size', self.update) 

73 

74 self.update_bindings() 

75 self.update() 

76 

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) 

82 

83 if self._controls is not None: 

84 self._parent.remove_panel(self._controls) 

85 self._controls = None 

86 

87 self._parent.update_view() 

88 self._parent = None 

89 

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) 

96 

97 def update(self, *args): 

98 state = self._state 

99 if not self._state: 

100 return 

101 pstate = self._parent.state 

102 

103 if self._actor is None: 

104 self._actor = vtk.vtkTextActor() 

105 

106 actor = self._actor 

107 

108 s = TalkieStringer(pstate) 

109 actor.SetInput(self._state.template.format_map(s)) 

110 

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] 

123 

124 actor.SetPosition(x, y) 

125 # actor.SetPosition2(200, 100) 

126 prop = actor.GetTextProperty() 

127 prop.SetFontSize(int(round(state.fontsize*sy))) 

128 

129 lightness = state.lightness 

130 prop.SetColor(lightness*0.8, lightness*0.8, lightness*0.7) 

131 prop.SetJustification(hj) 

132 prop.SetVerticalJustification(vj) 

133 

134 if state.visible: 

135 self._parent.add_actor_2d(actor) 

136 else: 

137 self._parent.remove_actor_2d(actor) 

138 

139 self._parent.update_view() 

140 

141 def _get_controls(self): 

142 if not self._controls: 

143 from ..state import state_bind_lineedit, \ 

144 state_bind_combobox, state_bind_slider 

145 

146 frame = qw.QFrame() 

147 layout = qw.QGridLayout() 

148 frame.setLayout(layout) 

149 

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) 

154 

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) 

159 

160 layout.addWidget(qw.QLabel('Lightness'), 2, 0) 

161 

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) 

169 

170 state_bind_slider( 

171 self, self._state, 'lightness', slider, factor=0.001) 

172 

173 layout.addWidget(qw.QLabel('Fontsize'), 3, 0) 

174 

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) 

182 

183 state_bind_slider( 

184 self, self._state, 'fontsize', slider, factor=0.001) 

185 

186 layout.addWidget(qw.QFrame(), 4, 0) 

187 

188 self._controls = frame 

189 

190 return self._controls 

191 

192 

193__all__ = [ 

194 'HudElement', 

195 'HudState']