2013-06-23 01:34:41 +00:00
|
|
|
# Created By: Virgil Dupras
|
|
|
|
# Created On: 2011-09-06
|
2015-01-03 21:30:57 +00:00
|
|
|
# Copyright 2015 Hardcoded Software (http://www.hardcoded.net)
|
2014-07-01 12:30:56 +00:00
|
|
|
#
|
2015-01-03 21:33:16 +00:00
|
|
|
# This software is licensed under the "GPLv3" License as described in the "LICENSE" file,
|
2014-07-01 12:30:56 +00:00
|
|
|
# which should be included with this package. The terms are also available at
|
2015-01-03 21:33:16 +00:00
|
|
|
# http://www.gnu.org/licenses/gpl-3.0.html
|
2013-06-23 01:34:41 +00:00
|
|
|
|
2013-10-20 19:15:09 +00:00
|
|
|
from PyQt5.QtCore import Qt, QAbstractListModel, QItemSelection, QItemSelectionModel
|
2013-06-23 01:34:41 +00:00
|
|
|
|
|
|
|
class SelectableList(QAbstractListModel):
|
2013-10-20 19:53:59 +00:00
|
|
|
def __init__(self, model, view, **kwargs):
|
|
|
|
super().__init__(**kwargs)
|
2013-06-23 01:34:41 +00:00
|
|
|
self._updating = False
|
|
|
|
self.view = view
|
|
|
|
self.model = model
|
|
|
|
self.view.setModel(self)
|
|
|
|
self.model.view = self
|
2014-07-01 12:30:56 +00:00
|
|
|
|
2013-06-23 01:34:41 +00:00
|
|
|
#--- Override
|
|
|
|
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
|
2014-07-01 12:30:56 +00:00
|
|
|
|
2013-06-23 01:34:41 +00:00
|
|
|
def rowCount(self, index):
|
|
|
|
if index.isValid():
|
|
|
|
return 0
|
|
|
|
return len(self.model)
|
2014-07-01 12:30:56 +00:00
|
|
|
|
2013-06-23 01:34:41 +00:00
|
|
|
#--- Virtual
|
|
|
|
def _updateSelection(self):
|
|
|
|
raise NotImplementedError()
|
2014-07-01 12:30:56 +00:00
|
|
|
|
2013-06-23 01:34:41 +00:00
|
|
|
def _restoreSelection(self):
|
|
|
|
raise NotImplementedError()
|
2014-07-01 12:30:56 +00:00
|
|
|
|
2013-06-23 01:34:41 +00:00
|
|
|
#--- model --> view
|
|
|
|
def refresh(self):
|
|
|
|
self._updating = True
|
2014-07-01 12:30:56 +00:00
|
|
|
self.beginResetModel()
|
|
|
|
self.endResetModel()
|
2013-06-23 01:34:41 +00:00
|
|
|
self._updating = False
|
|
|
|
self._restoreSelection()
|
2014-07-01 12:30:56 +00:00
|
|
|
|
2013-06-23 01:34:41 +00:00
|
|
|
def update_selection(self):
|
|
|
|
self._restoreSelection()
|
|
|
|
|
|
|
|
class ComboboxModel(SelectableList):
|
2013-10-20 19:53:59 +00:00
|
|
|
def __init__(self, model, view, **kwargs):
|
|
|
|
super().__init__(model, view, **kwargs)
|
2013-06-23 01:34:41 +00:00
|
|
|
self.view.currentIndexChanged[int].connect(self.selectionChanged)
|
2014-07-01 12:30:56 +00:00
|
|
|
|
2013-06-23 01:34:41 +00:00
|
|
|
#--- Override
|
|
|
|
def _updateSelection(self):
|
|
|
|
index = self.view.currentIndex()
|
|
|
|
if index != self.model.selected_index:
|
|
|
|
self.model.select(index)
|
2014-07-01 12:30:56 +00:00
|
|
|
|
2013-06-23 01:34:41 +00:00
|
|
|
def _restoreSelection(self):
|
|
|
|
index = self.model.selected_index
|
|
|
|
if index is not None:
|
|
|
|
self.view.setCurrentIndex(index)
|
2014-07-01 12:30:56 +00:00
|
|
|
|
2013-06-23 01:34:41 +00:00
|
|
|
#--- Events
|
|
|
|
def selectionChanged(self, index):
|
|
|
|
if not self._updating:
|
|
|
|
self._updateSelection()
|
|
|
|
|
|
|
|
class ListviewModel(SelectableList):
|
2013-10-20 19:53:59 +00:00
|
|
|
def __init__(self, model, view, **kwargs):
|
|
|
|
super().__init__(model, view, **kwargs)
|
2013-06-23 01:34:41 +00:00
|
|
|
self.view.selectionModel().selectionChanged[(QItemSelection, QItemSelection)].connect(
|
|
|
|
self.selectionChanged)
|
2014-07-01 12:30:56 +00:00
|
|
|
|
2013-06-23 01:34:41 +00:00
|
|
|
#--- Override
|
|
|
|
def _updateSelection(self):
|
|
|
|
newIndexes = [modelIndex.row() for modelIndex in self.view.selectionModel().selectedRows()]
|
|
|
|
if newIndexes != self.model.selected_indexes:
|
|
|
|
self.model.select(newIndexes)
|
2014-07-01 12:30:56 +00:00
|
|
|
|
2013-06-23 01:34:41 +00:00
|
|
|
def _restoreSelection(self):
|
|
|
|
newSelection = QItemSelection()
|
|
|
|
for index in self.model.selected_indexes:
|
|
|
|
newSelection.select(self.createIndex(index, 0), self.createIndex(index, 0))
|
|
|
|
self.view.selectionModel().select(newSelection, QItemSelectionModel.ClearAndSelect)
|
|
|
|
if len(newSelection.indexes()):
|
|
|
|
currentIndex = newSelection.indexes()[0]
|
|
|
|
self.view.selectionModel().setCurrentIndex(currentIndex, QItemSelectionModel.Current)
|
|
|
|
self.view.scrollTo(currentIndex)
|
|
|
|
#--- Events
|
|
|
|
def selectionChanged(self, index):
|
|
|
|
if not self._updating:
|
|
|
|
self._updateSelection()
|
2014-07-01 12:30:56 +00:00
|
|
|
|