Drag and Drop with custom data - example required

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!

Thanks,
Jon

The information contained in this E-Mail and any subsequent
correspondence is private and is intended solely for the intended
recipient(s). The information in this communication may be confidential
and/or legally privileged. Nothing in this e-mail is intended to
conclude a contract on behalf of QinetiQ or make QinetiQ subject to any
other legally binding commitments, unless the e-mail contains an express
statement to the contrary or incorporates a formal Purchase Order.

For those other than the recipient any disclosure, copying,
distribution, or any action taken or omitted to be taken in reliance on
such information is prohibited and may be unlawful.

Emails and other electronic communication with QinetiQ may be monitored
and recorded for business purposes including security, audit and
archival purposes. Any response to this email indicates consent to
this.

Telephone calls to QinetiQ may be monitored or recorded for quality
control, security and other business purposes.

QinetiQ Group plc,

Company Registration No: 4586941,

Registered office: 85 Buckingham Gate, London SW1E 6PD

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.

···

_________________________________________
Andrea Gavana (gavana@kpo.kz)
Reservoir Engineer
KPDL
4, Millbank
SW1P 3JA London

Direct Tel: +44 (0) 20 717 08936
Mobile Tel: +44 (0) 77 487 70534
Fax: +44 (0) 20 717 08900
Web: http://xoomer.virgilio.it/infinity77
¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯

Jon Cage wrote:

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!

Have you looked at the CustomDragAndDrop module in the demo?

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!