reportlab: improved support for onDraw and SimpleIndex
authorrgbecker
Fri, 13 Feb 2009 17:14:47 +0000
changeset 3111 86a3158c50bd
parent 3110 c5b325f692ab
child 3112 564c4ef94978
reportlab: improved support for onDraw and SimpleIndex
src/reportlab/platypus/paraparser.py
src/reportlab/platypus/tableofcontents.py
--- a/src/reportlab/platypus/paraparser.py	Fri Feb 13 12:39:57 2009 +0000
+++ b/src/reportlab/platypus/paraparser.py	Fri Feb 13 17:14:47 2009 +0000
@@ -759,6 +759,8 @@
         self.handle_data('')
         self._pop()
 
+    end_onDraw=end_seq
+
 
     #---------------------------------------------------------------
     def _push(self,**attr):
--- a/src/reportlab/platypus/tableofcontents.py	Fri Feb 13 12:39:57 2009 +0000
+++ b/src/reportlab/platypus/tableofcontents.py	Fri Feb 13 17:14:47 2009 +0000
@@ -44,16 +44,21 @@
 epsilon.
 """
 
-import string
-
 from reportlab.lib import enums
 from reportlab.lib.units import cm
 from reportlab.lib.styles import ParagraphStyle
 from reportlab.platypus.paragraph import Paragraph
 from reportlab.platypus.doctemplate import IndexingFlowable
 from reportlab.platypus.tables import TableStyle, Table
-from reportlab.platypus.flowables import Spacer
+from reportlab.platypus.flowables import Spacer, Flowable
 from reportlab.pdfbase.pdfmetrics import stringWidth
+try:
+    set
+except:
+    class set(list):
+        def add(self,x):
+            if x not in self:
+                list.append(self,x)
 
 # Default paragraph styles for tables of contents.
 # (This could also be generated automatically or even
@@ -150,7 +155,6 @@
         if kind == 'TOCEntry':
             self.addEntry(*stuff)
 
-
     def clearEntries(self):
         self._entries = []
 
@@ -242,7 +246,6 @@
         """
         self._table.drawOn(canvas, x, y, _sW)
 
-
 class SimpleIndex(IndexingFlowable):
     """This creates a very simple index.
 
@@ -281,10 +284,7 @@
 
     def addEntry(self, text, pageNum):
         """Allows incremental buildup"""
-        if self._entries.has_key(text):
-            self._entries[text].append(str(pageNum))
-        else:
-            self._entries[text] = [pageNum]
+        self._entries.setdefault(text,set([])).add(pageNum)
 
     def split(self, availWidth, availHeight):
         """At this stage we do not care about splitting the entries,
@@ -300,23 +300,27 @@
         # we draw the LAST RUN's entries!  If there are
         # none, we make some dummy data to keep the table
         # from complaining
-        if len(self._lastEntries) == 0:
-            _tempEntries = [('Placeholder for index',[0,1,2])]
+        if not self._lastEntries:
+            if self._entries:
+                _tempEntries = self._entries.items()
+            else:
+                _tempEntries = [('Placeholder for index',[0,1,2])]
         else:
             _tempEntries = self._lastEntries.items()
-            _tempEntries.sort()
+
+        _tempEntries.sort()
 
         tableData = []
-        for (text, pageNumbers) in _tempEntries:
+        for text, pageNumbers in _tempEntries:
             #right col style is right aligned
-            allText = text + ': ' + string.join(map(str, pageNumbers), ', ')
+            allText = text + ': ' + ', '.join(map(str, pageNumbers))
             para = Paragraph(allText, self.textStyle)
             tableData.append([para])
 
         self._table = Table(tableData, colWidths=[availWidth])
 
         self.width, self.height = self._table.wrapOn(self.canv,availWidth, availHeight)
-        return (self.width, self.height)
+        return self.width, self.height
 
     def drawOn(self, canvas, x, y, _sW=0):
         """Don't do this at home!  The standard calls for implementing
@@ -325,6 +329,17 @@
         """
         self._table.drawOn(canvas, x, y, _sW)
 
+    def draw(self):
+        t = self._table
+        ocanv = getattr(t,'canv',None)
+        if not ocanv:
+            t.canv = self.canv
+        try:
+            t.draw()
+        finally:
+            if not ocanv:
+                del t.canv
+
 class ReferenceText(IndexingFlowable):
     """Fakery to illustrate how a reference would work if we could
     put it in a paragraph."""