Skip to content

Commit f871d7c

Browse files
committed
core/region: use QList over QQmlListProperty for child regions
1 parent 49a3752 commit f871d7c

File tree

2 files changed

+35
-81
lines changed

2 files changed

+35
-81
lines changed

src/core/region.cpp

Lines changed: 31 additions & 71 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>
45
#include <qobject.h>
56
#include <qpoint.h>
67
#include <qqmllist.h>
78
#include <qquickitem.h>
89
#include <qregion.h>
910
#include <qtmetamacros.h>
10-
#include <qtypes.h>
1111
#include <qvectornd.h>
1212

1313
PendingRegion::PendingRegion(QObject* parent): QObject(parent) {
@@ -19,9 +19,12 @@ 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);
2223
}
2324

2425
void PendingRegion::setItem(QQuickItem* item) {
26+
if (item == this->mItem) return;
27+
2528
if (this->mItem != nullptr) {
2629
QObject::disconnect(this->mItem, nullptr, this, nullptr);
2730
}
@@ -39,21 +42,33 @@ void PendingRegion::setItem(QQuickItem* item) {
3942
emit this->itemChanged();
4043
}
4144

42-
void PendingRegion::onItemDestroyed() { this->mItem = nullptr; }
43-
44-
void PendingRegion::onChildDestroyed() { this->mRegions.removeAll(this->sender()); }
45-
46-
QQmlListProperty<PendingRegion> PendingRegion::regions() {
47-
return QQmlListProperty<PendingRegion>(
48-
this,
49-
nullptr,
50-
&PendingRegion::regionsAppend,
51-
&PendingRegion::regionsCount,
52-
&PendingRegion::regionAt,
53-
&PendingRegion::regionsClear,
54-
&PendingRegion::regionsReplace,
55-
&PendingRegion::regionsRemoveLast
56-
);
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();
5772
}
5873

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

src/core/region.hpp

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

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

9292
void setItem(QQuickItem* item);
9393

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

9697
[[nodiscard]] bool empty() const;
9798
[[nodiscard]] QRegion build() const;
@@ -109,6 +110,7 @@ class PendingRegion: public QObject {
109110
void yChanged();
110111
void widthChanged();
111112
void heightChanged();
113+
void regionsChanged();
112114
void childrenChanged();
113115

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

124126
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-
133127
QQuickItem* mItem = nullptr;
134128

135129
qint32 mX = 0;

0 commit comments

Comments
 (0)