From 04fe8b1e43fbc9396dcf10e6db2e190df143aecd Mon Sep 17 00:00:00 2001 From: zach Date: Tue, 1 Sep 2026 15:49:21 +0200 Subject: [PATCH] feat: implement findChild* methods --- Plugins/ZShell/zutils.cpp | 68 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/Plugins/ZShell/zutils.cpp b/Plugins/ZShell/zutils.cpp index b6de6e0..4ae359b 100644 --- a/Plugins/ZShell/zutils.cpp +++ b/Plugins/ZShell/zutils.cpp @@ -214,6 +214,74 @@ QString ZUtils::enumToString( return QString::fromUtf8(key); } +namespace { + +template +QQuickItem* findChildDfs(QQuickItem* root, Predicate&& match) { + const auto children = root->childItems(); + for (QQuickItem* const child : children) { + if (match(child)) { + return child; + } + if (QQuickItem* const found = findChildDfs(child, match)) { + return found; + } + } + return nullptr; +} + +template +void findChildrenDfs( + QQuickItem* root, Predicate&& match, QList& out) { + const auto children = root->childItems(); + for (QQuickItem* const child : children) { + if (match(child)) { + out.append(child); + } + findChildrenDfs(child, match, out); + } +} + +} // namespace + +QQuickItem* ZUtils::findChild(QQuickItem* root, const QString& name) { + if (!root) { + return nullptr; + } + + return findChildDfs(root, [&name](const QQuickItem* item) { + return item->objectName() == name; + }); +} + +QList ZUtils::findChildren(QQuickItem* root, const QString& name) { + QList children; + if (root) { + findChildrenDfs( + root, + [&name](const QQuickItem* item) { + return item->objectName() == name; + }, + children); + } + return children; +} + +QList ZUtils::findChildrenMatching( + QQuickItem* root, const QString& pattern) { + QList children; + if (root) { + const QRegularExpression re(pattern); + findChildrenDfs( + root, + [&re](const QQuickItem* item) { + return re.match(item->objectName()).hasMatch(); + }, + children); + } + return children; +} + #ifndef ZSHELL_VERSION #define ZSHELL_VERSION "" #endif