1 | |
---|
2 | ################################################################################ |
---|
3 | #This software was developed by the University of Tennessee as part of the |
---|
4 | #Distributed Data Analysis of Neutron Scattering Experiments (DANSE) |
---|
5 | #project funded by the US National Science Foundation. |
---|
6 | # |
---|
7 | #See the license text in license.txt |
---|
8 | # |
---|
9 | #copyright 2009, University of Tennessee |
---|
10 | ################################################################################ |
---|
11 | |
---|
12 | """ |
---|
13 | Text controls for input/output of the main PrView panel |
---|
14 | """ |
---|
15 | |
---|
16 | import wx |
---|
17 | import os |
---|
18 | from wx.lib.scrolledpanel import ScrolledPanel |
---|
19 | class PrTextCtrl(wx.TextCtrl): |
---|
20 | """ |
---|
21 | Text control for model and fit parameters. |
---|
22 | Binds the appropriate events for user interactions. |
---|
23 | """ |
---|
24 | def __init__(self, *args, **kwds): |
---|
25 | |
---|
26 | wx.TextCtrl.__init__(self, *args, **kwds) |
---|
27 | |
---|
28 | ## Set to True when the mouse is clicked while the whole string is selected |
---|
29 | full_selection = False |
---|
30 | ## Call back for EVT_SET_FOCUS events |
---|
31 | _on_set_focus_callback = None |
---|
32 | # Bind appropriate events |
---|
33 | self.Bind(wx.EVT_LEFT_UP, self._highlight_text) |
---|
34 | self.Bind(wx.EVT_SET_FOCUS, self._on_set_focus) |
---|
35 | |
---|
36 | def _on_set_focus(self, event): |
---|
37 | """ |
---|
38 | Catch when the text control is set in focus to highlight the whole |
---|
39 | text if necessary |
---|
40 | |
---|
41 | :param event: mouse event |
---|
42 | |
---|
43 | """ |
---|
44 | event.Skip() |
---|
45 | self.full_selection = True |
---|
46 | |
---|
47 | def _highlight_text(self, event): |
---|
48 | """ |
---|
49 | Highlight text of a TextCtrl only of no text has be selected |
---|
50 | |
---|
51 | :param event: mouse event |
---|
52 | |
---|
53 | """ |
---|
54 | # Make sure the mouse event is available to other listeners |
---|
55 | event.Skip() |
---|
56 | control = event.GetEventObject() |
---|
57 | if self.full_selection: |
---|
58 | self.full_selection = False |
---|
59 | # Check that we have a TextCtrl |
---|
60 | if issubclass(control.__class__, wx.TextCtrl): |
---|
61 | # Check whether text has been selected, |
---|
62 | # if not, select the whole string |
---|
63 | (start, end) = control.GetSelection() |
---|
64 | if start==end: |
---|
65 | control.SetSelection(-1,-1) |
---|
66 | |
---|
67 | class OutputTextCtrl(wx.TextCtrl): |
---|
68 | """ |
---|
69 | Text control used to display outputs. |
---|
70 | No editing allowed. The background is |
---|
71 | grayed out. User can't select text. |
---|
72 | """ |
---|
73 | def __init__(self, *args, **kwds): |
---|
74 | """ |
---|
75 | """ |
---|
76 | wx.TextCtrl.__init__(self, *args, **kwds) |
---|
77 | self.SetEditable(False) |
---|
78 | self.SetBackgroundColour(self.GetParent().GetBackgroundColour()) |
---|
79 | |
---|
80 | # Bind to mouse event to avoid text highlighting |
---|
81 | # The event will be skipped once the call-back |
---|
82 | # is called. |
---|
83 | self.Bind(wx.EVT_MOUSE_EVENTS, self._click) |
---|
84 | |
---|
85 | def _click(self, event): |
---|
86 | """ |
---|
87 | Prevent further handling of the mouse event |
---|
88 | by not calling Skip(). |
---|
89 | """ |
---|
90 | pass |
---|
91 | |
---|
92 | |
---|
93 | class DataFileTextCtrl(OutputTextCtrl): |
---|
94 | """ |
---|
95 | Text control used to display only the file name |
---|
96 | given a full path. |
---|
97 | |
---|
98 | :TODO: now that we no longer choose the data file from the panel, |
---|
99 | it's no longer necessary to pass around the file path. That code |
---|
100 | should be refactored away and simplified. |
---|
101 | """ |
---|
102 | def __init__(self, *args, **kwds): |
---|
103 | """ |
---|
104 | """ |
---|
105 | OutputTextCtrl.__init__(self, *args, **kwds) |
---|
106 | self._complete_path = None |
---|
107 | |
---|
108 | def SetValue(self, value): |
---|
109 | """ |
---|
110 | Sets the file name given a path |
---|
111 | """ |
---|
112 | self._complete_path = str(value) |
---|
113 | file = os.path.basename(self._complete_path) |
---|
114 | OutputTextCtrl.SetValue(self, file) |
---|
115 | |
---|
116 | def GetValue(self): |
---|
117 | """ |
---|
118 | Return the full path |
---|
119 | """ |
---|
120 | return self._complete_path |
---|
121 | |
---|
122 | class DataDialog(wx.Dialog): |
---|
123 | """ |
---|
124 | Allow file selection at loading time |
---|
125 | """ |
---|
126 | def __init__(self, data_list, parent=None, text='', *args, **kwds): |
---|
127 | wx.Dialog.__init__(self, parent, *args, **kwds) |
---|
128 | self.list_of_ctrl = [] |
---|
129 | self._do_layout(data_list, text=text) |
---|
130 | |
---|
131 | def _do_layout(self, data_list, text=''): |
---|
132 | """ |
---|
133 | layout the dialog |
---|
134 | """ |
---|
135 | if not data_list or len(data_list) <= 1: |
---|
136 | return |
---|
137 | # Dialog box properties |
---|
138 | self.SetTitle("Data Selection") |
---|
139 | w = 400 |
---|
140 | h = 200 |
---|
141 | self.SetSize((w, h)) |
---|
142 | sizer_main = wx.BoxSizer(wx.VERTICAL) |
---|
143 | sizer_txt = wx.BoxSizer(wx.VERTICAL) |
---|
144 | sizer_button = wx.BoxSizer(wx.HORIZONTAL) |
---|
145 | choice_sizer = wx.GridBagSizer(5, 5) |
---|
146 | #choice_sizer.SetMinSize((w-20, h-50)) |
---|
147 | panel = ScrolledPanel(self, style=wx.RAISED_BORDER, size=(w-20, h-50)) |
---|
148 | panel.SetupScrolling() |
---|
149 | #add text |
---|
150 | if text.strip() == "": |
---|
151 | text = "This Perspective does not allow multiple data !\n" |
---|
152 | text += "Please select only one Data.\n" |
---|
153 | text_ctrl = wx.StaticText(self, -1, str(text)) |
---|
154 | sizer_txt.Add(text_ctrl) |
---|
155 | iy = 0 |
---|
156 | ix = 0 |
---|
157 | rbox = wx.RadioButton(panel, -1, str(data_list[0].name), |
---|
158 | (10, 10), style= wx.RB_GROUP) |
---|
159 | rbox.SetValue(True) |
---|
160 | self.list_of_ctrl.append((rbox, data_list[0])) |
---|
161 | choice_sizer.Add(rbox, (iy, ix), (1, 1), |
---|
162 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
163 | for i in range(1, len(data_list)): |
---|
164 | iy += 1 |
---|
165 | rbox = wx.RadioButton(panel, -1, str(data_list[i].name), (10, 10)) |
---|
166 | rbox.SetValue(False) |
---|
167 | self.list_of_ctrl.append((rbox, data_list[i])) |
---|
168 | choice_sizer.Add(rbox, (iy, ix), |
---|
169 | (1, 1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
170 | panel.SetSizer(choice_sizer) |
---|
171 | #add sizer |
---|
172 | sizer_button.Add((20, 20), 1, wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
173 | button_cancel = wx.Button(self, wx.ID_CANCEL, "Cancel") |
---|
174 | sizer_button.Add(button_cancel, 0, |
---|
175 | wx.LEFT|wx.RIGHT|wx.ADJUST_MINSIZE, 10) |
---|
176 | button_OK = wx.Button(self, wx.ID_OK, "Ok") |
---|
177 | button_OK.SetFocus() |
---|
178 | sizer_button.Add(button_OK, 0, wx.LEFT|wx.RIGHT|wx.ADJUST_MINSIZE, 10) |
---|
179 | static_line = wx.StaticLine(self, -1) |
---|
180 | |
---|
181 | sizer_txt.Add(panel, 1, wx.EXPAND|wx.LEFT|wx.RIGHT, 5) |
---|
182 | sizer_main.Add(sizer_txt, 1, wx.EXPAND|wx.ALL, 10) |
---|
183 | sizer_main.Add(static_line, 0, wx.EXPAND, 0) |
---|
184 | sizer_main.Add(sizer_button, 0, wx.EXPAND|wx.ALL, 10) |
---|
185 | self.SetSizer(sizer_main) |
---|
186 | self.Layout() |
---|
187 | |
---|
188 | def get_data(self): |
---|
189 | """ |
---|
190 | return the selected data |
---|
191 | """ |
---|
192 | for item in self.list_of_ctrl: |
---|
193 | rbox, data = item |
---|
194 | if rbox.GetValue(): |
---|
195 | return data |
---|
196 | def load_error(error=None): |
---|
197 | """ |
---|
198 | Pop up an error message. |
---|
199 | |
---|
200 | :param error: details error message to be displayed |
---|
201 | """ |
---|
202 | message = "The data file you selected could not be loaded.\n" |
---|
203 | message += "Make sure the content of your file is properly formatted.\n\n" |
---|
204 | |
---|
205 | if error is not None: |
---|
206 | message += "When contacting the DANSE team, mention the" |
---|
207 | message += " following:\n%s" % str(error) |
---|
208 | |
---|
209 | dial = wx.MessageDialog(None, message, 'Error Loading File', |
---|
210 | wx.OK | wx.ICON_EXCLAMATION) |
---|
211 | dial.ShowModal() |
---|
212 | |
---|