# # Gramps - a GTK+/GNOME based genealogy program # # Copyright (C) 2004-2007 Donald N. Allingham # Copyright (C) 2008 Gary Burton # # 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 # # $Id$ # Written by Alex Roitman """ Report option handling, including saving and parsing. """ #------------------------------------------------------------------------- # # Standard Python modules # #------------------------------------------------------------------------- import os import copy from xml.sax.saxutils import escape def escxml(d): return escape(d, { '"' : '"' } ) #------------------------------------------------------------------------- # # SAX interface # #------------------------------------------------------------------------- try: from xml.sax import make_parser, SAXParseException except: from _xmlplus.sax import make_parser, SAXParseException #------------------------------------------------------------------------- # # gramps modules # #------------------------------------------------------------------------- import const import Config from gen.plug.docgen import PAPER_PORTRAIT from PluginUtils import _Options, GuiMenuOptions #------------------------------------------------------------------------- # # List of options for a single report # #------------------------------------------------------------------------- class OptionList(_Options.OptionList): """ Implements a set of options to parse and store for a given report. """ def __init__(self): _Options.OptionList.__init__(self) self.style_name = None self.paper_metric = None self.paper_name = None self.orientation = None self.custom_paper_size = [29.7, 21.0] self.margins = [2.54, 2.54, 2.54, 2.54] self.template_name = None self.format_name = None def set_style_name(self, style_name): """ Set the style name for the OptionList. @param style_name: name of the style to set. @type style_name: str """ self.style_name = style_name def get_style_name(self): """ Return the style name of the OptionList. @returns: string representing the style name @rtype: str """ return self.style_name def set_paper_metric(self, paper_metric): """ Set the paper metric for the OptionList. @param paper_metric: whether to use metric. @type paper_name: boolean """ self.paper_metric = paper_metric def get_paper_metric(self): """ Return the paper metric of the OptionList. @returns: returns whether to use metric @rtype: boolean """ return self.paper_metric def set_paper_name(self, paper_name): """ Set the paper name for the OptionList. @param paper_name: name of the paper to set. @type paper_name: str """ self.paper_name = paper_name def get_paper_name(self): """ Return the paper name of the OptionList. @returns: returns the paper name @rtype: str """ return self.paper_name def set_orientation(self, orientation): """ Set the orientation for the OptionList. @param orientation: orientation to set. Possible values are PAPER_LANDSCAPE or PAPER_PORTRAIT @type orientation: int """ self.orientation = orientation def get_orientation(self): """ Return the orientation for the OptionList. @returns: returns the selected orientation. Valid values are PAPER_LANDSCAPE or PAPER_PORTRAIT @rtype: int """ return self.orientation def set_custom_paper_size(self, paper_size): """ Set the custom paper size for the OptionList. @param paper_size: paper size to set in cm. @type paper_size: [float, float] """ self.custom_paper_size = paper_size def get_custom_paper_size(self): """ Return the custom paper size for the OptionList. @returns: returns the custom paper size in cm @rtype: [float, float] """ return self.custom_paper_size def set_margins(self, margins): """ Set the margins for the OptionList. @param margins: margins to set. Possible values are floats in cm @type margins: [float, float, float, float] """ self.margins = copy.copy(margins) def get_margins(self): """ Return the margins for the OptionList. @returns margins: returns the margins, floats in cm @rtype margins: [float, float, float, float] """ return copy.copy(self.margins) def set_margin(self, pos, value): """ Set a margin for the OptionList. @param pos: Position of margin [left, right, top, bottom] @param value: floating point in cm @type pos: int @type value: float """ self.margins[pos] = value def get_margin(self, pos): """ Return a margin for the OptionList. @param pos: Position of margin [left, right, top, bottom] @type pos: int @returns: float cm of margin @rtype: float """ return self.margins[pos] def set_template_name(self, template_name): """ Set the template name for the OptionList. @param template_name: name of the template to set. @type template_name: str """ self.template_name = template_name def get_template_name(self): """ Return the template name of the OptionList. @returns: template name @rtype: str """ return self.template_name def set_format_name(self, format_name): """ Set the format name for the OptionList. @param format_name: name of the format to set. @type format_name: str """ self.format_name = format_name def get_format_name(self): """ Return the format name of the OptionList. @returns: returns the format name @rtype: str """ return self.format_name #------------------------------------------------------------------------- # # Collection of option lists # #------------------------------------------------------------------------- class OptionListCollection(_Options.OptionListCollection): """ Implements a collection of option lists. """ def __init__(self, filename): _Options.OptionListCollection.__init__(self, filename) def init_common(self): # Default values for common options self.default_style_name = "default" self.default_paper_metric = Config.get(Config.PAPER_METRIC) self.default_paper_name = Config.get(Config.PAPER_PREFERENCE) self.default_template_name = "" self.default_orientation = PAPER_PORTRAIT self.default_custom_paper_size = [29.7, 21.0] self.default_margins = [2.54, 2.54, 2.54, 2.54] self.default_format_name = 'print' self.last_paper_metric = self.default_paper_metric self.last_paper_name = self.default_paper_name self.last_orientation = self.default_orientation self.last_custom_paper_size = copy.copy(self.default_custom_paper_size) self.last_margins = copy.copy(self.default_margins) self.last_template_name = self.default_template_name self.last_format_name = self.default_format_name self.option_list_map = {} def set_last_paper_metric(self, paper_metric): """ Set the last paper metric used for the any report in this collection. @param paper_metric: whether to use metric. @type paper_name: boolean """ self.last_paper_metric = paper_metric def get_last_paper_metric(self): """ Return the last paper metric used for the any report in this collection. @returns: returns whether or not to use metric @rtype: boolean """ return self.last_paper_metric def set_last_paper_name(self, paper_name): """ Set the last paper name used for the any report in this collection. @param paper_name: name of the paper to set. @type paper_name: str """ self.last_paper_name = paper_name def get_last_paper_name(self): """ Return the last paper name used for the any report in this collection. @returns: returns the name of the paper @rtype: str """ return self.last_paper_name def set_last_orientation(self, orientation): """ Set the last orientation used for the any report in this collection. @param orientation: orientation to set. @type orientation: int """ self.last_orientation = orientation def get_last_orientation(self): """ Return the last orientation used for the any report in this collection. @returns: last orientation used @rtype: int """ return self.last_orientation def set_last_custom_paper_size(self, custom_paper_size): """ Set the last custom paper size used for the any report in this collection. @param custom_paper_size: size to set in cm (width, height) @type margins: [float, float] """ self.last_custom_paper_size = copy.copy(custom_paper_size) def get_last_custom_paper_size(self): """ Return the last custom paper size used for the any report in this collection. @returns: list of last custom paper size used in cm (width, height) @rtype: [float, float] """ return copy.copy(self.last_custom_paper_size) def set_last_margins(self, margins): """ Set the last margins used for the any report in this collection. @param margins: margins to set in cm (left, right, top, bottom) @type margins: [float, float, float, float] """ self.last_margins = copy.copy(margins) def get_last_margins(self): """ Return the last margins used for the any report in this collection. @returns: list of last margins used in cm (left, right, top, bottom) @rtype: [float, float, float, float] """ return copy.copy(self.last_margins) def set_last_margin(self, pos, value): """ Set the last margin used for the any report in this collection. @param pos: pos to set (0-4) (left, right, top, bottom) @type pos: int @param value: value to set the margin to in cm @type value: float """ self.last_margins[pos] = value def get_last_margin(self, pos): """ Return the last margins used for the any report in this collection. @param pos: position in margins list @type pos: int @returns: last margin used in pos @rtype: float """ return self.last_margins[pos] def set_last_template_name(self, template_name): """ Set the last template used for the any report in this collection. template_name: name of the style to set. """ self.last_template_name = template_name def get_last_template_name(self): """ Return the last template used for the any report in this collection. """ return self.last_template_name def set_last_format_name(self, format_name): """ Set the last format used for the any report in this collection. format_name: name of the format to set. """ self.last_format_name = format_name def get_last_format_name(self): """ Return the last format used for the any report in this collection. """ return self.last_format_name def write_common(self, f): f.write('\n') if self.get_last_paper_metric() != self.default_paper_metric: f.write(' \n' % self.get_last_paper_metric() ) if self.get_last_custom_paper_size() != self.default_custom_paper_size: size = self.get_last_custom_paper_size() f.write(' \n' % (size[0], size[1]) ) if self.get_last_paper_name() != self.default_paper_name: f.write(' \n' % escxml(self.get_last_paper_name()) ) if self.get_last_template_name() != self.default_template_name: f.write('