From b2228bcf25a57ee11c4334f7fa1c43e786a5862d Mon Sep 17 00:00:00 2001 From: Michael Zanetti Date: Wed, 3 Apr 2019 18:16:13 +0200 Subject: [PATCH] Add a method to filter Devices --- libnymea/plugin/device.cpp | 19 +++++++++++++++++++ libnymea/plugin/device.h | 3 ++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/libnymea/plugin/device.cpp b/libnymea/plugin/device.cpp index 301a2f18..d47e8b63 100644 --- a/libnymea/plugin/device.cpp +++ b/libnymea/plugin/device.cpp @@ -268,3 +268,22 @@ QDebug operator<<(QDebug dbg, Device *device) dbg.nospace() << ", deviceClassId" << device->deviceClassId() << ")"; return dbg.space(); } + +/*! Filter a Devices list by a parameter. Only Devices having a parameter of the given + \a paramTypeId will be returned. If \a value is given and it is not null, only Devices + with the given \a paramTypeId and the same \a value will be returned. + */ +Devices Devices::filterByParam(const ParamTypeId ¶mTypeId, const QVariant &value) +{ + Devices ret; + foreach (Device* device, *this) { + if (paramTypeId != paramTypeId) { + continue; + } + if (!value.isNull() && device->paramValue(paramTypeId) != value) { + continue; + } + ret << device; + } + return ret; +} diff --git a/libnymea/plugin/device.h b/libnymea/plugin/device.h index 1e17f3e9..12f8803f 100644 --- a/libnymea/plugin/device.h +++ b/libnymea/plugin/device.h @@ -96,12 +96,13 @@ private: QDebug operator<<(QDebug dbg, Device *device); -class Devices: public QList +class LIBNYMEA_EXPORT Devices: public QList { public: Devices() = default; Devices(const QList &other); Device* findById(const DeviceId &id); + Devices filterByParam(const ParamTypeId ¶mTypeId, const QVariant &value = QVariant()); }; #endif