Skip to content

Commit 604c49f

Browse files
committed
fw/uicomponents: Precompute role name to role map
1 parent ad74c66 commit 604c49f

2 files changed

Lines changed: 21 additions & 2 deletions

File tree

src/framework/uicomponents/qml/Muse/UiComponents/sortfilterproxymodel.cpp

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
#include <QTimer>
2626

27+
#include "global/log.h"
2728
#include "uicomponents/view/modelutils.h"
2829

2930
using namespace muse::uicomponents;
@@ -145,7 +146,7 @@ void SortFilterProxyModel::setAlwaysExcludeIndices(const QList<int>& indices)
145146

146147
int SortFilterProxyModel::roleFromRoleName(const QString& roleName) const
147148
{
148-
return roleNames().key(roleName.toUtf8(), INVALID_KEY);
149+
return m_roles.value(roleName.toUtf8(), INVALID_KEY);
149150
}
150151

151152
QHash<int, QByteArray> SortFilterProxyModel::roleNames() const
@@ -165,11 +166,16 @@ void SortFilterProxyModel::setSourceModel(QAbstractItemModel* sourceModel)
165166

166167
QSortFilterProxyModel::setSourceModel(sourceModel);
167168

169+
updateRoleMap();
170+
168171
emit sourceModelRoleNamesChanged();
169172

170173
if (auto sourceSortFilterModel = qobject_cast<SortFilterProxyModel*>(sourceModel)) {
171174
m_subSourceModelConnection = connect(sourceSortFilterModel, &SortFilterProxyModel::sourceModelRoleNamesChanged,
172-
this, &SortFilterProxyModel::sourceModelRoleNamesChanged);
175+
this, [this] {
176+
updateRoleMap();
177+
emit sourceModelRoleNamesChanged();
178+
});
173179
}
174180
}
175181

@@ -218,3 +224,12 @@ Sorter* SortFilterProxyModel::currentSorter() const
218224

219225
return nullptr;
220226
}
227+
228+
void SortFilterProxyModel::updateRoleMap()
229+
{
230+
m_roles.clear();
231+
for (const auto& [role, roleName] : roleNames().asKeyValueRange()) {
232+
const auto[it, didInsert] = m_roles.try_emplace(roleName, role);
233+
DO_ASSERT(didInsert);
234+
}
235+
}

src/framework/uicomponents/qml/Muse/UiComponents/sortfilterproxymodel.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
#pragma once
2424

25+
#include <QByteArray>
2526
#include <QHash>
2627
#include <QList>
2728
#include <QMetaObject>
@@ -76,6 +77,9 @@ class SortFilterProxyModel : public QSortFilterProxyModel
7677

7778
private:
7879
Sorter* currentSorter() const;
80+
void updateRoleMap();
81+
82+
QHash<QByteArray, int> m_roles;
7983

8084
QmlListProperty<Filter> m_filters;
8185
QmlListProperty<Sorter> m_sorters;

0 commit comments

Comments
 (0)