Skip to content

Commit

Permalink
Merge pull request #283 from ales-erjavec/fixes/widget-tool-box-model…
Browse files Browse the repository at this point in the history
…-reset

[FIX] widgettoolbox: Fix WidgetToolBox tests with Python 3.12
  • Loading branch information
janezd committed Oct 13, 2023
2 parents 88883cb + 1ecdf6d commit 636c6b3
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
5 changes: 5 additions & 0 deletions orangecanvas/application/tests/test_widgettoolbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ def setUp(self):
reg = registry_tests.small_testing_registry()
self.reg = QtWidgetRegistry(reg)

def tearDown(self):
self.reg.model().clear()
del self.reg
super().tearDown()

def test_widgettoolgrid(self):
w = QWidget()
layout = QHBoxLayout()
Expand Down
17 changes: 15 additions & 2 deletions orangecanvas/application/widgettoolbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ def setModel(self, model, rootIndex=QModelIndex()):
if self.__model is not None:
self.__model.rowsInserted.disconnect(self.__on_rowsInserted)
self.__model.rowsRemoved.disconnect(self.__on_rowsRemoved)
self.__model.modelReset.disconnect(self.__on_modelReset)
self.__model = None

self.__model = model
Expand All @@ -116,6 +117,7 @@ def setModel(self, model, rootIndex=QModelIndex()):
if self.__model is not None:
self.__model.rowsInserted.connect(self.__on_rowsInserted)
self.__model.rowsRemoved.connect(self.__on_rowsRemoved)
self.__model.modelReset.connect(self.__on_modelReset)

self.__initFromModel(model, rootIndex)

Expand Down Expand Up @@ -214,8 +216,14 @@ def __on_rowsRemoved(self, parent, start, end):
if parent == QModelIndex(self.__rootIndex):
actions = self.actions()
actions = actions[start: end + 1]
for action in actions:
self.removeAction(action)
self.__removeActions(actions)

def __on_modelReset(self):
self.__removeActions(self.actions())

def __removeActions(self, actions: Iterable[QAction]):
for action in actions:
self.removeAction(action)

def __startDrag(self, button):
# type: (QToolButton) -> None
Expand Down Expand Up @@ -297,6 +305,7 @@ def __init__(self, parent=None):
self.__proxyModel.dataChanged.connect(self.__on_dataChanged)
self.__proxyModel.rowsInserted.connect(self.__on_rowsInserted)
self.__proxyModel.rowsRemoved.connect(self.__on_rowsRemoved)
self.__proxyModel.modelReset.connect(self.__on_modelReset)

self.__iconSize = QSize(25, 25)
self.__buttonSize = QSize(50, 50)
Expand Down Expand Up @@ -500,6 +509,10 @@ def __on_rowsRemoved(self, parent, start, end):
for i in reversed(range(start, end + 1)):
self.removeItem(i)

def __on_modelReset(self):
for i in reversed(range(self.count())):
self.removeItem(i)

def __on_filterTextChanged(self, text: str) -> None:
def acceptable(desc: Optional[WidgetDescription]) -> bool:
if desc is not None:
Expand Down

0 comments on commit 636c6b3

Please sign in to comment.