1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 """Module containing the base GUI element for listing things."""
25
26
27 import wx
28 import wx.lib.buttons
29
30
31 from status import Status; status = Status()
32 from user_functions.data import Uf_info; uf_info = Uf_info()
33
34
35 from gui.components.menu import build_menu_item
36 from gui.fonts import font
37 from gui.misc import add_border
38 from gui.string_conv import str_to_gui
39 from gui.uf_objects import Uf_storage; uf_store = Uf_storage()
40
41
43 """The GUI element for listing the software used in the analysis."""
44
45 - def __init__(self, gui=None, parent=None, box=None, id=None, proportion=0, button_placement='default'):
46 """Build the base list GUI element.
47
48 @keyword gui: The main GUI object.
49 @type gui: wx.Frame instance
50 @keyword parent: The parent GUI element that this is to be attached to.
51 @type parent: wx object
52 @keyword box: The box sizer to pack this GUI component into.
53 @type box: wx.BoxSizer instance
54 @keyword id: A unique identification string. This is used to register the update method with the GUI user function observer object.
55 @type id: str
56 @keyword proportion: The window proportion parameter.
57 @type proportion: bool
58 @keyword button_placement: Override the button visibility and placement. The value of 'default' will leave the buttons at the default setting. The value of 'top' will place the buttons at the top, 'bottom' will place them at the bottom, and None will turn off the buttons.
59 @type button_placement: str or None
60 """
61
62
63 self.gui = gui
64 self.parent = parent
65 self.proportion = proportion
66
67
68 self.title = ""
69 self.spacing = 5
70 self.border = 5
71 self.observer_base_name = None
72 self.columns = []
73 self.button_placement = None
74 self.button_size = (120, 40)
75 self.button_spacing = 5
76 self.button_info = []
77 self.popup_menus = []
78
79
80 self.setup()
81
82
83 if button_placement != 'default':
84 self.button_placement = button_placement
85
86
87 self.panel = wx.Panel(self.parent)
88 box.Add(self.panel, self.proportion, wx.ALL|wx.EXPAND, 0)
89
90
91 panel_sizer = wx.BoxSizer(wx.VERTICAL)
92 self.panel.SetSizer(panel_sizer)
93
94
95 self.data_box = wx.StaticBox(self.panel, -1)
96 self.set_box_label()
97 self.data_box.SetFont(font.subtitle)
98 sub_sizer = wx.StaticBoxSizer(self.data_box, wx.VERTICAL)
99
100
101 panel_sizer.Add(sub_sizer, self.proportion, wx.ALL|wx.EXPAND, 0)
102
103
104 box_centre = add_border(sub_sizer, border=self.border)
105
106
107 if self.button_placement == 'top':
108 self.add_buttons(box_centre)
109 box_centre.AddSpacer(self.spacing)
110
111
112 self.init_element(box_centre)
113
114
115 self.build_element()
116
117
118 if self.button_placement == 'bottom':
119 box_centre.AddSpacer(self.spacing)
120 self.add_buttons(box_centre)
121
122
123 if self.observer_base_name:
124 self.name = '%s: %s' % (self.observer_base_name, id)
125 else:
126 self.name = id
127
128
129 self.observer_register()
130
131
132 - def Enable(self, enable=True):
133 """Enable or disable the element.
134
135 @keyword enable: The flag specifying if the element should be enabled or disabled.
136 @type enable: bool
137 """
138
139
140 for i in range(len(self.button_info)):
141
142 button = getattr(self, self.button_info[i]['object'])
143
144
145 button.Enable(enable)
146
147
184
185
195
196
198 """Build the spectra listing GUI element in a thread safe wx.CallAfter call."""
199
200
201 self.element.Freeze()
202
203
204 self.set_box_label()
205
206
207 self.element.DeleteAllItems()
208
209
210 self.update_data()
211
212
213 self.size_cols()
214
215
216 event = wx.PyCommandEvent(wx.EVT_SIZE.typeId, self.parent.GetId())
217 wx.PostEvent(self.parent.GetEventHandler(), event)
218
219
220 if not self.proportion:
221
222 n = self.element.GetItemCount()
223
224
225 head = self.height_char + 10
226
227
228 centre = (self.height_char + 6) * n
229
230
231 foot = wx.SystemSettings_GetMetric(wx.SYS_HSCROLL_Y)
232
233
234 height = head + centre + foot
235
236
237 self.element.SetMinSize((-1, height))
238 self.element.Layout()
239
240
241 self.element.Thaw()
242
243
249
250
252 """Initialise the GUI element.
253
254 @param sizer: The sizer element to pack the element into.
255 @type sizer: wx.BoxSizer instance
256 """
257
258
259 self.element = wx.ListCtrl(self.panel, -1, style=wx.BORDER_SUNKEN|wx.LC_REPORT)
260
261
262 for i in range(len(self.columns)):
263 self.element.InsertColumn(i, str_to_gui(self.columns[i]))
264
265
266 self.element.SetFont(font.normal)
267
268
269 self.height_char = self.element.GetCharHeight()
270
271
272 self.element.Bind(wx.EVT_SIZE, self.resize)
273 self.element.Bind(wx.EVT_COMMAND_RIGHT_CLICK, self.on_right_click)
274 self.element.Bind(wx.EVT_RIGHT_UP, self.on_right_click)
275
276
277 sizer.Add(self.element, self.proportion, wx.ALL|wx.EXPAND, 0)
278
279
281 """Base method which always returns True.
282
283 @return: The answer to the question.
284 @rtype: bool
285 """
286
287
288 return True
289
290
292 """Register and unregister methods with the observer objects.
293
294 @keyword remove: If set to True, then the methods will be unregistered.
295 @type remove: False
296 """
297
298
299 if not remove:
300 status.observers.gui_uf.register(self.name, self.build_element, method_name='build_element')
301 status.observers.pipe_alteration.register(self.name, self.build_element, method_name='build_element')
302
303
304 else:
305 status.observers.gui_uf.unregister(self.name)
306 status.observers.pipe_alteration.unregister(self.name)
307
308
310 """Pop up menu for the right click.
311
312 @param event: The wx event.
313 @type event: wx event
314 """
315
316
317 if self.popup_menus == []:
318 return
319
320
321 if status.exec_lock.locked():
322 return
323
324
325 menu = wx.Menu()
326
327
328 for i in range(len(self.popup_menus)):
329
330 info = self.popup_menus[i]
331
332
333 menu.AppendItem(build_menu_item(menu, id=info['id'], text=info['text'], icon=info['icon']))
334
335
336 self.element.Bind(wx.EVT_MENU, info['method'], id=info['id'])
337
338
339 if status.show_gui:
340 self.element.PopupMenu(menu)
341 menu.Destroy()
342
343
345 """Catch the resize to allow the element to be resized.
346
347 @param event: The wx event.
348 @type event: wx event
349 """
350
351
352 self.size_cols()
353
354
355 event.Skip()
356
357
359 """Set the label of the StaticBox."""
360
361
362 self.data_box.SetLabel(self.title)
363
364
366 """Set the column sizes."""
367
368
369 x, y = self.element.GetSize()
370
371
372 n = self.element.GetColumnCount()
373
374
375 if n == 0:
376 width = x
377 else:
378 width = int(x / n)
379
380
381 for i in range(n):
382 self.element.SetColumnWidth(i, width)
383