table color cycle changes
authorandy
Wed, 29 Sep 2004 10:18:32 +0000
changeset 2392 6c59b0eb312d
parent 2391 1540e0003820
child 2393 6b3e47730acc
table color cycle changes
reportlab/lib/colors.py
reportlab/lib/corp.py
reportlab/platypus/flowables.py
reportlab/platypus/tables.py
reportlab/test/test_platypus_tables.py
--- a/reportlab/lib/colors.py	Wed Sep 29 09:17:52 2004 +0000
+++ b/reportlab/lib/colors.py	Wed Sep 29 10:18:32 2004 +0000
@@ -512,6 +512,13 @@
             raise 'Invalid color value', str(arg)
         return default
 
+def toColorOrNone(arg,default=None):
+    '''as above but allows None as a legal value'''
+    if arg is None:
+        return None
+    else:
+        return toColor(arg, default)
+
 def setColors(**kw):
     UNDEF = []
     progress = 1
--- a/reportlab/lib/corp.py	Wed Sep 29 09:17:52 2004 +0000
+++ b/reportlab/lib/corp.py	Wed Sep 29 10:18:32 2004 +0000
@@ -411,31 +411,22 @@
 
     #wbite on blue
     rl = RL_CorpLogo()
-    D = Drawing(rl.width,rl.height)
-    D.add(rl)
-    D.__dict__['verbose'] = 1
-    D.save(fnRoot='corplogo_whiteonblue',formats=['pdf','eps'])
-
     rl.width = 129
     rl.height = 86
     D = Drawing(rl.width,rl.height)
     D.add(rl)
     D.__dict__['verbose'] = 1
-    D.save(fnRoot='corplogo_whiteonblue',formats=['jpg','gif'])
+    D.save(fnRoot='corplogo_whiteonblue',formats=['pdf','eps','jpg','gif'])
 
 
     #blue on white
     rl = RL_CorpLogoReversed()
-    D = Drawing(rl.width,rl.height)
-    D.add(rl)
-    D.__dict__['verbose'] = 1
-    D.save(fnRoot='corplogo_blueonwhite',formats=['pdf','eps'])
     rl.width = 129
     rl.height = 86
     D = Drawing(rl.width,rl.height)
     D.add(rl)
     D.__dict__['verbose'] = 1
-    D.save(fnRoot='corplogo_whiteonblue',formats=['jpg','gif'])
+    D.save(fnRoot='corplogo_blueonwhite',formats=['pdf','eps','jpg','gif'])
 
 
 
--- a/reportlab/platypus/flowables.py	Wed Sep 29 09:17:52 2004 +0000
+++ b/reportlab/platypus/flowables.py	Wed Sep 29 10:18:32 2004 +0000
@@ -442,6 +442,7 @@
     return W, H
 
 def _makeIndexable(V):
+    "if it isn't a list or tuple, wrap it in a list"
     if type(V) not in (ListType, TupleType): V = V is not None and [V] or []
     return V
 
--- a/reportlab/platypus/tables.py	Wed Sep 29 09:17:52 2004 +0000
+++ b/reportlab/platypus/tables.py	Wed Sep 29 10:18:32 2004 +0000
@@ -603,6 +603,10 @@
     def _addCommand(self,cmd):
         if cmd[0] == 'BACKGROUND':
             self._bkgrndcmds.append(cmd)
+        if cmd[0] == 'ROWBACKGROUNDS':
+            self._bkgrndcmds.append(cmd)
+        if cmd[0] == 'COLBACKGROUNDS':
+            self._bkgrndcmds.append(cmd)
         elif cmd[0] == 'SPAN':
             self._spanCmds.append(cmd)
         elif _isLineCommand(cmd):
@@ -941,9 +945,40 @@
             canv = self.canv
             if callable(arg):
                 apply(arg,(self,canv, x0, y0, w, h))
-            else:
+            elif cmd == 'BACKGROUND':
                 canv.setFillColor(colors.toColor(arg))
                 canv.rect(x0, y0, w, h, stroke=0,fill=1)
+            elif cmd == 'ROWBACKGROUNDS':
+                #Need a list of colors to cycle through.  The arguments
+                #might be already colours, or convertible to colors, or
+                # None, or the string 'None'.
+                #It's very common to alternate a pale shade with None.
+                #print 'rowHeights=', self._rowHeights
+                colorCycle = map(colors.toColorOrNone, arg)
+                count = len(colorCycle)
+                rowCount = er - sr
+                for i in range(rowCount):
+                    color = colorCycle[i%count]
+                    h = self._rowHeights[sr + i]
+                    if color:
+                        canv.setFillColor(color)
+                        canv.rect(x0, y0, w, -h, stroke=0,fill=1)
+                    #print '    draw %0.0f, %0.0f, %0.0f, %0.0f' % (x0,y0,w,-h)
+                    y0 = y0 - h
+
+            elif cmd == 'COLBACKGROUNDS':
+                #cycle through colours columnwise
+                colorCycle = map(colors.toColorOrNone, arg)
+                count = len(colorCycle)
+                colCount = ec - sc
+                for i in range(colCount):
+                    color = colorCycle[i%count]
+                    w = self._colWidths[sc + i]
+                    if color:
+                        canv.setFillColor(color)
+                        canv.rect(x0, y0, w, h, stroke=0,fill=1)
+                    x0 = x0 +w
+                
 
     def _drawCell(self, cellval, cellstyle, (colpos, rowpos), (colwidth, rowheight)):
         if self._curcellstyle is not cellstyle:
@@ -1102,6 +1137,12 @@
      ('ALIGN', (1,1), (-1,-1), 'RIGHT')]
     )
 
+
+# experimental iterator which can apply a sequence
+# of colors e.g. Blue, None, Blue, None as you move
+# down.
+
+
 if __name__ == '__main__':
     from reportlab.test.test_platypus_tables import old_tables_test
     old_tables_test()
--- a/reportlab/test/test_platypus_tables.py	Wed Sep 29 09:17:52 2004 +0000
+++ b/reportlab/test/test_platypus_tables.py	Wed Sep 29 10:18:32 2004 +0000
@@ -536,6 +536,50 @@
     t=LongTable(data, style=sty, colWidths = [50,100,200])
     lst.append(t)
 
+
+    lst.append(PageBreak())
+
+    lst.append(Paragraph('Trying colour cycling in background', styleSheet['Heading1']))
+    lst.append(Paragraph("This should alternate pale blue and uncolored by row", styleSheet['BodyText']))
+    data=  [['001', '01', '02', '03', '04', '05'],
+            ['002', '01', '02', '03', '04', '05'],
+            ['003', '01', '02', '03', '04', '05'],
+            ['004', '01', '02', '03', '04', '05'],
+            ['005', '01', '02', '03', '04', '05'],
+            ['006', '01', '02', '03', '04', '05'],
+            ['007', '01', '02', '03', '04', '05'],
+            ['008', '01', '02', '03', '04', '05'],
+            ['009', '01', '02', '03', '04', '05'],
+            ['010', '01', '02', '03', '04', '05'],
+
+            ]
+    t=Table(data,style=[
+                    ('GRID',(0,0),(-1,-1),0.5,colors.grey),
+                    ('ROWBACKGROUNDS', (0, 0), (-1, -1), (0xD0D0FF, None)),
+                    ])
+    lst.append(t)
+    lst.append(Spacer(0,6))
+    lst.append(Paragraph("And this should pale blue, pale pink and None by column", styleSheet['BodyText']))
+    data=  [['001', '01', '02', '03', '04', '05'],
+            ['002', '01', '02', '03', '04', '05'],
+            ['003', '01', '02', '03', '04', '05'],
+            ['004', '01', '02', '03', '04', '05'],
+            ['005', '01', '02', '03', '04', '05'],
+            ['006', '01', '02', '03', '04', '05'],
+            ['007', '01', '02', '03', '04', '05'],
+            ['008', '01', '02', '03', '04', '05'],
+            ['009', '01', '02', '03', '04', '05'],
+            ['010', '01', '02', '03', '04', '05'],
+
+            ]
+    t=Table(data,style=[
+                    ('GRID',(0,0),(-1,-1),0.5,colors.grey),
+                    ('COLBACKGROUNDS', (0, 0), (-1, -1), (0xD0D0FF, 0xFFD0D0, None)),
+                    ])
+    lst.append(t)
+
+
+
     SimpleDocTemplate(outputfile('tables.pdf'), showBoundary=1).build(lst)