Skip to content

Commit 5ac9096

Browse files
committed
Revert "core/region: use QList over QQmlListProperty for child regions"
This reverts commit 0c9c5be. Using QList breaks the default property usage.
1 parent 05fbead commit 5ac9096

File tree

2 files changed

+81
-33
lines changed

2 files changed

+81
-33
lines changed

src/core/region.cpp

Lines changed: 71 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
#include "region.hpp"
22
#include <cmath>
33

4-
#include <qlist.h>
54
#include <qobject.h>
65
#include <qpoint.h>
76
#include <qqmllist.h>
87
#include <qquickitem.h>
98
#include <qregion.h>
109
#include <qtmetamacros.h>
10+
#include <qtypes.h>
1111
#include <qvectornd.h>
1212

1313
PendingRegion::PendingRegion(QObject* parent): QObject(parent) {
@@ -19,7 +19,6 @@ PendingRegion::PendingRegion(QObject* parent): QObject(parent) {
1919
QObject::connect(this, &PendingRegion::widthChanged, this, &PendingRegion::changed);
2020
QObject::connect(this, &PendingRegion::heightChanged, this, &PendingRegion::changed);
2121
QObject::connect(this, &PendingRegion::childrenChanged, this, &PendingRegion::changed);
22-
QObject::connect(this, &PendingRegion::regionsChanged, this, &PendingRegion::childrenChanged);
2322
}
2423

2524
void PendingRegion::setItem(QQuickItem* item) {
@@ -42,33 +41,21 @@ void PendingRegion::setItem(QQuickItem* item) {
4241
emit this->itemChanged();
4342
}
4443

45-
void PendingRegion::onItemDestroyed() {
46-
this->mItem = nullptr;
47-
emit this->itemChanged();
48-
}
49-
50-
void PendingRegion::onChildDestroyed() {
51-
this->mRegions.removeAll(this->sender());
52-
emit this->regionsChanged();
53-
}
54-
55-
const QList<PendingRegion*>& PendingRegion::regions() const { return this->mRegions; }
56-
57-
void PendingRegion::setRegions(const QList<PendingRegion*>& regions) {
58-
if (regions == this->mRegions) return;
59-
60-
for (auto* region: this->mRegions) {
61-
QObject::disconnect(region, nullptr, this, nullptr);
62-
}
63-
64-
this->mRegions = regions;
65-
66-
for (auto* region: regions) {
67-
QObject::connect(region, &QObject::destroyed, this, &PendingRegion::onChildDestroyed);
68-
QObject::connect(region, &PendingRegion::changed, this, &PendingRegion::childrenChanged);
69-
}
70-
71-
emit this->regionsChanged();
44+
void PendingRegion::onItemDestroyed() { this->mItem = nullptr; }
45+
46+
void PendingRegion::onChildDestroyed() { this->mRegions.removeAll(this->sender()); }
47+
48+
QQmlListProperty<PendingRegion> PendingRegion::regions() {
49+
return QQmlListProperty<PendingRegion>(
50+
this,
51+
nullptr,
52+
&PendingRegion::regionsAppend,
53+
&PendingRegion::regionsCount,
54+
&PendingRegion::regionAt,
55+
&PendingRegion::regionsClear,
56+
&PendingRegion::regionsReplace,
57+
&PendingRegion::regionsRemoveLast
58+
);
7259
}
7360

7461
bool PendingRegion::empty() const {
@@ -130,3 +117,58 @@ QRegion PendingRegion::applyTo(const QRect& rect) const {
130117
return this->applyTo(baseRegion);
131118
}
132119
}
120+
121+
void PendingRegion::regionsAppend(QQmlListProperty<PendingRegion>* prop, PendingRegion* region) {
122+
auto* self = static_cast<PendingRegion*>(prop->object); // NOLINT
123+
if (!region) return;
124+
125+
QObject::connect(region, &QObject::destroyed, self, &PendingRegion::onChildDestroyed);
126+
QObject::connect(region, &PendingRegion::changed, self, &PendingRegion::childrenChanged);
127+
128+
self->mRegions.append(region);
129+
130+
emit self->childrenChanged();
131+
}
132+
133+
PendingRegion* PendingRegion::regionAt(QQmlListProperty<PendingRegion>* prop, qsizetype i) {
134+
return static_cast<PendingRegion*>(prop->object)->mRegions.at(i); // NOLINT
135+
}
136+
137+
void PendingRegion::regionsClear(QQmlListProperty<PendingRegion>* prop) {
138+
auto* self = static_cast<PendingRegion*>(prop->object); // NOLINT
139+
140+
for (auto* region: self->mRegions) {
141+
QObject::disconnect(region, nullptr, self, nullptr);
142+
}
143+
144+
self->mRegions.clear(); // NOLINT
145+
emit self->childrenChanged();
146+
}
147+
148+
qsizetype PendingRegion::regionsCount(QQmlListProperty<PendingRegion>* prop) {
149+
return static_cast<PendingRegion*>(prop->object)->mRegions.length(); // NOLINT
150+
}
151+
152+
void PendingRegion::regionsRemoveLast(QQmlListProperty<PendingRegion>* prop) {
153+
auto* self = static_cast<PendingRegion*>(prop->object); // NOLINT
154+
155+
auto* last = self->mRegions.last();
156+
if (last != nullptr) QObject::disconnect(last, nullptr, self, nullptr);
157+
158+
self->mRegions.removeLast();
159+
emit self->childrenChanged();
160+
}
161+
162+
void PendingRegion::regionsReplace(
163+
QQmlListProperty<PendingRegion>* prop,
164+
qsizetype i,
165+
PendingRegion* region
166+
) {
167+
auto* self = static_cast<PendingRegion*>(prop->object); // NOLINT
168+
169+
auto* old = self->mRegions.at(i);
170+
if (old != nullptr) QObject::disconnect(old, nullptr, self, nullptr);
171+
172+
self->mRegions.replace(i, region);
173+
emit self->childrenChanged();
174+
}

src/core/region.hpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ class PendingRegion: public QObject {
8282
/// }
8383
/// }
8484
/// ```
85-
Q_PROPERTY(QList<PendingRegion*> regions READ regions WRITE setRegions NOTIFY regionsChanged);
85+
Q_PROPERTY(QQmlListProperty<PendingRegion> regions READ regions);
8686
Q_CLASSINFO("DefaultProperty", "regions");
8787
QML_NAMED_ELEMENT(Region);
8888

@@ -91,8 +91,7 @@ class PendingRegion: public QObject {
9191

9292
void setItem(QQuickItem* item);
9393

94-
[[nodiscard]] const QList<PendingRegion*>& regions() const;
95-
void setRegions(const QList<PendingRegion*>& regions);
94+
QQmlListProperty<PendingRegion> regions();
9695

9796
[[nodiscard]] bool empty() const;
9897
[[nodiscard]] QRegion build() const;
@@ -110,7 +109,6 @@ class PendingRegion: public QObject {
110109
void yChanged();
111110
void widthChanged();
112111
void heightChanged();
113-
void regionsChanged();
114112
void childrenChanged();
115113

116114
/// Triggered when the region's geometry changes.
@@ -124,6 +122,14 @@ private slots:
124122
void onChildDestroyed();
125123

126124
private:
125+
static void regionsAppend(QQmlListProperty<PendingRegion>* prop, PendingRegion* region);
126+
static PendingRegion* regionAt(QQmlListProperty<PendingRegion>* prop, qsizetype i);
127+
static void regionsClear(QQmlListProperty<PendingRegion>* prop);
128+
static qsizetype regionsCount(QQmlListProperty<PendingRegion>* prop);
129+
static void regionsRemoveLast(QQmlListProperty<PendingRegion>* prop);
130+
static void
131+
regionsReplace(QQmlListProperty<PendingRegion>* prop, qsizetype i, PendingRegion* region);
132+
127133
QQuickItem* mItem = nullptr;
128134

129135
qint32 mX = 0;

0 commit comments

Comments
 (0)