2007-08-30 01:07:58 +05:30
|
|
|
#
|
|
|
|
# Gramps - a GTK+/GNOME based genealogy program
|
|
|
|
#
|
|
|
|
# Copyright (C) 2007 B. Malengier
|
2008-05-19 00:54:28 +05:30
|
|
|
# Copyright (C) 2008 Brian G. Matherly
|
2007-08-30 01:07:58 +05:30
|
|
|
#
|
|
|
|
# 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
|
|
|
|
# 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$
|
2007-08-30 01:07:58 +05:30
|
|
|
|
|
|
|
"""
|
|
|
|
This module provides the functions to build the quick report context menu's
|
|
|
|
"""
|
|
|
|
|
|
|
|
#------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# python modules
|
|
|
|
#
|
|
|
|
#------------------------------------------------------------------------
|
|
|
|
from gettext import gettext as _
|
|
|
|
from cStringIO import StringIO
|
|
|
|
|
|
|
|
#------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# Set up logging
|
|
|
|
#
|
|
|
|
#------------------------------------------------------------------------
|
|
|
|
import logging
|
|
|
|
log = logging.getLogger(".QuickReports")
|
|
|
|
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# GNOME modules
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
import gtk
|
|
|
|
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# GRAMPS modules
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
|
2009-12-31 20:29:44 +05:30
|
|
|
from gen.plug import (CATEGORY_QR_PERSON, CATEGORY_QR_FAMILY, CATEGORY_QR_MEDIA,
|
2009-10-24 19:23:20 +05:30
|
|
|
CATEGORY_QR_EVENT, CATEGORY_QR_SOURCE, CATEGORY_QR_MISC,
|
2009-12-31 20:29:44 +05:30
|
|
|
CATEGORY_QR_PLACE, CATEGORY_QR_REPOSITORY, CATEGORY_QR_NOTE)
|
2009-10-25 19:22:29 +05:30
|
|
|
from gui.pluginmanager import GuiPluginManager
|
2007-08-30 01:07:58 +05:30
|
|
|
|
2008-02-24 19:25:55 +05:30
|
|
|
def create_quickreport_menu(category,dbstate,uistate, handle) :
|
2008-02-18 21:22:40 +05:30
|
|
|
""" This functions querries the registered quick reports with
|
2007-08-30 01:07:58 +05:30
|
|
|
quick_report_list of _PluginMgr.py
|
|
|
|
It collects the reports of the requested category, which must be one of
|
|
|
|
CATEGORY_QR_PERSON, CATEGORY_QR_FAMILY,
|
2009-12-31 20:29:44 +05:30
|
|
|
CATEGORY_QR_EVENT, CATEGORY_QR_SOURCE, CATEGORY_QR_MEDIA,
|
2007-08-30 01:07:58 +05:30
|
|
|
CATEGORY_QR_PLACE, CATEGORY_QR_REPOSITORY
|
|
|
|
It constructs the ui string of the quick report menu, and it's actions
|
|
|
|
The action callback function is constructed, using the dbstate and the
|
|
|
|
handle as input method.
|
|
|
|
A tuple is returned, containing the ui string of the quick report menu,
|
|
|
|
and its associated actions
|
2008-02-18 21:22:40 +05:30
|
|
|
"""
|
2007-08-30 01:07:58 +05:30
|
|
|
|
|
|
|
actions = []
|
|
|
|
ofile = StringIO()
|
|
|
|
ofile.write('<menu action="QuickReport">')
|
|
|
|
|
2009-01-11 01:03:55 +05:30
|
|
|
actions.append(('QuickReport', None, _("Quick View"), None, None, None))
|
2007-08-30 01:07:58 +05:30
|
|
|
|
|
|
|
menu = gtk.Menu()
|
|
|
|
menu.show()
|
|
|
|
|
|
|
|
#select the reports to show
|
|
|
|
showlst = []
|
2009-10-25 19:22:29 +05:30
|
|
|
pmgr = GuiPluginManager.get_instance()
|
2009-10-24 19:23:20 +05:30
|
|
|
for pdata in pmgr.get_reg_quick_reports():
|
|
|
|
if pdata.supported and pdata.category == category :
|
2007-08-30 01:07:58 +05:30
|
|
|
#add tuple function, translated name, name, status
|
2009-10-24 19:23:20 +05:30
|
|
|
showlst.append(pdata)
|
2007-08-30 01:07:58 +05:30
|
|
|
|
|
|
|
showlst.sort(by_menu_name)
|
2009-10-24 19:23:20 +05:30
|
|
|
for pdata in showlst:
|
|
|
|
new_key = pdata.id.replace(' ', '-')
|
2007-08-30 01:07:58 +05:30
|
|
|
ofile.write('<menuitem action="%s"/>' % new_key)
|
2009-10-24 19:23:20 +05:30
|
|
|
actions.append((new_key, None, pdata.name, None, None,
|
|
|
|
make_quick_report_callback(pdata, category,
|
2007-12-21 11:52:46 +05:30
|
|
|
dbstate, uistate, handle)))
|
2007-08-30 01:07:58 +05:30
|
|
|
ofile.write('</menu>')
|
|
|
|
|
|
|
|
return (ofile.getvalue(), actions)
|
|
|
|
|
|
|
|
def by_menu_name(first, second):
|
2009-10-24 19:23:20 +05:30
|
|
|
return cmp(first.name, second.name)
|
2007-08-30 01:07:58 +05:30
|
|
|
|
2009-10-24 19:23:20 +05:30
|
|
|
def make_quick_report_callback(pdata, category, dbstate, uistate, handle):
|
|
|
|
return lambda x: run_report(dbstate, uistate, category, handle, pdata)
|
2007-12-30 09:16:39 +05:30
|
|
|
|
2008-12-13 22:29:14 +05:30
|
|
|
def get_quick_report_list(qv_category=None):
|
|
|
|
"""
|
2009-10-24 19:23:20 +05:30
|
|
|
Returns a list of PluginData of quick views of category qv_category
|
2008-12-13 22:29:14 +05:30
|
|
|
CATEGORY_QR_PERSON, CATEGORY_QR_FAMILY, CATEGORY_QR_EVENT,
|
|
|
|
CATEGORY_QR_SOURCE, CATEGORY_QR_MISC, CATEGORY_QR_PLACE,
|
2009-12-31 20:29:44 +05:30
|
|
|
CATEGORY_QR_REPOSITORY, CATEGORY_QR_MEDIA or None for all
|
2008-12-13 22:29:14 +05:30
|
|
|
"""
|
|
|
|
names = []
|
2009-10-25 19:22:29 +05:30
|
|
|
pmgr = GuiPluginManager.get_instance()
|
2009-10-24 19:23:20 +05:30
|
|
|
for pdata in pmgr.get_reg_quick_reports():
|
|
|
|
if qv_category == pdata.category or qv_category is None:
|
|
|
|
names.append(pdata) # (see below for item struct)
|
2008-12-13 22:29:14 +05:30
|
|
|
return names
|
|
|
|
|
2009-10-24 19:23:20 +05:30
|
|
|
def run_quick_report_by_name(dbstate, uistate, report_name, handle,
|
2009-11-07 18:59:35 +05:30
|
|
|
container=None, **kwargs):
|
|
|
|
"""
|
|
|
|
Run a QuickView by name.
|
|
|
|
**kwargs provides a way of passing special quick views additional
|
|
|
|
arguments.
|
|
|
|
"""
|
2007-12-30 09:16:39 +05:30
|
|
|
report = None
|
2009-10-25 19:22:29 +05:30
|
|
|
pmgr = GuiPluginManager.get_instance()
|
2009-10-24 19:23:20 +05:30
|
|
|
for pdata in pmgr.get_reg_quick_reports():
|
|
|
|
if pdata.id == report_name:
|
|
|
|
report = pdata
|
2007-12-30 09:16:39 +05:30
|
|
|
break
|
|
|
|
if report:
|
2009-10-24 19:23:20 +05:30
|
|
|
return run_report(dbstate, uistate, report.category,
|
2009-11-07 18:59:35 +05:30
|
|
|
handle, report, container=container, **kwargs)
|
2008-03-05 09:31:27 +05:30
|
|
|
else:
|
|
|
|
raise AttributeError, ("No such quick report '%s'" % report_name)
|
|
|
|
|
|
|
|
def run_quick_report_by_name_direct(report_name, database, document, handle):
|
|
|
|
"""
|
|
|
|
Useful for running one quick report from another
|
|
|
|
"""
|
|
|
|
from docgen import TextBufDoc
|
|
|
|
from Simple import make_basic_stylesheet
|
|
|
|
report = None
|
2009-10-25 19:22:29 +05:30
|
|
|
pmgr = GuiPluginManager.get_instance()
|
2009-10-24 19:23:20 +05:30
|
|
|
for pdata in pmgr.get_reg_quick_reports():
|
|
|
|
if pdata.id == report_name:
|
|
|
|
report = pdata
|
2008-03-05 09:31:27 +05:30
|
|
|
break
|
|
|
|
if report:
|
|
|
|
# FIXME: allow auto lookup of obj like below?
|
2009-06-15 21:48:16 +05:30
|
|
|
d = TextBufDoc(make_basic_stylesheet(), None)
|
2008-03-05 09:31:27 +05:30
|
|
|
d.dbstate = document.dbstate
|
|
|
|
d.uistate = document.uistate
|
|
|
|
d.open("")
|
2009-10-24 19:23:20 +05:30
|
|
|
mod = pmgr.load_plugin(report)
|
|
|
|
if mod:
|
|
|
|
reportfunc = eval('mod.' + report.runfunc)
|
|
|
|
retval = reportfunc(database, d, handle)
|
|
|
|
d.close()
|
|
|
|
return retval
|
|
|
|
else:
|
|
|
|
raise ImportError, ("Quick report id = '%s' could not be loaded"
|
|
|
|
% report_name)
|
2008-03-05 09:31:27 +05:30
|
|
|
else:
|
2009-10-24 19:23:20 +05:30
|
|
|
raise AttributeError, ("No such quick report id = '%s'" % report_name)
|
2007-08-30 01:07:58 +05:30
|
|
|
|
2009-11-07 18:59:35 +05:30
|
|
|
def run_report(dbstate, uistate, category, handle, pdata, container=None,
|
|
|
|
**kwargs):
|
2008-12-13 22:29:14 +05:30
|
|
|
"""
|
|
|
|
Run a Quick Report.
|
2009-10-24 19:23:20 +05:30
|
|
|
Optionally container can be passed, rather than putting the report
|
|
|
|
in a new window.
|
2009-11-07 18:59:35 +05:30
|
|
|
**kwargs are only used for special quick views that allow additional
|
|
|
|
arguments, and that are run by run_quick_report_by_name().
|
2008-12-13 22:29:14 +05:30
|
|
|
"""
|
2008-02-19 01:37:09 +05:30
|
|
|
from docgen import TextBufDoc
|
2007-08-30 01:07:58 +05:30
|
|
|
from Simple import make_basic_stylesheet
|
2009-10-25 19:22:29 +05:30
|
|
|
pmgr = GuiPluginManager.get_instance()
|
2009-10-24 19:23:20 +05:30
|
|
|
mod = pmgr.load_plugin(pdata)
|
|
|
|
if not mod:
|
|
|
|
print "QuickView Error: plugin does not load"
|
|
|
|
return
|
|
|
|
func = eval('mod.' + pdata.runfunc)
|
2008-04-18 09:05:38 +05:30
|
|
|
if handle:
|
2009-06-15 21:48:16 +05:30
|
|
|
d = TextBufDoc(make_basic_stylesheet(), None)
|
2007-12-21 11:52:46 +05:30
|
|
|
d.dbstate = dbstate
|
|
|
|
d.uistate = uistate
|
2008-05-26 01:25:47 +05:30
|
|
|
if isinstance(handle, basestring): # a handle
|
2007-12-30 21:09:00 +05:30
|
|
|
if category == CATEGORY_QR_PERSON :
|
|
|
|
obj = dbstate.db.get_person_from_handle(handle)
|
|
|
|
elif category == CATEGORY_QR_FAMILY :
|
|
|
|
obj = dbstate.db.get_family_from_handle(handle)
|
|
|
|
elif category == CATEGORY_QR_EVENT :
|
|
|
|
obj = dbstate.db.get_event_from_handle(handle)
|
|
|
|
elif category == CATEGORY_QR_SOURCE :
|
|
|
|
obj = dbstate.db.get_source_from_handle(handle)
|
|
|
|
elif category == CATEGORY_QR_PLACE :
|
|
|
|
obj = dbstate.db.get_place_from_handle(handle)
|
2009-12-31 20:29:44 +05:30
|
|
|
elif category == CATEGORY_QR_MEDIA :
|
|
|
|
obj = dbstate.db.get_object_from_handle(handle)
|
2007-12-30 21:09:00 +05:30
|
|
|
elif category == CATEGORY_QR_REPOSITORY :
|
|
|
|
obj = dbstate.db.get_repository_from_handle(handle)
|
2009-12-31 20:29:44 +05:30
|
|
|
elif category == CATEGORY_QR_NOTE :
|
|
|
|
obj = dbstate.db.get_note_from_handle(handle)
|
2008-03-05 09:31:27 +05:30
|
|
|
elif category == CATEGORY_QR_MISC:
|
|
|
|
obj = handle
|
2007-12-30 21:09:00 +05:30
|
|
|
else:
|
|
|
|
obj = None
|
|
|
|
else: # allow caller to send object directly
|
|
|
|
obj = handle
|
2007-12-30 09:16:39 +05:30
|
|
|
if obj:
|
2008-12-13 22:29:14 +05:30
|
|
|
if container:
|
|
|
|
result = d.open("", container=container)
|
2009-11-07 18:59:35 +05:30
|
|
|
func(dbstate.db, d, obj, **kwargs)
|
2008-12-13 22:29:14 +05:30
|
|
|
return result
|
|
|
|
else:
|
|
|
|
d.open("")
|
2009-11-07 18:59:35 +05:30
|
|
|
retval = func(dbstate.db, d, obj, **kwargs)
|
2008-12-13 22:29:14 +05:30
|
|
|
d.close()
|
|
|
|
return retval
|
2008-04-18 09:05:38 +05:30
|
|
|
else:
|
|
|
|
print "QuickView Error: failed to run report: no obj"
|
|
|
|
else:
|
|
|
|
print "QuickView Error: handle is not set"
|