2
# Enthought package imports
3
from enthought.traits.api import Event, HasTraits, Instance, List, Str, This, Trait
5
# Local relative imports
6
from stylesheets import Style, StyleSheet
9
class Stylable(HasTraits):
11
A mix-in class to give objects the ability to participate in Chaco's Style
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
20
# The parent stylable, or None
21
style_parent = Trait(None, None, This)
23
# The stylesheet that this component is using
24
stylesheet = Instance(StyleSheet, args=())
26
# The style for this component
27
style = Instance(Style, args=())
29
# The ID of this stylable object; this ID should be unique across the entire
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"
37
# This event is fired when our style changes, or when any of our ancestors'
41
#------------------------------------------------------------------------
43
#------------------------------------------------------------------------
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=())
50
cached_ancestors = List
52
#------------------------------------------------------------------------
54
#------------------------------------------------------------------------
56
def save_style(self, style_id=None, as_class=False):
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.
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
68
self.cached_ancestors = []
69
#print "updated ancestors for", self.name + ":", [a.name for a in self.cached_ancestors]
72
#------------------------------------------------------------------------
74
#------------------------------------------------------------------------
76
def _style_parent_changed(self, old, new):
77
self._cache_style_ancestors()
79
# Re-wire the event handlers
81
old.on_trait_event(self._parent_style_updated, "style_updated", remove=True)
83
new.on_trait_event(self._parent_style_updated, "style_updated")
85
# We have to do the same thing as if the parent style_updated had fired
86
self._parent_style_updated()
89
def _stylesheet_changed(self, old, new):
90
self.cached_style = self.stylesheet.query(self, self.cached_ancestors)
91
self.style_updated = True
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
101
self.cached_style = self.stylesheet.query(self, self.cached_ancestors)
102
self.style.set_dict(self.cached_style)
104
# Now that we have a valid, updated style, fire the event handler to
105
# notify our children.
106
self.style_updated = True
109
#------------------------------------------------------------------------
111
#------------------------------------------------------------------------
113
# TODO: add stuff here!