1
0
mirror of https://github.com/arsenetar/dupeguru.git synced 2025-05-11 19:19:49 +00:00

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

View File

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