GRASS Programmer's Manual  6.5.svn(2012)-r51648
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines
gprint.py
Go to the documentation of this file.
00001 """!
00002 @package mapdisp.gprint
00003 
00004 @brief Print context and utility functions for printing
00005 contents of map display window.
00006 
00007 Classes:
00008  - gprint::MapPrint
00009  - gprint::PrintOptions
00010 
00011 (C) 2007-2011 by the GRASS Development Team
00012 
00013 This program is free software under the GNU General Public License
00014 (>=v2). Read the file COPYING that comes with GRASS for details.
00015 
00016 @author Michael Barton (Arizona State University)
00017 """
00018 
00019 import  wx
00020 
00021 from core.gcmd import GMessage
00022 
00023 class MapPrint(wx.Printout):
00024     def __init__(self, canvas):
00025         wx.Printout.__init__(self)
00026         self.canvas = canvas
00027 
00028     def OnBeginDocument(self, start, end):
00029         return super(MapPrint, self).OnBeginDocument(start, end)
00030 
00031     def OnEndDocument(self):
00032         super(MapPrint, self).OnEndDocument()
00033 
00034     def OnBeginPrinting(self):
00035         super(MapPrint, self).OnBeginPrinting()
00036 
00037     def OnEndPrinting(self):
00038         super(MapPrint, self).OnEndPrinting()
00039 
00040     def OnPreparePrinting(self):
00041         super(MapPrint, self).OnPreparePrinting()
00042 
00043     def HasPage(self, page):
00044         if page <= 2:
00045             return True
00046         else:
00047             return False
00048 
00049     def GetPageInfo(self):
00050         return (1, 2, 1, 2)
00051 
00052     def OnPrintPage(self, page):
00053         dc = self.GetDC()
00054 
00055         #-------------------------------------------
00056         # One possible method of setting scaling factors...
00057         maxX, maxY = self.canvas.GetSize()
00058 
00059         # Let's have at least 50 device units margin
00060         marginX = 10
00061         marginY = 10
00062 
00063         # Add the margin to the graphic size
00064         maxX = maxX + (2 * marginX)
00065         maxY = maxY + (2 * marginY)
00066 
00067         # Get the size of the DC in pixels
00068         (w, h) = dc.GetSizeTuple()
00069 
00070         # Calculate a suitable scaling factor
00071         scaleX = float(w) / maxX
00072         scaleY = float(h) / maxY
00073 
00074         # Use x or y scaling factor, whichever fits on the DC
00075         actualScale = min(scaleX, scaleY)
00076 
00077         # Calculate the position on the DC for centering the graphic
00078         posX = (w - (self.canvas.GetSize()[0] * actualScale)) / 2.0
00079         posY = (h - (self.canvas.GetSize()[1] * actualScale)) / 2.0
00080 
00081         # Set the scale and origin
00082         dc.SetUserScale(actualScale, actualScale)
00083         dc.SetDeviceOrigin(int(posX), int(posY))
00084 
00085         #-------------------------------------------
00086 
00087         self.canvas.pdc.DrawToDC(dc)
00088 
00089         # prints a page number on the page
00090 #        dc.DrawText("Page: %d" % page, marginX/2, maxY-marginY)
00091 
00092         return True
00093 
00094 class PrintOptions:
00095     def __init__(self, parent, mapwin):
00096         self.mapframe = parent
00097         self.mapwin = mapwin
00098         #self.frame = frame
00099 
00100         self.printData = None
00101 
00102         #self.canvas = ScrolledWindow.MyCanvas(self)
00103 
00104     def setup(self):
00105         if self.printData:
00106             return
00107         self.printData = wx.PrintData()
00108         self.printData.SetPaperId(wx.PAPER_LETTER)
00109         self.printData.SetPrintMode(wx.PRINT_MODE_PRINTER)
00110 
00111     def OnPageSetup(self, event):
00112         self.setup()
00113         psdd = wx.PageSetupDialogData(self.printData)
00114         psdd.CalculatePaperSizeFromId()
00115         dlg = wx.PageSetupDialog(self.mapwin, psdd)
00116         dlg.ShowModal()
00117 
00118         # this makes a copy of the wx.PrintData instead of just saving
00119         # a reference to the one inside the PrintDialogData that will
00120         # be destroyed when the dialog is destroyed
00121         self.printData = wx.PrintData( dlg.GetPageSetupData().GetPrintData() )
00122 
00123         dlg.Destroy()
00124 
00125     def OnPrintPreview(self, event):
00126         self.setup()
00127         data = wx.PrintDialogData(self.printData)
00128         printout = MapPrint(self.mapwin)
00129         printout2 = MapPrint(self.mapwin)
00130         self.preview = wx.PrintPreview(printout, printout2, data)
00131 
00132         if not self.preview.Ok():
00133             wx.MessageBox("There was a problem printing this display\n", wx.OK)
00134             return
00135 
00136         pfrm = wx.PreviewFrame(self.preview, self.mapframe, "Print preview")
00137 
00138         pfrm.Initialize()
00139         pfrm.SetPosition(self.mapframe.GetPosition())
00140         pfrm.SetSize(self.mapframe.GetClientSize())
00141         pfrm.Show(True)
00142 
00143     def OnDoPrint(self, event):
00144         self.setup()
00145         pdd = wx.PrintDialogData(self.printData)
00146         # set number of pages/copies
00147         pdd.SetToPage(1)
00148         printer = wx.Printer(pdd)
00149         printout = MapPrint(self.mapwin)
00150 
00151         if not printer.Print(self.mapframe, printout, True):
00152             wx.MessageBox("There was a problem printing.\nPerhaps your current printer is not set correctly?", "Printing", wx.OK)
00153         else:
00154             self.printData = wx.PrintData( printer.GetPrintDialogData().GetPrintData() )
00155         printout.Destroy()
00156 class MapPrint(wx.Printout):
00157     def __init__(self, canvas):
00158         wx.Printout.__init__(self)
00159         self.canvas = canvas
00160 
00161     def OnBeginDocument(self, start, end):
00162         return super(MapPrint, self).OnBeginDocument(start, end)
00163 
00164     def OnEndDocument(self):
00165         super(MapPrint, self).OnEndDocument()
00166 
00167     def OnBeginPrinting(self):
00168         super(MapPrint, self).OnBeginPrinting()
00169 
00170     def OnEndPrinting(self):
00171         super(MapPrint, self).OnEndPrinting()
00172 
00173     def OnPreparePrinting(self):
00174         super(MapPrint, self).OnPreparePrinting()
00175 
00176     def HasPage(self, page):
00177         if page <= 2:
00178             return True
00179         else:
00180             return False
00181 
00182     def GetPageInfo(self):
00183         return (1, 2, 1, 2)
00184 
00185     def OnPrintPage(self, page):
00186         dc = self.GetDC()
00187 
00188         #-------------------------------------------
00189         # One possible method of setting scaling factors...
00190         maxX, maxY = self.canvas.GetSize()
00191 
00192         # Let's have at least 50 device units margin
00193         marginX = 10
00194         marginY = 10
00195 
00196         # Add the margin to the graphic size
00197         maxX = maxX + (2 * marginX)
00198         maxY = maxY + (2 * marginY)
00199 
00200         # Get the size of the DC in pixels
00201         (w, h) = dc.GetSizeTuple()
00202 
00203         # Calculate a suitable scaling factor
00204         scaleX = float(w) / maxX
00205         scaleY = float(h) / maxY
00206 
00207         # Use x or y scaling factor, whichever fits on the DC
00208         actualScale = min(scaleX, scaleY)
00209 
00210         # Calculate the position on the DC for centering the graphic
00211         posX = (w - (self.canvas.GetSize()[0] * actualScale)) / 2.0
00212         posY = (h - (self.canvas.GetSize()[1] * actualScale)) / 2.0
00213 
00214         # Set the scale and origin
00215         dc.SetUserScale(actualScale, actualScale)
00216         dc.SetDeviceOrigin(int(posX), int(posY))
00217 
00218         #-------------------------------------------
00219 
00220         self.canvas.pdc.DrawToDC(dc)
00221 
00222         # prints a page number on the page
00223         # dc.DrawText("Page: %d" % page, marginX/2, maxY-marginY)
00224 
00225         return True
00226 
00227 class PrintOptions(wx.Object):
00228     def __init__(self, parent, mapwin):
00229         self.mapframe = parent
00230         self.mapwin = mapwin
00231         #self.frame = frame
00232 
00233         self.printData = None
00234 
00235         #self.canvas = ScrolledWindow.MyCanvas(self)
00236 
00237     def setup(self):
00238         if self.printData:
00239             return
00240         self.printData = wx.PrintData()
00241         self.printData.SetPaperId(wx.PAPER_LETTER)
00242         self.printData.SetPrintMode(wx.PRINT_MODE_PRINTER)
00243 
00244     def OnPageSetup(self, event):
00245         self.setup()
00246         psdd = wx.PageSetupDialogData(self.printData)
00247         psdd.CalculatePaperSizeFromId()
00248         dlg = wx.PageSetupDialog(self.mapwin, psdd)
00249         dlg.ShowModal()
00250 
00251         # this makes a copy of the wx.PrintData instead of just saving
00252         # a reference to the one inside the PrintDialogData that will
00253         # be destroyed when the dialog is destroyed
00254         self.printData = wx.PrintData( dlg.GetPageSetupData().GetPrintData() )
00255 
00256         dlg.Destroy()
00257 
00258     def OnPrintPreview(self, event):
00259         self.setup()
00260         data = wx.PrintDialogData(self.printData)
00261         printout = MapPrint(self.mapwin)
00262         printout2 = MapPrint(self.mapwin)
00263         self.preview = wx.PrintPreview(printout, printout2, data)
00264 
00265         if not self.preview.Ok():
00266             wx.MessageBox("There was a problem printing this display\n", wx.OK)
00267             return
00268 
00269         pfrm = wx.PreviewFrame(self.preview, self.mapframe, "Print preview")
00270 
00271         pfrm.Initialize()
00272         pfrm.SetPosition(self.mapframe.GetPosition())
00273         pfrm.SetSize(self.mapframe.GetClientSize())
00274         pfrm.Show(True)
00275 
00276     def OnDoPrint(self, event):
00277         self.setup()
00278         pdd = wx.PrintDialogData(self.printData)
00279         # set number of pages/copies
00280         pdd.SetToPage(1)
00281         printer = wx.Printer(pdd)
00282         printout = MapPrint(self.mapwin)
00283 
00284         if not printer.Print(self.mapframe, printout, True):
00285             GMessage(_("There was a problem printing.\n"
00286                        "Perhaps your current printer is not set correctly?"))
00287         else:
00288             self.printData = wx.PrintData( printer.GetPrintDialogData().GetPrintData() )
00289         printout.Destroy()