[af84162] | 1 | import wx |
---|
| 2 | import sys |
---|
| 3 | from sas.sasgui.perspectives.calculator import calculator_widgets as widget |
---|
| 4 | from sas.sasgui.perspectives.file_converter.converter_widgets import VectorInput |
---|
| 5 | from wx.lib.scrolledpanel import ScrolledPanel |
---|
| 6 | from sas.sasgui.guiframe.panel_base import PanelBase |
---|
| 7 | from sas.sascalc.dataloader.data_info import Detector |
---|
| 8 | from sas.sasgui.guiframe.events import StatusEvent |
---|
| 9 | from sas.sasgui.guiframe.utils import check_float |
---|
| 10 | |
---|
| 11 | if sys.platform.count("win32") > 0: |
---|
| 12 | PANEL_TOP = 0 |
---|
| 13 | _STATICBOX_WIDTH = 350 |
---|
| 14 | PANEL_SIZE = 440 |
---|
| 15 | FONT_VARIANT = 0 |
---|
| 16 | else: |
---|
| 17 | PANEL_TOP = 60 |
---|
| 18 | _STATICBOX_WIDTH = 380 |
---|
| 19 | PANEL_SIZE = 470 |
---|
| 20 | FONT_VARIANT = 1 |
---|
| 21 | |
---|
| 22 | class DetectorPanel(ScrolledPanel, PanelBase): |
---|
| 23 | |
---|
| 24 | def __init__(self, parent, detector, 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 | if detector.name is None: |
---|
| 31 | detector.name = '' |
---|
| 32 | |
---|
| 33 | self.base = base |
---|
| 34 | self.parent = parent |
---|
| 35 | self.detector = detector |
---|
| 36 | self._to_validate = [] |
---|
| 37 | self._vectors = [] |
---|
| 38 | |
---|
| 39 | self._do_layout() |
---|
| 40 | self.SetAutoLayout(True) |
---|
| 41 | self.Layout() |
---|
| 42 | |
---|
| 43 | def on_change(self, event): |
---|
| 44 | ctrl = event.GetEventObject() |
---|
| 45 | value = ctrl.GetValue() |
---|
| 46 | if value == '': value = None |
---|
| 47 | setattr(self.detector, ctrl.GetName(), value) |
---|
| 48 | |
---|
| 49 | def on_close(self, event=None): |
---|
| 50 | for ctrl in self._to_validate: |
---|
| 51 | ctrl.SetBackgroundColour(wx.WHITE) |
---|
| 52 | if ctrl.GetValue() == '': continue |
---|
| 53 | if not check_float(ctrl): |
---|
| 54 | msg = "{} must be a valid float".format( |
---|
| 55 | ctrl.GetName().replace("_", " ")) |
---|
| 56 | wx.PostEvent(self.parent.manager.parent.manager.parent, |
---|
| 57 | StatusEvent(status=msg, info='error')) |
---|
| 58 | return |
---|
| 59 | for vector_in in self._vectors: |
---|
| 60 | is_valid, invalid_ctrl = vector_in.Validate() |
---|
| 61 | if not is_valid: |
---|
| 62 | msg = "{} must be a valid float".format( |
---|
| 63 | invalid_ctrl.GetName().replace("_", " ")) |
---|
| 64 | wx.PostEvent(self.parent.manager.parent.manager.parent, |
---|
| 65 | StatusEvent(status=msg, info='error')) |
---|
| 66 | return |
---|
| 67 | setattr(self.detector, vector_in.GetName(), vector_in.GetValue()) |
---|
| 68 | |
---|
| 69 | self.parent.on_close(event) |
---|
| 70 | |
---|
| 71 | def _do_layout(self): |
---|
| 72 | vbox = wx.BoxSizer(wx.VERTICAL) |
---|
| 73 | |
---|
| 74 | section = wx.StaticBox(self, -1, "Detector") |
---|
| 75 | section_sizer = wx.StaticBoxSizer(section, wx.VERTICAL) |
---|
| 76 | section_sizer.SetMinSize((_STATICBOX_WIDTH, -1)) |
---|
| 77 | |
---|
| 78 | input_grid = wx.GridBagSizer(5, 5) |
---|
| 79 | |
---|
| 80 | y = 0 |
---|
| 81 | name_label = wx.StaticText(self, -1, "Name: ") |
---|
| 82 | input_grid.Add(name_label, (y,0), (1,1), wx.ALL, 5) |
---|
| 83 | name_input = wx.TextCtrl(self, -1, name="name") |
---|
| 84 | input_grid.Add(name_input, (y,1), (1,1)) |
---|
| 85 | name_input.Bind(wx.EVT_TEXT, self.on_change) |
---|
| 86 | y += 1 |
---|
| 87 | |
---|
| 88 | distance_label = wx.StaticText(self, -1, |
---|
| 89 | "Distance (mm): ") |
---|
| 90 | input_grid.Add(distance_label, (y, 0), (1,1), wx.ALL, 5) |
---|
| 91 | distance_input = wx.TextCtrl(self, -1, |
---|
| 92 | name="distance", size=(50,-1)) |
---|
| 93 | input_grid.Add(distance_input, (y,1), (1,1)) |
---|
| 94 | distance_input.Bind(wx.EVT_TEXT, self.on_change) |
---|
| 95 | self._to_validate.append(distance_input) |
---|
| 96 | y += 1 |
---|
| 97 | |
---|
| 98 | offset_label = wx.StaticText(self, -1, "Offset (mm): ") |
---|
| 99 | input_grid.Add(offset_label, (y,0), (1,1), wx.ALL, 5) |
---|
| 100 | offset_input = VectorInput(self, "offset") |
---|
| 101 | input_grid.Add(offset_input.GetSizer(), (y,1), (1,1)) |
---|
| 102 | self._vectors.append(offset_input) |
---|
| 103 | y += 1 |
---|
| 104 | |
---|
| 105 | orientation_label = wx.StaticText(self, -1, "Orientation (\xb0): ") |
---|
| 106 | input_grid.Add(orientation_label, (y,0), (1,1), wx.ALL, 5) |
---|
| 107 | orientation_input = VectorInput(self, "orientation", z_enabled=True, |
---|
| 108 | labels=["Roll: ", "Pitch: ", "Yaw: "]) |
---|
| 109 | input_grid.Add(orientation_input.GetSizer(), (y,1), (1,1)) |
---|
| 110 | self._vectors.append(orientation_input) |
---|
| 111 | y += 1 |
---|
| 112 | |
---|
| 113 | pixel_label = wx.StaticText(self, -1, "Pixel Size (mm): ") |
---|
| 114 | input_grid.Add(pixel_label, (y,0), (1,1), wx.ALL, 5) |
---|
| 115 | pixel_input = VectorInput(self, "pixel_size") |
---|
| 116 | input_grid.Add(pixel_input.GetSizer(), (y,1), (1,1)) |
---|
| 117 | self._vectors.append(pixel_input) |
---|
| 118 | y += 1 |
---|
| 119 | |
---|
| 120 | beam_label = wx.StaticText(self, -1, "Beam Center (mm): ") |
---|
| 121 | input_grid.Add(beam_label, (y,0), (1,1), wx.ALL, 5) |
---|
| 122 | beam_input = VectorInput(self, "beam_center") |
---|
| 123 | input_grid.Add(beam_input.GetSizer(), (y,1), (1,1)) |
---|
| 124 | self._vectors.append(beam_input) |
---|
| 125 | y += 1 |
---|
| 126 | |
---|
| 127 | slit_label = wx.StaticText(self, -1, "Slit Length (mm): ") |
---|
| 128 | input_grid.Add(slit_label, (y,0), (1,1), wx.ALL, 5) |
---|
| 129 | slit_input = wx.TextCtrl(self, -1, name="slit_length", size=(50,-1)) |
---|
| 130 | input_grid.Add(slit_input, (y,1), (1,1)) |
---|
| 131 | slit_input.Bind(wx.EVT_TEXT, self.on_change) |
---|
| 132 | self._to_validate.append(slit_input) |
---|
| 133 | y += 1 |
---|
| 134 | |
---|
| 135 | done_btn = wx.Button(self, -1, "Done") |
---|
| 136 | input_grid.Add(done_btn, (y,0), (1,1), wx.ALL, 5) |
---|
| 137 | done_btn.Bind(wx.EVT_BUTTON, self.on_close) |
---|
| 138 | |
---|
| 139 | section_sizer.Add(input_grid) |
---|
| 140 | vbox.Add(section_sizer, flag=wx.ALL, border=10) |
---|
| 141 | |
---|
| 142 | name_input.SetValue(self.detector.name) |
---|
| 143 | distance = self.detector.distance |
---|
| 144 | if distance is None: distance = '' |
---|
| 145 | elif '.' not in distance: distance += '.0' |
---|
| 146 | distance_input.SetValue(str(distance)) |
---|
| 147 | offset_input.SetValue(self.detector.offset) |
---|
| 148 | orientation_input.SetValue(self.detector.orientation) |
---|
| 149 | pixel_input.SetValue(self.detector.pixel_size) |
---|
| 150 | beam_input.SetValue(self.detector.beam_center) |
---|
| 151 | slit_len = self.detector.slit_length |
---|
| 152 | if slit_len is None: slit_len = '' |
---|
| 153 | elif '.' not in slit_len: slit_len += '.0' |
---|
| 154 | slit_input.SetValue(slit_len) |
---|
| 155 | |
---|
| 156 | vbox.Fit(self) |
---|
| 157 | self.SetSizer(vbox) |
---|
| 158 | |
---|
| 159 | class DetectorWindow(widget.CHILD_FRAME): |
---|
| 160 | |
---|
| 161 | def __init__(self, parent=None, title='Detector Metadata', base=None, |
---|
| 162 | manager=None, size=(PANEL_SIZE, PANEL_SIZE*0.8), detector=None, |
---|
| 163 | *args, **kwargs): |
---|
| 164 | kwargs['title'] = title |
---|
| 165 | kwargs['size'] = size |
---|
| 166 | widget.CHILD_FRAME.__init__(self, parent, *args, **kwargs) |
---|
| 167 | |
---|
| 168 | self.manager = manager |
---|
| 169 | self.panel = DetectorPanel(self, detector, base=None) |
---|
| 170 | self.Bind(wx.EVT_CLOSE, self.on_close) |
---|
| 171 | |
---|
| 172 | def on_close(self, event): |
---|
| 173 | if self.manager is not None: |
---|
| 174 | self.manager.detector_frame = None |
---|
| 175 | self.manager.metadata['detector'] = [self.panel.detector] |
---|
| 176 | self.Destroy() |
---|