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