comparison engine/extensions/pychan/widgets/buttons.py @ 332:457e626296ba

Working on the XXX_image attributes to force consistent behaviour - this needs a double check. Failed to reproduce #355.
author phoku@33b003aa-7bff-0310-803a-e67f0ece8222
date Mon, 24 Aug 2009 13:23:03 +0000
parents 48c99636453e
children dfd48d49c044
comparison
equal deleted inserted replaced
331:48c99636453e 332:457e626296ba
22 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 22 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 # #################################################################### 23 # ####################################################################
24 24
25 from common import * 25 from common import *
26 from basictextwidget import BasicTextWidget 26 from basictextwidget import BasicTextWidget
27 from pychan.properties import ImageProperty
27 28
28 class Button(BasicTextWidget): 29 class Button(BasicTextWidget):
29 """ 30 """
30 A basic push button. 31 A basic push button.
31 """ 32 """
48 """ 49 """
49 50
50 ATTRIBUTES = BasicTextWidget.ATTRIBUTES + [Attr('up_image'),Attr('down_image'),PointAttr('offset'),Attr('hover_image')] 51 ATTRIBUTES = BasicTextWidget.ATTRIBUTES + [Attr('up_image'),Attr('down_image'),PointAttr('offset'),Attr('hover_image')]
51 52
52 def __init__(self,up_image="",down_image="",hover_image="",offset=(0,0),**kwargs): 53 def __init__(self,up_image="",down_image="",hover_image="",offset=(0,0),**kwargs):
53 self.real_widget = fife.TwoButton() 54 self.real_widget = kwargs.get("real_widget", fife.TwoButton())
54 super(ImageButton,self).__init__(**kwargs) 55 super(ImageButton,self).__init__(**kwargs)
55 56
56 self.up_image = up_image 57 self.up_image = up_image
57 self.down_image = down_image 58 self.down_image = down_image
58 self.hover_image = hover_image 59 self.hover_image = hover_image
59 self.offset = offset 60 self.offset = offset
60 61
61 def _setUpImage(self, source): 62 up_image = ImageProperty("UpImage")
62 if isinstance(source,str): 63 down_image = ImageProperty("DownImage")
63 self._upimage_source = source 64 hover_image = ImageProperty("HoverImage")
64 try:
65 self._upimage = get_manager().loadImage(source)
66 self.real_widget.setUpImage( self._upimage )
67 except:
68 self._upimage = DummyImage()
69 elif isinstance(source,fife.GuiImage):
70 self._upimage_source = None
71 self._upimage = source
72 self.real_widget.setUpImage( self._upimage )
73 else:
74 raise RuntimeError("Icon.image only accepts GuiImage and python strings, not '%s'" % repr(source))
75
76 # Set minimum size accoriding to image
77 self.min_size = self.real_widget.getWidth(),self.real_widget.getHeight()
78 self.size = self.max_size = self.min_size
79
80 def _getUpImage(self): return self._upimage_source
81 up_image = property(_getUpImage,_setUpImage)
82
83 def _setDownImage(self, source):
84 if isinstance(source,str):
85 self._downimage_source = source
86 try:
87 self._downimage = get_manager().loadImage(source)
88 self.real_widget.setDownImage( self._downimage )
89 except:
90 self._downimage = DummyImage()
91 elif isinstance(source,fife.GuiImage):
92 self._downimage_source = None
93 self._downimage = source
94 self.real_widget.setDownImage( self._downimage )
95 else:
96 raise RuntimeError("Icon.image only accepts GuiImage and python strings, not '%s'" % repr(source))
97
98 def _getDownImage(self): return self._downimage_source
99 down_image = property(_getDownImage,_setDownImage)
100
101 def _setHoverImage(self, source):
102 if isinstance(source, str):
103 self._hoverimage_source = source
104 try:
105 self._hoverimage = get_manager().loadImage(source)
106 self.real_widget.setHoverImage( self._hoverimage )
107 except:
108 self._hoverimage = DummyImage()
109 elif isinstance(source,fife.GuiImage):
110 self._hoverimage_source = None
111 self._hoverimage = source
112 self.real_widget.setHoverImage( self._hoverimage )
113 else:
114 raise RuntimeError("Icon.image only accepts GuiImage and python strings, not '%s'" % repr(source))
115
116 def _getHoverImage(self): return self._hoverimage_source
117 hover_image = property(_getHoverImage,_setHoverImage)
118 65
119 def _setOffset(self, offset): 66 def _setOffset(self, offset):
120 self.real_widget.setDownOffset(offset[0], offset[1]) 67 self.real_widget.setDownOffset(offset[0], offset[1])
121 def _getOffset(self): 68 def _getOffset(self):
122 return (self.real_widget.getDownXOffset(), self.real_widget.getDownYOffset()) 69 return (self.real_widget.getDownXOffset(), self.real_widget.getDownYOffset())
123 offset = property(_getOffset,_setOffset) 70 offset = property(_getOffset,_setOffset)
124 71
125 def resizeToContent(self, recurse=True): 72 def resizeToContent(self, recurse=True):
126 self.height = max(self._upimage.getHeight(),self._downimage.getHeight(),self._hoverimage.getHeight()) + self.margins[1]*2 73 th, tw = 0, 0
127 self.width = max(self._upimage.getWidth(),self._downimage.getWidth(),self._hoverimage.getWidth()) + self.margins[0]*2 74 if self.text:
75 th = self.real_font.getHeight()#+self.real_font.getSpacing()
76 tw = self.real_font.getWidth(text2gui(self.text))#+self.real_font.getSpacing()
77 self.height = max(
78 self._prop_upimage["image"].getHeight(),
79 self._prop_downimage["image"].getHeight(),
80 self._prop_hoverimage["image"].getHeight(),
81 th) + self.margins[1]*2
82 self.width = max(
83 self._prop_upimage["image"].getWidth(),
84 self._prop_downimage["image"].getWidth(),
85 self._prop_hoverimage["image"].getWidth(),
86 tw) + self.margins[0]*2
128 87
129 class ToggleButton(BasicTextWidget): 88 class ToggleButton(ImageButton):
130 """ 89 """
131 A basic push button that can be toggled. 90 A basic push button that can be toggled.
132 91
133 Unfortunately a bit of code duplication from ImageButton. 92 Unfortunately a bit of code duplication from ImageButton.
134 93
142 ATTRIBUTES = BasicTextWidget.ATTRIBUTES + [ 101 ATTRIBUTES = BasicTextWidget.ATTRIBUTES + [
143 Attr('up_image'),Attr('down_image'),Attr('hover_image'), 102 Attr('up_image'),Attr('down_image'),Attr('hover_image'),
144 PointAttr('offset'),Attr('group') 103 PointAttr('offset'),Attr('group')
145 ] 104 ]
146 105
147 def __init__(self,up_image="",down_image="",hover_image="",offset=(0,0),group="",**kwargs): 106 def __init__(self,group="",**kwargs):
148 107
149 self.real_widget = fife.ToggleButton() 108 super(ToggleButton,self).__init__(real_widget = fife.ToggleButton(), **kwargs)
150 super(ToggleButton,self).__init__(**kwargs)
151 self.group = group 109 self.group = group
152 self.up_image = up_image
153 self.down_image = down_image
154 self.hover_image = hover_image
155 self.offset = offset
156 110
157 def _setGroup(self,group): 111 def _setGroup(self,group):
158 self.real_widget.setGroup( group ) 112 self.real_widget.setGroup( group )
159 113
160 def _getGroup(self): 114 def _getGroup(self):
166 120
167 def _isToggled(self): 121 def _isToggled(self):
168 return self.real_widget.isToggled() 122 return self.real_widget.isToggled()
169 toggled = property(_isToggled, _setToggled) 123 toggled = property(_isToggled, _setToggled)
170 124
171 ###
172 # I didn't want to do this, but this is just cut and paste from the ImageButton class:
173 ###
174
175 def _setUpImage(self,image):
176 self._upimage_source = image
177 try:
178 self._upimage = get_manager().loadImage(image)
179 self.real_widget.setUpImage( self._upimage )
180 except:
181 self._upimage = DummyImage()
182 def _getUpImage(self): return self._upimage_source
183 up_image = property(_getUpImage,_setUpImage)
184
185 def _setDownImage(self,image):
186 self._downimage_source = image
187 try:
188 self._downimage = get_manager().loadImage(image)
189 self.real_widget.setDownImage( self._downimage )
190 except:
191 self._downimage = DummyImage()
192 def _getDownImage(self): return self._downimage_source
193 down_image = property(_getDownImage,_setDownImage)
194
195 def _setHoverImage(self,image):
196 self._hoverimage_source = image
197 try:
198 self._hoverimage = get_manager().loadImage(image)
199 self.real_widget.setHoverImage( self._hoverimage )
200 except:
201 self._hoverimage = DummyImage()
202 def _getHoverImage(self): return self._hoverimage_source
203 hover_image = property(_getHoverImage,_setHoverImage)
204
205 def _setOffset(self, offset):
206 self.real_widget.setDownOffset(offset[0], offset[1])
207 def _getOffset(self):
208 return (self.real_widget.getDownXOffset(), self.real_widget.getDownYOffset())
209 offset = property(_getOffset,_setOffset)
210
211 def resizeToContent(self, recurse=True): 125 def resizeToContent(self, recurse=True):
212 th = self.real_font.getHeight()+self.real_widget.getSpacing() 126 # NOTE: Figure out how the spacing comes into play
213 tw = self.real_font.getWidth(text2gui(self.text))+self.real_widget.getSpacing() 127 tw, th = 0, 0
214 self.height = max(self._upimage.getHeight(),self._downimage.getHeight(),self._hoverimage.getHeight(),th) + self.margins[1]*2 128 if self.text:
215 self.width = max(self._upimage.getWidth(),self._downimage.getWidth(),self._hoverimage.getWidth(),tw) + self.margins[0]*2 129 th = self.real_font.getHeight() + self.real_widget.getSpacing()
130 tw = self.real_font.getWidth(text2gui(self.text)) + self.real_widget.getSpacing()
131 self.height = max(
132 self._prop_upimage["image"].getHeight(),
133 self._prop_downimage["image"].getHeight(),
134 self._prop_hoverimage["image"].getHeight(),
135 th) + self.margins[1]*2
136 self.width = max(
137 self._prop_upimage["image"].getWidth(),
138 self._prop_downimage["image"].getWidth(),
139 self._prop_hoverimage["image"].getWidth(),
140 tw) + self.margins[0]*2