Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- QObjectList ObjectSearch::findByClassName(const QString& className, const QObjectList& roots) const
- {
- QObjectList items;
- for (const auto& root : roots)
- {
- items.append(findByClassNameImpl(className, root));
- }
- return items;
- }
- QObjectList ObjectSearch::findByClassNameImpl(const QString &className, QObject *parent) const
- {
- Q_ASSERT(parent);
- QObjectList items;
- const auto hierarchy = _delegate.getClassHierarchy(parent);
- for (const auto& i : hierarchy)
- {
- if (checkMatch(className, i))
- {
- items.append(parent);
- }
- }
- for (auto* child : _delegate.getChildrenList(parent))
- {
- const auto recursiveItems = findByClassNameImpl(className, child);
- items.append(recursiveItems);
- }
- return items;
- }
- std::unique_ptr<Element> ObjectSearch::findByQualifiedNameImpl(QObject* currentRoot,
- const QObjectList& topLevelRoots,
- const QVariantMap& name,
- const int depth) const
- {
- const QString logIndent(depth * 2, ' ');
- qCDebug(categoryObjectSearch) << qPrintable(logIndent) << "START looking for" << name.value("type");
- if (!name.contains("type"))
- {
- return nullptr;
- }
- if (name.contains("container"))
- {
- auto containerRoot = findByQualifiedNameImpl(nullptr, topLevelRoots, name["container"].toMap(), depth + 1);
- if (!containerRoot)
- {
- return nullptr;
- }
- auto* object = containerRoot->toObject();
- if (const auto* containerDelegate = _delegatePool.getDelegate(object, _delegate))
- {
- qCDebug(categoryObjectSearch) << qPrintable(logIndent) << "Delegate searching to container with type"
- << object->metaObject()->className();
- if (auto result = containerDelegate->findByQualifiedName(name, object))
- {
- qCDebug(categoryObjectSearch) << qPrintable(logIndent) << "GOT result"
- << (result.value() ? result.value()->getClassname() : "nullptr");
- return std::move(result.value());
- }
- }
- }
- const auto getCurrentRoot = [&]() -> std::optional<QObject*>
- {
- if (currentRoot)
- {
- return currentRoot;
- }
- for (const auto& tag : QStringList({"container", "window", "parentWidget"}))
- {
- if (name.contains(tag))
- {
- const auto parent = findByQualifiedNameImpl(currentRoot, topLevelRoots, name[tag].toMap(), depth + 1);
- return parent ? parent->toObject() : nullptr;
- }
- }
- return std::nullopt;
- };
- const auto getRoots = [&]() -> QObjectList
- {
- const auto rootObject = getCurrentRoot();
- if (!rootObject.has_value())
- {
- return topLevelRoots;
- }
- if (!rootObject.value())
- {
- return {};
- }
- return {rootObject.value()};
- };
- QObjectList result;
- const auto roots = getRoots();
- const auto candidates = findByClassName(name["type"].toString(), roots);
- const auto occurrence = qMax(0, name.value("occurrence", "0").toInt() - 1);
- qCDebug(categoryObjectSearch) << qPrintable(logIndent) << "ROOTS" << roots.count() << "CANDIDATES"
- << candidates.count();
- for (const auto& o : candidates)
- {
- qCDebug(categoryObjectSearch) << qPrintable(logIndent) << "TRY" << o;
- QString unmatchedKey;
- QVariant unmatchedValue;
- if (itemMatches(o, name, unmatchedKey, unmatchedValue))
- {
- qCDebug(categoryObjectSearch) << qPrintable(logIndent) << "MATCHED" << o;
- result.append(o);
- if (result.size() > occurrence)
- {
- break;
- }
- }
- else
- {
- qCDebug(categoryObjectSearch) << qPrintable(logIndent) << "UNMATCHED" << unmatchedKey << "="
- << unmatchedValue << o->property(unmatchedKey.toLatin1());
- }
- }
- auto* obj = result.value(occurrence);
- return obj ? std::make_unique<ObjectElement>(obj) : nullptr;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement