1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 """Miscellaneous functions used throughout the GUI."""
25
26
27 from copy import deepcopy
28 import os
29 import platform
30 import sys
31 from textwrap import wrap
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 """Build and return the bitmap, handling transparency for all operating systems.
120
121 This function is required to handle alpha in bitmap on MS Windows so that regions with partial transparency are not blended into the default dark grey colour of Windows' windows.
122
123 @param path: The absolute path to the bitmap image.
124 @type path: str
125 @return: The processed bitmap object.
126 @rtype: wx.Bitmap instance
127 """
128
129
130 bitmap = wx.Bitmap(path, wx.BITMAP_TYPE_ANY)
131
132
133 if bitmap.HasAlpha() and status.wx_info["os"] != "darwin":
134 bitmap.SetMaskColour(wx.Colour(alpha=wx.ALPHA_OPAQUE))
135
136
137 return bitmap
138
139
259
260
262 """Open the file in the platform's native editor/viewer.
263
264 @param file: The path of the file to open.
265 @type file: str
266 @keyword force_text: A flag which if True will cause a text editor to be launched.
267 @type force_text: bool
268 """
269
270
271 if platform.uname()[0] in ['Windows', 'Microsoft']:
272
273 if force_text:
274 os.system('notepad %s' % os.path.normpath(file))
275
276
277 else:
278 os.startfile(os.path.normpath(file))
279
280
281 elif platform.uname()[0] == 'Darwin':
282
283 if force_text:
284 os.system('open -t %s' % file)
285
286
287 else:
288 os.system('open %s' % file)
289
290
291 else:
292 os.system('/usr/bin/xdg-open %s' % file)
293
294
296 """Apply the given function, catching all RelaxErrors.
297
298 All args and keyword args supplied will be directly applied to the given function.
299
300 @param fn: The function to apply.
301 @type fn: func
302 @return: The status of execution.
303 @rtype: bool
304 """
305
306
307 try:
308 apply(fn, args, kargs)
309
310
311 except AllRelaxErrors:
312 instance = sys.exc_info()[1]
313
314
315 if status.debug:
316 raise
317
318
319 gui_raise(instance, raise_flag=False)
320
321
322 return False
323
324
325 return True
326
327
328 -def table_line(text=None, widths=None, bottom=False):
329 """Format a line of a table.
330
331 @keyword text: The list of table elements. If not given, an empty line will be be produced.
332 @type text: list of str or None
333 @keyword widths: The list of column widths for the table.
334 @type widths: list of int
335 @keyword botton: A flag which if True will cause a table bottom line to be produced.
336 @type bottom: bool
337 @return: The table line.
338 @rtype: str
339 """
340
341
342 if bottom:
343 line = " _"
344 else:
345 line = " "
346
347
348 for i in range(len(widths)):
349
350 if i > 0:
351 if bottom:
352 line += "___"
353 else:
354 line += " "
355
356
357 if bottom:
358 line += "_" * widths[i]
359
360
361 elif text == None:
362 line += " " * widths[i]
363
364
365 else:
366 line += text[i]
367 line += " " * (widths[i] - len(text[i]))
368
369
370 if bottom:
371 line += "_ \n"
372 else:
373 line += " \n"
374
375
376 return line
377