qtlib: Fix broken SelectableList

It was still using `.reset()`, which disappeared in Qt5.

Fixes #254.
This commit is contained in:
Virgil Dupras 2014-07-01 08:30:56 -04:00
parent 8434befe1f
commit 9e9e73aa6b
1 changed files with 17 additions and 16 deletions

View File

@ -1,9 +1,9 @@
# Created By: Virgil Dupras
# Created On: 2011-09-06
# Copyright 2014 Hardcoded Software (http://www.hardcoded.net)
#
# This software is licensed under the "BSD" License as described in the "LICENSE" file,
# which should be included with this package. The terms are also available at
#
# This software is licensed under the "BSD" License as described in the "LICENSE" file,
# which should be included with this package. The terms are also available at
# http://www.hardcoded.net/licenses/bsd_license
from PyQt5.QtCore import Qt, QAbstractListModel, QItemSelection, QItemSelectionModel
@ -16,7 +16,7 @@ class SelectableList(QAbstractListModel):
self.model = model
self.view.setModel(self)
self.model.view = self
#--- Override
def data(self, index, role):
if not index.isValid():
@ -25,26 +25,27 @@ class SelectableList(QAbstractListModel):
if role in {Qt.DisplayRole, Qt.EditRole}:
return self.model[index.row()]
return None
def rowCount(self, index):
if index.isValid():
return 0
return len(self.model)
#--- Virtual
def _updateSelection(self):
raise NotImplementedError()
def _restoreSelection(self):
raise NotImplementedError()
#--- model --> view
def refresh(self):
self._updating = True
self.reset()
self.beginResetModel()
self.endResetModel()
self._updating = False
self._restoreSelection()
def update_selection(self):
self._restoreSelection()
@ -52,18 +53,18 @@ class ComboboxModel(SelectableList):
def __init__(self, model, view, **kwargs):
super().__init__(model, view, **kwargs)
self.view.currentIndexChanged[int].connect(self.selectionChanged)
#--- Override
def _updateSelection(self):
index = self.view.currentIndex()
if index != self.model.selected_index:
self.model.select(index)
def _restoreSelection(self):
index = self.model.selected_index
if index is not None:
self.view.setCurrentIndex(index)
#--- Events
def selectionChanged(self, index):
if not self._updating:
@ -74,13 +75,13 @@ class ListviewModel(SelectableList):
super().__init__(model, view, **kwargs)
self.view.selectionModel().selectionChanged[(QItemSelection, QItemSelection)].connect(
self.selectionChanged)
#--- Override
def _updateSelection(self):
newIndexes = [modelIndex.row() for modelIndex in self.view.selectionModel().selectedRows()]
if newIndexes != self.model.selected_indexes:
self.model.select(newIndexes)
def _restoreSelection(self):
newSelection = QItemSelection()
for index in self.model.selected_indexes:
@ -94,4 +95,4 @@ class ListviewModel(SelectableList):
def selectionChanged(self, index):
if not self._updating:
self._updateSelection()