Move STYLE_TYPE and STYLE_DEFAULT constants from StyledTextBuffer to StyledTextTagType.
svn: r10629
This commit is contained in:
@@ -57,28 +57,6 @@ from gen.lib import (StyledText, StyledTextTag, StyledTextTagType)
|
|||||||
# FIXME
|
# FIXME
|
||||||
ALLOWED_STYLES = [i for (i, s, e) in StyledTextTagType._DATAMAP]
|
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 = {
|
STYLE_TO_PROPERTY = {
|
||||||
StyledTextTagType.BOLD: 'weight', # permanent tag is used instead
|
StyledTextTagType.BOLD: 'weight', # permanent tag is used instead
|
||||||
StyledTextTagType.ITALIC: 'style', # permanent tag is used instead
|
StyledTextTagType.ITALIC: 'style', # permanent tag is used instead
|
||||||
@@ -90,7 +68,6 @@ STYLE_TO_PROPERTY = {
|
|||||||
StyledTextTagType.SUPERSCRIPT: 'rise',
|
StyledTextTagType.SUPERSCRIPT: 'rise',
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
(MATCH_START,
|
(MATCH_START,
|
||||||
MATCH_END,
|
MATCH_END,
|
||||||
MATCH_FLAVOR,
|
MATCH_FLAVOR,
|
||||||
@@ -265,7 +242,7 @@ class StyledTextBuffer(gtk.TextBuffer):
|
|||||||
## 1. are used to format inserted characters (self.after_insert_text)
|
## 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)
|
## 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)
|
## 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
|
# internally used attribute
|
||||||
self._insert = self.get_insert()
|
self._insert = self.get_insert()
|
||||||
@@ -308,7 +285,7 @@ class StyledTextBuffer(gtk.TextBuffer):
|
|||||||
# apply active formats for the inserted text
|
# apply active formats for the inserted text
|
||||||
for style in ALLOWED_STYLES:
|
for style in ALLOWED_STYLES:
|
||||||
value = self.style_state[style]
|
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),
|
self.apply_tag(self._find_tag_by_name(style, value),
|
||||||
insert_start, iter)
|
insert_start, iter)
|
||||||
|
|
||||||
@@ -353,14 +330,14 @@ class StyledTextBuffer(gtk.TextBuffer):
|
|||||||
changed_styles = {}
|
changed_styles = {}
|
||||||
|
|
||||||
for style in ALLOWED_STYLES:
|
for style in ALLOWED_STYLES:
|
||||||
if STYLE_TYPE[style] == bool:
|
if StyledTextTagType.STYLE_TYPE[style] == bool:
|
||||||
value = str(style) in tag_names
|
value = str(style) in tag_names
|
||||||
else:
|
else:
|
||||||
value = STYLE_DEFAULT[style]
|
value = StyledTextTagType.STYLE_DEFAULT[style]
|
||||||
for tname in tag_names:
|
for tname in tag_names:
|
||||||
if tname.startswith(str(style)):
|
if tname.startswith(str(style)):
|
||||||
value = tname.split(' ', 1)[1]
|
value = tname.split(' ', 1)[1]
|
||||||
value = STYLE_TYPE[style](value)
|
value = StyledTextTagType.STYLE_TYPE[style](value)
|
||||||
|
|
||||||
if self.style_state[style] != value:
|
if self.style_state[style] != value:
|
||||||
changed_styles[style] = value
|
changed_styles[style] = value
|
||||||
@@ -416,18 +393,18 @@ class StyledTextBuffer(gtk.TextBuffer):
|
|||||||
|
|
||||||
def _apply_style_to_selection(self, style, value):
|
def _apply_style_to_selection(self, style, value):
|
||||||
# FIXME can this be unified?
|
# FIXME can this be unified?
|
||||||
if STYLE_TYPE[style] == bool:
|
if StyledTextTagType.STYLE_TYPE[style] == bool:
|
||||||
start, end = self._get_selection()
|
start, end = self._get_selection()
|
||||||
|
|
||||||
if value:
|
if value:
|
||||||
self.apply_tag_by_name(str(style), start, end)
|
self.apply_tag_by_name(str(style), start, end)
|
||||||
else:
|
else:
|
||||||
self.remove_tag_by_name(str(style), start, end)
|
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)
|
tag = self._find_tag_by_name(style, value)
|
||||||
self._remove_style_from_selection(style)
|
self._remove_style_from_selection(style)
|
||||||
self._apply_tag_to_selection(tag)
|
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)
|
tag = self._find_tag_by_name(style, value)
|
||||||
self._remove_style_from_selection(style)
|
self._remove_style_from_selection(style)
|
||||||
self._apply_tag_to_selection(tag)
|
self._apply_tag_to_selection(tag)
|
||||||
@@ -488,11 +465,11 @@ class StyledTextBuffer(gtk.TextBuffer):
|
|||||||
If TextTag does not exist yet, it is created.
|
If TextTag does not exist yet, it is created.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
if STYLE_TYPE[style] == bool:
|
if StyledTextTagType.STYLE_TYPE[style] == bool:
|
||||||
tag_name = str(style)
|
tag_name = str(style)
|
||||||
elif STYLE_TYPE[style] == str:
|
elif StyledTextTagType.STYLE_TYPE[style] == str:
|
||||||
tag_name = "%d %s" % (style, value)
|
tag_name = "%d %s" % (style, value)
|
||||||
elif STYLE_TYPE[style] == int:
|
elif StyledTextTagType.STYLE_TYPE[style] == int:
|
||||||
tag_name = "%d %d" % (style, value)
|
tag_name = "%d %d" % (style, value)
|
||||||
else:
|
else:
|
||||||
raise ValueError("Unknown style (%s) value type: %s" %
|
raise ValueError("Unknown style (%s) value type: %s" %
|
||||||
@@ -501,7 +478,7 @@ class StyledTextBuffer(gtk.TextBuffer):
|
|||||||
tag = self.get_tag_table().lookup(tag_name)
|
tag = self.get_tag_table().lookup(tag_name)
|
||||||
|
|
||||||
if not tag:
|
if not tag:
|
||||||
if STYLE_TYPE[style] != bool:
|
if StyledTextTagType.STYLE_TYPE[style] != bool:
|
||||||
# bool style tags are not created here, but in constuctor
|
# bool style tags are not created here, but in constuctor
|
||||||
tag = self.create_tag(tag_name)
|
tag = self.create_tag(tag_name)
|
||||||
tag.set_property(STYLE_TO_PROPERTY[style], value)
|
tag.set_property(STYLE_TO_PROPERTY[style], value)
|
||||||
@@ -554,7 +531,8 @@ class StyledTextBuffer(gtk.TextBuffer):
|
|||||||
if len(style_and_value) == 1:
|
if len(style_and_value) == 1:
|
||||||
s_value = None
|
s_value = None
|
||||||
else:
|
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:
|
if style in ALLOWED_STYLES:
|
||||||
s_ranges = [(start, end+1) for (start, end) in g_ranges]
|
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
|
@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" %
|
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)
|
self._apply_style_to_selection(style, value)
|
||||||
|
|
||||||
|
@@ -48,7 +48,6 @@ from pango import UNDERLINE_SINGLE
|
|||||||
#-------------------------------------------------------------------------
|
#-------------------------------------------------------------------------
|
||||||
from gen.lib import StyledTextTagType
|
from gen.lib import StyledTextTagType
|
||||||
from Editors._StyledTextBuffer import (StyledTextBuffer, ALLOWED_STYLES,
|
from Editors._StyledTextBuffer import (StyledTextBuffer, ALLOWED_STYLES,
|
||||||
STYLE_TYPE, STYLE_DEFAULT,
|
|
||||||
MATCH_START, MATCH_END,
|
MATCH_START, MATCH_END,
|
||||||
MATCH_FLAVOR, MATCH_STRING)
|
MATCH_FLAVOR, MATCH_STRING)
|
||||||
from Spell import Spell
|
from Spell import Spell
|
||||||
@@ -482,7 +481,7 @@ class StyledTextEditor(gtk.TextView):
|
|||||||
if self._internal_style_change:
|
if self._internal_style_change:
|
||||||
return
|
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)))
|
_LOG.debug("applying style '%d' with value '%s'" % (style, str(value)))
|
||||||
self.textbuffer.apply_style(style, value)
|
self.textbuffer.apply_style(style, value)
|
||||||
|
|
||||||
@@ -513,8 +512,8 @@ class StyledTextEditor(gtk.TextView):
|
|||||||
model = combo.get_model()
|
model = combo.get_model()
|
||||||
iter = model.get_iter_first()
|
iter = model.get_iter_first()
|
||||||
while iter:
|
while iter:
|
||||||
if (STYLE_TYPE[style](model.get_value(iter, 0)) ==
|
if (StyledTextTagType.STYLE_TYPE[style](
|
||||||
changed_styles[style]):
|
model.get_value(iter, 0)) == changed_styles[style]):
|
||||||
break
|
break
|
||||||
iter = model.iter_next(iter)
|
iter = model.iter_next(iter)
|
||||||
|
|
||||||
@@ -685,7 +684,8 @@ def set_fontface_toolitem(combotoolitem, callback):
|
|||||||
fontface.append_text(family)
|
fontface.append_text(family)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
default = families.index(STYLE_DEFAULT[StyledTextTagType.FONTFACE])
|
def_fam = StyledTextTagType.STYLE_DEFAULT[StyledTextTagType.FONTFACE]
|
||||||
|
default = families.index(def_fam)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
default = 0
|
default = 0
|
||||||
fontface.set_active(default)
|
fontface.set_active(default)
|
||||||
@@ -702,7 +702,8 @@ def set_fontsize_toolitem(combotoolitem, callback):
|
|||||||
fontsize.append_text(str(size))
|
fontsize.append_text(str(size))
|
||||||
|
|
||||||
try:
|
try:
|
||||||
default = FONT_SIZES.index(STYLE_DEFAULT[StyledTextTagType.FONTSIZE])
|
def_size = StyledTextTagType.STYLE_DEFAULT[StyledTextTagType.FONTSIZE]
|
||||||
|
default = FONT_SIZES.index(def_size)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
default = 0
|
default = 0
|
||||||
fontsize.set_active(default)
|
fontsize.set_active(default)
|
||||||
|
@@ -71,5 +71,27 @@ class StyledTextTagType(GrampsType):
|
|||||||
(SUPERSCRIPT, _("Superscript"), "superscript"),
|
(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):
|
def __init__(self, value=None):
|
||||||
GrampsType.__init__(self, value)
|
GrampsType.__init__(self, value)
|
||||||
|
Reference in New Issue
Block a user