1 | |
---|
2 | |
---|
3 | |
---|
4 | ################################################################################ |
---|
5 | #This software was developed by the University of Tennessee as part of the |
---|
6 | #Distributed Data Analysis of Neutron Scattering Experiments (DANSE) |
---|
7 | #project funded by the US National Science Foundation. |
---|
8 | # |
---|
9 | #See the license text in license.txt |
---|
10 | # |
---|
11 | #copyright 2009, University of Tennessee |
---|
12 | ################################################################################ |
---|
13 | |
---|
14 | |
---|
15 | import wx |
---|
16 | from wx.lib.scrolledpanel import ScrolledPanel |
---|
17 | |
---|
18 | WIDTH = 400 |
---|
19 | HEIGHT = 350 |
---|
20 | |
---|
21 | class DialogPanel(ScrolledPanel): |
---|
22 | def __init__(self, *args, **kwds): |
---|
23 | ScrolledPanel.__init__(self, *args, **kwds) |
---|
24 | self.SetupScrolling() |
---|
25 | |
---|
26 | class InvTextCtrl(wx.TextCtrl): |
---|
27 | """ |
---|
28 | Text control for model and fit parameters. |
---|
29 | Binds the appropriate events for user interactions. |
---|
30 | """ |
---|
31 | def __init__(self, *args, **kwds): |
---|
32 | wx.TextCtrl.__init__(self, *args, **kwds) |
---|
33 | ## Set to True when the mouse is clicked while |
---|
34 | #the whole string is selected |
---|
35 | self.full_selection = False |
---|
36 | ## Call back for EVT_SET_FOCUS events |
---|
37 | _on_set_focus_callback = None |
---|
38 | |
---|
39 | # Bind appropriate events |
---|
40 | self.Bind(wx.EVT_LEFT_UP, self._highlight_text) |
---|
41 | self.Bind(wx.EVT_SET_FOCUS, self._on_set_focus) |
---|
42 | |
---|
43 | def _on_set_focus(self, event): |
---|
44 | """ |
---|
45 | Catch when the text control is set in focus to highlight the whole |
---|
46 | text if necessary |
---|
47 | |
---|
48 | :param event: mouse event |
---|
49 | """ |
---|
50 | event.Skip() |
---|
51 | self.full_selection = True |
---|
52 | |
---|
53 | def _highlight_text(self, event): |
---|
54 | """ |
---|
55 | Highlight text of a TextCtrl only of no text has be selected |
---|
56 | |
---|
57 | :param event: mouse event |
---|
58 | """ |
---|
59 | # Make sure the mouse event is available to other listeners |
---|
60 | event.Skip() |
---|
61 | control = event.GetEventObject() |
---|
62 | if self.full_selection: |
---|
63 | self.full_selection = False |
---|
64 | # Check that we have a TextCtrl |
---|
65 | if issubclass(control.__class__, wx.TextCtrl): |
---|
66 | # Check whether text has been selected, |
---|
67 | # if not, select the whole string |
---|
68 | (start, end) = control.GetSelection() |
---|
69 | if start == end: |
---|
70 | control.SetSelection(-1, -1) |
---|
71 | |
---|
72 | |
---|
73 | class OutputTextCtrl(wx.TextCtrl): |
---|
74 | """ |
---|
75 | Text control used to display outputs. |
---|
76 | No editing allowed. The background is |
---|
77 | grayed out. User can't select text. |
---|
78 | """ |
---|
79 | def __init__(self, *args, **kwds): |
---|
80 | wx.TextCtrl.__init__(self, *args, **kwds) |
---|
81 | self.SetEditable(False) |
---|
82 | self.SetBackgroundColour(self.GetParent().GetBackgroundColour()) |
---|
83 | |
---|
84 | # Bind to mouse event to avoid text highlighting |
---|
85 | # The event will be skipped once the call-back |
---|
86 | # is called. |
---|
87 | |
---|
88 | self.Bind(wx.EVT_MOUSE_EVENTS, self._click) |
---|
89 | |
---|
90 | |
---|
91 | def _click(self, event): |
---|
92 | """ |
---|
93 | Prevent further handling of the mouse event |
---|
94 | by not calling Skip(). |
---|
95 | """ |
---|
96 | pass |
---|
97 | |
---|
98 | class DataDialog(wx.Dialog): |
---|
99 | """ |
---|
100 | Allow file selection at loading time |
---|
101 | """ |
---|
102 | def __init__(self, data_list, parent=None, text='', *args, **kwds): |
---|
103 | kwds['size'] = (WIDTH, HEIGHT) |
---|
104 | kwds['title'] = "Data Selection" |
---|
105 | wx.Dialog.__init__(self, parent, *args, **kwds) |
---|
106 | self.list_of_ctrl = [] |
---|
107 | if not data_list: |
---|
108 | return |
---|
109 | self._sizer_main = wx.BoxSizer(wx.VERTICAL) |
---|
110 | self._sizer_txt = wx.BoxSizer(wx.VERTICAL) |
---|
111 | self._sizer_button = wx.BoxSizer(wx.HORIZONTAL) |
---|
112 | self._choice_sizer = wx.GridBagSizer(5, 5) |
---|
113 | self._panel = DialogPanel(self, style=wx.RAISED_BORDER, |
---|
114 | size=(WIDTH-20, HEIGHT/3)) |
---|
115 | self.SetSizer(self._sizer_main) |
---|
116 | self.__do_layout(data_list, text=text) |
---|
117 | self.Layout() |
---|
118 | |
---|
119 | def __do_layout(self, data_list, text=''): |
---|
120 | """ |
---|
121 | layout the dialog |
---|
122 | """ |
---|
123 | if not data_list or len(data_list) <= 1: |
---|
124 | return |
---|
125 | #add text |
---|
126 | if text.strip() == "": |
---|
127 | text = "This Perspective does not allow multiple data !\n" |
---|
128 | text += "Please select only one Data.\n" |
---|
129 | text_ctrl = wx.TextCtrl(self, -1, str(text), style=wx.TE_MULTILINE, |
---|
130 | size=(-1, HEIGHT/3)) |
---|
131 | text_ctrl.SetEditable(False) |
---|
132 | self._sizer_txt.Add(text_ctrl , 1, wx.EXPAND|wx.ALL, 10) |
---|
133 | iy = 0 |
---|
134 | ix = 0 |
---|
135 | rbox = wx.RadioButton(self._panel, -1, str(data_list[0].name), |
---|
136 | (10, 10), style= wx.RB_GROUP) |
---|
137 | rbox.SetValue(True) |
---|
138 | self.list_of_ctrl.append((rbox, data_list[0])) |
---|
139 | self._choice_sizer.Add(rbox, (iy, ix), (1, 1), |
---|
140 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
141 | for i in range(1, len(data_list)): |
---|
142 | iy += 1 |
---|
143 | rbox = wx.RadioButton(self._panel, -1, |
---|
144 | str(data_list[i].name), (10, 10)) |
---|
145 | rbox.SetValue(False) |
---|
146 | self.list_of_ctrl.append((rbox, data_list[i])) |
---|
147 | self._choice_sizer.Add(rbox, (iy, ix), |
---|
148 | (1, 1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
149 | self._panel.SetSizer(self._choice_sizer) |
---|
150 | #add sizer |
---|
151 | self._sizer_button.Add((20, 20), 1, wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
152 | button_cancel = wx.Button(self, wx.ID_CANCEL, "Cancel") |
---|
153 | self._sizer_button.Add(button_cancel, 0, |
---|
154 | wx.LEFT|wx.RIGHT|wx.ADJUST_MINSIZE, 10) |
---|
155 | button_OK = wx.Button(self, wx.ID_OK, "Ok") |
---|
156 | button_OK.SetFocus() |
---|
157 | self._sizer_button.Add(button_OK, 0, |
---|
158 | wx.LEFT|wx.RIGHT|wx.ADJUST_MINSIZE, 10) |
---|
159 | static_line = wx.StaticLine(self, -1) |
---|
160 | |
---|
161 | self._sizer_txt.Add(self._panel, 0, wx.EXPAND|wx.ALL, 5) |
---|
162 | self._sizer_main.Add(self._sizer_txt, 0, wx.EXPAND|wx.ALL, 5) |
---|
163 | self._sizer_main.Add(static_line, 0, wx.EXPAND, 0) |
---|
164 | self._sizer_main.Add(self._sizer_button, 0, wx.EXPAND|wx.ALL, 10) |
---|
165 | |
---|
166 | |
---|
167 | def get_data(self): |
---|
168 | """ |
---|
169 | return the selected data |
---|
170 | """ |
---|
171 | for item in self.list_of_ctrl: |
---|
172 | rbox, data = item |
---|
173 | if rbox.GetValue(): |
---|
174 | return data |
---|
175 | |
---|
176 | |
---|
177 | |
---|
178 | |
---|