Solved

wx.lib.ogl ShapeCanvas question

Posted on 2009-04-13
4
800 Views
Last Modified: 2013-12-06
How can I find out what shape on the shape canvas is currently selected?

I had a look at the API (http://www.wxpython.org/docs/api/wx.lib.ogl.ShapeCanvas-class.html) but it doesn't seem to have any method like GetSelectedShape.

I know that Shape has a method called Selected which returns True if a shape is selected. So I could possible do something like:
1. create a shape on the canvas
2. add it to a global array of created shapes
3. iterate through this array to see for which shape Selected returns true and return this shape

But is there a better way of doing things?

The reason I ask is because in my frame is defined a method which is a method that appears on the pop up menu when a shape is right clicked and I need this method to be able to access the shape that was right clicked....I can't find a way to pass along the information about what shape was selected from my Event Handler's OnRightClick method to the frame.PopupMenu(frame.shape_menu) to the frame's self.Bind(wx.EVT_MENU, self.OnAddChild, id=1000) such that I can pass the shape as a parameter in def OnAddChild(self, event). I don't even know if this is something that can/should be done.

Phew! If that explanation sounded crazy please have a look at the relevant bits of code I attached for your perusal :)

Can anyone advise on what would be the best way to solve my issue?

Thanks
import wx

import wx.lib.ogl as ogl

        

       
 

class MyEvtHandler(ogl.ShapeEvtHandler):

  

[SNIPPED FOR CLARITY]
 

        def OnRightClick(self, x, y, *dontcare): #*dontcare

            self.OnLeftClick(x, y, 0, 0)

            

            shape = self.GetShape()

            canvas = shape.GetCanvas()

            frame = canvas.GetFrame()

            frame.PopupMenu(frame.shape_menu)

 

                                

    

class OGLCanvas(ogl.ShapeCanvas):

      [SNIPPED FOR CLARITY]        
 

      #This is where I want to define the method that returns the shape current

      # selected by the mouse
 

       def GetSelectedShape(self):

            pass

        

        def MyAddShape(self, shape, x, y, pen, brush, text):
 

            [SNIPPED FOR CLARITY]

    

            evthandler = MyEvtHandler()

            evthandler.SetShape(shape)

            evthandler.SetPreviousHandler(shape.GetEventHandler())

            shape.SetEventHandler(evthandler)

        

            return shape

        
 

        

class OGLFrame(wx.Frame):

        def __init__(self, *args, **kwds):

                wx.Frame.__init__(self, *args, **kwds)
 

                self.SetTitle("Testing right click")

                self.SetBackgroundColour(wx.Colour(8, 197, 248))

                self.canvas = OGLCanvas(self, self)

                

                self.shape_menu = wx.Menu()

                self.shape_menu.Append(1000, "Add Child")

                self.shape_menu.Append(2000, "Add Text")

            

                self.Bind(wx.EVT_MENU, self.OnAddChild, id=1000)

                self.Bind(wx.EVT_MENU, self.OnAddText, id=2000)

                

        def OnAddChild(self, event):

             

                toShape = self.canvas.MyAddShape(ogl.TextShape(120, 45), 

                    160, 35, wx.GREEN_PEN, wx.LIGHT_GREY_BRUSH, "node 1")

         

                # this is where I need to use the method

                fromShape = self.canvas.GetSelectedShape()    
 

                self.canvas.shapes.append(fromShape)

                self.canvas.shapes.append(toShape)

                self.canvas.MyAddLink()                 
 

                self.canvas.Refresh(False)

Open in new window

0
Comment
Question by:pybee
  • 2
  • 2
4 Comments
 
LVL 11

Expert Comment

by:jgordos
ID: 24221158
Doesn't the canvas already have a collection that it's keeping the Shapes on the canvas in?

In other words, I think that yes, i think you need to iterate over the shapes and check the flag, but don't create your own array; use the one the canvas already has..

However, all that being said... make sure that overlapping shapes don't BOTH get their selected bit set....

-john

0
 

Accepted Solution

by:
pybee earned 0 total points
ID: 24238742
A while back I tried checking the Selected flag for each shape but it never returned the right answer...ie the shape returned was not the correct one even though there was no overapping going on. I ended up doing something like
frame.currentShape = shape
in my Shape Event Handler so then I can access the current shape from the shape and that is working out fine so far.
0
 
LVL 11

Expert Comment

by:jgordos
ID: 24245597
"Selected" might mean something different than we think it does... not everything is selectable...

You could try hooking "mousein" / "mouseout"
0
 

Author Comment

by:pybee
ID: 24246838
Yes I was thinking the same...that perhaps selected does not mean what I think it means. Thanks for the mouse in/out suggestion as an alternative. At the moment though I'll stick with accessing the current shape from the frame since that seems to do the job and I don't think (or I hope not ) it is bad practice or anything...
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Variable is a place holder or reserved memory locations to store any value. Which means whenever we create a variable, indirectly we are reserving some space in the memory. The interpreter assigns or allocates some space in the memory based on the d…
Sequence is something that used to store data in it in very simple words. Let us just create a list first. To create a list first of all we need to give a name to our list which I have taken as “COURSE” followed by equals sign and finally enclosed …
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now