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

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

Abstract vector inputs into their own class

  • Property mode set to 100644
File size: 3.5 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.labels = labels
28        self.z_enabled = z_enabled
29        self._sizer = None
30        self._inputs = {}
31
32        self._do_layout()
33
34    def GetSizer(self):
35        """
36        Get the control's sizer
37
38        :return sizer: a wx.BoxSizer object
39        """
40        return self._sizer
41
42    def GetValue(self):
43        """
44        Get the value of the vector input
45
46        :return v: A Vector object
47        """
48        v = Vector()
49        if not self.Validate(): return v
50        for direction, control in self._inputs:
51            try:
52                value = float(control.GetValue())
53                setattr(v, direction, value)
54            except: # Text field is empty
55                pass
56
57        return v
58
59    def Validate(self):
60        """
61        Validate the contents of the inputs
62
63        :return control_valid: Whether or not the inputs are valid
64        :return invalid_control: The control that is not valid
65            (or None if all are valid)
66        """
67        for control in self._inputs.values():
68            if control.GetValue() == '': continue
69            control.SetBackgroundColour(wx.WHITE)
70            control_valid = check_float(control)
71            if not control_valid:
72                return False, control
73        return True, None
74
75
76    def _do_layout(self):
77        self._sizer = wx.BoxSizer(wx.HORIZONTAL)
78        x_label = wx.StaticText(self.parent, -1, self.labels[0],
79            style=wx.ALIGN_CENTER_VERTICAL)
80        self._sizer.Add(x_label, wx.ALIGN_CENTER_VERTICAL)
81        x_input = wx.TextCtrl(self.parent, -1,
82            name="{}_x".format(self.control_name), size=(50, -1))
83        self._sizer.Add(x_input)
84        self._inputs['x'] = x_input
85        x_input.Bind(wx.EVT_TEXT, self._callback)
86
87        self._sizer.AddSpacer((15, -1))
88
89        y_label = wx.StaticText(self.parent, -1, self.labels[1],
90            style=wx.ALIGN_CENTER_VERTICAL)
91        self._sizer.Add(y_label, wx.ALIGN_CENTER_VERTICAL)
92        y_input = wx.TextCtrl(self.parent, -1,
93            name="{}_y".format(self.control_name), size=(50, -1))
94        self._sizer.Add(y_input)
95        self._inputs['y'] = y_input
96        y_input.Bind(wx.EVT_TEXT, self._callback)
97
98        if self.z_enabled:
99            self._sizer.AddSpacer((15, -1))
100
101            z_label = wx.StaticText(self.parent, -1, self.labels[2],
102                style=wx.ALIGN_CENTER_VERTICAL)
103            self._sizer.Add(z_label, wx.ALIGN_CENTER_VERTICAL)
104            z_input = wx.TextCtrl(self.parent, -1,
105                name="{}_z".format(self.control_name), size=(50, -1))
106            self._sizer.Add(z_input)
107            self._inputs['z'] = z_input
108            z_input.Bind(wx.EVT_TEXT, self._callback)
Note: See TracBrowser for help on using the repository browser.