reportlab/platypus/tables.py
author rgbecker
Fri, 14 Apr 2000 08:56:20 +0000
changeset 122 fd68411f49db
parent 16 f9c7525619fb
child 128 a0676f013314
permissions -rwxr-xr-x
Drawable ==> Flowable
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 $
122
fd68411f49db Drawable ==> Flowable
rgbecker
parents: 16
diff changeset
    34
#	Revision 1.5  2000/04/14 08:56:20  rgbecker
fd68411f49db Drawable ==> Flowable
rgbecker
parents: 16
diff changeset
    35
#	Drawable ==> Flowable
fd68411f49db Drawable ==> Flowable
rgbecker
parents: 16
diff changeset
    36
#
16
f9c7525619fb Docstring & other fixes
rgbecker
parents: 7
diff changeset
    37
#	Revision 1.4  2000/02/17 02:09:05  rgbecker
f9c7525619fb Docstring & other fixes
rgbecker
parents: 7
diff changeset
    38
#	Docstring & other fixes
122
fd68411f49db Drawable ==> Flowable
rgbecker
parents: 16
diff changeset
    39
#	
7
ca9eb4f68eab License text fixes
rgbecker
parents: 6
diff changeset
    40
#	Revision 1.3  2000/02/15 17:55:59  rgbecker
ca9eb4f68eab License text fixes
rgbecker
parents: 6
diff changeset
    41
#	License text fixes
16
f9c7525619fb Docstring & other fixes
rgbecker
parents: 7
diff changeset
    42
#	
6
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    43
#	Revision 1.2  2000/02/15 15:47:09  rgbecker
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    44
#	Added license, __version__ and Logi comment
7
ca9eb4f68eab License text fixes
rgbecker
parents: 6
diff changeset
    45
#	
122
fd68411f49db Drawable ==> Flowable
rgbecker
parents: 16
diff changeset
    46
__version__=''' $Id: tables.py,v 1.5 2000/04/14 08:56:20 rgbecker Exp $ '''
16
f9c7525619fb Docstring & other fixes
rgbecker
parents: 7
diff changeset
    47
__doc__="""
6
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    48
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
    49
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
    50
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
    51
"""
16
f9c7525619fb Docstring & other fixes
rgbecker
parents: 7
diff changeset
    52
from reportlab.platypus import layout
6
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    53
import operator
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    54
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    55
_stringtype = type('')
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    56
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    57
class CellStyle(layout.PropertySet):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    58
    defaults = {
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    59
        'fontname':'Times-Roman',
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    60
        'fontsize':10,
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    61
        'leading':12,
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    62
        'leftPadding':6,
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    63
        'rightPadding':6,
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    64
        'topPadding':3,
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    65
        'bottomPadding':3,
6
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    66
        'firstLineIndent':0,
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    67
        'color':(0,0,0),
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    68
        'alignment': 'LEFT',
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    69
        'background': (1,1,1),
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    70
        }
6
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    71
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    72
class TableStyle:
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    73
    def __init__(self, cmds=None):
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    74
        self._cmds = cmds
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    75
        if cmds is None:
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    76
            self._cmds = []
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    77
    def add(self, *cmd):
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    78
        self._cmds.append(cmd)
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    79
    def getCommands(self):
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    80
        return self._cmds
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    81
        
122
fd68411f49db Drawable ==> Flowable
rgbecker
parents: 16
diff changeset
    82
class Table(layout.Flowable):
6
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    83
    def __init__(self, colWidths, rowHeights, data):
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    84
        if not colWidths:
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    85
            raise ValueError, "Table must have at least 1 column"
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    86
        if not rowHeights:
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    87
            raise ValueError, "Table must have at least 1 row"
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    88
        nrows = self._nrows = len(rowHeights)
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    89
        if len(data) != nrows:
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    90
            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
    91
        ncols = self._ncols = len(colWidths)
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    92
        for i in range(nrows):
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    93
            if len(data[i]) != ncols:
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    94
                raise ValueError, "Not enough data points in row %d!" % i
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    95
        self._rowHeights = rowHeights
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    96
        self._colWidths = colWidths
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    97
        self._cellvalues = data
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    98
        dflt = CellStyle('<default>')
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    99
        self._cellstyles = [None]*nrows
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   100
        for i in range(nrows):
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   101
            self._cellstyles[i] = [dflt]*ncols
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   102
        self._bkgrndcmds = []
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   103
        self._linecmds = []
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   104
        height = self._height = reduce(operator.add, rowHeights, 0)
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   105
        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
   106
        for h in rowHeights:
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   107
            height = height - h
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   108
            self._rowpositions.append(height)
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   109
        assert height == 0
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   110
        width = 0
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   111
        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
   112
        for w in colWidths:
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   113
            width = width + w
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   114
            self._colpositions.append(width)
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   115
        self._width = width
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   116
        self._curweight = self._curcolor = self._curcellstyle = None
6
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   117
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   118
    def setStyle(self, tblstyle):
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   119
        for cmd in tblstyle.getCommands():
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   120
            if cmd[0] == 'BACKGROUND':
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   121
                self._bkgrndcmds.append(cmd)
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   122
            elif _isLineCommand(cmd):
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   123
                self._linecmds.append(cmd)
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   124
            else:
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   125
                (op, (sc, sr), (ec, er)), values = cmd[:3] , cmd[3:]
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   126
                if sc < 0: sc = sc + self._ncols
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   127
                if ec < 0: ec = ec + self._ncols
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   128
                if sr < 0: sr = sr + self._nrows
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   129
                if er < 0: er = er + self._nrows
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   130
                for i in range(sr, er+1):
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   131
                    for j in range(sc, ec+1):
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   132
                        _setCellStyle(self._cellstyles, i, j, op, values)
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   133
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   134
    def _drawLines(self):
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   135
        for op, (sc, sr), (ec, er), weight, color in self._linecmds:
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   136
            if sc < 0: sc = sc + self._ncols
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   137
            if ec < 0: ec = ec + self._ncols
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   138
            if sr < 0: sr = sr + self._nrows
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   139
            if er < 0: er = er + self._nrows
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   140
            if op == 'GRID':
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
                self._drawInnerGrid( (sc, sr), (ec, er), weight, color)
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   143
            elif op in ('BOX',  'OUTLINE',):
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   144
                self._drawBox( (sc, sr), (ec, er), weight, color)
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   145
            elif op == 'INNERGRID':
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   146
                self._drawInnerGrid( (sc, sr), (ec, er), weight, color)
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   147
            elif op == 'LINEBELOW':
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   148
                self._drawHLines((sc, sr+1), (ec, er+1), weight, color)
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   149
            elif op == 'LINEABOVE':
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   150
                self._drawHLines((sc, sr), (ec, er), weight, color)
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   151
            elif op == 'LINEBEFORE':
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   152
                self._drawVLines((sc, sr), (ec, er), weight, color)
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   153
            elif op == 'LINEAFTER':
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   154
                self._drawVLines((sc+1, sr), (ec+1, er), weight, color)
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   155
            else:
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   156
                raise ValueError, "Unknown line style %s" % op
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   157
        self._curcolor = None
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   158
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   159
    def _drawBox(self,  (sc, sr), (ec, er), weight, color):
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   160
        self._drawHLines((sc, sr), (ec, sr), weight, color)
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   161
        self._drawHLines((sc, er+1), (ec, er+1), weight, color)
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   162
        self._drawVLines((sc, sr), (sc, er), weight, color)
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   163
        self._drawVLines((ec+1, sr), (ec+1, er), weight, color)
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   164
    def _drawInnerGrid(self, (sc, sr), (ec, er), weight, color):
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   165
        self._drawHLines((sc, sr+1), (ec, er), weight, color)
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   166
        self._drawVLines((sc+1, sr), (ec, er), weight, color)
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   167
    def _prepLine(self, weight, color):
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   168
        if type(color) is _stringtype:
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   169
            color = COLORS.get(color, (0,0,0))
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   170
        if color != self._curcolor:
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   171
            apply(self.canv.setStrokeColorRGB, color)
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   172
            self._curcolor = color
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   173
        if weight != self._curweight:
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   174
            self.canv.setLineWidth(weight)
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   175
            self._curweight = weight
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   176
    def _drawHLines(self, (sc, sr), (ec, er), weight, color):
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   177
        self._prepLine(weight, color)
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   178
        scp = self._colpositions[sc]
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   179
        ecp = self._colpositions[ec+1]
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   180
        for rowpos in self._rowpositions[sr:er+1]:
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   181
            self.canv.line(scp, rowpos, ecp, rowpos)
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   182
    def _drawVLines(self, (sc, sr), (ec, er), weight, color):
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   183
        self._prepLine(weight, color)
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   184
        srp = self._rowpositions[sr]
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   185
        erp = self._rowpositions[er+1]
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   186
        for colpos in self._colpositions[sc:ec+1]:
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   187
            self.canv.line(colpos, srp, colpos, erp)
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   188
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   189
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   190
    def wrap(self, availWidth, availHeight):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   191
        #nice and easy, since they are predetermined size
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   192
        self.availWidth = availWidth
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   193
        return (self._width, self._height)
6
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   194
                
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   195
    def draw(self):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   196
        nudge = 0.5 * (self.availWidth - self._width)
6
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   197
        self.canv.translate(nudge, 0)
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   198
        self._drawBkgrnd()
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   199
        self._drawLines()
6
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   200
        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
   201
            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
   202
                self._drawCell(cellval, cellstyle, (colpos, rowpos), (colwidth, rowheight))
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   203
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   204
    def _drawBkgrnd(self):
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   205
        for cmd, (sc, sr), (ec, er), color in self._bkgrndcmds:
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   206
            if sc < 0: sc = sc + self._ncols
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   207
            if ec < 0: ec = ec + self._ncols
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   208
            if sr < 0: sr = sr + self._nrows
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   209
            if er < 0: er = er + self._nrows
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   210
            if type(color) is _stringtype:
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   211
                color = COLORS.get(values[0], (1,1,1))
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   212
            x0 = self._colpositions[sc]
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   213
            y0 = self._rowpositions[sr]
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   214
            x1 = self._colpositions[ec+1]
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   215
            y1 = self._rowpositions[er+1]
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   216
            apply(self.canv.setFillColorRGB, color)
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   217
            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
   218
    def _drawCell(self, cellval, cellstyle, (colpos, rowpos), (colwidth, rowheight)):
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   219
        #print "cellstyle is ", repr(cellstyle), id(cellstyle)
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   220
        if self._curcellstyle is not cellstyle:
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   221
            cur = self._curcellstyle
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   222
            if cur is None or cellstyle.color != cur.color:
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   223
                #print "setting cell color to %s" % `cellstyle.color`
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   224
                apply(self.canv.setFillColorRGB, cellstyle.color)
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   225
            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
   226
                #print "setting font: %s, %s, %s" % (cellstyle.fontname, cellstyle.fontsize, cellstyle.leading)
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   227
                self.canv.setFont(cellstyle.fontname, cellstyle.fontsize, cellstyle.leading)
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   228
            self._curcellstyle = cellstyle
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   229
        just = cellstyle.alignment
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   230
        #print "alignment is ", just
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   231
        if just == 'LEFT':
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   232
            draw = self.canv.drawString
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   233
            x = colpos + cellstyle.leftPadding
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   234
        elif just in ('CENTRE', 'CENTER'):
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   235
            draw = self.canv.drawCentredString
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   236
            x = colpos + colwidth * 0.5
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   237
        else:
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   238
            draw = self.canv.drawRightString
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   239
            x = colpos + colwidth - cellstyle.rightPadding
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   240
        y = rowpos + cellstyle.bottomPadding
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   241
        if type(cellval) is _stringtype:
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   242
            val = cellval
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   243
        else:
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   244
            val = str(cellval)
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   245
        draw(x, y, val)
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   246
        
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   247
# for text,
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   248
#   drawCentredString(self, x, y, text) where x is center
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   249
#   drawRightString(self, x, y, text) where x is right
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   250
#   drawString(self, x, y, text) where x is left
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
LINECOMMANDS = (
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   253
    'GRID', 'BOX', 'OUTLINE', 'INNERGRID', 'LINEBELOW', 'LINEABOVE', 'LINEBEFORE', 'LINEAFTER', )
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   254
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   255
COLORS = {
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   256
    'BLACK': (0,0,0),
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   257
    'RED': (1,0,0),
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   258
    'GREEN': (0,1,0),
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   259
    'BLUE': (0,0,1),
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   260
    'WHITE':(1,1,1),
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   261
    }
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 _isLineCommand(cmd):
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   264
    return cmd[0] in LINECOMMANDS
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   265
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   266
def _setCellStyle(cellstyles, i, j, op, values):
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   267
    new = CellStyle('<%d, %d>' % (i,j), cellstyles[i][j])
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   268
    cellstyles[i][j] = new
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   269
    if op == 'FONT':
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   270
        new.fontname = values[0]
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   271
        new.fontsize = values[1]
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   272
    elif op == 'TEXTCOLOR':
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   273
        if type(values[0]) is _stringtype:
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   274
            new.color = COLORS.get(values[0], (0,0,0))
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   275
        else:
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   276
            new.color = values[0]
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   277
    elif op in ('ALIGN', 'ALIGNMENT'):
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   278
        new.alignment = values[0]
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   279
    elif op == 'LEFTPADDING':
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   280
        new.leftPadding = values[0]
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   281
    elif op == 'RIGHTPADDING':
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   282
        new.rightPadding = values[0]
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   283
    elif op == 'TOPPADDING':
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   284
        new.topPadding = values[0]
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   285
    elif op == 'BOTTOMPADDING':
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   286
        new.bottomPadding = values[0]
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   287
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   288
GRID_STYLE = TableStyle(
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   289
    [('GRID', (0,0), (-1,-1), 0.25, 'BLACK'),
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   290
     ('ALIGN', (1,1), (-1,-1), 'RIGHT')]
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   291
    )
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   292
BOX_STYLE = TableStyle(
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   293
    [('BOX', (0,0), (-1,-1), 0.50, 'BLACK'),
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   294
     ('ALIGN', (1,1), (-1,-1), 'RIGHT')]
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   295
    )
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   296
LABELED_GRID_STYLE = TableStyle(
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   297
    [('INNERGRID', (0,0), (-1,-1), 0.25, 'BLACK'),
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   298
     ('BOX', (0,0), (-1,-1), 2, 'BLACK'),
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   299
     ('LINEBELOW', (0,0), (-1,0), 2, 'BLACK'),
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   300
     ('LINEAFTER', (0,0), (0,-1), 2, 'BLACK'),
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   301
     ('ALIGN', (1,1), (-1,-1), 'RIGHT')]
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   302
    )
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   303
COLORED_GRID_STYLE = TableStyle(
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   304
    [('INNERGRID', (0,0), (-1,-1), 0.25, 'BLACK'),
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   305
     ('BOX', (0,0), (-1,-1), 2, 'RED'),
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   306
     ('LINEBELOW', (0,0), (-1,0), 2, 'BLACK'),
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   307
     ('LINEAFTER', (0,0), (0,-1), 2, 'BLACK'),
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   308
     ('ALIGN', (1,1), (-1,-1), 'RIGHT')]
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   309
    )
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   310
LIST_STYLE = TableStyle(
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   311
    [('LINEABOVE', (0,0), (-1,0), 2, 'GREEN'),
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   312
     ('LINEABOVE', (0,1), (-1,-1), 0.25, 'BLACK'),
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   313
     ('LINEBELOW', (0,-1), (-1,-1), 2, 'GREEN'),
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   314
     ('ALIGN', (1,1), (-1,-1), 'RIGHT')]
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   315
    )
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   316
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   317
def test():
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   318
    rowheights = (24, 16, 16, 16, 16)
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   319
    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
   320
    data = (
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   321
        ('', 'Jan', 'Feb', 'Mar','Apr','May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'),
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   322
        ('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
   323
        ('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
   324
        ('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
   325
        ('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
   326
        )
16
f9c7525619fb Docstring & other fixes
rgbecker
parents: 7
diff changeset
   327
    doc = layout.SimpleFlowDocument('testtables.pdf', layout.DEFAULT_PAGE_SIZE, 1)
6
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   328
    styleSheet = layout.getSampleStyleSheet()
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   329
    lst = []
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   330
    lst.append(layout.Paragraph("Tables", styleSheet['Heading1']))
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   331
    lst.append(layout.Paragraph(__doc__, styleSheet['BodyText']))
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   332
    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
   333
    lst.append(layout.Preformatted("""
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   334
    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
   335
    rowheights = (24, 16, 16, 16, 16)
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   336
    data = (
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   337
        ('', 'Jan', 'Feb', 'Mar','Apr','May', 'Jun',
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   338
           'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'),
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   339
        ('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
   340
        ('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
   341
        ('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
   342
        ('Hats', 893, 912, '1,212', 643, 789, 159,
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   343
             888, '1,298', 832, 453, '1,344','2,843')
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   344
        )   
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   345
    t = Table(colwidths, rowheights,  data)
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   346
    """, styleSheet['Code'], dedent=4))
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   347
    lst.append(layout.Paragraph("""
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   348
    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
   349
    created as follows:
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   350
    """, styleSheet['BodyText']))
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   351
    lst.append(layout.Preformatted("""
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   352
GRID_STYLE = TableStyle(
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   353
    [('GRID', (0,0), (-1,-1), 0.25, 'BLACK'),
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   354
     ('ALIGN', (1,1), (-1,-1), 'RIGHT')]
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   355
    )
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   356
    """, styleSheet['Code']))
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   357
    lst.append(layout.Paragraph("""
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   358
    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
   359
    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
   360
    the starting cell and the ending cell.
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   361
    """, styleSheet['BodyText']))
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   362
    lst.append(layout.Paragraph("""
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   363
    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
   364
    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
   365
    command names are: GRID, BOX, OUTLINE, INNERGRID, LINEBELOW, LINEABOVE, LINEBEFORE
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   366
    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
   367
    INNERGRID.
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
    Cell formatting commands are:
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   371
    """, styleSheet['BodyText']))
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
    FONT - takes fontname, fontsize and (optional) leading.
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
    TEXTCOLOR - takes a color name or (R,G,B) tuple.
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
    ALIGNMENT (or ALIGN) - takes one of LEFT, RIGHT and CENTRE (or CENTER).
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
    LEFTPADDING - 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
    RIGHTPADDING - defaults to 6.
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
    BOTTOMPADDING - defaults to 3.
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   389
    """, styleSheet['Definition']))
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   390
    lst.append(layout.Paragraph("""
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   391
    A tablestyle is applied to a table by calling Table.setStyle(tablestyle).
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   392
    """, styleSheet['BodyText']))
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   393
    t = Table(colwidths, rowheights,  data)
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   394
    t.setStyle(GRID_STYLE)
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   395
    lst.append(layout.PageBreak())
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   396
    lst.append(layout.Paragraph("This is GRID_STYLE\n", styleSheet['BodyText']))
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   397
    lst.append(t)
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   398
    
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   399
    t = Table(colwidths, rowheights,  data)
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   400
    t.setStyle(BOX_STYLE)
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   401
    lst.append(layout.Paragraph("This is BOX_STYLE\n", styleSheet['BodyText']))
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   402
    lst.append(t)
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   403
    lst.append(layout.Paragraph("""
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   404
    It was created as follows:
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   405
    """, styleSheet['BodyText']))
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   406
    lst.append(layout.Preformatted("""
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   407
BOX_STYLE = TableStyle(
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   408
    [('BOX', (0,0), (-1,-1), 0.50, 'BLACK'),
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   409
     ('ALIGN', (1,1), (-1,-1), 'RIGHT')]
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   410
    )
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   411
    """, styleSheet['Code']))
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   412
    
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   413
    t = Table(colwidths, rowheights,  data)
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   414
    t.setStyle(LABELED_GRID_STYLE)
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   415
    lst.append(layout.Paragraph("This is LABELED_GRID_STYLE\n", styleSheet['BodyText']))
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   416
    lst.append(t)
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   417
    lst.append(layout.Paragraph("""
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   418
    It was created as follows:
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   419
    """, styleSheet['BodyText']))
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   420
    lst.append(layout.Preformatted("""
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   421
LABELED_GRID_STYLE = TableStyle(
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   422
    [('INNERGRID', (0,0), (-1,-1), 0.25, 'BLACK'),
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   423
     ('BOX', (0,0), (-1,-1), 2, 'BLACK'),
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   424
     ('LINEBELOW', (0,0), (-1,0), 2, 'BLACK'),
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   425
     ('LINEAFTER', (0,0), (0,-1), 2, 'BLACK'),
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   426
     ('ALIGN', (1,1), (-1,-1), 'RIGHT')]
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
    """, styleSheet['Code']))
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   429
    lst.append(layout.PageBreak())
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   430
    
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   431
    t = Table(colwidths, rowheights,  data)
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   432
    t.setStyle(COLORED_GRID_STYLE)
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   433
    lst.append(layout.Paragraph("This is COLORED_GRID_STYLE\n", styleSheet['BodyText']))
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   434
    lst.append(t)
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   435
    lst.append(layout.Paragraph("""
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   436
    It was created as follows:
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   437
    """, styleSheet['BodyText']))
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   438
    lst.append(layout.Preformatted("""
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   439
COLORED_GRID_STYLE = TableStyle(
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   440
    [('INNERGRID', (0,0), (-1,-1), 0.25, 'BLACK'),
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   441
     ('BOX', (0,0), (-1,-1), 2, 'RED'),
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   442
     ('LINEBELOW', (0,0), (-1,0), 2, 'BLACK'),
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   443
     ('LINEAFTER', (0,0), (0,-1), 2, 'BLACK'),
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   444
     ('ALIGN', (1,1), (-1,-1), 'RIGHT')]
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   445
    )
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   446
    """, styleSheet['Code']))
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   447
    
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   448
    t = Table(colwidths, rowheights,  data)
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   449
    t.setStyle(LIST_STYLE)
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   450
    lst.append(layout.Paragraph("This is LIST_STYLE\n", styleSheet['BodyText']))
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   451
    lst.append(t)
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   452
    lst.append(layout.Paragraph("""
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   453
    It was created as follows:
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   454
    """, styleSheet['BodyText']))
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   455
    lst.append(layout.Preformatted("""
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   456
LIST_STYLE = TableStyle(
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   457
    [('LINEABOVE', (0,0), (-1,0), 2, 'GREEN'),
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   458
     ('LINEABOVE', (0,1), (-1,-1), 0.25, 'BLACK'),
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   459
     ('LINEBELOW', (0,-1), (-1,-1), 2, 'GREEN'),
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   460
     ('ALIGN', (1,1), (-1,-1), 'RIGHT')]
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   461
    )
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   462
    """, styleSheet['Code']))
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   463
   
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   464
    t = Table(colwidths, rowheights,  data)
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   465
    ts = TableStyle(
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   466
    [('LINEABOVE', (0,0), (-1,0), 2, 'GREEN'),
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   467
     ('LINEABOVE', (0,1), (-1,-1), 0.25, 'BLACK'),
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   468
     ('LINEBELOW', (0,-1), (-1,-1), 2, 'GREEN'),
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   469
     ('ALIGN', (1,1), (-1,-1), 'RIGHT'),
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   470
     ('TEXTCOLOR', (0,1), (0,-1), 'RED'),
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   471
     ('BACKGROUND', (0,0), (-1,0), (0,0.7,0.7))]
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   472
    )
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   473
    t.setStyle(ts)
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   474
    lst.append(layout.Paragraph("This is a custom style\n", styleSheet['BodyText']))
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   475
    lst.append(t)
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   476
    lst.append(layout.Paragraph("""
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   477
    It was created as follows:
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   478
    """, styleSheet['BodyText']))
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   479
    lst.append(layout.Preformatted("""
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   480
   ts = TableStyle(
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   481
    [('LINEABOVE', (0,0), (-1,0), 2, 'GREEN'),
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   482
     ('LINEABOVE', (0,1), (-1,-1), 0.25, 'BLACK'),
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   483
     ('LINEBELOW', (0,-1), (-1,-1), 2, 'GREEN'),
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   484
     ('ALIGN', (1,1), (-1,-1), 'RIGHT'),
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   485
     ('TEXTCOLOR', (0,1), (0,-1), 'RED'),
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   486
     ('BACKGROUND', (0,0), (-1,0), (0,0.7,0.7))]
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
    """, styleSheet['Code']))
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   489
    doc.build(lst)
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   490
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   491
if __name__ == '__main__':
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   492
    test()