1
0
mirror of https://github.com/arsenetar/dupeguru.git synced 2024-10-31 22:05:58 +00:00
dupeguru/qt/selectable_list.py

101 lines
3.3 KiB
Python
Raw Normal View History

2019-09-10 00:54:28 +00:00
# Created By: Virgil Dupras
# Created On: 2011-09-06
# Copyright 2015 Hardcoded Software (http://www.hardcoded.net)
#
# This software is licensed under the "GPLv3" License as described in the "LICENSE" file,
# which should be included with this package. The terms are also available at
# http://www.gnu.org/licenses/gpl-3.0.html
from PyQt5.QtCore import Qt, QAbstractListModel, QItemSelection, QItemSelectionModel
2019-09-10 00:54:28 +00:00
class SelectableList(QAbstractListModel):
def __init__(self, model, view, **kwargs):
super().__init__(**kwargs)
self._updating = False
self.view = view
self.model = model
self.view.setModel(self)
self.model.view = self
# --- Override
2019-09-10 00:54:28 +00:00
def data(self, index, role):
if not index.isValid():
return None
# We need EditRole for QComboBoxes with setEditable(True)
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
2019-09-10 00:54:28 +00:00
def _updateSelection(self):
raise NotImplementedError()
def _restoreSelection(self):
raise NotImplementedError()
# --- model --> view
2019-09-10 00:54:28 +00:00
def refresh(self):
self._updating = True
self.beginResetModel()
self.endResetModel()
self._updating = False
self._restoreSelection()
def update_selection(self):
self._restoreSelection()
2019-09-10 00:54:28 +00:00
class ComboboxModel(SelectableList):
def __init__(self, model, view, **kwargs):
super().__init__(model, view, **kwargs)
self.view.currentIndexChanged[int].connect(self.selectionChanged)
# --- Override
2019-09-10 00:54:28 +00:00
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
2019-09-10 00:54:28 +00:00
def selectionChanged(self, index):
if not self._updating:
self._updateSelection()
2019-09-10 00:54:28 +00:00
class ListviewModel(SelectableList):
def __init__(self, model, view, **kwargs):
super().__init__(model, view, **kwargs)
self.view.selectionModel().selectionChanged[(QItemSelection, QItemSelection)].connect(self.selectionChanged)
2019-09-10 00:54:28 +00:00
# --- Override
2019-09-10 00:54:28 +00:00
def _updateSelection(self):
2021-08-24 05:12:23 +00:00
new_indexes = [modelIndex.row() for modelIndex in self.view.selectionModel().selectedRows()]
if new_indexes != self.model.selected_indexes:
self.model.select(new_indexes)
2019-09-10 00:54:28 +00:00
def _restoreSelection(self):
2021-08-24 05:12:23 +00:00
new_selection = QItemSelection()
2019-09-10 00:54:28 +00:00
for index in self.model.selected_indexes:
2021-08-24 05:12:23 +00:00
new_selection.select(self.createIndex(index, 0), self.createIndex(index, 0))
self.view.selectionModel().select(new_selection, QItemSelectionModel.ClearAndSelect)
if len(new_selection.indexes()):
current_index = new_selection.indexes()[0]
self.view.selectionModel().setCurrentIndex(current_index, QItemSelectionModel.Current)
self.view.scrollTo(current_index)
# --- Events
2019-09-10 00:54:28 +00:00
def selectionChanged(self, index):
if not self._updating:
self._updateSelection()