gprint.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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.frame = frame
  70. self.printData = None
  71. #self.canvas = ScrolledWindow.MyCanvas(self)
  72. def setup(self):
  73. if self.printData:
  74. return
  75. self.printData = wx.PrintData()
  76. self.printData.SetPaperId(wx.PAPER_LETTER)
  77. self.printData.SetPrintMode(wx.PRINT_MODE_PRINTER)
  78. def OnPageSetup(self, event):
  79. self.setup()
  80. psdd = wx.PageSetupDialogData(self.printData)
  81. psdd.CalculatePaperSizeFromId()
  82. dlg = wx.PageSetupDialog(self.mapwin, psdd)
  83. dlg.ShowModal()
  84. # this makes a copy of the wx.PrintData instead of just saving
  85. # a reference to the one inside the PrintDialogData that will
  86. # be destroyed when the dialog is destroyed
  87. self.printData = wx.PrintData( dlg.GetPageSetupData().GetPrintData() )
  88. dlg.Destroy()
  89. def OnPrintPreview(self, event):
  90. self.setup()
  91. data = wx.PrintDialogData(self.printData)
  92. printout = MapPrint(self.mapwin)
  93. printout2 = MapPrint(self.mapwin)
  94. self.preview = wx.PrintPreview(printout, printout2, data)
  95. if not self.preview.Ok():
  96. wx.MessageBox("There was a problem printing this display\n", wx.OK)
  97. return
  98. pfrm = wx.PreviewFrame(self.preview, self.mapframe, "Print preview")
  99. pfrm.Initialize()
  100. pfrm.SetPosition(self.mapframe.GetPosition())
  101. pfrm.SetSize(self.mapframe.GetClientSize())
  102. pfrm.Show(True)
  103. def OnDoPrint(self, event):
  104. self.setup()
  105. pdd = wx.PrintDialogData(self.printData)
  106. # set number of pages/copies
  107. pdd.SetToPage(1)
  108. printer = wx.Printer(pdd)
  109. printout = MapPrint(self.mapwin)
  110. if not printer.Print(self.mapframe, printout, True):
  111. GMessage(_("There was a problem printing.\n"
  112. "Perhaps your current printer is not set correctly?"))
  113. else:
  114. self.printData = wx.PrintData( printer.GetPrintDialogData().GetPrintData() )
  115. printout.Destroy()