Package horizons :: Package gui :: Package widgets :: Module imagebutton
[hide private]
[frames] | no frames]

Source Code for Module horizons.gui.widgets.imagebutton

  1  # ################################################### 
  2  # Copyright (C) 2008-2017 The Unknown Horizons Team 
  3  # team@unknown-horizons.org 
  4  # This file is part of Unknown Horizons. 
  5  # 
  6  # Unknown Horizons is free software; you can redistribute it and/or modify 
  7  # it under the terms of the GNU General Public License as published by 
  8  # the Free Software Foundation; either version 2 of the License, or 
  9  # (at your option) any later version. 
 10  # 
 11  # This program is distributed in the hope that it will be useful, 
 12  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 13  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 14  # GNU General Public License for more details. 
 15  # 
 16  # You should have received a copy of the GNU General Public License 
 17  # along with this program; if not, write to the 
 18  # Free Software Foundation, Inc., 
 19  # 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA 
 20  # ################################################### 
 21   
 22  from fife import fife 
 23  from fife.extensions.pychan.widgets import Icon, ImageButton as FifeImageButton 
 24  from fife.extensions.pychan.widgets.common import Attr 
25 26 27 -class ImageButton(FifeImageButton):
28 """Extends ImageButton functionality by providing a new path= attribute. 29 Unless manually overridden, specifying path="path/to/file" (note: no .png 30 extension) will interpret that as these attributes: 31 32 up_image = "content/gui/path/to/file.png" 33 down_image = "content/gui/path/to/file_d.png" 34 hover_image = "content/gui/path/to/file_h.png" 35 inactive_image = "content/gui/path/to/file_bw.png" ("black/white") 36 37 Other possible names also checked are 38 up_image = "content/gui/path/to/file_u.png" ("up", for tabwidget) 39 inactive_image = "content/gui/path/to/file_gr.png" ("grayscale") 40 41 If some of those files could not be found, we use up_image instead. 42 Sets is_focusable to False unless overridden. 43 44 You can set the button active or inactive (only in code for now). 45 Setting to inactive will change *all* images (up, down and hover) to the 46 inactive image. If you set it active again, everything will be reset. 47 """ 48 ATTRIBUTES = FifeImageButton.ATTRIBUTES + [Attr('path'), Attr('inactive_image')] 49 IMAGE = "content/gui/{path}{{mode}}.png" 50 # These two constants are used to describe the toggle state of the widget. 51 ACTIVE = 0 52 INACTIVE = 1 53
54 - def __init__(self, path='', inactive_image=None, is_focusable=False, **kwargs):
55 super().__init__(is_focusable=is_focusable, **kwargs) 56 self.old_images = (None, None, None) 57 if path: 58 # initializing from python, not xml, so path is available here 59 # and should be set 60 self.path = path 61 if inactive_image: 62 self.inactive_image = inactive_image 63 self.state = self.ACTIVE
64
65 - def toggle(self):
66 if self.is_active: 67 self.set_inactive() 68 else: 69 self.set_active()
70
71 - def set_active(self):
72 """Sets the button active. Restores up, down and hover image to 73 previous state.""" 74 if self.is_active: 75 return 76 self.up_image, self.down_image, self.hover_image = self.old_images 77 self.state = self.ACTIVE
78
79 - def set_inactive(self):
80 """Sets the button inactive. Overrides up, down and hover image with 81 inactive image.""" 82 if not self.is_active: 83 # running this with inactive state would overwrite all elements 84 # of old_images with inactive_image 85 return 86 # store old images to be reloaded when button is set active again 87 self.old_images = (self.up_image, self.down_image, self.hover_image) 88 self.up_image = self.down_image = self.hover_image = self.inactive_image 89 self.state = self.INACTIVE
90 91 @property
92 - def is_active(self):
93 return (self.state == self.ACTIVE)
94
95 - def _get_path(self):
96 return self.__path
97
98 - def _set_path(self, path):
99 self.__path = path 100 image_path = self.IMAGE.format(path=path) 101 try: 102 self.up_image = image_path.format(mode='') 103 except fife.NotFound: 104 # NotFound: _[NotFound]_ , Something was searched, but not found 105 #TODO Temporarily try to find _u for the tabwidget 106 self.up_image = image_path.format(mode='_u') 107 try: 108 self.hover_image = image_path.format(mode='_h') 109 except fife.NotFound: 110 # By default, guichan/pychan will set hover_image to be the same as 111 # up_image even if it is not explicitly set here (the following line 112 # just reading `pass` instead of setting hover_image to up_image). 113 # This however is stored internally in a way that would segfault FIFE 114 # when trying to restore images from self.old_images that were set 115 # like that implicitly (see #2000). 116 self.hover_image = self.up_image 117 try: 118 self.down_image = image_path.format(mode='_d') 119 except fife.NotFound: 120 self.down_image = self.up_image 121 122 # Since inactive_image is no image attribute in pychan, it would 123 # not be validated upon setting self.inactive_image (which works 124 # for ImageButton.{up,down,hover}_image and Icon.image). 125 # Instead, we try to load an Icon with that image and manually 126 # set inactive_image to the path that worked, if there is any. 127 try: 128 image = image_path.format(mode='_bw') 129 Icon(image=image).hide() # hide will remove Icon from widgets of pychan.internals.manager 130 self.inactive_image = image 131 except fife.NotFound: 132 try: 133 image = image_path.format(mode='_gr') 134 Icon(image=image).hide() # hide will remove Icon from widgets of pychan.internals.manager 135 self.inactive_image = image 136 except fife.NotFound: 137 self.inactive_image = self.up_image
138 139 path = property(_get_path, _set_path)
140
141 142 -class OkButton(ImageButton):
143 """The OkButton is a shortcut for an ImageButton with our OK / apply icon. 144 Its default attributes are: 145 name="okButton" path="images/buttons/ok" 146 """ 147 DEFAULT_NAME = 'okButton' 148
149 - def __init__(self, name=None, **kwargs):
150 if name is None: 151 name = self.__class__.DEFAULT_NAME 152 size = (34, 40) 153 super().__init__( 154 name=name, is_focusable=False, 155 max_size=size, min_size=size, size=size, **kwargs) 156 self.path = "images/buttons/ok" 157 self.inactive_image = "content/gui/images/buttons/close.png"
158
159 160 -class CancelButton(ImageButton):
161 """The CancelButton is a shortcut for an ImageButton with our cancel / close 162 icon. Its default attributes are: 163 name="cancelButton" path="images/buttons/close" 164 """ 165 DEFAULT_NAME = 'cancelButton' 166
167 - def __init__(self, name=None, **kwargs):
168 if name is None: 169 name = self.__class__.DEFAULT_NAME 170 size = (34, 40) 171 super().__init__( 172 name=name, is_focusable=False, 173 max_size=size, min_size=size, size=size, **kwargs) 174 self.path = "images/buttons/close"
175
176 177 -class DeleteButton(ImageButton):
178 """The DeleteButton is a shortcut for an ImageButton with our delete / tear 179 icon. Its default attributes are: 180 name="deleteButton" path="images/buttons/delete" 181 """ 182 DEFAULT_NAME = 'deleteButton' 183
184 - def __init__(self, name=None, **kwargs):
185 if name is None: 186 name = self.__class__.DEFAULT_NAME 187 size = (34, 40) 188 super().__init__( 189 name=name, is_focusable=False, 190 max_size=size, min_size=size, size=size, **kwargs) 191 self.path = "images/buttons/delete"
192 213