diff options
| author | Mikaël Capelle <capelle.mikael@gmail.com> | 2021-01-04 19:06:43 +0100 |
|---|---|---|
| committer | Mikaël Capelle <capelle.mikael@gmail.com> | 2021-01-04 19:06:43 +0100 |
| commit | 85cd33715723f294abeeb8a0f3f77c0de27f6a37 (patch) | |
| tree | d290cb566abb0a3c238b85bf402d184480f9e17d | |
| parent | 7d53976ecc7c289ec44b8740a57758c6132aa6a9 (diff) | |
Disable proxies when they are not used.
| -rw-r--r-- | src/modlistbypriorityproxy.cpp | 9 | ||||
| -rw-r--r-- | src/modlistsortproxy.cpp | 6 | ||||
| -rw-r--r-- | src/modlistview.cpp | 53 | ||||
| -rw-r--r-- | src/qtgroupingproxy.cpp | 74 | ||||
| -rw-r--r-- | src/qtgroupingproxy.h | 7 |
5 files changed, 90 insertions, 59 deletions
diff --git a/src/modlistbypriorityproxy.cpp b/src/modlistbypriorityproxy.cpp index 749991d0..bf3c2d09 100644 --- a/src/modlistbypriorityproxy.cpp +++ b/src/modlistbypriorityproxy.cpp @@ -18,12 +18,15 @@ ModListByPriorityProxy::~ModListByPriorityProxy() void ModListByPriorityProxy::setSourceModel(QAbstractItemModel* model) { + if (sourceModel()) { + disconnect(sourceModel(), nullptr, this, nullptr); + } + QAbstractProxyModel::setSourceModel(model); if (sourceModel()) { - m_CollapsedItems.clear(); - connect(sourceModel(), &QAbstractItemModel::layoutChanged, this, [this]() { buildTree(); }, Qt::UniqueConnection); - connect(sourceModel(), &QAbstractItemModel::rowsRemoved, this, [this]() { buildTree(); }, Qt::UniqueConnection); + connect(sourceModel(), &QAbstractItemModel::layoutChanged, this, &ModListByPriorityProxy::buildTree, Qt::UniqueConnection); + connect(sourceModel(), &QAbstractItemModel::rowsRemoved, this, &ModListByPriorityProxy::buildTree, Qt::UniqueConnection); connect(sourceModel(), &QAbstractItemModel::modelReset, this, &ModListByPriorityProxy::buildTree, Qt::UniqueConnection); connect(sourceModel(), &QAbstractItemModel::dataChanged, this, &ModListByPriorityProxy::modelDataChanged, Qt::UniqueConnection); refresh(); diff --git a/src/modlistsortproxy.cpp b/src/modlistsortproxy.cpp index 054b980c..9ae37a43 100644 --- a/src/modlistsortproxy.cpp +++ b/src/modlistsortproxy.cpp @@ -652,8 +652,10 @@ void ModListSortProxy::setSourceModel(QAbstractItemModel *sourceModel) if (proxy != nullptr) {
sourceModel = proxy->sourceModel();
}
- connect(sourceModel, SIGNAL(aboutToChangeData()), this, SLOT(aboutToChangeData()), Qt::UniqueConnection);
- connect(sourceModel, SIGNAL(postDataChanged()), this, SLOT(postDataChanged()), Qt::UniqueConnection);
+ if (sourceModel) {
+ connect(sourceModel, SIGNAL(aboutToChangeData()), this, SLOT(aboutToChangeData()), Qt::UniqueConnection);
+ connect(sourceModel, SIGNAL(postDataChanged()), this, SLOT(postDataChanged()), Qt::UniqueConnection);
+ }
}
void ModListSortProxy::aboutToChangeData()
diff --git a/src/modlistview.cpp b/src/modlistview.cpp index 2edcf56e..6399e910 100644 --- a/src/modlistview.cpp +++ b/src/modlistview.cpp @@ -631,6 +631,18 @@ bool ModListView::toggleSelectionState() return m_core->modList()->toggleState(indexViewToModel(selectionModel()->selectedRows()));
}
+// helper function because QtGroupingProxy and ModListByPriorityProxy
+// have similar methods but do not share a common parent class
+template <class Proxy>
+void setProxy(OrganizerCore* core, QTreeView* view, ModListSortProxy* sortProxy, Proxy* proxy)
+{
+ proxy->setSourceModel(core->modList());
+ sortProxy->setSourceModel(proxy);
+ QObject::connect(view, &QTreeView::expanded, proxy, &Proxy::expanded);
+ QObject::connect(view, &QTreeView::collapsed, proxy, &Proxy::collapsed);
+ proxy->refreshExpandedItems();
+}
+
void ModListView::updateGroupByProxy(int groupIndex)
{
// if the index is -1, we do not refresh unless we are grouping
@@ -642,18 +654,18 @@ void ModListView::updateGroupByProxy(int groupIndex) groupIndex = ui.groupBy->currentIndex();
}
+ auto* previousModel = m_sortProxy->sourceModel();
+
if (groupIndex == GroupBy::CATEGORY) {
- m_byCategoryProxy->setGroupedColumn(ModList::COL_CATEGORY);
- m_sortProxy->setSourceModel(m_byCategoryProxy);
+ setProxy(m_core, this, m_sortProxy, m_byCategoryProxy);
}
else if (groupIndex == GroupBy::NEXUS_ID) {
- m_byNexusIdProxy->setGroupedColumn(ModList::COL_MODID);
- m_sortProxy->setSourceModel(m_byNexusIdProxy);
+ setProxy(m_core, this, m_sortProxy, m_byNexusIdProxy);
}
else if (m_core->settings().interface().collapsibleSeparators()
&& m_sortProxy->sortColumn() == ModList::COL_PRIORITY
&& m_sortProxy->sortOrder() == Qt::AscendingOrder) {
- m_sortProxy->setSourceModel(m_byPriorityProxy);
+ setProxy(m_core, this, m_sortProxy, m_byPriorityProxy);
}
else {
m_sortProxy->setSourceModel(m_core->modList());
@@ -661,12 +673,21 @@ void ModListView::updateGroupByProxy(int groupIndex) if (hasCollapsibleSeparators()) {
ui.filterSeparators->setCurrentIndex(ModListSortProxy::SeparatorFilter);
- m_byPriorityProxy->refresh();
ui.filterSeparators->setEnabled(false);
}
else {
ui.filterSeparators->setEnabled(true);
}
+
+ // reset the source model of the old proxy because we do not want to
+ // react to signals
+ //
+ // also stop notifying the old proxy when items are expanded
+ //
+ if (auto* proxy = qobject_cast<QAbstractProxyModel*>(previousModel)) {
+ proxy->setSourceModel(nullptr);
+ disconnect(this, nullptr, proxy, nullptr);
+ }
}
void ModListView::setup(OrganizerCore& core, CategoryFactory& factory, MainWindow* mw, Ui::MainWindow* mwui)
@@ -688,22 +709,12 @@ void ModListView::setup(OrganizerCore& core, CategoryFactory& factory, MainWindo connect(core.modList(), &ModList::modelReset, [=] { clearOverwriteMarkers(); });
m_byPriorityProxy = new ModListByPriorityProxy(core.currentProfile(), core, this);
- m_byPriorityProxy->setSourceModel(core.modList());
- connect(this, &QTreeView::expanded, [=](auto&& name) { m_byPriorityProxy->expanded(name); });
- connect(this, &QTreeView::collapsed, [=](auto&& name) { m_byPriorityProxy->collapsed(name); });
connect(m_byPriorityProxy, &ModListByPriorityProxy::expandItem, [=](auto&& index) { expandItem(index); });
-
- m_byCategoryProxy = new QtGroupingProxy(core.modList(), QModelIndex(), ModList::COL_CATEGORY,
- ModList::GroupingRole, 0, ModList::AggrRole);
- connect(this, &QTreeView::expanded, m_byCategoryProxy, &QtGroupingProxy::expanded);
- connect(this, &QTreeView::collapsed, m_byCategoryProxy, &QtGroupingProxy::collapsed);
- connect(m_byCategoryProxy, &QtGroupingProxy::expandItem, this, &ModListView::expandItem);
- m_byNexusIdProxy = new QtGroupingProxy(core.modList(), QModelIndex(), ModList::COL_MODID,
- ModList::GroupingRole, QtGroupingProxy::FLAG_NOGROUPNAME | QtGroupingProxy::FLAG_NOSINGLE,
- ModList::AggrRole);
- connect(this, &QTreeView::expanded, m_byNexusIdProxy, &QtGroupingProxy::expanded);
- connect(this, &QTreeView::collapsed, m_byNexusIdProxy, &QtGroupingProxy::collapsed);
- connect(m_byNexusIdProxy, &QtGroupingProxy::expandItem, this, &ModListView::expandItem);
+ m_byCategoryProxy = new QtGroupingProxy(QModelIndex(), ModList::COL_CATEGORY, ModList::GroupingRole, 0, ModList::AggrRole);
+ connect(m_byCategoryProxy, &QtGroupingProxy::expandItem, [=](auto&& index) { expandItem(index); });
+ m_byNexusIdProxy = new QtGroupingProxy(QModelIndex(), ModList::COL_MODID,
+ ModList::GroupingRole, QtGroupingProxy::FLAG_NOGROUPNAME | QtGroupingProxy::FLAG_NOSINGLE, ModList::AggrRole);
+ connect(m_byNexusIdProxy, &QtGroupingProxy::expandItem, [=](auto&& index) { expandItem(index); });
m_sortProxy = new ModListSortProxy(core.currentProfile(), &core);
setModel(m_sortProxy);
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);
}
diff --git a/src/qtgroupingproxy.h b/src/qtgroupingproxy.h index 6567cb0e..131f33dc 100644 --- a/src/qtgroupingproxy.h +++ b/src/qtgroupingproxy.h @@ -47,12 +47,13 @@ public: };
public:
- explicit QtGroupingProxy( QAbstractItemModel *model, QModelIndex rootNode = QModelIndex(),
+ explicit QtGroupingProxy( QModelIndex rootNode = QModelIndex(),
int groupedColumn = -1, int groupedRole = Qt::DisplayRole,
unsigned int flags = 0,
int aggregateRole = Qt::DisplayRole);
~QtGroupingProxy();
+ void setSourceModel(QAbstractItemModel* model) override;
void setGroupedColumn( int groupedColumn );
/* QAbstractProxyModel methods */
@@ -79,10 +80,10 @@ public: virtual QModelIndex addEmptyGroup( const RowData &data );
virtual bool removeGroup( const QModelIndex &idx );
- QStringList expandedState();
+ void refreshExpandedItems() const ;
signals:
- void expandItem(const QModelIndex &index);
+ void expandItem(const QModelIndex &index) const;
public slots:
/**
|
