comparison cagou/core/common.py @ 134:1cca97e27a69

core (common): new Symbol widget: it uses font icon to display a symbol by name (should move to direct svg rendering once it's stable in Kivy). bg_color property allows to change background color. margin property allows to display a margin around the symbol
author Goffi <goffi@goffi.org>
date Fri, 06 Apr 2018 16:13:08 +0200
parents cd99f70ea592
children 091e288838e1
comparison
equal deleted inserted replaced
133:c3aa71daceb8 134:1cca97e27a69
17 # You should have received a copy of the GNU Affero General Public License 17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. 18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 19
20 """common widgets, which can be reused everywhere""" 20 """common widgets, which can be reused everywhere"""
21 21
22 from sat.core.i18n import _
22 from kivy.uix.image import Image 23 from kivy.uix.image import Image
24 from kivy.uix.label import Label
23 from kivy.uix.behaviors import ButtonBehavior 25 from kivy.uix.behaviors import ButtonBehavior
24 from kivy.uix.boxlayout import BoxLayout 26 from kivy.uix.boxlayout import BoxLayout
27 from kivy import properties
25 from cagou import G 28 from cagou import G
29 import json
30 from sat.core import log as logging
31
32 log = logging.getLogger(__name__)
33
34 UNKNOWN_SYMBOL = u'Unknown symbol name'
26 35
27 36
28 class IconButton(ButtonBehavior, Image): 37 class IconButton(ButtonBehavior, Image):
29 pass 38 pass
30 39
42 if host.contact_lists[self.profile].isRoom(self.jid.bare): 51 if host.contact_lists[self.profile].isRoom(self.jid.bare):
43 wid.opacity = 0 52 wid.opacity = 0
44 return "" 53 return ""
45 else: 54 else:
46 return host.getAvatar(self.jid, profile=self.profile) or host.getDefaultAvatar(self.jid) 55 return host.getAvatar(self.jid, profile=self.profile) or host.getDefaultAvatar(self.jid)
56
57
58 class Symbol(Label):
59 symbol_map = None
60 symbol = properties.StringProperty()
61 margin = properties.NumericProperty(0)
62
63 def __init__(self, **kwargs):
64 if self.symbol_map is None:
65 with open(G.host.app.expand('{media}/fonts/fontello/config.json')) as f:
66 fontello_conf = json.load(f)
67 Symbol.symbol_map = {g['css']:g['code'] for g in fontello_conf['glyphs']}
68
69 super(Symbol, self).__init__(**kwargs)
70
71 def on_symbol(self, instance, symbol):
72 try:
73 code = self.symbol_map[symbol]
74 except KeyError:
75 log.warning(_(u"Invalid symbol {symbol}").format(symbol=symbol))
76 else:
77 self.text = unichr(code)