From ad87706d47534717652325e953734773a9625d1b Mon Sep 17 00:00:00 2001 From: Maxime Costalonga Date: Thu, 16 Sep 2021 23:57:32 +0100 Subject: [PATCH] ENH: Provides control on item enability and selectability in data models --- pyface/data_view/abstract_data_model.py | 38 +++++++++++++++++++ .../ui/qt4/data_view/data_view_item_model.py | 16 +++++--- 2 files changed, 48 insertions(+), 6 deletions(-) diff --git a/pyface/data_view/abstract_data_model.py b/pyface/data_view/abstract_data_model.py index c75a657ea..75167311a 100644 --- a/pyface/data_view/abstract_data_model.py +++ b/pyface/data_view/abstract_data_model.py @@ -235,6 +235,44 @@ def get_value_type(self, row, column): """ raise NotImplementedError() + def is_enabled(self, row, column): + """ Whether the item located at row and column is enabled. + + The default method returns True. + + Parameters + ---------- + row : sequence of int + The indices of the row as a sequence from root to leaf. + column : sequence of int + The indices of the column as a sequence of length 0 or 1. + + Returns + ------- + can_set_value : bool + Whether or not the item is enabled. + """ + return True + + def is_selectable(self, row, column): + """ Whether the item located at row and column is selectable. + + The default method returns True. + + Parameters + ---------- + row : sequence of int + The indices of the row as a sequence from root to leaf. + column : sequence of int + The indices of the column as a sequence of length 0 or 1. + + Returns + ------- + can_set_value : bool + Whether or not the item is selectable. + """ + return True + # Convenience methods def is_row_valid(self, row): diff --git a/pyface/ui/qt4/data_view/data_view_item_model.py b/pyface/ui/qt4/data_view/data_view_item_model.py index f57414378..4ba46a587 100644 --- a/pyface/ui/qt4/data_view/data_view_item_model.py +++ b/pyface/ui/qt4/data_view/data_view_item_model.py @@ -142,14 +142,18 @@ def flags(self, index): row = self._to_row_index(index) column = self._to_column_index(index) value_type = self.model.get_value_type(row, column) - if row == () and column == (): - return Qt.ItemIsEnabled - - flags = Qt.ItemIsEnabled | Qt.ItemIsSelectable | Qt.ItemIsDragEnabled - if is_qt5 and not self.model.can_have_children(row): - flags |= Qt.ItemNeverHasChildren + flags = Qt.ItemIsDragEnabled try: + if self.model.is_enabled(row, column): + flags |= Qt.ItemIsEnabled + + if self.model.is_selectable(row, column): + flags |= Qt.ItemIsSelectable + + if is_qt5 and not self.model.can_have_children(row): + flags |= Qt.ItemNeverHasChildren + if value_type: if value_type.has_editor_value(self.model, row, column): flags |= Qt.ItemIsEditable