1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 """Miscellaneous functions used throughout the GUI."""
26
27
28 from math import pow
29 import os
30 import platform
31 from string import split
32 import wx
33
34
35 from relax_errors import AllRelaxErrors
36 from status import Status; status = Status()
37
38
39 from gui.errors import gui_raise
40
41
42 -def add_border(box, border=0, packing=wx.VERTICAL, debug=False):
43 """Create the main part of the frame, returning the central sizer.
44
45 @param box: The box sizer element to pack the borders into.
46 @type box: wx.BoxSizer instance
47 @keyword border: The size of the border in pixels.
48 @type border: int
49 @keyword packing: Specify if the central sizer should be vertically or horizontally packed.
50 @type packing: wx.VERTICAL or wx.HORIZONTAL
51 @keyword debug: A flag which if true will make colourful borders.
52 @type debug: bool
53 @return: The central sizer.
54 @rtype: wx.BoxSizer instance
55 """
56
57
58 orient = box.GetOrientation()
59 if orient == wx.HORIZONTAL:
60 orient_sub = wx.VERTICAL
61 else:
62 orient_sub = wx.HORIZONTAL
63
64
65 sizer_sub = wx.BoxSizer(orient_sub)
66 sizer_cent = wx.BoxSizer(packing)
67
68
69 if debug:
70
71 panel = wx.Panel(box.GetContainingWindow(), -1)
72 panel.SetSize((border, border))
73 panel.SetBackgroundColour("Red")
74 box.Add(panel, 0, wx.EXPAND|wx.ALL)
75
76
77 box.Add(sizer_sub, 1, wx.EXPAND|wx.ALL)
78
79
80 panel = wx.Panel(box.GetContainingWindow(), -1)
81 panel.SetSize((border, border))
82 panel.SetBackgroundColour("Yellow")
83 box.Add(panel, 0, wx.EXPAND|wx.ALL)
84
85
86 else:
87 box.AddSpacer(border)
88 box.Add(sizer_sub, 1, wx.EXPAND|wx.ALL)
89 box.AddSpacer(border)
90
91
92 if debug:
93
94 panel = wx.Panel(box.GetContainingWindow(), -1)
95 panel.SetSize((border, border))
96 panel.SetBackgroundColour("Blue")
97 sizer_sub.Add(panel, 0, wx.EXPAND|wx.ALL)
98
99
100 sizer_sub.Add(sizer_cent, 1, wx.EXPAND|wx.ALL)
101
102
103 panel = wx.Panel(box.GetContainingWindow(), -1)
104 panel.SetSize((border, border))
105 panel.SetBackgroundColour("Green")
106 sizer_sub.Add(panel, 0, wx.EXPAND|wx.ALL)
107
108
109 else:
110 sizer_sub.AddSpacer(border)
111 sizer_sub.Add(sizer_cent, 1, wx.EXPAND|wx.ALL)
112 sizer_sub.AddSpacer(border)
113
114
115 return sizer_cent
116
117
119 """Convert the bool into the GUI string.
120
121 @param bool: The boolean value of True or False.
122 @type bool: bool
123 @return: The GUI string.
124 @rtype: unicode
125 """
126
127
128 return unicode(bool)
129
130
132 """Method to convert a string like '1.02*1e-10' to a float variable.
133
134 @param string: The number in string form.
135 @type string: str or unicode
136 @return: The floating point number.
137 @rtype: float
138 """
139
140
141 entries = split('*')
142
143
144 a = entries[0]
145 a = float(a)
146
147
148 b = entries[1]
149 b = float(b[2:len(b)])
150
151
152 result = a * pow(10, b)
153
154
155 return result
156
157
159 """Convert the float into the GUI string.
160
161 @param num: The number in float or None form.
162 @type num: float or None
163 @return: The GUI string.
164 @rtype: unicode
165 """
166
167
168 if num == None:
169 num = ''
170
171
172 return unicode(num)
173
174
176 """Convert the GUI obtained string to a bool.
177
178 @param string: The bool in string form.
179 @type string: str or unicode
180 @return: The bool.
181 @rtype: bool
182 """
183
184
185 if string in ['', u'', None]:
186 return None
187
188
189 return eval(string)
190
191
193 """Convert the GUI obtained string to an float.
194
195 @param string: The number in string form.
196 @type string: str or unicode
197 @return: The float
198 @rtype: float or None
199 """
200
201
202 if string in ['', u'', None]:
203 return None
204
205
206 if isinstance(string, float):
207 return string
208
209
210 val = eval(string)
211
212
213 if isinstance(val, int):
214 val = float(val)
215
216
217 if not isinstance(val, float):
218 return string
219
220
221 return val
222
223
225 """Convert the GUI obtained string to an int.
226
227 @param string: The number in string form.
228 @type string: str or unicode
229 @return: The integer
230 @rtype: int or None
231 """
232
233
234 if string in ['', u'', None]:
235 return None
236
237
238 if isinstance(string, int):
239 return string
240
241
242 try:
243 val = eval(string)
244 except:
245 val = None
246
247
248 if not isinstance(val, int):
249 return string
250
251
252 return val
253
254
256 """Convert the GUI obtained string to a list.
257
258 @param string: The list in string form.
259 @type string: str or unicode
260 @return: The integer or list of integers.
261 @rtype: int or int list
262 """
263
264
265 if string in ['', u'', None]:
266 return None
267
268
269 if isinstance(string, int) or isinstance(string, list):
270 return string
271
272
273 try:
274 val = eval(string)
275
276
277 except NameError:
278 return string
279
280
281
282 return val
283
284
286 """Convert the GUI obtained string to a list.
287
288 @param string: The list in string form.
289 @type string: str or unicode
290 @return: The list.
291 @rtype: list
292 """
293
294
295 if string in ['', u'', None]:
296 return []
297
298
299 val = eval(string)
300 if not isinstance(val, list):
301 val = [val]
302
303
304 return val
305
306
308 """Convert the GUI obtained string to a string.
309
310 @param string: The number in string form.
311 @type string: str or unicode
312 @return: The string.
313 @rtype: str
314 """
315
316
317 if string in ['', u'', None]:
318 return None
319
320
321 return str(string)
322
323
325 """Convert the GUI obtained string to a list.
326
327 @param string: The list in string form.
328 @type string: str or unicode
329 @return: The integer or list of integers.
330 @rtype: int or int list
331 """
332
333
334 if string in ['', u'', None]:
335 return None
336
337
338 try:
339 val = eval(string)
340
341
342 except NameError:
343 return str(string)
344
345
346 return val
347
348
350 """Convert the int into the GUI string.
351
352 @param num: The number in int or None form.
353 @type num: int or None
354 @return: The GUI string.
355 @rtype: unicode
356 """
357
358
359 if num == None:
360 num = ''
361
362
363 return unicode(num)
364
365
367 """Convert the list into the GUI string.
368
369 @param list: The Python list.
370 @type list: list or None
371 @return: The GUI string.
372 @rtype: unicode
373 """
374
375
376 if list == None:
377 list = ''
378
379
380 return unicode(list)
381
382
384 """Open the file in the platform's native editor/viewer.
385
386 @param file: The path of the file to open.
387 @type file: str
388 @keyword force_text: A flag which if True will cause a text editor to be launched.
389 @type force_text: bool
390 """
391
392
393 if platform.uname()[0] in ['Windows', 'Microsoft']:
394
395 if force_text:
396 os.system('notepad %s' % os.path.normpath(file))
397
398
399 else:
400 os.startfile(os.path.normpath(file))
401
402
403 elif platform.uname()[0] == 'Darwin':
404
405 if force_text:
406 os.system('open -t %s' % file)
407
408
409 else:
410 os.system('open %s' % file)
411
412
413 else:
414 os.system('/usr/bin/xdg-open %s' % file)
415
416
418 """Apply the given function, catching all RelaxErrors.
419
420 All args and keyword args supplied will be directly applied to the given function.
421
422 @param fn: The function to apply.
423 @type fn: func
424 @return: The status of execution.
425 @rtype: bool
426 """
427
428
429 try:
430 apply(fn, args, kargs)
431
432
433 except AllRelaxErrors, instance:
434
435 if status.debug:
436 raise
437
438
439 gui_raise(instance, raise_flag=False)
440
441
442 return False
443
444
445 return True
446
447
449 """Convert the string into the GUI string.
450
451 @param string: The string or None to convert.
452 @type string: str or None
453 @return: The GUI string.
454 @rtype: unicode
455 """
456
457
458 if string == None:
459 string = ''
460
461
462 return unicode(string)
463