~ubuntu-branches/ubuntu/utopic/python-chaco/utopic

« back to all changes in this revision

Viewing changes to enthought/chaco/attic/stylable.py

  • Committer: Bazaar Package Importer
  • Author(s): Varun Hiremath
  • Date: 2011-04-06 19:03:54 UTC
  • mfrom: (7.2.2 sid)
  • Revision ID: james.westby@ubuntu.com-20110406190354-rwd55l2ezjecfo41
Tags: 3.4.0-2
d/rules: fix pyshared directory path (Closes: #621116)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
 
2
 
# Enthought package imports
3
 
from enthought.traits.api import Event, HasTraits, Instance, List, Str, This, Trait
4
 
 
5
 
# Local relative imports
6
 
from stylesheets import Style, StyleSheet
7
 
 
8
 
 
9
 
class Stylable(HasTraits):
10
 
    """
11
 
    A mix-in class to give objects the ability to participate in Chaco's Style
12
 
    system.  
13
 
    
14
 
    The primary functions of the Stylable mix-in class is it:
15
 
        1. provide a mechanism for style lookup on parents
16
 
        2. cache styles information (automatically invalidated when the stylesheet changes)
17
 
        3. allow for objects to only 
18
 
    """
19
 
 
20
 
    # The parent stylable, or None
21
 
    style_parent = Trait(None, None, This)
22
 
 
23
 
    # The stylesheet that this component is using
24
 
    stylesheet = Instance(StyleSheet, args=())
25
 
    
26
 
    # The style for this component
27
 
    style = Instance(Style, args=())
28
 
 
29
 
    # The ID of this stylable object; this ID should be unique across the entire
30
 
    # style hierarchy.
31
 
    style_id = Str
32
 
    
33
 
    # The class name of this stylable object, e.g. axis, grid, box, text, etc.
34
 
    # This is defined at the class level and defaults to "component".
35
 
    style_class = "component"
36
 
 
37
 
    # This event is fired when our style changes, or when any of our ancestors'
38
 
    # styles change.
39
 
    style_updated = Event
40
 
    
41
 
    #------------------------------------------------------------------------
42
 
    # Protected traits
43
 
    #------------------------------------------------------------------------
44
 
    
45
 
    # The cached Style object from our previous query to the stylesheet.  Also
46
 
    # includes the parent's stylesheet merged in.  Local changes made to our
47
 
    # Style dict are not committed.
48
 
    cached_style = Instance(Style, args=())
49
 
 
50
 
    cached_ancestors = List
51
 
 
52
 
    #------------------------------------------------------------------------
53
 
    # Public methods
54
 
    #------------------------------------------------------------------------
55
 
 
56
 
    def save_style(self, style_id=None, as_class=False):
57
 
        """
58
 
        Saves our style to the stylesheet, using style_id as the identifier
59
 
        (or self.style_id if one is not provided).  Alternatively, if as_class
60
 
        is True, then saves to the stylesheet using self.style_class.
61
 
        """
62
 
        pass
63
 
 
64
 
    def _cache_style_ancestors(self):
65
 
        if self.style_parent is not None:
66
 
            self.cached_ancestors = [self.style_parent] + self.style_parent.cached_ancestors
67
 
        else:
68
 
            self.cached_ancestors = []
69
 
        #print "updated ancestors for", self.name + ":", [a.name for a in self.cached_ancestors]
70
 
        return
71
 
 
72
 
    #------------------------------------------------------------------------
73
 
    # Event handlers
74
 
    #------------------------------------------------------------------------
75
 
 
76
 
    def _style_parent_changed(self, old, new):
77
 
        self._cache_style_ancestors()
78
 
        
79
 
        # Re-wire the event handlers
80
 
        if old is not None:
81
 
            old.on_trait_event(self._parent_style_updated, "style_updated", remove=True)
82
 
        if new is not None:
83
 
            new.on_trait_event(self._parent_style_updated, "style_updated")
84
 
        
85
 
        # We have to do the same thing as if the parent style_updated had fired
86
 
        self._parent_style_updated()
87
 
        return
88
 
 
89
 
    def _stylesheet_changed(self, old, new):
90
 
        self.cached_style = self.stylesheet.query(self, self.cached_ancestors)
91
 
        self.style_updated = True
92
 
        return
93
 
 
94
 
    def _parent_style_updated(self):
95
 
        if self.style_parent is not None:
96
 
            self.set(stylesheet=self.style_parent.stylesheet, trait_change_notify=False)
97
 
            self.cached_style = Style(self.style_parent.cached_style)
98
 
            self.cached_style.update(self.stylesheet.query(self, self.cached_ancestors))
99
 
            print "cached style:", self.cached_style
100
 
        else:
101
 
            self.cached_style = self.stylesheet.query(self, self.cached_ancestors)
102
 
        self.style.set_dict(self.cached_style)
103
 
        
104
 
        # Now that we have a valid, updated style, fire the event handler to
105
 
        # notify our children.
106
 
        self.style_updated = True
107
 
        return
108
 
 
109
 
    #------------------------------------------------------------------------
110
 
    # Persistence etc.
111
 
    #------------------------------------------------------------------------
112
 
 
113
 
    # TODO: add stuff here!
114
 
 
115
 
 
116
 
 
117
 
# EOF