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 2023-10-04 09:52 +0000
« prev ^ index » next coverage.py v6.5.0, created at 2023-10-04 09:52 +0000
1# https://pyrocko.org - GPLv3
2#
3# The Pyrocko Developers, 21st Century
4# ---|P------/S----------~Lg----------
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
12from pyrocko.gui.vtk_util import cpt_to_vtk_lookuptable
14from .base import Element
15from .topo import TopoMeshPipe, TopoCPTChoice, TopoState
17from .. import common
19guts_prefix = 'sparrow'
22class CustomTopoState(TopoState):
23 path = String.T(optional=True)
25 def create(self):
26 element = CustomTopoElement()
27 return element
30class CustomTopoElement(Element):
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
41 def get_name(self):
42 return 'Custom Topography'
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)
51 def set_parent(self, parent):
52 self._parent = parent
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()])
62 self.update()
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)
70 self._mesh = None
72 if self._controls:
73 self._parent.remove_panel(self._controls)
74 self._controls = None
76 self._parent.update_view()
77 self._parent = None
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'
85 elif cpt_name == 'uniform':
86 topo_cpt_wet = 'light_sea_uniform'
87 topo_cpt_dry = 'light_land_uniform'
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)
93 lut_combi = cpt_to_vtk_lookuptable(cpt_combi)
94 lut_combi.SetNanColor(0.0, 0.0, 0.0, 0.0)
96 self._lookuptables[cpt_name] = lut_combi
98 def update(self, *args):
99 visible = self._state.visible
101 self.update_cpt(self._state.cpt)
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:
107 if self._mesh:
108 self._parent.remove_actor(self._mesh.actor)
109 self._mesh = None
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])
119 self._parent.add_actor(self._mesh.actor)
121 if not visible and self._mesh:
122 self._parent.remove_actor(self._mesh.actor)
124 if self._mesh:
125 if visible:
126 self._parent.add_actor(self._mesh.actor)
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])
134 self._parent.update_view()
136 def open_file_dialog(self):
137 caption = 'Select a file to open'
139 fn, _ = qw.QFileDialog.getOpenFileName(
140 self._parent, caption, options=common.qfiledialog_options)
142 if fn:
143 self._state.path = str(fn)
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
151 frame = qw.QFrame()
152 layout = qw.QGridLayout()
153 frame.setLayout(layout)
155 lab = qw.QLabel('Load from:')
156 pb_file = qw.QPushButton('File')
158 layout.addWidget(lab, 0, 0)
159 layout.addWidget(pb_file, 0, 1)
161 pb_file.clicked.connect(self.open_file_dialog)
163 # exaggeration
165 layout.addWidget(qw.QLabel('Exaggeration'), 1, 0)
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)
175 state_bind_slider(self, state, 'exaggeration', slider, factor=0.01)
177 # opacity
179 layout.addWidget(qw.QLabel('Opacity'), 2, 0)
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)
189 state_bind_slider(self, state, 'opacity', slider, factor=0.001)
191 cb = qw.QCheckBox('Smooth')
192 layout.addWidget(cb, 3, 1)
193 state_bind_checkbox(self, state, 'smooth', cb)
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)
200 layout.addWidget(qw.QFrame(), 6, 0, 1, 2)
202 self._controls = frame
204 return self._controls
207__all__ = [
208 'CustomTopoElement',
209 'CustomTopoState',
210]