Hi All, I'm trying to do drag&drop onto a wx.grid, the grid has a
virtual table... the problem is when I drop and try to get the cell, I
always get a coord -1,-1
any ideas??
sample code:
import wx
import wx.grid
import json
class MyFrame1 ( wx.Frame ):
def __init__( self, parent ):
wx.Frame.__init__ ( self, parent, id = wx.ID_ANY, title =
wx.EmptyString, pos = wx.DefaultPosition, size = wx.Size( 500,300 ),
style = wx.DEFAULT_FRAME_STYLE|wx.TAB_TRAVERSAL )
self.SetSizeHintsSz( wx.DefaultSize, wx.DefaultSize )
bSizer1 = wx.BoxSizer( wx.VERTICAL )
self.listc = wx.ListCtrl( self, wx.ID_ANY, wx.DefaultPosition,
wx.DefaultSize, wx.LC_REPORT|wx.LC_SINGLE_SEL )
bSizer1.Add( self.listc, 0, wx.ALL|wx.EXPAND, 5 )
self.m_grid1 = wx.grid.Grid( self, wx.ID_ANY, wx.DefaultPosition,
wx.DefaultSize, 0 )
# Grid
#self.m_grid1.CreateGrid( 5, 5 )
self.m_grid1.EnableEditing( True )
self.m_grid1.EnableGridLines( True )
self.m_grid1.EnableDragGridSize( False )
self.m_grid1.SetMargins( 0, 0 )
# Columns
self.m_grid1.EnableDragColMove( False )
self.m_grid1.EnableDragColSize( True )
self.m_grid1.SetColLabelSize( 30 )
self.m_grid1.SetColLabelAlignment( wx.ALIGN_CENTRE,
wx.ALIGN_CENTRE )
# Rows
self.m_grid1.EnableDragRowSize( True )
self.m_grid1.SetRowLabelSize( 80 )
self.m_grid1.SetRowLabelAlignment( wx.ALIGN_CENTRE,
wx.ALIGN_CENTRE )
# Label Appearance
# Cell Defaults
self.m_grid1.SetDefaultCellAlignment( wx.ALIGN_LEFT, wx.ALIGN_TOP )
bSizer1.Add( self.m_grid1, 0, wx.ALL, 5 )
self.SetSizer( bSizer1 )
self.Layout()
def __del__( self ):
pass
class tableDrop(wx.PyDropTarget):
def __init__(self, fn):
wx.PyDropTarget.__init__(self)
self.data = wx.PyTextDataObject()
self.fn = fn
self.SetDataObject(self.data)
def OnData(self, x, y, d):
if self.GetData():
obj = json.loads(str(self.data.GetText()))
self.fn(x, y, obj, d)
return d
class Frame(MyFrame1):
def __init__(self, *args, **kwargs):
MyFrame1.__init__(self, None)
self.vtable = TestTable(["one", "Two", "Three"])
self.m_grid1.SetTable(self.vtable)
self.m_grid1.SetDropTarget(tableDrop(self.OnDrop))
self.listc.InsertColumn(0, "column 1")
self.listc.InsertStringItem(2, unicode("value 1"))
self.listc.Bind(wx.EVT_LIST_BEGIN_DRAG, self.OnDragInit)
def OnDragInit(self, event):
index = self.listc.GetFocusedItem()
item = self.listc.GetItemText(index)
serialized = json.dumps(item)
tdo = wx.TextDataObject(serialized)
tds = wx.DropSource(self.listc)
tds.SetData(tdo)
tds.DoDragDrop(True)
def OnDrop(self, x, y, item, d):
cell = self.m_grid1.XYToCell(x, y)
print cell #always print -1, -1
class TestTable(wx.grid.PyGridTableBase):
'''
Virtual table
'''
def __init__(self, cols):
wx.grid.PyGridTableBase.__init__(self)
self.data = {}
self.rowLabels = [u"Row 1"]
self.colLabels = cols
def GetNumberRows(self):
return len(self.rowLabels)
def GetNumberCols(self):
return len(self.colLabels)
def IsEmptyCell(self, row, col):
return self.data.get((row, col)) is not None
def GetValue(self, row, col):
value = self.data.get((row, col))
if value:
return value
else:
return ''
def SetValueAsObject(self, row, col, value):
self.data[(row, col)] = value
def SetValue(self, row, col, value):
pass
'''
metodos que retornan y setean los labels de las filas y columnas
'''
def SetColLabelValue(self, col, value):
self.colLabels[col] = value
def GetColLabelValue(self, col):
return self.colLabels[col]
def GetRowLabelValue(self, row):
return self.rowLabels[row]
def SetRowLabelValue(self, row, value):
self.rowLabels[row] = value
app = wx.PySimpleApp()
frame = Frame(None)
frame.Show()
app.MainLoop()