source: sasview/src/sas/sasgui/perspectives/file_converter/meta_panels.py @ 1aad14e

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalccostrafo411magnetic_scattrelease-4.1.1release-4.1.2release-4.2.2ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since 1aad14e was 1aad14e, checked in by lewis, 8 years ago

Complete adding sample metadata window

  • Property mode set to 100644
File size: 12.0 KB
Line 
1import wx
2import sys
3from sas.sasgui.perspectives.calculator import calculator_widgets as widget
4from sas.sasgui.perspectives.file_converter.converter_widgets import VectorInput
5from wx.lib.scrolledpanel import ScrolledPanel
6from sas.sasgui.guiframe.panel_base import PanelBase
7from sas.sascalc.dataloader.data_info import Detector
8from sas.sasgui.guiframe.events import StatusEvent
9from sas.sasgui.guiframe.utils import check_float
10
11if sys.platform.count("win32") > 0:
12    PANEL_TOP = 0
13    _STATICBOX_WIDTH = 350
14    PANEL_SIZE = 440
15    FONT_VARIANT = 0
16else:
17    PANEL_TOP = 60
18    _STATICBOX_WIDTH = 380
19    PANEL_SIZE = 470
20    FONT_VARIANT = 1
21
22class MetadataPanel(ScrolledPanel, PanelBase):
23
24    def __init__(self, parent, metadata, base=None, *args, **kwargs):
25        ScrolledPanel.__init__(self, parent, *args, **kwargs)
26        PanelBase.__init__(self)
27        self.SetupScrolling()
28        self.SetWindowVariant(variant=FONT_VARIANT)
29
30        self.base = base
31        self.parent = parent
32        self._to_validate = []
33        self._vectors = []
34        self.metadata = metadata
35
36    def get_property_string(self, name, is_float=False):
37        value = getattr(self.metadata, name)
38        if value is None or value == []:
39            value = ''
40            is_float = False
41        value = str(value)
42        if is_float and not '.' in value: value += '.0'
43        return value
44
45    def on_change(self, event):
46        ctrl = event.GetEventObject()
47        value = ctrl.GetValue()
48        name = ctrl.GetName()
49        old_value = getattr(self.metadata, name)
50        if value == '': value = None
51        if isinstance(old_value, list): value = [value]
52
53        setattr(self.metadata, name, value)
54
55    def on_close(self, event=None):
56        for ctrl in self._to_validate:
57            ctrl.SetBackgroundColour(wx.WHITE)
58            if ctrl.GetValue() == '': continue
59            if not check_float(ctrl):
60                msg = "{} must be a valid float".format(
61                    ctrl.GetName().replace("_", " "))
62                wx.PostEvent(self.parent.manager.parent.manager.parent,
63                    StatusEvent(status=msg, info='error'))
64                return False
65        for vector_in in self._vectors:
66            is_valid, invalid_ctrl = vector_in.Validate()
67            if not is_valid:
68                msg = "{} must be a valid float".format(
69                    invalid_ctrl.GetName().replace("_", " "))
70                wx.PostEvent(self.parent.manager.parent.manager.parent,
71                    StatusEvent(status=msg, info='error'))
72                return False
73            setattr(self.metadata, vector_in.GetName(), vector_in.GetValue())
74        return True
75
76class DetectorPanel(MetadataPanel):
77
78    def __init__(self, parent, detector, base=None, *args, **kwargs):
79        if detector.name is None:
80            detector.name = ''
81
82        MetadataPanel.__init__(self, parent, detector, base, *args, **kwargs)
83
84        self._do_layout()
85        self.SetAutoLayout(True)
86        self.Layout()
87
88    def on_close(self, event=None):
89        if not MetadataPanel.on_close(self, event):
90            return
91
92        self.parent.manager.metadata['detector'] = [self.metadata]
93        self.parent.on_close(event)
94
95    def _do_layout(self):
96        vbox = wx.BoxSizer(wx.VERTICAL)
97
98        section = wx.StaticBox(self, -1, "Detector")
99        section_sizer = wx.StaticBoxSizer(section, wx.VERTICAL)
100        section_sizer.SetMinSize((_STATICBOX_WIDTH, -1))
101
102        input_grid = wx.GridBagSizer(5, 5)
103
104        y = 0
105        name_label = wx.StaticText(self, -1, "Name: ")
106        input_grid.Add(name_label, (y,0), (1,1), wx.ALL, 5)
107        name_input = wx.TextCtrl(self, -1, name="name")
108        input_grid.Add(name_input, (y,1), (1,1))
109        name_input.Bind(wx.EVT_TEXT, self.on_change)
110        y += 1
111
112        distance_label = wx.StaticText(self, -1,
113            "Distance (mm): ")
114        input_grid.Add(distance_label, (y, 0), (1,1), wx.ALL, 5)
115        distance_input = wx.TextCtrl(self, -1,
116            name="distance", size=(50,-1))
117        input_grid.Add(distance_input, (y,1), (1,1))
118        distance_input.Bind(wx.EVT_TEXT, self.on_change)
119        self._to_validate.append(distance_input)
120        y += 1
121
122        offset_label = wx.StaticText(self, -1, "Offset (mm): ")
123        input_grid.Add(offset_label, (y,0), (1,1), wx.ALL, 5)
124        offset_input = VectorInput(self, "offset")
125        input_grid.Add(offset_input.GetSizer(), (y,1), (1,1))
126        self._vectors.append(offset_input)
127        y += 1
128
129        orientation_label = wx.StaticText(self, -1, "Orientation (\xb0): ")
130        input_grid.Add(orientation_label, (y,0), (1,1), wx.ALL, 5)
131        orientation_input = VectorInput(self, "orientation", z_enabled=True,
132            labels=["Roll: ", "Pitch: ", "Yaw: "])
133        input_grid.Add(orientation_input.GetSizer(), (y,1), (1,1))
134        self._vectors.append(orientation_input)
135        y += 1
136
137        pixel_label = wx.StaticText(self, -1, "Pixel Size (mm): ")
138        input_grid.Add(pixel_label, (y,0), (1,1), wx.ALL, 5)
139        pixel_input = VectorInput(self, "pixel_size")
140        input_grid.Add(pixel_input.GetSizer(), (y,1), (1,1))
141        self._vectors.append(pixel_input)
142        y += 1
143
144        beam_label = wx.StaticText(self, -1, "Beam Center (mm): ")
145        input_grid.Add(beam_label, (y,0), (1,1), wx.ALL, 5)
146        beam_input = VectorInput(self, "beam_center")
147        input_grid.Add(beam_input.GetSizer(), (y,1), (1,1))
148        self._vectors.append(beam_input)
149        y += 1
150
151        slit_label = wx.StaticText(self, -1, "Slit Length (mm): ")
152        input_grid.Add(slit_label, (y,0), (1,1), wx.ALL, 5)
153        slit_input = wx.TextCtrl(self, -1, name="slit_length", size=(50,-1))
154        input_grid.Add(slit_input, (y,1), (1,1))
155        slit_input.Bind(wx.EVT_TEXT, self.on_change)
156        self._to_validate.append(slit_input)
157        y += 1
158
159        done_btn = wx.Button(self, -1, "Done")
160        input_grid.Add(done_btn, (y,0), (1,1), wx.ALL, 5)
161        done_btn.Bind(wx.EVT_BUTTON, self.on_close)
162
163        section_sizer.Add(input_grid)
164        vbox.Add(section_sizer, flag=wx.ALL, border=10)
165
166        name_input.SetValue(self.metadata.name)
167        distance = self.get_property_string("distance", is_float=True)
168        distance_input.SetValue(distance)
169        offset_input.SetValue(self.metadata.offset)
170        orientation_input.SetValue(self.metadata.orientation)
171        pixel_input.SetValue(self.metadata.pixel_size)
172        beam_input.SetValue(self.metadata.beam_center)
173        slit_len = self.get_property_string("slit_length", is_float=True)
174        slit_input.SetValue(slit_len)
175
176        vbox.Fit(self)
177        self.SetSizer(vbox)
178
179class SamplePanel(MetadataPanel):
180
181    def __init__(self, parent, sample, base=None, *args, **kwargs):
182        MetadataPanel.__init__(self, parent, sample, base, *args, **kwargs)
183        if sample.name is None:
184            sample.name = ''
185
186        self._do_layout()
187        self.SetAutoLayout(True)
188        self.Layout()
189
190    def on_close(self, event=None):
191        if not MetadataPanel.on_close(self, event):
192            return
193
194        self.parent.manager.metadata['sample'] = self.metadata
195        self.parent.on_close(event)
196
197    def _do_layout(self):
198        vbox = wx.BoxSizer(wx.VERTICAL)
199
200        section = wx.StaticBox(self, -1, "Sample")
201        section_sizer = wx.StaticBoxSizer(section, wx.VERTICAL)
202        section_sizer.SetMinSize((_STATICBOX_WIDTH, -1))
203
204        input_grid = wx.GridBagSizer(5, 5)
205
206        y = 0
207        name_label = wx.StaticText(self, -1, "Name: ")
208        input_grid.Add(name_label, (y,0), (1,1), wx.ALL, 5)
209        name_input = wx.TextCtrl(self, -1, name="name")
210        input_grid.Add(name_input, (y,1), (1,1))
211        name_input.Bind(wx.EVT_TEXT, self.on_change)
212        y += 1
213
214        id_label = wx.StaticText(self, -1, "ID: ")
215        input_grid.Add(id_label, (y,0), (1,1), wx.ALL, 5)
216        id_input = wx.TextCtrl(self, -1, name="ID")
217        input_grid.Add(id_input, (y,1), (1,1))
218        id_input.Bind(wx.EVT_TEXT, self.on_change)
219        y += 1
220
221        thickness_label = wx.StaticText(self, -1, "Thickness (mm): ")
222        input_grid.Add(thickness_label, (y,0), (1,1), wx.ALL, 5)
223        thickness_input = wx.TextCtrl(self, -1, name="thickness")
224        input_grid.Add(thickness_input, (y,1), (1,1))
225        thickness_input.Bind(wx.EVT_TEXT, self.on_change)
226        self._to_validate.append(thickness_input)
227        y += 1
228
229        transmission_label = wx.StaticText(self, -1, "Transmission: ")
230        input_grid.Add(transmission_label, (y,0), (1,1), wx.ALL, 5)
231        transmission_input = wx.TextCtrl(self, -1, name="transmission")
232        input_grid.Add(transmission_input, (y,1), (1,1))
233        transmission_input.Bind(wx.EVT_TEXT, self.on_change)
234        self._to_validate.append(transmission_input)
235        y += 1
236
237        temperature_label = wx.StaticText(self, -1, "Temperature: ")
238        input_grid.Add(temperature_label, (y,0), (1,1), wx.ALL, 5)
239        temperature_input = wx.TextCtrl(self, -1, name="temperature")
240        temperature_input.Bind(wx.EVT_TEXT, self.on_change)
241        self._to_validate.append(temperature_input)
242        input_grid.Add(temperature_input, (y,1), (1,1))
243        temp_unit_label = wx.StaticText(self, -1, "Unit: ")
244        input_grid.Add(temp_unit_label, (y,2), (1,1))
245        temp_unit_input = wx.TextCtrl(self, -1, name="temperature_unit",
246            size=(50,-1))
247        temp_unit_input.Bind(wx.EVT_TEXT, self.on_change)
248        input_grid.Add(temp_unit_input, (y,3), (1,1))
249        y += 1
250
251        position_label = wx.StaticText(self, -1, "Position (mm): ")
252        input_grid.Add(position_label, (y,0), (1,1), wx.ALL, 5)
253        position_input = VectorInput(self, "position")
254        self._vectors.append(position_input)
255        input_grid.Add(position_input.GetSizer(), (y,1), (1,2))
256        y += 1
257
258        orientation_label = wx.StaticText(self, -1, "Orientation (\xb0): ")
259        input_grid.Add(orientation_label, (y,0), (1,1), wx.ALL, 5)
260        orientation_input = VectorInput(self, "orientation",
261            labels=["Roll: ", "Pitch: ", "Yaw: "], z_enabled=True)
262        self._vectors.append(orientation_input)
263        input_grid.Add(orientation_input.GetSizer(), (y,1), (1,3))
264        y += 1
265
266        details_label = wx.StaticText(self, -1, "Details: ")
267        input_grid.Add(details_label, (y,0), (1,1), wx.ALL, 5)
268        details_input = wx.TextCtrl(self, -1, name="details",
269            style=wx.TE_MULTILINE)
270        input_grid.Add(details_input, (y,1), (3,3), wx.EXPAND)
271        y += 3
272
273        name_input.SetValue(self.metadata.name)
274        id_input.SetValue(self.get_property_string("ID"))
275        thickness_input.SetValue(
276            self.get_property_string("thickness", is_float=True))
277        transmission_input.SetValue(
278            self.get_property_string("transmission", is_float=True))
279        temperature_input.SetValue(
280            self.get_property_string("temperature", is_float=True))
281        temp_unit_input.SetValue(self.get_property_string("temperature_unit"))
282        position_input.SetValue(self.metadata.position)
283        orientation_input.SetValue(self.metadata.orientation)
284        details_input.SetValue(self.get_property_string("details"))
285        details_input.Bind(wx.EVT_TEXT, self.on_change)
286
287        done_btn = wx.Button(self, -1, "Done")
288        input_grid.Add(done_btn, (y,0), (1,1), wx.ALL, 5)
289        done_btn.Bind(wx.EVT_BUTTON, self.on_close)
290
291        section_sizer.Add(input_grid)
292        vbox.Add(section_sizer, flag=wx.ALL, border=10)
293
294        vbox.Fit(self)
295        self.SetSizer(vbox)
296
297class MetadataWindow(widget.CHILD_FRAME):
298
299    def __init__(self, PanelClass, parent=None, title='', base=None,
300        manager=None, size=(PANEL_SIZE, PANEL_SIZE*0.8), metadata=None,
301         *args, **kwargs):
302        kwargs['title'] = title
303        kwargs['size'] = size
304        widget.CHILD_FRAME.__init__(self, parent, *args, **kwargs)
305
306        self.manager = manager
307        self.panel = PanelClass(self, metadata, base=None)
308        self.Bind(wx.EVT_CLOSE, self.on_close)
309
310    def on_close(self, event):
311        if self.manager is not None:
312            self.manager.meta_frames.remove(self)
313        self.Destroy()
Note: See TracBrowser for help on using the repository browser.