Implementing a Rubber Banding Script Question.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Nebulism
    New Member
    • Jun 2007
    • 31

    #1

    Implementing a Rubber Banding Script Question.

    Hi everyone!

    Earlier I asked a question about mouse interaction with a GUI. I have found a pretty comprehensive script that is supposed to work <mod edit: link removed, source inserted>:
    Originally posted by ASPN
    Title: wxPython Graphics - Drawing rubberbands over a canvas
    Submitter: Anand Pillai (other recipes)
    Last Updated: 2003/12/30
    Version no: 1.1
    Category: Image
    Description:

    This recipe talks about how to implement rubberbanding code for canvas
    objects in a wxPython application. Canvas classes in wxPython include
    wxPanel, wxStaticBitmap, wxGlCanvas etc.
    [CODE=python]# This class describes a generic method of drawing rubberbands
    # on a wxPython canvas object (wxStaticBitmap , wxPanel etc) when
    # the user presses the left mouse button and drags it over a rectangular
    # area. It has methods to return the selected area by the user as
    # a rectangular 4 tuple / Clear the selected area.

    # Beginning of code

    class wxPyRubberBande r:
    """ A class to manage mouse events/ rubberbanding of a wxPython
    canvas object """

    def __init__(self, canvas):

    # canvas object
    self._canvas = canvas
    # mouse selection start point
    self.m_stpoint= wxPoint(0,0)
    # mouse selection end point
    self.m_endpoint =wxPoint(0,0)
    # mouse selection cache point
    self.m_savepoin t=wxPoint(0,0)

    # flags for left click/ selection
    self._leftclick ed=false
    self._selected= false

    # Register event handlers for mouse
    self.RegisterEv entHandlers()

    def RegisterEventHa ndlers(self):
    """ Register event handlers for this object """

    EVT_LEFT_DOWN(s elf._canvas, self.OnMouseEve nt)
    EVT_LEFT_UP(sel f._canvas, self.OnMouseEve nt)
    EVT_MOTION(self ._canvas, self.OnMouseEve nt)


    def OnMouseEvent(se lf, event):
    """ This function manages mouse events """


    if event:

    # set mouse cursor
    self._canvas.Se tCursor(wxStock Cursor(wxCURSOR _ARROW))
    # get device context of canvas
    dc= wxClientDC(self ._canvas)

    # Set logical function to XOR for rubberbanding
    dc.SetLogicalFu nction(wxXOR)

    # Set dc brush and pen
    # Here I set brush and pen to white and grey respectively
    # You can set it to your own choices

    # The brush setting is not really needed since we
    # dont do any filling of the dc. It is set just for
    # the sake of completion.

    wbrush = wxBrush(wxColou r(255,255,255), wxTRANSPARENT)
    wpen = wxPen(wxColour( 200, 200, 200), 1, wxSOLID)
    dc.SetBrush(wbr ush)
    dc.SetPen(wpen)


    if event.LeftDown( ):

    # Left mouse button down, change cursor to
    # something else to denote event capture
    self.m_stpoint = event.GetPositi on()
    cur = wxStockCursor(w xCURSOR_CROSS)
    self._canvas.Se tCursor(cur)

    # invalidate current canvas
    self._canvas.Re fresh()
    # cache current position
    self.m_savepoin t = self.m_stpoint
    self._selected = false
    self._leftclick ed = true

    elif event.Dragging( ):

    # User is dragging the mouse, check if
    # left button is down

    if self._leftclick ed:

    # reset dc bounding box
    dc.ResetBoundin gBox()
    dc.BeginDrawing ()
    w = (self.m_savepoi nt.x - self.m_stpoint. x)
    h = (self.m_savepoi nt.y - self.m_stpoint. y)

    # To erase previous rectangle
    dc.DrawRectangl e(self.m_stpoin t.x, self.m_stpoint. y, w, h)

    # Draw new rectangle
    self.m_endpoint = event.GetPositi on()

    w = (self.m_endpoin t.x - self.m_stpoint. x)
    h = (self.m_endpoin t.y - self.m_stpoint. y)

    # Set clipping region to rectangle corners
    dc.SetClippingR egion(self.m_st point.x, self.m_stpoint. y, w,h)
    dc.DrawRectangl e(self.m_stpoin t.x, self.m_stpoint. y, w, h)
    dc.EndDrawing()

    self.m_savepoin t = self.m_endpoint # cache current endpoint

    elif event.LeftUp():

    # User released left button, change cursor back
    self._canvas.Se tCursor(wxSTOCK _CURSOR(wxCURSO R_ARROW))
    self._selected = true #selection is done
    self._leftclick ed = false # end of clicking


    def GetCurrentSelec tion(self):
    """ Return the current selected rectangle """

    # if there is no selection, selection defaults to
    # current viewport

    left = wxPoint(0,0)
    right = wxPoint(0,0)

    # user dragged mouse to right
    if self.m_endpoint .y > self.m_stpoint. y:
    right = self.m_endpoint
    left = self.m_stpoint
    # user dragged mouse to left
    elif self.m_endpoint .y < self.m_stpoint. y:
    right = self.m_stpoint
    left = self.m_endpoint

    return (left.x, left.y, right.x, right.y)


    def ClearCurrentSel ection(self):
    """ Clear the current selected rectangle """

    box = self.GetCurrent Selection()

    dc=wxClientDC(s elf._canvas)

    w = box[2] - box[0]
    h = box[3] - box[1]
    dc.SetClippingR egion(box[0], box[1], w, h)
    dc.SetLogicalFu nction(wxXOR)

    # The brush is not really needed since we
    # dont do any filling of the dc. It is set for
    # sake of completion.

    wbrush = wxBrush(wxColou r(255,255,255), wxTRANSPARENT)
    wpen = wxPen(wxColour( 200, 200, 200), 1, wxSOLID)
    dc.SetBrush(wbr ush)
    dc.SetPen(wpen)
    dc.DrawRectangl e(box[0], box[1], w,h)
    self._selected = false

    # reset selection to canvas size
    self.ResetSelec tion()

    def ResetSelection( self):
    """ Resets the mouse selection to entire canvas """

    self.m_stpoint = wxPoint(0,0)
    sz=self._canvas .GetSize()
    w,h=sz.GetWidth (), sz.GetHeight()
    self.m_endpoint = wxPoint(w,h)[/CODE]
    The problem is I'm not to savy with classes yet and I don't know how to call it and use it. Can someone guide me through this, I would be pretty grateful.

    The main objective would be to set the image that is loaded as the canvas of the rubberbander and output the result.

    Says we start with some pseudocode:

    root=Tk()
    im=Image.open(" filename")

    rubberbandscrip t(........)

    print output

    root.mainloop()



    Thanks alot,
    JP
    Last edited by bartonc; Jun 21 '07, 04:55 PM. Reason: removed link.
  • bartonc
    Recognized Expert Expert
    • Sep 2006
    • 6478

    #2
    Originally posted by Nebulism
    root=Tk()
    im=Image.open(" filename")

    rubberbandscrip t(........)

    print output

    root.mainloop()
    You won't be able to use this class with Tkinter. It's designed to run in an old-style wxPython environment from back when they used to use
    Code:
    from wx import *
    It would need a little tweaking to run in a modern wxPython installation.

    Comment

    • Nebulism
      New Member
      • Jun 2007
      • 31

      #3
      Damn,

      Well thanks for the input bud, guess it's back to the drawing board. Let me know if you know of any rubber banding area selection scripts for tkinter.

      Thanks,

      JP

      Comment

      • bartonc
        Recognized Expert Expert
        • Sep 2006
        • 6478

        #4
        Originally posted by Nebulism
        Damn,

        Well thanks for the input bud, guess it's back to the drawing board. Let me know if you know of any rubber banding area selection scripts for tkinter.

        Thanks,

        JP
        OR... you could install wxPython 2.8. Tkinter is really very limited. It's a good learning experience, but one soon finds that support and features are greatly lacking.

        Comment

        Working...