gprint.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. """
  2. @package mapdisp.gprint
  3. @brief Print context and utility functions for printing
  4. contents of map display window.
  5. Classes:
  6. - gprint::MapPrint
  7. - gprint::PrintOptions
  8. (C) 2007-2011 by the GRASS Development Team
  9. This program is free software under the GNU General Public License
  10. (>=v2). Read the file COPYING that comes with GRASS for details.
  11. @author Michael Barton (Arizona State University)
  12. """
  13. import wx
  14. from core.gcmd import GMessage
  15. from core.utils import _
  16. class MapPrint(wx.Printout):
  17. def __init__(self, canvas):
  18. wx.Printout.__init__(self)
  19. self.canvas = canvas
  20. def OnBeginDocument(self, start, end):
  21. return super(MapPrint, self).OnBeginDocument(start, end)
  22. def OnEndDocument(self):
  23. super(MapPrint, self).OnEndDocument()
  24. def OnBeginPrinting(self):
  25. super(MapPrint, self).OnBeginPrinting()
  26. def OnEndPrinting(self):
  27. super(MapPrint, self).OnEndPrinting()
  28. def OnPreparePrinting(self):
  29. super(MapPrint, self).OnPreparePrinting()
  30. def HasPage(self, page):
  31. if page <= 2:
  32. return True
  33. else:
  34. return False
  35. def GetPageInfo(self):
  36. return (1, 2, 1, 2)
  37. def OnPrintPage(self, page):
  38. dc = self.GetDC()
  39. #-------------------------------------------
  40. # One possible method of setting scaling factors...
  41. maxX, maxY = self.canvas.GetSize()
  42. # Let's have at least 50 device units margin
  43. marginX = 10
  44. marginY = 10
  45. # Add the margin to the graphic size
  46. maxX = maxX + (2 * marginX)
  47. maxY = maxY + (2 * marginY)
  48. # Get the size of the DC in pixels
  49. (w, h) = dc.GetSizeTuple()
  50. # Calculate a suitable scaling factor
  51. scaleX = float(w) / maxX
  52. scaleY = float(h) / maxY
  53. # Use x or y scaling factor, whichever fits on the DC
  54. actualScale = min(scaleX, scaleY)
  55. # Calculate the position on the DC for centering the graphic
  56. posX = (w - (self.canvas.GetSize()[0] * actualScale)) / 2.0
  57. posY = (h - (self.canvas.GetSize()[1] * actualScale)) / 2.0
  58. # Set the scale and origin
  59. dc.SetUserScale(actualScale, actualScale)
  60. dc.SetDeviceOrigin(int(posX), int(posY))
  61. #-------------------------------------------
  62. self.canvas.pdc.DrawToDC(dc)
  63. # prints a page number on the page
  64. # dc.DrawText("Page: %d" % page, marginX/2, maxY-marginY)
  65. return True
  66. class PrintOptions(wx.Object):
  67. def __init__(self, parent, mapwin):
  68. self.mapframe = parent
  69. self.mapwin = mapwin
  70. self.printData = None
  71. def setup(self):
  72. if self.printData:
  73. return
  74. self.printData = wx.PrintData()
  75. self.printData.SetPaperId(wx.PAPER_LETTER)
  76. self.printData.SetPrintMode(wx.PRINT_MODE_PRINTER)
  77. def OnPageSetup(self, event):
  78. self.setup()
  79. psdd = wx.PageSetupDialogData(self.printData)
  80. psdd.CalculatePaperSizeFromId()
  81. dlg = wx.PageSetupDialog(self.mapwin, psdd)
  82. dlg.ShowModal()
  83. # this makes a copy of the wx.PrintData instead of just saving
  84. # a reference to the one inside the PrintDialogData that will
  85. # be destroyed when the dialog is destroyed
  86. self.printData = wx.PrintData( dlg.GetPageSetupData().GetPrintData() )
  87. dlg.Destroy()
  88. def OnPrintPreview(self, event):
  89. self.setup()
  90. data = wx.PrintDialogData(self.printData)
  91. printout = MapPrint(self.mapwin)
  92. printout2 = MapPrint(self.mapwin)
  93. self.preview = wx.PrintPreview(printout, printout2, data)
  94. if not self.preview.Ok():
  95. wx.MessageBox("There was a problem printing this display\n", wx.OK)
  96. return
  97. pfrm = wx.PreviewFrame(self.preview, self.mapframe, "Print preview")
  98. pfrm.Initialize()
  99. pfrm.SetPosition(self.mapframe.GetPosition())
  100. pfrm.SetSize(self.mapframe.GetClientSize())
  101. pfrm.Show(True)
  102. def OnDoPrint(self, event):
  103. self.setup()
  104. pdd = wx.PrintDialogData(self.printData)
  105. # set number of pages/copies
  106. pdd.SetToPage(1)
  107. printer = wx.Printer(pdd)
  108. printout = MapPrint(self.mapwin)
  109. if not printer.Print(self.mapframe, printout, True):
  110. GMessage(_("There was a problem printing.\n"
  111. "Perhaps your current printer is not set correctly?"))
  112. else:
  113. self.printData = wx.PrintData( printer.GetPrintDialogData().GetPrintData() )
  114. printout.Destroy()