diff 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
line wrap: on
line diff
--- a/cagou/core/common.py	Fri Apr 06 16:13:08 2018 +0200
+++ b/cagou/core/common.py	Fri Apr 06 16:13:08 2018 +0200
@@ -19,10 +19,19 @@
 
 """common widgets, which can be reused everywhere"""
 
+from sat.core.i18n import _
 from kivy.uix.image import Image
+from kivy.uix.label import Label
 from kivy.uix.behaviors import ButtonBehavior
 from kivy.uix.boxlayout import BoxLayout
+from kivy import properties
 from cagou import G
+import json
+from sat.core import log as logging
+
+log = logging.getLogger(__name__)
+
+UNKNOWN_SYMBOL = u'Unknown symbol name'
 
 
 class IconButton(ButtonBehavior, Image):
@@ -44,3 +53,25 @@
             return ""
         else:
             return host.getAvatar(self.jid, profile=self.profile) or host.getDefaultAvatar(self.jid)
+
+
+class Symbol(Label):
+    symbol_map = None
+    symbol = properties.StringProperty()
+    margin = properties.NumericProperty(0)
+
+    def __init__(self, **kwargs):
+        if self.symbol_map is None:
+            with open(G.host.app.expand('{media}/fonts/fontello/config.json')) as f:
+                fontello_conf = json.load(f)
+            Symbol.symbol_map = {g['css']:g['code'] for g in fontello_conf['glyphs']}
+
+        super(Symbol, self).__init__(**kwargs)
+
+    def on_symbol(self, instance, symbol):
+        try:
+            code = self.symbol_map[symbol]
+        except KeyError:
+            log.warning(_(u"Invalid symbol {symbol}").format(symbol=symbol))
+        else:
+            self.text = unichr(code)