From 799bb41fa85ad38eec300cc3423d0a5eb37d2c66 Mon Sep 17 00:00:00 2001 From: Aleksey Lim Date: Wed, 07 Mar 2012 01:56:43 +0000 Subject: Revert icons parser and cache removed in TextVew patch --- (limited to 'chat') diff --git a/chat/box.py b/chat/box.py index 6218092..b850c92 100644 --- a/chat/box.py +++ b/chat/box.py @@ -219,12 +219,12 @@ class TextBox(gtk.TextView): self.get_buffer().insert_with_tags(self.iter_text, word, tag, self.fg_tag) else: - smile_pxb = smilies.get_pixbuf(word) - if smile_pxb is not None: - self.get_buffer().insert_pixbuf(self.iter_text, smile_pxb) - else: - self.get_buffer().insert_with_tags(self.iter_text, word, - self.fg_tag) + for i in smilies.parse(word): + if isinstance(i, gtk.gdk.Pixbuf): + self.get_buffer().insert_pixbuf(self.iter_text, i) + else: + self.get_buffer().insert_with_tags(self.iter_text, i, + self.fg_tag) self.get_buffer().insert_with_tags(self.iter_text, ' ', self.fg_tag) diff --git a/chat/smilies.py b/chat/smilies.py index 4c41ac9..c3e4025 100644 --- a/chat/smilies.py +++ b/chat/smilies.py @@ -93,24 +93,39 @@ THEME = [ SMILIES_SIZE = int(style.STANDARD_ICON_SIZE * 0.75) -_catalog = None +_catalog = {} -def get_pixbuf(word): - """Return a pixbuf associated to a smile, or None if not available""" - for (name, __, codes) in THEME: - if word in codes: - return gtk.gdk.pixbuf_new_from_file(name) - return None +def parse(text): + """Parse text and find smiles. + :param text: + string to parse for smilies + :returns: + array of string parts and pixbufs -def init(): - """Initialise smilies data.""" - global _catalog + """ + result = [text] + + for smiley in sorted(_catalog.keys(), lambda x, y: cmp(len(y), len(x))): + new_result = [] + for word in result: + if isinstance(word, gtk.gdk.Pixbuf): + new_result.append(word) + else: + parts = word.split(smiley) + for i in parts[:-1]: + new_result.append(i) + new_result.append(_catalog[smiley]) + new_result.append(parts[-1]) + result = new_result + + return result - if _catalog is not None: + +def init(): + if _catalog: return - _catalog = {} png_dir = join(get_activity_root(), 'data', 'icons', 'smilies') svg_dir = join(get_bundle_path(), 'icons', 'smilies') @@ -120,16 +135,16 @@ def init(): for index, (name, hint, codes) in enumerate(THEME): png_path = join(png_dir, name + '.png') - - for i in codes: - _catalog[i] = png_path - THEME[index] = (png_path, hint, codes) - - if not exists(png_path): + if exists(png_path): + pixbuf = gtk.gdk.pixbuf_new_from_file(png_path) + else: pixbuf = _from_svg_at_size( join(svg_dir, name + '.svg'), SMILIES_SIZE, SMILIES_SIZE, None, True) pixbuf.save(png_path, 'png') + for i in codes: + _catalog[i] = pixbuf + THEME[index] = (png_path, hint, codes) def _from_svg_at_size(filename=None, width=None, height=None, handle=None, -- cgit v0.9.1