~glitter-team/glitter/trunk

« back to all changes in this revision

Viewing changes to glitter/style.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__ = "Widget styles" 
 
9
 
 
10
import os
 
11
import sys
 
12
 
 
13
import gobject
 
14
 
 
15
class Style(gobject.GObject):
 
16
    """ 
 
17
    A style object for widgets in their various states.
 
18
    
 
19
    Styles are described in JSON (Java Script Object Notation, a subset of the 
 
20
    ECMA 262-3 standard)
 
21
    
 
22
    """
 
23
    
 
24
    def __init__(self, **kwargs):
 
25
        """ Initialize style 
 
26
        
 
27
        kwargs -- dict(**) A dictionary of properties
 
28
        
 
29
        """
 
30
        
 
31
        super(Style, self).__init__()
 
32
        
 
33
        self._properties = {}
 
34
        
 
35
        for key, value in kwargs.iteritems():
 
36
            setattr(self, key, value)
 
37
    
 
38
    def __setattr__(self, key, value):
 
39
        """ Sets a property """
 
40
        
 
41
        key = key.replace('-', '_')
 
42
        super(Style, self).__setattr__(key, value)
 
43
        
 
44
        if not key.startswith('_'):
 
45
            key = key.replace('_', '-') 
 
46
            self._properties[key] = value
 
47
        
 
48
    def __iter__(self):
 
49
        """ Iter properties """
 
50
        
 
51
        return self._properties.iteritems()
 
52
        
 
53
    def merge(self, style):
 
54
        """ Merges current style with given style by overwriting current 
 
55
        properties with properties from the given style, case different, and 
 
56
        adds new properties from it.
 
57
        
 
58
        style -- (Style) Style to be merged
 
59
        """
 
60
        
 
61
        new_style = Style(**self._properties)
 
62
        for key, value in style:
 
63
            setattr(new_style, key, value)
 
64
            
 
65
        return new_style
 
66
        
 
67
    def get_properties(self):
 
68
        """ Retrieve the property dictionary """
 
69
        
 
70
        return self._properties    
 
71