1"""Core objects and helper functions.""" 2from __future__ import print_function, division 3 4import os 5import sys 6import wx 7 8from monty.string import list_strings 9#import abipy.tools.decorators as dec 10 11 12#import logging 13#logger = logging.getLogger(__name__) 14 15 16__all__ = [ 17 "path_img", 18 "makeAboutBox", 19 "verbose", 20 "Panel", 21 "Frame", 22 "get_width_height", 23] 24 25_DEBUG = True 26_DEBUG = False 27 28 29#if _DEBUG: 30# verbose = dec.verbose 31# 32#else: 33def verbose(func): 34 return func 35 36 37#class Error(Exception): 38# """Base class for exceptions raised by awx library""" 39 40 41def path_img(filename): 42 """Returns the absolute path of an image.""" 43 dirname = os.path.dirname(__file__) 44 return os.path.join(dirname, "images", filename) 45 46 47def makeAboutBox(codename, version, description, developers, website=None, icon_path=None): 48 49 licence = """%(codename)s is free software; you can redistribute 50it and/or modify it under the terms of the GNU General Public License as 51published by the Free Software Foundation; either version 2 of the License, 52or (at your option) any later version. 53 54%(codename)s is distributed in the hope that it will be useful, 55but WITHOUT ANY WARRANTY; without even the implied warranty of 56MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 57See the GNU General Public License for more details. You should have 58received a copy of the GNU General Public License along with the code; 59if not, write to the Free Software Foundation, Inc., 59 Temple Place, 60Suite 330, Boston, MA 02111-1307 USA""" % {"codename": codename} 61 62 # Make a template for the description 63 #desc = "\n".join(["\nwxPython Cookbook Chapter 5\n", 64 # "Platform Info: (%s,%s)", 65 # "License: Public Domain"]) 66 67 ## Get the platform information 68 #py_version = [sys.platform, ", python ", sys.version.split()[0]] 69 #platform = list(wx.PlatformInfo[1:]) 70 #platform[0] += (" " + wx.VERSION_STRING) 71 #wx_info = ", ".join(platform) 72 73 #info.SetDescription(desc % (py_version, wx_info)) 74 75 info = wx.AboutDialogInfo() 76 77 if icon_path is not None: 78 info.SetIcon(wx.Icon(icon_path, wx.BITMAP_TYPE_PNG)) 79 80 info.SetName(codename) 81 info.SetVersion(version) 82 info.SetDescription(description) 83 info.SetCopyright('(C) Abipy group') 84 85 if website is not None: 86 info.SetWebSite(website) 87 88 info.SetLicence(licence) 89 90 for dev in list_strings(developers): 91 info.AddDeveloper(dev) 92 93 wx.AboutBox(info) 94 95 96def get_width_height(window, string, pads=(10, 10)): 97 """ 98 Returns the width and the height (in pixels) of a string used in window. 99 Returns are padded with pads 100 101 See http://docs.wxwidgets.org/2.8/wx_wxdc.html#wxdcgettextextent 102 """ 103 f = window.GetFont() 104 dc = wx.WindowDC(window) 105 dc.SetFont(f) 106 width, height = dc.GetTextExtent(string) 107 return width + pads[0], height + pads[1] 108 109 110class MyWindow(object): 111 """ 112 Mixin class providing helper functions and commonly used callbacks. 113 114 Attributes: 115 HELP_MSG: 116 string with a short help (will be displayed in MessageDialog in onHelp callback) 117 """ 118 HELP_MSG = "No help available" 119 120 def getParentWithType(self, cls): 121 """ 122 Returns the first parent window of type cls. 123 124 Raises: 125 RuntimeError if we have reached the head of the linked list. 126 """ 127 parent = self.GetParent() 128 129 while True: 130 if parent is None: 131 raise RuntimeError("Cannot find parent with class %s, reached None parent!" % cls) 132 133 if isinstance(parent, cls): 134 return parent 135 else: 136 parent = parent.GetParent() 137 138 def onHelp(self, event): 139 """Short help.""" 140 dialog = wx.MessageDialog(self, self.HELP_MSG, " Quick Reference", wx.OK | wx.ICON_INFORMATION) 141 dialog.ShowModal() 142 dialog.Destroy() 143 144 145class Panel(wx.Panel, MyWindow): 146 def __init__(self, parent, *args, **kwargs): 147 super(Panel, self).__init__(parent, *args, **kwargs) 148 149 150class Frame(wx.Frame, MyWindow): 151 """Base class for frames.""" 152 153 def __init__(self, parent, *args, **kwargs): 154 if "title" not in kwargs: 155 kwargs["title"] = self.__class__.__name__ 156 157 super(Frame, self).__init__(parent, *args, **kwargs) 158