feat: abstract out scavenger scopes

This commit is contained in:
outfoxxed 2024-02-14 03:03:41 -08:00
parent 82aa7d45d3
commit d6ed717c39
Signed by: outfoxxed
GPG key ID: 4C88A185FB89301E
4 changed files with 74 additions and 56 deletions

View file

@ -2,6 +2,8 @@
#include <qobject.h>
#include <qqmlcomponent.h>
#include <qqmlintegration.h>
#include <qqmllist.h>
#include <qqmlparserstatus.h>
#include <qtmetamacros.h>
@ -30,7 +32,7 @@ protected:
class Scavengeable {
public:
Scavengeable() = default;
explicit Scavengeable() = default;
virtual ~Scavengeable() = default;
Scavengeable(Scavengeable&) = delete;
@ -47,3 +49,31 @@ QObject* createComponentScavengeable(
QQmlComponent& component,
QVariantMap& initialProperties
);
///! Reloader connection scope
/// Attempts to maintain scavengeable connections.
/// This is mostly useful to split a scavengeable component slot (e.g. `Variants`)
/// into multiple slots.
///
/// If you don't know what that means you probably don't need it.
class ScavengeableScope: public Scavenger, virtual public Scavengeable {
Q_OBJECT;
Q_PROPERTY(QQmlListProperty<QObject> data READ data);
Q_CLASSINFO("DefaultProperty", "data");
QML_ELEMENT;
public:
explicit ScavengeableScope(QObject* parent = nullptr): Scavenger(parent) {}
void earlyInit(QObject* old) override;
QObject* scavengeTargetFor(QObject* child) override;
QQmlListProperty<QObject> data();
private:
static void appendComponent(QQmlListProperty<QObject>* list, QObject* component);
// track only the children assigned to `data` in order
QList<QObject*> mData;
QList<QObject*> scavengeableData;
};