From 2f7e14a05d3ce960447e500c67b2da4138a1c4a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C4=81ris=20Narti=C5=A1s?= Date: Fri, 5 Aug 2022 17:55:04 +0300 Subject: [PATCH] GUI: looping over GetSelections() results in removal of all list items (#2511) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * GUI: wx.ListBox is dynamic – looping over GetSelections() results in removal of all list items When the last item returned by GetSelections() has been .Delete()'d a new item is selected. The newly selected item is added to GetSelections(). If user selects the last item from a list, looping over GetSelections() output eventually will remove all list items not only ones selected by the user. This is a workaround by freezing user selection in a list and iterating it in a reverse order as each .Delete() call updates the ListBox and thus changes positions of selected items. Reverse traversal ensures that no selected item gets a new position in the list. --- gui/wxpython/gui_core/dialogs.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/gui/wxpython/gui_core/dialogs.py b/gui/wxpython/gui_core/dialogs.py index dabf9e17930..b625d340a7a 100644 --- a/gui/wxpython/gui_core/dialogs.py +++ b/gui/wxpython/gui_core/dialogs.py @@ -1107,8 +1107,13 @@ def OnAddLayer(self, event): def OnRemoveLayer(self, event): """Remove layer from listbox""" - while self.gLayerBox.GetSelections(): - sel = self.gLayerBox.GetSelections()[0] + # After removal of last selected item by .Delete, + # ListBox selects the last of remaining items in the list + # and thus adds a new item to GetSelections + # Items are removed in reverse order to maintain positional number + # of other selected items (ListBox is dynamic!) + selections = sorted(self.gLayerBox.GetSelections(), reverse=True) + for sel in selections: m = self.gLayerBox.GetString(sel) self.gLayerBox.Delete(sel) self.gmaps.remove(m)