diff options
author | Ulf Hermann <ulf.hermann@qt.io> | 2024-01-29 17:09:12 +0100 |
---|---|---|
committer | Ulf Hermann <ulf.hermann@qt.io> | 2024-02-01 09:31:35 +0100 |
commit | 1b8b77285948813b6e74554798b06210736e521c (patch) | |
tree | b0ada2a079b30e145ba554bf7e52f80bd68d8f12 /src/quick/util/qquickstategroup.cpp | |
parent | c1208af5004d0f200b471bfe8f3b396be7940edd (diff) |
QQuickStateGroup: Apply some style
We don't need to iterate on indices there and we can always
const-iterate. We can also return or continue early in a few places.
Change-Id: I71c4d6698d536c7118d4d1e5c03ecd3c2db7174d
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
Reviewed-by: Shawn Rutledge <shawn.rutledge@qt.io>
Diffstat (limited to 'src/quick/util/qquickstategroup.cpp')
-rw-r--r-- | src/quick/util/qquickstategroup.cpp | 90 |
1 files changed, 43 insertions, 47 deletions
diff --git a/src/quick/util/qquickstategroup.cpp b/src/quick/util/qquickstategroup.cpp index a111d4b543..f61261035f 100644 --- a/src/quick/util/qquickstategroup.cpp +++ b/src/quick/util/qquickstategroup.cpp @@ -96,9 +96,9 @@ QQuickStateGroup::QQuickStateGroup(QObject *parent) QQuickStateGroup::~QQuickStateGroup() { Q_D(const QQuickStateGroup); - for (int i = 0; i < d->states.size(); ++i) { - if (d->states.at(i)) - d->states.at(i)->setStateGroup(nullptr); + for (QQuickState *state : std::as_const(d->states)) { + if (state) + state->setStateGroup(nullptr); } if (d->nullState) d->nullState->setStateGroup(nullptr); @@ -164,13 +164,13 @@ QQuickState *QQuickStateGroupPrivate::at_state(QQmlListProperty<QQuickState> *li void QQuickStateGroupPrivate::clear_states(QQmlListProperty<QQuickState> *list) { - QQuickStateGroup *_this = static_cast<QQuickStateGroup *>(list->object); - _this->d_func()->setCurrentStateInternal(QString(), true); - for (qsizetype i = 0; i < _this->d_func()->states.size(); ++i) { - if (_this->d_func()->states.at(i)) - _this->d_func()->states.at(i)->setStateGroup(nullptr); + QQuickStateGroupPrivate *d = static_cast<QQuickStateGroup *>(list->object)->d_func(); + d->setCurrentStateInternal(QString(), true); + for (QQuickState *state : std::as_const(d->states)) { + if (state) + state->setStateGroup(nullptr); } - _this->d_func()->states.clear(); + d->states.clear(); } void QQuickStateGroupPrivate::replace_states(QQmlListProperty<QQuickState> *list, qsizetype index, QQuickState *state) @@ -308,8 +308,7 @@ void QQuickStateGroup::componentComplete() QVarLengthArray<QString, 4> names; names.reserve(d->states.size()); - for (int ii = 0; ii < d->states.size(); ++ii) { - QQuickState *state = d->states.at(ii); + for (QQuickState *state : std::as_const(d->states)) { if (!state) continue; @@ -317,11 +316,10 @@ void QQuickStateGroup::componentComplete() state->setName(QLatin1String("anonymousState") + QString::number(++d->unnamedCount)); QString stateName = state->name(); - if (names.contains(stateName)) { + if (names.contains(stateName)) qmlWarning(state->parent()) << "Found duplicate state name: " << stateName; - } else { + else names.append(std::move(stateName)); - } } if (d->updateAutoState()) { @@ -349,40 +347,38 @@ bool QQuickStateGroupPrivate::updateAutoState() return false; bool revert = false; - for (int ii = 0; ii < states.size(); ++ii) { - QQuickState *state = states.at(ii); - if (state && state->isWhenKnown()) { - if (state->isNamed()) { - bool whenValue = state->when(); - const QQmlPropertyIndex whenIndex(state->metaObject()->indexOfProperty("when")); - const auto potentialWhenBinding = QQmlAnyBinding::ofProperty(state, whenIndex); - Q_ASSERT(!potentialWhenBinding.isUntypedPropertyBinding()); - - // if there is a binding, the value in when might not be up-to-date at this point - // so we manually re-evaluate the binding - QQmlAbstractBinding *abstractBinding = potentialWhenBinding.asAbstractBinding(); - if (abstractBinding && abstractBinding->kind() == QQmlAbstractBinding::QmlBinding) { - QQmlBinding *binding = static_cast<QQmlBinding *>(abstractBinding); - if (binding->hasValidContext()) { - const auto boolType = QMetaType::fromType<bool>(); - const bool isUndefined = !binding->evaluate(&whenValue, boolType); - if (isUndefined) - whenValue = false; - } - } - - if (whenValue) { - qCDebug(lcStates) << "Setting auto state due to expression"; - if (currentState != state->name()) { - q->setState(state->name()); - return true; - } else { - return false; - } - } else if (state->name() == currentState) { - revert = true; - } + for (QQuickState *state : std::as_const(states)) { + if (!state || !state->isWhenKnown() || !state->isNamed()) + continue; + + bool whenValue = state->when(); + const QQmlPropertyIndex whenIndex(state->metaObject()->indexOfProperty("when")); + const auto potentialWhenBinding = QQmlAnyBinding::ofProperty(state, whenIndex); + Q_ASSERT(!potentialWhenBinding.isUntypedPropertyBinding()); + + // if there is a binding, the value in when might not be up-to-date at this point + // so we manually re-evaluate the binding + QQmlAbstractBinding *abstractBinding = potentialWhenBinding.asAbstractBinding(); + if (abstractBinding && abstractBinding->kind() == QQmlAbstractBinding::QmlBinding) { + QQmlBinding *binding = static_cast<QQmlBinding *>(abstractBinding); + if (binding->hasValidContext()) { + const auto boolType = QMetaType::fromType<bool>(); + const bool isUndefined = !binding->evaluate(&whenValue, boolType); + if (isUndefined) + whenValue = false; + } + } + + if (whenValue) { + qCDebug(lcStates) << "Setting auto state due to expression"; + if (currentState != state->name()) { + q->setState(state->name()); + return true; + } else { + return false; } + } else if (state->name() == currentState) { + revert = true; } } if (revert) { |