7097: add UndoableStyledBuffer.undo_disabled

Refactoring away the coupling between Spell and StyleTextEditor
innards:

1) add a context manager UndoableStyledBuffer.undo_disabled
2) add a forwarding to it as StyleTextEditor.undo_disabled
3) use that in Spell.__real_set_active_spellcheck
This commit is contained in:
Vassilii Khachaturov 2014-02-19 12:42:22 +02:00
parent 78bcb52703
commit 283c1b1aa2
3 changed files with 27 additions and 6 deletions

View File

@ -2,6 +2,7 @@
# Gramps - a GTK+/GNOME based genealogy program
#
# Copyright (C) 2005-2006 Donald N. Allingham
# Copyright (C) 2014 Vassilii Khachaturov
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
@ -128,9 +129,8 @@ class Spell(object):
#other installed one will also be available
self.gtkspell_spell.set_language("en")
#if that fails no spellchecker will be available
self.textview.textbuffer.not_undoable_action = True
success = self.gtkspell_spell.attach(self.textview)
self.textview.textbuffer.not_undoable_action = False
with self.textview.undo_disabled():
success = self.gtkspell_spell.attach(self.textview)
try:
#show decoded language codes in the context menu
#requires the iso-codes package from http://pkg-isocodes.alioth.debian.org

View File

@ -178,6 +178,7 @@ class StyledTextEditor(Gtk.TextView):
def __init__(self):
"""Setup initial instance variable values."""
self.textbuffer = UndoableStyledBuffer()
self.undo_disabled = self.textbuffer.undo_disabled # see bug 7097
self.textbuffer.connect('style-changed', self._on_buffer_style_changed)
self.textbuffer.connect('changed', self._on_buffer_changed)
GObject.GObject.__init__(self, buffer=self.textbuffer)

View File

@ -3,7 +3,8 @@
#
# Copyright (C) 2009 Florian Heinle
# Copyright (C) 2010 Doug Blank <doug.blank@gmail.com>
# Copyright (C) 2010 Benny Malengier
# Copyright (C) 2010 Benny Malengier
# Copyright (C) 2014 Vassilii Khachaturov
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
@ -20,14 +21,14 @@
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# $Id$
"""
gtk textbuffer with undo functionality
"""
__all__ = ["UndoableStyledBuffer"]
from contextlib import contextmanager
from gi.repository import Gtk
from gramps.gen.lib.styledtext import StyledText
@ -79,6 +80,25 @@ class UndoableStyledBuffer(StyledTextBuffer):
self.connect('apply-tag', self.on_tag_insert_undoable)
self.connect_after('apply-tag', self.on_tag_afterinsert_undoable)
@contextmanager
def undo_disabled(self):
"""
Assures that not_undoable_action is False during the context.
Usage example (see gramps/gui/widgets/styledtexteditor.py)::
with self.buffer.undo_disabled():
... # heavy stuff like spell checking
"""
oldflag = self.not_undoable_action
self.not_undoable_action = True
try:
yield
except:
raise
finally:
self.not_undoable_action = oldflag
def on_tag_insert_undoable(self, buffer, tag, start, end):
if not self.undo_in_progress:
self._empty_redo_stack()