X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/d14a1e28567de23c586bc80017073d0c39f8d18f..5589b01ba74638fcf94ee21f5581b050ee233156:/wxPython/wx/lib/printout.py diff --git a/wxPython/wx/lib/printout.py b/wxPython/wx/lib/printout.py index 56814081dc..2c11a6f2c6 100644 --- a/wxPython/wx/lib/printout.py +++ b/wxPython/wx/lib/printout.py @@ -14,36 +14,42 @@ # fixed bug for string wider than print region # add index to data list after parsing total pages for paging #---------------------------------------------------------------------------- - -import os, sys, copy - -from wxPython.wx import * -import copy +# 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 types +import wx class PrintBase: def SetPrintFont(self, font): # set the DC font parameters fattr = font["Attr"] if fattr[0] == 1: - weight = wxBOLD + weight = wx.BOLD else: - weight = wxNORMAL + weight = wx.NORMAL if fattr[1] == 1: - set_style = wxITALIC + set_style = wx.ITALIC else: - set_style = wxNORMAL + set_style = wx.NORMAL underline = fattr[2] fcolour = self.GetFontColour(font) self.DC.SetTextForeground(fcolour) - setfont = wxFont(font["Size"], wxSWISS, set_style, weight, underline) + setfont = wx.Font(font["Size"], wx.SWISS, set_style, weight, underline) setfont.SetFaceName(font["Name"]) self.DC.SetFont(setfont) def GetFontColour(self, font): fcolour = font["Colour"] - return wxColour(fcolour[0], fcolour[1], fcolour[2]) + return wx.Colour(fcolour[0], fcolour[1], fcolour[2]) def OutTextRegion(self, textout, txtdraw = True): textlines = textout.split('\n') @@ -54,14 +60,14 @@ class PrintBase: vout, remain = self.SetFlow(text, self.region) if self.draw == True and txtdraw == True: test_out = self.TestFull(vout) - if self.align == wxALIGN_LEFT: + if self.align == wx.ALIGN_LEFT: self.DC.DrawText(test_out, self.indent+self.pcell_left_margin, y) - elif self.align == wxALIGN_CENTRE: + elif self.align == wx.ALIGN_CENTRE: diff = self.GetCellDiff(test_out, self.region) self.DC.DrawText(test_out, self.indent+diff/2, y) - elif self.align == wxALIGN_RIGHT: + elif self.align == wx.ALIGN_RIGHT: diff = self.GetCellDiff(test_out, self.region) self.DC.DrawText(test_out, self.indent+diff, y) @@ -142,14 +148,14 @@ class PrintBase: vout, remain = self.SetFlow(text, pagew) if self.draw == True and txtdraw == True: test_out = vout - if align == wxALIGN_LEFT: + if align == wx.ALIGN_LEFT: self.DC.DrawText(test_out, indent, y) - elif align == wxALIGN_CENTRE: + elif align == wx.ALIGN_CENTRE: diff = self.GetCellDiff(test_out, pagew) self.DC.DrawText(test_out, indent+diff/2, y) - elif align == wxALIGN_RIGHT: + elif align == wx.ALIGN_RIGHT: diff = self.GetCellDiff(test_out, pagew) self.DC.DrawText(test_out, indent+diff, y) @@ -168,10 +174,9 @@ class PrintBase: return date + ' ' + time def GetNow(self): - full = str(wxDateTime_Now()) # get the current date and time in print format - flds = full.split() - date = flds[0] - time = flds[1] + now = wx.DateTime.Now() + date = now.FormatDate() + time = now.FormatTime() return date, time def SetPreview(self, preview): @@ -195,7 +200,7 @@ class PrintBase: return self.sizeh -class PrintTableDraw(wxScrolledWindow, PrintBase): +class PrintTableDraw(wx.ScrolledWindow, PrintBase): def __init__(self, parent, DC, size): self.parent = parent self.DC = DC @@ -264,10 +269,47 @@ class PrintTableDraw(wxScrolledWindow, 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 @@ -286,18 +328,15 @@ class PrintTableDraw(wxScrolledWindow, 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 except: - align = wxALIGN_LEFT + align = wx.ALIGN_LEFT self.column_align.append(align) try: @@ -310,13 +349,11 @@ class PrintTableDraw(wxScrolledWindow, 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 = wxFont(10, wxSWISS, wxNORMAL, wxNORMAL) # setup using 10 point + f = wx.Font(10, wx.SWISS, wx.NORMAL, wx.NORMAL) # setup using 10 point self.DC.SetFont(f) f.SetFaceName(self.text_font["Name"]) x, y = self.DC.GetTextExtent("W") @@ -429,7 +466,7 @@ class PrintTableDraw(wxScrolledWindow, PrintBase): self.region = self.column[self.col+1] - self.column[self.col] self.indent = self.column[self.col] - self.align = wxALIGN_LEFT + self.align = wx.ALIGN_LEFT max_out = self.OutTextRegion(vtxt, True) if max_out > max_y: @@ -488,10 +525,11 @@ class PrintTableDraw(wxScrolledWindow, PrintBase): def LabelColorRow(self, colour): - brush = wxBrush(colour, wxSOLID) + brush = wx.Brush(colour, wx.SOLID) self.DC.SetBrush(brush) height = self.label_space + self.label_pt_space_before + self.label_pt_space_after - self.DC.DrawRectangle(self.column[0], self.y, self.end_x-self.column[0]+1, height) + self.DC.DrawRectangle(self.column[0], self.y, + self.end_x-self.column[0]+1, height) def ColourRowCells(self, height): if self.draw == False: @@ -503,16 +541,16 @@ class PrintTableDraw(wxScrolledWindow, PrintBase): if cellcolour is not None: colour = cellcolour - brush = wxBrush(colour, wxSOLID) + brush = wx.Brush(colour, wx.SOLID) self.DC.SetBrush(brush) - self.DC.SetPen(wxPen(wxNamedColour('WHITE'), 0)) + self.DC.SetPen(wx.Pen(wx.NamedColour('WHITE'), 0)) start_x = self.column[col] width = self.column[col+1] - start_x + 2 self.DC.DrawRectangle(start_x, self.y, width, height) col = col + 1 - def PrintRow(self, row_val, draw = True, align = wxALIGN_LEFT): + def PrintRow(self, row_val, draw = True, align = wx.ALIGN_LEFT): self.SetPrintFont(self.text_font) self.pt_space_before = self.text_pt_space_before # set the point spacing @@ -521,12 +559,14 @@ class PrintTableDraw(wxScrolledWindow, 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 @@ -549,7 +589,7 @@ class PrintTableDraw(wxScrolledWindow, 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: @@ -565,25 +605,29 @@ class PrintTableDraw(wxScrolledWindow, 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: colour = self.row_def_line_colour - self.DC.SetPen(wxPen(colour, size)) + self.DC.SetPen(wx.Pen(colour, size)) y_out = self.y # y_out = self.y + self.pt_space_before + self.pt_space_after # adjust for extra spacing 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: @@ -591,6 +635,8 @@ class PrintTableDraw(wxScrolledWindow, PrintBase): except: size = self.column_def_line_size + if size < 1: continue + try: colour = self.column_line_colour[col] except: @@ -598,7 +644,7 @@ class PrintTableDraw(wxScrolledWindow, PrintBase): indent = val - self.DC.SetPen(wxPen(colour, size)) + self.DC.SetPen(wx.Pen(colour, size)) self.DC.DrawLine(indent, self.y_start, indent, self.y) col = col + 1 @@ -606,7 +652,7 @@ class PrintTableDraw(wxScrolledWindow, PrintBase): self.DoRefresh() def DoDrawing(self, DC): - size = DC.GetSizeTuple() + size = DC.GetSize() self.DC = DC DC.BeginDrawing() @@ -638,7 +684,7 @@ class PrintTable: self.parentFrame = parentFrame self.SetPreviewSize() - self.printData = wxPrintData() + self.printData = wx.PrintData() self.scale = 1.0 self.SetParms() @@ -652,11 +698,11 @@ class PrintTable: self.SetMargins() self.SetPortrait() - def SetPreviewSize(self, position = wxPoint(0, 0), size="Full"): + def SetPreviewSize(self, position = wx.Point(0, 0), size="Full"): if size == "Full": - r = wxGetClientDisplayRect() - self.preview_frame_size = wxSize(r.width, r.height) - self.preview_frame_pos = wxPoint(r.x, r.y) + r = wx.GetClientDisplayRect() + self.preview_frame_size = r.GetSize() + self.preview_frame_pos = r.GetPosition() else: self.preview_frame_size = size self.preview_frame_pos = position @@ -668,14 +714,14 @@ class PrintTable: self.printData.SetOrientation(orient) def SetColors(self): - self.row_def_line_colour = wxNamedColour('BLACK') + self.row_def_line_colour = wx.NamedColour('BLACK') self.row_def_line_size = 1 - self.column_def_line_colour = wxNamedColour('BLACK') + self.column_def_line_colour = wx.NamedColour('BLACK') self.column_def_line_size = 1 - self.column_colour = wxNamedColour('WHITE') + self.column_colour = wx.NamedColour('WHITE') - self.label_colour = wxNamedColour('LIGHT GREY') + self.label_colour = wx.NamedColour('LIGHT GREY') def SetFonts(self): self.label_font = { "Name": self.default_font_name, "Size": 12, "Colour": [0, 0, 0], "Attr": [0, 0, 0] } @@ -703,20 +749,20 @@ class PrintTable: def SetHeaderValue(self): self.header_margin = 0.25 self.header_font = { "Name": self.default_font_name, "Size": 11, "Colour": [0, 0, 0], "Attr": [0, 0, 0] } - self.header_align = wxALIGN_CENTRE + self.header_align = wx.ALIGN_CENTRE self.header_indent = 0 self.header_type = "Text" def SetFooterValue(self): self.footer_margin = 0.7 self.footer_font = { "Name": self.default_font_name, "Size": 11, "Colour": [0, 0, 0], "Attr": [0, 0, 0] } - self.footer_align = wxALIGN_CENTRE + self.footer_align = wx.ALIGN_CENTRE self.footer_indent = 0 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 @@ -724,13 +770,13 @@ class PrintTable: self.cell_right_margin = 0.1 def SetPortrait(self): - self.printData.SetPaperId(wxPAPER_LETTER) - self.printData.SetOrientation(wxPORTRAIT) + self.printData.SetPaperId(wx.PAPER_LETTER) + self.printData.SetOrientation(wx.PORTRAIT) self.page_width = 8.5 self.page_height = 11.0 def SetLandscape(self): - self.printData.SetOrientation(wxLANDSCAPE) + self.printData.SetOrientation(wx.LANDSCAPE) self.page_width = 11.0 self.page_height = 8.5 @@ -746,7 +792,7 @@ class PrintTable: self.default_font_name = "Arial" self.default_font = { "Name": self.default_font_name, "Size": 10, "Colour": [0, 0, 0], "Attr": [0, 0, 0] } - def SetColAlignment(self, col, align=wxALIGN_LEFT): + def SetColAlignment(self, col, align=wx.ALIGN_LEFT): self.set_column_align[col] = align def SetColBackgroundColour(self, col, colour): @@ -864,16 +910,16 @@ class PrintTable: self.footer.append(set) def Preview(self): + data = wx.PrintDialogData(self.printData) printout = SetPrintout(self) printout2 = SetPrintout(self) - self.preview = wxPrintPreview(printout, printout2, self.printData) + self.preview = wx.PrintPreview(printout, printout2, data) if not self.preview.Ok(): - wxMessageBox("There was a problem printing!", "Printing", wxOK) + wx.MessageBox("There was a problem printing!", "Printing", wx.OK) return self.preview.SetZoom(60) # initial zoom value - - frame = wxPreviewFrame(self.preview, self.parentFrame, "Print preview") + frame = wx.PreviewFrame(self.preview, self.parentFrame, "Print preview") frame.Initialize() if self.parentFrame: @@ -882,18 +928,17 @@ class PrintTable: frame.Show(True) def Print(self): - pdd = wxPrintDialogData() - pdd.SetPrintData(self.printData) - printer = wxPrinter(pdd) + pdd = wx.PrintDialogData(self.printData) + printer = wx.Printer(pdd) printout = SetPrintout(self) if not printer.Print(self.parentFrame, printout): - wxMessageBox("There was a problem printing.\nPerhaps your current printer is not set correctly?", "Printing", wxOK) + 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): - size = DC.GetSizeTuple() + size = DC.GetSize() DC.BeginDrawing() table = PrintTableDraw(self, DC, size) @@ -1008,17 +1053,17 @@ class PrintGrid: self.table.Print() -class SetPrintout(wxPrintout): +class SetPrintout(wx.Printout): def __init__(self, canvas): - wxPrintout.__init__(self) + wx.Printout.__init__(self) self.canvas = canvas 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: @@ -1038,7 +1083,7 @@ class SetPrintout(wxPrintout): 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() @@ -1049,12 +1094,12 @@ class SetPrintout(wxPrintout): else: self.pixelsPerInch = self.GetPPIPrinter() - (w, h) = dc.GetSizeTuple() + (w, h) = dc.GetSize() scaleX = float(w) / 1000 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() @@ -1066,7 +1111,7 @@ class SetPrintout(wxPrintout): def OnPrintPage(self, page): dc = self.GetDC() - (w, h) = dc.GetSizeTuple() + (w, h) = dc.GetSize() scaleX = float(w) / 1000 scaleY = float(h) / 1000 self.printUserScale = min(scaleX, scaleY) @@ -1086,8 +1131,23 @@ class SetPrintout(wxPrintout): 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()