GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
gprint.py
Go to the documentation of this file.
1 """!
2 @package mapdisp.gprint
3 
4 @brief Print context and utility functions for printing
5 contents of map display window.
6 
7 Classes:
8  - gprint::MapPrint
9  - gprint::PrintOptions
10 
11 (C) 2007-2011 by the GRASS Development Team
12 
13 This program is free software under the GNU General Public License
14 (>=v2). Read the file COPYING that comes with GRASS for details.
15 
16 @author Michael Barton (Arizona State University)
17 """
18 
19 import wx
20 
21 from core.gcmd import GMessage
22 
23 class MapPrint(wx.Printout):
24  def __init__(self, canvas):
25  wx.Printout.__init__(self)
26  self.canvas = canvas
27 
28  def OnBeginDocument(self, start, end):
29  return super(MapPrint, self).OnBeginDocument(start, end)
30 
31  def OnEndDocument(self):
32  super(MapPrint, self).OnEndDocument()
33 
34  def OnBeginPrinting(self):
35  super(MapPrint, self).OnBeginPrinting()
36 
37  def OnEndPrinting(self):
38  super(MapPrint, self).OnEndPrinting()
39 
40  def OnPreparePrinting(self):
41  super(MapPrint, self).OnPreparePrinting()
42 
43  def HasPage(self, page):
44  if page <= 2:
45  return True
46  else:
47  return False
48 
49  def GetPageInfo(self):
50  return (1, 2, 1, 2)
51 
52  def OnPrintPage(self, page):
53  dc = self.GetDC()
54 
55  #-------------------------------------------
56  # One possible method of setting scaling factors...
57  maxX, maxY = self.canvas.GetSize()
58 
59  # Let's have at least 50 device units margin
60  marginX = 10
61  marginY = 10
62 
63  # Add the margin to the graphic size
64  maxX = maxX + (2 * marginX)
65  maxY = maxY + (2 * marginY)
66 
67  # Get the size of the DC in pixels
68  (w, h) = dc.GetSizeTuple()
69 
70  # Calculate a suitable scaling factor
71  scaleX = float(w) / maxX
72  scaleY = float(h) / maxY
73 
74  # Use x or y scaling factor, whichever fits on the DC
75  actualScale = min(scaleX, scaleY)
76 
77  # Calculate the position on the DC for centering the graphic
78  posX = (w - (self.canvas.GetSize()[0] * actualScale)) / 2.0
79  posY = (h - (self.canvas.GetSize()[1] * actualScale)) / 2.0
80 
81  # Set the scale and origin
82  dc.SetUserScale(actualScale, actualScale)
83  dc.SetDeviceOrigin(int(posX), int(posY))
84 
85  #-------------------------------------------
86 
87  self.canvas.pdc.DrawToDC(dc)
88 
89  # prints a page number on the page
90 # dc.DrawText("Page: %d" % page, marginX/2, maxY-marginY)
91 
92  return True
93 
95  def __init__(self, parent, mapwin):
96  self.mapframe = parent
97  self.mapwin = mapwin
98  #self.frame = frame
99 
100  self.printData = None
101 
102  #self.canvas = ScrolledWindow.MyCanvas(self)
103 
104  def setup(self):
105  if self.printData:
106  return
107  self.printData = wx.PrintData()
108  self.printData.SetPaperId(wx.PAPER_LETTER)
109  self.printData.SetPrintMode(wx.PRINT_MODE_PRINTER)
110 
111  def OnPageSetup(self, event):
112  self.setup()
113  psdd = wx.PageSetupDialogData(self.printData)
114  psdd.CalculatePaperSizeFromId()
115  dlg = wx.PageSetupDialog(self.mapwin, psdd)
116  dlg.ShowModal()
117 
118  # this makes a copy of the wx.PrintData instead of just saving
119  # a reference to the one inside the PrintDialogData that will
120  # be destroyed when the dialog is destroyed
121  self.printData = wx.PrintData( dlg.GetPageSetupData().GetPrintData() )
122 
123  dlg.Destroy()
124 
125  def OnPrintPreview(self, event):
126  self.setup()
127  data = wx.PrintDialogData(self.printData)
128  printout = MapPrint(self.mapwin)
129  printout2 = MapPrint(self.mapwin)
130  self.preview = wx.PrintPreview(printout, printout2, data)
131 
132  if not self.preview.Ok():
133  wx.MessageBox("There was a problem printing this display\n", wx.OK)
134  return
135 
136  pfrm = wx.PreviewFrame(self.preview, self.mapframe, "Print preview")
137 
138  pfrm.Initialize()
139  pfrm.SetPosition(self.mapframe.GetPosition())
140  pfrm.SetSize(self.mapframe.GetClientSize())
141  pfrm.Show(True)
142 
143  def OnDoPrint(self, event):
144  self.setup()
145  pdd = wx.PrintDialogData(self.printData)
146  # set number of pages/copies
147  pdd.SetToPage(1)
148  printer = wx.Printer(pdd)
149  printout = MapPrint(self.mapwin)
150 
151  if not printer.Print(self.mapframe, printout, True):
152  wx.MessageBox("There was a problem printing.\nPerhaps your current printer is not set correctly?", "Printing", wx.OK)
153  else:
154  self.printData = wx.PrintData( printer.GetPrintDialogData().GetPrintData() )
155  printout.Destroy()
156 class MapPrint(wx.Printout):
157  def __init__(self, canvas):
158  wx.Printout.__init__(self)
159  self.canvas = canvas
160 
161  def OnBeginDocument(self, start, end):
162  return super(MapPrint, self).OnBeginDocument(start, end)
163 
164  def OnEndDocument(self):
165  super(MapPrint, self).OnEndDocument()
166 
167  def OnBeginPrinting(self):
168  super(MapPrint, self).OnBeginPrinting()
169 
170  def OnEndPrinting(self):
171  super(MapPrint, self).OnEndPrinting()
172 
173  def OnPreparePrinting(self):
174  super(MapPrint, self).OnPreparePrinting()
175 
176  def HasPage(self, page):
177  if page <= 2:
178  return True
179  else:
180  return False
181 
182  def GetPageInfo(self):
183  return (1, 2, 1, 2)
184 
185  def OnPrintPage(self, page):
186  dc = self.GetDC()
187 
188  #-------------------------------------------
189  # One possible method of setting scaling factors...
190  maxX, maxY = self.canvas.GetSize()
191 
192  # Let's have at least 50 device units margin
193  marginX = 10
194  marginY = 10
195 
196  # Add the margin to the graphic size
197  maxX = maxX + (2 * marginX)
198  maxY = maxY + (2 * marginY)
199 
200  # Get the size of the DC in pixels
201  (w, h) = dc.GetSizeTuple()
202 
203  # Calculate a suitable scaling factor
204  scaleX = float(w) / maxX
205  scaleY = float(h) / maxY
206 
207  # Use x or y scaling factor, whichever fits on the DC
208  actualScale = min(scaleX, scaleY)
209 
210  # Calculate the position on the DC for centering the graphic
211  posX = (w - (self.canvas.GetSize()[0] * actualScale)) / 2.0
212  posY = (h - (self.canvas.GetSize()[1] * actualScale)) / 2.0
213 
214  # Set the scale and origin
215  dc.SetUserScale(actualScale, actualScale)
216  dc.SetDeviceOrigin(int(posX), int(posY))
217 
218  #-------------------------------------------
219 
220  self.canvas.pdc.DrawToDC(dc)
221 
222  # prints a page number on the page
223  # dc.DrawText("Page: %d" % page, marginX/2, maxY-marginY)
224 
225  return True
226 
227 class PrintOptions(wx.Object):
228  def __init__(self, parent, mapwin):
229  self.mapframe = parent
230  self.mapwin = mapwin
231  #self.frame = frame
232 
233  self.printData = None
234 
235  #self.canvas = ScrolledWindow.MyCanvas(self)
236 
237  def setup(self):
238  if self.printData:
239  return
240  self.printData = wx.PrintData()
241  self.printData.SetPaperId(wx.PAPER_LETTER)
242  self.printData.SetPrintMode(wx.PRINT_MODE_PRINTER)
243 
244  def OnPageSetup(self, event):
245  self.setup()
246  psdd = wx.PageSetupDialogData(self.printData)
247  psdd.CalculatePaperSizeFromId()
248  dlg = wx.PageSetupDialog(self.mapwin, psdd)
249  dlg.ShowModal()
250 
251  # this makes a copy of the wx.PrintData instead of just saving
252  # a reference to the one inside the PrintDialogData that will
253  # be destroyed when the dialog is destroyed
254  self.printData = wx.PrintData( dlg.GetPageSetupData().GetPrintData() )
255 
256  dlg.Destroy()
257 
258  def OnPrintPreview(self, event):
259  self.setup()
260  data = wx.PrintDialogData(self.printData)
261  printout = MapPrint(self.mapwin)
262  printout2 = MapPrint(self.mapwin)
263  self.preview = wx.PrintPreview(printout, printout2, data)
264 
265  if not self.preview.Ok():
266  wx.MessageBox("There was a problem printing this display\n", wx.OK)
267  return
268 
269  pfrm = wx.PreviewFrame(self.preview, self.mapframe, "Print preview")
270 
271  pfrm.Initialize()
272  pfrm.SetPosition(self.mapframe.GetPosition())
273  pfrm.SetSize(self.mapframe.GetClientSize())
274  pfrm.Show(True)
275 
276  def OnDoPrint(self, event):
277  self.setup()
278  pdd = wx.PrintDialogData(self.printData)
279  # set number of pages/copies
280  pdd.SetToPage(1)
281  printer = wx.Printer(pdd)
282  printout = MapPrint(self.mapwin)
283 
284  if not printer.Print(self.mapframe, printout, True):
285  GMessage(_("There was a problem printing.\n"
286  "Perhaps your current printer is not set correctly?"))
287  else:
288  self.printData = wx.PrintData( printer.GetPrintDialogData().GetPrintData() )
289  printout.Destroy()
wxGUI command interface
#define min(x, y)
Definition: draw2.c:68
def OnEndPrinting
Definition: gprint.py:37
def GetPageInfo
Definition: gprint.py:49
def OnPrintPage
Definition: gprint.py:52
def OnEndDocument
Definition: gprint.py:31
def __init__
Definition: gprint.py:24
def OnBeginDocument
Definition: gprint.py:28
def OnPreparePrinting
Definition: gprint.py:40
def OnBeginPrinting
Definition: gprint.py:34