/** * @RDE_HEADER@ * Implementation of CheckStateProxyModel * $Id$ * $HeadURL$ * $Author$ * Created by hjl/Christian Froebel, NLE, 2019-05-10 */ #include "check_state_proxy_model.h" namespace LtsDataView { Qt::ItemFlags CheckStateProxyModel::flags(const QModelIndex &index) const { if (!index.isValid()) return sourceModel()->flags(index); // delegate to source model for further processing if necessary // Sets only the checkbox nodes on user checkable, // checkbox leaves must be handled in source model. QModelIndex sourceIndex = mapToSource(index); Qt::ItemFlags fl = sourceModel()->flags(sourceIndex); if (index.column() == 0) return fl | Qt::ItemIsUserCheckable; return fl; } QVariant CheckStateProxyModel::data(const QModelIndex &index, int role) const { if (!index.isValid()) return QVariant(); QModelIndex sourceIndex = mapToSource(index); // delegate everything except CheckStateRole to source model if (role != Qt::CheckStateRole) return sourceModel()->data(sourceIndex, role); // delegate a leaf too with CheckStateRole to source model if (isLeaf(index)) return sourceModel()->data(sourceIndex, role); // delegate to source model for further processing of further columns if necessary if (index.column() != 0) return sourceModel()->data(sourceIndex, role); // sets the node checkboxes and decoration depending on the entries in the tree structure int checkedCount = 0; int childCount = rowCount(index); for (int childRow = 0; childRow < childCount; ++childRow) { QModelIndex childIndex = this->index(childRow, 0, index); Qt::CheckState checkState = data(childIndex, Qt::CheckStateRole).value(); if (checkState == Qt::PartiallyChecked) return Qt::PartiallyChecked; if (checkState == Qt::Checked) ++checkedCount; } if (checkedCount == childCount) return Qt::Checked; else if (checkedCount == 0) return Qt::Unchecked; else return Qt::PartiallyChecked; } bool CheckStateProxyModel::setData(const QModelIndex &index, const QVariant &value, int role) { if (!index.isValid()) return false; QModelIndex sourceIndex = mapToSource(index); // delegate to source model for further processing of further columns if necessary if (index.column() != 0) return sourceModel()->setData(sourceIndex, value, role); // delegate everything except CheckStateRole to source mode if (role != Qt::CheckStateRole) return sourceModel()->setData(sourceIndex, value, role); if (isLeaf(index)) { // delegate a leaf to source model sourceModel()->setData(sourceIndex, value, role); // update parents recursively here QModelIndex idx = index.parent(); do { emit dataChanged(idx, idx, {Qt::CheckStateRole}); idx = idx.parent(); } while (idx.isValid()); return true; } // update underlying children recursively (Additional nodes with leaves may also exist) int childCount = rowCount(index); for (int childRow = 0; childRow < childCount; ++childRow) { QModelIndex childIndex = this->index(childRow, 0, index); setData(childIndex, value, Qt::CheckStateRole); } return true; } bool CheckStateProxyModel::isLeaf(const QModelIndex &index) const { QModelIndex sourceIndex = mapToSource(index); return sourceModel()->flags(sourceIndex).testFlag(Qt::ItemIsUserCheckable); } } // namespace InfrastructureManagement