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 | self._sizer_main = wx.BoxSizer(wx.VERTICAL) |
---|
164 | self._sizer_txt = wx.BoxSizer(wx.VERTICAL) |
---|
165 | self._sizer_button = wx.BoxSizer(wx.HORIZONTAL) |
---|
166 | self._choice_sizer = wx.GridBagSizer(5, 5) |
---|
167 | self._panel = DialogPanel(self, style=wx.RAISED_BORDER, |
---|
168 | size=(WIDTH-20, HEIGHT/3)) |
---|
169 | |
---|
170 | self.__do_layout(data_list, text=text) |
---|
171 | |
---|
172 | |
---|
173 | def __do_layout(self, data_list, text=''): |
---|
174 | """ |
---|
175 | layout the dialog |
---|
176 | """ |
---|
177 | #if not data_list or len(data_list) <= 1: |
---|
178 | # return |
---|
179 | #add text |
---|
180 | if text.strip() == "": |
---|
181 | text = "This Perspective does not allow multiple data !\n" |
---|
182 | text += "Please select only one Data.\n" |
---|
183 | text_ctrl = wx.TextCtrl(self, -1, str(text), style=wx.TE_MULTILINE, |
---|
184 | size=(-1, HEIGHT/3)) |
---|
185 | text_ctrl.SetEditable(False) |
---|
186 | self._sizer_txt.Add(text_ctrl , 1, wx.EXPAND|wx.ALL, 10) |
---|
187 | iy = 0 |
---|
188 | ix = 0 |
---|
189 | rbox = wx.RadioButton(self._panel, -1, str(data_list[0].name), |
---|
190 | (10, 10), style= wx.RB_GROUP) |
---|
191 | rbox.SetValue(True) |
---|
192 | self.list_of_ctrl.append((rbox, data_list[0])) |
---|
193 | self._choice_sizer.Add(rbox, (iy, ix), (1, 1), |
---|
194 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
195 | for i in range(1, len(data_list)): |
---|
196 | iy += 1 |
---|
197 | rbox = wx.RadioButton(self._panel, -1, |
---|
198 | str(data_list[i].name), (10, 10)) |
---|
199 | rbox.SetValue(False) |
---|
200 | self.list_of_ctrl.append((rbox, data_list[i])) |
---|
201 | self._choice_sizer.Add(rbox, (iy, ix), |
---|
202 | (1, 1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
203 | self._panel.SetSizer(self._choice_sizer) |
---|
204 | #add sizer |
---|
205 | self._sizer_button.Add((20, 20), 1, wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
206 | button_cancel = wx.Button(self, wx.ID_CANCEL, "Cancel") |
---|
207 | self._sizer_button.Add(button_cancel, 0, |
---|
208 | wx.LEFT|wx.RIGHT|wx.ADJUST_MINSIZE, 10) |
---|
209 | button_OK = wx.Button(self, wx.ID_OK, "Ok") |
---|
210 | button_OK.SetFocus() |
---|
211 | self._sizer_button.Add(button_OK, 0, |
---|
212 | wx.LEFT|wx.RIGHT|wx.ADJUST_MINSIZE, 10) |
---|
213 | static_line = wx.StaticLine(self, -1) |
---|
214 | |
---|
215 | self._sizer_txt.Add(self._panel, 0, wx.EXPAND|wx.ALL, 5) |
---|
216 | self._sizer_main.Add(self._sizer_txt, 0, wx.EXPAND|wx.ALL, 5) |
---|
217 | self._sizer_main.Add(static_line, 0, wx.EXPAND, 0) |
---|
218 | self._sizer_main.Add(self._sizer_button, 0, wx.EXPAND|wx.ALL, 10) |
---|
219 | self.SetSizer(self._sizer_main) |
---|
220 | |
---|
221 | def get_data(self): |
---|
222 | """ |
---|
223 | return the selected data |
---|
224 | """ |
---|
225 | for item in self.list_of_ctrl: |
---|
226 | rbox, data = item |
---|
227 | if rbox.GetValue(): |
---|
228 | return data |
---|
229 | |
---|
230 | |
---|
231 | |
---|
232 | |
---|