Last active
October 12, 2015 10:48
-
-
Save tim-patterson/4015673 to your computer and use it in GitHub Desktop.
Wxpython Dnd listctrl test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import wx | |
import sys | |
from wx._core import EVT_LEFT_UP | |
class MainWindow(wx.Frame): | |
def __init__(self): | |
wx.Frame.__init__(self, None, title='Python Listbox test') | |
self.setup_menu() | |
self.setup_list() | |
def setup_menu(self): | |
menu_bar = wx.MenuBar() | |
file_menu = wx.Menu() | |
self.menu_exit = file_menu.Append(wx.ID_EXIT, 'Bye', "quits the application") | |
self.SetMenuBar(menu_bar) | |
menu_bar.Append(file_menu, '&File') | |
def setup_list(self): | |
self.listbox = DraggableListCtrl(self) | |
class MainWindowController(object): | |
def __init__(self): | |
self.window = MainWindow() | |
self.register_listeners() | |
self.populate_list() | |
self.window.Show() | |
def register_listeners(self): | |
self.window.Bind(wx.EVT_MENU, self.quit) | |
def populate_list(self): | |
self.ds = ListDataSource(range(100)) | |
self.window.listbox.set_data_source(self.ds) | |
self.window.listbox.InsertColumn(0, 'MyList') | |
self.window.listbox.refresh() | |
def quit(self, event): | |
sys.exit() | |
class ListDataSource(object): | |
def __init__(self, items): | |
self.items = items | |
def getItemCount(self): | |
return len(self.items) | |
def getItemText(self, item_idx, col_idx): | |
return self.items[item_idx] | |
def move_item(self, from_idx, to_idx): | |
item = self.items[from_idx] | |
self.items[:] = self.items[:from_idx] + self.items[(from_idx+1):] | |
if to_idx < from_idx: | |
to_idx = to_idx +1 | |
self.items.insert(to_idx, item) | |
return to_idx | |
class DraggableListCtrl(wx.ListCtrl): | |
def __init__(self, parent): | |
wx.ListCtrl.__init__(self, parent, style=wx.LC_REPORT | wx.LC_VIRTUAL | wx.LC_SINGLE_SEL) | |
self.Bind(wx.EVT_LIST_BEGIN_DRAG, self._start_drag) | |
def set_data_source(self, data_source): | |
self.data_source = data_source | |
def refresh(self): | |
self.SetItemCount(self.data_source.getItemCount()) | |
self.Refresh() | |
def OnGetItemText(self, item_idx, col_idx): | |
return self.data_source.getItemText(item_idx, col_idx) | |
def _start_drag(self, event): | |
self.drag_start = event.Index | |
self.Bind(EVT_LEFT_UP, handler=self._finish_drag) | |
def _finish_drag(self, event): | |
drag_end = self.HitTest(event.Position)[0] | |
final_idx = self.data_source.move_item(self.drag_start, drag_end) | |
self.refresh() | |
self.Focus(final_idx) | |
self.Select(final_idx) | |
self.Unbind(EVT_LEFT_UP, handler=self._finish_drag) | |
class ListDropTarget(wx.TextDropTarget): | |
def OnDropText(self, x, y, text): | |
print text | |
app = wx.App() | |
controller = MainWindowController() | |
app.MainLoop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment