1
by Jan Jokela
Hi, Glitter here |
1 |
# !/usr/bin/python
|
2 |
# -*- coding: utf-8 -*-
|
|
3 |
||
4 |
# Glitter Toolkit
|
|
5 |
||
6 |
__authors__ = ["Jan Jokela <janjokela@gmail.com>"] |
|
7 |
__licenses__ = ["LICENSE.LGPL"] |
|
8 |
__description__ = "Image widget" |
|
9 |
||
10 |
import os |
|
11 |
import sys |
|
12 |
||
7.1.10
by Jan Jokela
(- glitter/pixbuf_image.py) Removed; (glitter/image.py) Support for png images working again |
13 |
import clutter |
14 |
||
1
by Jan Jokela
Hi, Glitter here |
15 |
from theme import Theme |
16 |
from container import Container |
|
17 |
from pixbuf_image import PixbufImage |
|
18 |
from svg_image import SVGImage |
|
19 |
||
20 |
class Image(Container): |
|
21 |
""" Glitter image widget
|
|
22 |
|
|
23 |
The Glitter Image widget derives from Container and represents an image.
|
|
24 |
Images can be bitmaps, cairo textures or scalable vector graphics. If the
|
|
25 |
source_scalable flag is enabled, the source will be handled as scalable,
|
|
26 |
this means that every time it changes size through the Container API, the
|
|
27 |
image will be recomputed to fit the new size.
|
|
28 |
|
|
29 |
* The source_scalable flag doesn't affect bitmaps or operations through the
|
|
30 |
clutter.Actor API (ex: scale, rotate, etc..) as to enable fast transforms
|
|
31 |
for things like transitions and effects.
|
|
32 |
"""
|
|
33 |
||
34 |
def __init__(self, source=None, source_scalable=True): |
|
35 |
""" Initialize image widget """
|
|
36 |
||
37 |
super(Image, self).__init__() |
|
38 |
||
39 |
# Set property values
|
|
40 |
self._source = source |
|
41 |
self._source_scalable = source_scalable |
|
42 |
||
43 |
self.texture = None |
|
44 |
||
45 |
self._update_style(self.style) |
|
46 |
||
47 |
def _update_style(self, props=None): |
|
48 |
""" Update style """
|
|
49 |
||
50 |
super(Image, self)._update_style(props) |
|
51 |
||
52 |
if not props: |
|
53 |
return
|
|
54 |
||
55 |
||
56 |
def get_source(self): |
|
57 |
""" Retrieves image source file uri
|
|
58 |
|
|
59 |
return (str) -- image source uri in standard uri format
|
|
60 |
"""
|
|
61 |
||
62 |
return self._source |
|
63 |
||
64 |
def set_source(self, value): |
|
65 |
""" Sets image source uri
|
|
66 |
|
|
67 |
value (str) -- image source uri in standard uri format
|
|
68 |
"""
|
|
69 |
||
70 |
self._source = value |
|
71 |
if self._source: |
|
72 |
if self.texture: |
|
73 |
self.remove(self.texture) |
|
74 |
del(self.texture) |
|
75 |
self.texture = None |
|
76 |
self._set_source() |
|
77 |
self._update_layout() |
|
78 |
||
79 |
source = property(get_source, set_source) |
|
80 |
||
81 |
def get_source_scalable(self): |
|
82 |
""" Retrieve whether image source file should be handled as a scalable
|
|
83 |
graphic (only applicable to SVGs and Cairo textures)
|
|
84 |
|
|
85 |
return (bool)
|
|
86 |
"""
|
|
87 |
||
88 |
return self._source_scalable |
|
89 |
||
90 |
def set_source_scalable(self, value): |
|
91 |
""" Sets wether image source file should be handled as a scalable
|
|
92 |
graphic (only applicable to SVGs and Cairo textures)
|
|
93 |
|
|
94 |
value (bool)
|
|
95 |
"""
|
|
96 |
||
97 |
self._source_scalable = value |
|
98 |
self._set_source() |
|
99 |
||
100 |
source_scalable = property(get_source_scalable, set_source_scalable) |
|
101 |
||
102 |
# Global
|
|
103 |
||
104 |
def _update_layout(self): |
|
105 |
""" Update widget """
|
|
106 |
||
107 |
super(Image, self)._update_layout() |
|
108 |
||
109 |
if not self.texture or self.source_scalable: |
|
110 |
self._set_source() |
|
111 |
else: |
|
112 |
width = self.get_width() |
|
113 |
height = self.get_height() |
|
114 |
self.texture.set_width(width) |
|
115 |
self.texture.set_height(height) |
|
116 |
||
117 |
# Private
|
|
118 |
||
119 |
def _set_source(self): |
|
120 |
""" Set texture based on given source
|
|
121 |
|
|
122 |
- current support for PNG, SVG and Cairo
|
|
123 |
"""
|
|
124 |
||
125 |
width = self.get_width() |
|
126 |
height = self.get_height() |
|
127 |
||
128 |
# Account for 0 width or height
|
|
129 |
if width == 0 or height == 0: |
|
130 |
return
|
|
131 |
||
132 |
# Account for no change in size
|
|
133 |
if self.texture and \ |
|
134 |
width == self.texture.get_width() and \ |
|
135 |
height == self.texture.get_height(): |
|
136 |
return
|
|
7.1.1
by Jan Jokela
(glitter/frame.py) Fixes for frame widget; (glitter/data/themes/*/styles/frame.json) Added; (glitter/tests/test_frame.py) Slight fixes, eliminated warnings |
137 |
|
7.1.10
by Jan Jokela
(- glitter/pixbuf_image.py) Removed; (glitter/image.py) Support for png images working again |
138 |
# Source filename and extension
|
139 |
source_name = os.path.splitext(self.source)[0] |
|
1
by Jan Jokela
Hi, Glitter here |
140 |
source_ext = os.path.splitext(self.source)[1] |
141 |
||
142 |
theme = Theme.get_default() |
|
143 |
if source_ext == '.py': |
|
144 |
source_path = os.path.join(theme.get_path(), 'resources') |
|
7.1.10
by Jan Jokela
(- glitter/pixbuf_image.py) Removed; (glitter/image.py) Support for png images working again |
145 |
elif os.path.exists(self.source): |
146 |
source_path = os.path.dirname(self.source) |
|
147 |
source = self.source |
|
1
by Jan Jokela
Hi, Glitter here |
148 |
else: |
149 |
source_path = os.path.join(theme.get_path(), 'resources') |
|
7.1.10
by Jan Jokela
(- glitter/pixbuf_image.py) Removed; (glitter/image.py) Support for png images working again |
150 |
source = os.path.join(source_path, self.source) |
1
by Jan Jokela
Hi, Glitter here |
151 |
|
152 |
if os.path.exists(source_path): |
|
153 |
if source_ext == '.png': |
|
7.1.10
by Jan Jokela
(- glitter/pixbuf_image.py) Removed; (glitter/image.py) Support for png images working again |
154 |
if not self.texture: |
155 |
self.texture = clutter.Texture(filename=source) |
|
156 |
self.add(self.texture) |
|
157 |
self.texture.show() |
|
1
by Jan Jokela
Hi, Glitter here |
158 |
self.texture.set_width(width) |
159 |
self.texture.set_height(height) |
|
160 |
elif source_ext == '.svg': |
|
161 |
if self.texture: |
|
162 |
self.remove(self.texture) |
|
163 |
del(self.texture) |
|
7.1.10
by Jan Jokela
(- glitter/pixbuf_image.py) Removed; (glitter/image.py) Support for png images working again |
164 |
self.texture = SVGImage(source, width, height) |
165 |
self.add(self.texture) |
|
166 |
self.texture.show() |
|
1
by Jan Jokela
Hi, Glitter here |
167 |
elif source_ext == '.py': |
168 |
if self.texture: |
|
169 |
self.remove(self.texture) |
|
170 |
del(self.texture) |
|
7.1.10
by Jan Jokela
(- glitter/pixbuf_image.py) Removed; (glitter/image.py) Support for png images working again |
171 |
source_import = os.path.basename(source_name) |
1
by Jan Jokela
Hi, Glitter here |
172 |
sys.path[1] = source_path |
173 |
cairotexture = __import__(source_import) |
|
174 |
self.texture = cairotexture.Texture(width, height) |
|
7.1.10
by Jan Jokela
(- glitter/pixbuf_image.py) Removed; (glitter/image.py) Support for png images working again |
175 |
self.add(self.texture) |
176 |
self.texture.show() |
|
1
by Jan Jokela
Hi, Glitter here |
177 |
else: |
178 |
raise AttributeError, "source doesn't exist: %s" % source_path |
|
179 |