Author: jcater Date: 2006-07-18 16:48:51 -0500 (Tue, 18 Jul 2006) New Revision: 8534
Modified: trunk/gnue-common/src/printing/pdftable/pdftable.py trunk/gnue-common/src/printing/pdftable/sample.py Log: misc changes to make pdftable subclassable; do away with module globals Modified: trunk/gnue-common/src/printing/pdftable/pdftable.py =================================================================== --- trunk/gnue-common/src/printing/pdftable/pdftable.py 2006-07-17 14:34:11 UTC (rev 8533) +++ trunk/gnue-common/src/printing/pdftable/pdftable.py 2006-07-18 21:48:51 UTC (rev 8534) @@ -68,7 +68,7 @@ 'title3Font' : ('Helvetica-Oblique',12, 14), 'repeatTitleFont' : ('Helvetica-Oblique', 9, 10), 'footerFont' : ('Times-Roman', 9, 10), - + 'subtitleFont' : ('Times-Bold', 12, 13), 'subtitleLabelFont' : ('Times-Roman', 12, 13), 'subtitleContinueFont' : ('Times-Italic', 10, 13), @@ -79,8 +79,6 @@ # # TODO: This really needs handled in a more flexible mannor. # ---------------------------------------------------------------------------- -highlightColor = colors.HexColor("0xffffcc") -headingColor = subtotalColor = colors.HexColor("0xe6e6ff") # ---------------------------------------------------------------------------- @@ -98,7 +96,7 @@ topmargin = .75*inch # This is nothing but voodoo guesses... -# Greatly depends on pointsize of fontDefs['dataFont'] +# Greatly depends on pointsize of self.fontDefs['dataFont'] # TODO: This should probably be computed based on width of a "0" # TODO: and the width of the report. But, eh, this works for now... maxColsForPortraitNonscaled = 100 # Number of columns before we start scaling down @@ -109,7 +107,7 @@ # Class definition # ============================================================================ class pdftable: - + fontDefs = fontDefs # ========================================================================== # Creation/closure functions # ========================================================================== @@ -117,7 +115,7 @@ # -------------------------------------------------------------------------- # Initialize # -------------------------------------------------------------------------- - def __init__(self, file, parameterBox = ()): + def __init__(self, destination, parameterBox = ()): """ A simple table based report generator. @@ -131,9 +129,12 @@ """ self._titleList = ['Your Report','Your Report'] - self.file = file + self.destination = destination # self.parameterBox = parameterBox + self.highlightColor = colors.HexColor("0xffffcc") + self.headingColor = self.subtotalColor = colors.HexColor("0xe6e6ff") + self.highlightIndex = 0 self.init = 1 self._scalingComplete = 0 @@ -333,10 +334,10 @@ self.y -= 4 * self.scale if style in ("Subtotal","Total"): - font, size, tracking = fontDefs['subtotalFont'] + font, size, tracking = self.fontDefs['subtotalFont'] fontWidth = self.subtotalFontWidth else: - font, size, tracking = fontDefs['dataFont'] + font, size, tracking = self.fontDefs['dataFont'] fontWidth = self.dataFontWidth size = size * self.scale @@ -357,13 +358,13 @@ if style in ("Subtotal","Total"): self.drawHorizBorderedBox(leftmargin, boxy, self.width-leftmargin*2, - boxh, subtotalColor, lines=lineWidth * self.scale) + boxh, self.subtotalColor, lines=lineWidth * self.scale) elif highlighted: # If there is a bottom border we need to not draw over the top # of the previous rows border. Currently minimum must be 1 point adjustment = max([self._currentSection['columnBottomBorder'][0],1]) self.drawHorizBorderedBox(leftmargin, boxy, self.width-leftmargin*2, - boxh-adjustment, highlightColor, lines=0) + boxh-adjustment, self.highlightColor, lines=0) canvas.setFont(font, size) @@ -389,7 +390,7 @@ else: adjust = 0 if highlighted: - color = colors.Blacker(highlightColor,.98) + color = colors.Blacker(self.highlightColor,.98) else: color = highlightColumn @@ -463,7 +464,7 @@ """ canvas = self.canvas - font, size, tracking = fontDefs['dataFont'] + font, size, tracking = self.fontDefs['dataFont'] fontWidth = self.dataFontWidth size = size * self.scale @@ -482,7 +483,7 @@ # of the previous rows border. Currently minimum must be 1 point adjustment = max([self._currentSection['columnBottomBorder'][0],1]) self.drawHorizBorderedBox(leftmargin, boxy, self.width-leftmargin*2, - boxh-adjustment, highlightColor, lines=0) + boxh-adjustment, self.highlightColor, lines=0) # Place a border around the memo line if borders: @@ -566,7 +567,7 @@ Private function that creates the footer containing the time/page # """ canvas = self.canvas - font, size, tracking = fontDefs['footerFont'] + font, size, tracking = self.fontDefs['footerFont'] canvas.setFont(font, size) canvas.drawString(leftmargin, topmargin, self.timestamp) canvas.drawRightString(self.width - leftmargin, topmargin, "Page %s" % self.page) @@ -580,7 +581,7 @@ Private function that creates a full (first page) header on a new page. """ canvas = self.canvas - self.y -= fontDefs['titleFont'][2] + self.y -= self.fontDefs['titleFont'][2] for text, fontspec in self._titleList: if text: font, size, tracking = fontspec @@ -597,7 +598,7 @@ Private function that creates a short ( non first page) header on a new page. """ canvas = self.canvas - font, size, tracking = fontDefs['repeatTitleFont'] + font, size, tracking = self.fontDefs['repeatTitleFont'] self. y -= size canvas.setFont(font, size) canvas.drawString(leftmargin, self.y, self._titleList[0][0]) @@ -617,16 +618,16 @@ if not self.subtitle: return - self.y -= fontDefs['subtitleFont'][2] + self.y -= self.fontDefs['subtitleFont'][2] - font, size, tracking = fontDefs['subtitleLabelFont'] + font, size, tracking = self.fontDefs['subtitleLabelFont'] text = canvas.beginText(leftmargin, self.y) for l in self.subtitle.split(): boldOff = 0 if l[0] == '*': l = l[1:] - font, size, tracking = fontDefs['subtitleFont'] + font, size, tracking = self.fontDefs['subtitleFont'] if l[-1] == '*': boldOff = 1 @@ -635,11 +636,11 @@ text.setFont(font, size) text.textOut(l+ ' ') if boldOff: - font, size, tracking = fontDefs['subtitleLabelFont'] + font, size, tracking = self.fontDefs['subtitleLabelFont'] text.textOut(' ') if self.continued: - font2, size2, tracking2 = fontDefs['subtitleContinueFont'] + font2, size2, tracking2 = self.fontDefs['subtitleContinueFont'] text.setFont(font2, size2) text.textOut("(Continued)") @@ -658,7 +659,7 @@ numRows = len(self._currentSection['headerList']) - font, size, tracking = fontDefs['tableHeaderFont'] + font, size, tracking = self.fontDefs['tableHeaderFont'] size = size * self.scale tracking = tracking * self.scale canvas.setFont(font, size) @@ -666,7 +667,7 @@ boxy = self.y + tracking - (tracking-size)/2.0 boxh = -tracking*numRows - (tracking-size)/2.0 - lineWidth*self.scale self.drawHorizBorderedBox(leftmargin, boxy, self.width-leftmargin*2, - boxh, headingColor) + boxh, self.headingColor) for list in self._currentSection['headerList']: for header in list: @@ -754,15 +755,20 @@ if self.scale < 1: print "Scaling to %.2f%%" % (self.scale*100) - # in pts, Amount of gap to leave between columns... doesn't need to be too much + # in pts, Amount of gap to leave between columns... + # doesn't need to be too much self.columnGap = self.columnGap * self.scale - self.canvas = canvas.Canvas(self.file, pagesize=self.pageOrient(self.pageSize)) + if isinstance(self.destination, canvas.Canvas): + self.canvas = self.destination + else: + self.canvas = canvas.Canvas(self.destination, + pagesize=self.pageOrient(self.pageSize)) - font, size, leading = fontDefs['dataFont'] + font, size, leading = self.fontDefs['dataFont'] self.dataFontWidth = getFont(font).stringWidth - font, size, leading = fontDefs['subtotalFont'] + font, size, leading = self.fontDefs['subtotalFont'] self.subtotalFontWidth = getFont(font).stringWidth # This is not scaled down according to self.scale... @@ -775,11 +781,9 @@ x += colSize + self.columnGap def setHighlightColorHex(self, hexColor): - global highlightColor - highlightColor = colors.HexColor(hexColor) + self.highlightColor = colors.HexColor(hexColor) def setFont(self, fontStyle, settings): - global fontDefs - assert fontStyle in fontDefs.keys(), 'Invalid font style: %s' + assert fontStyle in self.fontDefs.keys(), 'Invalid font style: %s' - fontDefs[fontStyle] = settings + self.fontDefs[fontStyle] = settings Modified: trunk/gnue-common/src/printing/pdftable/sample.py =================================================================== --- trunk/gnue-common/src/printing/pdftable/sample.py 2006-07-17 14:34:11 UTC (rev 8533) +++ trunk/gnue-common/src/printing/pdftable/sample.py 2006-07-18 21:48:51 UTC (rev 8534) @@ -56,8 +56,8 @@ writer = pdftable(f) - writer.setFullTitle([("Quarterly Sales Report", fontDefs['titleFont']), - ('2002 - 2004', fontDefs['title2Font']), + writer.setFullTitle([("Quarterly Sales Report", writer.fontDefs['titleFont']), + ('2002 - 2004', writer.fontDefs['title2Font']), ]) # ======================================================================== @@ -75,8 +75,10 @@ writer.addColumn(RIGHT, 5, leftBorder=.25) writer.addColumn(RIGHT, 7) # 2003 count and % - writer.addColumn(RIGHT, 5, highlight=colors.Whiter(highlightColor,.3)) - writer.addColumn(RIGHT, 7, highlight=colors.Whiter(highlightColor,.3)) + writer.addColumn(RIGHT, 5, + highlight=colors.Whiter(writer.highlightColor,.3)) + writer.addColumn(RIGHT, 7, + highlight=colors.Whiter(writer.highlightColor,.3)) # 2004 count and % writer.addColumn(RIGHT, 5) writer.addColumn(RIGHT, 7) _______________________________________________ commit-gnue mailing list commit-gnue@gnu.org http://lists.gnu.org/mailman/listinfo/commit-gnue