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
1313PendingRegion::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
2425void 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
5974bool 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- }
0 commit comments