[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): |
---|
[ba65aff] | 65 | """ |
---|
| 66 | Set the value of the vector input |
---|
| 67 | |
---|
| 68 | :param vector: A Vector object |
---|
| 69 | """ |
---|
[af84162] | 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 | |
---|
[c9a519f] | 77 | def Validate(self): |
---|
| 78 | """ |
---|
| 79 | Validate the contents of the inputs |
---|
| 80 | |
---|
[af84162] | 81 | :return all_valid: Whether or not the inputs are valid |
---|
| 82 | :return invalid_ctrl: A control that is not valid |
---|
[c9a519f] | 83 | (or None if all are valid) |
---|
| 84 | """ |
---|
[af84162] | 85 | all_valid = True |
---|
| 86 | invalid_ctrl = None |
---|
[c9a519f] | 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: |
---|
[af84162] | 92 | all_valid = False |
---|
| 93 | invalid_ctrl = control |
---|
| 94 | return all_valid, invalid_ctrl |
---|
[c9a519f] | 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 | |
---|
[af84162] | 108 | self._sizer.AddSpacer((10, -1)) |
---|
[c9a519f] | 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: |
---|
[af84162] | 120 | self._sizer.AddSpacer((10, -1)) |
---|
[c9a519f] | 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) |
---|