1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 """Module containing a set of special GUI elements to be used in the relax wizards."""
24
25
26 import wx
27 import wx.lib.mixins.listctrl
28
29
30 from graphics import fetch_icon
31 from gui.input_elements.combo_list import Combo_list
32 from gui.fonts import font
33 from gui.misc import add_border
34 from gui.string_conv import float_to_gui, gui_to_float, gui_to_int, gui_to_list, gui_to_py, gui_to_str, gui_to_tuple, int_to_gui, list_to_gui, py_to_gui, str_to_gui, tuple_to_gui
35 from lib.check_types import is_list
36 from lib.errors import RelaxError
37 from status import Status; status = Status()
38
39
41 """Wizard GUI element for the input of all types of Python sequence objects.
42
43 The supported Python types include:
44 - list of floats
45 - list of integers
46 - list of strings
47 - tuple of floats
48 - tuple of integers
49 - tuple of strings
50 """
51
52 - def __init__(self, name=None, default=None, parent=None, element_type='default', seq_type=None, value_type=None, dim=None, min=0, max=1000, sizer=None, titles=None, desc=None, combo_choices=None, combo_data=None, combo_list_min=None, tooltip=None, divider=None, padding=0, spacer=None, height_element=27, single_value=False, read_only=False, can_be_none=False):
53 """Set up the element.
54
55 @keyword name: The name of the element to use in titles, etc.
56 @type name: str
57 @keyword default: The default value of the element.
58 @type default: sequence object
59 @keyword parent: The wizard GUI element.
60 @type parent: wx.Panel instance
61 @keyword element_type: The type of GUI element to create. If set to 'default', the wx.TextCtrl element with a button to bring up a dialog with ListCtrl will be used. If set to 'combo_list', the special gui.components.combo_list.Combo_list element will be used.
62 @type element_type: str
63 @keyword seq_type: The type of Python sequence. This should be one of 'list' or 'tuple'.
64 @type seq_type: str
65 @keyword value_type: The type of Python object that the value should be. This can be one of 'float', 'int', or 'str'.
66 @type value_type: str
67 @keyword dim: The dimensions that a list or tuple must conform to. For a 1D sequence, this can be a single value or a tuple of possible sizes. For a 2D sequence (a numpy matrix or list of lists), this must be a tuple of the fixed dimension sizes, e.g. a 3x5 matrix should be specified as (3, 5).
68 @type dim: int, tuple of int or None
69 @keyword min: For a SpinCtrl, the minimum value allowed.
70 @type min: int
71 @keyword max: For a SpinCtrl, the maximum value allowed.
72 @type max: int
73 @keyword sizer: The sizer to put the input field widget into.
74 @type sizer: wx.Sizer instance
75 @keyword titles: The titles of each of the elements of the fixed dimension elements.
76 @type titles: list of str
77 @keyword desc: The text description.
78 @type desc: str
79 @keyword combo_choices: The list of choices to present to the user. This is only used if the element_type is set to 'combo'.
80 @type combo_choices: list of str
81 @keyword combo_data: The data returned by a call to GetValue(). This is only used if the element_type is set to 'combo'. If supplied, it should be the same length at the combo_choices list. If not supplied, the combo_choices list will be used for the returned data.
82 @type combo_data: list
83 @keyword combo_list_min: The minimum length for the Combo_list object.
84 @type combo_list_min: int or None
85 @keyword tooltip: The tooltip which appears on hovering over the text or input field.
86 @type tooltip: str
87 @keyword divider: The position of the divider.
88 @type divider: int
89 @keyword padding: Spacing to the left and right of the widgets.
90 @type padding: int
91 @keyword spacer: The amount of spacing to add below the field in pixels. If None, a stretchable spacer will be used.
92 @type spacer: None or int
93 @keyword height_element: The height in pixels of the GUI element.
94 @type height_element: int
95 @keyword single_value: A flag which if True will cause single input values to be treated as single values rather than a list or tuple.
96 @type single_value: bool
97 @keyword read_only: A flag which if True means that the text of the element cannot be edited.
98 @type read_only: bool
99 @keyword can_be_none: A flag which specifies if the element is allowed to have the None value.
100 @type can_be_none: bool
101 """
102
103
104 self.parent = parent
105 self.name = name
106 self.default = default
107 self.element_type = element_type
108 self.seq_type = seq_type
109 self.value_type = value_type
110 self.dim = dim
111 self.min = min
112 self.max = max
113 self.titles = titles
114 self.single_value = single_value
115 self.can_be_none = can_be_none
116
117
118 if value_type in ['float', 'num']:
119 self.convert_from_gui = gui_to_float
120 self.convert_to_gui = float_to_gui
121 elif value_type == 'int':
122 self.convert_from_gui = gui_to_int
123 self.convert_to_gui = int_to_gui
124 elif value_type == 'str':
125 self.convert_from_gui = gui_to_str
126 self.convert_to_gui = str_to_gui
127 else:
128 self.convert_from_gui = gui_to_py
129 self.convert_to_gui = py_to_gui
130
131
132 if seq_type == 'list':
133 self.convert_from_gui_seq = gui_to_list
134 self.convert_to_gui_seq = list_to_gui
135 elif seq_type == 'tuple':
136 self.convert_from_gui_seq = gui_to_tuple
137 self.convert_to_gui_seq = tuple_to_gui
138 else:
139 raise RelaxError("Unknown sequence type '%s'." % seq_type)
140
141
142 if self.element_type == 'default':
143
144 if read_only == None:
145 read_only = False
146
147
148 sub_sizer = wx.BoxSizer(wx.HORIZONTAL)
149
150
151 sub_sizer.AddSpacer(padding)
152
153
154 text = wx.StaticText(parent, -1, desc, style=wx.ALIGN_LEFT)
155 text.SetFont(font.normal)
156 sub_sizer.Add(text, 0, wx.LEFT|wx.ALIGN_CENTER_VERTICAL, 0)
157
158
159 if not divider:
160 raise RelaxError("The divider position has not been supplied.")
161
162
163 x, y = text.GetSize()
164 sub_sizer.AddSpacer((divider - x, 0))
165
166
167 self._field = wx.TextCtrl(parent, -1, '')
168 self._field.SetMinSize((50, height_element))
169 self._field.SetFont(font.normal)
170 sub_sizer.Add(self._field, 1, wx.ADJUST_MINSIZE|wx.ALIGN_CENTER_VERTICAL, 0)
171
172
173 if read_only:
174 self._field.SetEditable(False)
175 colour = parent.GetBackgroundColour()
176 self._field.SetOwnBackgroundColour(colour)
177
178
179 sub_sizer.AddSpacer(5)
180
181
182 button = wx.BitmapButton(parent, -1, wx.Bitmap(fetch_icon('oxygen.actions.edit-rename', "16x16"), wx.BITMAP_TYPE_ANY))
183 button.SetMinSize((height_element, height_element))
184 button.SetToolTipString("Edit the values.")
185 sub_sizer.Add(button, 0, wx.ADJUST_MINSIZE|wx.ALIGN_CENTER_VERTICAL, 0)
186 parent.Bind(wx.EVT_BUTTON, self.open_dialog, button)
187
188
189 sub_sizer.AddSpacer(padding)
190
191
192 sizer.Add(sub_sizer, 1, wx.EXPAND|wx.ALL, 0)
193
194
195 if spacer == None:
196 sizer.AddStretchSpacer()
197 else:
198 sizer.AddSpacer(spacer)
199
200
201 if tooltip:
202 text.SetToolTipString(tooltip)
203 self._field.SetToolTipString(tooltip)
204
205
206 if self.default != None:
207 self._field.SetValue(self.convert_to_gui_seq(self.default))
208
209
210 elif self.element_type == 'combo_list':
211
212 if read_only == None:
213 read_only = False
214
215
216 self._field = Combo_list(parent, sizer, desc, value_type=value_type, min_length=combo_list_min, choices=combo_choices, data=combo_data, default=default, tooltip=tooltip, read_only=read_only, can_be_none=can_be_none)
217
218
219 else:
220 raise RelaxError("Unknown element type '%s'." % self.element_type)
221
222
224 """Special method for clearing or resetting the GUI element."""
225
226
227 if self.element_type in ['default', 'combo_list']:
228 self._field.Clear()
229
230
232 """Special method for returning the sequence values of the GUI element.
233
234 @return: The sequence of values.
235 @rtype: sequence type
236 """
237
238
239 value = self._field.GetValue()
240
241
242 if self.element_type == 'combo_list':
243
244 if value == [] or value == None:
245 return None
246
247
248 else:
249
250 value_set = False
251 if self.single_value:
252 try:
253
254 value = self.convert_from_gui(value)
255
256
257 if value == None and self.can_be_none:
258 value_set = True
259 elif self.value_type == None:
260 value_set = True
261 elif self.value_type in ['float', 'num']:
262 if isinstance(value, int) or isinstance(value, float):
263 value_set = True
264 elif self.value_type == 'int':
265 if isinstance(value, int):
266 value_set = True
267 elif self.value_type == 'str':
268 if self.seq_type == 'list' and value[0] != '[':
269 value_set = True
270 elif self.seq_type == 'tuple' and value[0] != '(':
271 value_set = True
272 except:
273 pass
274
275
276 if not value_set:
277 try:
278 value = self.convert_from_gui_seq(value)
279
280
281 except RelaxError:
282 if self.can_be_none:
283 value = None
284 elif self.seq_type == 'list':
285 value = []
286 else:
287 value = ()
288
289
290 except:
291 value = None
292
293
294 if value == None:
295 return None
296
297
298 if self.single_value:
299 if (isinstance(value, list) or isinstance(value, tuple)) and len(value) == 1:
300 value = value[0]
301
302
303 elif value != None:
304 if self.seq_type == 'list' and not isinstance(value, list):
305 value = [value]
306 elif self.seq_type == 'tuple' and not isinstance(value, tuple):
307 value = (value,)
308
309
310 if not self.single_value and len(value) == 0:
311 return None
312
313
314 return value
315
316
317 - def SetValue(self, value=None, index=None):
318 """Special method for setting the value of the GUI element.
319
320 @keyword value: The value to set.
321 @type value: value or list of values
322 @keyword index: The index of the value to set, if the full list is not given.
323 @type index: int or None
324 """
325
326
327 if self.element_type == 'combo_list':
328 self._field.SetValue(value=value, index=index)
329
330
331 else:
332
333 if self.single_value and isinstance(value, list) and len(value) == 1:
334 value = value[0]
335
336
337 self._field.SetValue(self.convert_to_gui_seq(value))
338
339
340 - def UpdateChoices(self, combo_choices=None, combo_data=None, combo_default=None):
341 """Special wizard method for updating the list of choices in a ComboBox type element.
342
343 @keyword combo_choices: The list of choices to present to the user. This is only used if the element_type is set to 'combo_list'.
344 @type combo_choices: list of str
345 @keyword combo_data: The data returned by a call to GetValue(). This is only used if the element_type is set to 'combo_list'. If supplied, it should be the same length at the combo_choices list. If not supplied, the combo_choices list will be used for the returned data.
346 @type combo_data: list
347 @keyword combo_default: The default value of the ComboBox. This is only used if the element_type is set to 'combo_list'.
348 @type combo_default: str or None
349 """
350
351
352 if self.element_type == 'combo_list':
353 self._field.UpdateChoices(combo_choices=combo_choices, combo_data=combo_data, combo_default=combo_default)
354
355
357 """Open a special dialog for inputting a list of text values.
358
359 @param event: The wx event.
360 @type event: wx event
361 """
362
363
364 self.selection_win_show()
365
366
367 self.selection_win_data()
368
369
370 self.sel_win.Destroy()
371 del self.sel_win
372
373
375 """Extract the data from the selection window."""
376
377
378 value = self.sel_win.GetValue()
379
380
381 if value == None or not len(value):
382 self.Clear()
383
384
385 else:
386 self.SetValue(value)
387
388
390 """Show the selection window."""
391
392
393 if hasattr(self, 'sel_win'):
394 self.sel_win.Destroy()
395 del self.sel_win
396
397
398 self.sel_win = Sequence_window(parent=self.parent, name=self.name, seq_type=self.seq_type, value_type=self.value_type, titles=self.titles, dim=self.dim)
399
400
401 self.sel_win.SetValue(self.GetValue())
402
403
404 if status.show_gui:
405 self.sel_win.ShowModal()
406 self.sel_win.Close()
407
408
409
410 -class Sequence_list_ctrl(wx.ListCtrl, wx.lib.mixins.listctrl.TextEditMixin, wx.lib.mixins.listctrl.ListCtrlAutoWidthMixin):
411 """The string list ListCtrl object."""
412
414 """Initialise the control.
415
416 @param parent: The parent window.
417 @type parent: wx.Frame instance
418 """
419
420
421 wx.ListCtrl.__init__(self, parent, -1, style=wx.BORDER_SUNKEN|wx.LC_REPORT|wx.LC_HRULES|wx.LC_VRULES)
422 wx.lib.mixins.listctrl.TextEditMixin.__init__(self)
423 wx.lib.mixins.listctrl.ListCtrlAutoWidthMixin.__init__(self)
424
425
426 self.Bind(wx.EVT_LIST_BEGIN_LABEL_EDIT, self.begin_label_edit)
427
428
430 """Catch edits to make the first column read only.
431
432 @param event: The wx event.
433 @type event: wx event
434 """
435
436
437 if event.m_col == 0:
438 event.Veto()
439
440
441 else:
442 event.Skip()
443
444
445
447 """The Python sequence object editor window."""
448
449
450 SIZE = (800, 600)
451
452
453 BORDER = 10
454
455
456 SIZE_BUTTON = (150, 33)
457
458 - def __init__(self, parent=None, name='', seq_type='list', value_type='str', dim=None, titles=None):
459 """Set up the string list editor window.
460
461 @keyword parent: The parent GUI element.
462 @type parent: wx.Window instance or None
463 @keyword name: The name of the window.
464 @type name: str
465 @keyword seq_type: The type of Python sequence. This should be one of 'list' or 'tuple'.
466 @type seq_type: str
467 @keyword value_type: The type of Python data expected in the sequence. This should be one of 'float', 'int', or 'str'.
468 @type value_type: str
469 @keyword dim: The fixed dimension that the sequence must conform to.
470 @type dim: int or None
471 @keyword titles: The titles of each of the elements of the fixed dimension elements.
472 @type titles: list of str
473 """
474
475
476 self.name = name
477 self.seq_type = seq_type
478 self.value_type = value_type
479 self.dim = dim
480 self.titles = titles
481
482
483 if value_type in ['float', 'num']:
484 self.convert_from_gui = gui_to_float
485 self.convert_to_gui = float_to_gui
486 elif value_type == 'int':
487 self.convert_from_gui = gui_to_int
488 self.convert_to_gui = int_to_gui
489 elif value_type == 'str':
490 self.convert_from_gui = gui_to_str
491 self.convert_to_gui = str_to_gui
492 else:
493 raise RelaxError("Unknown base data type '%s'." % value_type)
494
495
496 if not hasattr(self, 'variable_length'):
497 self.variable_length = False
498 self.offset = 0
499 if dim == None:
500 self.variable_length = True
501 self.offset = 1
502
503
504 title = "Edit the %s values." % name
505
506
507 wx.Dialog.__init__(self, parent, id=-1, title=title)
508
509
510 self.width = self.SIZE[0] - 2*self.BORDER
511
512
513 self.SetSize(self.SIZE)
514 self.Centre()
515 self.SetFont(font.normal)
516
517
518 main_sizer = wx.BoxSizer(wx.VERTICAL)
519
520
521 self.SetSizer(main_sizer)
522
523
524 sizer = add_border(main_sizer, border=self.BORDER, packing=wx.VERTICAL)
525
526
527 self.add_list(sizer)
528
529
530 sizer.AddSpacer(self.BORDER)
531
532
533 self.add_buttons(sizer)
534
535
536 if not self.sequence.GetItemCount():
537 self.add_element()
538
539
541 """Return the values as a sequence of values.
542
543 @return: The sequence of values.
544 @rtype: sequence type
545 """
546
547
548 values = []
549
550
551 for i in range(self.sequence.GetItemCount()):
552
553 item = self.sequence.GetItem(i, col=1)
554 text = item.GetText()
555
556
557 try:
558 value = self.convert_from_gui(text)
559 except:
560 value = None
561 values.append(value)
562
563
564 if self.seq_type == 'tuple':
565 values = tuple(values)
566
567
568 empty = True
569 for i in range(len(values)):
570 if values[i] != None:
571 empty = False
572 break
573
574
575 if empty:
576 return None
577
578
579 return values
580
581
583 """Set up the list values.
584
585 @param values: The list of values to add to the list.
586 @type values: list of str or None
587 """
588
589
590 if values == None:
591 return
592
593
594 if not self.variable_length and is_list(values) and len(values) != self.dim:
595 return
596
597
598 try:
599 len(values)
600 except TypeError:
601 if self.seq_type == 'list':
602 values = [values]
603 elif self.seq_type == 'tuple':
604 values = (values,)
605
606
607 for i in range(len(values)):
608
609 if not self.variable_length:
610 self.sequence.SetStringItem(index=i, col=1, label=self.convert_to_gui(values[i]))
611
612
613 else:
614
615 if i != 0:
616
617 self.sequence.InsertStringItem(i, int_to_gui(i+1))
618
619
620 self.sequence.SetStringItem(index=i, col=1, label=self.convert_to_gui(values[i]))
621
622
679
680
682 """Append a new row to the list.
683
684 @keyword event: The wx event.
685 @type event: wx event
686 """
687
688
689 next = self.sequence.GetItemCount()
690
691
692 if self.variable_length:
693 self.sequence.InsertStringItem(next, int_to_gui(next+1))
694
695
696 else:
697 self.sequence.InsertStringItem(next, str_to_gui(''))
698
699
701 """Set up the list control.
702
703 @param sizer: A sizer object.
704 @type sizer: wx.Sizer instance
705 """
706
707
708 self.sequence = Sequence_list_ctrl(self)
709
710
711 title = "%s%s" % (self.name[0].upper(), self.name[1:])
712
713
714 if self.titles:
715 self.sequence.InsertColumn(0, "Title")
716 self.sequence.SetColumnWidth(0, 200)
717 else:
718 self.sequence.InsertColumn(0, "Number")
719 self.sequence.SetColumnWidth(0, 70)
720
721
722 self.sequence.InsertColumn(1, title)
723 self.sequence.SetColumnWidth(1, wx.LIST_AUTOSIZE)
724
725
726 sizer.Add(self.sequence, 1, wx.ALL|wx.EXPAND, 0)
727
728
729 if not self.variable_length:
730 for i in range(self.dim):
731
732 self.add_element()
733
734
735 if self.titles:
736 self.sequence.SetStringItem(i, 0, str_to_gui(self.titles[i]))
737
738
739 else:
740 self.sequence.SetStringItem(i, 0, int_to_gui(i+1))
741
742
744 """Close the window.
745
746 @param event: The wx event.
747 @type event: wx event
748 """
749
750
751 self.Close()
752
753
755 """Remove the last item from the list.
756
757 @param event: The wx event.
758 @type event: wx event
759 """
760
761
762 item = self.sequence.GetItemCount()
763 self.sequence.DeleteItem(item-1)
764
765
766 if not self.sequence.GetItemCount():
767 self.add_element()
768
769
771 """Remove all items from the list.
772
773 @param event: The wx event.
774 @type event: wx event
775 """
776
777
778 self.sequence.DeleteAllItems()
779
780
781 self.add_element()
782