forked from quickshell/quickshell
		
	Revert "core/region: use QList over QQmlListProperty for child regions"
This reverts commit 0c9c5be8dd.
Using QList breaks the default property usage.
			
			
This commit is contained in:
		
							parent
							
								
									05fbead660
								
							
						
					
					
						commit
						5ac9096c1c
					
				
					 2 changed files with 79 additions and 31 deletions
				
			
		| 
						 | 
					@ -1,13 +1,13 @@
 | 
				
			||||||
#include "region.hpp"
 | 
					#include "region.hpp"
 | 
				
			||||||
#include <cmath>
 | 
					#include <cmath>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include <qlist.h>
 | 
					 | 
				
			||||||
#include <qobject.h>
 | 
					#include <qobject.h>
 | 
				
			||||||
#include <qpoint.h>
 | 
					#include <qpoint.h>
 | 
				
			||||||
#include <qqmllist.h>
 | 
					#include <qqmllist.h>
 | 
				
			||||||
#include <qquickitem.h>
 | 
					#include <qquickitem.h>
 | 
				
			||||||
#include <qregion.h>
 | 
					#include <qregion.h>
 | 
				
			||||||
#include <qtmetamacros.h>
 | 
					#include <qtmetamacros.h>
 | 
				
			||||||
 | 
					#include <qtypes.h>
 | 
				
			||||||
#include <qvectornd.h>
 | 
					#include <qvectornd.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
PendingRegion::PendingRegion(QObject* parent): QObject(parent) {
 | 
					PendingRegion::PendingRegion(QObject* parent): QObject(parent) {
 | 
				
			||||||
| 
						 | 
					@ -19,7 +19,6 @@ PendingRegion::PendingRegion(QObject* parent): QObject(parent) {
 | 
				
			||||||
	QObject::connect(this, &PendingRegion::widthChanged, this, &PendingRegion::changed);
 | 
						QObject::connect(this, &PendingRegion::widthChanged, this, &PendingRegion::changed);
 | 
				
			||||||
	QObject::connect(this, &PendingRegion::heightChanged, this, &PendingRegion::changed);
 | 
						QObject::connect(this, &PendingRegion::heightChanged, this, &PendingRegion::changed);
 | 
				
			||||||
	QObject::connect(this, &PendingRegion::childrenChanged, this, &PendingRegion::changed);
 | 
						QObject::connect(this, &PendingRegion::childrenChanged, this, &PendingRegion::changed);
 | 
				
			||||||
	QObject::connect(this, &PendingRegion::regionsChanged, this, &PendingRegion::childrenChanged);
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void PendingRegion::setItem(QQuickItem* item) {
 | 
					void PendingRegion::setItem(QQuickItem* item) {
 | 
				
			||||||
| 
						 | 
					@ -42,33 +41,21 @@ void PendingRegion::setItem(QQuickItem* item) {
 | 
				
			||||||
	emit this->itemChanged();
 | 
						emit this->itemChanged();
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void PendingRegion::onItemDestroyed() {
 | 
					void PendingRegion::onItemDestroyed() { this->mItem = nullptr; }
 | 
				
			||||||
	this->mItem = nullptr;
 | 
					 | 
				
			||||||
	emit this->itemChanged();
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
void PendingRegion::onChildDestroyed() {
 | 
					void PendingRegion::onChildDestroyed() { this->mRegions.removeAll(this->sender()); }
 | 
				
			||||||
	this->mRegions.removeAll(this->sender());
 | 
					 | 
				
			||||||
	emit this->regionsChanged();
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
const QList<PendingRegion*>& PendingRegion::regions() const { return this->mRegions; }
 | 
					QQmlListProperty<PendingRegion> PendingRegion::regions() {
 | 
				
			||||||
 | 
						return QQmlListProperty<PendingRegion>(
 | 
				
			||||||
void PendingRegion::setRegions(const QList<PendingRegion*>& regions) {
 | 
						    this,
 | 
				
			||||||
	if (regions == this->mRegions) return;
 | 
						    nullptr,
 | 
				
			||||||
 | 
						    &PendingRegion::regionsAppend,
 | 
				
			||||||
	for (auto* region: this->mRegions) {
 | 
						    &PendingRegion::regionsCount,
 | 
				
			||||||
		QObject::disconnect(region, nullptr, this, nullptr);
 | 
						    &PendingRegion::regionAt,
 | 
				
			||||||
	}
 | 
						    &PendingRegion::regionsClear,
 | 
				
			||||||
 | 
						    &PendingRegion::regionsReplace,
 | 
				
			||||||
	this->mRegions = regions;
 | 
						    &PendingRegion::regionsRemoveLast
 | 
				
			||||||
 | 
						);
 | 
				
			||||||
	for (auto* region: regions) {
 | 
					 | 
				
			||||||
		QObject::connect(region, &QObject::destroyed, this, &PendingRegion::onChildDestroyed);
 | 
					 | 
				
			||||||
		QObject::connect(region, &PendingRegion::changed, this, &PendingRegion::childrenChanged);
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	emit this->regionsChanged();
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
bool PendingRegion::empty() const {
 | 
					bool PendingRegion::empty() const {
 | 
				
			||||||
| 
						 | 
					@ -130,3 +117,58 @@ QRegion PendingRegion::applyTo(const QRect& rect) const {
 | 
				
			||||||
		return this->applyTo(baseRegion);
 | 
							return this->applyTo(baseRegion);
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void PendingRegion::regionsAppend(QQmlListProperty<PendingRegion>* prop, PendingRegion* region) {
 | 
				
			||||||
 | 
						auto* self = static_cast<PendingRegion*>(prop->object); // NOLINT
 | 
				
			||||||
 | 
						if (!region) return;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						QObject::connect(region, &QObject::destroyed, self, &PendingRegion::onChildDestroyed);
 | 
				
			||||||
 | 
						QObject::connect(region, &PendingRegion::changed, self, &PendingRegion::childrenChanged);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						self->mRegions.append(region);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						emit self->childrenChanged();
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					PendingRegion* PendingRegion::regionAt(QQmlListProperty<PendingRegion>* prop, qsizetype i) {
 | 
				
			||||||
 | 
						return static_cast<PendingRegion*>(prop->object)->mRegions.at(i); // NOLINT
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void PendingRegion::regionsClear(QQmlListProperty<PendingRegion>* prop) {
 | 
				
			||||||
 | 
						auto* self = static_cast<PendingRegion*>(prop->object); // NOLINT
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						for (auto* region: self->mRegions) {
 | 
				
			||||||
 | 
							QObject::disconnect(region, nullptr, self, nullptr);
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						self->mRegions.clear(); // NOLINT
 | 
				
			||||||
 | 
						emit self->childrenChanged();
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					qsizetype PendingRegion::regionsCount(QQmlListProperty<PendingRegion>* prop) {
 | 
				
			||||||
 | 
						return static_cast<PendingRegion*>(prop->object)->mRegions.length(); // NOLINT
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void PendingRegion::regionsRemoveLast(QQmlListProperty<PendingRegion>* prop) {
 | 
				
			||||||
 | 
						auto* self = static_cast<PendingRegion*>(prop->object); // NOLINT
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						auto* last = self->mRegions.last();
 | 
				
			||||||
 | 
						if (last != nullptr) QObject::disconnect(last, nullptr, self, nullptr);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						self->mRegions.removeLast();
 | 
				
			||||||
 | 
						emit self->childrenChanged();
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void PendingRegion::regionsReplace(
 | 
				
			||||||
 | 
					    QQmlListProperty<PendingRegion>* prop,
 | 
				
			||||||
 | 
					    qsizetype i,
 | 
				
			||||||
 | 
					    PendingRegion* region
 | 
				
			||||||
 | 
					) {
 | 
				
			||||||
 | 
						auto* self = static_cast<PendingRegion*>(prop->object); // NOLINT
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						auto* old = self->mRegions.at(i);
 | 
				
			||||||
 | 
						if (old != nullptr) QObject::disconnect(old, nullptr, self, nullptr);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						self->mRegions.replace(i, region);
 | 
				
			||||||
 | 
						emit self->childrenChanged();
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -82,7 +82,7 @@ class PendingRegion: public QObject {
 | 
				
			||||||
	///   }
 | 
						///   }
 | 
				
			||||||
	/// }
 | 
						/// }
 | 
				
			||||||
	/// ```
 | 
						/// ```
 | 
				
			||||||
	Q_PROPERTY(QList<PendingRegion*> regions READ regions WRITE setRegions NOTIFY regionsChanged);
 | 
						Q_PROPERTY(QQmlListProperty<PendingRegion> regions READ regions);
 | 
				
			||||||
	Q_CLASSINFO("DefaultProperty", "regions");
 | 
						Q_CLASSINFO("DefaultProperty", "regions");
 | 
				
			||||||
	QML_NAMED_ELEMENT(Region);
 | 
						QML_NAMED_ELEMENT(Region);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -91,8 +91,7 @@ public:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	void setItem(QQuickItem* item);
 | 
						void setItem(QQuickItem* item);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	[[nodiscard]] const QList<PendingRegion*>& regions() const;
 | 
						QQmlListProperty<PendingRegion> regions();
 | 
				
			||||||
	void setRegions(const QList<PendingRegion*>& regions);
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	[[nodiscard]] bool empty() const;
 | 
						[[nodiscard]] bool empty() const;
 | 
				
			||||||
	[[nodiscard]] QRegion build() const;
 | 
						[[nodiscard]] QRegion build() const;
 | 
				
			||||||
| 
						 | 
					@ -110,7 +109,6 @@ signals:
 | 
				
			||||||
	void yChanged();
 | 
						void yChanged();
 | 
				
			||||||
	void widthChanged();
 | 
						void widthChanged();
 | 
				
			||||||
	void heightChanged();
 | 
						void heightChanged();
 | 
				
			||||||
	void regionsChanged();
 | 
					 | 
				
			||||||
	void childrenChanged();
 | 
						void childrenChanged();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/// Triggered when the region's geometry changes.
 | 
						/// Triggered when the region's geometry changes.
 | 
				
			||||||
| 
						 | 
					@ -124,6 +122,14 @@ private slots:
 | 
				
			||||||
	void onChildDestroyed();
 | 
						void onChildDestroyed();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
private:
 | 
					private:
 | 
				
			||||||
 | 
						static void regionsAppend(QQmlListProperty<PendingRegion>* prop, PendingRegion* region);
 | 
				
			||||||
 | 
						static PendingRegion* regionAt(QQmlListProperty<PendingRegion>* prop, qsizetype i);
 | 
				
			||||||
 | 
						static void regionsClear(QQmlListProperty<PendingRegion>* prop);
 | 
				
			||||||
 | 
						static qsizetype regionsCount(QQmlListProperty<PendingRegion>* prop);
 | 
				
			||||||
 | 
						static void regionsRemoveLast(QQmlListProperty<PendingRegion>* prop);
 | 
				
			||||||
 | 
						static void
 | 
				
			||||||
 | 
						regionsReplace(QQmlListProperty<PendingRegion>* prop, qsizetype i, PendingRegion* region);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	QQuickItem* mItem = nullptr;
 | 
						QQuickItem* mItem = nullptr;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	qint32 mX = 0;
 | 
						qint32 mX = 0;
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue