all: replace list properties with ObjectModels

This commit is contained in:
outfoxxed 2024-05-23 17:28:07 -07:00
parent 6326f60ce2
commit 5016dbf0d4
Signed by: outfoxxed
GPG key ID: 4C88A185FB89301E
11 changed files with 201 additions and 152 deletions

View file

@ -4,14 +4,12 @@
#include <qdbusconnection.h>
#include <qdbusconnectioninterface.h>
#include <qdbusservicewatcher.h>
#include <qlist.h>
#include <qlogging.h>
#include <qloggingcategory.h>
#include <qobject.h>
#include <qqmllist.h>
#include <qtmetamacros.h>
#include <qtypes.h>
#include "../../core/model.hpp"
#include "player.hpp"
namespace qs::service::mpris {
@ -74,34 +72,15 @@ void MprisWatcher::onServiceUnregistered(const QString& service) {
void MprisWatcher::onPlayerReady() {
auto* player = qobject_cast<MprisPlayer*>(this->sender());
this->readyPlayers.push_back(player);
emit this->playersChanged();
this->readyPlayers.insertObject(player);
}
void MprisWatcher::onPlayerDestroyed(QObject* object) {
auto* player = static_cast<MprisPlayer*>(object); // NOLINT
if (this->readyPlayers.removeOne(player)) {
emit this->playersChanged();
}
this->readyPlayers.removeObject(player);
}
QQmlListProperty<MprisPlayer> MprisWatcher::players() {
return QQmlListProperty<MprisPlayer>(
this,
nullptr,
&MprisWatcher::playersCount,
&MprisWatcher::playerAt
);
}
qsizetype MprisWatcher::playersCount(QQmlListProperty<MprisPlayer>* property) {
return static_cast<MprisWatcher*>(property->object)->readyPlayers.count(); // NOLINT
}
MprisPlayer* MprisWatcher::playerAt(QQmlListProperty<MprisPlayer>* property, qsizetype index) {
return static_cast<MprisWatcher*>(property->object)->readyPlayers.at(index); // NOLINT
}
ObjectModel<MprisPlayer>* MprisWatcher::players() { return &this->readyPlayers; }
void MprisWatcher::registerPlayer(const QString& address) {
if (this->mPlayers.contains(address)) {

View file

@ -4,14 +4,13 @@
#include <qdbusinterface.h>
#include <qdbusservicewatcher.h>
#include <qhash.h>
#include <qlist.h>
#include <qloggingcategory.h>
#include <qobject.h>
#include <qqmlintegration.h>
#include <qqmllist.h>
#include <qtmetamacros.h>
#include <qtypes.h>
#include "../../core/model.hpp"
#include "player.hpp"
namespace qs::service::mpris {
@ -22,15 +21,12 @@ class MprisWatcher: public QObject {
QML_NAMED_ELEMENT(Mpris);
QML_SINGLETON;
/// All connected MPRIS players.
Q_PROPERTY(QQmlListProperty<MprisPlayer> players READ players NOTIFY playersChanged);
Q_PROPERTY(ObjectModel<MprisPlayer>* players READ players CONSTANT);
public:
explicit MprisWatcher(QObject* parent = nullptr);
[[nodiscard]] QQmlListProperty<MprisPlayer> players();
signals:
void playersChanged();
[[nodiscard]] ObjectModel<MprisPlayer>* players();
private slots:
void onServiceRegistered(const QString& service);
@ -39,15 +35,12 @@ private slots:
void onPlayerDestroyed(QObject* object);
private:
static qsizetype playersCount(QQmlListProperty<MprisPlayer>* property);
static MprisPlayer* playerAt(QQmlListProperty<MprisPlayer>* property, qsizetype index);
void registerExisting();
void registerPlayer(const QString& address);
QDBusServiceWatcher serviceWatcher;
QHash<QString, MprisPlayer*> mPlayers;
QList<MprisPlayer*> readyPlayers;
ObjectModel<MprisPlayer> readyPlayers {this};
};
} // namespace qs::service::mpris