2006-05-06 03:25:01 +05:30
|
|
|
#
|
|
|
|
# Gramps - a GTK+/GNOME based genealogy program
|
|
|
|
#
|
|
|
|
# Copyright (C) 2000-2006 Donald N. Allingham
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modiy
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, write to the Free Software
|
|
|
|
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
#
|
|
|
|
|
2008-01-22 14:47:46 +05:30
|
|
|
# $Id$
|
2006-05-06 03:25:01 +05:30
|
|
|
|
|
|
|
# Written by Alex Roitman
|
|
|
|
|
|
|
|
#------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# standard python modules
|
|
|
|
#
|
|
|
|
#------------------------------------------------------------------------
|
|
|
|
import time
|
2010-01-18 10:12:17 +05:30
|
|
|
from gen.ggettext import gettext as _
|
2010-04-24 00:32:24 +05:30
|
|
|
from itertools import chain
|
2006-05-06 03:25:01 +05:30
|
|
|
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# GTK/Gnome modules
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
import gtk
|
2008-01-22 03:33:43 +05:30
|
|
|
import gobject
|
2006-05-06 03:25:01 +05:30
|
|
|
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# GRAMPS modules
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
from QuestionDialog import QuestionDialog
|
|
|
|
import ManagedWindow
|
|
|
|
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# UndoHistory class
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
class UndoHistory(ManagedWindow.ManagedWindow):
|
|
|
|
"""
|
|
|
|
The UndoHistory provides a list view with all the editing
|
|
|
|
steps available for undo/redo. Selecting a line in the list
|
|
|
|
will revert/advance to the appropriate step in editing history.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, dbstate, uistate):
|
|
|
|
|
|
|
|
self.title = _("Undo History")
|
2010-04-23 01:16:50 +05:30
|
|
|
ManagedWindow.ManagedWindow.__init__(self, uistate, [], self.__class__)
|
2006-05-06 03:25:01 +05:30
|
|
|
self.db = dbstate.db
|
2009-09-01 00:12:29 +05:30
|
|
|
self.undodb = self.db.undodb
|
2006-05-07 10:52:44 +05:30
|
|
|
self.dbstate = dbstate
|
|
|
|
|
2010-04-23 01:16:50 +05:30
|
|
|
window = gtk.Dialog("", uistate.window,
|
|
|
|
gtk.DIALOG_DESTROY_WITH_PARENT, None)
|
2006-05-07 10:52:44 +05:30
|
|
|
|
|
|
|
self.undo_button = window.add_button(gtk.STOCK_UNDO,
|
|
|
|
gtk.RESPONSE_REJECT)
|
|
|
|
self.redo_button = window.add_button(gtk.STOCK_REDO,
|
|
|
|
gtk.RESPONSE_ACCEPT)
|
|
|
|
self.clear_button = window.add_button(gtk.STOCK_CLEAR,
|
|
|
|
gtk.RESPONSE_APPLY)
|
|
|
|
self.close_button = window.add_button(gtk.STOCK_CLOSE,
|
|
|
|
gtk.RESPONSE_CLOSE)
|
|
|
|
|
|
|
|
self.set_window(window, None, self.title)
|
2010-04-23 01:16:50 +05:30
|
|
|
self.window.set_size_request(400, 200)
|
2006-05-06 03:25:01 +05:30
|
|
|
self.window.connect('response', self._response)
|
|
|
|
|
|
|
|
scrolled_window = gtk.ScrolledWindow()
|
2010-04-23 01:16:50 +05:30
|
|
|
scrolled_window.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
|
2006-05-09 04:48:16 +05:30
|
|
|
self.tree = gtk.TreeView()
|
2008-01-22 03:33:43 +05:30
|
|
|
self.model = gtk.ListStore(gobject.TYPE_STRING, gobject.TYPE_STRING,
|
|
|
|
gobject.TYPE_STRING, gobject.TYPE_STRING)
|
2006-05-09 04:48:16 +05:30
|
|
|
self.selection = self.tree.get_selection()
|
2006-05-06 03:25:01 +05:30
|
|
|
|
2006-05-12 02:34:46 +05:30
|
|
|
self.renderer = gtk.CellRendererText()
|
2006-05-09 04:48:16 +05:30
|
|
|
self.tree.set_model(self.model)
|
|
|
|
self.tree.set_rules_hint(True)
|
|
|
|
self.tree.append_column(
|
2006-05-12 02:34:46 +05:30
|
|
|
gtk.TreeViewColumn(_('Original time'), self.renderer,
|
2010-04-23 01:16:50 +05:30
|
|
|
text=0, foreground=2, background=3))
|
2006-05-09 04:48:16 +05:30
|
|
|
self.tree.append_column(
|
2006-05-12 02:34:46 +05:30
|
|
|
gtk.TreeViewColumn(_('Action'), self.renderer,
|
2010-04-23 01:16:50 +05:30
|
|
|
text=1, foreground=2, background=3))
|
2006-05-06 03:25:01 +05:30
|
|
|
|
2006-05-09 04:48:16 +05:30
|
|
|
scrolled_window.add(self.tree)
|
2006-05-06 03:25:01 +05:30
|
|
|
self.window.vbox.add(scrolled_window)
|
|
|
|
self.window.show_all()
|
|
|
|
|
|
|
|
self._build_model()
|
2006-05-07 10:52:44 +05:30
|
|
|
self._update_ui()
|
|
|
|
|
2010-04-23 01:16:50 +05:30
|
|
|
self.selection.connect('changed', self._selection_changed)
|
2006-05-07 10:52:44 +05:30
|
|
|
self.show()
|
|
|
|
|
2008-02-24 19:25:55 +05:30
|
|
|
def _selection_changed(self, obj):
|
|
|
|
(model, node) = self.selection.get_selected()
|
2006-05-12 02:34:46 +05:30
|
|
|
if not node:
|
|
|
|
return
|
|
|
|
path = self.model.get_path(node)
|
2010-04-24 00:32:24 +05:30
|
|
|
start = min(path[0], self.undodb.undo_count)
|
|
|
|
end = max(path[0], self.undodb.undo_count)
|
2006-05-12 02:34:46 +05:30
|
|
|
|
2010-05-21 00:02:08 +05:30
|
|
|
self._paint_rows(0, len(self.model) - 1, False)
|
2010-04-23 01:16:50 +05:30
|
|
|
self._paint_rows(start, end, True)
|
2006-05-06 03:25:01 +05:30
|
|
|
|
2010-04-24 00:32:24 +05:30
|
|
|
if path[0] < self.undodb.undo_count:
|
|
|
|
# This transaction is an undo candidate
|
2006-05-12 06:31:10 +05:30
|
|
|
self.redo_button.set_sensitive(False)
|
2010-05-21 00:02:08 +05:30
|
|
|
self.undo_button.set_sensitive(self.undodb.undo_count)
|
2010-04-23 01:16:50 +05:30
|
|
|
|
2010-04-24 00:32:24 +05:30
|
|
|
else: # path[0] >= self.undodb.undo_count:
|
|
|
|
# This transaction is an redo candidate
|
2006-05-12 06:31:10 +05:30
|
|
|
self.undo_button.set_sensitive(False)
|
2010-05-21 00:02:08 +05:30
|
|
|
self.redo_button.set_sensitive(self.undodb.redo_count)
|
2010-04-23 01:16:50 +05:30
|
|
|
|
|
|
|
def _paint_rows(self, start, end, selected=False):
|
2006-05-12 02:34:46 +05:30
|
|
|
if selected:
|
2010-04-23 01:16:50 +05:30
|
|
|
(fg, bg) = get_colors(self.tree, gtk.STATE_SELECTED)
|
2006-05-12 02:34:46 +05:30
|
|
|
else:
|
|
|
|
fg = bg = None
|
|
|
|
|
2010-04-23 01:16:50 +05:30
|
|
|
for idx in range(start, end+1):
|
2006-05-12 02:34:46 +05:30
|
|
|
the_iter = self.model.get_iter( (idx,) )
|
2010-04-23 01:16:50 +05:30
|
|
|
self.model.set(the_iter, 2, fg)
|
|
|
|
self.model.set(the_iter, 3, bg)
|
2006-05-12 02:34:46 +05:30
|
|
|
|
2010-04-23 01:16:50 +05:30
|
|
|
def _response(self, obj, response_id):
|
2006-05-06 03:25:01 +05:30
|
|
|
if response_id == gtk.RESPONSE_CLOSE:
|
2006-06-20 20:31:27 +05:30
|
|
|
self.close(obj)
|
2010-04-23 01:16:50 +05:30
|
|
|
|
2006-05-06 03:25:01 +05:30
|
|
|
elif response_id == gtk.RESPONSE_REJECT:
|
2010-04-24 00:32:24 +05:30
|
|
|
# Undo the selected entries
|
2008-02-24 19:25:55 +05:30
|
|
|
(model, node) = self.selection.get_selected()
|
2006-05-12 02:34:46 +05:30
|
|
|
if not node:
|
|
|
|
return
|
|
|
|
path = self.model.get_path(node)
|
2010-05-21 00:02:08 +05:30
|
|
|
nsteps = path[0] - self.undodb.undo_count - 1
|
2010-04-23 01:16:50 +05:30
|
|
|
self._move(nsteps or -1)
|
|
|
|
|
2006-05-06 03:25:01 +05:30
|
|
|
elif response_id == gtk.RESPONSE_ACCEPT:
|
2010-04-24 00:32:24 +05:30
|
|
|
# Redo the selected entries
|
2008-02-24 19:25:55 +05:30
|
|
|
(model, node) = self.selection.get_selected()
|
2006-05-12 02:34:46 +05:30
|
|
|
if not node:
|
|
|
|
return
|
|
|
|
path = self.model.get_path(node)
|
2010-05-21 00:02:08 +05:30
|
|
|
nsteps = path[0] - self.undodb.undo_count
|
2010-04-23 01:16:50 +05:30
|
|
|
self._move(nsteps or 1)
|
|
|
|
|
2006-05-06 03:25:01 +05:30
|
|
|
elif response_id == gtk.RESPONSE_APPLY:
|
|
|
|
self._clear_clicked()
|
2008-01-06 01:40:26 +05:30
|
|
|
elif response_id == gtk.RESPONSE_DELETE_EVENT:
|
2006-06-20 20:31:27 +05:30
|
|
|
self.close(obj)
|
2006-05-06 03:25:01 +05:30
|
|
|
|
2008-02-24 19:25:55 +05:30
|
|
|
def build_menu_names(self, obj):
|
2010-04-23 01:16:50 +05:30
|
|
|
return (self.title, None)
|
2006-05-06 03:25:01 +05:30
|
|
|
|
2008-02-24 19:25:55 +05:30
|
|
|
def _clear_clicked(self, obj=None):
|
2006-05-06 03:25:01 +05:30
|
|
|
QuestionDialog(_("Delete confirmation"),
|
|
|
|
_("Are you sure you want to clear the Undo history?"),
|
|
|
|
_("Clear"),
|
|
|
|
self.clear,
|
|
|
|
self.window)
|
|
|
|
|
|
|
|
def clear(self):
|
2009-09-01 00:12:29 +05:30
|
|
|
self.undodb.clear()
|
2010-04-24 00:32:24 +05:30
|
|
|
self.db.abort_possible = False
|
2006-05-06 03:25:01 +05:30
|
|
|
self.update()
|
|
|
|
if self.db.undo_callback:
|
|
|
|
self.db.undo_callback(None)
|
|
|
|
if self.db.redo_callback:
|
|
|
|
self.db.redo_callback(None)
|
|
|
|
|
2010-04-23 01:16:50 +05:30
|
|
|
def _move(self, steps=-1):
|
2010-05-21 00:02:08 +05:30
|
|
|
if steps == 0:
|
2006-05-07 10:52:44 +05:30
|
|
|
return
|
2010-04-23 01:16:50 +05:30
|
|
|
func = self.db.undo if steps < 0 else self.db.redo
|
2006-05-07 10:52:44 +05:30
|
|
|
|
|
|
|
for step in range(abs(steps)):
|
|
|
|
func(False)
|
|
|
|
self.update()
|
2006-05-06 03:25:01 +05:30
|
|
|
|
|
|
|
def _update_ui(self):
|
2010-04-23 01:16:50 +05:30
|
|
|
self._paint_rows(0, len(self.model)-1, False)
|
2010-05-21 00:02:08 +05:30
|
|
|
self.undo_button.set_sensitive(self.undodb.undo_count)
|
|
|
|
self.redo_button.set_sensitive(self.undodb.redo_count)
|
2006-05-07 10:52:44 +05:30
|
|
|
self.clear_button.set_sensitive(
|
2010-05-21 00:02:08 +05:30
|
|
|
self.undodb.undo_count or self.undodb.redo_count
|
2010-04-23 01:16:50 +05:30
|
|
|
)
|
2006-05-06 03:25:01 +05:30
|
|
|
|
|
|
|
def _build_model(self):
|
|
|
|
self.model.clear()
|
2006-05-12 02:34:46 +05:30
|
|
|
fg = bg = None
|
2006-08-01 07:54:49 +05:30
|
|
|
|
2009-09-01 00:12:29 +05:30
|
|
|
if self.undodb.undo_history_timestamp:
|
2006-08-01 07:54:49 +05:30
|
|
|
if self.db.abort_possible:
|
|
|
|
mod_text = _('Database opened')
|
|
|
|
else:
|
|
|
|
mod_text = _('History cleared')
|
2009-09-01 00:12:29 +05:30
|
|
|
time_text = time.ctime(self.undodb.undo_history_timestamp)
|
2010-04-23 01:16:50 +05:30
|
|
|
self.model.append(row=[time_text, mod_text, fg, bg])
|
2006-05-07 10:52:44 +05:30
|
|
|
|
2010-04-24 00:32:24 +05:30
|
|
|
# Add the undo and redo queues to the model
|
|
|
|
for txn in chain(self.undodb.undoq, reversed(self.undodb.redoq)):
|
|
|
|
time_text = time.ctime(txn.timestamp)
|
|
|
|
mod_text = txn.get_description()
|
2010-04-23 01:16:50 +05:30
|
|
|
self.model.append(row=[time_text, mod_text, fg, bg])
|
2010-04-24 00:32:24 +05:30
|
|
|
path = (self.undodb.undo_count,)
|
2006-05-07 10:52:44 +05:30
|
|
|
self.selection.select_path(path)
|
|
|
|
|
2006-05-06 03:25:01 +05:30
|
|
|
def update(self):
|
|
|
|
self._build_model()
|
|
|
|
self._update_ui()
|
2006-05-12 02:34:46 +05:30
|
|
|
|
|
|
|
def gtk_color_to_str(color):
|
2006-05-13 00:16:26 +05:30
|
|
|
color_str = u"#%02x%02x%02x" % (color.red/256,
|
|
|
|
color.green/256,
|
|
|
|
color.blue/256)
|
2006-05-12 02:34:46 +05:30
|
|
|
return color_str
|
|
|
|
|
2010-04-23 01:16:50 +05:30
|
|
|
def get_colors(obj, state):
|
2006-05-12 02:34:46 +05:30
|
|
|
fg_color = obj.style.fg[state]
|
|
|
|
bg_color = obj.style.bg[state]
|
|
|
|
|
|
|
|
fg_color_str = gtk_color_to_str(fg_color)
|
|
|
|
bg_color_str = gtk_color_to_str(bg_color)
|
|
|
|
|
2010-04-23 01:16:50 +05:30
|
|
|
return (fg_color_str, bg_color_str)
|