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 gui.errors import gui_raise
36 from lib.errors import AllRelaxErrors
37 from status import Status; status = Status()
38
39
40 -def add_border(box, border=0, packing=wx.VERTICAL, debug=False):
41 """Create the main part of the frame, returning the central sizer.
42
43 @param box: The box sizer element to pack the borders into.
44 @type box: wx.BoxSizer instance
45 @keyword border: The size of the border in pixels.
46 @type border: int
47 @keyword packing: Specify if the central sizer should be vertically or horizontally packed.
48 @type packing: wx.VERTICAL or wx.HORIZONTAL
49 @keyword debug: A flag which if true will make colourful borders.
50 @type debug: bool
51 @return: The central sizer.
52 @rtype: wx.BoxSizer instance
53 """
54
55
56 orient = box.GetOrientation()
57 if orient == wx.HORIZONTAL:
58 orient_sub = wx.VERTICAL
59 else:
60 orient_sub = wx.HORIZONTAL
61
62
63 sizer_sub = wx.BoxSizer(orient_sub)
64 sizer_cent = wx.BoxSizer(packing)
65
66
67 if debug:
68
69 panel = wx.Panel(box.GetContainingWindow(), -1)
70 panel.SetSize((border, border))
71 panel.SetBackgroundColour("Red")
72 box.Add(panel, 0, wx.EXPAND|wx.ALL)
73
74
75 box.Add(sizer_sub, 1, wx.EXPAND|wx.ALL)
76
77
78 panel = wx.Panel(box.GetContainingWindow(), -1)
79 panel.SetSize((border, border))
80 panel.SetBackgroundColour("Yellow")
81 box.Add(panel, 0, wx.EXPAND|wx.ALL)
82
83
84 else:
85 box.AddSpacer(border)
86 box.Add(sizer_sub, 1, wx.EXPAND|wx.ALL)
87 box.AddSpacer(border)
88
89
90 if debug:
91
92 panel = wx.Panel(box.GetContainingWindow(), -1)
93 panel.SetSize((border, border))
94 panel.SetBackgroundColour("Blue")
95 sizer_sub.Add(panel, 0, wx.EXPAND|wx.ALL)
96
97
98 sizer_sub.Add(sizer_cent, 1, wx.EXPAND|wx.ALL)
99
100
101 panel = wx.Panel(box.GetContainingWindow(), -1)
102 panel.SetSize((border, border))
103 panel.SetBackgroundColour("Green")
104 sizer_sub.Add(panel, 0, wx.EXPAND|wx.ALL)
105
106
107 else:
108 sizer_sub.AddSpacer(border)
109 sizer_sub.Add(sizer_cent, 1, wx.EXPAND|wx.ALL)
110 sizer_sub.AddSpacer(border)
111
112
113 return sizer_cent
114
115
117 """Build and return the bitmap, handling transparency for all operating systems.
118
119 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.
120
121 @param path: The absolute path to the bitmap image.
122 @type path: str
123 @return: The processed bitmap object.
124 @rtype: wx.Bitmap instance
125 """
126
127
128 bitmap = wx.Bitmap(path, wx.BITMAP_TYPE_ANY)
129
130
131 if bitmap.HasAlpha() and not (hasattr(status, 'wx_info') and status.wx_info["os"] == "darwin"):
132 bitmap.SetMaskColour(wx.Colour(alpha=wx.ALPHA_OPAQUE))
133
134
135 return bitmap
136
137
257
258
260 """Open the file in the platform's native editor/viewer.
261
262 @param file: The path of the file to open.
263 @type file: str
264 @keyword force_text: A flag which if True will cause a text editor to be launched.
265 @type force_text: bool
266 """
267
268
269 if platform.uname()[0] in ['Windows', 'Microsoft']:
270
271 if force_text:
272 os.system('notepad %s' % os.path.normpath(file))
273
274
275 else:
276 os.startfile(os.path.normpath(file))
277
278
279 elif platform.uname()[0] == 'Darwin':
280
281 if force_text:
282 os.system('open -t %s' % file)
283
284
285 else:
286 os.system('open %s' % file)
287
288
289 else:
290 os.system('/usr/bin/xdg-open %s' % file)
291
292
294 """Apply the given function, catching all RelaxErrors.
295
296 All args and keyword args supplied will be directly applied to the given function.
297
298 @param fn: The function to apply.
299 @type fn: func
300 @return: The status of execution.
301 @rtype: bool
302 """
303
304
305 try:
306 fn(*args, **kargs)
307
308
309 except AllRelaxErrors:
310 instance = sys.exc_info()[1]
311
312
313 if status.debug:
314 raise
315
316
317 gui_raise(instance, raise_flag=False)
318
319
320 return False
321
322
323 return True
324
325
326 -def table_line(text=None, widths=None, bottom=False):
327 """Format a line of a table.
328
329 @keyword text: The list of table elements. If not given, an empty line will be be produced.
330 @type text: list of str or None
331 @keyword widths: The list of column widths for the table.
332 @type widths: list of int
333 @keyword botton: A flag which if True will cause a table bottom line to be produced.
334 @type bottom: bool
335 @return: The table line.
336 @rtype: str
337 """
338
339
340 if bottom:
341 line = " _"
342 else:
343 line = " "
344
345
346 for i in range(len(widths)):
347
348 if i > 0:
349 if bottom:
350 line += "___"
351 else:
352 line += " "
353
354
355 if bottom:
356 line += "_" * widths[i]
357
358
359 elif text == None:
360 line += " " * widths[i]
361
362
363 else:
364 line += text[i]
365 line += " " * (widths[i] - len(text[i]))
366
367
368 if bottom:
369 line += "_ \n"
370 else:
371 line += " \n"
372
373
374 return line
375