QmlDesigner: Cleanup QmlTimelineKeyframeGroup

Many checks are not anymore need so we can just remove the code and make
it clean what we want. Use the filtered algorithm to remove some loops.
We add Utils::filteredCast to algorithm.h to return a different result
container and let it be cast in std::copy_if.

Change-Id: I114c17cd2d5a69c9ebbfbd804805c4d2fa0599e1
Reviewed-by: Thomas Hartmann <thomas.hartmann@qt.io>
Reviewed-by: Eike Ziller <eike.ziller@qt.io>
Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
This commit is contained in:
Marco Bubke
2022-09-07 12:13:53 +02:00
parent 8e0c09a35e
commit 016fb66c96
3 changed files with 33 additions and 28 deletions

View File

@@ -890,6 +890,17 @@ C filtered(const C &container, R (S::*predicate)() const)
return out; return out;
} }
//////////////////
// filteredCast
/////////////////
template<typename R, typename C, typename F>
Q_REQUIRED_RESULT R filteredCast(const C &container, F predicate)
{
R out;
std::copy_if(std::begin(container), std::end(container), inserter(out), predicate);
return out;
}
////////////////// //////////////////
// partition // partition
///////////////// /////////////////

View File

@@ -65,6 +65,7 @@ public:
QmlTimeline timeline() const; QmlTimeline timeline() const;
static bool isDangling(const ModelNode &node);
bool isDangling() const; bool isDangling() const;
}; };

View File

@@ -12,6 +12,7 @@
#include <nodelistproperty.h> #include <nodelistproperty.h>
#include <variantproperty.h> #include <variantproperty.h>
#include <utils/algorithm.h>
#include <utils/qtcassert.h> #include <utils/qtcassert.h>
#include <cmath> #include <cmath>
@@ -42,10 +43,7 @@ void QmlTimelineKeyframeGroup::destroy()
ModelNode QmlTimelineKeyframeGroup::target() const ModelNode QmlTimelineKeyframeGroup::target() const
{ {
if (modelNode().property("target").isBindingProperty()) return modelNode().bindingProperty("target").resolveToModelNode();
return modelNode().bindingProperty("target").resolveToModelNode();
return {};
} }
void QmlTimelineKeyframeGroup::setTarget(const ModelNode &target) void QmlTimelineKeyframeGroup::setTarget(const ModelNode &target)
@@ -123,17 +121,17 @@ QmlTimeline QmlTimelineKeyframeGroup::timeline() const
{ {
QTC_CHECK(isValid()); QTC_CHECK(isValid());
if (modelNode().hasParentProperty()) return modelNode().parentProperty().parentModelNode();
return modelNode().parentProperty().parentModelNode(); }
return {}; bool QmlTimelineKeyframeGroup::isDangling(const ModelNode &node)
{
QmlTimelineKeyframeGroup group{node};
return group.isDangling();
} }
bool QmlTimelineKeyframeGroup::isDangling() const bool QmlTimelineKeyframeGroup::isDangling() const
{ {
if (!isValid())
return false;
return !target().isValid() || keyframes().isEmpty(); return !target().isValid() || keyframes().isEmpty();
} }
@@ -247,14 +245,9 @@ QList<ModelNode> QmlTimelineKeyframeGroup::keyframes() const
QList<ModelNode> QmlTimelineKeyframeGroup::keyframePositions() const QList<ModelNode> QmlTimelineKeyframeGroup::keyframePositions() const
{ {
QList<ModelNode> returnValues; return Utils::filtered(modelNode().defaultNodeListProperty().toModelNodeList(), [](auto &&node) {
for (const ModelNode &childNode : modelNode().defaultNodeListProperty().toModelNodeList()) { return node.variantProperty("frame").value().isValid();
QVariant value = childNode.variantProperty("frame").value(); });
if (value.isValid())
returnValues.append(childNode);
}
return returnValues;
} }
bool QmlTimelineKeyframeGroup::isValidKeyframe(const ModelNode &node) bool QmlTimelineKeyframeGroup::isValidKeyframe(const ModelNode &node)
@@ -278,21 +271,21 @@ QmlTimelineKeyframeGroup QmlTimelineKeyframeGroup::keyframeGroupForKeyframe(cons
return QmlTimelineKeyframeGroup(); return QmlTimelineKeyframeGroup();
} }
QList<QmlTimelineKeyframeGroup> QmlTimelineKeyframeGroup::allInvalidTimelineKeyframeGroups(AbstractView *view) QList<QmlTimelineKeyframeGroup> QmlTimelineKeyframeGroup::allInvalidTimelineKeyframeGroups(
AbstractView *view)
{ {
QList<QmlTimelineKeyframeGroup> ret; QTC_CHECK(view);
QTC_CHECK(view->model());
QTC_ASSERT(view, return ret); if (!view || !view->model())
QTC_ASSERT(view->model(), return ret); return {};
QTC_ASSERT(view->rootModelNode().isValid(), return ret);
const auto groups = view->rootModelNode().subModelNodesOfType( const auto groups = view->rootModelNode().subModelNodesOfType(
view->model()->qtQuickTimelineKeyframeGroupMetaInfo()); view->model()->qtQuickTimelineKeyframeGroupMetaInfo());
for (const QmlTimelineKeyframeGroup group : groups) {
if (group.isDangling()) return Utils::filteredCast<QList<QmlTimelineKeyframeGroup>>(groups, [](auto &&group) {
ret.append(group); return isDangling(group);
} });
return ret;
} }
void QmlTimelineKeyframeGroup::moveAllKeyframes(qreal offset) void QmlTimelineKeyframeGroup::moveAllKeyframes(qreal offset)