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 |
---|
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 | 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 | |
---|
72 | def Validate(self): |
---|
73 | """ |
---|
74 | Validate the contents of the inputs |
---|
75 | |
---|
76 | :return all_valid: Whether or not the inputs are valid |
---|
77 | :return invalid_ctrl: A control that is not valid |
---|
78 | (or None if all are valid) |
---|
79 | """ |
---|
80 | all_valid = True |
---|
81 | invalid_ctrl = None |
---|
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: |
---|
87 | all_valid = False |
---|
88 | invalid_ctrl = control |
---|
89 | return all_valid, invalid_ctrl |
---|
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 | |
---|
103 | self._sizer.AddSpacer((10, -1)) |
---|
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: |
---|
115 | self._sizer.AddSpacer((10, -1)) |
---|
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) |
---|