Coverage for /usr/local/lib/python3.11/dist-packages/pyrocko/gui/sparrow/elements/custom_topo.py: 20%

122 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2024-03-07 11:54 +0000

1# https://pyrocko.org - GPLv3 

2# 

3# The Pyrocko Developers, 21st Century 

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

5 

6from pyrocko.plot import automap 

7from pyrocko.dataset.topo import tile 

8from pyrocko.guts import String 

9from pyrocko.dataset import topo 

10from pyrocko.gui.qt_compat import qw, qc 

11 

12from pyrocko.gui.vtk_util import cpt_to_vtk_lookuptable 

13 

14from .base import Element 

15from .topo import TopoMeshPipe, TopoCPTChoice, TopoState 

16 

17from .. import common 

18 

19guts_prefix = 'sparrow' 

20 

21 

22class CustomTopoState(TopoState): 

23 path = String.T(optional=True) 

24 

25 def create(self): 

26 element = CustomTopoElement() 

27 return element 

28 

29 

30class CustomTopoElement(Element): 

31 

32 def __init__(self): 

33 Element.__init__(self) 

34 self._parent = None 

35 self._controls = None 

36 self._visible = False 

37 self._mesh = None 

38 self._lookuptables = {} 

39 self._path_loaded = None 

40 

41 def get_name(self): 

42 return 'Custom Topography' 

43 

44 def bind_state(self, state): 

45 Element.bind_state(self, state) 

46 self.talkie_connect( 

47 state, 

48 ['visible', 'exaggeration', 'opacity', 'smooth', 'cpt', 'path'], 

49 self.update) 

50 

51 def set_parent(self, parent): 

52 self._parent = parent 

53 

54 self._parent.add_panel( 

55 self.get_title_label(), 

56 self._get_controls(), 

57 visible=True, 

58 title_controls=[ 

59 self.get_title_control_remove(), 

60 self.get_title_control_visible()]) 

61 

62 self.update() 

63 

64 def unset_parent(self): 

65 self.unbind_state() 

66 if self._parent: 

67 if self._mesh is not None: 

68 self._parent.remove_actor(self._mesh.actor) 

69 

70 self._mesh = None 

71 

72 if self._controls: 

73 self._parent.remove_panel(self._controls) 

74 self._controls = None 

75 

76 self._parent.update_view() 

77 self._parent = None 

78 

79 def update_cpt(self, cpt_name): 

80 if cpt_name not in self._lookuptables: 

81 if cpt_name == 'light': 

82 topo_cpt_wet = 'light_sea' 

83 topo_cpt_dry = 'light_land' 

84 

85 elif cpt_name == 'uniform': 

86 topo_cpt_wet = 'light_sea_uniform' 

87 topo_cpt_dry = 'light_land_uniform' 

88 

89 cpt_wet = automap.read_cpt(topo.cpt(topo_cpt_wet)) 

90 cpt_dry = automap.read_cpt(topo.cpt(topo_cpt_dry)) 

91 cpt_combi = automap.cpt_merge_wet_dry(cpt_wet, cpt_dry) 

92 

93 lut_combi = cpt_to_vtk_lookuptable(cpt_combi) 

94 lut_combi.SetNanColor(0.0, 0.0, 0.0, 0.0) 

95 

96 self._lookuptables[cpt_name] = lut_combi 

97 

98 def update(self, *args): 

99 visible = self._state.visible 

100 

101 self.update_cpt(self._state.cpt) 

102 

103 if visible: 

104 if self._path_loaded is None and self._state.path is not None or \ 

105 self._state.path != self._path_loaded: 

106 

107 if self._mesh: 

108 self._parent.remove_actor(self._mesh.actor) 

109 self._mesh = None 

110 

111 t = tile.Tile.from_grd(self._state.path) 

112 self._path_loaded = self._state.path 

113 self._mesh = TopoMeshPipe( 

114 t, 

115 mask_ocean=False, 

116 smooth=self._state.smooth, 

117 lut=self._lookuptables[self._state.cpt]) 

118 

119 self._parent.add_actor(self._mesh.actor) 

120 

121 if not visible and self._mesh: 

122 self._parent.remove_actor(self._mesh.actor) 

123 

124 if self._mesh: 

125 if visible: 

126 self._parent.add_actor(self._mesh.actor) 

127 

128 self._mesh.set_exaggeration(self._state.exaggeration) 

129 self._mesh.set_opacity(self._state.opacity) 

130 self._mesh.set_smooth(self._state.smooth) 

131 self._mesh.set_lookuptable( 

132 self._lookuptables[self._state.cpt]) 

133 

134 self._parent.update_view() 

135 

136 def open_file_dialog(self): 

137 caption = 'Select a file to open' 

138 

139 fn, _ = qw.QFileDialog.getOpenFileName( 

140 self._parent, caption, options=common.qfiledialog_options) 

141 

142 if fn: 

143 self._state.path = str(fn) 

144 

145 def _get_controls(self): 

146 state = self._state 

147 if not self._controls: 

148 from ..state import state_bind_slider, state_bind_checkbox, \ 

149 state_bind_combobox 

150 

151 frame = qw.QFrame() 

152 layout = qw.QGridLayout() 

153 frame.setLayout(layout) 

154 

155 lab = qw.QLabel('Load from:') 

156 pb_file = qw.QPushButton('File') 

157 

158 layout.addWidget(lab, 0, 0) 

159 layout.addWidget(pb_file, 0, 1) 

160 

161 pb_file.clicked.connect(self.open_file_dialog) 

162 

163 # exaggeration 

164 

165 layout.addWidget(qw.QLabel('Exaggeration'), 1, 0) 

166 

167 slider = qw.QSlider(qc.Qt.Horizontal) 

168 slider.setSizePolicy( 

169 qw.QSizePolicy( 

170 qw.QSizePolicy.Expanding, qw.QSizePolicy.Fixed)) 

171 slider.setMinimum(0) 

172 slider.setMaximum(2000) 

173 layout.addWidget(slider, 1, 1) 

174 

175 state_bind_slider(self, state, 'exaggeration', slider, factor=0.01) 

176 

177 # opacity 

178 

179 layout.addWidget(qw.QLabel('Opacity'), 2, 0) 

180 

181 slider = qw.QSlider(qc.Qt.Horizontal) 

182 slider.setSizePolicy( 

183 qw.QSizePolicy( 

184 qw.QSizePolicy.Expanding, qw.QSizePolicy.Fixed)) 

185 slider.setMinimum(0) 

186 slider.setMaximum(1000) 

187 layout.addWidget(slider, 2, 1) 

188 

189 state_bind_slider(self, state, 'opacity', slider, factor=0.001) 

190 

191 cb = qw.QCheckBox('Smooth') 

192 layout.addWidget(cb, 3, 1) 

193 state_bind_checkbox(self, state, 'smooth', cb) 

194 

195 cb = common.string_choices_to_combobox(TopoCPTChoice) 

196 layout.addWidget(qw.QLabel('CPT'), 4, 0) 

197 layout.addWidget(cb, 4, 1) 

198 state_bind_combobox(self, state, 'cpt', cb) 

199 

200 layout.addWidget(qw.QFrame(), 6, 0, 1, 2) 

201 

202 self._controls = frame 

203 

204 return self._controls 

205 

206 

207__all__ = [ 

208 'CustomTopoElement', 

209 'CustomTopoState', 

210]