From 283c1b1aa28fb01498be78fd6620a838bbdcf4d5 Mon Sep 17 00:00:00 2001 From: Vassilii Khachaturov Date: Wed, 19 Feb 2014 12:42:22 +0200 Subject: [PATCH] 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 --- gramps/gui/spell.py | 6 ++--- gramps/gui/widgets/styledtexteditor.py | 1 + gramps/gui/widgets/undoablestyledbuffer.py | 26 +++++++++++++++++++--- 3 files changed, 27 insertions(+), 6 deletions(-) diff --git a/gramps/gui/spell.py b/gramps/gui/spell.py index ccc48ef89..c9c0e2427 100644 --- a/gramps/gui/spell.py +++ b/gramps/gui/spell.py @@ -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 diff --git a/gramps/gui/widgets/styledtexteditor.py b/gramps/gui/widgets/styledtexteditor.py index c7d65cfa7..05f53a972 100644 --- a/gramps/gui/widgets/styledtexteditor.py +++ b/gramps/gui/widgets/styledtexteditor.py @@ -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) diff --git a/gramps/gui/widgets/undoablestyledbuffer.py b/gramps/gui/widgets/undoablestyledbuffer.py index c6ca1ac99..692ea1f21 100644 --- a/gramps/gui/widgets/undoablestyledbuffer.py +++ b/gramps/gui/widgets/undoablestyledbuffer.py @@ -3,7 +3,8 @@ # # Copyright (C) 2009 Florian Heinle # Copyright (C) 2010 Doug Blank -# 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()