added FrameBG frame action to allow frame backgrounding
authorrobin
Tue, 02 Jul 2013 10:17:00 +0100
changeset 3701 b3c5693903bf
parent 3700 fd16be681c51
child 3702 b230ea9a15da
added FrameBG frame action to allow frame backgrounding
src/reportlab/platypus/__init__.py
src/reportlab/platypus/doctemplate.py
src/reportlab/platypus/frames.py
tests/test_platypus_tables.py
--- a/src/reportlab/platypus/__init__.py	Tue Jun 11 10:49:00 2013 +0100
+++ b/src/reportlab/platypus/__init__.py	Tue Jul 02 10:17:00 2013 +0100
@@ -12,5 +12,6 @@
 from reportlab.platypus.tables import Table, TableStyle, CellStyle, LongTable
 from reportlab.platypus.frames import Frame
 from reportlab.platypus.doctemplate import BaseDocTemplate, NextPageTemplate, PageTemplate, ActionFlowable, \
-                        SimpleDocTemplate, FrameBreak, PageBegin, Indenter, NotAtTopPageBreak
+                        SimpleDocTemplate, FrameBreak, PageBegin, Indenter, NotAtTopPageBreak, \
+                        FrameBG
 from xpreformatted import XPreformatted
--- a/src/reportlab/platypus/doctemplate.py	Tue Jun 11 10:49:00 2013 +0100
+++ b/src/reportlab/platypus/doctemplate.py	Tue Jul 02 10:17:00 2013 +0100
@@ -233,6 +233,25 @@
         frame._leftExtraIndent += self.left
         frame._rightExtraIndent += self.right
 
+class FrameBG(FrameActionFlowable):
+    """Start or stop coloring the frame background
+    left & right are distances from the edge of the frame to start stop colouring.
+    """
+    _ZEROSIZE=True
+    width=0
+    height=0
+    def __init__(self, bg=None, left=0, right=0, start=True):
+        self.bgLeft = _evalMeasurement(left)
+        self.bgRight = _evalMeasurement(right)
+        self.bg = bg
+        self.start = start
+
+    def frameAction(self, frame):
+        if self.start:
+            frame._frameBGs.append((self.bgLeft,self.bgRight,self.bg))
+        elif frame._frameBGs:
+            frame._frameBGs.pop()
+
 class NotAtTopPageBreak(FrameActionFlowable):
     def __init__(self):
         pass
@@ -496,6 +515,7 @@
         #context sensitive margins - set by story, not from outside
         self._leftExtraIndent = 0.0
         self._rightExtraIndent = 0.0
+        self._frameBGs = []
 
         self._calc()
         self.afterInit()
@@ -561,6 +581,7 @@
         self._removeVars(('page','frame'))
         self._leftExtraIndent = self.frame._leftExtraIndent
         self._rightExtraIndent = self.frame._rightExtraIndent
+        self._frameBGs = self.frame._frameBGs
         #detect infinite loops...
         if self._curPageFlowableCount == 0:
             self._emptyPages += 1
@@ -613,6 +634,7 @@
                 self.frame.drawBoundary(self.canv)
         f._leftExtraIndent = self._leftExtraIndent
         f._rightExtraIndent = self._rightExtraIndent
+        f._frameBGs = self._frameBGs
 
     def handle_frameEnd(self,resume=0):
         ''' Handles the semantics of the end of a frame. This includes the selection of
@@ -621,6 +643,7 @@
         self._removeVars(('frame',))
         self._leftExtraIndent = self.frame._leftExtraIndent
         self._rightExtraIndent = self.frame._rightExtraIndent
+        self._frameBGs = self.frame._frameBGs
 
         f = self.frame
         if hasattr(self,'_nextFrameIndex'):
--- a/src/reportlab/platypus/frames.py	Tue Jun 11 10:49:00 2013 +0100
+++ b/src/reportlab/platypus/frames.py	Tue Jul 02 10:17:00 2013 +0100
@@ -171,10 +171,18 @@
                 return 0
             else:
                 #now we can draw it, and update the current point.
+                s = flowable.getSpaceAfter()
+                fbg = getattr(self,'_frameBGs',None)
+                if fbg:
+                    fbgl, fbgr, fbgc = fbg[-1]
+                    canv.saveState()
+                    canv.setFillColor(fbgc)
+                    canv.rect(self._x1+fbgl,y-s,self._width-fbgl-fbgr,h+s,stroke=0,fill=1)
+                    canv.restoreState()
+
                 flowable.drawOn(canv, self._x + self._leftExtraIndent, y, _sW=aW-w)
                 flowable.canv=canv
                 if self._debug: logger.debug('drew %s' % flowable.identity())
-                s = flowable.getSpaceAfter()
                 y -= s
                 if self._oASpace: self._prevASpace = s
                 if y!=self._y: self._atTop = 0
--- a/tests/test_platypus_tables.py	Tue Jun 11 10:49:00 2013 +0100
+++ b/tests/test_platypus_tables.py	Tue Jul 02 10:17:00 2013 +0100
@@ -6,7 +6,7 @@
 from reportlab.lib.testutils import setOutDir,makeSuiteForClasses, outputfile, printLocation
 setOutDir(__name__)
 import os,unittest
-from reportlab.platypus import Spacer, SimpleDocTemplate, Table, TableStyle
+from reportlab.platypus import Spacer, SimpleDocTemplate, Table, TableStyle, FrameBG
 from reportlab.platypus.paragraph import Paragraph
 from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
 from reportlab.lib.units import inch, cm
@@ -16,7 +16,7 @@
 from reportlab.graphics.charts.barcharts import VerticalBarChart
 
 styleSheet = getSampleStyleSheet()
-    
+
 def getTable():
     t = Table((('','North','South','East','West'),
              ('Quarter 1',100,200,300,400),
@@ -50,6 +50,7 @@
     styNormal = styleSheet['Normal']
     styBackground = ParagraphStyle('background', parent=styNormal, backColor=colors.pink)
     styH1 = styleSheet['Heading1']
+    lst.append(FrameBG(bg=colors.red))
     lst.append(Paragraph("First, a test of how tables align their content...", styH1))
     lst.append(Paragraph("""Generated with version %s""" % Version,
                         styNormal))
@@ -57,6 +58,7 @@
                          text differently to cells with Paragraphs using the
                          same font.  Hopefully now they are back on the same baseline""",
                         styNormal))
+    lst.append(FrameBG(bg=colors.blue))
     ts1 = TableStyle([
                 ('ALIGN', (0,0), (-1,0), 'RIGHT'),
                 ('BACKGROUND', (0,0), (-1,0), colors.lightgrey),
@@ -75,8 +77,10 @@
         ])
     t1.setStyle(ts1)
     lst.append(t1)
+    lst.append(FrameBG(start=False))
     lst.append(Spacer(0,10))
     lst.append(Paragraph("Now we make a table with just one cell containing a string...note how the text sits low", styNormal))
+    lst.append(FrameBG(start=False))
     
     tsGrid = TableStyle([
                 ('GRID', (0,0), (-1,-1), 0.25, colors.black),