view engine/extensions/pychan/properties.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 fee958103d58
line wrap: on
line source

#!/usr/bin/env python
# -*- coding: utf-8 -*-

# ####################################################################
#  Copyright (C) 2005-2009 by the FIFE team
#  http://www.fifengine.de
#  This file is part of FIFE.
#
#  FIFE is free software; you can redistribute it and/or
#  modify it under the terms of the GNU Lesser General Public
#  License as published by the Free Software Foundation; either
#  version 2.1 of the License, or (at your option) any later version.
#
#  This library is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#  Lesser General Public License for more details.
#
#  You should have received a copy of the GNU Lesser General Public
#  License along with this library; if not, write to the
#  Free Software Foundation, Inc.,
#  51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
# ####################################################################

import fife
from exceptions import RuntimeError

def get_manager():
	import pychan
	return pychan.manager

"""
Property bindings
=================

This module contains a set of property bindings for
the widgets, factored out to de-clutter the Widget.

"""
class WrappedProperty(object):
	def __init__(self, name):
		self.name = name
	def _getSetter(self,obj):
		setter_name = 'set' + self.name
		return getattr(obj.real_widget,setter_name)
	def _getGetter(self,obj):
		getter_name = 'get' + self.name
		return getattr(obj.real_widget,getter_name)


class ColorProperty(WrappedProperty):
	"""
	A color property. Fakes a color attribute of a guichan widget.
	This accepts either tuples of the colors (r,g,b)
	or L{fife.Color} objects.

	Color objects have value semantics in this case.
	"""
	def __init__(self, name):
		super(ColorProperty, self).__init__(name)
	def __set__(self, obj, color):
		if isinstance(color, tuple):
			color = fife.Color(*color)
		else:
			# Force a copy to get value semantics
			color = fife.Color(color.r,color.g,color.b,color.a)
		self._getSetter(obj)(color)
	def __get__(self, obj, objtype = None):
		color = self._getGetter(obj)()
		return fife.Color(color.r,color.g,color.b,color.a)

class DummyImage(object):
	def getWidth(self): return 0
	def getHeight(self): return 0

class ImageProperty(WrappedProperty):
	def __init__(self, name):
		super(ImageProperty, self).__init__(name)
		self.prop_name = "_prop_" + self.name.lower()
	def __set__(self, obj, image):
		image_info = getattr(obj, self.prop_name, {})
		if not image:
			image_info["source"] = ""
			image_info["image"] = DummyImage()
			self._getSetter(obj)(None)

		elif isinstance(image, str):
			image_info["source"] = image
			# to catch or not to catch ...
			# we just let the NotFound exception trickle here.
			# depedning on complains we can catch and print a warning.
			image_info["image"] = get_manager().loadImage(image)
			self._getSetter(obj)(image_info["image"])

		elif isinstance(image,fife.GuiImage):
			image_info["source"] = None
			image_info["image"] = image
			self._getSetter(obj)(image_info["image"])
		else:
			attribute_name = "%s.%s" % (obj.__class__.__name__,self.name)
			error_message = "%s only accepts GuiImage and python strings, not '%s'"
			raise RuntimeError(error_message % (attribute_name, repr(source)))
		
		setattr(obj, self.prop_name, image_info)

	def __get__(self, obj, objtype = None):
		return getattr(obj, self.prop_name, {}).get("image",None)