Package gui :: Module misc
[hide private]
[frames] | no frames]

Source Code for Module gui.misc

  1  ############################################################################### 
  2  #                                                                             # 
  3  # Copyright (C) 2009 Michael Bieri                                            # 
  4  # Copyright (C) 2010-2011 Edward d'Auvergne                                   # 
  5  #                                                                             # 
  6  # This file is part of the program relax.                                     # 
  7  #                                                                             # 
  8  # relax is free software; you can redistribute it and/or modify               # 
  9  # it under the terms of the GNU General Public License as published by        # 
 10  # the Free Software Foundation; either version 2 of the License, or           # 
 11  # (at your option) any later version.                                         # 
 12  #                                                                             # 
 13  # relax is distributed in the hope that it will be useful,                    # 
 14  # but WITHOUT ANY WARRANTY; without even the implied warranty of              # 
 15  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               # 
 16  # GNU General Public License for more details.                                # 
 17  #                                                                             # 
 18  # You should have received a copy of the GNU General Public License           # 
 19  # along with relax; if not, write to the Free Software                        # 
 20  # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA   # 
 21  #                                                                             # 
 22  ############################################################################### 
 23   
 24  # Module docstring. 
 25  """Miscellaneous functions used throughout the GUI.""" 
 26   
 27  # Python module imports. 
 28  from math import pow 
 29  import os 
 30  import platform 
 31  from string import split 
 32  import wx 
 33   
 34  # relax module imports. 
 35  from relax_errors import AllRelaxErrors 
 36  from status import Status; status = Status() 
 37   
 38  # relax GUI module imports. 
 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 # The orientation of the sub sizer. 58 orient = box.GetOrientation() 59 if orient == wx.HORIZONTAL: 60 orient_sub = wx.VERTICAL 61 else: 62 orient_sub = wx.HORIZONTAL 63 64 # Some sizers. 65 sizer_sub = wx.BoxSizer(orient_sub) 66 sizer_cent = wx.BoxSizer(packing) 67 68 # Left and right borders (debugging). 69 if debug: 70 # Left coloured panel. 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 # Centre. 77 box.Add(sizer_sub, 1, wx.EXPAND|wx.ALL) 78 79 # Top coloured panel. 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 # Left and right borders. 86 else: 87 box.AddSpacer(border) 88 box.Add(sizer_sub, 1, wx.EXPAND|wx.ALL) 89 box.AddSpacer(border) 90 91 # Top and bottom borders (debugging). 92 if debug: 93 # Top coloured panel. 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 # Centre. 100 sizer_sub.Add(sizer_cent, 1, wx.EXPAND|wx.ALL) 101 102 # Bottom coloured panel. 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 # Top and bottom borders. 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 # Return the central sizer. 115 return sizer_cent
116 117
118 -def bool_to_gui(bool):
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 # Convert. 128 return unicode(bool)
129 130
131 -def convert_to_float(string):
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 # Break the number up. 141 entries = split('*') 142 143 # The first part of the number. 144 a = entries[0] 145 a = float(a) 146 147 # The second part of the number. 148 b = entries[1] 149 b = float(b[2:len(b)]) 150 151 # Recombine. 152 result = a * pow(10, b) 153 154 # Return the float. 155 return result
156 157
158 -def float_to_gui(num):
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 # No input. 168 if num == None: 169 num = '' 170 171 # Convert. 172 return unicode(num)
173 174
175 -def gui_to_bool(string):
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 # No value. 185 if string in ['', u'', None]: 186 return None 187 188 # Convert. 189 return eval(string)
190 191
192 -def gui_to_float(string):
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 # No input. 202 if string in ['', u'', None]: 203 return None 204 205 # Already a float. 206 if isinstance(string, float): 207 return string 208 209 # Convert. 210 val = eval(string) 211 212 # An int. 213 if isinstance(val, int): 214 val = float(val) 215 216 # Not a float! 217 if not isinstance(val, float): 218 return string 219 220 # A float. 221 return val
222 223
224 -def gui_to_int(string):
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 # No input. 234 if string in ['', u'', None]: 235 return None 236 237 # Already an int. 238 if isinstance(string, int): 239 return string 240 241 # Convert. 242 try: 243 val = eval(string) 244 except: 245 val = None 246 247 # Not an int! 248 if not isinstance(val, int): 249 return string 250 251 # An int. 252 return val
253 254
255 -def gui_to_int_or_list(string):
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 # No value. 265 if string in ['', u'', None]: 266 return None 267 268 # Already an int or list. 269 if isinstance(string, int) or isinstance(string, list): 270 return string 271 272 # Convert. 273 try: 274 val = eval(string) 275 276 # Failure, so return the original value. 277 except NameError: 278 return string 279 280 281 # Return the list. 282 return val
283 284
285 -def gui_to_list(string):
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 # No value. 295 if string in ['', u'', None]: 296 return [] 297 298 # Convert. 299 val = eval(string) 300 if not isinstance(val, list): 301 val = [val] 302 303 # Return the list. 304 return val
305 306
307 -def gui_to_str(string):
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 # No value. 317 if string in ['', u'', None]: 318 return None 319 320 # Convert. 321 return str(string)
322 323
324 -def gui_to_str_or_list(string):
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 # No value. 334 if string in ['', u'', None]: 335 return None 336 337 # Try converting to a list. 338 try: 339 val = eval(string) 340 341 # Catch failures, and try as a string. 342 except NameError: 343 return str(string) 344 345 # Return the list. 346 return val
347 348
349 -def int_to_gui(num):
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 # No input. 359 if num == None: 360 num = '' 361 362 # Convert. 363 return unicode(num)
364 365
366 -def list_to_gui(list):
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 # No input. 376 if list == None: 377 list = '' 378 379 # Convert. 380 return unicode(list)
381 382
383 -def open_file(file, force_text=False):
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 # Windows. 393 if platform.uname()[0] in ['Windows', 'Microsoft']: 394 # Text file. 395 if force_text: 396 os.system('notepad %s' % os.path.normpath(file)) 397 398 # All other files. 399 else: 400 os.startfile(os.path.normpath(file)) 401 402 # Mac OS X. 403 elif platform.uname()[0] == 'Darwin': 404 # Text file. 405 if force_text: 406 os.system('open -t %s' % file) 407 408 # All other files. 409 else: 410 os.system('open %s' % file) 411 412 # POSIX Systems with xdg-open. 413 else: 414 os.system('/usr/bin/xdg-open %s' % file)
415 416
417 -def protected_exec(fn, *args, **kargs):
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 # Apply the function. 429 try: 430 apply(fn, args, kargs) 431 432 # Catch RelaxErrors. 433 except AllRelaxErrors, instance: 434 # Raise the error in debugging mode. 435 if status.debug: 436 raise 437 438 # Display a dialog with the error. 439 gui_raise(instance, raise_flag=False) 440 441 # Failure. 442 return False 443 444 # Success. 445 return True
446 447
448 -def str_to_gui(string):
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 # No input. 458 if string == None: 459 string = '' 460 461 # Convert. 462 return unicode(string)
463