changeset 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 c3aa71daceb8
children 091e288838e1
files cagou/core/common.py cagou/core/image.py cagou/kv/common.kv
diffstat 3 files changed, 45 insertions(+), 1 deletions(-) [+]
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)
--- a/cagou/core/image.py	Fri Apr 06 16:13:08 2018 +0200
+++ b/cagou/core/image.py	Fri Apr 06 16:13:08 2018 +0200
@@ -81,4 +81,3 @@
             self.source = self.source[7:]
         else:
             super(AsyncImage, self)._load_source(*args)
-
--- a/cagou/kv/common.kv	Fri Apr 06 16:13:08 2018 +0200
+++ b/cagou/kv/common.kv	Fri Apr 06 16:13:08 2018 +0200
@@ -37,3 +37,17 @@
         padding_x: dp(20)
 
 
+<Symbol>:
+    font_name: app.expand('{media}/fonts/fontello/font/fontello.ttf')
+    text_size: self.size
+    font_size: self.width - self.margin
+    margin: dp(5)
+    halign: 'center'
+    valign: 'middle'
+    bg_color: 1, 1, 1, 1
+    canvas.before:
+        Color:
+            rgba: self.bg_color
+        Rectangle:
+            pos: self.pos
+            size: self.size