Skip to content
Snippets Groups Projects
Commit 2353d5c9 authored by Paul McCarthy's avatar Paul McCarthy
Browse files

Support for displaying items in reverse order. I need to add some wrapper...

Support for displaying items in reverse order. I need to add some wrapper methods for adding/removing items, with reverse support.
parent b7c6f956
No related branches found
No related tags found
No related merge requests found
...@@ -65,6 +65,10 @@ ELB_NO_REMOVE = 2 ...@@ -65,6 +65,10 @@ ELB_NO_REMOVE = 2
# Do not allow items to be reordered. # Do not allow items to be reordered.
ELB_NO_MOVE = 4 ELB_NO_MOVE = 4
# The first item in the list (index 0) is shown
# at the botom, and the last item at the top.
ELB_REVERSE = 8
class EditableListBox(wx.Panel): class EditableListBox(wx.Panel):
""" """
An EditableListBox contains a ListBox containing some items, An EditableListBox contains a ListBox containing some items,
...@@ -88,23 +92,30 @@ class EditableListBox(wx.Panel): ...@@ -88,23 +92,30 @@ class EditableListBox(wx.Panel):
wx.Panel.__init__(self, parent) wx.Panel.__init__(self, parent)
reverseOrder = style & ELB_REVERSE
addSupport = not (style & ELB_NO_ADD) addSupport = not (style & ELB_NO_ADD)
removeSupport = not (style & ELB_NO_REMOVE) removeSupport = not (style & ELB_NO_REMOVE)
moveSupport = not (style & ELB_NO_MOVE) moveSupport = not (style & ELB_NO_MOVE)
noButtons = not any((addSupport, removeSupport, moveSupport)) noButtons = not any((addSupport, removeSupport, moveSupport))
self.reverseOrder = reverseOrder
if clientData is None: clientData = [None]*len(choices) if clientData is None: clientData = [None]*len(choices)
# The list box containing the list of items # The list box containing the list of items
self.listBox = wx.ListBox(self, style=wx.LB_SINGLE | wx.LB_NEEDED_SB) self.listBox = wx.ListBox(self, style=wx.LB_SINGLE | wx.LB_NEEDED_SB)
self.listBox.Bind(wx.EVT_LISTBOX, self._itemSelected) self.listBox.Bind(wx.EVT_LISTBOX, self._itemSelected)
for choice,data in zip(choices, clientData): items = zip(choices, clientData)
if reverseOrder:
items.reverse()
for choice,data in items:
self.listBox.Append(choice, data) self.listBox.Append(choice, data)
# A panel containing buttons for doing stuff with the list # A panel containing buttons for doing stuff with the list
if not noButtons: if not noButtons:
self.buttonPanel = wx.Panel(self) self.buttonPanel = wx.Panel(self)
self.buttonPanelSizer = wx.BoxSizer(wx.VERTICAL) self.buttonPanelSizer = wx.BoxSizer(wx.VERTICAL)
self.buttonPanel.SetSizer(self.buttonPanelSizer) self.buttonPanel.SetSizer(self.buttonPanelSizer)
...@@ -150,7 +161,8 @@ class EditableListBox(wx.Panel): ...@@ -150,7 +161,8 @@ class EditableListBox(wx.Panel):
""" """
Returns a 3-tuple containing the index, label, and associated client Returns a 3-tuple containing the index, label, and associated client
data of the currently selected list item, or (None, None, None) if data of the currently selected list item, or (None, None, None) if
no item is selected. no item is selected. If the ELB_REVERSE style is active, the index
is inverted.
""" """
idx = self.listBox.GetSelection() idx = self.listBox.GetSelection()
...@@ -163,6 +175,9 @@ class EditableListBox(wx.Panel): ...@@ -163,6 +175,9 @@ class EditableListBox(wx.Panel):
choice = self.listBox.GetString( idx) choice = self.listBox.GetString( idx)
data = self.listBox.GetClientData(idx) data = self.listBox.GetClientData(idx)
if (idx is not None) and self.reverseOrder:
idx = self.listBox.GetCount() - idx - 1
return idx, choice, data return idx, choice, data
...@@ -180,13 +195,16 @@ class EditableListBox(wx.Panel): ...@@ -180,13 +195,16 @@ class EditableListBox(wx.Panel):
wx.PostEvent(self, ev) wx.PostEvent(self, ev)
def _moveItem(self, oldIdx, newIdx): def _moveItem(self, offset):
""" """
Called when the 'move up' or 'move down' buttons are pushed. Moves Called when the 'move up' or 'move down' buttons are pushed. Moves
the selected item up or down and posts an EVT_ELB_MOVE_EVENT, unless the selected item by the specified offset and posts an
it doesn't make sense to do so. EVT_ELB_MOVE_EVENT, unless it doesn't make sense to do the move.
""" """
oldIdx = self.listBox.GetSelection()
newIdx = oldIdx + offset
# nothing is selected, or the selected # nothing is selected, or the selected
# item is at the top/bottom of the list. # item is at the top/bottom of the list.
if oldIdx == wx.NOT_FOUND: return if oldIdx == wx.NOT_FOUND: return
...@@ -200,6 +218,10 @@ class EditableListBox(wx.Panel): ...@@ -200,6 +218,10 @@ class EditableListBox(wx.Panel):
self.listBox.Insert(choice, newIdx, data) self.listBox.Insert(choice, newIdx, data)
self.listBox.SetSelection(newIdx) self.listBox.SetSelection(newIdx)
if self.reverseOrder:
oldIdx = self.listBox.GetCount() - oldIdx - 1
newIdx = self.listBox.GetCount() - newIdx - 1
log.debug('ListMoveEvent (oldIdx: {}; newIdx: {}; choice: {})'.format( log.debug('ListMoveEvent (oldIdx: {}; newIdx: {}; choice: {})'.format(
oldIdx, newIdx, choice)) oldIdx, newIdx, choice))
...@@ -213,9 +235,7 @@ class EditableListBox(wx.Panel): ...@@ -213,9 +235,7 @@ class EditableListBox(wx.Panel):
Called when the 'move down' button is pushed. Calls the _moveItem Called when the 'move down' button is pushed. Calls the _moveItem
method. method.
""" """
oldIdx = self.listBox.GetSelection() self._moveItem(1)
newIdx = oldIdx+1
self._moveItem(oldIdx, newIdx)
def _moveItemUp(self, ev): def _moveItemUp(self, ev):
...@@ -223,9 +243,7 @@ class EditableListBox(wx.Panel): ...@@ -223,9 +243,7 @@ class EditableListBox(wx.Panel):
Called when the 'move up' button is pushed. Calls the _moveItem Called when the 'move up' button is pushed. Calls the _moveItem
method. method.
""" """
oldIdx = self.listBox.GetSelection() self._moveItem(-1)
newIdx = oldIdx-1
self._moveItem(oldIdx, newIdx)
def _addItem(self, ev): def _addItem(self, ev):
...@@ -280,7 +298,7 @@ def main(items): ...@@ -280,7 +298,7 @@ def main(items):
app = wx.App() app = wx.App()
frame = wx.Frame(None) frame = wx.Frame(None)
panel = wx.Panel(frame) panel = wx.Panel(frame)
listbox = EditableListBox(panel, items) listbox = EditableListBox(panel, items, style=ELB_REVERSE)
panelSizer = wx.BoxSizer(wx.HORIZONTAL) panelSizer = wx.BoxSizer(wx.HORIZONTAL)
panel.SetSizer(panelSizer) panel.SetSizer(panelSizer)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment