[c9a519f] | 1 | """ |
---|
| 2 | This module provides some custom wx widgets for the file converter perspective |
---|
| 3 | """ |
---|
| 4 | import wx |
---|
| 5 | from sas.sascalc.dataloader.data_info import Vector |
---|
| 6 | from sas.sasgui.guiframe.utils import check_float |
---|
| 7 | |
---|
| 8 | class 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 |
---|
[af84162] | 27 | self._name = control_name |
---|
| 28 | |
---|
[c9a519f] | 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 | |
---|
[af84162] | 44 | def GetName(self): |
---|
| 45 | return self._name |
---|
| 46 | |
---|
[c9a519f] | 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 |
---|
[af84162] | 55 | for direction, control in self._inputs.iteritems(): |
---|
[c9a519f] | 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 | |
---|
[af84162] | 64 | def SetValue(self, vector): |
---|
| 65 | directions = ['x', 'y'] |
---|
| 66 | if self.z_enabled: directions.append('z') |
---|
| 67 | for direction in directions: |
---|
| 68 | value = getattr(vector, direction) |
---|
| 69 | if value is None: value = '' |
---|
| 70 | self._inputs[direction].SetValue(str(value)) |
---|
| 71 | |
---|
[c9a519f] | 72 | def Validate(self): |
---|
| 73 | """ |
---|
| 74 | Validate the contents of the inputs |
---|
| 75 | |
---|
[af84162] | 76 | :return all_valid: Whether or not the inputs are valid |
---|
| 77 | :return invalid_ctrl: A control that is not valid |
---|
[c9a519f] | 78 | (or None if all are valid) |
---|
| 79 | """ |
---|
[af84162] | 80 | all_valid = True |
---|
| 81 | invalid_ctrl = None |
---|
[c9a519f] | 82 | for control in self._inputs.values(): |
---|
| 83 | if control.GetValue() == '': continue |
---|
| 84 | control.SetBackgroundColour(wx.WHITE) |
---|
| 85 | control_valid = check_float(control) |
---|
| 86 | if not control_valid: |
---|
[af84162] | 87 | all_valid = False |
---|
| 88 | invalid_ctrl = control |
---|
| 89 | return all_valid, invalid_ctrl |
---|
[c9a519f] | 90 | |
---|
| 91 | |
---|
| 92 | def _do_layout(self): |
---|
| 93 | self._sizer = wx.BoxSizer(wx.HORIZONTAL) |
---|
| 94 | x_label = wx.StaticText(self.parent, -1, self.labels[0], |
---|
| 95 | style=wx.ALIGN_CENTER_VERTICAL) |
---|
| 96 | self._sizer.Add(x_label, wx.ALIGN_CENTER_VERTICAL) |
---|
| 97 | x_input = wx.TextCtrl(self.parent, -1, |
---|
| 98 | name="{}_x".format(self.control_name), size=(50, -1)) |
---|
| 99 | self._sizer.Add(x_input) |
---|
| 100 | self._inputs['x'] = x_input |
---|
| 101 | x_input.Bind(wx.EVT_TEXT, self._callback) |
---|
| 102 | |
---|
[af84162] | 103 | self._sizer.AddSpacer((10, -1)) |
---|
[c9a519f] | 104 | |
---|
| 105 | y_label = wx.StaticText(self.parent, -1, self.labels[1], |
---|
| 106 | style=wx.ALIGN_CENTER_VERTICAL) |
---|
| 107 | self._sizer.Add(y_label, wx.ALIGN_CENTER_VERTICAL) |
---|
| 108 | y_input = wx.TextCtrl(self.parent, -1, |
---|
| 109 | name="{}_y".format(self.control_name), size=(50, -1)) |
---|
| 110 | self._sizer.Add(y_input) |
---|
| 111 | self._inputs['y'] = y_input |
---|
| 112 | y_input.Bind(wx.EVT_TEXT, self._callback) |
---|
| 113 | |
---|
| 114 | if self.z_enabled: |
---|
[af84162] | 115 | self._sizer.AddSpacer((10, -1)) |
---|
[c9a519f] | 116 | |
---|
| 117 | z_label = wx.StaticText(self.parent, -1, self.labels[2], |
---|
| 118 | style=wx.ALIGN_CENTER_VERTICAL) |
---|
| 119 | self._sizer.Add(z_label, wx.ALIGN_CENTER_VERTICAL) |
---|
| 120 | z_input = wx.TextCtrl(self.parent, -1, |
---|
| 121 | name="{}_z".format(self.control_name), size=(50, -1)) |
---|
| 122 | self._sizer.Add(z_input) |
---|
| 123 | self._inputs['z'] = z_input |
---|
| 124 | z_input.Bind(wx.EVT_TEXT, self._callback) |
---|