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

ESS_GUIESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_opencl
Last change on this file since 9f3db13 was fa81e94, checked in by Piotr Rozyczko <rozyczko@…>, 7 years ago

Initial commit of the P(r) inversion perspective.
Code merged from Jeff Krzywon's ESS_GUI_Pr branch.
Also, minor 2to3 mods to sascalc/sasgui to enble error free setup.

  • Property mode set to 100755
File size: 5.8 KB
Line 
1"""
2This module provides some custom wx widgets for the file converter perspective
3"""
4import wx
5import os
6from sas.sascalc.dataloader.data_info import Vector
7from sas.sasgui.guiframe.utils import check_float
8
9class VectorInput(object):
10    """
11    An input field for inputting 2 (or 3) components of a vector.
12    """
13
14    def __init__(self, parent, control_name, callback=None,
15        labels=["x: ", "y: ", "z: "], z_enabled=False):
16        """
17        Create the control
18
19        :param parent: The window to add the control to
20        :param control_name: All TextCtrl names will start with control_name
21        :param callback: The function to call when the text is changed
22        :param labels: An array of labels for the TextCtrls
23        :param z_enabled: Whether or not to show the z input field
24        """
25        self.parent = parent
26        self.control_name = control_name
27        self._callback = callback
28        self._name = control_name
29
30        self.labels = labels
31        self.z_enabled = z_enabled
32        self._sizer = None
33        self._inputs = {}
34
35        self._do_layout()
36
37    def GetSizer(self):
38        """
39        Get the control's sizer
40
41        :return sizer: a wx.BoxSizer object
42        """
43        return self._sizer
44
45    def GetName(self):
46        return self._name
47
48    def GetValue(self):
49        """
50        Get the value of the vector input
51
52        :return v: A Vector object
53        """
54        v = Vector()
55        if not self.Validate(): return v
56        for direction, control in self._inputs.items():
57            try:
58                value = float(control.GetValue())
59                setattr(v, direction, value)
60            except: # Text field is empty
61                pass
62
63        return v
64
65    def SetValue(self, vector):
66        """
67        Set the value of the vector input
68
69        :param vector: A Vector object
70        """
71        directions = ['x', 'y']
72        if self.z_enabled: directions.append('z')
73        for direction in directions:
74            value = getattr(vector, direction)
75            if value is None: value = ''
76            self._inputs[direction].SetValue(str(value))
77
78    def Validate(self):
79        """
80        Validate the contents of the inputs
81
82        :return all_valid: Whether or not the inputs are valid
83        :return invalid_ctrl: A control that is not valid
84            (or None if all are valid)
85        """
86        all_valid = True
87        invalid_ctrl = None
88        for control in list(self._inputs.values()):
89            if control.GetValue() == '': continue
90            control.SetBackgroundColour(wx.WHITE)
91            control_valid = check_float(control)
92            if not control_valid:
93                all_valid = False
94                invalid_ctrl = control
95        return all_valid, invalid_ctrl
96
97
98    def _do_layout(self):
99        self._sizer = wx.BoxSizer(wx.HORIZONTAL)
100        x_label = wx.StaticText(self.parent, -1, self.labels[0],
101            style=wx.ALIGN_CENTER_VERTICAL)
102        self._sizer.Add(x_label, wx.ALIGN_CENTER_VERTICAL)
103        x_input = wx.TextCtrl(self.parent, -1,
104            name="{}_x".format(self.control_name), size=(50, -1))
105        self._sizer.Add(x_input)
106        self._inputs['x'] = x_input
107        x_input.Bind(wx.EVT_TEXT, self._callback)
108
109        self._sizer.AddSpacer((10, -1))
110
111        y_label = wx.StaticText(self.parent, -1, self.labels[1],
112            style=wx.ALIGN_CENTER_VERTICAL)
113        self._sizer.Add(y_label, wx.ALIGN_CENTER_VERTICAL)
114        y_input = wx.TextCtrl(self.parent, -1,
115            name="{}_y".format(self.control_name), size=(50, -1))
116        self._sizer.Add(y_input)
117        self._inputs['y'] = y_input
118        y_input.Bind(wx.EVT_TEXT, self._callback)
119
120        if self.z_enabled:
121            self._sizer.AddSpacer((10, -1))
122
123            z_label = wx.StaticText(self.parent, -1, self.labels[2],
124                style=wx.ALIGN_CENTER_VERTICAL)
125            self._sizer.Add(z_label, wx.ALIGN_CENTER_VERTICAL)
126            z_input = wx.TextCtrl(self.parent, -1,
127                name="{}_z".format(self.control_name), size=(50, -1))
128            self._sizer.Add(z_input)
129            self._inputs['z'] = z_input
130            z_input.Bind(wx.EVT_TEXT, self._callback)
131
132class FileInput(object):
133
134    def __init__(self, parent, wildcard=''):
135        self.parent = parent
136        self._sizer = None
137        self._text_ctrl = None
138        self._button_ctrl = None
139        self._filepath = ''
140        self._wildcard = wildcard
141
142        self._do_layout()
143
144    def GetCtrl(self):
145        return self._sizer
146
147    def GetPath(self):
148        return self._filepath
149
150    def SetWildcard(self, wildcard):
151        self._wildcard = wildcard
152
153
154    def _on_text_change(self, event):
155        event.Skip()
156        self._filepath = self._text_ctrl.GetValue()
157
158    def _on_browse(self, event):
159        event.Skip()
160        initial_path = self._filepath
161        initial_dir = os.getcwd()
162        if not os.path.isfile(initial_path):
163            initial_path = ''
164        else:
165            initial_dir = os.path.split(initial_path)[0]
166
167        file_dlg = wx.FileDialog(self.parent, defaultDir=initial_dir,
168            defaultFile=initial_path, wildcard=self._wildcard,
169            style=wx.FD_SAVE | wx.FD_OVERWRITE_PROMPT)
170        if file_dlg.ShowModal() == wx.ID_CANCEL:
171            file_dlg.Destroy()
172            return
173        self._filepath = file_dlg.GetPath()
174        file_dlg.Destroy()
175        self._text_ctrl.SetValue(self._filepath)
176
177    def _do_layout(self):
178        self._sizer = wx.BoxSizer(wx.HORIZONTAL)
179
180        self._text_ctrl = wx.TextCtrl(self.parent, -1)
181        self._sizer.Add(self._text_ctrl, wx.EXPAND)
182        self._text_ctrl.Bind(wx.EVT_TEXT, self._on_text_change)
183
184        self._sizer.AddSpacer(5)
185
186        self._button_ctrl = wx.Button(self.parent, -1, "Browse")
187        self._sizer.Add(self._button_ctrl)
188        self._button_ctrl.Bind(wx.EVT_BUTTON, self._on_browse)
Note: See TracBrowser for help on using the repository browser.