disp_print.py 4.6 KB

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