From 85cd33715723f294abeeb8a0f3f77c0de27f6a37 Mon Sep 17 00:00:00 2001 From: Mikaƫl Capelle Date: Mon, 4 Jan 2021 19:06:43 +0100 Subject: Disable proxies when they are not used. --- src/qtgroupingproxy.cpp | 74 +++++++++++++++++++++++++++++-------------------- 1 file changed, 44 insertions(+), 30 deletions(-) (limited to 'src/qtgroupingproxy.cpp') diff --git a/src/qtgroupingproxy.cpp b/src/qtgroupingproxy.cpp index 2c997fe9..3daa66ba 100644 --- a/src/qtgroupingproxy.cpp +++ b/src/qtgroupingproxy.cpp @@ -34,42 +34,50 @@ using namespace MOBase; \ingroup model-view */ -QtGroupingProxy::QtGroupingProxy(QAbstractItemModel *model, QModelIndex rootNode, int groupedColumn, int groupedRole, unsigned int flags , int aggregateRole) +QtGroupingProxy::QtGroupingProxy(QModelIndex rootNode, int groupedColumn, int groupedRole, unsigned int flags, int aggregateRole) : QAbstractProxyModel() - , m_rootNode( rootNode ) - , m_groupedColumn( 0 ) - , m_groupedRole( groupedRole ) - , m_aggregateRole( aggregateRole ) - , m_flags( flags ) + , m_rootNode(rootNode) + , m_groupedColumn(0) + , m_groupedRole(groupedRole) + , m_aggregateRole(aggregateRole) + , m_flags(flags) { - setSourceModel( model ); - - // signal proxies - connect( sourceModel(), - SIGNAL( dataChanged( const QModelIndex&, const QModelIndex& ) ), - this, SLOT( modelDataChanged( const QModelIndex&, const QModelIndex& ) ) - ); - connect( sourceModel(), SIGNAL( rowsInserted( const QModelIndex&, int, int ) ), - SLOT( modelRowsInserted( const QModelIndex &, int, int ) ) ); - connect( sourceModel(), SIGNAL(rowsAboutToBeInserted( const QModelIndex &, int ,int )), - SLOT(modelRowsAboutToBeInserted( const QModelIndex &, int ,int ))); - connect( sourceModel(), SIGNAL( rowsRemoved( const QModelIndex&, int, int ) ), - SLOT( modelRowsRemoved( const QModelIndex&, int, int ) ) ); - connect( sourceModel(), SIGNAL(rowsAboutToBeRemoved( const QModelIndex &, int ,int )), - SLOT(modelRowsAboutToBeRemoved(QModelIndex,int,int)) ); - connect( sourceModel(), SIGNAL(layoutChanged()), SLOT(buildTree()) ); - connect( sourceModel(), SIGNAL(dataChanged(QModelIndex,QModelIndex)), - SLOT(modelDataChanged(QModelIndex,QModelIndex)) ); - connect( sourceModel(), SIGNAL(modelReset()), this, SLOT(resetModel()) ); - - if( groupedColumn != -1 ) - setGroupedColumn( groupedColumn ); + if (groupedColumn != -1) { + setGroupedColumn(groupedColumn); + } } QtGroupingProxy::~QtGroupingProxy() { } +void QtGroupingProxy::setSourceModel(QAbstractItemModel* model) +{ + if (sourceModel()) { + disconnect(sourceModel(), nullptr, this, nullptr); + } + + QAbstractProxyModel::setSourceModel(model); + + if (sourceModel()) { + // signal proxies + connect(sourceModel(), SIGNAL(rowsInserted(const QModelIndex&, int, int)), + SLOT(modelRowsInserted(const QModelIndex&, int, int))); + connect(sourceModel(), SIGNAL(rowsAboutToBeInserted(const QModelIndex&, int, int)), + SLOT(modelRowsAboutToBeInserted(const QModelIndex&, int, int))); + connect(sourceModel(), SIGNAL(rowsRemoved(const QModelIndex&, int, int)), + SLOT(modelRowsRemoved(const QModelIndex&, int, int))); + connect(sourceModel(), SIGNAL(rowsAboutToBeRemoved(const QModelIndex&, int, int)), + SLOT(modelRowsAboutToBeRemoved(QModelIndex, int, int))); + connect(sourceModel(), SIGNAL(layoutChanged()), SLOT(buildTree())); + connect(sourceModel(), SIGNAL(dataChanged(QModelIndex, QModelIndex)), + SLOT(modelDataChanged(QModelIndex, QModelIndex))); + connect(sourceModel(), SIGNAL(modelReset()), this, SLOT(resetModel())); + + buildTree(); + } +} + void QtGroupingProxy::setGroupedColumn( int groupedColumn ) { @@ -205,9 +213,15 @@ QtGroupingProxy::buildTree() } endResetModel(); + // restore expand-state - for( int row = 0; row < rowCount(); row++ ) { - QModelIndex idx = index( row, 0, QModelIndex() ); + refreshExpandedItems(); +} + +void QtGroupingProxy::refreshExpandedItems() const +{ + for (int row = 0; row < rowCount(); row++) { + QModelIndex idx = index(row, 0, QModelIndex()); if (m_expandedItems.contains(idx.data(Qt::UserRole).toString())) { emit expandItem(idx); } -- cgit v1.3.1