diff --git a/libnymea-app-core/devicesproxy.cpp b/libnymea-app-core/devicesproxy.cpp index e59b3c35..b9e2404d 100644 --- a/libnymea-app-core/devicesproxy.cpp +++ b/libnymea-app-core/devicesproxy.cpp @@ -27,27 +27,61 @@ DevicesProxy::DevicesProxy(QObject *parent) : QSortFilterProxyModel(parent) { - connect(Engine::instance()->tagsManager()->tags(), &Tags::countChanged, this, &DevicesProxy::invalidateFilter); } -QAbstractItemModel *DevicesProxy::devices() const +Engine *DevicesProxy::engine() const { - return m_devices; + return m_engine; } -void DevicesProxy::setDevices(QAbstractItemModel *devices) +void DevicesProxy::setEngine(Engine *engine) { - if (m_devices != devices) { - m_devices = devices; - setSourceModel(devices); + if (m_engine != engine) { + m_engine = engine; + connect(m_engine->tagsManager()->tags(), &Tags::countChanged, this, &DevicesProxy::invalidateFilter); + emit engineChanged(); + + if (!sourceModel()) { + setSourceModel(m_engine->deviceManager()->devices()); + + setSortRole(Devices::RoleName); + sort(0); + connect(sourceModel(), SIGNAL(countChanged()), this, SIGNAL(countChanged())); + connect(sourceModel(), &QAbstractItemModel::dataChanged, this, [this]() { + invalidateFilter(); + emit countChanged(); + }); + + } + } +} + +DevicesProxy *DevicesProxy::parentProxy() const +{ + return m_parentProxy; +} + +void DevicesProxy::setParentProxy(DevicesProxy *parentProxy) +{ + if (m_parentProxy != parentProxy) { + m_parentProxy = parentProxy; + setSourceModel(parentProxy); + setSortRole(Devices::RoleName); sort(0); - connect(devices, SIGNAL(countChanged()), this, SIGNAL(countChanged())); - connect(devices, &QAbstractItemModel::dataChanged, this, [this]() { - invalidateFilter(); - emit countChanged(); + connect(m_parentProxy, SIGNAL(countChanged()), this, SIGNAL(countChanged())); + connect(m_parentProxy, &QAbstractItemModel::dataChanged, this, [this]() { + if (m_engine) { + invalidateFilter(); + emit countChanged(); + } }); - emit devicesChanged(); + + if (m_engine) { + invalidateFilter(); + } + + emit parentProxyChanged(); emit countChanged(); } } @@ -148,11 +182,11 @@ Device *DevicesProxy::get(int index) const Device *DevicesProxy::getInternal(int source_index) const { - Devices* d = qobject_cast(m_devices); + Devices* d = qobject_cast(sourceModel()); if (d) { return d->get(source_index); } - DevicesProxy *dp = qobject_cast(m_devices); + DevicesProxy *dp = qobject_cast(sourceModel()); if (dp) { return dp->get(source_index); } @@ -178,11 +212,11 @@ bool DevicesProxy::filterAcceptsRow(int source_row, const QModelIndex &source_pa { Device *device = getInternal(source_row); if (!m_filterTagId.isEmpty()) { - if (!Engine::instance()->tagsManager()->tags()->findDeviceTag(device->id().toString(), m_filterTagId)) { + if (!m_engine->tagsManager()->tags()->findDeviceTag(device->id().toString(), m_filterTagId)) { return false; } } - DeviceClass *deviceClass = Engine::instance()->deviceManager()->deviceClasses()->getDeviceClass(device->deviceClassId()); + DeviceClass *deviceClass = m_engine->deviceManager()->deviceClasses()->getDeviceClass(device->deviceClassId()); if (!m_shownInterfaces.isEmpty()) { bool foundMatch = false; foreach (const QString &filterInterface, m_shownInterfaces) { diff --git a/libnymea-app-core/devicesproxy.h b/libnymea-app-core/devicesproxy.h index 68b9142f..df6b4e36 100644 --- a/libnymea-app-core/devicesproxy.h +++ b/libnymea-app-core/devicesproxy.h @@ -29,11 +29,14 @@ #include "devices.h" +class Engine; + class DevicesProxy : public QSortFilterProxyModel { Q_OBJECT Q_PROPERTY(int count READ rowCount NOTIFY countChanged) - Q_PROPERTY(QAbstractItemModel *devices READ devices WRITE setDevices NOTIFY devicesChanged) + Q_PROPERTY(Engine* engine READ engine WRITE setEngine NOTIFY engineChanged) + Q_PROPERTY(DevicesProxy *parentProxy READ parentProxy WRITE setParentProxy NOTIFY parentProxyChanged) Q_PROPERTY(QString filterTagId READ filterTagId WRITE setFilterTagId NOTIFY filterTagIdChanged) Q_PROPERTY(QStringList shownInterfaces READ shownInterfaces WRITE setShownInterfaces NOTIFY shownInterfacesChanged) Q_PROPERTY(QStringList hiddenInterfaces READ hiddenInterfaces WRITE setHiddenInterfaces NOTIFY hiddenInterfacesChanged) @@ -47,10 +50,13 @@ class DevicesProxy : public QSortFilterProxyModel Q_PROPERTY(bool groupByInterface READ groupByInterface WRITE setGroupByInterface NOTIFY groupByInterfaceChanged) public: - explicit DevicesProxy(QObject *parent = 0); + explicit DevicesProxy(QObject *parent = nullptr); - QAbstractItemModel *devices() const; - void setDevices(QAbstractItemModel *devices); + Engine *engine() const; + void setEngine(Engine *engine); + + DevicesProxy *parentProxy() const; + void setParentProxy(DevicesProxy *parentProxy); QString filterTagId() const; void setFilterTagId(const QString &filterTag); @@ -73,7 +79,8 @@ public: Q_INVOKABLE Device *get(int index) const; signals: - void devicesChanged(); + void engineChanged(); + void parentProxyChanged(); void filterTagIdChanged(); void shownInterfacesChanged(); void hiddenInterfacesChanged(); @@ -85,7 +92,8 @@ signals: private: Device *getInternal(int source_index) const; - QAbstractItemModel *m_devices = nullptr; + Engine *m_engine = nullptr; + DevicesProxy *m_parentProxy = nullptr; QString m_filterTagId; QStringList m_shownInterfaces; QStringList m_hiddenInterfaces; diff --git a/nymea-app/ui/EditDevicesPage.qml b/nymea-app/ui/EditDevicesPage.qml index 5955042c..f8bf2d97 100644 --- a/nymea-app/ui/EditDevicesPage.qml +++ b/nymea-app/ui/EditDevicesPage.qml @@ -49,7 +49,7 @@ Page { anchors.fill: parent model: DevicesProxy { id: deviceProxy - devices: Engine.deviceManager.devices + engine: Engine groupByInterface: true } section.property: "baseInterface" diff --git a/nymea-app/ui/devicelistpages/DeviceListPageBase.qml b/nymea-app/ui/devicelistpages/DeviceListPageBase.qml index 1e1f649c..52f0500b 100644 --- a/nymea-app/ui/devicelistpages/DeviceListPageBase.qml +++ b/nymea-app/ui/devicelistpages/DeviceListPageBase.qml @@ -32,6 +32,6 @@ Page { DevicesProxy { id: devicesProxyInternal - devices: Engine.deviceManager.devices + engine: Engine } } diff --git a/nymea-app/ui/magic/SelectActionPage.qml b/nymea-app/ui/magic/SelectActionPage.qml index 425090fd..d109c27b 100644 --- a/nymea-app/ui/magic/SelectActionPage.qml +++ b/nymea-app/ui/magic/SelectActionPage.qml @@ -31,7 +31,7 @@ Page { DevicesProxy { id: ifaceFilterModel - devices: Engine.deviceManager.devices + engine: Engine } Component.onCompleted: { @@ -252,7 +252,7 @@ Page { model: DevicesProxy { id: lightsModel - devices: Engine.deviceManager.devices + engine: Engine shownInterfaces: ["light"] } delegate: CheckDelegate { @@ -337,7 +337,7 @@ Page { model: DevicesProxy { id: notificationsModel - devices: Engine.deviceManager.devices + engine: Engine shownInterfaces: ["notifications"] } delegate: CheckDelegate { diff --git a/nymea-app/ui/magic/SelectThingPage.qml b/nymea-app/ui/magic/SelectThingPage.qml index 56f6cf58..88e8311c 100644 --- a/nymea-app/ui/magic/SelectThingPage.qml +++ b/nymea-app/ui/magic/SelectThingPage.qml @@ -34,7 +34,7 @@ Page { DevicesProxy { id: devicesProxy - devices: Engine.deviceManager.devices + engine: Engine } ColumnLayout { diff --git a/nymea-app/ui/mainviews/DevicesPageDelegate.qml b/nymea-app/ui/mainviews/DevicesPageDelegate.qml index 38034476..8c8751d5 100644 --- a/nymea-app/ui/mainviews/DevicesPageDelegate.qml +++ b/nymea-app/ui/mainviews/DevicesPageDelegate.qml @@ -37,18 +37,20 @@ MainPageTile { DevicesProxy { id: devicesProxy - devices: Engine.deviceManager.devices + engine: Engine shownInterfaces: [model.name] } DevicesProxy { id: devicesSubProxyConnectables - devices: devicesProxy + engine: Engine + parentProxy: devicesProxy filterDisconnected: true } DevicesProxy { id: devicesSubProxyBattery - devices: devicesProxy + engine: Engine + parentProxy: devicesProxy filterBatteryCritical: true }