src/reportlab/graphics/widgets/table.py
author robin <robin@reportlab.com>
Tue, 07 Mar 2017 10:00:34 +0000
changeset 4330 617ffa6bbdc8
parent 4252 fe660f227cac
child 4370 823a8c33ce43
permissions -rw-r--r--
changes for release 3.4.0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3228
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
     1
#!/usr/bin/env python
4330
617ffa6bbdc8 changes for release 3.4.0
robin <robin@reportlab.com>
parents: 4252
diff changeset
     2
#Copyright ReportLab Europe Ltd. 2000-2017
3228
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
     3
#see license.txt for license details
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
     4
#history http://www.reportlab.co.uk/cgi-bin/viewcvs.cgi/public/reportlab/trunk/reportlab/graphics/widgets/grids.py
4252
fe660f227cac changes for release 3.3.0
robin
parents: 4067
diff changeset
     5
__version__='3.3.0'
3228
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
     6
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
     7
from reportlab.graphics.widgetbase import Widget
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
     8
from reportlab.graphics.charts.textlabels import Label
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
     9
from reportlab.graphics import shapes
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
    10
from reportlab.lib import colors
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
    11
from reportlab.lib.validators import *
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
    12
from reportlab.lib.attrmap import *
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
    13
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
    14
from reportlab.graphics.shapes import Drawing
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
    15
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
    16
class TableWidget(Widget):
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
    17
    """A two dimensions table of labels
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
    18
    """
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
    19
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
    20
    _attrMap = AttrMap(
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
    21
        x = AttrMapValue(isNumber, desc="x position of left edge of table"),
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
    22
        y = AttrMapValue(isNumber, desc="y position of bottom edge of table"),
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
    23
        width = AttrMapValue(isNumber, desc="table width"),
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
    24
        height = AttrMapValue(isNumber, desc="table height"),
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
    25
        borderStrokeColor = AttrMapValue(isColorOrNone, desc="table border color"),
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
    26
        fillColor = AttrMapValue(isColorOrNone, desc="table fill color"),
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
    27
        borderStrokeWidth = AttrMapValue(isNumber, desc="border line width"),
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
    28
        horizontalDividerStrokeColor = AttrMapValue(isColorOrNone, desc="table inner horizontal lines color"),
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
    29
        verticalDividerStrokeColor = AttrMapValue(isColorOrNone, desc="table inner vertical lines color"),
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
    30
        horizontalDividerStrokeWidth = AttrMapValue(isNumber, desc="table inner horizontal lines width"),
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
    31
        verticalDividerStrokeWidth = AttrMapValue(isNumber, desc="table inner vertical lines width"),
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
    32
        dividerDashArray = AttrMapValue(isListOfNumbersOrNone, desc='Dash array for dividerLines.'),
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
    33
        data = AttrMapValue(None, desc="a list of list of strings to be displayed in the cells"),
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
    34
        boxAnchor = AttrMapValue(isBoxAnchor, desc="location of the table anchoring point"),
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
    35
        fontName = AttrMapValue(isString, desc="text font in the table"),
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
    36
        fontSize = AttrMapValue(isNumber, desc="font size of the table"),
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
    37
        fontColor = AttrMapValue(isColorOrNone, desc="font color"),
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
    38
        alignment = AttrMapValue(OneOf("left", "right"), desc="Alignment of text within cells"),
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
    39
        textAnchor = AttrMapValue(OneOf('start','middle','end','numeric'), desc="Alignment of text within cells"),
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
    40
    )
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
    41
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
    42
    def __init__(self, x=10, y=10, **kw):
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
    43
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
    44
        self.x = x
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
    45
        self.y = y
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
    46
        self.width = 200
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
    47
        self.height = 100
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
    48
        self.borderStrokeColor = colors.black
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
    49
        self.fillColor = None
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
    50
        self.borderStrokeWidth = 0.5
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
    51
        self.horizontalDividerStrokeColor = colors.black
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
    52
        self.verticalDividerStrokeColor = colors.black
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
    53
        self.horizontalDividerStrokeWidth = 0.5
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
    54
        self.verticalDividerStrokeWidth = 0.25
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
    55
        self.dividerDashArray = None
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
    56
        self.data = [['North','South','East','West'],[100,110,120,130],['A','B','C','D']] # list of rows each row is a list of columns
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
    57
        self.boxAnchor = 'nw'
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
    58
        #self.fontName = None
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
    59
        self.fontSize = 8
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
    60
        self.fontColor = colors.black
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
    61
        self.alignment = 'right'
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
    62
        self.textAnchor = 'start'
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
    63
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
    64
3723
99aa837b6703 second stage of port to Python 3.3; working hello world
rptlab
parents: 3721
diff changeset
    65
        for k, v in kw.items():
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3617
diff changeset
    66
            if k in list(self.__class__._attrMap.keys()):
3228
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
    67
                setattr(self, k, v)
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
    68
            else:
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
    69
                raise ValueError('invalid argument supplied for class %s'%self.__class__)
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
    70
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
    71
    def demo(self):
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
    72
        """ returns a sample of this widget with data
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
    73
        """
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
    74
        d = Drawing(400, 200)
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
    75
        t = TableWidget()
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
    76
        d.add(t, name='table')
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
    77
        d.table.dividerDashArray = (1, 3, 2)
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
    78
        d.table.verticalDividerStrokeColor = None
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
    79
        d.table.borderStrokeWidth = 0
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
    80
        d.table.borderStrokeColor = colors.red
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
    81
        return d
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
    82
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
    83
    def draw(self):
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
    84
        """ returns a group of shapes
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
    85
        """
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
    86
        g = shapes.Group()
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
    87
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
    88
        #overall border and fill
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
    89
        if self.borderStrokeColor or self.fillColor: # adds border and filling color
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
    90
            rect = shapes.Rect(self.x, self.y, self.width, self.height)
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
    91
            rect.fillColor = self.fillColor
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
    92
            rect.strokeColor = self.borderStrokeColor
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
    93
            rect.strokeWidth = self.borderStrokeWidth
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
    94
            g.add(rect)
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
    95
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
    96
        #special case - for an empty table we want to avoid divide-by-zero
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
    97
        data = self.preProcessData(self.data)
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
    98
        rows = len(self.data)
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
    99
        cols = len(self.data[0])
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
   100
        #print "(rows,cols)=(%s, %s)"%(rows,cols)
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
   101
        row_step = self.height / float(rows)
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
   102
        col_step = self.width / float(cols)
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
   103
        #print "(row_step,col_step)=(%s, %s)"%(row_step,col_step)
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
   104
        # draw the grid
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
   105
        if self.horizontalDividerStrokeColor:
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3617
diff changeset
   106
            for i in range(rows): # make horizontal lines
3228
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
   107
                x1 = self.x
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
   108
                x2 = self.x + self.width
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
   109
                y = self.y + row_step*i
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
   110
                #print 'line (%s, %s), (%s, %s)'%(x1, y, x2, y)
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
   111
                line = shapes.Line(x1, y, x2, y)
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
   112
                line.strokeDashArray = self.dividerDashArray
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
   113
                line.strokeWidth = self.horizontalDividerStrokeWidth
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
   114
                line.strokeColor = self.horizontalDividerStrokeColor
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
   115
                g.add(line)
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
   116
        if self.verticalDividerStrokeColor:
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3617
diff changeset
   117
            for i in range(cols): # make vertical lines
3228
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
   118
                x = self.x+col_step*i
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
   119
                y1 = self.y
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
   120
                y2 = self.y + self.height
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
   121
                #print 'line (%s, %s), (%s, %s)'%(x, y1, x, y2)
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
   122
                line = shapes.Line(x, y1, x, y2)
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
   123
                line.strokeDashArray = self.dividerDashArray
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
   124
                line.strokeWidth = self.verticalDividerStrokeWidth
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
   125
                line.strokeColor = self.verticalDividerStrokeColor
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
   126
                g.add(line)
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
   127
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
   128
        # since we plot data from down up, we reverse the list
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
   129
        self.data.reverse()
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
   130
        for (j, row) in enumerate(self.data):
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
   131
            y = self.y + j*row_step + 0.5*row_step - 0.5 * self.fontSize
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
   132
            for (i, datum) in enumerate(row):
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
   133
                if datum:
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
   134
                    x = self.x + i*col_step + 0.5*col_step
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
   135
                    s = shapes.String(x, y, str(datum), textAnchor=self.textAnchor)
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
   136
                    s.fontName = self.fontName
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
   137
                    s.fontSize = self.fontSize
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
   138
                    s.fillColor = self.fontColor
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
   139
                    g.add(s)
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
   140
        return g
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
   141
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
   142
    def preProcessData(self, data):
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
   143
        """preprocess and return a new array with at least one row
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
   144
        and column (use a None) if needed, and all rows the same
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
   145
        length (adding Nones if needed)
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
   146
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
   147
        """
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
   148
        if not data:
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
   149
            return [[None]]
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
   150
        #make all rows have similar number of cells, append None when needed
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
   151
        max_row = max( [len(x) for x in data] )
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
   152
        for rowNo, row in enumerate(data):
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
   153
            if len(row) < max_row:
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
   154
                row.extend([None]*(max_row-len(row)))
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
   155
        return data
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
   156
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
   157
#test
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
   158
if __name__ == '__main__':
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
   159
    d = TableWidget().demo()
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
   160
    import os
60250a3b480d a new table widget has been added
meitham
parents:
diff changeset
   161
    d.save(formats=['pdf'],outDir=os.getcwd(),fnRoot=None)