File: GUI.py

package info (click to toggle)
wxwidgets2.8 2.8.12.1-12
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 242,920 kB
  • sloc: cpp: 1,840,772; xml: 385,749; python: 334,729; makefile: 51,774; ansic: 30,987; sh: 7,716; sql: 258; lex: 194; perl: 139; yacc: 128; pascal: 95; php: 45; lisp: 38; tcl: 38; java: 22; haskell: 20; cs: 18; erlang: 17; ruby: 16; asm: 15; ada: 9; ml: 9; csh: 9
file content (116 lines) | stat: -rw-r--r-- 3,349 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
"""

Part of the floatcanvas.Utilities package.

This module contains assorted GUI-related utilities that can be used
with FloatCanvas

So far, they are:

RubberBandBox: used to draw a RubberBand Box on the screen

"""

import wx
from wx.lib.floatcanvas import FloatCanvas

class RubberBandBox:
    """
    Class to provide a rubber band box that can be drawn on a Window

    """

    def __init__(self, Canvas, CallBack, Tol=5):

        """
        To initialize:
        
        RubberBandBox(Canvas, CallBack)

        Canvas:  the FloatCanvas you want the Rubber band box to be used on

        CallBack: is the method you want called when the mouse is
                  released. That method will be called, passing in a rect
                  parameter, where rect is: (Point, WH) of the rect in
                  world coords.

        Tol: The tolerance for the smallest rectangle allowed. defaults
             to 5. In pixels

        Methods:
        
        Enable() : Enables the Rubber Band Box (Binds the events)
        
        Disable() : Enables the Rubber Band Box (Unbinds the events)

        Attributes:

        CallBack: The callback function, if it's replaced you need to
                  call Enable() again.
                  
        """

        self.Canvas = Canvas
        self.CallBack = CallBack
        self.Tol = Tol
        
        self.Drawing = False
        self.RBRect = None
        self.StartPointWorld = None

        return None

    def Enable(self):
        """
        Called when you want the rubber band box to be enabled

        """

        # bind events:
        self.Canvas.Bind(FloatCanvas.EVT_MOTION, self.OnMove ) 
        self.Canvas.Bind(FloatCanvas.EVT_LEFT_DOWN, self.OnLeftDown)
        self.Canvas.Bind(FloatCanvas.EVT_LEFT_UP, self.OnLeftUp ) 

    def Disable(self):
        """
        Called when you don't want the rubber band box to be enabled

        """

        # unbind events:
        self.Canvas.Unbind(FloatCanvas.EVT_MOTION)
        self.Canvas.Unbind(FloatCanvas.EVT_LEFT_DOWN)
        self.Canvas.Unbind(FloatCanvas.EVT_LEFT_UP)

    def OnMove(self, event):
        if self.Drawing:
            x, y = self.StartPoint
            Cornerx, Cornery = event.GetPosition()
            w, h = ( Cornerx - x, Cornery - y)
            if abs(w) > self.Tol and abs(h) > self.Tol:
                # draw the RB box
                dc = wx.ClientDC(self.Canvas)
                dc.SetPen(wx.Pen('WHITE', 2, wx.SHORT_DASH))
                dc.SetBrush(wx.TRANSPARENT_BRUSH)
                dc.SetLogicalFunction(wx.XOR)
                if self.RBRect:
                    dc.DrawRectangle(*self.RBRect)
                self.RBRect = (x, y, w, h )
                dc.DrawRectangle(*self.RBRect)
        event.Skip() # skip so that other events can catch these

    def OnLeftDown(self, event):
        # Start drawing
        self.Drawing = True
        self.StartPoint = event.GetPosition()
        self.StartPointWorld = event.Coords
    
    def OnLeftUp(self, event):
        # Stop Drawing
        if self.Drawing:
            self.Drawing = False
            if self.RBRect:
                WH = event.Coords - self.StartPointWorld
                wx.CallAfter(self.CallBack, (self.StartPointWorld, WH))
        self.RBRect = None
        self.StartPointWorld = None