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

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 de0df2c was de0df2c, checked in by lewis, 8 years ago

Abstract MetadataPanel? from DetectorPanel?

  • Property mode set to 100644
File size: 6.8 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 on_change(self, event):
37        ctrl = event.GetEventObject()
38        value = ctrl.GetValue()
39        if value == '': value = None
40        setattr(self.metadata, ctrl.GetName(), value)
41
42    def on_close(self, event=None):
43        for ctrl in self._to_validate:
44            ctrl.SetBackgroundColour(wx.WHITE)
45            if ctrl.GetValue() == '': continue
46            if not check_float(ctrl):
47                msg = "{} must be a valid float".format(
48                    ctrl.GetName().replace("_", " "))
49                wx.PostEvent(self.parent.manager.parent.manager.parent,
50                    StatusEvent(status=msg, info='error'))
51                return
52        for vector_in in self._vectors:
53            is_valid, invalid_ctrl = vector_in.Validate()
54            if not is_valid:
55                msg = "{} must be a valid float".format(
56                    invalid_ctrl.GetName().replace("_", " "))
57                wx.PostEvent(self.parent.manager.parent.manager.parent,
58                    StatusEvent(status=msg, info='error'))
59                return
60            setattr(self.metadata, vector_in.GetName(), vector_in.GetValue())
61
62class DetectorPanel(MetadataPanel):
63
64    def __init__(self, parent, detector, base=None, *args, **kwargs):
65        MetadataPanel.__init__(self, parent, detector, base, *args, **kwargs)
66
67        if detector.name is None:
68            detector.name = ''
69
70        self._do_layout()
71        self.SetAutoLayout(True)
72        self.Layout()
73
74    def on_close(self, event=None):
75        MetadataPanel.on_close(self, event)
76
77        self.parent.manager.metadata['detector'] = [self.metadata]
78        self.parent.on_close(event)
79
80    def _do_layout(self):
81        vbox = wx.BoxSizer(wx.VERTICAL)
82
83        section = wx.StaticBox(self, -1, "Detector")
84        section_sizer = wx.StaticBoxSizer(section, wx.VERTICAL)
85        section_sizer.SetMinSize((_STATICBOX_WIDTH, -1))
86
87        input_grid = wx.GridBagSizer(5, 5)
88
89        y = 0
90        name_label = wx.StaticText(self, -1, "Name: ")
91        input_grid.Add(name_label, (y,0), (1,1), wx.ALL, 5)
92        name_input = wx.TextCtrl(self, -1, name="name")
93        input_grid.Add(name_input, (y,1), (1,1))
94        name_input.Bind(wx.EVT_TEXT, self.on_change)
95        y += 1
96
97        distance_label = wx.StaticText(self, -1,
98            "Distance (mm): ")
99        input_grid.Add(distance_label, (y, 0), (1,1), wx.ALL, 5)
100        distance_input = wx.TextCtrl(self, -1,
101            name="distance", size=(50,-1))
102        input_grid.Add(distance_input, (y,1), (1,1))
103        distance_input.Bind(wx.EVT_TEXT, self.on_change)
104        self._to_validate.append(distance_input)
105        y += 1
106
107        offset_label = wx.StaticText(self, -1, "Offset (mm): ")
108        input_grid.Add(offset_label, (y,0), (1,1), wx.ALL, 5)
109        offset_input = VectorInput(self, "offset")
110        input_grid.Add(offset_input.GetSizer(), (y,1), (1,1))
111        self._vectors.append(offset_input)
112        y += 1
113
114        orientation_label = wx.StaticText(self, -1, "Orientation (\xb0): ")
115        input_grid.Add(orientation_label, (y,0), (1,1), wx.ALL, 5)
116        orientation_input = VectorInput(self, "orientation", z_enabled=True,
117            labels=["Roll: ", "Pitch: ", "Yaw: "])
118        input_grid.Add(orientation_input.GetSizer(), (y,1), (1,1))
119        self._vectors.append(orientation_input)
120        y += 1
121
122        pixel_label = wx.StaticText(self, -1, "Pixel Size (mm): ")
123        input_grid.Add(pixel_label, (y,0), (1,1), wx.ALL, 5)
124        pixel_input = VectorInput(self, "pixel_size")
125        input_grid.Add(pixel_input.GetSizer(), (y,1), (1,1))
126        self._vectors.append(pixel_input)
127        y += 1
128
129        beam_label = wx.StaticText(self, -1, "Beam Center (mm): ")
130        input_grid.Add(beam_label, (y,0), (1,1), wx.ALL, 5)
131        beam_input = VectorInput(self, "beam_center")
132        input_grid.Add(beam_input.GetSizer(), (y,1), (1,1))
133        self._vectors.append(beam_input)
134        y += 1
135
136        slit_label = wx.StaticText(self, -1, "Slit Length (mm): ")
137        input_grid.Add(slit_label, (y,0), (1,1), wx.ALL, 5)
138        slit_input = wx.TextCtrl(self, -1, name="slit_length", size=(50,-1))
139        input_grid.Add(slit_input, (y,1), (1,1))
140        slit_input.Bind(wx.EVT_TEXT, self.on_change)
141        self._to_validate.append(slit_input)
142        y += 1
143
144        done_btn = wx.Button(self, -1, "Done")
145        input_grid.Add(done_btn, (y,0), (1,1), wx.ALL, 5)
146        done_btn.Bind(wx.EVT_BUTTON, self.on_close)
147
148        section_sizer.Add(input_grid)
149        vbox.Add(section_sizer, flag=wx.ALL, border=10)
150
151        name_input.SetValue(self.metadata.name)
152        distance = self.metadata.distance
153        if distance is None: distance = ''
154        elif '.' not in distance: distance += '.0'
155        distance_input.SetValue(str(distance))
156        offset_input.SetValue(self.metadata.offset)
157        orientation_input.SetValue(self.metadata.orientation)
158        pixel_input.SetValue(self.metadata.pixel_size)
159        beam_input.SetValue(self.metadata.beam_center)
160        slit_len = self.metadata.slit_length
161        if slit_len is None: slit_len = ''
162        elif '.' not in slit_len: slit_len += '.0'
163        slit_input.SetValue(slit_len)
164
165        vbox.Fit(self)
166        self.SetSizer(vbox)
167
168class MetadataWindow(widget.CHILD_FRAME):
169
170    def __init__(self, PanelClass, parent=None, title='', base=None,
171        manager=None, size=(PANEL_SIZE, PANEL_SIZE*0.8), metadata=None,
172         *args, **kwargs):
173        kwargs['title'] = title
174        kwargs['size'] = size
175        widget.CHILD_FRAME.__init__(self, parent, *args, **kwargs)
176
177        self.manager = manager
178        self.panel = PanelClass(self, metadata, base=None)
179        self.Bind(wx.EVT_CLOSE, self.on_close)
180
181    def on_close(self, event):
182        if self.manager is not None:
183            self.manager.meta_frames.remove(self)
184        self.Destroy()
Note: See TracBrowser for help on using the repository browser.