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) :
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<Devices*>(m_devices);
Devices* d = qobject_cast<Devices*>(sourceModel());
if (d) {
return d->get(source_index);
}
DevicesProxy *dp = qobject_cast<DevicesProxy*>(m_devices);
DevicesProxy *dp = qobject_cast<DevicesProxy*>(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) {

View File

@ -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;

View File

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

View File

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

View File

@ -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 {

View File

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

View File

@ -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
}