/[thuban]/branches/WIP-pyshapelib-bramz/Thuban/UI/view.py
ViewVC logotype

Diff of /branches/WIP-pyshapelib-bramz/Thuban/UI/view.py

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 909 by jonathan, Fri May 9 16:34:39 2003 UTC revision 910 by frank, Fri May 16 16:23:43 2003 UTC
# Line 1  Line 1 
1  # Copyright (c) 2001, 2002, 2003 by Intevation GmbH  # Copyright (c) 2001, 2002, 2003 by Intevation GmbH
2  # Authors:  # Authors:
3  # Bernhard Herzog <[email protected]>  # Bernhard Herzog <[email protected]>
4    # Frank Koormann <[email protected]>
5  #  #
6  # This program is free software under the GPL (>=v2)  # This program is free software under the GPL (>=v2)
7  # Read the file COPYING coming with Thuban for details.  # Read the file COPYING coming with Thuban for details.
# Line 11  Classes for display of a map and interac Line 12  Classes for display of a map and interac
12    
13  __version__ = "$Revision$"  __version__ = "$Revision$"
14    
15    from Thuban import _
16    
17  import sys  import sys
18    import os.path
19    
20  from math import hypot  from math import hypot
21    
# Line 19  from wxPython.wx import wxWindow,\ Line 23  from wxPython.wx import wxWindow,\
23       wxPaintDC, wxColour, wxClientDC, wxINVERT, wxTRANSPARENT_BRUSH, wxFont,\       wxPaintDC, wxColour, wxClientDC, wxINVERT, wxTRANSPARENT_BRUSH, wxFont,\
24       EVT_PAINT, EVT_LEFT_DOWN, EVT_LEFT_UP, EVT_MOTION, EVT_LEAVE_WINDOW, \       EVT_PAINT, EVT_LEFT_DOWN, EVT_LEFT_UP, EVT_MOTION, EVT_LEAVE_WINDOW, \
25       wxBITMAP_TYPE_XPM, wxBeginBusyCursor, wxEndBusyCursor, wxCursor, \       wxBITMAP_TYPE_XPM, wxBeginBusyCursor, wxEndBusyCursor, wxCursor, \
26       wxImageFromBitmap       wxImageFromBitmap, wxPlatform
27    
28    # Export related stuff
29    if wxPlatform == '__WXMSW__':
30        from wxPython.wx import wxMetaFileDC
31    from wxPython.wx import wxFileDialog, wxSAVE, wxOVERWRITE_PROMPT, wxID_OK
32    
33  from wxPython import wx  from wxPython import wx
34    
# Line 39  from Thuban.Model.color import Color Line 47  from Thuban.Model.color import Color
47  import resource  import resource
48    
49  from selection import Selection  from selection import Selection
50  from renderer import ScreenRenderer, PrinterRender  from renderer import ScreenRenderer, ExportRenderer, PrinterRenderer
51    
52  import labeldialog  import labeldialog
53    
# Line 224  class LabelTool(Tool): Line 232  class LabelTool(Tool):
232          self.view.LabelShapeAt(event.m_x, event.m_y)          self.view.LabelShapeAt(event.m_x, event.m_y)
233    
234    
   
   
235  class MapPrintout(wx.wxPrintout):  class MapPrintout(wx.wxPrintout):
236    
237      """      """
238      wxPrintout class for printing Thuban maps      wxPrintout class for printing Thuban maps
239      """      """
240    
241      def __init__(self, map):      def __init__(self, canvas, map, region, selected_layer, selected_shapes):
242          wx.wxPrintout.__init__(self)          wx.wxPrintout.__init__(self)
243            self.canvas = canvas
244          self.map = map          self.map = map
245            self.region = region
246            self.selected_layer = selected_layer
247            self.selected_shapes = selected_shapes
248    
249      def GetPageInfo(self):      def GetPageInfo(self):
250          return (1, 1, 1, 1)          return (1, 1, 1, 1)
# Line 248  class MapPrintout(wx.wxPrintout): Line 258  class MapPrintout(wx.wxPrintout):
258    
259      def draw_on_dc(self, dc):      def draw_on_dc(self, dc):
260          width, height = self.GetPageSizePixels()          width, height = self.GetPageSizePixels()
261          llx, lly, urx, ury = self.map.ProjectedBoundingBox()          scale, offset, mapregion = OutputTransform(self.canvas.scale,
262          scalex = width / (urx - llx)                                                     self.canvas.offset,
263          scaley = height / (ury - lly)                                                     self.canvas.GetSizeTuple(),
264          scale = min(scalex, scaley)                                                     self.GetPageSizePixels())
         offx = 0.5 * (width - (urx + llx) * scale)  
         offy = 0.5 * (height + (ury + lly) * scale)  
   
265          resx, resy = self.GetPPIPrinter()          resx, resy = self.GetPPIPrinter()
266          renderer = PrinterRender(dc, scale, (offx, offy), resolution = resx)          renderer = PrinterRenderer(dc, scale, offset, resolution = resy)
267          renderer.RenderMap(self.map)          x, y, width, height = self.region
268            canvas_scale = self.canvas.scale
269            renderer.RenderMap(self.map,
270                               (0,0,
271                                    (width/canvas_scale)*scale,
272                                    (height/canvas_scale)*scale),
273                                    mapregion,
274                               self.selected_layer, self.selected_shapes)
275          return wx.true          return wx.true
276    
277    
# Line 432  class MapCanvas(wxWindow, Publisher): Line 446  class MapCanvas(wxWindow, Publisher):
446          clientdc.Blit(0, 0, width, height, dc, 0, 0)          clientdc.Blit(0, 0, width, height, dc, 0, 0)
447          clientdc.EndDrawing()          clientdc.EndDrawing()
448    
449        def Export(self):
450            if hasattr(self, "export_path"):
451                export_path = self.export_path
452            else:
453                export_path="."
454            dlg = wxFileDialog(self, _("Export Map"), export_path, "",
455                               "Enhanced Metafile (*.wmf)|*.wmf",
456                               wxSAVE|wxOVERWRITE_PROMPT)
457            if dlg.ShowModal() == wxID_OK:
458                self.export_path = os.path.dirname(dlg.GetPath())
459                dc = wxMetaFileDC(dlg.GetPath())
460        
461                scale, offset, mapregion = OutputTransform(self.scale,
462                                                           self.canvas.offset,
463                                                           self.GetSizeTuple(),
464                                                           dc.GetSizeTuple())
465    
466                selected_layer = self.selection.SelectedLayer()
467                selected_shapes = self.selection.SelectedShapes()
468    
469                renderer = ExportRenderer(dc, scale, offset)
470    
471                # Pass the entire bitmap as update region to the renderer.
472                # We're redrawing the whole bitmap, after all.
473                width, height = self.GetSizeTuple()
474                renderer.RenderMap(self.map,
475                                    (0,0,
476                                        (width/self.scale)*scale,
477                                        (height/self.scale)*scale),
478                                    mapregion,
479                                    selected_layer, selected_shapes)
480                dc.EndDrawing()
481                dc.Close()
482            dlg.Destroy()
483            
484      def Print(self):      def Print(self):
485          printer = wx.wxPrinter()          printer = wx.wxPrinter()
486          printout = MapPrintout(self.map)          width, height = self.GetSizeTuple()
487            selected_layer = self.selection.SelectedLayer()
488            selected_shapes = self.selection.SelectedShapes()
489            
490            printout = MapPrintout(self, self.map, (0, 0, width, height),
491                                   selected_layer, selected_shapes)
492          printer.Print(self, printout, wx.true)          printer.Print(self, printout, wx.true)
493          printout.Destroy()          printout.Destroy()
494    
# Line 945  class MapCanvas(wxWindow, Publisher): Line 999  class MapCanvas(wxWindow, Publisher):
999                      valign = ALIGN_CENTER                      valign = ALIGN_CENTER
1000                  label_layer.AddLabel(x, y, text,                  label_layer.AddLabel(x, y, text,
1001                                       halign = halign, valign = valign)                                       halign = halign, valign = valign)
1002    
1003    def OutputTransform(canvas_scale, canvas_offset, canvas_size, device_extend):
1004        """Calculate dimensions to transform canvas content to output device."""
1005        width, height = device_extend
1006    
1007        # Only 80 % of the with are available for the map
1008        width = width * 0.8
1009    
1010        # Define the distance of the map from DC border
1011        distance = 20
1012    
1013        if height < width:
1014            # landscape
1015            map_height = height - 2*distance
1016            map_width = map_height
1017        else:
1018            # portrait, recalibrate width (usually the legend width is too
1019            # small
1020            width = width * 0.9
1021            map_height = width - 2*distance
1022            map_width = map_height
1023        
1024        mapregion = (distance, distance,
1025                     distance+map_width, distance+map_height)
1026    
1027        canvas_width, canvas_height = canvas_size
1028        
1029        scalex = map_width / (canvas_width/canvas_scale)
1030        scaley = map_height / (canvas_height/canvas_scale)
1031        scale = min(scalex, scaley)
1032        canvas_offx, canvas_offy = canvas_offset
1033        offx = scale*canvas_offx/canvas_scale
1034        offy = scale*canvas_offy/canvas_scale
1035    
1036        return scale, (offx, offy), mapregion

Legend:
Removed from v.909  
changed lines
  Added in v.910

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26