gprint.py 4.5 KB

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