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