Source

wxPython / wx / lib / floatcanvas / Utilities / GUI.py

"""

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 numpy as np

import wx
from wx.lib.floatcanvas import FloatCanvas, GUIMode

class RubberBandBox(GUIMode.GUIBase):
    """
    Class to provide a GUI Mode that makes a rubber band box that can be drawn on a Window

    """

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

        """
        To initialize:
        
        RubberBandBox(CallBack, Tol=5)

        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

        
        Attributes:

        CallBack: The callback function.
                  
        """

        self.Canvas = None # this will be set when the mode is set on a Canvas
        self.CallBack = CallBack
        self.Tol = Tol
        
        self.Drawing = False
        self.RBRect = None
        self.StartPointWorld = None

        return None

    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.DrawRectanglePointSize(*self.RBRect)
                self.RBRect = ((x, y), (w, h) )
                dc.DrawRectanglePointSize(*self.RBRect)
        self.Canvas._RaiseMouseEvent(event,FloatCanvas.EVT_FC_MOTION)
                
    def OnLeftDown(self, event):
        # Start drawing
        self.Drawing = True
        self.StartPoint = event.GetPosition()
            
    def OnLeftUp(self, event):
        # Stop Drawing
        if self.Drawing:
            self.Drawing = False
            if self.RBRect:
                world_rect = (self.Canvas.PixelToWorld(self.RBRect[0]),
                              self.Canvas.ScalePixelToWorld(self.RBRect[1])
                              )
                wx.CallAfter(self.CallBack, world_rect)
        self.RBRect = None
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.