Convert gui.views to use abstract base classes

This commit is contained in:
Nick Hall 2016-07-14 21:43:10 +01:00
parent 534eafef83
commit 620d43a544
7 changed files with 39 additions and 89 deletions

View File

@ -26,6 +26,7 @@
# Standard python modules # Standard python modules
# #
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
from abc import ABCMeta, abstractmethod
from io import StringIO from io import StringIO
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
@ -74,7 +75,7 @@ BTM = '''</menu></menubar></ui>'''
DISABLED = -1 DISABLED = -1
class Bookmarks: class Bookmarks(metaclass=ABCMeta):
"Handle the bookmarks interface for Gramps." "Handle the bookmarks interface for Gramps."
def __init__(self, dbstate, uistate, callback=None): def __init__(self, dbstate, uistate, callback=None):
@ -113,17 +114,17 @@ class Bookmarks:
self.connect_signals() self.connect_signals()
self.update_bookmarks() self.update_bookmarks()
@abstractmethod
def connect_signals(self): def connect_signals(self):
""" """
Connect the person-delete signal Connect the person-delete signal
""" """
raise NotImplementedError
@abstractmethod
def get_bookmarks(self): def get_bookmarks(self):
""" """
Retrieve bookmarks from the database. Retrieve bookmarks from the database.
""" """
raise NotImplementedError
def update_bookmarks(self): def update_bookmarks(self):
""" """
@ -184,20 +185,20 @@ class Bookmarks:
self.uistate.uimanager.ensure_update() self.uistate.uimanager.ensure_update()
text.close() text.close()
@abstractmethod
def make_label(self, handle): def make_label(self, handle):
""" """
Returns a (label, object) tuple appropriate to the type of the object Returns a (label, object) tuple appropriate to the type of the object
that the handle refers to. The label is a text for the object, e.g. the that the handle refers to. The label is a text for the object, e.g. the
object name. object name.
""" """
raise NotImplementedError
@abstractmethod
def callback(self, handle): def callback(self, handle):
""" """
Returns a unique call to a function with the associated handle. The Returns a unique call to a function with the associated handle. The
function that will be called is defined in the derived class function that will be called is defined in the derived class
""" """
raise NotImplementedError
def add(self, person_handle): def add(self, person_handle):
"""Append the person to the bottom of the bookmarks.""" """Append the person to the bottom of the bookmarks."""

View File

@ -29,6 +29,7 @@ Provide the base classes for GRAMPS' DataView classes
# python modules # python modules
# #
#---------------------------------------------------------------- #----------------------------------------------------------------
from abc import abstractmethod
import pickle import pickle
import time import time
import logging import logging
@ -1095,42 +1096,35 @@ class ListView(NavigationView):
#################################################################### ####################################################################
# Template functions # Template functions
#################################################################### ####################################################################
def get_bookmarks(self): @abstractmethod
"""
Template function to get bookmarks.
We could implement this in the NavigationView
"""
raise NotImplementedError
def edit(self, obj, data=None): def edit(self, obj, data=None):
""" """
Template function to allow the editing of the selected object Template function to allow the editing of the selected object
""" """
raise NotImplementedError
@abstractmethod
def remove(self, handle, data=None): def remove(self, handle, data=None):
""" """
Template function to allow the removal of an object by its handle Template function to allow the removal of an object by its handle
""" """
raise NotImplementedError
@abstractmethod
def add(self, obj, data=None): def add(self, obj, data=None):
""" """
Template function to allow the adding of a new object Template function to allow the adding of a new object
""" """
raise NotImplementedError
@abstractmethod
def merge(self, obj, data=None): def merge(self, obj, data=None):
""" """
Template function to allow the merger of two objects. Template function to allow the merger of two objects.
""" """
raise NotImplementedError
@abstractmethod
def remove_object_from_handle(self, handle): def remove_object_from_handle(self, handle):
""" """
Template function to allow the removal of an object by its handle Template function to allow the removal of an object by its handle
""" """
raise NotImplementedError
def open_all_nodes(self, obj): def open_all_nodes(self, obj):
""" """

View File

@ -28,6 +28,7 @@ Provide the base classes for GRAMPS' DataView classes
# python modules # python modules
# #
#---------------------------------------------------------------- #----------------------------------------------------------------
from abc import abstractmethod
import logging import logging
_LOG = logging.getLogger('.navigationview') _LOG = logging.getLogger('.navigationview')
@ -216,12 +217,12 @@ class NavigationView(PageView):
if handle and not hobj.lock and not (handle == hobj.present()): if handle and not hobj.lock and not (handle == hobj.present()):
hobj.push(handle) hobj.push(handle)
@abstractmethod
def goto_handle(self, handle): def goto_handle(self, handle):
""" """
Needs to be implemented by classes derived from this. Needs to be implemented by classes derived from this.
Used to move to the given handle. Used to move to the given handle.
""" """
raise NotImplementedError
def selected_handles(self): def selected_handles(self):
""" """
@ -457,50 +458,19 @@ class NavigationView(PageView):
#################################################################### ####################################################################
# Template functions # Template functions
#################################################################### ####################################################################
def get_bookmarks(self): @abstractmethod
"""
Template function to get bookmarks.
We could implement this here based on navigation_type()
"""
raise NotImplementedError
def edit(self, obj):
"""
Template function to allow the editing of the selected object
"""
raise NotImplementedError
def remove(self, handle):
"""
Template function to allow the removal of an object by its handle
"""
raise NotImplementedError
def add(self, obj):
"""
Template function to allow the adding of a new object
"""
raise NotImplementedError
def remove_object_from_handle(self, handle):
"""
Template function to allow the removal of an object by its handle
"""
raise NotImplementedError
def build_tree(self): def build_tree(self):
""" """
Rebuilds the current display. This must be overridden by the derived Rebuilds the current display. This must be overridden by the derived
class. class.
""" """
raise NotImplementedError
@abstractmethod
def build_widget(self): def build_widget(self):
""" """
Builds the container widget for the interface. Must be overridden by the Builds the container widget for the interface. Must be overridden by the
the base class. Returns a gtk container widget. the base class. Returns a gtk container widget.
""" """
raise NotImplementedError
def key_press_handler(self, widget, event): def key_press_handler(self, widget, event):
""" """

View File

@ -27,6 +27,7 @@ Provide the base class for GRAMPS' DataView classes
# python modules # python modules
# #
#---------------------------------------------------------------- #----------------------------------------------------------------
from abc import ABCMeta, abstractmethod
import logging import logging
_LOG = logging.getLogger('.pageview') _LOG = logging.getLogger('.pageview')
@ -57,7 +58,7 @@ from ..actiongroup import ActionGroup
# PageView # PageView
# #
#------------------------------------------------------------------------------ #------------------------------------------------------------------------------
class PageView(DbGUIElement): class PageView(DbGUIElement, metaclass=ABCMeta):
""" """
The PageView class is the base class for all Data Views in GRAMPS. All The PageView class is the base class for all Data Views in GRAMPS. All
Views should derive from this class. The ViewManager understands the public Views should derive from this class. The ViewManager understands the public
@ -312,12 +313,12 @@ class PageView(DbGUIElement):
self.bottombar.set_inactive() self.bottombar.set_inactive()
self.active = False self.active = False
@abstractmethod
def build_tree(self): def build_tree(self):
""" """
Rebuilds the current display. This must be overridden by the derived Rebuilds the current display. This must be overridden by the derived
class. class.
""" """
raise NotImplementedError
def ui_definition(self): def ui_definition(self):
""" """
@ -389,12 +390,12 @@ class PageView(DbGUIElement):
self.top = self.build_interface() self.top = self.build_interface()
return self.top return self.top
@abstractmethod
def build_widget(self): def build_widget(self):
""" """
Builds the container widget for the main view pane. Must be overridden Builds the container widget for the main view pane. Must be overridden
by the base class. Returns a gtk container widget. by the base class. Returns a gtk container widget.
""" """
raise NotImplementedError
def define_actions(self): def define_actions(self):
""" """
@ -473,40 +474,6 @@ class PageView(DbGUIElement):
""" """
self.uistate.clear_filter_results() self.uistate.clear_filter_results()
def edit(self, obj):
"""
Template function to allow the editing of the selected object
"""
raise NotImplementedError
def remove(self, handle):
"""
Template function to allow the removal of an object by its handle
"""
raise NotImplementedError
def remove_object_from_handle(self, handle):
"""
Template function to allow the removal of an object by its handle
"""
raise NotImplementedError
def add(self, obj):
"""
Template function to allow the adding of a new object
"""
raise NotImplementedError
def _key_press(self, obj, event):
"""
Define the action for a key press event
"""
# TODO: This is never used? (replaced in ListView)
if event.keyval in (Gdk.KEY_Return, Gdk.KEY_KP_Enter):
self.edit(obj)
return True
return False
def on_delete(self): def on_delete(self):
""" """
Method called on shutdown. Data views should put code here Method called on shutdown. Data views should put code here
@ -569,7 +536,7 @@ class PageView(DbGUIElement):
:return: list of functions :return: list of functions
""" """
raise NotImplementedError return []
def configure(self): def configure(self):
""" """

View File

@ -346,6 +346,12 @@ class BasePersonView(ListView):
self.uistate.set_busy_cursor(False) self.uistate.set_busy_cursor(False)
def remove_object_from_handle(self, handle):
"""
The remove_selected_objects method is not called in this view.
"""
pass
def define_actions(self): def define_actions(self):
""" """
Required define_actions function for PageView. Builds the action Required define_actions function for PageView. Builds the action

View File

@ -75,6 +75,12 @@ class DashboardView(PageView):
self.dbstate, self.uistate) self.dbstate, self.uistate)
return self.widget return self.widget
def build_tree(self):
"""
Rebuilds the current display.
"""
pass
def get_stock(self): def get_stock(self):
""" """
Return image associated with the view, which is used for the Return image associated with the view, which is used for the

View File

@ -281,6 +281,12 @@ class FamilyView(ListView):
trans.set_description(_("Family [%s]") % gramps_id) trans.set_description(_("Family [%s]") % gramps_id)
self.uistate.set_busy_cursor(False) self.uistate.set_busy_cursor(False)
def remove_object_from_handle(self, handle):
"""
The remove_selected_objects method is not called in this view.
"""
pass
def edit(self, obj): def edit(self, obj):
for handle in self.selected_handles(): for handle in self.selected_handles():
family = self.dbstate.db.get_family_from_handle(handle) family = self.dbstate.db.get_family_from_handle(handle)