Advertisement
fasked

Untitled

Jan 29th, 2025
229
0
3 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. QObjectList ObjectSearch::findByClassName(const QString& className, const QObjectList& roots) const
  2. {
  3.     QObjectList items;
  4.     for (const auto& root : roots)
  5.     {
  6.         items.append(findByClassNameImpl(className, root));
  7.     }
  8.  
  9.     return items;
  10. }
  11.  
  12. QObjectList ObjectSearch::findByClassNameImpl(const QString &className, QObject *parent) const
  13. {
  14.     Q_ASSERT(parent);
  15.  
  16.     QObjectList items;
  17.     const auto hierarchy = _delegate.getClassHierarchy(parent);
  18.     for (const auto& i : hierarchy)
  19.     {
  20.         if (checkMatch(className, i))
  21.         {
  22.             items.append(parent);
  23.         }
  24.     }
  25.  
  26.     for (auto* child : _delegate.getChildrenList(parent))
  27.     {
  28.         const auto recursiveItems = findByClassNameImpl(className, child);
  29.         items.append(recursiveItems);
  30.     }
  31.  
  32.     return items;
  33. }
  34.  
  35. std::unique_ptr<Element> ObjectSearch::findByQualifiedNameImpl(QObject* currentRoot,
  36.                                                                const QObjectList& topLevelRoots,
  37.                                                                const QVariantMap& name,
  38.                                                                const int depth) const
  39. {
  40.     const QString logIndent(depth * 2, ' ');
  41.     qCDebug(categoryObjectSearch) << qPrintable(logIndent) << "START looking for" << name.value("type");
  42.  
  43.     if (!name.contains("type"))
  44.     {
  45.         return nullptr;
  46.     }
  47.  
  48.     if (name.contains("container"))
  49.     {
  50.         auto containerRoot = findByQualifiedNameImpl(nullptr, topLevelRoots, name["container"].toMap(), depth + 1);
  51.         if (!containerRoot)
  52.         {
  53.             return nullptr;
  54.         }
  55.  
  56.         auto* object = containerRoot->toObject();
  57.         if (const auto* containerDelegate = _delegatePool.getDelegate(object, _delegate))
  58.         {
  59.             qCDebug(categoryObjectSearch) << qPrintable(logIndent) << "Delegate searching to container with type"
  60.                                           << object->metaObject()->className();
  61.  
  62.             if (auto result = containerDelegate->findByQualifiedName(name, object))
  63.             {
  64.                 qCDebug(categoryObjectSearch) << qPrintable(logIndent) << "GOT result"
  65.                                               << (result.value() ? result.value()->getClassname() : "nullptr");
  66.  
  67.                 return std::move(result.value());
  68.             }
  69.         }
  70.     }
  71.  
  72.     const auto getCurrentRoot = [&]() -> std::optional<QObject*>
  73.     {
  74.         if (currentRoot)
  75.         {
  76.             return currentRoot;
  77.         }
  78.  
  79.         for (const auto& tag : QStringList({"container", "window", "parentWidget"}))
  80.         {
  81.             if (name.contains(tag))
  82.             {
  83.                 const auto parent = findByQualifiedNameImpl(currentRoot, topLevelRoots, name[tag].toMap(), depth + 1);
  84.                 return parent ? parent->toObject() : nullptr;
  85.             }
  86.         }
  87.  
  88.         return std::nullopt;
  89.     };
  90.  
  91.     const auto getRoots = [&]() -> QObjectList
  92.     {
  93.         const auto rootObject = getCurrentRoot();
  94.         if (!rootObject.has_value())
  95.         {
  96.             return topLevelRoots;
  97.         }
  98.  
  99.         if (!rootObject.value())
  100.         {
  101.             return {};
  102.         }
  103.  
  104.         return {rootObject.value()};
  105.     };
  106.  
  107.     QObjectList result;
  108.     const auto roots = getRoots();
  109.     const auto candidates = findByClassName(name["type"].toString(), roots);
  110.     const auto occurrence = qMax(0, name.value("occurrence", "0").toInt() - 1);
  111.  
  112.     qCDebug(categoryObjectSearch) << qPrintable(logIndent) << "ROOTS" << roots.count() << "CANDIDATES"
  113.                                   << candidates.count();
  114.     for (const auto& o : candidates)
  115.     {
  116.         qCDebug(categoryObjectSearch) << qPrintable(logIndent) << "TRY" << o;
  117.  
  118.         QString unmatchedKey;
  119.         QVariant unmatchedValue;
  120.         if (itemMatches(o, name, unmatchedKey, unmatchedValue))
  121.         {
  122.             qCDebug(categoryObjectSearch) << qPrintable(logIndent) << "MATCHED" << o;
  123.  
  124.             result.append(o);
  125.             if (result.size() > occurrence)
  126.             {
  127.                 break;
  128.             }
  129.         }
  130.         else
  131.         {
  132.             qCDebug(categoryObjectSearch) << qPrintable(logIndent) << "UNMATCHED" << unmatchedKey << "="
  133.                                           << unmatchedValue << o->property(unmatchedKey.toLatin1());
  134.         }
  135.     }
  136.  
  137.     auto* obj = result.value(occurrence);
  138.     return obj ? std::make_unique<ObjectElement>(obj) : nullptr;
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement