1 | ## $Id: theming.py 7459 2012-01-12 16:19:57Z henrik $ |
---|
2 | ## |
---|
3 | ## Copyright (C) 2011 Uli Fouquet & Henrik Bettermann |
---|
4 | ## This program is free software; you can redistribute it and/or modify |
---|
5 | ## it under the terms of the GNU General Public License as published by |
---|
6 | ## the Free Software Foundation; either version 2 of the License, or |
---|
7 | ## (at your option) any later version. |
---|
8 | ## |
---|
9 | ## This program is distributed in the hope that it will be useful, |
---|
10 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
11 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
12 | ## GNU General Public License for more details. |
---|
13 | ## |
---|
14 | ## You should have received a copy of the GNU General Public License |
---|
15 | ## along with this program; if not, write to the Free Software |
---|
16 | ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
17 | ## |
---|
18 | """ |
---|
19 | Components to support themes. |
---|
20 | """ |
---|
21 | import grok |
---|
22 | from zope.component import getUtilitiesFor |
---|
23 | from zope.interface import Interface |
---|
24 | from zope.schema.interfaces import IVocabularyFactory |
---|
25 | from waeup.sirp.interfaces import SimpleSIRPVocabulary |
---|
26 | from waeup.sirp.browser.interfaces import ITheme |
---|
27 | from waeup.sirp.browser.resources import ( |
---|
28 | waeuptheme_empty, waeup_base_css, |
---|
29 | ) |
---|
30 | |
---|
31 | class SIRPThemeBase(grok.GlobalUtility): |
---|
32 | grok.implements(ITheme) |
---|
33 | grok.name('base waeup theme') |
---|
34 | |
---|
35 | description = u"Base Theme" |
---|
36 | |
---|
37 | def getResources(self): |
---|
38 | return [waeup_base_css] |
---|
39 | |
---|
40 | #class SIRPThemeRed1(grok.GlobalUtility): |
---|
41 | # grok.implements(ITheme) |
---|
42 | # grok.name('red waeup theme') |
---|
43 | |
---|
44 | # description = u"Uli's Red Theme" |
---|
45 | |
---|
46 | # def getResources(self): |
---|
47 | # return [waeuptheme_red1] |
---|
48 | |
---|
49 | #class SIRPThemeGray1(grok.GlobalUtility): |
---|
50 | # grok.implements(ITheme) |
---|
51 | # grok.name('gray waeup theme') |
---|
52 | |
---|
53 | # description = u"Henrik's Gray Theme" |
---|
54 | |
---|
55 | # def getResources(self): |
---|
56 | # return [waeuptheme_gray1] |
---|
57 | |
---|
58 | class SIRPThemeEmpty(grok.GlobalUtility): |
---|
59 | """A theme based on jQuery only. |
---|
60 | |
---|
61 | XXX: crappy yet. |
---|
62 | """ |
---|
63 | grok.implements(ITheme) |
---|
64 | grok.name('empty theme') |
---|
65 | description = u'Empty Theme' |
---|
66 | |
---|
67 | def getResources(self): |
---|
68 | return [waeuptheme_empty] |
---|
69 | |
---|
70 | def get_all_themes(): |
---|
71 | """Return all themes registered as `ITheme` somewhere. |
---|
72 | |
---|
73 | Returns a list of ``(<NAME>, <THEME>)`` tuples where ``<NAME>`` is |
---|
74 | the internal name under which a theme was registered and |
---|
75 | ``<THEME>`` is the real theme implementing `ITheme`. |
---|
76 | """ |
---|
77 | for name, theme in getUtilitiesFor(ITheme): |
---|
78 | yield name, theme |
---|
79 | |
---|
80 | class ThemesVocabulary(grok.GlobalUtility): |
---|
81 | """A vocabulary that provides all themes available. |
---|
82 | |
---|
83 | A named global utility implementing |
---|
84 | |
---|
85 | :class:`zope.schema.interfaces.IVocabularyFactory` |
---|
86 | |
---|
87 | and registered under the name |
---|
88 | |
---|
89 | 'waeup.sirp.browser.theming.ThemesVocabulary' |
---|
90 | |
---|
91 | Interface fields that wish to provide a list of available themes |
---|
92 | can require a 'named vocabulary', i.e. set: |
---|
93 | |
---|
94 | vocabulary = 'waeup.sirp.browser.theming.ThemesVocabulary' |
---|
95 | |
---|
96 | and the vocabulary will deliver themes and their descriptive text |
---|
97 | for instance in select boxes of forms. |
---|
98 | |
---|
99 | The name is chosen so that the location of this vocabulary can be |
---|
100 | found easily when used in an interface definition. |
---|
101 | """ |
---|
102 | grok.implements(IVocabularyFactory) |
---|
103 | grok.name('waeup.sirp.browser.theming.ThemesVocabulary') |
---|
104 | |
---|
105 | def __call__(self, context): |
---|
106 | """Deliver a vocabulary of available themes. |
---|
107 | |
---|
108 | The `description`s of available themes are used as 'title' and |
---|
109 | the utility names delivering the different themes as 'value' |
---|
110 | and 'token'. |
---|
111 | """ |
---|
112 | terms = [(theme.description, name) |
---|
113 | for name, theme in get_all_themes()] |
---|
114 | vocab = SimpleSIRPVocabulary(*terms) |
---|
115 | return vocab |
---|