GRASS Programmer's Manual
6.4.2(2012)
|
00001 """ 00002 MODULE: disp_print.py 00003 00004 CLASSES: 00005 * MapPrint 00006 * PrintOptions 00007 00008 PURPOSE: Print context and utility functions for printing 00009 contents of map display window. 00010 00011 AUTHORS: The GRASS Development Team 00012 Michael Barton (Arizona State University) 00013 Based on generic example code from wxPython 00014 demo program by Robin Dunn 00015 00016 COPYRIGHT: (C) 2007 by the GRASS Development Team 00017 This program is free software under the GNU General Public 00018 License (>=v2). Read the file COPYING that comes with GRASS 00019 for details. 00020 00021 """ 00022 00023 import wx 00024 00025 00026 class MapPrint(wx.Printout): 00027 def __init__(self, canvas): 00028 wx.Printout.__init__(self) 00029 self.canvas = canvas 00030 00031 def OnBeginDocument(self, start, end): 00032 return super(MapPrint, self).OnBeginDocument(start, end) 00033 00034 def OnEndDocument(self): 00035 super(MapPrint, self).OnEndDocument() 00036 00037 def OnBeginPrinting(self): 00038 super(MapPrint, self).OnBeginPrinting() 00039 00040 def OnEndPrinting(self): 00041 super(MapPrint, self).OnEndPrinting() 00042 00043 def OnPreparePrinting(self): 00044 super(MapPrint, self).OnPreparePrinting() 00045 00046 def HasPage(self, page): 00047 if page <= 2: 00048 return True 00049 else: 00050 return False 00051 00052 def GetPageInfo(self): 00053 return (1, 2, 1, 2) 00054 00055 def OnPrintPage(self, page): 00056 dc = self.GetDC() 00057 00058 #------------------------------------------- 00059 # One possible method of setting scaling factors... 00060 maxX, maxY = self.canvas.GetSize() 00061 00062 # Let's have at least 50 device units margin 00063 marginX = 10 00064 marginY = 10 00065 00066 # Add the margin to the graphic size 00067 maxX = maxX + (2 * marginX) 00068 maxY = maxY + (2 * marginY) 00069 00070 # Get the size of the DC in pixels 00071 (w, h) = dc.GetSizeTuple() 00072 00073 # Calculate a suitable scaling factor 00074 scaleX = float(w) / maxX 00075 scaleY = float(h) / maxY 00076 00077 # Use x or y scaling factor, whichever fits on the DC 00078 actualScale = min(scaleX, scaleY) 00079 00080 # Calculate the position on the DC for centering the graphic 00081 posX = (w - (self.canvas.GetSize()[0] * actualScale)) / 2.0 00082 posY = (h - (self.canvas.GetSize()[1] * actualScale)) / 2.0 00083 00084 # Set the scale and origin 00085 dc.SetUserScale(actualScale, actualScale) 00086 dc.SetDeviceOrigin(int(posX), int(posY)) 00087 00088 #------------------------------------------- 00089 00090 self.canvas.pdc.DrawToDC(dc) 00091 00092 # prints a page number on the page 00093 # dc.DrawText("Page: %d" % page, marginX/2, maxY-marginY) 00094 00095 return True 00096 00097 class PrintOptions: 00098 def __init__(self, parent, mapwin): 00099 self.mapframe = parent 00100 self.mapwin = mapwin 00101 #self.frame = frame 00102 00103 self.printData = None 00104 00105 #self.canvas = ScrolledWindow.MyCanvas(self) 00106 00107 def setup(self): 00108 if self.printData: 00109 return 00110 self.printData = wx.PrintData() 00111 self.printData.SetPaperId(wx.PAPER_LETTER) 00112 self.printData.SetPrintMode(wx.PRINT_MODE_PRINTER) 00113 00114 def OnPageSetup(self, event): 00115 self.setup() 00116 psdd = wx.PageSetupDialogData(self.printData) 00117 psdd.CalculatePaperSizeFromId() 00118 dlg = wx.PageSetupDialog(self.mapwin, psdd) 00119 dlg.ShowModal() 00120 00121 # this makes a copy of the wx.PrintData instead of just saving 00122 # a reference to the one inside the PrintDialogData that will 00123 # be destroyed when the dialog is destroyed 00124 self.printData = wx.PrintData( dlg.GetPageSetupData().GetPrintData() ) 00125 00126 dlg.Destroy() 00127 00128 def OnPrintPreview(self, event): 00129 self.setup() 00130 data = wx.PrintDialogData(self.printData) 00131 printout = MapPrint(self.mapwin) 00132 printout2 = MapPrint(self.mapwin) 00133 self.preview = wx.PrintPreview(printout, printout2, data) 00134 00135 if not self.preview.Ok(): 00136 wx.MessageBox("There was a problem printing this display\n", wx.OK) 00137 return 00138 00139 pfrm = wx.PreviewFrame(self.preview, self.mapframe, "Print preview") 00140 00141 pfrm.Initialize() 00142 pfrm.SetPosition(self.mapframe.GetPosition()) 00143 pfrm.SetSize(self.mapframe.GetClientSize()) 00144 pfrm.Show(True) 00145 00146 def OnDoPrint(self, event): 00147 self.setup() 00148 pdd = wx.PrintDialogData(self.printData) 00149 # set number of pages/copies 00150 pdd.SetToPage(1) 00151 printer = wx.Printer(pdd) 00152 printout = MapPrint(self.mapwin) 00153 00154 if not printer.Print(self.mapframe, printout, True): 00155 wx.MessageBox("There was a problem printing.\nPerhaps your current printer is not set correctly?", "Printing", wx.OK) 00156 else: 00157 self.printData = wx.PrintData( printer.GetPrintDialogData().GetPrintData() ) 00158 printout.Destroy()