reportlab/platypus/tableofcontents.py
author rgbecker
Wed, 10 Nov 2004 18:35:10 +0000
changeset 2408 1c5e79611b59
parent 2332 2a7ab4405e18
permissions -rw-r--r--
flowables: add SlowPageBreak
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2332
2a7ab4405e18 Remove $Header:, fix CopyRight & history
rgbecker
parents: 1683
diff changeset
     1
#Copyright ReportLab Europe Ltd. 2000-2004
817
8c3a399effda License text changes
rgbecker
parents: 754
diff changeset
     2
#see license.txt for license details
2332
2a7ab4405e18 Remove $Header:, fix CopyRight & history
rgbecker
parents: 1683
diff changeset
     3
#history http://www.reportlab.co.uk/cgi-bin/viewcvs.cgi/public/reportlab/trunk/reportlab/platypus/tableofcontents.py
754
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
     4
"""
1440
243d35446390 Removed 0 from multiBuild stuff prior to further changes;
andy_robinson
parents: 817
diff changeset
     5
This module defines a single TableOfContents() class that can be used to
754
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
     6
create automatically a table of tontents for Platypus documents like
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
     7
this:
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
     8
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
     9
    story = []
1440
243d35446390 Removed 0 from multiBuild stuff prior to further changes;
andy_robinson
parents: 817
diff changeset
    10
    toc = TableOfContents()
754
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
    11
    story.append(toc)
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
    12
    # some heading paragraphs here...
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
    13
    doc = MyTemplate(path)
1440
243d35446390 Removed 0 from multiBuild stuff prior to further changes;
andy_robinson
parents: 817
diff changeset
    14
    doc.multiBuild(story)
754
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
    15
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
    16
The data needed to create the table is a list of (level, text, pageNum)
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
    17
triplets, plus some paragraph styles for each level of the table itself.
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
    18
The triplets will usually be created in a document template's method
1440
243d35446390 Removed 0 from multiBuild stuff prior to further changes;
andy_robinson
parents: 817
diff changeset
    19
like afterFlowable(), making notification calls using the notify()
754
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
    20
method with appropriate data like this:
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
    21
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
    22
    (level, text, pageNum) = ...
1440
243d35446390 Removed 0 from multiBuild stuff prior to further changes;
andy_robinson
parents: 817
diff changeset
    23
    self.notify('TOCEntry', (level, text, pageNum))
754
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
    24
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
    25
As the table of contents need at least two passes over the Platypus
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
    26
story which is why the moultiBuild0() method must be called.
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
    27
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
    28
The level<NUMBER>ParaStyle variables are the paragraph styles used
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
    29
to format the entries in the table of contents. Their indentation
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
    30
is calculated like this: each entry starts at a multiple of some
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
    31
constant named delta. If one entry spans more than one line, all
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
    32
lines after the first are indented by the same constant named
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1559
diff changeset
    33
epsilon.
754
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
    34
"""
2332
2a7ab4405e18 Remove $Header:, fix CopyRight & history
rgbecker
parents: 1683
diff changeset
    35
__version__=''' $Id$ '''
1444
454d117af3a5 Missed a checkin
andy_robinson
parents: 1440
diff changeset
    36
import string
754
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
    37
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
    38
from reportlab.lib import enums
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
    39
from reportlab.lib.units import cm
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
    40
from reportlab.lib.styles import ParagraphStyle
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
    41
from reportlab.platypus.paragraph import Paragraph
1440
243d35446390 Removed 0 from multiBuild stuff prior to further changes;
andy_robinson
parents: 817
diff changeset
    42
from reportlab.platypus.doctemplate import IndexingFlowable
754
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
    43
from reportlab.platypus.tables import TableStyle, Table
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
    44
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
    45
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
    46
# Default paragraph styles for tables of contents.
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
    47
# (This could also be generated automatically or even
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
    48
# on-demand if it is not known how many levels the
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
    49
# TOC will finally need to display...)
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
    50
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
    51
delta = 1*cm
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
    52
epsilon = 0.5*cm
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
    53
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
    54
levelZeroParaStyle = \
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
    55
    ParagraphStyle(name='LevelZero',
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
    56
                   fontName='Times-Roman',
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
    57
                   fontSize=10,
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
    58
                   leading=11,
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
    59
                   firstLineIndent = -epsilon,
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
    60
                   leftIndent = 0*delta + epsilon)
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
    61
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
    62
levelOneParaStyle = \
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
    63
    ParagraphStyle(name='LevelOne',
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
    64
                   parent = levelZeroParaStyle,
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
    65
                   leading=11,
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
    66
                   firstLineIndent = -epsilon,
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
    67
                   leftIndent = 1*delta + epsilon)
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
    68
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
    69
levelTwoParaStyle = \
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
    70
    ParagraphStyle(name='LevelTwo',
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
    71
                   parent = levelOneParaStyle,
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
    72
                   leading=11,
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
    73
                   firstLineIndent = -epsilon,
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
    74
                   leftIndent = 2*delta + epsilon)
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
    75
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
    76
levelThreeParaStyle = \
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
    77
    ParagraphStyle(name='LevelThree',
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
    78
                   parent = levelTwoParaStyle,
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
    79
                   leading=11,
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
    80
                   firstLineIndent = -epsilon,
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
    81
                   leftIndent = 3*delta + epsilon)
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
    82
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
    83
levelFourParaStyle = \
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
    84
    ParagraphStyle(name='LevelFour',
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
    85
                   parent = levelTwoParaStyle,
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
    86
                   leading=11,
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
    87
                   firstLineIndent = -epsilon,
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
    88
                   leftIndent = 4*delta + epsilon)
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
    89
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
    90
defaultTableStyle = \
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
    91
    TableStyle([('VALIGN', (0,0), (-1,-1), 'TOP')])
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
    92
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
    93
1440
243d35446390 Removed 0 from multiBuild stuff prior to further changes;
andy_robinson
parents: 817
diff changeset
    94
class TableOfContents(IndexingFlowable):
754
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
    95
    """This creates a formatted table of contents.
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
    96
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
    97
    It presumes a correct block of data is passed in.
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
    98
    The data block contains a list of (level, text, pageNumber)
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
    99
    triplets.  You can supply a paragraph style for each level
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
   100
    (starting at zero).
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
   101
    """
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
   102
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
   103
    def __init__(self):
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
   104
        self.entries = []
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
   105
        self.rightColumnWidth = 72
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
   106
        self.levelStyles = [levelZeroParaStyle,
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
   107
                            levelOneParaStyle,
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
   108
                            levelTwoParaStyle,
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
   109
                            levelThreeParaStyle,
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
   110
                            levelFourParaStyle]
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
   111
        self.tableStyle = defaultTableStyle
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
   112
        self._table = None
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
   113
        self._entries = []
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
   114
        self._lastEntries = []
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
   115
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
   116
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
   117
    def beforeBuild(self):
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
   118
        # keep track of the last run
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
   119
        self._lastEntries = self._entries[:]
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
   120
        self.clearEntries()
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
   121
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
   122
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
   123
    def isIndexing(self):
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
   124
        return 1
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
   125
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
   126
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
   127
    def isSatisfied(self):
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
   128
        return (self._entries == self._lastEntries)
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
   129
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
   130
    def notify(self, kind, stuff):
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
   131
        """The notification hook called to register all kinds of events.
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
   132
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
   133
        Here we are interested in 'TOCEntry' events only.
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
   134
        """
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
   135
        if kind == 'TOCEntry':
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
   136
            (level, text, pageNum) = stuff
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
   137
            self.addEntry(level, text, pageNum)
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
   138
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
   139
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
   140
    def clearEntries(self):
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
   141
        self._entries = []
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
   142
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
   143
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
   144
    def addEntry(self, level, text, pageNum):
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
   145
        """Adds one entry to the table of contents.
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
   146
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
   147
        This allows incremental buildup by a doctemplate.
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
   148
        Requires that enough styles are defined."""
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
   149
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
   150
        assert type(level) == type(1), "Level must be an integer"
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
   151
        assert level < len(self.levelStyles), \
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
   152
               "Table of contents must have a style defined " \
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
   153
               "for paragraph level %d before you add an entry" % level
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
   154
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
   155
        self._entries.append((level, text, pageNum))
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
   156
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
   157
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
   158
    def addEntries(self, listOfEntries):
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
   159
        """Bulk creation of entries in the table of contents.
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
   160
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
   161
        If you knew the titles but not the page numbers, you could
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
   162
        supply them to get sensible output on the first run."""
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1559
diff changeset
   163
754
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
   164
        for (level, text, pageNum) in listOfEntries:
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
   165
            self.addEntry(level, text, pageNum)
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
   166
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
   167
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
   168
    def wrap(self, availWidth, availHeight):
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
   169
        "All table properties should be known by now."
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
   170
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
   171
        widths = (availWidth - self.rightColumnWidth,
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
   172
                  self.rightColumnWidth)
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
   173
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
   174
        # makes an internal table which does all the work.
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
   175
        # we draw the LAST RUN's entries!  If there are
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
   176
        # none, we make some dummy data to keep the table
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
   177
        # from complaining
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
   178
        if len(self._lastEntries) == 0:
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
   179
            _tempEntries = [(0,'Placeholder for table of contents',0)]
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
   180
        else:
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
   181
            _tempEntries = self._lastEntries
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
   182
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
   183
        tableData = []
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
   184
        for (level, text, pageNum) in _tempEntries:
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
   185
            leftColStyle = self.levelStyles[level]
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
   186
            #right col style is right aligned
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
   187
            rightColStyle = ParagraphStyle(name='leftColLevel%d' % level,
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
   188
                                           parent=leftColStyle,
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
   189
                                           leftIndent=0,
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
   190
                                           alignment=enums.TA_RIGHT)
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
   191
            leftPara = Paragraph(text, leftColStyle)
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
   192
            rightPara = Paragraph(str(pageNum), rightColStyle)
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
   193
            tableData.append([leftPara, rightPara])
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
   194
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
   195
        self._table = Table(tableData, colWidths=widths,
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
   196
                            style=self.tableStyle)
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
   197
1493
bfcdeed2f9e7 Propogate the canvas
rgbecker
parents: 1444
diff changeset
   198
        self.width, self.height = self._table.wrapOn(self.canv,availWidth, availHeight)
754
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
   199
        return (self.width, self.height)
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
   200
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
   201
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
   202
    def split(self, availWidth, availHeight):
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
   203
        """At this stage we do not care about splitting the entries,
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
   204
        we will just return a list of platypus tables.  Presumably the
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
   205
        calling app has a pointer to the original TableOfContents object;
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
   206
        Platypus just sees tables.
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
   207
        """
1493
bfcdeed2f9e7 Propogate the canvas
rgbecker
parents: 1444
diff changeset
   208
        return self._table.splitOn(self.canv,availWidth, availHeight)
754
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
   209
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
   210
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
   211
    def drawOn(self, canvas, x, y, _sW=0):
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
   212
        """Don't do this at home!  The standard calls for implementing
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
   213
        draw(); we are hooking this in order to delegate ALL the drawing
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
   214
        work to the embedded table object.
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
   215
        """
ac7d1e99bd3e Replaced trailing digit file.
dinu_gherman
parents:
diff changeset
   216
        self._table.drawOn(canvas, x, y, _sW)
1444
454d117af3a5 Missed a checkin
andy_robinson
parents: 1440
diff changeset
   217
454d117af3a5 Missed a checkin
andy_robinson
parents: 1440
diff changeset
   218
454d117af3a5 Missed a checkin
andy_robinson
parents: 1440
diff changeset
   219
class SimpleIndex(IndexingFlowable):
454d117af3a5 Missed a checkin
andy_robinson
parents: 1440
diff changeset
   220
    """This creates a very simple index.
454d117af3a5 Missed a checkin
andy_robinson
parents: 1440
diff changeset
   221
454d117af3a5 Missed a checkin
andy_robinson
parents: 1440
diff changeset
   222
    Entries have a string key, and appear with a page number on
454d117af3a5 Missed a checkin
andy_robinson
parents: 1440
diff changeset
   223
    the right.  Prototype for more sophisticated multi-level index."""
454d117af3a5 Missed a checkin
andy_robinson
parents: 1440
diff changeset
   224
    def __init__(self):
454d117af3a5 Missed a checkin
andy_robinson
parents: 1440
diff changeset
   225
        #keep stuff in a dictionary while building
454d117af3a5 Missed a checkin
andy_robinson
parents: 1440
diff changeset
   226
        self._entries = {}
454d117af3a5 Missed a checkin
andy_robinson
parents: 1440
diff changeset
   227
        self._lastEntries = {}
454d117af3a5 Missed a checkin
andy_robinson
parents: 1440
diff changeset
   228
        self._table = None
454d117af3a5 Missed a checkin
andy_robinson
parents: 1440
diff changeset
   229
        self.textStyle = ParagraphStyle(name='index',
454d117af3a5 Missed a checkin
andy_robinson
parents: 1440
diff changeset
   230
                                        fontName='Times-Roman',
454d117af3a5 Missed a checkin
andy_robinson
parents: 1440
diff changeset
   231
                                        fontSize=12)
454d117af3a5 Missed a checkin
andy_robinson
parents: 1440
diff changeset
   232
    def isIndexing(self):
454d117af3a5 Missed a checkin
andy_robinson
parents: 1440
diff changeset
   233
        return 1
454d117af3a5 Missed a checkin
andy_robinson
parents: 1440
diff changeset
   234
454d117af3a5 Missed a checkin
andy_robinson
parents: 1440
diff changeset
   235
    def isSatisfied(self):
454d117af3a5 Missed a checkin
andy_robinson
parents: 1440
diff changeset
   236
        return (self._entries == self._lastEntries)
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1559
diff changeset
   237
1444
454d117af3a5 Missed a checkin
andy_robinson
parents: 1440
diff changeset
   238
    def beforeBuild(self):
454d117af3a5 Missed a checkin
andy_robinson
parents: 1440
diff changeset
   239
        # keep track of the last run
454d117af3a5 Missed a checkin
andy_robinson
parents: 1440
diff changeset
   240
        self._lastEntries = self._entries.copy()
454d117af3a5 Missed a checkin
andy_robinson
parents: 1440
diff changeset
   241
        self.clearEntries()
454d117af3a5 Missed a checkin
andy_robinson
parents: 1440
diff changeset
   242
454d117af3a5 Missed a checkin
andy_robinson
parents: 1440
diff changeset
   243
    def clearEntries(self):
454d117af3a5 Missed a checkin
andy_robinson
parents: 1440
diff changeset
   244
        self._entries = {}
454d117af3a5 Missed a checkin
andy_robinson
parents: 1440
diff changeset
   245
454d117af3a5 Missed a checkin
andy_robinson
parents: 1440
diff changeset
   246
    def notify(self, kind, stuff):
454d117af3a5 Missed a checkin
andy_robinson
parents: 1440
diff changeset
   247
        """The notification hook called to register all kinds of events.
454d117af3a5 Missed a checkin
andy_robinson
parents: 1440
diff changeset
   248
454d117af3a5 Missed a checkin
andy_robinson
parents: 1440
diff changeset
   249
        Here we are interested in 'IndexEntry' events only.
454d117af3a5 Missed a checkin
andy_robinson
parents: 1440
diff changeset
   250
        """
454d117af3a5 Missed a checkin
andy_robinson
parents: 1440
diff changeset
   251
        if kind == 'IndexEntry':
454d117af3a5 Missed a checkin
andy_robinson
parents: 1440
diff changeset
   252
            (text, pageNum) = stuff
454d117af3a5 Missed a checkin
andy_robinson
parents: 1440
diff changeset
   253
            self.addEntry(text, pageNum)
454d117af3a5 Missed a checkin
andy_robinson
parents: 1440
diff changeset
   254
454d117af3a5 Missed a checkin
andy_robinson
parents: 1440
diff changeset
   255
    def addEntry(self, text, pageNum):
454d117af3a5 Missed a checkin
andy_robinson
parents: 1440
diff changeset
   256
        """Allows incremental buildup"""
454d117af3a5 Missed a checkin
andy_robinson
parents: 1440
diff changeset
   257
        if self._entries.has_key(text):
454d117af3a5 Missed a checkin
andy_robinson
parents: 1440
diff changeset
   258
            self._entries[text].append(str(pageNum))
454d117af3a5 Missed a checkin
andy_robinson
parents: 1440
diff changeset
   259
        else:
454d117af3a5 Missed a checkin
andy_robinson
parents: 1440
diff changeset
   260
            self._entries[text] = [pageNum]
454d117af3a5 Missed a checkin
andy_robinson
parents: 1440
diff changeset
   261
454d117af3a5 Missed a checkin
andy_robinson
parents: 1440
diff changeset
   262
    def split(self, availWidth, availHeight):
454d117af3a5 Missed a checkin
andy_robinson
parents: 1440
diff changeset
   263
        """At this stage we do not care about splitting the entries,
454d117af3a5 Missed a checkin
andy_robinson
parents: 1440
diff changeset
   264
        we will just return a list of platypus tables.  Presumably the
454d117af3a5 Missed a checkin
andy_robinson
parents: 1440
diff changeset
   265
        calling app has a pointer to the original TableOfContents object;
454d117af3a5 Missed a checkin
andy_robinson
parents: 1440
diff changeset
   266
        Platypus just sees tables.
454d117af3a5 Missed a checkin
andy_robinson
parents: 1440
diff changeset
   267
        """
1493
bfcdeed2f9e7 Propogate the canvas
rgbecker
parents: 1444
diff changeset
   268
        return self._table.splitOn(self.canv,availWidth, availHeight)
1444
454d117af3a5 Missed a checkin
andy_robinson
parents: 1440
diff changeset
   269
454d117af3a5 Missed a checkin
andy_robinson
parents: 1440
diff changeset
   270
    def wrap(self, availWidth, availHeight):
454d117af3a5 Missed a checkin
andy_robinson
parents: 1440
diff changeset
   271
        "All table properties should be known by now."
454d117af3a5 Missed a checkin
andy_robinson
parents: 1440
diff changeset
   272
        # makes an internal table which does all the work.
454d117af3a5 Missed a checkin
andy_robinson
parents: 1440
diff changeset
   273
        # we draw the LAST RUN's entries!  If there are
454d117af3a5 Missed a checkin
andy_robinson
parents: 1440
diff changeset
   274
        # none, we make some dummy data to keep the table
454d117af3a5 Missed a checkin
andy_robinson
parents: 1440
diff changeset
   275
        # from complaining
454d117af3a5 Missed a checkin
andy_robinson
parents: 1440
diff changeset
   276
        if len(self._lastEntries) == 0:
454d117af3a5 Missed a checkin
andy_robinson
parents: 1440
diff changeset
   277
            _tempEntries = [('Placeholder for index',[0,1,2])]
454d117af3a5 Missed a checkin
andy_robinson
parents: 1440
diff changeset
   278
        else:
454d117af3a5 Missed a checkin
andy_robinson
parents: 1440
diff changeset
   279
            _tempEntries = self._lastEntries.items()
454d117af3a5 Missed a checkin
andy_robinson
parents: 1440
diff changeset
   280
            _tempEntries.sort()
454d117af3a5 Missed a checkin
andy_robinson
parents: 1440
diff changeset
   281
454d117af3a5 Missed a checkin
andy_robinson
parents: 1440
diff changeset
   282
        tableData = []
454d117af3a5 Missed a checkin
andy_robinson
parents: 1440
diff changeset
   283
        for (text, pageNumbers) in _tempEntries:
454d117af3a5 Missed a checkin
andy_robinson
parents: 1440
diff changeset
   284
            #right col style is right aligned
454d117af3a5 Missed a checkin
andy_robinson
parents: 1440
diff changeset
   285
            allText = text + ': ' + string.join(map(str, pageNumbers), ', ')
454d117af3a5 Missed a checkin
andy_robinson
parents: 1440
diff changeset
   286
            para = Paragraph(allText, self.textStyle)
454d117af3a5 Missed a checkin
andy_robinson
parents: 1440
diff changeset
   287
            tableData.append([para])
454d117af3a5 Missed a checkin
andy_robinson
parents: 1440
diff changeset
   288
454d117af3a5 Missed a checkin
andy_robinson
parents: 1440
diff changeset
   289
        self._table = Table(tableData, colWidths=[availWidth])
454d117af3a5 Missed a checkin
andy_robinson
parents: 1440
diff changeset
   290
1493
bfcdeed2f9e7 Propogate the canvas
rgbecker
parents: 1444
diff changeset
   291
        self.width, self.height = self._table.wrapOn(self.canv,availWidth, availHeight)
1444
454d117af3a5 Missed a checkin
andy_robinson
parents: 1440
diff changeset
   292
        return (self.width, self.height)
454d117af3a5 Missed a checkin
andy_robinson
parents: 1440
diff changeset
   293
454d117af3a5 Missed a checkin
andy_robinson
parents: 1440
diff changeset
   294
    def drawOn(self, canvas, x, y, _sW=0):
454d117af3a5 Missed a checkin
andy_robinson
parents: 1440
diff changeset
   295
        """Don't do this at home!  The standard calls for implementing
454d117af3a5 Missed a checkin
andy_robinson
parents: 1440
diff changeset
   296
        draw(); we are hooking this in order to delegate ALL the drawing
454d117af3a5 Missed a checkin
andy_robinson
parents: 1440
diff changeset
   297
        work to the embedded table object.
454d117af3a5 Missed a checkin
andy_robinson
parents: 1440
diff changeset
   298
        """
454d117af3a5 Missed a checkin
andy_robinson
parents: 1440
diff changeset
   299
        self._table.drawOn(canvas, x, y, _sW)
454d117af3a5 Missed a checkin
andy_robinson
parents: 1440
diff changeset
   300
454d117af3a5 Missed a checkin
andy_robinson
parents: 1440
diff changeset
   301
class ReferenceText(IndexingFlowable):
454d117af3a5 Missed a checkin
andy_robinson
parents: 1440
diff changeset
   302
    """Fakery to illustrate how a reference would work if we could
454d117af3a5 Missed a checkin
andy_robinson
parents: 1440
diff changeset
   303
    put it in a paragraph."""
454d117af3a5 Missed a checkin
andy_robinson
parents: 1440
diff changeset
   304
    def __init__(self, textPattern, targetKey):
454d117af3a5 Missed a checkin
andy_robinson
parents: 1440
diff changeset
   305
        self.textPattern = textPattern
454d117af3a5 Missed a checkin
andy_robinson
parents: 1440
diff changeset
   306
        self.target = targetKey
454d117af3a5 Missed a checkin
andy_robinson
parents: 1440
diff changeset
   307
        self.paraStyle = ParagraphStyle('tmp')
454d117af3a5 Missed a checkin
andy_robinson
parents: 1440
diff changeset
   308
        self._lastPageNum = None
454d117af3a5 Missed a checkin
andy_robinson
parents: 1440
diff changeset
   309
        self._pageNum = -999
454d117af3a5 Missed a checkin
andy_robinson
parents: 1440
diff changeset
   310
        self._para = None
454d117af3a5 Missed a checkin
andy_robinson
parents: 1440
diff changeset
   311
454d117af3a5 Missed a checkin
andy_robinson
parents: 1440
diff changeset
   312
    def beforeBuild(self):
454d117af3a5 Missed a checkin
andy_robinson
parents: 1440
diff changeset
   313
        self._lastPageNum = self._pageNum
454d117af3a5 Missed a checkin
andy_robinson
parents: 1440
diff changeset
   314
454d117af3a5 Missed a checkin
andy_robinson
parents: 1440
diff changeset
   315
    def notify(self, kind, stuff):
454d117af3a5 Missed a checkin
andy_robinson
parents: 1440
diff changeset
   316
        if kind == 'Target':
454d117af3a5 Missed a checkin
andy_robinson
parents: 1440
diff changeset
   317
            (key, pageNum) = stuff
454d117af3a5 Missed a checkin
andy_robinson
parents: 1440
diff changeset
   318
            if key == self.target:
454d117af3a5 Missed a checkin
andy_robinson
parents: 1440
diff changeset
   319
                self._pageNum = pageNum
454d117af3a5 Missed a checkin
andy_robinson
parents: 1440
diff changeset
   320
454d117af3a5 Missed a checkin
andy_robinson
parents: 1440
diff changeset
   321
    def wrap(self, availWidth, availHeight):
454d117af3a5 Missed a checkin
andy_robinson
parents: 1440
diff changeset
   322
        text = self.textPattern % self._lastPageNum
454d117af3a5 Missed a checkin
andy_robinson
parents: 1440
diff changeset
   323
        self._para = Paragraph(text, self.paraStyle)
454d117af3a5 Missed a checkin
andy_robinson
parents: 1440
diff changeset
   324
        return self._para.wrap(availWidth, availHeight)
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1559
diff changeset
   325
1444
454d117af3a5 Missed a checkin
andy_robinson
parents: 1440
diff changeset
   326
    def drawOn(self, canvas, x, y, _sW=0):
454d117af3a5 Missed a checkin
andy_robinson
parents: 1440
diff changeset
   327
        self._para.drawOn(canvas, x, y, _sW)
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1559
diff changeset
   328
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1559
diff changeset
   329