forked from qt-creator/qt-creator
Simplify IndexFilterModel::filter()
Get rid of code repetition. Use some lambdas. Change-Id: Ib344a2fdb0079faa86091d67e78c0c67c7ed077b Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org> Reviewed-by: Eike Ziller <eike.ziller@qt.io>
This commit is contained in:
@@ -319,7 +319,7 @@ QModelIndex IndexFilterModel::filter(const QString &filter, const QString &wildc
|
|||||||
// adapted copy from QHelpIndexModel
|
// adapted copy from QHelpIndexModel
|
||||||
|
|
||||||
if (filter.isEmpty() && wildcard.isEmpty()) {
|
if (filter.isEmpty() && wildcard.isEmpty()) {
|
||||||
int count = sourceModel()->rowCount();
|
const int count = sourceModel()->rowCount();
|
||||||
m_toSource.reserve(count);
|
m_toSource.reserve(count);
|
||||||
for (int i = 0; i < count; ++i)
|
for (int i = 0; i < count; ++i)
|
||||||
m_toSource.append(i);
|
m_toSource.append(i);
|
||||||
@@ -329,55 +329,48 @@ QModelIndex IndexFilterModel::filter(const QString &filter, const QString &wildc
|
|||||||
return index(0, 0);
|
return index(0, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
QHelpIndexModel *indexModel = qobject_cast<QHelpIndexModel *>(sourceModel());
|
const QStringList indices = static_cast<QHelpIndexModel *>(sourceModel())->stringList();
|
||||||
const QStringList indices = indexModel->stringList();
|
|
||||||
|
const auto match = [this, &indices, &filter] (std::function<bool(const QString &)> matcher) {
|
||||||
int goodMatch = -1;
|
int goodMatch = -1;
|
||||||
int perfectMatch = -1;
|
int perfectMatch = -1;
|
||||||
|
int i = 0;
|
||||||
|
for (const QString &index : indices) {
|
||||||
|
if (matcher(index)) {
|
||||||
|
m_toSource.append(i);
|
||||||
|
if (perfectMatch == -1 && index.startsWith(filter, Qt::CaseInsensitive)) {
|
||||||
|
if (goodMatch == -1)
|
||||||
|
goodMatch = m_toSource.size() - 1;
|
||||||
|
if (filter.length() == index.length()){
|
||||||
|
perfectMatch = m_toSource.size() - 1;
|
||||||
|
}
|
||||||
|
} else if (perfectMatch > -1 && index == filter) {
|
||||||
|
perfectMatch = m_toSource.size() - 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
++i;
|
||||||
|
}
|
||||||
|
return perfectMatch >= 0 ? perfectMatch : goodMatch;
|
||||||
|
};
|
||||||
|
|
||||||
if (!wildcard.isEmpty()) {
|
const auto matchSimpleOrRegExp = [&] () {
|
||||||
|
if (wildcard.isEmpty()) {
|
||||||
|
return match([&filter] (const QString &index) {
|
||||||
|
return index.contains(filter, Qt::CaseInsensitive);
|
||||||
|
});
|
||||||
|
}
|
||||||
const QRegularExpression regExp(QRegularExpression::wildcardToRegularExpression(wildcard),
|
const QRegularExpression regExp(QRegularExpression::wildcardToRegularExpression(wildcard),
|
||||||
QRegularExpression::CaseInsensitiveOption);
|
QRegularExpression::CaseInsensitiveOption);
|
||||||
int i = 0;
|
return match([®Exp] (const QString &index) {
|
||||||
for (const QString &index : indices) {
|
return index.contains(regExp);
|
||||||
if (index.contains(regExp)) {
|
});
|
||||||
m_toSource.append(i);
|
};
|
||||||
if (perfectMatch == -1 && index.startsWith(filter, Qt::CaseInsensitive)) {
|
const int matchedIndex = matchSimpleOrRegExp();
|
||||||
if (goodMatch == -1)
|
|
||||||
goodMatch = m_toSource.size() - 1;
|
|
||||||
if (filter.length() == index.length()){
|
|
||||||
perfectMatch = m_toSource.size() - 1;
|
|
||||||
}
|
|
||||||
} else if (perfectMatch > -1 && index == filter) {
|
|
||||||
perfectMatch = m_toSource.size() - 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
++i;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
int i = 0;
|
|
||||||
for (const QString &index : indices) {
|
|
||||||
if (index.contains(filter, Qt::CaseInsensitive)) {
|
|
||||||
m_toSource.append(i);
|
|
||||||
if (perfectMatch == -1 && index.startsWith(filter, Qt::CaseInsensitive)) {
|
|
||||||
if (goodMatch == -1)
|
|
||||||
goodMatch = m_toSource.size() - 1;
|
|
||||||
if (filter.length() == index.length()){
|
|
||||||
perfectMatch = m_toSource.size() - 1;
|
|
||||||
}
|
|
||||||
} else if (perfectMatch > -1 && index == filter) {
|
|
||||||
perfectMatch = m_toSource.size() - 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
++i;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
endResetModel();
|
endResetModel();
|
||||||
if (goodMatch == -1)
|
if (matchedIndex == -1)
|
||||||
return {};
|
return {};
|
||||||
if (perfectMatch == -1)
|
return index(matchedIndex, 0);
|
||||||
perfectMatch = qMax(0, goodMatch);
|
|
||||||
return index(perfectMatch, 0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
QModelIndex IndexFilterModel::mapToSource(const QModelIndex &proxyIndex) const
|
QModelIndex IndexFilterModel::mapToSource(const QModelIndex &proxyIndex) const
|
||||||
|
Reference in New Issue
Block a user