Commit d25f7673 authored by Valentin Valls's avatar Valentin Valls
Browse files

Create ProxyColumnModel

parent 039baf7b
......@@ -16,6 +16,99 @@ from . import delegates
_logger = logging.getLogger(__name__)
class ProxyColumnModel(qt.QIdentityProxyModel):
"""Provides proxyfied multi columns pointing to the same source column.
"""
def __init__(self, parent=None):
qt.QIdentityProxyModel.__init__(self, parent=parent)
self.__columns = 0
self.__columnTitle = {}
self.__columnEditor = set()
def setColumn(self, columnId: int, title: str):
"""Define a column to this model.
This new column will point to the first column of the source model.
"""
self.beginResetModel()
self.__columns = max(self.__columns, columnId + 1)
self.__columnTitle[columnId] = title
self.endResetModel()
def setColumnEditor(self, columnId: int, editor: bool):
if editor:
self.__columnEditor.add(columnId)
else:
self.__columnEditor.discard(columnId)
def data(self, index: qt.QModelIndex, role: int = qt.Qt.DisplayRole):
if index.isValid():
if role == qt.Qt.DisplayRole:
if index.column() in self.__columnEditor:
return ""
return qt.QIdentityProxyModel.data(self, index, role)
def object(self, index: qt.QModelIndex):
return self.data(index, role=delegates.ObjectRole)
def columnCount(self, parent: qt.QModelIndex = qt.QModelIndex()):
return self.__columns
def rowCount(self, parent: qt.QModelIndex = qt.QModelIndex()):
sourceModel = self.sourceModel()
if sourceModel is None:
return 0
parent = self.mapToSource(parent)
result = sourceModel.rowCount(parent)
return result
def index(self, row, column, parent=qt.QModelIndex()):
if column != 0:
firstCol = self.index(row, 0, parent)
return self.createIndex(row, column, firstCol.internalPointer())
return super(ProxyColumnModel, self).index(row, column, parent)
def parent(self, child):
if not child.isValid():
return qt.QModelIndex()
if child.column() != 0:
child = self.createIndex(child.row(), 0, child.internalPointer())
return super(ProxyColumnModel, self).parent(child)
def mapFromSource(self, sourceIndex: qt.QModelIndex) -> qt.QModelIndex:
if not sourceIndex.isValid():
return qt.QModelIndex()
if sourceIndex.column() != 0:
return qt.QModelIndex()
return super(ProxyColumnModel, self).mapFromSource(sourceIndex)
def mapToSource(self, proxyIndex: qt.QModelIndex) -> qt.QModelIndex:
if not proxyIndex.isValid():
return qt.QModelIndex()
if proxyIndex.column() != 0:
proxyIndex = self.createIndex(
proxyIndex.row(), 0, proxyIndex.internalPointer()
)
return super(ProxyColumnModel, self).mapToSource(proxyIndex)
def headerData(
self,
section: int,
orientation: qt.Qt.Orientation,
role: int = qt.Qt.DisplayRole,
):
if role == qt.Qt.DisplayRole:
if orientation == qt.Qt.Horizontal:
return self.__columnTitle.get(section, str(section))
sourceModel = self.sourceModel()
if sourceModel is None:
return None
if orientation == qt.Qt.Horizontal:
return sourceModel.headerData(0, orientation, role)
return sourceModel.headerData(section, orientation, role)
class ObjectListModel(qt.QAbstractItemModel):
"""Store a list of object as a Qt model.
......@@ -128,6 +221,19 @@ class VDataTableView(qt.QTableView):
model.modelReset.connect(self.__modelWasReset)
self.__modelWasReset()
def indexToView(self, index, column=None):
"""Make sure an index can be used by the view."""
if index.model() is self.__proxyModel.sourceModel():
index = self.__proxyModel.mapFromSource(index)
if column is not None:
parent = index.parent()
index = self.__proxyModel.index(index.row(), column, parent)
return index
def indexWidget(self, index, column=None):
index = self.indexToView(index, column=column)
return super(VDataTableView, self).indexWidget(index)
def __modelWasReset(self):
"""
Enforce that each editors are open.
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment