~glitter-team/glitter/trunk

« back to all changes in this revision

Viewing changes to glitter/image.py

  • Committer: Jan Jokela
  • Date: 2008-12-10 22:18:59 UTC
  • Revision ID: janjokela@gmail.com-20081210221859-zxr2ut255a7xu15x
Hi, Glitter here

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
 
13
from theme import Theme
 
14
from container import Container
 
15
from pixbuf_image import PixbufImage
 
16
from svg_image import SVGImage
 
17
 
 
18
class Image(Container):
 
19
    """ Glitter image widget 
 
20
    
 
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.
 
26
    
 
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.
 
30
    """
 
31
 
 
32
    def __init__(self, source=None, source_scalable=True):
 
33
        """ Initialize image widget """
 
34
                
 
35
        super(Image, self).__init__()
 
36
        
 
37
        # Set property values
 
38
        self._source = source
 
39
        self._source_scalable = source_scalable
 
40
        
 
41
        self.texture = None
 
42
        
 
43
        self._update_style(self.style)
 
44
    
 
45
    def _update_style(self, props=None):
 
46
        """ Update style """
 
47
        
 
48
        super(Image, self)._update_style(props)
 
49
        
 
50
        if not props:
 
51
            return
 
52
            
 
53
        
 
54
    def get_source(self):
 
55
        """ Retrieves image source file uri 
 
56
        
 
57
        return (str) -- image source uri in standard uri format
 
58
        """
 
59
        
 
60
        return self._source
 
61
        
 
62
    def set_source(self, value):
 
63
        """ Sets image source uri 
 
64
        
 
65
        value (str) -- image source uri in standard uri format
 
66
        """
 
67
        
 
68
        self._source = value
 
69
        if self._source:
 
70
            if self.texture:
 
71
                self.remove(self.texture)
 
72
                del(self.texture)
 
73
                self.texture = None
 
74
            self._set_source()   
 
75
            self._update_layout()    
 
76
    
 
77
    source = property(get_source, set_source)
 
78
         
 
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) 
 
82
        
 
83
        return (bool)
 
84
        """
 
85
        
 
86
        return self._source_scalable
 
87
        
 
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) 
 
91
        
 
92
        value (bool)
 
93
        """
 
94
    
 
95
        self._source_scalable = value
 
96
        self._set_source()
 
97
        
 
98
    source_scalable = property(get_source_scalable, set_source_scalable)
 
99
        
 
100
    # Global
 
101
 
 
102
    def _update_layout(self):
 
103
        """ Update widget """
 
104
        
 
105
        super(Image, self)._update_layout()
 
106
 
 
107
        if not self.texture or self.source_scalable:
 
108
            self._set_source()
 
109
        else:
 
110
            width = self.get_width()
 
111
            height = self.get_height()
 
112
            self.texture.set_width(width)
 
113
            self.texture.set_height(height)
 
114
    
 
115
    # Private
 
116
     
 
117
    def _set_source(self):
 
118
        """ Set texture based on given source
 
119
        
 
120
        - current support for PNG, SVG and Cairo
 
121
        """
 
122
        
 
123
        width = self.get_width() 
 
124
        height = self.get_height()
 
125
        
 
126
        # Account for 0 width or height
 
127
        if width == 0 or height == 0:
 
128
            return
 
129
       
 
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():
 
134
            return    
 
135
          
 
136
        source = os.path.splitext(self.source)[0]
 
137
        source_ext = os.path.splitext(self.source)[1]
 
138
        
 
139
        theme = Theme.get_default()
 
140
        if source_ext == '.py':
 
141
            source_path = os.path.join(theme.get_path(), 'resources')
 
142
        else:
 
143
            source_path = os.path.join(theme.get_path(), 'resources')
 
144
        
 
145
        if os.path.exists(source_path):
 
146
            if source_ext == '.png':
 
147
                self.texture = PixbufImage(
 
148
                                 os.path.join(source_path, self.source)
 
149
                               )
 
150
                self.texture.set_width(width)
 
151
                self.texture.set_height(height)
 
152
            elif source_ext == '.svg':
 
153
                if self.texture:
 
154
                    self.remove(self.texture)
 
155
                    del(self.texture)
 
156
                self.texture = SVGImage(os.path.join(source_path, self.source), width, height)
 
157
            elif source_ext == '.py':
 
158
                if self.texture:
 
159
                    self.remove(self.texture)
 
160
                    del(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)
 
166
            self.texture.show()
 
167
        else: 
 
168
            raise AttributeError, "source doesn't exist: %s" % source_path
 
169