1 |
# Copyright (c) 2001, 2002 by Intevation GmbH |
2 |
# Authors: |
3 |
# Frank Koormann <[email protected]> |
4 |
# |
5 |
# This program is free software under the GPL (>=v2) |
6 |
# Read the file COPYING coming with Thuban for details. |
7 |
|
8 |
__version__ = "$Revision$" |
9 |
|
10 |
from Thuban import _ |
11 |
from Thuban.Model.scalebar import deriveInterval, roundInterval |
12 |
|
13 |
from wxPython.wx import * |
14 |
|
15 |
class ScaleBar: |
16 |
|
17 |
def __init__(self, map): |
18 |
self.map = map |
19 |
|
20 |
def DrawScaleBar(self, scale, dc, position, size): |
21 |
"""Draw a scalebar on a given DC""" |
22 |
|
23 |
# Only draw a legend if the corresponding map has a layer |
24 |
if self.map is not None and len(self.map.layers) > 0 and scale > 0.0: |
25 |
|
26 |
# If no projection is specified, the scale information _might_ |
27 |
# be reasonable. So gray out the scalebar in these cases. |
28 |
if self.map.projection is None: |
29 |
BlackPen = wxGREY_PEN |
30 |
BlackBrush = wxGREY_BRUSH |
31 |
BlackText = wxColor(127,127,127) |
32 |
else: |
33 |
BlackPen = wxBLACK_PEN |
34 |
BlackBrush = wxBLACK_BRUSH |
35 |
BlackText = wxBLACK |
36 |
|
37 |
# Get the dimension |
38 |
width, height = size |
39 |
posx, posy = position |
40 |
l1width, l1height = dc.GetTextExtent("%d"%0) |
41 |
|
42 |
# Make a first guess for the interval (to get the size we have |
43 |
# to reserve for the labels) |
44 |
interval, unit = deriveInterval(width, scale) |
45 |
l2width, l2height = dc.GetTextExtent("%d %s"%(interval,unit)) |
46 |
width = width - 4.0 - l1width/2.0 -l2width/2.0 |
47 |
|
48 |
# Having precised the width now the final interval can be calculated |
49 |
interval, unit = deriveInterval(width, scale) |
50 |
interval, label = roundInterval(interval) |
51 |
|
52 |
if interval > 0.0: |
53 |
# We draw 2 rectangles with half the width |
54 |
if unit == 'km': |
55 |
width = int(interval*1000.0*scale/2) |
56 |
else: |
57 |
width = int(interval*scale/2) |
58 |
|
59 |
dc.SetPen(BlackPen) |
60 |
|
61 |
brush = wxBrush(wxWHITE, wxSOLID) |
62 |
dc.SetBrush(brush) |
63 |
dc.DrawRectangle(posx+4,posy+2,width,8) |
64 |
|
65 |
dc.SetBrush(BlackBrush) |
66 |
dc.DrawRectangle(posx+width+4,posy+2,width,8) |
67 |
|
68 |
dc.SetTextForeground(BlackText) |
69 |
dc.DrawText("%d"%0, posx+ 4 - l1width/2, posy+12) |
70 |
|
71 |
l2width, l2height = dc.GetTextExtent("%s %s"%(label, unit)) |
72 |
dc.DrawText("%s %s"%(interval, unit), posx+ 2*width+4 - l2width/2, posy + 12) |
73 |
|