reportlab/platypus/tables.py
author rgbecker
Thu, 17 Feb 2000 02:09:05 +0000
changeset 16 f9c7525619fb
parent 7 ca9eb4f68eab
child 122 fd68411f49db
permissions -rwxr-xr-x
Docstring & other fixes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
     1
###############################################################################
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
     2
#
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
     3
#	ReportLab Public License Version 1.0
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
     4
#
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
     5
#   Except for the change of names the spirit and intention of this
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
     6
#   license is the same as that of Python
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
     7
#
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
     8
#	(C) Copyright ReportLab Inc. 1998-2000.
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
     9
#
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    10
#
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    11
# All Rights Reserved
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    12
#
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    13
# Permission to use, copy, modify, and distribute this software and its
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    14
# documentation for any purpose and without fee is hereby granted, provided
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    15
# that the above copyright notice appear in all copies and that both that
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    16
# copyright notice and this permission notice appear in supporting
7
ca9eb4f68eab License text fixes
rgbecker
parents: 6
diff changeset
    17
# documentation, and that the name of ReportLab not be used
6
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    18
# in advertising or publicity pertaining to distribution of the software
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    19
# without specific, written prior permission. 
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    20
# 
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    21
#
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    22
# Disclaimer
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    23
#
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    24
# ReportLab Inc. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    25
# SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS,
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    26
# IN NO EVENT SHALL ReportLab BE LIABLE FOR ANY SPECIAL, INDIRECT
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    27
# OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    28
# OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    29
# OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    30
# PERFORMANCE OF THIS SOFTWARE. 
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    31
#
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    32
###############################################################################
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    33
#	$Log: tables.py,v $
16
f9c7525619fb Docstring & other fixes
rgbecker
parents: 7
diff changeset
    34
#	Revision 1.4  2000/02/17 02:09:05  rgbecker
f9c7525619fb Docstring & other fixes
rgbecker
parents: 7
diff changeset
    35
#	Docstring & other fixes
f9c7525619fb Docstring & other fixes
rgbecker
parents: 7
diff changeset
    36
#
7
ca9eb4f68eab License text fixes
rgbecker
parents: 6
diff changeset
    37
#	Revision 1.3  2000/02/15 17:55:59  rgbecker
ca9eb4f68eab License text fixes
rgbecker
parents: 6
diff changeset
    38
#	License text fixes
16
f9c7525619fb Docstring & other fixes
rgbecker
parents: 7
diff changeset
    39
#	
6
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    40
#	Revision 1.2  2000/02/15 15:47:09  rgbecker
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    41
#	Added license, __version__ and Logi comment
7
ca9eb4f68eab License text fixes
rgbecker
parents: 6
diff changeset
    42
#	
16
f9c7525619fb Docstring & other fixes
rgbecker
parents: 7
diff changeset
    43
__version__=''' $Id: tables.py,v 1.4 2000/02/17 02:09:05 rgbecker Exp $ '''
f9c7525619fb Docstring & other fixes
rgbecker
parents: 7
diff changeset
    44
__doc__="""
6
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    45
Tables are created by passing the constructor a tuple of column widths, a tuple of row heights and the data in
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    46
row order. Drawing of the table can be controlled by using a TableStyle instance. This allows control of the
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    47
color and weight of the lines (if any), and the font, alignment and padding of the text.
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    48
"""
16
f9c7525619fb Docstring & other fixes
rgbecker
parents: 7
diff changeset
    49
from reportlab.platypus import layout
6
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    50
import operator
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    51
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    52
_stringtype = type('')
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    53
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    54
class CellStyle(layout.PropertySet):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    55
    defaults = {
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    56
        'fontname':'Times-Roman',
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    57
        'fontsize':10,
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    58
        'leading':12,
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    59
        'leftPadding':6,
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    60
        'rightPadding':6,
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    61
        'topPadding':3,
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    62
        'bottomPadding':3,
6
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    63
        'firstLineIndent':0,
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    64
        'color':(0,0,0),
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    65
        'alignment': 'LEFT',
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    66
        'background': (1,1,1),
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    67
        }
6
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    68
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    69
class TableStyle:
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    70
    def __init__(self, cmds=None):
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    71
        self._cmds = cmds
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    72
        if cmds is None:
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    73
            self._cmds = []
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    74
    def add(self, *cmd):
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    75
        self._cmds.append(cmd)
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    76
    def getCommands(self):
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    77
        return self._cmds
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    78
        
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    79
class Table(layout.Drawable):
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    80
    def __init__(self, colWidths, rowHeights, data):
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    81
        if not colWidths:
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    82
            raise ValueError, "Table must have at least 1 column"
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    83
        if not rowHeights:
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    84
            raise ValueError, "Table must have at least 1 row"
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    85
        nrows = self._nrows = len(rowHeights)
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    86
        if len(data) != nrows:
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    87
            raise ValueError, "Data error - %d rows in data but %d in grid" % (len(data), nrows)
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    88
        ncols = self._ncols = len(colWidths)
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    89
        for i in range(nrows):
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    90
            if len(data[i]) != ncols:
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    91
                raise ValueError, "Not enough data points in row %d!" % i
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    92
        self._rowHeights = rowHeights
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    93
        self._colWidths = colWidths
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    94
        self._cellvalues = data
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    95
        dflt = CellStyle('<default>')
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    96
        self._cellstyles = [None]*nrows
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    97
        for i in range(nrows):
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    98
            self._cellstyles[i] = [dflt]*ncols
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    99
        self._bkgrndcmds = []
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   100
        self._linecmds = []
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   101
        height = self._height = reduce(operator.add, rowHeights, 0)
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   102
        self._rowpositions = [height]    # index 0 is actually topline; we skip when processing cells
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   103
        for h in rowHeights:
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   104
            height = height - h
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   105
            self._rowpositions.append(height)
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   106
        assert height == 0
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   107
        width = 0
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   108
        self._colpositions = [0]        #index -1 is right side boundary; we skip when processing cells
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   109
        for w in colWidths:
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   110
            width = width + w
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   111
            self._colpositions.append(width)
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   112
        self._width = width
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   113
        self._curweight = self._curcolor = self._curcellstyle = None
6
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   114
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   115
    def setStyle(self, tblstyle):
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   116
        for cmd in tblstyle.getCommands():
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   117
            if cmd[0] == 'BACKGROUND':
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   118
                self._bkgrndcmds.append(cmd)
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   119
            elif _isLineCommand(cmd):
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   120
                self._linecmds.append(cmd)
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   121
            else:
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   122
                (op, (sc, sr), (ec, er)), values = cmd[:3] , cmd[3:]
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   123
                if sc < 0: sc = sc + self._ncols
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   124
                if ec < 0: ec = ec + self._ncols
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   125
                if sr < 0: sr = sr + self._nrows
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   126
                if er < 0: er = er + self._nrows
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   127
                for i in range(sr, er+1):
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   128
                    for j in range(sc, ec+1):
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   129
                        _setCellStyle(self._cellstyles, i, j, op, values)
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   130
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   131
    def _drawLines(self):
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   132
        for op, (sc, sr), (ec, er), weight, color in self._linecmds:
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   133
            if sc < 0: sc = sc + self._ncols
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   134
            if ec < 0: ec = ec + self._ncols
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   135
            if sr < 0: sr = sr + self._nrows
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   136
            if er < 0: er = er + self._nrows
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   137
            if op == 'GRID':
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   138
                self._drawBox( (sc, sr), (ec, er), weight, color)
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   139
                self._drawInnerGrid( (sc, sr), (ec, er), weight, color)
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   140
            elif op in ('BOX',  'OUTLINE',):
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   141
                self._drawBox( (sc, sr), (ec, er), weight, color)
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   142
            elif op == 'INNERGRID':
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   143
                self._drawInnerGrid( (sc, sr), (ec, er), weight, color)
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   144
            elif op == 'LINEBELOW':
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   145
                self._drawHLines((sc, sr+1), (ec, er+1), weight, color)
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   146
            elif op == 'LINEABOVE':
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   147
                self._drawHLines((sc, sr), (ec, er), weight, color)
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   148
            elif op == 'LINEBEFORE':
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   149
                self._drawVLines((sc, sr), (ec, er), weight, color)
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   150
            elif op == 'LINEAFTER':
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   151
                self._drawVLines((sc+1, sr), (ec+1, er), weight, color)
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   152
            else:
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   153
                raise ValueError, "Unknown line style %s" % op
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   154
        self._curcolor = None
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   155
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   156
    def _drawBox(self,  (sc, sr), (ec, er), weight, color):
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   157
        self._drawHLines((sc, sr), (ec, sr), weight, color)
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   158
        self._drawHLines((sc, er+1), (ec, er+1), weight, color)
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   159
        self._drawVLines((sc, sr), (sc, er), weight, color)
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   160
        self._drawVLines((ec+1, sr), (ec+1, er), weight, color)
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   161
    def _drawInnerGrid(self, (sc, sr), (ec, er), weight, color):
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   162
        self._drawHLines((sc, sr+1), (ec, er), weight, color)
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   163
        self._drawVLines((sc+1, sr), (ec, er), weight, color)
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   164
    def _prepLine(self, weight, color):
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   165
        if type(color) is _stringtype:
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   166
            color = COLORS.get(color, (0,0,0))
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   167
        if color != self._curcolor:
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   168
            apply(self.canv.setStrokeColorRGB, color)
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   169
            self._curcolor = color
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   170
        if weight != self._curweight:
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   171
            self.canv.setLineWidth(weight)
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   172
            self._curweight = weight
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   173
    def _drawHLines(self, (sc, sr), (ec, er), weight, color):
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   174
        self._prepLine(weight, color)
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   175
        scp = self._colpositions[sc]
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   176
        ecp = self._colpositions[ec+1]
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   177
        for rowpos in self._rowpositions[sr:er+1]:
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   178
            self.canv.line(scp, rowpos, ecp, rowpos)
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   179
    def _drawVLines(self, (sc, sr), (ec, er), weight, color):
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   180
        self._prepLine(weight, color)
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   181
        srp = self._rowpositions[sr]
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   182
        erp = self._rowpositions[er+1]
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   183
        for colpos in self._colpositions[sc:ec+1]:
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   184
            self.canv.line(colpos, srp, colpos, erp)
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   185
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   186
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   187
    def wrap(self, availWidth, availHeight):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   188
        #nice and easy, since they are predetermined size
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   189
        self.availWidth = availWidth
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   190
        return (self._width, self._height)
6
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   191
                
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   192
    def draw(self):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   193
        nudge = 0.5 * (self.availWidth - self._width)
6
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   194
        self.canv.translate(nudge, 0)
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   195
        self._drawBkgrnd()
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   196
        self._drawLines()
6
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   197
        for row, rowstyle, rowpos, rowheight in map(None, self._cellvalues, self._cellstyles, self._rowpositions[1:], self._rowHeights):
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   198
            for cellval, cellstyle, colpos, colwidth in map(None, row, rowstyle, self._colpositions[:-1], self._colWidths):
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   199
                self._drawCell(cellval, cellstyle, (colpos, rowpos), (colwidth, rowheight))
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   200
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   201
    def _drawBkgrnd(self):
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   202
        for cmd, (sc, sr), (ec, er), color in self._bkgrndcmds:
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   203
            if sc < 0: sc = sc + self._ncols
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   204
            if ec < 0: ec = ec + self._ncols
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   205
            if sr < 0: sr = sr + self._nrows
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   206
            if er < 0: er = er + self._nrows
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   207
            if type(color) is _stringtype:
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   208
                color = COLORS.get(values[0], (1,1,1))
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   209
            x0 = self._colpositions[sc]
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   210
            y0 = self._rowpositions[sr]
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   211
            x1 = self._colpositions[ec+1]
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   212
            y1 = self._rowpositions[er+1]
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   213
            apply(self.canv.setFillColorRGB, color)
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   214
            self.canv.rect(x0, y0, x1-x0, y1-y0,stroke=0,fill=1)
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   215
    def _drawCell(self, cellval, cellstyle, (colpos, rowpos), (colwidth, rowheight)):
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   216
        #print "cellstyle is ", repr(cellstyle), id(cellstyle)
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   217
        if self._curcellstyle is not cellstyle:
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   218
            cur = self._curcellstyle
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   219
            if cur is None or cellstyle.color != cur.color:
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   220
                #print "setting cell color to %s" % `cellstyle.color`
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   221
                apply(self.canv.setFillColorRGB, cellstyle.color)
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   222
            if cur is None or cellstyle.leading != cur.leading or cellstyle.fontname != cur.fontname or cellstyle.fontsize != cur.fontsize:
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   223
                #print "setting font: %s, %s, %s" % (cellstyle.fontname, cellstyle.fontsize, cellstyle.leading)
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   224
                self.canv.setFont(cellstyle.fontname, cellstyle.fontsize, cellstyle.leading)
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   225
            self._curcellstyle = cellstyle
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   226
        just = cellstyle.alignment
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   227
        #print "alignment is ", just
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   228
        if just == 'LEFT':
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   229
            draw = self.canv.drawString
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   230
            x = colpos + cellstyle.leftPadding
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   231
        elif just in ('CENTRE', 'CENTER'):
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   232
            draw = self.canv.drawCentredString
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   233
            x = colpos + colwidth * 0.5
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   234
        else:
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   235
            draw = self.canv.drawRightString
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   236
            x = colpos + colwidth - cellstyle.rightPadding
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   237
        y = rowpos + cellstyle.bottomPadding
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   238
        if type(cellval) is _stringtype:
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   239
            val = cellval
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   240
        else:
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   241
            val = str(cellval)
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   242
        draw(x, y, val)
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   243
        
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   244
# for text,
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   245
#   drawCentredString(self, x, y, text) where x is center
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   246
#   drawRightString(self, x, y, text) where x is right
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   247
#   drawString(self, x, y, text) where x is left
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   248
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   249
LINECOMMANDS = (
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   250
    'GRID', 'BOX', 'OUTLINE', 'INNERGRID', 'LINEBELOW', 'LINEABOVE', 'LINEBEFORE', 'LINEAFTER', )
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   251
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   252
COLORS = {
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   253
    'BLACK': (0,0,0),
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   254
    'RED': (1,0,0),
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   255
    'GREEN': (0,1,0),
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   256
    'BLUE': (0,0,1),
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   257
    'WHITE':(1,1,1),
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   258
    }
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   259
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   260
def _isLineCommand(cmd):
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   261
    return cmd[0] in LINECOMMANDS
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   262
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   263
def _setCellStyle(cellstyles, i, j, op, values):
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   264
    new = CellStyle('<%d, %d>' % (i,j), cellstyles[i][j])
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   265
    cellstyles[i][j] = new
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   266
    if op == 'FONT':
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   267
        new.fontname = values[0]
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   268
        new.fontsize = values[1]
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   269
    elif op == 'TEXTCOLOR':
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   270
        if type(values[0]) is _stringtype:
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   271
            new.color = COLORS.get(values[0], (0,0,0))
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   272
        else:
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   273
            new.color = values[0]
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   274
    elif op in ('ALIGN', 'ALIGNMENT'):
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   275
        new.alignment = values[0]
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   276
    elif op == 'LEFTPADDING':
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   277
        new.leftPadding = values[0]
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   278
    elif op == 'RIGHTPADDING':
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   279
        new.rightPadding = values[0]
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   280
    elif op == 'TOPPADDING':
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   281
        new.topPadding = values[0]
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   282
    elif op == 'BOTTOMPADDING':
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   283
        new.bottomPadding = values[0]
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   284
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   285
GRID_STYLE = TableStyle(
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   286
    [('GRID', (0,0), (-1,-1), 0.25, 'BLACK'),
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   287
     ('ALIGN', (1,1), (-1,-1), 'RIGHT')]
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   288
    )
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   289
BOX_STYLE = TableStyle(
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   290
    [('BOX', (0,0), (-1,-1), 0.50, 'BLACK'),
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   291
     ('ALIGN', (1,1), (-1,-1), 'RIGHT')]
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   292
    )
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   293
LABELED_GRID_STYLE = TableStyle(
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   294
    [('INNERGRID', (0,0), (-1,-1), 0.25, 'BLACK'),
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   295
     ('BOX', (0,0), (-1,-1), 2, 'BLACK'),
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   296
     ('LINEBELOW', (0,0), (-1,0), 2, 'BLACK'),
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   297
     ('LINEAFTER', (0,0), (0,-1), 2, 'BLACK'),
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   298
     ('ALIGN', (1,1), (-1,-1), 'RIGHT')]
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   299
    )
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   300
COLORED_GRID_STYLE = TableStyle(
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   301
    [('INNERGRID', (0,0), (-1,-1), 0.25, 'BLACK'),
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   302
     ('BOX', (0,0), (-1,-1), 2, 'RED'),
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   303
     ('LINEBELOW', (0,0), (-1,0), 2, 'BLACK'),
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   304
     ('LINEAFTER', (0,0), (0,-1), 2, 'BLACK'),
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   305
     ('ALIGN', (1,1), (-1,-1), 'RIGHT')]
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   306
    )
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   307
LIST_STYLE = TableStyle(
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   308
    [('LINEABOVE', (0,0), (-1,0), 2, 'GREEN'),
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   309
     ('LINEABOVE', (0,1), (-1,-1), 0.25, 'BLACK'),
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   310
     ('LINEBELOW', (0,-1), (-1,-1), 2, 'GREEN'),
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   311
     ('ALIGN', (1,1), (-1,-1), 'RIGHT')]
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   312
    )
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   313
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   314
def test():
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   315
    rowheights = (24, 16, 16, 16, 16)
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   316
    colwidths = (50, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32)
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   317
    data = (
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   318
        ('', 'Jan', 'Feb', 'Mar','Apr','May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'),
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   319
        ('Mugs', 0, 4, 17, 3, 21, 47, 12, 33, 2, -2, 44, 89),
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   320
        ('T-Shirts', 0, 42, 9, -3, 16, 4, 72, 89, 3, 19, 32, 119),
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   321
        ('Key Ring', 0,0,0,0,0,0,1,0,0,0,2,13),
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   322
        ('Hats', 893, 912, '1,212', 643, 789, 159, 888, '1,298', 832, 453, '1,344','2,843')
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   323
        )
16
f9c7525619fb Docstring & other fixes
rgbecker
parents: 7
diff changeset
   324
    doc = layout.SimpleFlowDocument('testtables.pdf', layout.DEFAULT_PAGE_SIZE, 1)
6
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   325
    styleSheet = layout.getSampleStyleSheet()
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   326
    lst = []
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   327
    lst.append(layout.Paragraph("Tables", styleSheet['Heading1']))
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   328
    lst.append(layout.Paragraph(__doc__, styleSheet['BodyText']))
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   329
    lst.append(layout.Paragraph("The Tables (shown in different styles below) were created using the following code:", styleSheet['BodyText']))
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   330
    lst.append(layout.Preformatted("""
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   331
    colwidths = (50, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32)
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   332
    rowheights = (24, 16, 16, 16, 16)
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   333
    data = (
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   334
        ('', 'Jan', 'Feb', 'Mar','Apr','May', 'Jun',
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   335
           'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'),
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   336
        ('Mugs', 0, 4, 17, 3, 21, 47, 12, 33, 2, -2, 44, 89),
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   337
        ('T-Shirts', 0, 42, 9, -3, 16, 4, 72, 89, 3, 19, 32, 119),
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   338
        ('Key Ring', 0,0,0,0,0,0,1,0,0,0,2,13),
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   339
        ('Hats', 893, 912, '1,212', 643, 789, 159,
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   340
             888, '1,298', 832, 453, '1,344','2,843')
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   341
        )   
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   342
    t = Table(colwidths, rowheights,  data)
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   343
    """, styleSheet['Code'], dedent=4))
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   344
    lst.append(layout.Paragraph("""
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   345
    You can then give the Table a TableStyle object to control its format. The first TableStyle used was
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   346
    created as follows:
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   347
    """, styleSheet['BodyText']))
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   348
    lst.append(layout.Preformatted("""
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   349
GRID_STYLE = TableStyle(
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   350
    [('GRID', (0,0), (-1,-1), 0.25, 'BLACK'),
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   351
     ('ALIGN', (1,1), (-1,-1), 'RIGHT')]
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   352
    )
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   353
    """, styleSheet['Code']))
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   354
    lst.append(layout.Paragraph("""
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   355
    TableStyles are created by passing in a list of commands. There are two types of commands - line commands
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   356
    and cell formatting commands. In all cases, the first three elements of a command are the command name,
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   357
    the starting cell and the ending cell.
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   358
    """, styleSheet['BodyText']))
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   359
    lst.append(layout.Paragraph("""
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   360
    Line commands always follow this with the weight and color of the desired lines. Colors can be names,
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   361
    or they can be specified as a (R,G,B) tuple, where R, G and B are floats and (0,0,0) is black. The line
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   362
    command names are: GRID, BOX, OUTLINE, INNERGRID, LINEBELOW, LINEABOVE, LINEBEFORE
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   363
    and LINEAFTER. BOX and OUTLINE are equivalent, and GRID is the equivalent of applying both BOX and
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   364
    INNERGRID.
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   365
    """, styleSheet['BodyText']))
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   366
    lst.append(layout.Paragraph("""
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   367
    Cell formatting commands are:
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   368
    """, styleSheet['BodyText']))
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   369
    lst.append(layout.Paragraph("""
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   370
    FONT - takes fontname, fontsize and (optional) leading.
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   371
    """, styleSheet['Definition']))
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   372
    lst.append(layout.Paragraph("""
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   373
    TEXTCOLOR - takes a color name or (R,G,B) tuple.
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   374
    """, styleSheet['Definition']))
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   375
    lst.append(layout.Paragraph("""
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   376
    ALIGNMENT (or ALIGN) - takes one of LEFT, RIGHT and CENTRE (or CENTER).
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   377
    """, styleSheet['Definition']))
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   378
    lst.append(layout.Paragraph("""
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   379
    LEFTPADDING - defaults to 6.
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   380
    """, styleSheet['Definition']))
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   381
    lst.append(layout.Paragraph("""
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   382
    RIGHTPADDING - defaults to 6.
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   383
    """, styleSheet['Definition']))
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   384
    lst.append(layout.Paragraph("""
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   385
    BOTTOMPADDING - defaults to 3.
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   386
    """, styleSheet['Definition']))
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   387
    lst.append(layout.Paragraph("""
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   388
    A tablestyle is applied to a table by calling Table.setStyle(tablestyle).
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   389
    """, styleSheet['BodyText']))
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   390
    t = Table(colwidths, rowheights,  data)
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   391
    t.setStyle(GRID_STYLE)
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   392
    lst.append(layout.PageBreak())
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   393
    lst.append(layout.Paragraph("This is GRID_STYLE\n", styleSheet['BodyText']))
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   394
    lst.append(t)
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   395
    
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   396
    t = Table(colwidths, rowheights,  data)
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   397
    t.setStyle(BOX_STYLE)
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   398
    lst.append(layout.Paragraph("This is BOX_STYLE\n", styleSheet['BodyText']))
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   399
    lst.append(t)
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   400
    lst.append(layout.Paragraph("""
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   401
    It was created as follows:
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   402
    """, styleSheet['BodyText']))
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   403
    lst.append(layout.Preformatted("""
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   404
BOX_STYLE = TableStyle(
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   405
    [('BOX', (0,0), (-1,-1), 0.50, 'BLACK'),
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   406
     ('ALIGN', (1,1), (-1,-1), 'RIGHT')]
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   407
    )
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   408
    """, styleSheet['Code']))
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   409
    
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   410
    t = Table(colwidths, rowheights,  data)
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   411
    t.setStyle(LABELED_GRID_STYLE)
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   412
    lst.append(layout.Paragraph("This is LABELED_GRID_STYLE\n", styleSheet['BodyText']))
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   413
    lst.append(t)
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   414
    lst.append(layout.Paragraph("""
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   415
    It was created as follows:
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   416
    """, styleSheet['BodyText']))
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   417
    lst.append(layout.Preformatted("""
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   418
LABELED_GRID_STYLE = TableStyle(
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   419
    [('INNERGRID', (0,0), (-1,-1), 0.25, 'BLACK'),
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   420
     ('BOX', (0,0), (-1,-1), 2, 'BLACK'),
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   421
     ('LINEBELOW', (0,0), (-1,0), 2, 'BLACK'),
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   422
     ('LINEAFTER', (0,0), (0,-1), 2, 'BLACK'),
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   423
     ('ALIGN', (1,1), (-1,-1), 'RIGHT')]
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   424
    )
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   425
    """, styleSheet['Code']))
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   426
    lst.append(layout.PageBreak())
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   427
    
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   428
    t = Table(colwidths, rowheights,  data)
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   429
    t.setStyle(COLORED_GRID_STYLE)
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   430
    lst.append(layout.Paragraph("This is COLORED_GRID_STYLE\n", styleSheet['BodyText']))
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   431
    lst.append(t)
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   432
    lst.append(layout.Paragraph("""
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   433
    It was created as follows:
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   434
    """, styleSheet['BodyText']))
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   435
    lst.append(layout.Preformatted("""
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   436
COLORED_GRID_STYLE = TableStyle(
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   437
    [('INNERGRID', (0,0), (-1,-1), 0.25, 'BLACK'),
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   438
     ('BOX', (0,0), (-1,-1), 2, 'RED'),
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   439
     ('LINEBELOW', (0,0), (-1,0), 2, 'BLACK'),
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   440
     ('LINEAFTER', (0,0), (0,-1), 2, 'BLACK'),
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   441
     ('ALIGN', (1,1), (-1,-1), 'RIGHT')]
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   442
    )
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   443
    """, styleSheet['Code']))
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   444
    
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   445
    t = Table(colwidths, rowheights,  data)
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   446
    t.setStyle(LIST_STYLE)
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   447
    lst.append(layout.Paragraph("This is LIST_STYLE\n", styleSheet['BodyText']))
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   448
    lst.append(t)
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   449
    lst.append(layout.Paragraph("""
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   450
    It was created as follows:
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   451
    """, styleSheet['BodyText']))
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   452
    lst.append(layout.Preformatted("""
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   453
LIST_STYLE = TableStyle(
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   454
    [('LINEABOVE', (0,0), (-1,0), 2, 'GREEN'),
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   455
     ('LINEABOVE', (0,1), (-1,-1), 0.25, 'BLACK'),
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   456
     ('LINEBELOW', (0,-1), (-1,-1), 2, 'GREEN'),
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   457
     ('ALIGN', (1,1), (-1,-1), 'RIGHT')]
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   458
    )
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   459
    """, styleSheet['Code']))
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   460
   
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   461
    t = Table(colwidths, rowheights,  data)
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   462
    ts = TableStyle(
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   463
    [('LINEABOVE', (0,0), (-1,0), 2, 'GREEN'),
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   464
     ('LINEABOVE', (0,1), (-1,-1), 0.25, 'BLACK'),
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   465
     ('LINEBELOW', (0,-1), (-1,-1), 2, 'GREEN'),
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   466
     ('ALIGN', (1,1), (-1,-1), 'RIGHT'),
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   467
     ('TEXTCOLOR', (0,1), (0,-1), 'RED'),
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   468
     ('BACKGROUND', (0,0), (-1,0), (0,0.7,0.7))]
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   469
    )
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   470
    t.setStyle(ts)
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   471
    lst.append(layout.Paragraph("This is a custom style\n", styleSheet['BodyText']))
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   472
    lst.append(t)
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   473
    lst.append(layout.Paragraph("""
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   474
    It was created as follows:
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   475
    """, styleSheet['BodyText']))
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   476
    lst.append(layout.Preformatted("""
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   477
   ts = TableStyle(
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   478
    [('LINEABOVE', (0,0), (-1,0), 2, 'GREEN'),
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   479
     ('LINEABOVE', (0,1), (-1,-1), 0.25, 'BLACK'),
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   480
     ('LINEBELOW', (0,-1), (-1,-1), 2, 'GREEN'),
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   481
     ('ALIGN', (1,1), (-1,-1), 'RIGHT'),
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   482
     ('TEXTCOLOR', (0,1), (0,-1), 'RED'),
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   483
     ('BACKGROUND', (0,0), (-1,0), (0,0.7,0.7))]
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   484
    )
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   485
    """, styleSheet['Code']))
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   486
    doc.build(lst)
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   487
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   488
if __name__ == '__main__':
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   489
    test()