~glitter-team/glitter/trunk

« back to all changes in this revision

Viewing changes to glitter/theme.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__ = "Glitter theme" 
 
9
 
 
10
import os
 
11
import sys
 
12
 
 
13
import gobject
 
14
import simplejson
 
15
 
 
16
from style import Style
 
17
 
 
18
_DEFAULT_THEME = None
 
19
 
 
20
class Theme(gobject.GObject):
 
21
    """ 
 
22
    A Glitter theme holds the styles for individual widgets in their various 
 
23
    states, theme resources and other misc configuration data.
 
24
    
 
25
    """
 
26
    
 
27
    def __init__(self, name):
 
28
        """ Initialize theme """
 
29
    
 
30
        super(Theme, self).__init__()
 
31
        
 
32
        self.name = name
 
33
        self.theme_path = None
 
34
        
 
35
        self.widget_styles = {}
 
36
    
 
37
        self._init_common()
 
38
        self._init_styles()
 
39
    
 
40
    def _init_common(self):
 
41
        """ Common initialization procedures """
 
42
        
 
43
        theme_names = []
 
44
        themes_path = os.path.join(
 
45
                        os.path.dirname(os.path.abspath(__file__)),
 
46
                        'data',
 
47
                        'themes'
 
48
                      )
 
49
        for entry in os.listdir(themes_path):
 
50
            if os.path.isdir(os.path.join(themes_path, entry)):
 
51
                theme_names.append(entry)
 
52
        
 
53
        if self.name in theme_names:
 
54
            self.theme_path = os.path.join(
 
55
                                os.path.dirname(os.path.abspath(__file__)),
 
56
                                'data',
 
57
                                'themes',
 
58
                                self.name
 
59
                              )
 
60
        else:
 
61
            raise ValueError("Theme not found.")
 
62
            
 
63
    def _init_styles(self):
 
64
        """ Initialize widget styles """
 
65
        
 
66
        theme_styles_path = os.path.join(
 
67
                              os.path.dirname(os.path.abspath(__file__)),
 
68
                              'data',
 
69
                              'themes',
 
70
                              self.name,
 
71
                              'styles'
 
72
                            )
 
73
        # Parse widget styles from our JSON (ECMA 262-3) descriptions
 
74
        for entry in os.listdir(theme_styles_path):
 
75
            if os.path.isfile(os.path.join(theme_styles_path, entry)):
 
76
                json_file = os.path.join(theme_styles_path, entry)
 
77
                widget, ext = os.path.splitext(entry)
 
78
                if ext == '.json':
 
79
                    if not widget in self.widget_styles:
 
80
                        self.widget_styles[widget] = {}
 
81
                    json_file = open(json_file)
 
82
                    style_description = simplejson.load(json_file)
 
83
                    for state in style_description:
 
84
                        props = {}
 
85
                        for key in style_description[state]:
 
86
                            value = style_description[state][key]
 
87
                            if type(value) is list:
 
88
                                value = tuple(value)
 
89
                            props[str(key)] = value
 
90
                            
 
91
                        self.widget_styles[widget][state] = Style(**props)
 
92
                        
 
93
    def get_style(self, widget, state):
 
94
        """ Retrieve the style for given widget in given state """
 
95
  
 
96
        widget = widget.lower()
 
97
        widget_styles = self.widget_styles.get(widget)
 
98
        if not widget_styles:
 
99
            return
 
100
        style = widget_styles.get(state)
 
101
        if not style:
 
102
            return
 
103
        
 
104
        return style
 
105
        
 
106
    def get_path(self):
 
107
        """ Retrieve theme path """
 
108
        
 
109
        return self.theme_path
 
110
    
 
111
    @classmethod
 
112
    def get_default(cls):
 
113
        """ Retrieve default theme """
 
114
        
 
115
        global _DEFAULT_THEME
 
116
        
 
117
        if not _DEFAULT_THEME:
 
118
            default_theme_name = 'nublo'
 
119
            _DEFAULT_THEME = cls(default_theme_name)
 
120
        
 
121
        return _DEFAULT_THEME
 
122
        
 
123
    @staticmethod
 
124
    def set_default(theme):
 
125
        """ Sets default theme
 
126
        
 
127
        theme -- (glitter.Theme)
 
128
        """
 
129
        
 
130
        global _DEFAULT_THEME
 
131
        
 
132
        _DEFAULT_THEME = theme
 
133