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