2
# -*- coding: utf-8 -*-
6
__authors__ = ["Jan Jokela <janjokela@gmail.com>"]
7
__licenses__ = ["LICENSE.LGPL"]
8
__description__ = "Image widget"
13
from theme import Theme
14
from container import Container
15
from pixbuf_image import PixbufImage
16
from svg_image import SVGImage
18
class Image(Container):
19
""" Glitter image widget
21
The Glitter Image widget derives from Container and represents an image.
22
Images can be bitmaps, cairo textures or scalable vector graphics. If the
23
source_scalable flag is enabled, the source will be handled as scalable,
24
this means that every time it changes size through the Container API, the
25
image will be recomputed to fit the new size.
27
* The source_scalable flag doesn't affect bitmaps or operations through the
28
clutter.Actor API (ex: scale, rotate, etc..) as to enable fast transforms
29
for things like transitions and effects.
32
def __init__(self, source=None, source_scalable=True):
33
""" Initialize image widget """
35
super(Image, self).__init__()
39
self._source_scalable = source_scalable
43
self._update_style(self.style)
45
def _update_style(self, props=None):
48
super(Image, self)._update_style(props)
55
""" Retrieves image source file uri
57
return (str) -- image source uri in standard uri format
62
def set_source(self, value):
63
""" Sets image source uri
65
value (str) -- image source uri in standard uri format
71
self.remove(self.texture)
77
source = property(get_source, set_source)
79
def get_source_scalable(self):
80
""" Retrieve whether image source file should be handled as a scalable
81
graphic (only applicable to SVGs and Cairo textures)
86
return self._source_scalable
88
def set_source_scalable(self, value):
89
""" Sets wether image source file should be handled as a scalable
90
graphic (only applicable to SVGs and Cairo textures)
95
self._source_scalable = value
98
source_scalable = property(get_source_scalable, set_source_scalable)
102
def _update_layout(self):
103
""" Update widget """
105
super(Image, self)._update_layout()
107
if not self.texture or self.source_scalable:
110
width = self.get_width()
111
height = self.get_height()
112
self.texture.set_width(width)
113
self.texture.set_height(height)
117
def _set_source(self):
118
""" Set texture based on given source
120
- current support for PNG, SVG and Cairo
123
width = self.get_width()
124
height = self.get_height()
126
# Account for 0 width or height
127
if width == 0 or height == 0:
130
# Account for no change in size
131
if self.texture and \
132
width == self.texture.get_width() and \
133
height == self.texture.get_height():
136
source = os.path.splitext(self.source)[0]
137
source_ext = os.path.splitext(self.source)[1]
139
theme = Theme.get_default()
140
if source_ext == '.py':
141
source_path = os.path.join(theme.get_path(), 'resources')
143
source_path = os.path.join(theme.get_path(), 'resources')
145
if os.path.exists(source_path):
146
if source_ext == '.png':
147
self.texture = PixbufImage(
148
os.path.join(source_path, self.source)
150
self.texture.set_width(width)
151
self.texture.set_height(height)
152
elif source_ext == '.svg':
154
self.remove(self.texture)
156
self.texture = SVGImage(os.path.join(source_path, self.source), width, height)
157
elif source_ext == '.py':
159
self.remove(self.texture)
161
source_import = os.path.basename(source)
162
sys.path[1] = source_path
163
cairotexture = __import__(source_import)
164
self.texture = cairotexture.Texture(width, height)
165
self.add(self.texture)
168
raise AttributeError, "source doesn't exist: %s" % source_path