1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
|
"""!
@package mapdisp.gprint
@brief Print context and utility functions for printing
contents of map display window.
Classes:
- gprint::MapPrint
- gprint::PrintOptions
(C) 2007-2011 by the GRASS Development Team
This program is free software under the GNU General Public License
(>=v2). Read the file COPYING that comes with GRASS for details.
@author Michael Barton (Arizona State University)
"""
import wx
from core.gcmd import GMessage
class MapPrint(wx.Printout):
def __init__(self, canvas):
wx.Printout.__init__(self)
self.canvas = canvas
def OnBeginDocument(self, start, end):
return super(MapPrint, self).OnBeginDocument(start, end)
def OnEndDocument(self):
super(MapPrint, self).OnEndDocument()
def OnBeginPrinting(self):
super(MapPrint, self).OnBeginPrinting()
def OnEndPrinting(self):
super(MapPrint, self).OnEndPrinting()
def OnPreparePrinting(self):
super(MapPrint, self).OnPreparePrinting()
def HasPage(self, page):
if page <= 2:
return True
else:
return False
def GetPageInfo(self):
return (1, 2, 1, 2)
def OnPrintPage(self, page):
dc = self.GetDC()
#-------------------------------------------
# One possible method of setting scaling factors...
maxX, maxY = self.canvas.GetSize()
# Let's have at least 50 device units margin
marginX = 10
marginY = 10
# Add the margin to the graphic size
maxX = maxX + (2 * marginX)
maxY = maxY + (2 * marginY)
# Get the size of the DC in pixels
(w, h) = dc.GetSizeTuple()
# Calculate a suitable scaling factor
scaleX = float(w) / maxX
scaleY = float(h) / maxY
# Use x or y scaling factor, whichever fits on the DC
actualScale = min(scaleX, scaleY)
# Calculate the position on the DC for centering the graphic
posX = (w - (self.canvas.GetSize()[0] * actualScale)) / 2.0
posY = (h - (self.canvas.GetSize()[1] * actualScale)) / 2.0
# Set the scale and origin
dc.SetUserScale(actualScale, actualScale)
dc.SetDeviceOrigin(int(posX), int(posY))
#-------------------------------------------
self.canvas.pdc.DrawToDC(dc)
# prints a page number on the page
# dc.DrawText("Page: %d" % page, marginX/2, maxY-marginY)
return True
class PrintOptions:
def __init__(self, parent, mapwin):
self.mapframe = parent
self.mapwin = mapwin
#self.frame = frame
self.printData = None
#self.canvas = ScrolledWindow.MyCanvas(self)
def setup(self):
if self.printData:
return
self.printData = wx.PrintData()
self.printData.SetPaperId(wx.PAPER_LETTER)
self.printData.SetPrintMode(wx.PRINT_MODE_PRINTER)
def OnPageSetup(self, event):
self.setup()
psdd = wx.PageSetupDialogData(self.printData)
psdd.CalculatePaperSizeFromId()
dlg = wx.PageSetupDialog(self.mapwin, psdd)
dlg.ShowModal()
# this makes a copy of the wx.PrintData instead of just saving
# a reference to the one inside the PrintDialogData that will
# be destroyed when the dialog is destroyed
self.printData = wx.PrintData( dlg.GetPageSetupData().GetPrintData() )
dlg.Destroy()
def OnPrintPreview(self, event):
self.setup()
data = wx.PrintDialogData(self.printData)
printout = MapPrint(self.mapwin)
printout2 = MapPrint(self.mapwin)
self.preview = wx.PrintPreview(printout, printout2, data)
if not self.preview.Ok():
wx.MessageBox("There was a problem printing this display\n", wx.OK)
return
pfrm = wx.PreviewFrame(self.preview, self.mapframe, "Print preview")
pfrm.Initialize()
pfrm.SetPosition(self.mapframe.GetPosition())
pfrm.SetSize(self.mapframe.GetClientSize())
pfrm.Show(True)
def OnDoPrint(self, event):
self.setup()
pdd = wx.PrintDialogData(self.printData)
# set number of pages/copies
pdd.SetToPage(1)
printer = wx.Printer(pdd)
printout = MapPrint(self.mapwin)
if not printer.Print(self.mapframe, printout, True):
wx.MessageBox("There was a problem printing.\nPerhaps your current printer is not set correctly?", "Printing", wx.OK)
else:
self.printData = wx.PrintData( printer.GetPrintDialogData().GetPrintData() )
printout.Destroy()
class MapPrint(wx.Printout):
def __init__(self, canvas):
wx.Printout.__init__(self)
self.canvas = canvas
def OnBeginDocument(self, start, end):
return super(MapPrint, self).OnBeginDocument(start, end)
def OnEndDocument(self):
super(MapPrint, self).OnEndDocument()
def OnBeginPrinting(self):
super(MapPrint, self).OnBeginPrinting()
def OnEndPrinting(self):
super(MapPrint, self).OnEndPrinting()
def OnPreparePrinting(self):
super(MapPrint, self).OnPreparePrinting()
def HasPage(self, page):
if page <= 2:
return True
else:
return False
def GetPageInfo(self):
return (1, 2, 1, 2)
def OnPrintPage(self, page):
dc = self.GetDC()
#-------------------------------------------
# One possible method of setting scaling factors...
maxX, maxY = self.canvas.GetSize()
# Let's have at least 50 device units margin
marginX = 10
marginY = 10
# Add the margin to the graphic size
maxX = maxX + (2 * marginX)
maxY = maxY + (2 * marginY)
# Get the size of the DC in pixels
(w, h) = dc.GetSizeTuple()
# Calculate a suitable scaling factor
scaleX = float(w) / maxX
scaleY = float(h) / maxY
# Use x or y scaling factor, whichever fits on the DC
actualScale = min(scaleX, scaleY)
# Calculate the position on the DC for centering the graphic
posX = (w - (self.canvas.GetSize()[0] * actualScale)) / 2.0
posY = (h - (self.canvas.GetSize()[1] * actualScale)) / 2.0
# Set the scale and origin
dc.SetUserScale(actualScale, actualScale)
dc.SetDeviceOrigin(int(posX), int(posY))
#-------------------------------------------
self.canvas.pdc.DrawToDC(dc)
# prints a page number on the page
# dc.DrawText("Page: %d" % page, marginX/2, maxY-marginY)
return True
class PrintOptions(wx.Object):
def __init__(self, parent, mapwin):
self.mapframe = parent
self.mapwin = mapwin
#self.frame = frame
self.printData = None
#self.canvas = ScrolledWindow.MyCanvas(self)
def setup(self):
if self.printData:
return
self.printData = wx.PrintData()
self.printData.SetPaperId(wx.PAPER_LETTER)
self.printData.SetPrintMode(wx.PRINT_MODE_PRINTER)
def OnPageSetup(self, event):
self.setup()
psdd = wx.PageSetupDialogData(self.printData)
psdd.CalculatePaperSizeFromId()
dlg = wx.PageSetupDialog(self.mapwin, psdd)
dlg.ShowModal()
# this makes a copy of the wx.PrintData instead of just saving
# a reference to the one inside the PrintDialogData that will
# be destroyed when the dialog is destroyed
self.printData = wx.PrintData( dlg.GetPageSetupData().GetPrintData() )
dlg.Destroy()
def OnPrintPreview(self, event):
self.setup()
data = wx.PrintDialogData(self.printData)
printout = MapPrint(self.mapwin)
printout2 = MapPrint(self.mapwin)
self.preview = wx.PrintPreview(printout, printout2, data)
if not self.preview.Ok():
wx.MessageBox("There was a problem printing this display\n", wx.OK)
return
pfrm = wx.PreviewFrame(self.preview, self.mapframe, "Print preview")
pfrm.Initialize()
pfrm.SetPosition(self.mapframe.GetPosition())
pfrm.SetSize(self.mapframe.GetClientSize())
pfrm.Show(True)
def OnDoPrint(self, event):
self.setup()
pdd = wx.PrintDialogData(self.printData)
# set number of pages/copies
pdd.SetToPage(1)
printer = wx.Printer(pdd)
printout = MapPrint(self.mapwin)
if not printer.Print(self.mapframe, printout, True):
GMessage(_("There was a problem printing.\n"
"Perhaps your current printer is not set correctly?"))
else:
self.printData = wx.PrintData( printer.GetPrintDialogData().GetPrintData() )
printout.Destroy()
|