source: sasview/src/sas/sasgui/perspectives/file_converter/converter_widgets.py @ dc8a553

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

Add docstrings & comments, and rename old bsl_loader to otoko_loader

  • Property mode set to 100644
File size: 4.1 KB
Line 
1"""
2This module provides some custom wx widgets for the file converter perspective
3"""
4import wx
5from sas.sascalc.dataloader.data_info import Vector
6from sas.sasgui.guiframe.utils import check_float
7
8class VectorInput(object):
9    """
10    An input field for inputting 2 (or 3) components of a vector.
11    """
12
13    def __init__(self, parent, control_name, callback=None,
14        labels=["x: ", "y: ", "z: "], z_enabled=False):
15        """
16        Create the control
17
18        :param parent: The window to add the control to
19        :param control_name: All TextCtrl names will start with control_name
20        :param callback: The function to call when the text is changed
21        :param labels: An array of labels for the TextCtrls
22        :param z_enabled: Whether or not to show the z input field
23        """
24        self.parent = parent
25        self.control_name = control_name
26        self._callback = callback
27        self._name = control_name
28
29        self.labels = labels
30        self.z_enabled = z_enabled
31        self._sizer = None
32        self._inputs = {}
33
34        self._do_layout()
35
36    def GetSizer(self):
37        """
38        Get the control's sizer
39
40        :return sizer: a wx.BoxSizer object
41        """
42        return self._sizer
43
44    def GetName(self):
45        return self._name
46
47    def GetValue(self):
48        """
49        Get the value of the vector input
50
51        :return v: A Vector object
52        """
53        v = Vector()
54        if not self.Validate(): return v
55        for direction, control in self._inputs.iteritems():
56            try:
57                value = float(control.GetValue())
58                setattr(v, direction, value)
59            except: # Text field is empty
60                pass
61
62        return v
63
64    def SetValue(self, vector):
65        """
66        Set the value of the vector input
67
68        :param vector: A Vector object
69        """
70        directions = ['x', 'y']
71        if self.z_enabled: directions.append('z')
72        for direction in directions:
73            value = getattr(vector, direction)
74            if value is None: value = ''
75            self._inputs[direction].SetValue(str(value))
76
77    def Validate(self):
78        """
79        Validate the contents of the inputs
80
81        :return all_valid: Whether or not the inputs are valid
82        :return invalid_ctrl: A control that is not valid
83            (or None if all are valid)
84        """
85        all_valid = True
86        invalid_ctrl = None
87        for control in self._inputs.values():
88            if control.GetValue() == '': continue
89            control.SetBackgroundColour(wx.WHITE)
90            control_valid = check_float(control)
91            if not control_valid:
92                all_valid = False
93                invalid_ctrl = control
94        return all_valid, invalid_ctrl
95
96
97    def _do_layout(self):
98        self._sizer = wx.BoxSizer(wx.HORIZONTAL)
99        x_label = wx.StaticText(self.parent, -1, self.labels[0],
100            style=wx.ALIGN_CENTER_VERTICAL)
101        self._sizer.Add(x_label, wx.ALIGN_CENTER_VERTICAL)
102        x_input = wx.TextCtrl(self.parent, -1,
103            name="{}_x".format(self.control_name), size=(50, -1))
104        self._sizer.Add(x_input)
105        self._inputs['x'] = x_input
106        x_input.Bind(wx.EVT_TEXT, self._callback)
107
108        self._sizer.AddSpacer((10, -1))
109
110        y_label = wx.StaticText(self.parent, -1, self.labels[1],
111            style=wx.ALIGN_CENTER_VERTICAL)
112        self._sizer.Add(y_label, wx.ALIGN_CENTER_VERTICAL)
113        y_input = wx.TextCtrl(self.parent, -1,
114            name="{}_y".format(self.control_name), size=(50, -1))
115        self._sizer.Add(y_input)
116        self._inputs['y'] = y_input
117        y_input.Bind(wx.EVT_TEXT, self._callback)
118
119        if self.z_enabled:
120            self._sizer.AddSpacer((10, -1))
121
122            z_label = wx.StaticText(self.parent, -1, self.labels[2],
123                style=wx.ALIGN_CENTER_VERTICAL)
124            self._sizer.Add(z_label, wx.ALIGN_CENTER_VERTICAL)
125            z_input = wx.TextCtrl(self.parent, -1,
126                name="{}_z".format(self.control_name), size=(50, -1))
127            self._sizer.Add(z_input)
128            self._inputs['z'] = z_input
129            z_input.Bind(wx.EVT_TEXT, self._callback)
Note: See TracBrowser for help on using the repository browser.