1#
2# Gramps - a GTK+/GNOME based genealogy program
3#
4# Copyright (C) 2007  Donald N. Allingham
5#
6# This program is free software; you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by
8# the Free Software Foundation; either version 2 of the License, or
9# (at your option) any later version.
10#
11# This program is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14# GNU General Public License for more details.
15#
16# You should have received a copy of the GNU General Public License
17# along with this program; if not, write to the Free Software
18# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19#
20
21"""
22Provide a simplified database access interface to the Gramps database.
23"""
24from ..plug.docgen import StyleSheet, ParagraphStyle, TableStyle,\
25                            TableCellStyle,  FONT_SANS_SERIF, PARA_ALIGN_LEFT
26
27class SimpleDoc:
28    """
29    Provide a simplified database access interface to the Gramps database.
30    """
31
32    def __init__(self, doc):
33        """
34        Initialize the class with the real document
35        """
36        self.doc = doc
37
38    def __write(self, format, text):
39        """
40        Writes a paragraph using the specified format to the BaseDoc
41        """
42        self.doc.start_paragraph(format)
43        self.doc.write_text(text)
44        self.doc.end_paragraph()
45
46    def title(self, text):
47        """
48        Writes the Title using the Title paragraph
49        """
50        self.__write('Title', text)
51
52    def header1(self, text):
53        """
54        Writes the first level header using the Header1 paragraph
55        """
56        self.__write('Header1', text)
57
58    def header2(self, text):
59        """
60        Writes the second level header using the Header2 paragraph
61        """
62        self.__write('Header2', text)
63
64    def header3(self, text):
65        """
66        Writes the third level header using the Header3 paragraph
67        """
68        self.__write('Header3', text)
69
70    def paragraph(self, text):
71        """
72        Writes a paragraph using the Normal format
73        """
74        self.__write('Normal', text)
75
76def make_basic_stylesheet(**kwargs):
77    """
78    Create the basic style sheet for the SimpleDoc class.
79
80    kwargs - a dictionary of the form:
81           item={method: value, ...}, ...
82
83    Example:
84
85    make_basic_stylesheet(Table={"set_width": 90})
86
87    """
88    sheet = StyleSheet()
89
90    pstyle = ParagraphStyle()
91    fstyle = pstyle.get_font()
92    fstyle.set_type_face(FONT_SANS_SERIF)
93    fstyle.set_size(14)
94    fstyle.set_bold(True)
95    pstyle.set_font(fstyle)
96    pstyle.set_alignment(PARA_ALIGN_LEFT)
97    # Handle args:
98    if "Title" in kwargs:
99        for method in kwargs["Title"]:
100            value = kwargs["Title"][method]
101            if value is not None:
102                getattr(pstyle, method)(value)
103    sheet.add_paragraph_style('Title', pstyle)
104
105    pstyle = ParagraphStyle()
106    fstyle = pstyle.get_font()
107    fstyle.set_type_face(FONT_SANS_SERIF)
108    fstyle.set_size(12)
109    fstyle.set_bold(True)
110    pstyle.set_font(fstyle)
111    pstyle.set_alignment(PARA_ALIGN_LEFT)
112    pstyle.set_tabs([4, 8, 12, 16])
113    # Handle args:
114    if "Header1" in kwargs:
115        for method in kwargs["Header1"]:
116            value = kwargs["Header1"][method]
117            if value is not None:
118                getattr(pstyle, method)(value)
119    sheet.add_paragraph_style('Header1', pstyle)
120
121    pstyle = ParagraphStyle()
122    fstyle = pstyle.get_font()
123    fstyle.set_type_face(FONT_SANS_SERIF)
124    fstyle.set_size(10)
125    fstyle.set_bold(True)
126    pstyle.set_font(fstyle)
127    pstyle.set_alignment(PARA_ALIGN_LEFT)
128    pstyle.set_tabs([4, 8, 12, 16])
129    # Handle args:
130    if "Header2" in kwargs:
131        for method in kwargs["Header2"]:
132            value = kwargs["Header2"][method]
133            if value is not None:
134                getattr(pstyle, method)(value)
135    sheet.add_paragraph_style('Header2', pstyle)
136
137    pstyle = ParagraphStyle()
138    fstyle = pstyle.get_font()
139    fstyle.set_type_face(FONT_SANS_SERIF)
140    fstyle.set_size(10)
141    fstyle.set_bold(True)
142    fstyle.set_italic(True)
143    pstyle.set_font(fstyle)
144    pstyle.set_alignment(PARA_ALIGN_LEFT)
145    pstyle.set_tabs([4, 8, 12, 16])
146    # Handle args:
147    if "Header3" in kwargs:
148        for method in kwargs["Header3"]:
149            value = kwargs["Header3"][method]
150            if value is not None:
151                getattr(pstyle, method)(value)
152    sheet.add_paragraph_style('Header3', pstyle)
153
154    pstyle = ParagraphStyle()
155    pstyle.set_tabs([4, 8, 12, 16])
156    # Handle args:
157    if "Normal" in kwargs:
158        for method in kwargs["Normal"]:
159            value = kwargs["Normal"][method]
160            if value is not None:
161                getattr(pstyle, method)(value)
162    sheet.add_paragraph_style('Normal', pstyle)
163
164    # Styles for tables:
165    tbl = TableStyle()
166    tbl.set_width(100)
167    tbl.set_columns(2)
168    tbl.set_column_width(0,20)
169    tbl.set_column_width(1,80)
170    # Handle args:
171    if "Table" in kwargs:
172        for method in kwargs["Table"]:
173            value = kwargs["Table"][method]
174            if value is not None:
175                getattr(tbl, method)(value)
176    sheet.add_table_style("Table",tbl)
177
178    cell = TableCellStyle()
179    cell.set_top_border(1)
180    cell.set_bottom_border(1)
181    # Handle args:
182    if "TableHead" in kwargs:
183        for method in kwargs["TableHead"]:
184            value = kwargs["TableHead"][method]
185            if value is not None:
186                getattr(cell, method)(value)
187    sheet.add_cell_style("TableHead",cell)
188
189    cell = TableCellStyle()
190    # Handle args:
191    if "TableHeaderCell" in kwargs:
192        for method in kwargs["TableHeaderCell"]:
193            value = kwargs["TableHeaderCell"][method]
194            if value is not None:
195                getattr(cell, method)(value)
196    sheet.add_cell_style("TableHeaderCell",cell)
197
198    cell = TableCellStyle()
199    cell.set_longlist(1)
200    # Handle args:
201    if "TableDataCell" in kwargs:
202        for method in kwargs["TableDataCell"]:
203            value = kwargs["TableDataCell"][method]
204            if value is not None:
205                getattr(cell, method)(value)
206    sheet.add_cell_style("TableDataCell",cell)
207
208    return sheet
209