From 9a497b9264030e52054c2713996befa09ea15ae0 Mon Sep 17 00:00:00 2001 From: Tim G L Lyons Date: Thu, 2 Jun 2011 22:57:32 +0000 Subject: [PATCH] 3866 3976 3977: Two 'spell' entries; remove Enchant dependency; Gramps does spellcheck on/off menu, gtkspell does the different languages. Cleaned-up code to reflect fact that spellcheck can only be on or off (see 3866). svn: r17669 --- src/Spell.py | 59 +++++++++++++---------------- src/gui/widgets/styledtexteditor.py | 33 ++++++++-------- 2 files changed, 44 insertions(+), 48 deletions(-) diff --git a/src/Spell.py b/src/Spell.py index bfdbd0732..9323c8b2e 100644 --- a/src/Spell.py +++ b/src/Spell.py @@ -82,62 +82,57 @@ import config class Spell(object): """Attach a gtkspell instance to the passed TextView instance. """ - _spellcheck_options = {'off': _('Off')} - - if HAVE_GTKSPELL: - _spellcheck_options['on'] = _('On') + _spellcheck_options = {False: _('Off'), True: _('On') } def __init__(self, textview): self.textview = textview if HAVE_GTKSPELL and config.get('behavior.spellcheck'): - self.spellcheck = 'on' + self.spellcheck = True else: - self.spellcheck = 'off' + self.spellcheck = False - self._active_spellcheck = 'off' + self._previous_spellcheck = False self.__real_set_active_spellcheck(self.spellcheck) # Private - def __real_set_active_spellcheck(self, spellcheck_code): + def __real_set_active_spellcheck(self, next_spellcheck): """Set active spellcheck by its code.""" - if self._active_spellcheck == 'off': - if spellcheck_code == 'off': - return - else: - try: - gtkspell_spell = gtkspell.Spell(self.textview) - self._active_spellcheck = spellcheck_code - except: - import traceback - print traceback.print_exc() - # attaching the spellchecker will fail if - # the language does not exist - # and presumably if there is no dictionary - pass - else: - if spellcheck_code == 'on': - return - else: + if self._previous_spellcheck == next_spellcheck: + return + elif self._previous_spellcheck == False and next_spellcheck == True: + try: + gtkspell_spell = gtkspell.Spell(self.textview) + self._previous_spellcheck = next_spellcheck + except: + import traceback + print traceback.print_exc() + # attaching the spellchecker will fail if + # the language does not exist + # and presumably if there is no dictionary + pass + elif self._previous_spellcheck == True and next_spellcheck == False: gtkspell_spell = gtkspell.get_from_text_view(self.textview) gtkspell_spell.detach() - self._active_spellcheck = spellcheck_code - + self._previous_spellcheck = next_spellcheck + else: + assert False, "spellcheck flags are not boolean -- shouldn't get here" + # Public API - def get_all_spellchecks(self): + def get_spellcheck_options(self): """Get the list of installed spellcheck names.""" return self._spellcheck_options.values() - def set_active_spellcheck(self, spellcheck): + def set_spellcheck_state(self, spellcheck): """Set active spellcheck by it's name.""" for code, name in self._spellcheck_options.items(): if name == spellcheck: self.__real_set_active_spellcheck(code) return - def get_active_spellcheck(self): + def get_spellcheck_state(self): """Get the name of the active spellcheck.""" - return self._spellcheck_options[self._active_spellcheck] + return self._spellcheck_options[self._previous_spellcheck] diff --git a/src/gui/widgets/styledtexteditor.py b/src/gui/widgets/styledtexteditor.py index da9b6aab7..f91415fcc 100644 --- a/src/gui/widgets/styledtexteditor.py +++ b/src/gui/widgets/styledtexteditor.py @@ -57,7 +57,7 @@ from gui.widgets.undoablestyledbuffer import UndoableStyledBuffer from gui.widgets.valueaction import ValueAction from gui.widgets.toolcomboentry import ToolComboEntry from gui.widgets.springseparator import SpringSeparatorAction -from Spell import Spell +from Spell import Spell, HAVE_GTKSPELL from GrampsDisplay import url as display_url import config from constfunc import has_display @@ -364,10 +364,11 @@ class StyledTextEditor(gtk.TextView): """ # spell checker submenu - spell_menu = gtk.MenuItem(_('Spellcheck')) - spell_menu.set_submenu(self._create_spell_menu()) - spell_menu.show_all() - menu.prepend(spell_menu) + if HAVE_GTKSPELL: + spell_menu = gtk.MenuItem(_('Spellcheck')) + spell_menu.set_submenu(self._create_spell_menu()) + spell_menu.show_all() + menu.prepend(spell_menu) search_menu = gtk.MenuItem(_("Search selection on web")) search_menu.connect('activate', self.search_web) @@ -548,23 +549,23 @@ class StyledTextEditor(gtk.TextView): "[a-z0-9-]*(\\.[a-z0-9][a-z0-9-]*)+", MAIL) def _create_spell_menu(self): - """Create a menu with all the installed spellchecks. + """Create a menu with the spellcheck option Off and On. - It is called each time the popup menu is opened. Each spellcheck - forms a radio menu item, and the selected spellcheck is set as active. + It is called each time the popup menu is opened. Each option + forms a radio menu item, and the selected option is set as active. - @returns: menu containing all the installed spellchecks. + @returns: menu containing the spellcheck options. @returntype: gtk.Menu """ - active_spellcheck = self.spellcheck.get_active_spellcheck() + spellcheck_state = self.spellcheck.get_spellcheck_state() menu = gtk.Menu() group = None - for lang in self.spellcheck.get_all_spellchecks(): - menuitem = gtk.RadioMenuItem(group, lang) - menuitem.set_active(lang == active_spellcheck) - menuitem.connect('activate', self._spell_change_cb, lang) + for spellcheck in self.spellcheck.get_spellcheck_options(): + menuitem = gtk.RadioMenuItem(group, spellcheck) + menuitem.set_active(spellcheck == spellcheck_state) + menuitem.connect('activate', self._spell_change_cb, spellcheck) menu.append(menuitem) if group is None: @@ -706,8 +707,8 @@ class StyledTextEditor(gtk.TextView): self._internal_style_change = False def _spell_change_cb(self, menuitem, spellcheck): - """Set spell checker spellcheck according to user selection.""" - self.spellcheck.set_active_spellcheck(spellcheck) + """Set spell checker option according to user selection.""" + self.spellcheck.set_spellcheck_state(spellcheck) def _open_url_cb(self, menuitem, url, flavor): """Open the URL in a browser."""