From 70b76297fd074b4adda5e659d260c8cc18e51ef9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Tinkl?= Date: Wed, 19 Jul 2023 01:00:41 +0200 Subject: [PATCH] clazy fixes - don't detach containers while iterating - mixing const/non-const iterators - const/qAsConst fixes - QStringLiteral usage - nullptr fixes - don't #include whole Qt modules in .h files --- filters/expressionfilter.cpp | 13 +++++++------ filters/filtercontainer.h | 2 +- filters/filtercontainerfilter.cpp | 2 +- filters/filtersqmltypes.cpp | 4 ++-- filters/rangefilter.cpp | 4 ++-- filters/rangefilter.h | 4 ++-- filters/regexpfilter.h | 2 +- proxyroles/expressionrole.cpp | 15 ++++++++------- proxyroles/filterrole.cpp | 2 +- proxyroles/joinrole.cpp | 2 +- proxyroles/joinrole.h | 2 +- proxyroles/proxyrole.cpp | 10 +--------- proxyroles/proxyrolesqmltypes.cpp | 2 +- proxyroles/switchrole.cpp | 6 +++--- proxyroles/switchrole.h | 3 +-- qqmlsortfilterproxymodel.cpp | 4 ++-- qqmlsortfilterproxymodel.h | 2 +- sorters/expressionsorter.cpp | 29 +++++++++++++++-------------- sorters/filtersorter.cpp | 2 +- sorters/sortercontainer.h | 2 +- sorters/sortersqmltypes.cpp | 4 ++-- 21 files changed, 55 insertions(+), 61 deletions(-) diff --git a/filters/expressionfilter.cpp b/filters/expressionfilter.cpp index f3673ce..2e29815 100644 --- a/filters/expressionfilter.cpp +++ b/filters/expressionfilter.cpp @@ -65,9 +65,9 @@ bool ExpressionFilter::filterRow(const QModelIndex& sourceIndex, const QQmlSortF for (auto it = roles.cbegin(); it != roles.cend(); ++it) addToContext(it.value(), proxyModel.sourceData(sourceIndex, it.key())); - addToContext("index", sourceIndex.row()); + addToContext(QStringLiteral("index"), sourceIndex.row()); - context.setContextProperty("model", modelMap); + context.setContextProperty(QStringLiteral("model"), modelMap); QQmlExpression expression(m_scriptString, &context); QVariant variantResult = expression.evaluate(); @@ -101,12 +101,13 @@ void ExpressionFilter::updateContext(const QQmlSortFilterProxyModel& proxyModel) modelMap.insert(name, value); }; - for (const QByteArray& roleName : proxyModel.roleNames().values()) + const auto roleNames = proxyModel.roleNames(); + for (const QByteArray& roleName : roleNames) addToContext(roleName, QVariant()); - addToContext("index", -1); + addToContext(QStringLiteral("index"), -1); - m_context->setContextProperty("model", modelMap); + m_context->setContextProperty(QStringLiteral("model"), modelMap); updateExpression(); } @@ -116,7 +117,7 @@ void ExpressionFilter::updateExpression() return; delete m_expression; - m_expression = new QQmlExpression(m_scriptString, m_context, 0, this); + m_expression = new QQmlExpression(m_scriptString, m_context, nullptr, this); connect(m_expression, &QQmlExpression::valueChanged, this, &ExpressionFilter::invalidate); m_expression->setNotifyOnValueChanged(true); m_expression->evaluate(); diff --git a/filters/filtercontainer.h b/filters/filtercontainer.h index 4fc06f3..e0724b2 100644 --- a/filters/filtercontainer.h +++ b/filters/filtercontainer.h @@ -3,7 +3,7 @@ #include #include -#include +#include #include namespace qqsfpm { diff --git a/filters/filtercontainerfilter.cpp b/filters/filtercontainerfilter.cpp index a847e97..67501e2 100644 --- a/filters/filtercontainerfilter.cpp +++ b/filters/filtercontainerfilter.cpp @@ -4,7 +4,7 @@ namespace qqsfpm { void FilterContainerFilter::proxyModelCompleted(const QQmlSortFilterProxyModel& proxyModel) { - for (Filter* filter : m_filters) + for (Filter* filter : qAsConst(m_filters)) filter->proxyModelCompleted(proxyModel); } diff --git a/filters/filtersqmltypes.cpp b/filters/filtersqmltypes.cpp index 6704472..bd73c02 100644 --- a/filters/filtersqmltypes.cpp +++ b/filters/filtersqmltypes.cpp @@ -12,7 +12,7 @@ namespace qqsfpm { void registerFiltersTypes() { - qmlRegisterUncreatableType("SortFilterProxyModel", 0, 2, "Filter", "Filter is an abstract class"); + qmlRegisterUncreatableType("SortFilterProxyModel", 0, 2, "Filter", QStringLiteral("Filter is an abstract class")); qmlRegisterType("SortFilterProxyModel", 0, 2, "ValueFilter"); qmlRegisterType("SortFilterProxyModel", 0, 2, "IndexFilter"); qmlRegisterType("SortFilterProxyModel", 0, 2, "RegExpFilter"); @@ -20,7 +20,7 @@ void registerFiltersTypes() { qmlRegisterType("SortFilterProxyModel", 0, 2, "ExpressionFilter"); qmlRegisterType("SortFilterProxyModel", 0, 2, "AnyOf"); qmlRegisterType("SortFilterProxyModel", 0, 2, "AllOf"); - qmlRegisterUncreatableType("SortFilterProxyModel", 0, 2, "FilterContainer", "FilterContainer can only be used as an attaching type"); + qmlRegisterUncreatableType("SortFilterProxyModel", 0, 2, "FilterContainer", QStringLiteral("FilterContainer can only be used as an attaching type")); } Q_COREAPP_STARTUP_FUNCTION(registerFiltersTypes) diff --git a/filters/rangefilter.cpp b/filters/rangefilter.cpp index 2a6fde2..65583b3 100644 --- a/filters/rangefilter.cpp +++ b/filters/rangefilter.cpp @@ -43,7 +43,7 @@ QVariant RangeFilter::minimumValue() const return m_minimumValue; } -void RangeFilter::setMinimumValue(QVariant minimumValue) +void RangeFilter::setMinimumValue(const QVariant &minimumValue) { if (m_minimumValue == minimumValue) return; @@ -92,7 +92,7 @@ QVariant RangeFilter::maximumValue() const return m_maximumValue; } -void RangeFilter::setMaximumValue(QVariant maximumValue) +void RangeFilter::setMaximumValue(const QVariant &maximumValue) { if (m_maximumValue == maximumValue) return; diff --git a/filters/rangefilter.h b/filters/rangefilter.h index 2de2f8c..bd6bbbd 100644 --- a/filters/rangefilter.h +++ b/filters/rangefilter.h @@ -18,12 +18,12 @@ public: using RoleFilter::RoleFilter; QVariant minimumValue() const; - void setMinimumValue(QVariant minimumValue); + void setMinimumValue(const QVariant &minimumValue); bool minimumInclusive() const; void setMinimumInclusive(bool minimumInclusive); QVariant maximumValue() const; - void setMaximumValue(QVariant maximumValue); + void setMaximumValue(const QVariant &maximumValue); bool maximumInclusive() const; void setMaximumInclusive(bool maximumInclusive); diff --git a/filters/regexpfilter.h b/filters/regexpfilter.h index 2c20a6a..ff7dc4f 100644 --- a/filters/regexpfilter.h +++ b/filters/regexpfilter.h @@ -19,7 +19,7 @@ public: RegExp2 = QRegExp::RegExp2, WildcardUnix = QRegExp::WildcardUnix, W3CXmlSchema11 = QRegExp::W3CXmlSchema11 }; - Q_ENUMS(PatternSyntax) + Q_ENUM(PatternSyntax) using RoleFilter::RoleFilter; diff --git a/proxyroles/expressionrole.cpp b/proxyroles/expressionrole.cpp index 70af4fa..b9d9442 100644 --- a/proxyroles/expressionrole.cpp +++ b/proxyroles/expressionrole.cpp @@ -76,9 +76,9 @@ QVariant ExpressionRole::data(const QModelIndex& sourceIndex, const QQmlSortFilt for (auto it = roles.cbegin(); it != roles.cend(); ++it) addToContext(it.value(), proxyModel.sourceData(sourceIndex, it.key())); - addToContext("index", sourceIndex.row()); + addToContext(QStringLiteral("index"), sourceIndex.row()); - context.setContextProperty("model", modelMap); + context.setContextProperty(QStringLiteral("model"), modelMap); QQmlExpression expression(m_scriptString, &context); QVariant result = expression.evaluate(); @@ -89,7 +89,7 @@ QVariant ExpressionRole::data(const QModelIndex& sourceIndex, const QQmlSortFilt } return result; } - return QVariant(); + return {}; } void ExpressionRole::updateContext(const QQmlSortFilterProxyModel& proxyModel) @@ -104,12 +104,13 @@ void ExpressionRole::updateContext(const QQmlSortFilterProxyModel& proxyModel) modelMap.insert(name, value); }; - for (const QByteArray& roleName : proxyModel.roleNames().values()) + const auto roleNames = proxyModel.roleNames(); + for (const QByteArray& roleName : roleNames) addToContext(roleName, QVariant()); - addToContext("index", -1); + addToContext(QStringLiteral("index"), -1); - m_context->setContextProperty("model", modelMap); + m_context->setContextProperty(QStringLiteral("model"), modelMap); updateExpression(); } @@ -119,7 +120,7 @@ void ExpressionRole::updateExpression() return; delete m_expression; - m_expression = new QQmlExpression(m_scriptString, m_context, 0, this); + m_expression = new QQmlExpression(m_scriptString, m_context, nullptr, this); connect(m_expression, &QQmlExpression::valueChanged, this, &ExpressionRole::invalidate); m_expression->setNotifyOnValueChanged(true); m_expression->evaluate(); diff --git a/proxyroles/filterrole.cpp b/proxyroles/filterrole.cpp index 84dcbcd..f14b533 100644 --- a/proxyroles/filterrole.cpp +++ b/proxyroles/filterrole.cpp @@ -55,7 +55,7 @@ void FilterRole::onFiltersCleared() QVariant FilterRole::data(const QModelIndex& sourceIndex, const QQmlSortFilterProxyModel& proxyModel) { - return std::all_of(m_filters.begin(), m_filters.end(), + return std::all_of(m_filters.cbegin(), m_filters.cend(), [&] (Filter* filter) { return filter->filterAcceptsRow(sourceIndex, proxyModel); } diff --git a/proxyroles/joinrole.cpp b/proxyroles/joinrole.cpp index c252be3..1b80fd2 100644 --- a/proxyroles/joinrole.cpp +++ b/proxyroles/joinrole.cpp @@ -71,7 +71,7 @@ QVariant JoinRole::data(const QModelIndex &sourceIndex, const QQmlSortFilterProx { QString result; - for (const QString& roleName : m_roleNames) + for (const QString& roleName : qAsConst(m_roleNames)) result += proxyModel.sourceData(sourceIndex, roleName).toString() + m_separator; if (!m_roleNames.isEmpty()) diff --git a/proxyroles/joinrole.h b/proxyroles/joinrole.h index 520b861..9e78068 100644 --- a/proxyroles/joinrole.h +++ b/proxyroles/joinrole.h @@ -28,7 +28,7 @@ Q_SIGNALS: private: QStringList m_roleNames; QVariant data(const QModelIndex& sourceIndex, const QQmlSortFilterProxyModel& proxyModel) override; - QString m_separator = " "; + QString m_separator = QStringLiteral(" "); }; } diff --git a/proxyroles/proxyrole.cpp b/proxyroles/proxyrole.cpp index 172b0c2..8c77077 100644 --- a/proxyroles/proxyrole.cpp +++ b/proxyroles/proxyrole.cpp @@ -1,11 +1,4 @@ #include "proxyrole.h" -#include -#include -#include -#include -#include -#include -#include "filters/filter.h" #include "qqmlsortfilterproxymodel.h" namespace qqsfpm { @@ -28,9 +21,8 @@ QVariant ProxyRole::roleData(const QModelIndex& sourceIndex, const QQmlSortFilte QVariant result = data(sourceIndex, proxyModel, name); m_mutex.unlock(); return result; - } else { - return {}; } + return {}; } void ProxyRole::proxyModelCompleted(const QQmlSortFilterProxyModel &proxyModel) diff --git a/proxyroles/proxyrolesqmltypes.cpp b/proxyroles/proxyrolesqmltypes.cpp index efea256..39a4d75 100644 --- a/proxyroles/proxyrolesqmltypes.cpp +++ b/proxyroles/proxyrolesqmltypes.cpp @@ -10,7 +10,7 @@ namespace qqsfpm { void registerProxyRoleTypes() { - qmlRegisterUncreatableType("SortFilterProxyModel", 0, 2, "ProxyRole", "ProxyRole is an abstract class"); + qmlRegisterUncreatableType("SortFilterProxyModel", 0, 2, "ProxyRole", QStringLiteral("ProxyRole is an abstract class")); qmlRegisterType("SortFilterProxyModel", 0, 2, "JoinRole"); qmlRegisterType("SortFilterProxyModel", 0, 2, "SwitchRole"); qmlRegisterType("SortFilterProxyModel", 0, 2, "ExpressionRole"); diff --git a/proxyroles/switchrole.cpp b/proxyroles/switchrole.cpp index f21de84..69ff9eb 100644 --- a/proxyroles/switchrole.cpp +++ b/proxyroles/switchrole.cpp @@ -50,7 +50,7 @@ QVariant SwitchRoleAttached::value() const return m_value; } -void SwitchRoleAttached::setValue(QVariant value) +void SwitchRoleAttached::setValue(const QVariant &value) { if (m_value == value) return; @@ -113,7 +113,7 @@ void SwitchRole::setDefaultValue(const QVariant& defaultValue) void SwitchRole::proxyModelCompleted(const QQmlSortFilterProxyModel& proxyModel) { - for (Filter* filter : m_filters) + for (Filter* filter : qAsConst(m_filters)) filter->proxyModelCompleted(proxyModel); } @@ -124,7 +124,7 @@ SwitchRoleAttached* SwitchRole::qmlAttachedProperties(QObject* object) QVariant SwitchRole::data(const QModelIndex &sourceIndex, const QQmlSortFilterProxyModel &proxyModel) { - for (auto filter: m_filters) { + for (auto filter: qAsConst(m_filters)) { if (!filter->enabled()) continue; if (filter->filterAcceptsRow(sourceIndex, proxyModel)) { diff --git a/proxyroles/switchrole.h b/proxyroles/switchrole.h index 203a8eb..16d2295 100644 --- a/proxyroles/switchrole.h +++ b/proxyroles/switchrole.h @@ -3,7 +3,6 @@ #include "singlerole.h" #include "filters/filtercontainer.h" -#include namespace qqsfpm { @@ -15,7 +14,7 @@ public: SwitchRoleAttached(QObject* parent); QVariant value() const; - void setValue(QVariant value); + void setValue(const QVariant &value); Q_SIGNALS: void valueChanged(); diff --git a/qqmlsortfilterproxymodel.cpp b/qqmlsortfilterproxymodel.cpp index d13efdf..3ebca88 100644 --- a/qqmlsortfilterproxymodel.cpp +++ b/qqmlsortfilterproxymodel.cpp @@ -230,8 +230,8 @@ QVariant QQmlSortFilterProxyModel::sourceData(const QModelIndex &sourceIndex, in QPair proxyRolePair = m_proxyRoleMap[role]; if (ProxyRole* proxyRole = proxyRolePair.first) return proxyRole->roleData(sourceIndex, *this, proxyRolePair.second); - else - return sourceModel()->data(sourceIndex, role); + + return sourceModel()->data(sourceIndex, role); } QVariant QQmlSortFilterProxyModel::data(const QModelIndex &index, int role) const diff --git a/qqmlsortfilterproxymodel.h b/qqmlsortfilterproxymodel.h index dbe0229..ee02f30 100644 --- a/qqmlsortfilterproxymodel.h +++ b/qqmlsortfilterproxymodel.h @@ -44,7 +44,7 @@ public: RegExp2 = QRegExp::RegExp2, WildcardUnix = QRegExp::WildcardUnix, W3CXmlSchema11 = QRegExp::W3CXmlSchema11 }; - Q_ENUMS(PatternSyntax) + Q_ENUM(PatternSyntax) QQmlSortFilterProxyModel(QObject* parent = 0); diff --git a/sorters/expressionsorter.cpp b/sorters/expressionsorter.cpp index 7bb9206..0d2904d 100644 --- a/sorters/expressionsorter.cpp +++ b/sorters/expressionsorter.cpp @@ -92,20 +92,20 @@ int ExpressionSorter::compare(const QModelIndex& sourceLeft, const QModelIndex& modelLeftMap.insert(it.value(), proxyModel.sourceData(sourceLeft, it.key())); modelRightMap.insert(it.value(), proxyModel.sourceData(sourceRight, it.key())); } - modelLeftMap.insert("index", sourceLeft.row()); - modelRightMap.insert("index", sourceRight.row()); + modelLeftMap.insert(QStringLiteral("index"), sourceLeft.row()); + modelRightMap.insert(QStringLiteral("index"), sourceRight.row()); QQmlExpression expression(m_scriptString, &context); - context.setContextProperty("modelLeft", modelLeftMap); - context.setContextProperty("modelRight", modelRightMap); + context.setContextProperty(QStringLiteral("modelLeft"), modelLeftMap); + context.setContextProperty(QStringLiteral("modelRight"), modelRightMap); if (evaluateBoolExpression(expression)) - return -1; + return -1; - context.setContextProperty("modelLeft", modelRightMap); - context.setContextProperty("modelRight", modelLeftMap); + context.setContextProperty(QStringLiteral("modelLeft"), modelRightMap); + context.setContextProperty(QStringLiteral("modelRight"), modelLeftMap); if (evaluateBoolExpression(expression)) - return 1; + return 1; } return 0; } @@ -118,15 +118,16 @@ void ExpressionSorter::updateContext(const QQmlSortFilterProxyModel& proxyModel) QVariantMap modelLeftMap, modelRightMap; // what about roles changes ? - for (const QByteArray& roleName : proxyModel.roleNames().values()) { + const auto roleNames = proxyModel.roleNames(); + for (const QByteArray& roleName : roleNames) { modelLeftMap.insert(roleName, QVariant()); modelRightMap.insert(roleName, QVariant()); } - modelLeftMap.insert("index", -1); - modelRightMap.insert("index", -1); + modelLeftMap.insert(QStringLiteral("index"), -1); + modelRightMap.insert(QStringLiteral("index"), -1); - m_context->setContextProperty("modelLeft", modelLeftMap); - m_context->setContextProperty("modelRight", modelRightMap); + m_context->setContextProperty(QStringLiteral("modelLeft"), modelLeftMap); + m_context->setContextProperty(QStringLiteral("modelRight"), modelRightMap); updateExpression(); } @@ -137,7 +138,7 @@ void ExpressionSorter::updateExpression() return; delete m_expression; - m_expression = new QQmlExpression(m_scriptString, m_context, 0, this); + m_expression = new QQmlExpression(m_scriptString, m_context, nullptr, this); connect(m_expression, &QQmlExpression::valueChanged, this, &ExpressionSorter::invalidate); m_expression->setNotifyOnValueChanged(true); m_expression->evaluate(); diff --git a/sorters/filtersorter.cpp b/sorters/filtersorter.cpp index 185c504..29025f1 100644 --- a/sorters/filtersorter.cpp +++ b/sorters/filtersorter.cpp @@ -48,7 +48,7 @@ int FilterSorter::compare(const QModelIndex& sourceLeft, const QModelIndex& sour void FilterSorter::proxyModelCompleted(const QQmlSortFilterProxyModel& proxyModel) { - for (Filter* filter : m_filters) + for (Filter* filter : qAsConst(m_filters)) filter->proxyModelCompleted(proxyModel); } diff --git a/sorters/sortercontainer.h b/sorters/sortercontainer.h index 016cc6d..b4da94e 100644 --- a/sorters/sortercontainer.h +++ b/sorters/sortercontainer.h @@ -3,7 +3,7 @@ #include #include -#include +#include #include namespace qqsfpm { diff --git a/sorters/sortersqmltypes.cpp b/sorters/sortersqmltypes.cpp index ceba423..2f81cf1 100644 --- a/sorters/sortersqmltypes.cpp +++ b/sorters/sortersqmltypes.cpp @@ -10,12 +10,12 @@ namespace qqsfpm { void registerSorterTypes() { - qmlRegisterUncreatableType("SortFilterProxyModel", 0, 2, "Sorter", "Sorter is an abstract class"); + qmlRegisterUncreatableType("SortFilterProxyModel", 0, 2, "Sorter", QStringLiteral("Sorter is an abstract class")); qmlRegisterType("SortFilterProxyModel", 0, 2, "RoleSorter"); qmlRegisterType("SortFilterProxyModel", 0, 2, "StringSorter"); qmlRegisterType("SortFilterProxyModel", 0, 2, "FilterSorter"); qmlRegisterType("SortFilterProxyModel", 0, 2, "ExpressionSorter"); - qmlRegisterUncreatableType("SortFilterProxyModel", 0, 2, "SorterContainer", "SorterContainer can only be used as an attaching type"); + qmlRegisterUncreatableType("SortFilterProxyModel", 0, 2, "SorterContainer", QStringLiteral("SorterContainer can only be used as an attaching type")); } Q_COREAPP_STARTUP_FUNCTION(registerSorterTypes)