X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/c5d11c71d791122ec50379c15887898a4d49f1af..7f3450784cc9c696f269ff1bfde3623c180d4b58:/wxPython/wx/lib/printout.py diff --git a/wxPython/wx/lib/printout.py b/wxPython/wx/lib/printout.py index 086a6c1fc0..2c11a6f2c6 100644 --- a/wxPython/wx/lib/printout.py +++ b/wxPython/wx/lib/printout.py @@ -15,14 +15,15 @@ # add index to data list after parsing total pages for paging #---------------------------------------------------------------------------- # 12/10/2003 - Jeff Grimmett (grimmtooth@softhome.net) -# # o 2.5 compatability update. -# - +#---------------------------------------------------------------------------- +# 11/23/2004 - Vernon Cole (wnvcole@peppermillcas.com) +# o Generalize for non-2-dimensional sequences and non-text data +# (can use as a simple text printer by supplying a list of strings.) +# o Add a small _main_ for self test + import copy -import os -import sys - +import types import wx class PrintBase: @@ -268,10 +269,47 @@ class PrintTableDraw(wx.ScrolledWindow, PrintBase): pos_x = self.left_margin * self.pwidth + self.horizontal_offset # left margin self.column.append(pos_x) + #module logic expects two dimensional data -- fix input if needed + if isinstance(self.data,types.StringTypes): + self.data = [[copy.copy(self.data)]] # a string becomes a single cell + try: + rows = len(self.data) + except TypeError: + self.data = [[str(self.data)]] # a non-iterable becomes a single cell + rows = 1 + first_value = self.data[0] + + if isinstance(first_value, types.StringTypes): # a sequence of strings + if self.label == [] and self.set_column == []: + data = [] + for x in self.data: #becomes one column + data.append([x]) + else: + data = [self.data] #becames one row + self.data = data + first_value = data[0] + try: + column_total = len(first_value) + except TypeError: # a sequence of non-iterables + if self.label == [] and self.set_column == []: + data = [] #becomes one column + for x in self.data: + data.append([str(x)]) + column_total = 1 + else: + data = [self.data] #becomes one row + column_total = len(self.data) + self.data = data + first_value = data[0] + if self.set_column == []: table_width = self.page_width - self.left_margin - self.right_margin - width = table_width/(len(self.label)) - for val in self.label: + if self.label == []: + temp = first_value + else: + temp = self.label + width = table_width/(len(temp)) + for val in temp: column_width = width * self.pwidth pos_x = pos_x + column_width self.column.append(pos_x) # position of each column @@ -290,13 +328,10 @@ class PrintTableDraw(wx.ScrolledWindow, PrintBase): print "Column Settings Incorrect", "\nColumn Value: " + str(self.column), "\nLabel Value: " + str(self.label) return - first_value = self.data[0] - column_total = len(first_value) if column_total != len(self.column) -1: - print "Column Settings Incorrect", first_value, self.column + print "Cannot fit", first_value, 'in', len(self.column)-1, 'columns.' return - col = 0 for col in range(column_total): try: align = set_column_align[col] # check if custom column alignment @@ -314,10 +349,8 @@ class PrintTableDraw(wx.ScrolledWindow, PrintBase): colour = set_column_txtcolour[col] # check if custom column text colour except: colour = self.GetFontColour(self.parent.text_font) - self.column_txtcolour.append(colour) - col = col + 1 def SetPointAdjust(self): f = wx.Font(10, wx.SWISS, wx.NORMAL, wx.NORMAL) # setup using 10 point @@ -526,12 +559,14 @@ class PrintTableDraw(wx.ScrolledWindow, PrintBase): self.col = 0 max_y = 0 for vtxt in row_val: + if not isinstance(vtxt,types.StringTypes): + vtxt = str(vtxt) self.region = self.column[self.col+1] - self.column[self.col] self.indent = self.column[self.col] self.align = self.column_align[self.col] fcolour = self.column_txtcolour[self.col] # set font colour - celltext = self.GetCellText(self.data_cnt, self.col) + celltext = self.GetCellTextColour(self.data_cnt, self.col) if celltext is not None: fcolour = celltext # override the column colour @@ -554,7 +589,7 @@ class PrintTableDraw(wx.ScrolledWindow, PrintBase): except: return None - def GetCellText(self, row, col): # check if custom colour defined for the cell text + def GetCellTextColour(self, row, col): # check if custom colour defined for the cell text try: set = self.cell_text[row] except: @@ -570,12 +605,15 @@ class PrintTableDraw(wx.ScrolledWindow, PrintBase): self.DrawColumns() # draw all vertical lines def DrawGridLine(self): - if self.draw == True: + if self.draw == True \ + and len(self.column) > 2: #supress grid lines if only one column try: size = self.row_line_size[self.data_cnt] except: size = self.row_def_line_size + if size < 1: return + try: colour = self.row_line_colour[self.data_cnt] except: @@ -588,7 +626,8 @@ class PrintTableDraw(wx.ScrolledWindow, PrintBase): self.DC.DrawLine(self.column[0], y_out, self.end_x, y_out) def DrawColumns(self): - if self.draw == True: + if self.draw == True \ + and len(self.column) > 2: #surpress grid line if only one column col = 0 for val in self.column: try: @@ -596,6 +635,8 @@ class PrintTableDraw(wx.ScrolledWindow, PrintBase): except: size = self.column_def_line_size + if size < 1: continue + try: colour = self.column_line_colour[col] except: @@ -720,8 +761,8 @@ class PrintTable: self.footer_type = "Pageof" def SetMargins(self): - self.left_margin = 1.0 - self.right_margin = 1.0 # only used if no column sizes + self.left_margin = 0.5 + self.right_margin = 0.5 # only used if no column sizes self.top_margin = 0.8 self.bottom_margin = 1.0 @@ -869,15 +910,15 @@ class PrintTable: self.footer.append(set) def Preview(self): + data = wx.PrintDialogData(self.printData) printout = SetPrintout(self) printout2 = SetPrintout(self) - self.preview = wx.PrintPreview(printout, printout2, self.printData) + self.preview = wx.PrintPreview(printout, printout2, data) if not self.preview.Ok(): - wxMessageBox("There was a problem printing!", "Printing", wx.OK) + wx.MessageBox("There was a problem printing!", "Printing", wx.OK) return self.preview.SetZoom(60) # initial zoom value - frame = wx.PreviewFrame(self.preview, self.parentFrame, "Print preview") frame.Initialize() @@ -887,14 +928,13 @@ class PrintTable: frame.Show(True) def Print(self): - pdd = wx.PrintDialogData() - pdd.SetPrintData(self.printData) + pdd = wx.PrintDialogData(self.printData) printer = wx.Printer(pdd) printout = SetPrintout(self) if not printer.Print(self.parentFrame, printout): wx.MessageBox("There was a problem printing.\nPerhaps your current printer is not set correctly?", "Printing", wx.OK) else: - self.printData = printer.GetPrintDialogData().GetPrintData() + self.printData = wx.PrintData( printer.GetPrintDialogData().GetPrintData() ) printout.Destroy() def DoDrawing(self, DC): @@ -1020,10 +1060,10 @@ class SetPrintout(wx.Printout): self.end_pg = 1000 def OnBeginDocument(self, start, end): - return self.base_OnBeginDocument(start, end) + return super(SetPrintout, self).OnBeginDocument(start, end) def OnEndDocument(self): - self.base_OnEndDocument() + super(SetPrintout, self).OnEndDocument() def HasPage(self, page): try: @@ -1043,7 +1083,7 @@ class SetPrintout(wx.Printout): return (str_pg, end_pg, str_pg, end_pg) def OnPreparePrinting(self): - self.base_OnPreparePrinting() + super(SetPrintout, self).OnPreparePrinting() def OnBeginPrinting(self): dc = self.GetDC() @@ -1059,7 +1099,7 @@ class SetPrintout(wx.Printout): scaleY = float(h) / 1000 self.printUserScale = min(scaleX, scaleY) - self.base_OnBeginPrinting() + super(SetPrintout, self).OnBeginPrinting() def GetSize(self): self.psizew, self.psizeh = self.GetPPIPrinter() @@ -1091,8 +1131,23 @@ class SetPrintout(wx.Printout): self.canvas.DoDrawing(dc) return True - - - - - +if __name__ == '__main__': + app = wx.PySimpleApp() + frame = wx.Frame(None, -1, "Dummy wx frame for testing printout.py") + frame.Show(True) + ptbl = PrintTable(frame) + ptbl.SetHeader('This is the test HEADER') + # a single sequence will print out as a single column with no borders ... + ptbl.data = ( + 'This is the first line of text.', + 'This is the second line\nand the third. The fourth will be the number "4.0".', + 04.00, + 'This is the fifth line, but by design it is too long to fit in the width of a standard'\ + ' page, so it will be forced to wrap around in order to fit without having '\ + 'some of its verbose verbage truncated.', + 'Here we have the final line.' + ) + #... but, if labels or columns are defined, a single sequence will print out as a single row + ##ptbl.label = ('One','Two','Three','Four','5') + ptbl.Preview() + app.MainLoop()