Commit 06caa120 authored by Arjen Hiemstra's avatar Arjen Hiemstra
Browse files

Make SensorContainer parameter of SensorObject ctor optional

This allows us to create SensorObject instances in contexts where we
don't have access to a SensorContainer.
parent 5f4d4b7b
......@@ -56,6 +56,8 @@ SensorObject *SensorContainer::object(const QString &id) const
void SensorContainer::addObject(SensorObject *object)
{
object->setParentContainer(this);
const QString id = object->id();
Q_ASSERT(!m_sensorObjects.contains(id));
m_sensorObjects[id] = object;
......@@ -72,6 +74,7 @@ void SensorContainer::removeObject(SensorObject *object)
return;
}
object->setParentContainer(nullptr);
m_sensorObjects.remove(object->id());
Q_EMIT objectRemoved(object);
}
......@@ -29,11 +29,13 @@ SensorObject::SensorObject(const QString &id, SensorContainer *parent)
SensorObject::SensorObject(const QString &id, const QString &name, SensorContainer *parent)
: QObject(parent)
, m_parent(parent)
, m_id(id)
, m_name(name)
, m_path(parent->id() + "/" + id)
{
if (parent) {
parent->addObject(this);
}
}
SensorObject::~SensorObject()
......@@ -52,7 +54,16 @@ QString SensorObject::name() const
QString SensorObject::path() const
{
return m_path;
if (!m_parent) {
return QString{};
}
return m_parent->id() % QLatin1Char('/') % m_id;
}
void SensorObject::setParentContainer(SensorContainer* parent)
{
m_parent = parent;
}
QList<SensorProperty *> SensorObject::sensors() const
......
......@@ -35,13 +35,14 @@ class Q_DECL_EXPORT SensorObject : public QObject
{
Q_OBJECT
public:
explicit SensorObject(const QString &id, const QString &name, SensorContainer *parent);
explicit SensorObject(const QString &id, SensorContainer *parent);
explicit SensorObject(const QString &id, const QString &name, SensorContainer *parent = nullptr);
explicit SensorObject(const QString &id, SensorContainer *parent = nullptr);
~SensorObject();
QString id() const;
QString name() const;
QString path() const;
void setParentContainer(SensorContainer *parent);
QList<SensorProperty *> sensors() const;
SensorProperty *sensor(const QString &sensorId) const;
......@@ -62,8 +63,8 @@ Q_SIGNALS:
void aboutToBeRemoved();
private:
SensorContainer *m_parent = nullptr;
QString m_id;
QString m_name;
QString m_path; //or keep parent refernce?
QHash<QString, SensorProperty *> m_sensors;
};
......@@ -32,9 +32,9 @@ SensorProperty::SensorProperty(const QString &id, const QString &name, SensorObj
SensorProperty::SensorProperty(const QString &id, const QString &name, const QVariant &initalValue, SensorObject *parent)
: QObject(parent)
, m_parent(parent)
, m_id(id)
{
m_path = parent->path() + "/" + m_id;
setName(name);
if (initalValue.isValid()) {
setValue(initalValue);
......@@ -58,7 +58,7 @@ QString SensorProperty::id() const
QString SensorProperty::path() const
{
return m_path;
return m_parent->path() % QLatin1Char('/') % m_id;
}
void SensorProperty::setName(const QString &name)
......
......@@ -112,6 +112,7 @@ Q_SIGNALS:
void subscribedChanged(bool);
private:
SensorObject *m_parent = nullptr;
SensorInfo m_info;
QString m_id;
QString m_path;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment