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
28
29 from graphics import fetch_icon
30 from gui.filedialog import RelaxFileDialog
31 from gui.fonts import font
32 from gui.misc import open_file
33 from gui.string_conv import gui_to_str, str_to_gui
34 from lib.errors import RelaxError
35 from status import Status; status = Status()
36
37
39 """Wizard GUI element for selecting files."""
40
41 - def __init__(self, name=None, default=None, parent=None, sizer=None, desc=None, message='File selection', wildcard=wx.FileSelectorDefaultWildcardStr, style=wx.FD_DEFAULT_STYLE, tooltip=None, divider=None, padding=0, spacer=None, height_element=27, preview=True, read_only=False):
42 """Build the file selection element.
43
44 @keyword name: The name of the element to use in titles, etc.
45 @type name: str
46 @keyword default: The default value of the element.
47 @type default: str
48 @keyword parent: The wizard GUI element.
49 @type parent: wx.Panel instance
50 @keyword sizer: The sizer to put the input field into.
51 @type sizer: wx.Sizer instance
52 @keyword desc: The text description.
53 @type desc: str
54 @keyword message: The file selector prompt string.
55 @type message: String
56 @keyword wildcard: The file wildcard pattern. For example for opening PDB files, this could be "PDB files (*.pdb)|*.pdb;*.PDB".
57 @type wildcard: String
58 @keyword style: The dialog style. To open a single file, set to wx.FD_OPEN. To open multiple files, set to wx.FD_OPEN|wx.FD_MULTIPLE. To save a single file, set to wx.FD_SAVE. To save multiple files, set to wx.FD_SAVE|wx.FD_MULTIPLE.
59 @type style: long
60 @keyword tooltip: The tooltip which appears on hovering over all the GUI elements.
61 @type tooltip: str
62 @keyword divider: The position of the divider.
63 @type divider: int
64 @keyword padding: Spacing to the left and right of the widgets.
65 @type padding: int
66 @keyword spacer: The amount of spacing to add below the field in pixels. If None, a stretchable spacer will be used.
67 @type spacer: None or int
68 @keyword height_element: The height in pixels of the GUI element.
69 @type height_element: int
70 @keyword preview: A flag which if true will allow the file to be previewed.
71 @type preview: bool
72 @keyword read_only: A flag which if True means that the text of the element cannot be edited.
73 @type read_only: bool
74 """
75
76
77 self.name = name
78
79
80 if default == None:
81 default = wx.EmptyString
82
83
84 sub_sizer = wx.BoxSizer(wx.HORIZONTAL)
85
86
87 sub_sizer.AddSpacer(padding)
88
89
90 text = wx.StaticText(parent, -1, desc, style=wx.ALIGN_LEFT)
91 text.SetFont(font.normal)
92 sub_sizer.Add(text, 0, wx.LEFT|wx.ALIGN_CENTER_VERTICAL, 0)
93
94
95 if not divider:
96 raise RelaxError("The divider position has not been supplied.")
97
98
99 x, y = text.GetSize()
100 sub_sizer.AddSpacer((divider - x, 0))
101
102
103 self._field = wx.TextCtrl(parent, -1, default)
104 self._field.SetMinSize((-1, height_element))
105 self._field.SetFont(font.normal)
106 sub_sizer.Add(self._field, 1, wx.ADJUST_MINSIZE|wx.ALIGN_CENTER_VERTICAL, 0)
107
108
109 obj = RelaxFileDialog(parent, field=self._field, message=message, defaultFile=default, wildcard=wildcard, style=style)
110
111
112 sub_sizer.AddSpacer(5)
113
114
115 button = wx.BitmapButton(parent, -1, wx.Bitmap(fetch_icon('oxygen.actions.document-open', "16x16"), wx.BITMAP_TYPE_ANY))
116 button.SetMinSize((height_element, height_element))
117 button.SetToolTipString("Select the file.")
118 sub_sizer.Add(button, 0, wx.ADJUST_MINSIZE|wx.ALIGN_CENTER_VERTICAL, 0)
119 parent.Bind(wx.EVT_BUTTON, obj.select_event, button)
120
121
122 if preview:
123
124 sub_sizer.AddSpacer(5)
125
126
127 button = wx.BitmapButton(parent, -1, wx.Bitmap(fetch_icon('oxygen.actions.document-preview', "16x16"), wx.BITMAP_TYPE_ANY))
128 button.SetMinSize((height_element, height_element))
129 button.SetToolTipString("Preview")
130 sub_sizer.Add(button, 0, wx.ADJUST_MINSIZE|wx.ALIGN_CENTER_VERTICAL, 0)
131 parent.Bind(wx.EVT_BUTTON, self.preview_file, button)
132
133
134 sub_sizer.AddSpacer(padding)
135
136
137 sizer.Add(sub_sizer, 1, wx.EXPAND|wx.ALL, 0)
138
139
140 if spacer == None:
141 sizer.AddStretchSpacer()
142 else:
143 sizer.AddSpacer(spacer)
144
145
146 if tooltip:
147 text.SetToolTipString(tooltip)
148 self._field.SetToolTipString(tooltip)
149
150
152 """Special method for clearing or resetting the GUI element."""
153
154
155 self._field.Clear()
156
157
159 """Special method for returning the value of the GUI element.
160
161 @return: The string value.
162 @rtype: list of str
163 """
164
165
166 return gui_to_str(self._field.GetValue())
167
168
170 """Special method for setting the value of the GUI element.
171
172 @param value: The value to set.
173 @type value: str
174 """
175
176
177 self._field.SetValue(str_to_gui(value))
178
179
181 """Preview a file.
182
183 @keyword event: The wx event.
184 @type event: wx event
185 """
186
187
188 file = gui_to_str(self._field.GetValue())
189
190
191 if file == None:
192 return
193
194
195 open_file(file, force_text=True)
196