File: OverlayDemo.py

package info (click to toggle)
wxpython4.0 4.2.3%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 221,752 kB
  • sloc: cpp: 962,555; python: 230,573; ansic: 170,731; makefile: 51,756; sh: 9,342; perl: 1,564; javascript: 584; php: 326; xml: 200
file content (142 lines) | stat: -rw-r--r-- 4,021 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/usr/bin/env python

"""
A simple demo to show how to "Overlays": i.e. drawing something
on top of eveything else on the Canvas, relative to window coords,
rather than screen coords.

This method uses the "GridOver" object in the FloatCanvas
  - it was originally dsigend for grids, graticule,s etc. that
    are always drawn regardless of zoom, pan, etc, but it works
    for overlays too.

"""

import wx


try:
    # See if there is a local copy
    import sys
    sys.path.append("../")
    from floatcanvas import NavCanvas, FloatCanvas
except ImportError:
    from wx.lib.floatcanvas import NavCanvas, FloatCanvas

class TextOverlay(FloatCanvas.Text):
    """
    An example of an Overlay object:

    all it needs is a new _Draw method.

    NOTE: you may want to get fancier with this,
          deriving from ScaledTextBox

    """
    def __init__(self,
                 String,
                 xy,
                 Size = 24,
                 Color = "Black",
                 BackgroundColor = None,
                 Family = wx.MODERN,
                 Style = wx.NORMAL,
                 Weight = wx.NORMAL,
                 Underlined = False,
                 Font = None):
        FloatCanvas.Text.__init__(self,
                                  String,
                                  xy,
                                  Size = Size,
                                  Color = Color,
                                  BackgroundColor = BackgroundColor,
                                  Family = Family,
                                  Style = Style,
                                  Weight = Weight,
                                  Underlined = Underlined,
                                  Font = Font)

    def _Draw(self, dc, Canvas):
        """
        _Draw method for Overlay
         note: this is a differeent signarture than the DrawObject Draw
        """
        dc.SetFont(self.Font)
        dc.SetTextForeground(self.Color)
        if self.BackgroundColor:
            dc.SetBackgroundMode(wx.SOLID)
            dc.SetTextBackground(self.BackgroundColor)
        else:
            dc.SetBackgroundMode(wx.TRANSPARENT)
        ## maybe inpliment this...
        #if self.TextWidth is None or self.TextHeight is None:
        #    (self.TextWidth, self.TextHeight) = dc.GetTextExtent(self.String)
        #XY = self.ShiftFun(XY[0], XY[1], self.TextWidth, self.TextHeight)
        dc.DrawText(self.String, self.XY)


class DrawFrame(wx.Frame):

    """
    A frame used for the FloatCanvas Demo

    """

    def __init__(self, *args, **kwargs):
        wx.Frame.__init__(self, *args, **kwargs)

        self.CreateStatusBar()

        # Add the Canvas
        Canvas = NavCanvas.NavCanvas(self,-1,
                                     size = (500,500),
                                     ProjectionFun = None,
                                     Debug = 0,
                                     BackgroundColor = "DARK SLATE BLUE",
                                     ).Canvas


        Point = (45,40)
        Box = Canvas.AddCircle(Point,
                               Diameter = 10,
                               FillColor = "Cyan",
                               LineColor = "Red",
                               LineWidth = 6)

        Canvas.GridOver = TextOverlay("Some Text",
                                      (20,20),
                                      Size = 48,
                                      Color = "Black",
                                      BackgroundColor = 'Pink',
                                      )

        Canvas.Bind(FloatCanvas.EVT_MOTION, self.OnMove)

        self.Show()
        Canvas.ZoomToBB()

    def OnMove(self, event):
        """
        Updates the status bar with the world coordinates

        """
        self.SetStatusText("%.2f, %.2f"%tuple(event.Coords))



app = wx.App(False) # true to get its own output window.
F = DrawFrame(None, title="FloatCanvas Demo App", size=(700,700) )
app.MainLoop()