[Fwd: RE: Drag and Drop with custom data - example required]

Fantastic!!

Potential solution...

Subject: RE: [wxPython-users] Drag and Drop with custom data - example required
Date: Wed, 28 Feb 2007 13:18:36 -0000
From: Gavana, Andrea <gavana@kpo.kz>
Reply-To: wxPython-users@lists.wxwidgets.org
To: <wxPython-users@lists.wxwidgets.org>

Hi Jon,

> As the subject suggests; I've been banging my head against a > brick wall for a couple of days now trying to get a simple > drag and drop app working. I want to be able to drag'n'drop > some data from a list control to another panel within the > same app, but all the tutorials I've found talk about using > the standard text versions. If anyone has any experience in > dragging and dropping custom data, I'd be extremely grateful!

You could try something like this:

import cPickle
import weakref

class DragInfo:

    _map = weakref.WeakValueDictionary()

    def __init__(self, data):
        """ Default class constructor. """
                self._id = id(data)
        DragInfo._map[self._id] = data

    def GetMyData(self):
        """ Returns the data. """
                return DragInfo._map.get(self._id, None)

class DropTarget(wx.DropTarget):

    def __init__(self, parent):
        """ Default class constructor. """
                wx.DropTarget.__init__(self)

        self._parent = parent
        self._dataobject = wx.CustomDataObject(wx.CustomDataFormat("MySpecialData"))
        self.SetDataObject(self._dataobject)

    def OnData(self, x, y, dragres):
        """ Handles the OnData() method to call the real DnD routine. """

        if not self.GetData():
            return wx.DragNone

        draginfo = self._dataobject.GetData()
        drginfo = cPickle.loads(draginfo)

        if not drginfo.GetMyData():
            return wx.DragNone
                return self._parent.OnDropTarget(x, y, drginfo.GetMyData())

# If you defined an event like OnBeginDrag in your ListCtrl
def OnBeginDrag(self, event):

        draginfo = cPickle.dumps(DragInfo(item))
        dataobject = wx.CustomDataObject(wx.CustomDataFormat("MySpecialData"))
        dataobject.SetData(draginfo)
        dragSource = wx.DropSource(self)
        dragSource.SetData(dataobject)
        dragSource.DoDragDrop(wx.Drag_DefaultMove)
        event.Skip()

# Then, in your panel (drop target) you can do:

def OnDropTarget(self, x, y, myData):
        """ Handles the drop action from a DND operation. """

        # do whatever you like here with myData
        return wx.DragMove

I hope this helps...

Andrea.

I think I've almost got it. You can see the result here:

The only problem left to fix is that when I try to start to drag the text, I get this:

Started dragging
Traceback (most recent call last):
  File "D:\HyperLight Online\v4.0\HyperSequencer_Py\HyperGUI_v3.py", line 142, in OnBeginDrag
    dndData = DnD(text)
TypeError: this constructor takes no arguments

The DnD class will be a holder for the data that I'll be passing across. I'm quite new to Python so I've probably just overlooked something.. ? Can anyone spot it?

Thanks very much,

Jon

ยทยทยท

-------- Original Message --------

Jon <Jon <at> digital-explosion.co.uk> writes:

Started dragging
Traceback (most recent call last):
  File "D:\HyperLight Online\v4.0\HyperSequencer_Py\HyperGUI_v3.py",
line 142, in OnBeginDrag
    dndData = DnD(text)
TypeError: this constructor takes no arguments

The DnD class will be a holder for the data that I'll be passing across.
I'm quite new to Python so I've probably just overlooked something.. ?
Can anyone spot it?

It's due to a typo in the DnD class. It should be '__init__', not '__init_'.

Christian