change DevicesProxy to not access the engine singleton

This commit is contained in:
Michael Zanetti 2018-08-31 18:18:02 +02:00
parent fc0f57ae07
commit 5f6a3f5f31
7 changed files with 75 additions and 31 deletions

View File

@ -27,27 +27,61 @@
DevicesProxy::DevicesProxy(QObject *parent) : DevicesProxy::DevicesProxy(QObject *parent) :
QSortFilterProxyModel(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) { if (m_engine != engine) {
m_devices = devices; m_engine = engine;
setSourceModel(devices); 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); setSortRole(Devices::RoleName);
sort(0); sort(0);
connect(devices, SIGNAL(countChanged()), this, SIGNAL(countChanged())); connect(m_parentProxy, SIGNAL(countChanged()), this, SIGNAL(countChanged()));
connect(devices, &QAbstractItemModel::dataChanged, this, [this]() { connect(m_parentProxy, &QAbstractItemModel::dataChanged, this, [this]() {
invalidateFilter(); if (m_engine) {
emit countChanged(); invalidateFilter();
emit countChanged();
}
}); });
emit devicesChanged();
if (m_engine) {
invalidateFilter();
}
emit parentProxyChanged();
emit countChanged(); emit countChanged();
} }
} }
@ -148,11 +182,11 @@ Device *DevicesProxy::get(int index) const
Device *DevicesProxy::getInternal(int source_index) const Device *DevicesProxy::getInternal(int source_index) const
{ {
Devices* d = qobject_cast<Devices*>(m_devices); Devices* d = qobject_cast<Devices*>(sourceModel());
if (d) { if (d) {
return d->get(source_index); return d->get(source_index);
} }
DevicesProxy *dp = qobject_cast<DevicesProxy*>(m_devices); DevicesProxy *dp = qobject_cast<DevicesProxy*>(sourceModel());
if (dp) { if (dp) {
return dp->get(source_index); return dp->get(source_index);
} }
@ -178,11 +212,11 @@ bool DevicesProxy::filterAcceptsRow(int source_row, const QModelIndex &source_pa
{ {
Device *device = getInternal(source_row); Device *device = getInternal(source_row);
if (!m_filterTagId.isEmpty()) { 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; return false;
} }
} }
DeviceClass *deviceClass = Engine::instance()->deviceManager()->deviceClasses()->getDeviceClass(device->deviceClassId()); DeviceClass *deviceClass = m_engine->deviceManager()->deviceClasses()->getDeviceClass(device->deviceClassId());
if (!m_shownInterfaces.isEmpty()) { if (!m_shownInterfaces.isEmpty()) {
bool foundMatch = false; bool foundMatch = false;
foreach (const QString &filterInterface, m_shownInterfaces) { foreach (const QString &filterInterface, m_shownInterfaces) {

View File

@ -29,11 +29,14 @@
#include "devices.h" #include "devices.h"
class Engine;
class DevicesProxy : public QSortFilterProxyModel class DevicesProxy : public QSortFilterProxyModel
{ {
Q_OBJECT Q_OBJECT
Q_PROPERTY(int count READ rowCount NOTIFY countChanged) 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(QString filterTagId READ filterTagId WRITE setFilterTagId NOTIFY filterTagIdChanged)
Q_PROPERTY(QStringList shownInterfaces READ shownInterfaces WRITE setShownInterfaces NOTIFY shownInterfacesChanged) Q_PROPERTY(QStringList shownInterfaces READ shownInterfaces WRITE setShownInterfaces NOTIFY shownInterfacesChanged)
Q_PROPERTY(QStringList hiddenInterfaces READ hiddenInterfaces WRITE setHiddenInterfaces NOTIFY hiddenInterfacesChanged) 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) Q_PROPERTY(bool groupByInterface READ groupByInterface WRITE setGroupByInterface NOTIFY groupByInterfaceChanged)
public: public:
explicit DevicesProxy(QObject *parent = 0); explicit DevicesProxy(QObject *parent = nullptr);
QAbstractItemModel *devices() const; Engine *engine() const;
void setDevices(QAbstractItemModel *devices); void setEngine(Engine *engine);
DevicesProxy *parentProxy() const;
void setParentProxy(DevicesProxy *parentProxy);
QString filterTagId() const; QString filterTagId() const;
void setFilterTagId(const QString &filterTag); void setFilterTagId(const QString &filterTag);
@ -73,7 +79,8 @@ public:
Q_INVOKABLE Device *get(int index) const; Q_INVOKABLE Device *get(int index) const;
signals: signals:
void devicesChanged(); void engineChanged();
void parentProxyChanged();
void filterTagIdChanged(); void filterTagIdChanged();
void shownInterfacesChanged(); void shownInterfacesChanged();
void hiddenInterfacesChanged(); void hiddenInterfacesChanged();
@ -85,7 +92,8 @@ signals:
private: private:
Device *getInternal(int source_index) const; Device *getInternal(int source_index) const;
QAbstractItemModel *m_devices = nullptr; Engine *m_engine = nullptr;
DevicesProxy *m_parentProxy = nullptr;
QString m_filterTagId; QString m_filterTagId;
QStringList m_shownInterfaces; QStringList m_shownInterfaces;
QStringList m_hiddenInterfaces; QStringList m_hiddenInterfaces;

View File

@ -49,7 +49,7 @@ Page {
anchors.fill: parent anchors.fill: parent
model: DevicesProxy { model: DevicesProxy {
id: deviceProxy id: deviceProxy
devices: Engine.deviceManager.devices engine: Engine
groupByInterface: true groupByInterface: true
} }
section.property: "baseInterface" section.property: "baseInterface"

View File

@ -32,6 +32,6 @@ Page {
DevicesProxy { DevicesProxy {
id: devicesProxyInternal id: devicesProxyInternal
devices: Engine.deviceManager.devices engine: Engine
} }
} }

View File

@ -31,7 +31,7 @@ Page {
DevicesProxy { DevicesProxy {
id: ifaceFilterModel id: ifaceFilterModel
devices: Engine.deviceManager.devices engine: Engine
} }
Component.onCompleted: { Component.onCompleted: {
@ -252,7 +252,7 @@ Page {
model: DevicesProxy { model: DevicesProxy {
id: lightsModel id: lightsModel
devices: Engine.deviceManager.devices engine: Engine
shownInterfaces: ["light"] shownInterfaces: ["light"]
} }
delegate: CheckDelegate { delegate: CheckDelegate {
@ -337,7 +337,7 @@ Page {
model: DevicesProxy { model: DevicesProxy {
id: notificationsModel id: notificationsModel
devices: Engine.deviceManager.devices engine: Engine
shownInterfaces: ["notifications"] shownInterfaces: ["notifications"]
} }
delegate: CheckDelegate { delegate: CheckDelegate {

View File

@ -34,7 +34,7 @@ Page {
DevicesProxy { DevicesProxy {
id: devicesProxy id: devicesProxy
devices: Engine.deviceManager.devices engine: Engine
} }
ColumnLayout { ColumnLayout {

View File

@ -37,18 +37,20 @@ MainPageTile {
DevicesProxy { DevicesProxy {
id: devicesProxy id: devicesProxy
devices: Engine.deviceManager.devices engine: Engine
shownInterfaces: [model.name] shownInterfaces: [model.name]
} }
DevicesProxy { DevicesProxy {
id: devicesSubProxyConnectables id: devicesSubProxyConnectables
devices: devicesProxy engine: Engine
parentProxy: devicesProxy
filterDisconnected: true filterDisconnected: true
} }
DevicesProxy { DevicesProxy {
id: devicesSubProxyBattery id: devicesSubProxyBattery
devices: devicesProxy engine: Engine
parentProxy: devicesProxy
filterBatteryCritical: true filterBatteryCritical: true
} }