1
1
#include " region.hpp"
2
2
#include < cmath>
3
3
4
- #include < qlist.h>
5
4
#include < qobject.h>
6
5
#include < qpoint.h>
7
6
#include < qqmllist.h>
8
7
#include < qquickitem.h>
9
8
#include < qregion.h>
10
9
#include < qtmetamacros.h>
10
+ #include < qtypes.h>
11
11
#include < qvectornd.h>
12
12
13
13
PendingRegion::PendingRegion (QObject* parent): QObject(parent) {
@@ -19,7 +19,6 @@ PendingRegion::PendingRegion(QObject* parent): QObject(parent) {
19
19
QObject::connect (this , &PendingRegion::widthChanged, this , &PendingRegion::changed);
20
20
QObject::connect (this , &PendingRegion::heightChanged, this , &PendingRegion::changed);
21
21
QObject::connect (this , &PendingRegion::childrenChanged, this , &PendingRegion::changed);
22
- QObject::connect (this , &PendingRegion::regionsChanged, this , &PendingRegion::childrenChanged);
23
22
}
24
23
25
24
void PendingRegion::setItem (QQuickItem* item) {
@@ -42,33 +41,21 @@ void PendingRegion::setItem(QQuickItem* item) {
42
41
emit this ->itemChanged ();
43
42
}
44
43
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
+ );
72
59
}
73
60
74
61
bool PendingRegion::empty () const {
@@ -130,3 +117,58 @@ QRegion PendingRegion::applyTo(const QRect& rect) const {
130
117
return this ->applyTo (baseRegion);
131
118
}
132
119
}
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
+ }
0 commit comments