From 9bf1c718f6f66dd4871ab3955beed5b15713444a Mon Sep 17 00:00:00 2001 From: Zsolt Foldvari Date: Wed, 23 Apr 2008 13:34:13 +0000 Subject: [PATCH] Move STYLE_TYPE and STYLE_DEFAULT constants from StyledTextBuffer to StyledTextTagType. svn: r10629 --- src/Editors/_StyledTextBuffer.py | 55 ++++++++++---------------------- src/Editors/_StyledTextEditor.py | 13 ++++---- src/gen/lib/styledtexttagtype.py | 22 +++++++++++++ 3 files changed, 46 insertions(+), 44 deletions(-) diff --git a/src/Editors/_StyledTextBuffer.py b/src/Editors/_StyledTextBuffer.py index 427a94687..a3d2b317d 100644 --- a/src/Editors/_StyledTextBuffer.py +++ b/src/Editors/_StyledTextBuffer.py @@ -57,28 +57,6 @@ from gen.lib import (StyledText, StyledTextTag, StyledTextTagType) # FIXME ALLOWED_STYLES = [i for (i, s, e) in StyledTextTagType._DATAMAP] -STYLE_TYPE = { - StyledTextTagType.BOLD: bool, - StyledTextTagType.ITALIC: bool, - StyledTextTagType.UNDERLINE: bool, - StyledTextTagType.FONTCOLOR: str, - StyledTextTagType.HIGHLIGHT: str, - StyledTextTagType.FONTFACE: str, - StyledTextTagType.FONTSIZE: int, - StyledTextTagType.SUPERSCRIPT: bool, -} - -STYLE_DEFAULT = { - StyledTextTagType.BOLD: False, - StyledTextTagType.ITALIC: False, - StyledTextTagType.UNDERLINE: False, - StyledTextTagType.FONTCOLOR: '#000000', - StyledTextTagType.HIGHLIGHT: '#FFFFFF', - StyledTextTagType.FONTFACE: 'Sans', - StyledTextTagType.FONTSIZE: 10, - StyledTextTagType.SUPERSCRIPT: False, -} - STYLE_TO_PROPERTY = { StyledTextTagType.BOLD: 'weight', # permanent tag is used instead StyledTextTagType.ITALIC: 'style', # permanent tag is used instead @@ -90,7 +68,6 @@ STYLE_TO_PROPERTY = { StyledTextTagType.SUPERSCRIPT: 'rise', } - (MATCH_START, MATCH_END, MATCH_FLAVOR, @@ -265,7 +242,7 @@ class StyledTextBuffer(gtk.TextBuffer): ## 1. are used to format inserted characters (self.after_insert_text) ## 2. are set each time the Insert marker is set (self.do_mark_set) ## 3. are set when a style is set (self._apply_style_to_selection) - self.style_state = STYLE_DEFAULT.copy() + self.style_state = StyledTextTagType.STYLE_DEFAULT.copy() # internally used attribute self._insert = self.get_insert() @@ -308,7 +285,7 @@ class StyledTextBuffer(gtk.TextBuffer): # apply active formats for the inserted text for style in ALLOWED_STYLES: value = self.style_state[style] - if value and (value != STYLE_DEFAULT[style]): + if value and (value != StyledTextTagType.STYLE_DEFAULT[style]): self.apply_tag(self._find_tag_by_name(style, value), insert_start, iter) @@ -353,14 +330,14 @@ class StyledTextBuffer(gtk.TextBuffer): changed_styles = {} for style in ALLOWED_STYLES: - if STYLE_TYPE[style] == bool: + if StyledTextTagType.STYLE_TYPE[style] == bool: value = str(style) in tag_names else: - value = STYLE_DEFAULT[style] + value = StyledTextTagType.STYLE_DEFAULT[style] for tname in tag_names: if tname.startswith(str(style)): value = tname.split(' ', 1)[1] - value = STYLE_TYPE[style](value) + value = StyledTextTagType.STYLE_TYPE[style](value) if self.style_state[style] != value: changed_styles[style] = value @@ -416,18 +393,18 @@ class StyledTextBuffer(gtk.TextBuffer): def _apply_style_to_selection(self, style, value): # FIXME can this be unified? - if STYLE_TYPE[style] == bool: + if StyledTextTagType.STYLE_TYPE[style] == bool: start, end = self._get_selection() if value: self.apply_tag_by_name(str(style), start, end) else: self.remove_tag_by_name(str(style), start, end) - elif STYLE_TYPE[style] == str: + elif StyledTextTagType.STYLE_TYPE[style] == str: tag = self._find_tag_by_name(style, value) self._remove_style_from_selection(style) self._apply_tag_to_selection(tag) - elif STYLE_TYPE[style] == int: + elif StyledTextTagType.STYLE_TYPE[style] == int: tag = self._find_tag_by_name(style, value) self._remove_style_from_selection(style) self._apply_tag_to_selection(tag) @@ -488,11 +465,11 @@ class StyledTextBuffer(gtk.TextBuffer): If TextTag does not exist yet, it is created. """ - if STYLE_TYPE[style] == bool: + if StyledTextTagType.STYLE_TYPE[style] == bool: tag_name = str(style) - elif STYLE_TYPE[style] == str: + elif StyledTextTagType.STYLE_TYPE[style] == str: tag_name = "%d %s" % (style, value) - elif STYLE_TYPE[style] == int: + elif StyledTextTagType.STYLE_TYPE[style] == int: tag_name = "%d %d" % (style, value) else: raise ValueError("Unknown style (%s) value type: %s" % @@ -501,7 +478,7 @@ class StyledTextBuffer(gtk.TextBuffer): tag = self.get_tag_table().lookup(tag_name) if not tag: - if STYLE_TYPE[style] != bool: + if StyledTextTagType.STYLE_TYPE[style] != bool: # bool style tags are not created here, but in constuctor tag = self.create_tag(tag_name) tag.set_property(STYLE_TO_PROPERTY[style], value) @@ -554,7 +531,8 @@ class StyledTextBuffer(gtk.TextBuffer): if len(style_and_value) == 1: s_value = None else: - s_value = STYLE_TYPE[style](style_and_value[1]) + s_value = StyledTextTagType.STYLE_TYPE[style]\ + (style_and_value[1]) if style in ALLOWED_STYLES: s_ranges = [(start, end+1) for (start, end) in g_ranges] @@ -575,9 +553,10 @@ class StyledTextBuffer(gtk.TextBuffer): @type value: depends on the I{style} type """ - if not isinstance(value, STYLE_TYPE[style]): + if not isinstance(value, StyledTextTagType.STYLE_TYPE[style]): raise TypeError("Style (%d) value must be %s and not %s" % - (style, STYLE_TYPE[style], value.__class__)) + (style, StyledTextTagType.STYLE_TYPE[style], + value.__class__)) self._apply_style_to_selection(style, value) diff --git a/src/Editors/_StyledTextEditor.py b/src/Editors/_StyledTextEditor.py index 4765a5bba..a24d38c70 100644 --- a/src/Editors/_StyledTextEditor.py +++ b/src/Editors/_StyledTextEditor.py @@ -48,7 +48,6 @@ from pango import UNDERLINE_SINGLE #------------------------------------------------------------------------- from gen.lib import StyledTextTagType from Editors._StyledTextBuffer import (StyledTextBuffer, ALLOWED_STYLES, - STYLE_TYPE, STYLE_DEFAULT, MATCH_START, MATCH_END, MATCH_FLAVOR, MATCH_STRING) from Spell import Spell @@ -482,7 +481,7 @@ class StyledTextEditor(gtk.TextView): if self._internal_style_change: return - value = STYLE_TYPE[style](combobox.get_active_text()) + value = StyledTextTagType.STYLE_TYPE[style](combobox.get_active_text()) _LOG.debug("applying style '%d' with value '%s'" % (style, str(value))) self.textbuffer.apply_style(style, value) @@ -513,8 +512,8 @@ class StyledTextEditor(gtk.TextView): model = combo.get_model() iter = model.get_iter_first() while iter: - if (STYLE_TYPE[style](model.get_value(iter, 0)) == - changed_styles[style]): + if (StyledTextTagType.STYLE_TYPE[style]( + model.get_value(iter, 0)) == changed_styles[style]): break iter = model.iter_next(iter) @@ -685,7 +684,8 @@ def set_fontface_toolitem(combotoolitem, callback): fontface.append_text(family) try: - default = families.index(STYLE_DEFAULT[StyledTextTagType.FONTFACE]) + def_fam = StyledTextTagType.STYLE_DEFAULT[StyledTextTagType.FONTFACE] + default = families.index(def_fam) except ValueError: default = 0 fontface.set_active(default) @@ -702,7 +702,8 @@ def set_fontsize_toolitem(combotoolitem, callback): fontsize.append_text(str(size)) try: - default = FONT_SIZES.index(STYLE_DEFAULT[StyledTextTagType.FONTSIZE]) + def_size = StyledTextTagType.STYLE_DEFAULT[StyledTextTagType.FONTSIZE] + default = FONT_SIZES.index(def_size) except ValueError: default = 0 fontsize.set_active(default) diff --git a/src/gen/lib/styledtexttagtype.py b/src/gen/lib/styledtexttagtype.py index a02fe5024..2e0b94984 100644 --- a/src/gen/lib/styledtexttagtype.py +++ b/src/gen/lib/styledtexttagtype.py @@ -70,6 +70,28 @@ class StyledTextTagType(GrampsType): (HIGHLIGHT, _("Highlight"), "highlight"), (SUPERSCRIPT, _("Superscript"), "superscript"), ] + + STYLE_TYPE = { + BOLD: bool, + ITALIC: bool, + UNDERLINE: bool, + FONTCOLOR: str, + HIGHLIGHT: str, + FONTFACE: str, + FONTSIZE: int, + SUPERSCRIPT: bool, + } + STYLE_DEFAULT = { + BOLD: False, + ITALIC: False, + UNDERLINE: False, + FONTCOLOR: '#000000', + HIGHLIGHT: '#FFFFFF', + FONTFACE: 'Sans', + FONTSIZE: 10, + SUPERSCRIPT: False, + } + def __init__(self, value=None): GrampsType.__init__(self, value)