diff --git a/SHinterface.pro b/SHinterface.pro index f3a806e..f94e80d 100644 --- a/SHinterface.pro +++ b/SHinterface.pro @@ -17,12 +17,19 @@ TEMPLATE = app # deprecated API in order to know how to port your code away from it. DEFINES += QT_DEPRECATED_WARNINGS -QMAKE_CXXFLAGS += -flto -std=c++17 -O2 +QMAKE_CXXFLAGS += -std=c++17 -O2 SOURCES += \ + src/actors/factoractor.cpp \ + src/actors/polynomalactor.cpp \ src/broadcast.cpp \ src/iomuliplexer.cpp \ + src/items/messageitem.cpp \ + src/ui/actorwidgets/factoractorwidget.cpp \ + src/ui/itemcreationdialog.cpp \ + src/ui/itemsettingswidgets/messageitemsettingswidget.cpp \ src/mainobject.cpp \ + src/ui/actorwidgets/polynomalactorwidget.cpp \ src/ui/actorwidgets/sensoractorwidget.cpp \ src/ui/actorwidgets/alarmwidget.cpp \ src/ui/actorwidgets/timeractorwidget.cpp \ @@ -65,10 +72,17 @@ SOURCES += \ HEADERS += \ + src/actors/factoractor.h \ + src/actors/polynomalactor.h \ src/broadcast.h \ src/iomuliplexer.h \ + src/items/messageitem.h \ + src/ui/actorwidgets/factoractorwidget.h \ + src/ui/itemcreationdialog.h \ + src/ui/itemsettingswidgets/messageitemsettingswidget.h \ src/mainobject.h \ src/ui/actorwidgets/alarmwidget.h \ + src/ui/actorwidgets/polynomalactorwidget.h \ src/ui/actorwidgets/sensoractorwidget.h \ src/ui/actorwidgets/timeractorwidget.h \ src/ui/itemwidget.h \ @@ -108,7 +122,11 @@ HEADERS += \ src/sun.h FORMS += \ + src/ui/actorwidgets/factoractorwidget.ui \ + src/ui/itemcreationdialog.ui \ + src/ui/itemsettingswidgets/messageitemsettingswidget.ui \ src/ui/actorsettingsdialog.ui \ + src/ui/actorwidgets/polynomalactorwidget.ui \ src/ui/mainwindow.ui \ src/ui/relayscrollbox.ui \ src/ui/actorwidgets/sensoractorwidget.ui \ diff --git a/src/actors/actor.cpp b/src/actors/actor.cpp index 9466ccc..f6d4f33 100644 --- a/src/actors/actor.cpp +++ b/src/actors/actor.cpp @@ -5,8 +5,10 @@ #include "sensoractor.h" #include "timeractor.h" #include "regulator.h" +#include "polynomalactor.h" +#include "factoractor.h" -Actor::Actor(QObject *parent): QObject(parent) +Actor::Actor(QObject *parent): Item(QRandomGenerator::global()->generate(), "", 0, parent) { } @@ -19,7 +21,6 @@ void Actor::performAction() { if(active) { - trigger(); sigValue(triggerValue); } } @@ -61,17 +62,17 @@ bool Actor::isExausted() void Actor::store(QJsonObject& json) { + Item::store(json); json["Active"] = active; json["Exausted"] = exausted; - if(!name.isEmpty()) json["Name"] = name; json["TriggerValue"] = triggerValue; } -void Actor::load(const QJsonObject& json) +void Actor::load(const QJsonObject& json, const bool preserve) { + Item::load(json, preserve); active = json["Active"].toBool(); exausted = json["Exausted"].toBool(); - name = json["Name"].toString(""); triggerValue = json["TriggerValue"].toInt(); } @@ -85,12 +86,6 @@ uint8_t Actor::getTriggerValue() return triggerValue; } -QString Actor::getName() -{ - if(name.size() > 0) return name; - else return "Actor"; -} - void Actor::onValueChanged(uint8_t value) { @@ -103,6 +98,8 @@ Actor* Actor::createActor(const QString& type) else if(type == "Sensor") actor = new SensorActor(); else if(type == "Timer") actor = new TimerActor(); else if(type == "Regulator") actor = new Regulator(); + else if(type == "Polynomal") actor = new PolynomalActor(); + else if(type == "MultiFactor") actor = new MultiFactorActor(); else if(type == "Actor") actor = new Actor(); return actor; } @@ -114,3 +111,10 @@ Actor* Actor::loadActor(const QJsonObject &json) if(actor) actor->load(json); return actor; } + +void Actor::setValue(uint8_t value) +{ + Item::setValue(value); + setActive(value); +} + diff --git a/src/actors/actor.h b/src/actors/actor.h index 4a36b78..6de8b4d 100644 --- a/src/actors/actor.h +++ b/src/actors/actor.h @@ -5,7 +5,9 @@ #include #include -class Actor : public QObject +#include "../items/item.h" + +class Actor : public Item { Q_OBJECT public: @@ -18,12 +20,9 @@ protected: void performAction(); - QString name; - signals: void sigValue(uint8_t value); - void trigger(); public slots: virtual void makeActive(); @@ -31,6 +30,7 @@ public slots: virtual void setActive(uint8_t state); virtual void onValueChanged(uint8_t state); + virtual void setValue(uint8_t value); public: Actor(QObject* parent = nullptr); @@ -39,8 +39,6 @@ public: virtual QString actionName(); - virtual QString getName(); - bool isActive(); void setTriggerValue(uint8_t value); @@ -49,7 +47,7 @@ public: static Actor* createActor(const QString& type); virtual void store(QJsonObject& json); - virtual void load(const QJsonObject& json); + virtual void load(const QJsonObject& json, const bool preserve = false); static Actor* loadActor(const QJsonObject& json); }; diff --git a/src/actors/alarmtime.cpp b/src/actors/alarmtime.cpp index 11b1e1e..7240689 100644 --- a/src/actors/alarmtime.cpp +++ b/src/actors/alarmtime.cpp @@ -4,6 +4,7 @@ AlarmTime::AlarmTime(const QDateTime time, QObject *parent) : Actor(parent), tim { connect(&timer, SIGNAL(timeout()), this, SLOT(doTick())); timer.setInterval(1000); + run(); } AlarmTime::~AlarmTime() @@ -26,9 +27,9 @@ void AlarmTime::makeActive() run(); } -QString AlarmTime::getName() +QString AlarmTime::getName() const { - if(name.size() > 0)return name; + if(name_.size() > 0)return name_; else { QString string; @@ -111,6 +112,7 @@ void AlarmTime::doTick() void AlarmTime::changeTime(const QDateTime& time) { time_=time; + qDebug()<<"time: "< activationTime ) + { + performAction(); + } + bool exausted = true; + for(size_t i = 0; i < getActors().size(); ++i) if(!getActors()[i]->isExausted()) exausted = false; + } +} + +QString MultiFactorActor::getName() const +{ + if(name_.size() > 0) return name_; + else + { + QString string; + string = "Multi Factor \"" + (factorActor_ ? factorActor_->getName() : "NULL") + "\""; + return string; + } +} + +void MultiFactorActor::setFactorActor(Actor* factorActor) +{ + if(factorActor_) delete factorActor_; + factorActor_=factorActor; + connect(factorActor_, &Actor::sigValue, this, &MultiFactorActor::factorActorSlot); +} + +void MultiFactorActor::store(QJsonObject &json) +{ + json["Type"] = "MultiFactor"; + Actor::store(json); + json["PreCancleMinutes"] = static_cast(preCancleMin_); + json["FactorDirection"] = factorDirection; + QJsonObject factorActorObject; + if(factorActor_) + { + factorActor_->store(factorActorObject); + } +} + +void MultiFactorActor::load(const QJsonObject &json, bool preserve) +{ + Actor::load(json, preserve); + preCancleMin_ = static_cast(json["PreCancleMinutes"].toInt(10)); + factorDirection = json["FacotorDirection"].toBool(true); + if(json["FactorActor"].isObject()) + { + factorActor_ = Actor::loadActor(json["FactorActor"].toObject()); + } + if(factorActor_) + { + connect(factorActor_, &Actor::sigValue, this, &MultiFactorActor::factorActorSlot); + } +} + + diff --git a/src/actors/factoractor.h b/src/actors/factoractor.h new file mode 100644 index 0000000..ae011ff --- /dev/null +++ b/src/actors/factoractor.h @@ -0,0 +1,44 @@ +#ifndef REMINDERACTOR_H +#define REMINDERACTOR_H + +#include +#include "actor.h" + +class MultiFactorActor: public Actor +{ +private: + + Actor* factorActor_; + QDateTime activationTime; + uint preCancleMin_; + + bool factorDirection = true; + +private slots: + + void factorActorSlot(uint8_t value); + +public slots: + + virtual void setValue(uint8_t value); + +public: + + MultiFactorActor(Actor* FactorActor = nullptr, const uint preCancleMin = 10, QObject *parent = nullptr); + + virtual QString getName() const; + + void setFactorActor(Actor* factorActor); + Actor* getFactorActor(){return factorActor_;} + void setFactorDirection(const bool direction){factorDirection = direction;} + bool getFactorDirection(){return factorDirection;} + uint getPreCancleTime(){return preCancleMin_;} + void setPreCancleTime(uint minutes){preCancleMin_ = minutes;} + + virtual ~MultiFactorActor(){} + + virtual void store(QJsonObject& json); + virtual void load(const QJsonObject& json, bool preserve); +}; + +#endif // REMINDERACTOR_H diff --git a/src/actors/polynomalactor.cpp b/src/actors/polynomalactor.cpp new file mode 100644 index 0000000..7ed9bc5 --- /dev/null +++ b/src/actors/polynomalactor.cpp @@ -0,0 +1,83 @@ +#include "polynomalactor.h" + +PolynomalActor::PolynomalActor(const Sensor sensor, QObject* parent): Actor(parent), sensor_(sensor) +{ + +} + +PolynomalActor::PolynomalActor(QObject* parent): Actor(parent) +{ + +} + +void PolynomalActor::setSensor(const Sensor sensor) +{ + sensor_ = sensor; +} + + +void PolynomalActor::setCoeffiancts( const double pow3, const double pow2, const double pow1, const double pow0 ) +{ + pow3_=pow3; + pow2_=pow2; + pow1_=pow1; + pow0_=pow0; +} +void PolynomalActor::getCoeffiancts( double& pow3, double& pow2, double& pow1, double& pow0 ) +{ + pow3=pow3_; + pow2=pow2_; + pow1=pow1_; + pow0=pow0_; +} + +void PolynomalActor::sensorEvent(Sensor sensor) +{ + if(active && sensor == sensor_) + { + double result = pow3_*(sensor.field*sensor.field*sensor.field)+pow2_*(sensor.field*sensor.field)+pow1_*sensor.field+pow0_; + if(result < 0) result = 0; + else if(result > 254) result = 255; + if(result != prevValue)sigValue(static_cast(result)); + prevValue = result; + } +} + +void PolynomalActor::store(QJsonObject& json) +{ + json["Type"] = "Polynomal"; + Actor::store(json); + json["Pow3"] = pow3_; + json["Pow2"] = pow2_; + json["Pow1"] = pow1_; + json["Pow0"] = pow0_; + json["SensorType"] = static_cast(sensor_.type); + json["SensorId"] = static_cast(sensor_.id); + json["SensorField"] = sensor_.field; + json["SensorName"] = sensor_.name; +} + +void PolynomalActor::load(const QJsonObject& json, bool preserve) +{ + Actor::load(json, preserve); + pow3_ = json["Pow3"].toDouble(0); + pow2_ = json["Pow2"].toDouble(0); + pow1_ = json["Pow1"].toDouble(1); + pow0_ = json["Pow0"].toDouble(0); + sensor_.type = json["SensorType"].toInt(0); + sensor_.id = json["SensorId"].toInt(0); + sensor_.field = json["SensorField"].toInt(0); + sensor_.name = json["SensorName"].toString("Sensor"); +} + +QString PolynomalActor::getName() const +{ + if(name_.size() > 0) return name_; + else + { + QString string; + string = QString::number(pow3_) + "x^2 + " + QString::number(pow2_) + "x^2 + " + QString::number(pow1_) + "x + " + QString::number(pow0_) + " (x: " + sensor_.name + ")"; + return string; + } +} + diff --git a/src/actors/polynomalactor.h b/src/actors/polynomalactor.h new file mode 100644 index 0000000..4dfbcd7 --- /dev/null +++ b/src/actors/polynomalactor.h @@ -0,0 +1,39 @@ +#ifndef POLYNOMALACTOR_H +#define POLYNOMALACTOR_H +#include "actor.h" +#include "../sensors/sensor.h" + +class PolynomalActor: public Actor +{ + Q_OBJECT + +private: + Sensor sensor_; + double pow3_ = 0; + double pow2_ = 0; + double pow1_ = 1; + double pow0_ = 0; + + double prevValue = -1; + +public slots: + + void sensorEvent(Sensor sensor); + +public: + + void setCoeffiancts( const double pow3, const double pow2, const double pow1, const double pow0 ); + void getCoeffiancts( double& pow3, double& pow2, double& pow1, double& pow0 ); + + PolynomalActor(const Sensor sensor, QObject* parent = nullptr); + PolynomalActor(QObject* parent = nullptr); + void setSensor(const Sensor sensor); + Sensor getSensor(){return sensor_;} + virtual QString getName() const; + virtual ~PolynomalActor(){} + + virtual void store(QJsonObject& json); + virtual void load(const QJsonObject& json, bool preserve); +}; + +#endif // POLYNOMALACTOR_H diff --git a/src/actors/regulator.cpp b/src/actors/regulator.cpp index 3f72518..d68eb4b 100644 --- a/src/actors/regulator.cpp +++ b/src/actors/regulator.cpp @@ -22,16 +22,15 @@ void Regulator::sensorEvent(Sensor sensor) if(active && sensor == sensor_) { qDebug()<<"got sensor: "< setPoint_-band_) ) + if( sensor.field < setPoint_-band_ && (sensor.field < sensor_.field || sensor_.field > setPoint_-band_ || first) ) { - trigger(); sigValue(triggerValue); } - else if( sensor.field > setPoint_+band_ && (sensor.field > sensor_.field || sensor_.field < setPoint_+band_) ) + else if( sensor.field > setPoint_+band_ && (sensor.field > sensor_.field || sensor_.field < setPoint_+band_ || first) ) { - trigger(); sigValue(!triggerValue); } + first = false; sensor_ = sensor; } } @@ -63,9 +62,9 @@ void Regulator::store(QJsonObject& json) json["SensorName"] = sensor_.name; } -void Regulator::load(const QJsonObject& json) +void Regulator::load(const QJsonObject& json, bool preserve) { - Actor::load(json); + Actor::load(json, preserve); band_ = json["Band"].toInt(1); setPoint_ = json["SetPoint"].toInt(22); sensor_.type = json["SensorType"].toInt(0); @@ -74,9 +73,9 @@ void Regulator::load(const QJsonObject& json) sensor_.name = json["SensorName"].toString("Sensor"); } -QString Regulator::getName() +QString Regulator::getName() const { - if(name.size() > 0) return name; + if(name_.size() > 0) return name_; else { QString string; diff --git a/src/actors/regulator.h b/src/actors/regulator.h index 2626b2d..8c41e3c 100644 --- a/src/actors/regulator.h +++ b/src/actors/regulator.h @@ -12,6 +12,8 @@ private: float band_ = 1; bool invert_ = false; + bool first = true; + public slots: void sensorEvent(Sensor sensor); @@ -28,9 +30,9 @@ public: Regulator(const Sensor sensor, QObject* parent = nullptr); Regulator(QObject* parent = nullptr); Sensor getSensor(){return sensor_;} - virtual QString getName(); + virtual QString getName() const; virtual ~Regulator(){} virtual void store(QJsonObject& json); - virtual void load(const QJsonObject& json); + virtual void load(const QJsonObject& json, bool preserve); }; diff --git a/src/actors/sensoractor.cpp b/src/actors/sensoractor.cpp index 3832c24..8413011 100644 --- a/src/actors/sensoractor.cpp +++ b/src/actors/sensoractor.cpp @@ -59,9 +59,9 @@ void SensorActor::store(QJsonObject& json) json["SensorName"] = sensor_.name; } -void SensorActor::load(const QJsonObject& json) +void SensorActor::load(const QJsonObject& json, bool preserve) { - Actor::load(json); + Actor::load(json, preserve); sloap_ = json["Sloap"].toInt(0); threshold_ = json["Threshold"].toDouble(0); sensor_.type = json["SensorType"].toInt(0); @@ -70,9 +70,9 @@ void SensorActor::load(const QJsonObject& json) sensor_.name = json["SensorName"].toString("Sensor"); } -QString SensorActor::getName() +QString SensorActor::getName() const { - if(name.size() > 0) return name; + if(name_.size() > 0) return name_; else { QString string; diff --git a/src/actors/sensoractor.h b/src/actors/sensoractor.h index c638074..76241ab 100644 --- a/src/actors/sensoractor.h +++ b/src/actors/sensoractor.h @@ -29,14 +29,14 @@ public: SensorActor(const Sensor sensor, QObject* parent = nullptr); SensorActor(QObject* parent = nullptr); Sensor getSensor(){return sensor_;} - virtual QString getName(); + virtual QString getName() const; virtual ~SensorActor(){} float getThreshold(); uint8_t getSloap(); virtual void store(QJsonObject& json); - virtual void load(const QJsonObject& json); + virtual void load(const QJsonObject& json, bool preserve); }; diff --git a/src/actors/timeractor.cpp b/src/actors/timeractor.cpp index 99ff340..98b1b51 100644 --- a/src/actors/timeractor.cpp +++ b/src/actors/timeractor.cpp @@ -24,9 +24,9 @@ void TimerActor::store(QJsonObject& json) json["Timeout"] = timeoutMsec_; } -void TimerActor::load(const QJsonObject& json) +void TimerActor::load(const QJsonObject& json, bool preserve) { - Actor::load(json); + Actor::load(json, preserve); timeoutMsec_ = json["Timeout"].toInt(10000); } @@ -45,9 +45,9 @@ void TimerActor::timeout() performAction(); } -QString TimerActor::getName() +QString TimerActor::getName() const { - if(name.size() > 0) return name; + if(name_.size() > 0) return name_; else { QString string; diff --git a/src/actors/timeractor.h b/src/actors/timeractor.h index db4ef66..973a8c5 100644 --- a/src/actors/timeractor.h +++ b/src/actors/timeractor.h @@ -21,10 +21,10 @@ public slots: public: explicit TimerActor(const int timeoutSec = 60, QObject *parent = nullptr); - virtual QString getName(); + virtual QString getName() const; int getTimeout(); virtual void store(QJsonObject& json); - virtual void load(const QJsonObject& json); + virtual void load(const QJsonObject& json, bool preserve); }; diff --git a/src/broadcast.cpp b/src/broadcast.cpp index 02ba768..75af2e1 100644 --- a/src/broadcast.cpp +++ b/src/broadcast.cpp @@ -81,12 +81,17 @@ void BroadCast::decode() qDebug()<<"after parse:"; qDebug()<= 6 && buffer_[0] == 'S' && buffer_[1] == 'E' && buffer_[2] == 'N' && buffer_[3] == 'S' && buffer_[4] == 'O' && buffer_[5] == 'R') + { + Sensor sensor = Sensor::sensorFromString(buffer_); + if(sensor.type != Sensor::TYPE_DUMMY) gotSensorState(sensor); + } } void BroadCast::readyRead() @@ -106,4 +111,5 @@ void BroadCast::readyRead() } else buffer_.clear(); } + else if(buffer_.contains('\n')) buffer_.clear(); } diff --git a/src/broadcast.h b/src/broadcast.h index f7964b7..3a46622 100644 --- a/src/broadcast.h +++ b/src/broadcast.h @@ -4,6 +4,7 @@ #include #include #include +#include "sensors/sensor.h" class BroadCast: public QObject { @@ -38,6 +39,7 @@ signals: void jsonRequested(); void gotJson(QJsonObject json); + void gotSensorState(Sensor sensor); public: diff --git a/src/items/auxitem.cpp b/src/items/auxitem.cpp index ea7acee..8f104c5 100644 --- a/src/items/auxitem.cpp +++ b/src/items/auxitem.cpp @@ -1,6 +1,6 @@ #include "auxitem.h" -AuxItem::AuxItem(SensorStore* sensors, Microcontroller* micro, uint32_t itemIdIn, QString name, uint8_t value, QObject* parent): Item(sensors, itemIdIn, name, value, parent), micro_(micro) +AuxItem::AuxItem(Microcontroller* micro, uint32_t itemIdIn, QString name, uint8_t value, QObject* parent): Item(itemIdIn, name, value, parent), micro_(micro) { } diff --git a/src/items/auxitem.h b/src/items/auxitem.h index 0821dc8..a1e6765 100644 --- a/src/items/auxitem.h +++ b/src/items/auxitem.h @@ -14,7 +14,7 @@ public slots: virtual void setValue(uint8_t value); public: - AuxItem(SensorStore* sensors, Microcontroller* micro, uint32_t itemIdIn = QRandomGenerator::global()->generate(), QString name = "", uint8_t value = 0, QObject* parent = nullptr); + AuxItem(Microcontroller* micro, uint32_t itemIdIn = QRandomGenerator::global()->generate(), QString name = "", uint8_t value = 0, QObject* parent = nullptr); virtual void store(QJsonObject& json); }; diff --git a/src/items/item.cpp b/src/items/item.cpp index b4c1c51..149b259 100644 --- a/src/items/item.cpp +++ b/src/items/item.cpp @@ -4,6 +4,7 @@ #include "../microcontroller.h" #include "../actors/sensoractor.h" #include "../actors/regulator.h" +#include "../actors/polynomalactor.h" #include @@ -37,32 +38,34 @@ uint32_t ItemData::id() const bool Item::secondaryFlag = false; -Item::Item(SensorStore* sensors, uint32_t itemIdIn, QString name, uint8_t value, QObject *parent): QObject(parent), ItemData (itemIdIn, name, value), sensors_(sensors) +Item::Item(uint32_t itemIdIn, QString name, uint8_t value, QObject *parent): QObject(parent), ItemData (itemIdIn, name, value) { } -Item::Item(SensorStore* sensors, const ItemData& itemData, QObject *parent): QObject(parent), ItemData(itemData), sensors_(sensors) +Item::Item(const ItemData& itemData, QObject *parent): QObject(parent), ItemData(itemData) { } -bool Item::actorsActive() const +Item::~Item() { - return actorsActive_; + for(size_t i = 0; i < actors_.size(); i++) delete actors_[i]; } void Item::store(QJsonObject &json) { json["Name"] = name_; json["ItemId"] = static_cast(itemId_); - json["ActorsActive"] = actorsActive_; QJsonArray actorsArray; for(size_t i = 0; i < actors_.size(); ++i) { - QJsonObject actorObject; - actors_[i]->store(actorObject); - actorsArray.append(actorObject); + if(!actors_[i]->isExausted()) + { + QJsonObject actorObject; + actors_[i]->store(actorObject); + actorsArray.append(actorObject); + } } json["Actors"] = actorsArray; } @@ -74,7 +77,6 @@ void Item::load(const QJsonObject &json, const bool preserve) name_ = json["Name"].toString(name_); itemId_ = static_cast(json["ItemId"].toDouble(0)); } - actorsActive_ = json["ActorsActive"].toBool(true); const QJsonArray actorsArray(json["Actors"].toArray(QJsonArray())); for(int i = 0; i < actorsArray.size(); ++i) { @@ -101,17 +103,21 @@ void Item::addActor(Actor* actor) { actor->setParent(this); actors_.push_back(actor); - if(!secondaryFlag)connect(actor, &Actor::sigValue, this, &Item::setValue); + if(!secondaryFlag) + { + qDebug()<<"connecting actor"; + connect(actor, &Actor::sigValue, this, &Item::setValue); + } connect(this, &Item::valueChanged, actor, &Actor::onValueChanged); SensorActor* sensorActor = dynamic_cast(actor); - if(sensorActor != nullptr && sensors_ != nullptr)connect(sensors_, &SensorStore::sensorChangedState, sensorActor, &SensorActor::sensorEvent); + if(sensorActor != nullptr )connect(&globalSensors, &SensorStore::sensorChangedState, sensorActor, &SensorActor::sensorEvent); Regulator* regulator = dynamic_cast(actor); - if(regulator != nullptr && sensors_ != nullptr)connect(sensors_, &SensorStore::sensorChangedState, regulator, &Regulator::sensorEvent); + if(regulator != nullptr )connect(&globalSensors, &SensorStore::sensorChangedState, regulator, &Regulator::sensorEvent); - if(actorsActive_) actor->makeActive(); - else actor->makeInactive(); + PolynomalActor* polynomalActor = dynamic_cast(actor); + if(polynomalActor != nullptr )connect(&globalSensors, &SensorStore::sensorChangedState, polynomalActor, &PolynomalActor::sensorEvent); } bool Item::removeActor(Actor* actor) @@ -146,6 +152,5 @@ bool Item::hasActors() void Item::setActorsActive(bool in) { - actorsActive_ = in; for(unsigned i = 0; i < actors_.size(); i++) in ? actors_[i]->makeActive() : actors_[i]->makeInactive(); } diff --git a/src/items/item.h b/src/items/item.h index 7f55fab..de4947b 100644 --- a/src/items/item.h +++ b/src/items/item.h @@ -5,8 +5,7 @@ #include #include -#include "../actors/actor.h" -#include "../sensors/sensor.h" +class Actor; class ItemData { @@ -24,9 +23,9 @@ public: uint32_t id() const; - QString getName() const; void setName(QString name); uint8_t getValue() const; + virtual QString getName() const; }; @@ -35,12 +34,10 @@ class Item: public QObject, public ItemData Q_OBJECT private: std::vector< Actor* > actors_; - bool actorsActive_ = true; public: static bool secondaryFlag; - SensorStore* sensors_; signals: @@ -52,19 +49,18 @@ public slots: public: - Item(SensorStore* sensors = nullptr, uint32_t itemIdIn = QRandomGenerator::global()->generate(), QString name = "Item", uint8_t value = 0, QObject *parent = nullptr); - Item(SensorStore* sensors, const ItemData& itemData, QObject *parent = nullptr); + Item(uint32_t itemIdIn = QRandomGenerator::global()->generate(), QString name = "Item", uint8_t value = 0, QObject *parent = nullptr); + Item(const ItemData& itemData, QObject *parent = nullptr); + + virtual ~Item(); std::vector< Actor* >& getActors(); bool hasActors(); void addActor(Actor* actor); bool removeActor(Actor* actor); void removeAllActors(); - bool actorsActive() const; void setActorsActive(bool in); void informValue(uint8_t value); - void setSensorStore(SensorStore* sensors){sensors_ = sensors;} - SensorStore* getSensorStore(){return sensors_;} virtual void store(QJsonObject& json); virtual void load(const QJsonObject& json, const bool preserve = false); diff --git a/src/items/itemstore.cpp b/src/items/itemstore.cpp index e3f0e59..3dcdb78 100644 --- a/src/items/itemstore.cpp +++ b/src/items/itemstore.cpp @@ -1,8 +1,9 @@ #include "itemstore.h" #include "relay.h" +#include "messageitem.h" #include -ItemStore::ItemStore(SensorStore* sensors, QObject *parent): QObject(parent), sensors_(sensors) +ItemStore::ItemStore(QObject *parent): QObject(parent) { } @@ -13,7 +14,6 @@ void ItemStore::addItem(std::shared_ptr item) if(!mached) { items_.push_back(std::shared_ptr(item)); - items_.back()->setSensorStore(sensors_); itemAdded(std::weak_ptr(items_.back())); qDebug()<<"item added"; } @@ -49,6 +49,18 @@ void ItemStore::addItems(const std::vector>& itemIn) } +void ItemStore::removeItem(const ItemData& item) +{ + for(unsigned j = 0; j < items_.size(); j++) + { + if(item == *items_[j]) + { + items_.erase(items_.begin()+j); + --j; + } + } +} + void ItemStore::clear() { @@ -91,10 +103,14 @@ void ItemStore::load(const QJsonObject& json, Microcontroller * const micro) if(itemsArray[i].isObject()) { const QJsonObject itemObject = itemsArray[i].toObject(); - std::shared_ptr newItem; + std::shared_ptr newItem; if(itemObject["Type"].toString("") == "Relay") { - newItem = std::shared_ptr(new Relay(sensors_, micro)); + newItem = std::shared_ptr(new Relay(micro)); + } + else if(itemObject["Type"].toString("") == "Message") + { + newItem = std::shared_ptr(new MessageItem); } else if(itemObject["Type"].toString("") == "Aux") { diff --git a/src/items/itemstore.h b/src/items/itemstore.h index 405abea..274e999 100644 --- a/src/items/itemstore.h +++ b/src/items/itemstore.h @@ -13,11 +13,9 @@ class ItemStore: public QObject private: std::vector< std::shared_ptr > items_; - SensorStore* sensors_; - public: - ItemStore(SensorStore* sensors_ = nullptr, QObject *parent = nullptr); + ItemStore(QObject *parent = nullptr); virtual ~ItemStore(){} inline std::vector< std::shared_ptr >* getItems(){ return &items_; } @@ -34,6 +32,7 @@ signals: public slots: + void removeItem(const ItemData& item); void addItem(std::shared_ptr item); void addItems(const std::vector>& itemsIn); void itemStateChanged(const ItemData& item); diff --git a/src/items/messageitem.cpp b/src/items/messageitem.cpp new file mode 100644 index 0000000..1823a6d --- /dev/null +++ b/src/items/messageitem.cpp @@ -0,0 +1,72 @@ +#include "messageitem.h" + +#include + +MessageItem::MessageItem(uint32_t itemIdIn, QString name, uint8_t value, QObject *parent): + Item(itemIdIn, name, value, parent) +{ + +} + +MessageItem::MessageItem(const ItemData& itemData, QObject *parent): + Item(itemData, parent) +{ + +} + +MessageItem::~MessageItem() +{ + closeMessageBox(); +} + +void MessageItem::setValue(uint8_t value) +{ + Item::setValue(value); + if(value && !messageBox_) + { + messageBox_ = new QMessageBox(QMessageBox::NoIcon, name_, message_); + messageBox_->setModal(false); + connect(messageBox_, &QMessageBox::finished, this, &MessageItem::closeMessageBox); + messageBox_->show(); + //QTimer::singleShot(600000, this, &MessageItem::closeMessageBox); + } + else if(!value && messageBox_) + { + closeMessageBox(); + } +} + + +void MessageItem::closeMessageBox() +{ + if(messageBox_) + { + value_ = 0; + messageBox_->hide(); + delete messageBox_; + messageBox_ = nullptr; + } +} + +void MessageItem::setMessage(const QString& in) +{ + message_ = in; +} + +QString MessageItem::getMessage() +{ + return message_; +} + +void MessageItem::store(QJsonObject &json) +{ + json["Type"] = "Message"; + Item::store(json); + json["Message"] = message_; +} + +void MessageItem::load(const QJsonObject &json, const bool preserve) +{ + Item::load(json,preserve); + message_ = json["Message"].toString("Invalid Message"); +} diff --git a/src/items/messageitem.h b/src/items/messageitem.h new file mode 100644 index 0000000..58e8bcf --- /dev/null +++ b/src/items/messageitem.h @@ -0,0 +1,37 @@ +#ifndef MESSAGEITEM_H +#define MESSAGEITEM_H + +#include + +#include "item.h" + +class MessageItem : public Item +{ +Q_OBJECT +private: + + QString message_; + QMessageBox* messageBox_ = nullptr; + +private slots: + + void closeMessageBox(); + +public: + + virtual void setValue(uint8_t value); + +public: + + MessageItem(uint32_t itemIdIn = QRandomGenerator::global()->generate(), QString name = "Item", uint8_t value = 0, QObject *parent = nullptr); + MessageItem(const ItemData& itemData, QObject *parent = nullptr); + ~MessageItem(); + + void setMessage(const QString& in); + QString getMessage(); + + virtual void store(QJsonObject& json); + virtual void load(const QJsonObject& json, const bool preserve = false); +}; + +#endif // MESSAGEITEM_H diff --git a/src/items/poweritem.cpp b/src/items/poweritem.cpp index df09107..3bcc628 100644 --- a/src/items/poweritem.cpp +++ b/src/items/poweritem.cpp @@ -2,9 +2,9 @@ #include #include -PowerItem::PowerItem(SensorStore* sensors, uint32_t itemIdIn, QString name, uint8_t value, QObject* parent): Item(sensors, itemIdIn, name, value, parent) +PowerItem::PowerItem(uint32_t itemIdIn, QString name, uint8_t value, QObject* parent): Item(itemIdIn, name, value, parent) { - stateChanged(Sensor(Sensor::TYPE_SHUTDOWN_IMMINENT, 0, 0)); + stateChanged(Sensor(Sensor::TYPE_SHUTDOWN_IMMINENT, 0, 0, "Shutdown Imminent", true)); setValue(true); } @@ -14,7 +14,7 @@ void PowerItem::setValue(uint8_t value) if(!value) { QTimer::singleShot(5000, this, &PowerItem::timeout); - stateChanged(Sensor(Sensor::TYPE_SHUTDOWN_IMMINENT, 0, 1)); + stateChanged(Sensor(Sensor::TYPE_SHUTDOWN_IMMINENT, 0, 1, "Shutdown Imminent", true)); } } diff --git a/src/items/poweritem.h b/src/items/poweritem.h index 6cf16f0..7390f37 100644 --- a/src/items/poweritem.h +++ b/src/items/poweritem.h @@ -24,7 +24,7 @@ public slots: virtual void setValue(uint8_t value); public: - PowerItem(SensorStore* sensors, uint32_t itemIdIn = QRandomGenerator::global()->generate(), QString name = "", uint8_t value = 0, QObject* parent = nullptr); - void emmitSensor(){stateChanged(Sensor(Sensor::TYPE_SHUTDOWN_IMMINENT, 0, 0));} + PowerItem(uint32_t itemIdIn = QRandomGenerator::global()->generate(), QString name = "", uint8_t value = 0, QObject* parent = nullptr); + void emmitSensor(){stateChanged(Sensor(Sensor::TYPE_SHUTDOWN_IMMINENT, 0, 0, "Shutdown Imminent", true));} virtual void store(QJsonObject& json); }; diff --git a/src/items/relay.cpp b/src/items/relay.cpp index eb10d55..bc79ddf 100644 --- a/src/items/relay.cpp +++ b/src/items/relay.cpp @@ -3,7 +3,7 @@ //Relay -Relay::Relay(SensorStore* sensors, Microcontroller* micro, uint8_t id, QString name, uint16_t address, bool state, QObject* parent): Item(sensors, 0, name, state, parent), micro_(micro), id_(id), address_(address) +Relay::Relay(Microcontroller* micro, uint8_t id, QString name, uint16_t address, bool state, QObject* parent): Item(0, name, state, parent), micro_(micro), id_(id), address_(address) { itemId_ = address | ((uint32_t)id << 16); } @@ -38,9 +38,9 @@ void Relay::store(QJsonObject& json) json["Address"] = address_; } -void Relay::load(const QJsonObject& json) +void Relay::load(const QJsonObject& json, const bool preserve) { - Item::load(json); + Item::load(json, preserve); id_ = static_cast(json["Id"].toInt(0)); address_ = static_cast(json["Address"].toInt(0)); itemId_ = address_ | (static_cast(id_) << 16); diff --git a/src/items/relay.h b/src/items/relay.h index 4cd9f64..261aa7f 100644 --- a/src/items/relay.h +++ b/src/items/relay.h @@ -26,13 +26,13 @@ public slots: void toggle(); public: - Relay(SensorStore* sensors, Microcontroller* micro, uint8_t id = 0, QString name = "", uint16_t address = 0, bool state = false, QObject* parent = nullptr); + Relay(Microcontroller* micro, uint8_t id = 0, QString name = "", uint16_t address = 0, bool state = false, QObject* parent = nullptr); uint16_t getAddress() const; uint8_t getId() const; void setId(uint8_t id); virtual void store(QJsonObject& json); - virtual void load(const QJsonObject& json); + virtual void load(const QJsonObject& json, const bool preserve = false); }; #endif // RELAY_H diff --git a/src/items/rgbitem.cpp b/src/items/rgbitem.cpp index 0c9a7de..24ec995 100644 --- a/src/items/rgbitem.cpp +++ b/src/items/rgbitem.cpp @@ -1,6 +1,6 @@ #include "rgbitem.h" -RgbItem::RgbItem(SensorStore* sensors, Microcontroller* micro, uint32_t itemIdIn, QString name, uint8_t value, QObject* parent): Item(sensors, itemIdIn, name, value, parent), micro_(micro) +RgbItem::RgbItem(Microcontroller* micro, uint32_t itemIdIn, QString name, uint8_t value, QObject* parent): Item(itemIdIn, name, value, parent), micro_(micro) { } diff --git a/src/items/rgbitem.h b/src/items/rgbitem.h index e8826ea..d3e0235 100644 --- a/src/items/rgbitem.h +++ b/src/items/rgbitem.h @@ -14,7 +14,7 @@ public slots: virtual void setValue(uint8_t value); public: - RgbItem(SensorStore* sensors, Microcontroller* micro, uint32_t itemIdIn = QRandomGenerator::global()->generate(), QString name = "", uint8_t value = 0, QObject* parent = nullptr); + RgbItem(Microcontroller* micro, uint32_t itemIdIn = QRandomGenerator::global()->generate(), QString name = "", uint8_t value = 0, QObject* parent = nullptr); virtual void store(QJsonObject& json); }; diff --git a/src/main.cpp b/src/main.cpp index 3be9032..b13196c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -97,6 +97,9 @@ int main(int argc, char *argv[]) if(!microPort->open(QIODevice::ReadWrite)) std::cout<<"Can not open serial port "<portName().toStdString()<<". Continueing in demo mode"<<'\n'; masterIODevice = microPort; } + + MainObject mainObject(masterIODevice, parser.isSet(settingsPathOption) ? parser.value(settingsPathOption) : "", !parser.isSet(secondaryOption)); + #else QTcpSocket* microSocket = new QTcpSocket; microSocket->connectToHost("10.0.0.1", 6856, QIODevice::ReadWrite); @@ -106,14 +109,16 @@ int main(int argc, char *argv[]) return 1; } masterIODevice = microSocket; + + MainObject mainObject(masterIODevice, parser.isSet(settingsPathOption) ? parser.value(settingsPathOption) : "", !parser.isSet(secondaryOption)); #endif - MainObject mainObject(masterIODevice, parser.isSet(settingsPathOption) ? parser.value(settingsPathOption) : "", !parser.isSet(secondaryOption)); //mainwindow - MainWindow w(&mainObject.micro, &mainObject.powerItem, &mainObject.items, &mainObject.sensors, !parser.isSet(secondaryOption)); + MainWindow w(&mainObject); QObject::connect(&mainObject.micro, SIGNAL(textRecived(QString)), &w, SLOT(changeHeaderLableText(QString))); QObject::connect(&w, &MainWindow::sigBrodcast, &mainObject, &MainObject::sendJson); + QObject::connect(&w, &MainWindow::createdItem, &mainObject.items, &ItemStore::addItem); if(!mainObject.micro.connected()) w.changeHeaderLableText("No io debug only!"); w.show(); diff --git a/src/mainobject.cpp b/src/mainobject.cpp index a94d413..9f087c7 100644 --- a/src/mainobject.cpp +++ b/src/mainobject.cpp @@ -6,40 +6,35 @@ MainObject::MainObject(QIODevice* ioDevice, const QString& settingsPathIn, const masterIODevice(ioDevice), ioMultiplexer(masterIODevice), micro(ioMultiplexer.getIoDevice()), - broadCast(ioMultiplexer.getIoDevice()), + broadCast(ioMultiplexer.getIoDevice(), masterIn), settingsPath(settingsPathIn), sunSensorSource(49.884450, 8.650536), - items(&sensors), - powerItem(&sensors), - rgbItem(new RgbItem(&sensors, µ, 5487422, "Rgb Lights")), - auxItem(new AuxItem(&sensors, µ, 5487421, "Desk Light")) + rgbItem(new RgbItem(µ, 5487422, "Rgb Lights")), + auxItem(new AuxItem(µ, 5487421, "Desk Light")) { qDebug()<<"Is master:"<removeAllActors(); auxItem->removeAllActors(); + powerItem.removeAllActors(); items.addItem(rgbItem); items.addItem(auxItem); items.load(json, µ); @@ -99,7 +95,7 @@ void MainObject::sendJson() broadCast.sendJson(json); } -QJsonObject MainObject::getJsonObjectFromDisk(const QString& filePath) +QJsonObject MainObject::getJsonObjectFromDisk(const QString& filePath, bool* error) { QFile file; @@ -115,25 +111,30 @@ QJsonObject MainObject::getJsonObjectFromDisk(const QString& filePath) if(!file.isOpen()) std::cerr<<"Can not open config file: "< 0) file.setFileName(filePath); - else + if(filePath.size() == 0) #endif { - file.setFileName(QStandardPaths::writableLocation(QStandardPaths::ConfigLocation) + "/shinterface.json"); + filePath = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation) + "/shinterface.json"; } - std::cout<<"config file: "< Microcontroller::processRelayLine(const QString& buffer) if(name.size() > 1)name.remove(name.size()-1, 1); else name = "Relay " + QString::number(bufferList[1].toInt(nullptr, 2)); qDebug()<<"Relay "<( new Relay(nullptr, this, bufferList[2].toInt(), name, bufferList[4].toInt(nullptr, 2), bufferList[6].toInt())); + return std::shared_ptr( new Relay(this, bufferList[2].toInt(), name, bufferList[4].toInt(nullptr, 2), bufferList[6].toInt())); } return nullptr; } @@ -160,10 +160,8 @@ void Microcontroller::processRelayState(const QString& buffer) void Microcontroller::processSensorState(const QString& buffer) { - QStringList bufferList = buffer.split(' '); - Sensor sensor(bufferList[2].toUInt(), bufferList[4].toUInt(), bufferList[6].toUInt()); - if(sensor.type == Sensor::TYPE_HUMIDITY || sensor.type == Sensor::TYPE_TEMPERATURE) sensor.field = sensor.field/10; - if(bufferList.size() >= 7)gotSensorState(sensor); + Sensor sensor = Sensor::sensorFromString(buffer); + if(sensor.type != Sensor::TYPE_DUMMY) gotSensorState(sensor); } diff --git a/src/sensors/sensor.cpp b/src/sensors/sensor.cpp index e907877..0a95763 100644 --- a/src/sensors/sensor.cpp +++ b/src/sensors/sensor.cpp @@ -2,6 +2,8 @@ #include +SensorStore globalSensors; + SensorStore::SensorStore(QObject *parent): QObject(parent) { sensors_.push_back(Sensor(0,1,0,"Front door")); @@ -43,3 +45,5 @@ void SensorStore::sensorGotState(const Sensor& sensor) } } } + + diff --git a/src/sensors/sensor.h b/src/sensors/sensor.h index 00baf13..e8b682e 100644 --- a/src/sensors/sensor.h +++ b/src/sensors/sensor.h @@ -14,6 +14,7 @@ public: static constexpr uint8_t TYPE_HUMIDITY = 2; static constexpr uint8_t TYPE_PRESSURE = 3; static constexpr uint8_t TYPE_BRIGHTNESS = 4; + static constexpr uint8_t TYPE_BUTTON = 5; static constexpr uint8_t TYPE_LOWBATTERY = 128; static constexpr uint8_t TYPE_SHUTDOWN_IMMINENT = 251; static constexpr uint8_t TYPE_OCUPANCY = 252; @@ -26,23 +27,36 @@ public: float field; QString name; QDateTime lastSeen; + bool hidden; - Sensor(uint8_t typeIn, uint8_t idIn, float fieldIn = 0, QString nameIn = ""): type(typeIn), id(idIn), field(fieldIn), name(nameIn) + Sensor(uint8_t typeIn, uint8_t idIn, float fieldIn = 0, QString nameIn = "", bool hiddenIn = false): type(typeIn), id(idIn), field(fieldIn), name(nameIn), hidden(hiddenIn) { lastSeen = QDateTime::currentDateTime(); if(nameIn == "") generateName(); } - Sensor(QString nameIn = "dummy"): type(TYPE_DUMMY), id(0), field(0), name(nameIn) + Sensor(QString nameIn = "dummy"): type(TYPE_DUMMY), id(0), field(0), name(nameIn), hidden(false) { lastSeen = QDateTime::currentDateTime(); } - inline bool operator==(const Sensor& in){ return type==in.type && id == in.id; } - inline bool operator!=(const Sensor& in){ return !(*this==in); } + inline bool operator==(const Sensor& in) const{ return type==in.type && id == in.id; } + inline bool operator!=(const Sensor& in) const{ return !(*this==in); } inline void updateSeen(){lastSeen = QDateTime::currentDateTime();} + static Sensor sensorFromString(const QString& str) + { + QStringList bufferList = str.split(' '); + if(bufferList.size() >= 7) + { + Sensor sensor(bufferList[2].toUInt(), bufferList[4].toUInt(), bufferList[6].toUInt()); + if(sensor.type == Sensor::TYPE_HUMIDITY || sensor.type == Sensor::TYPE_TEMPERATURE) sensor.field = sensor.field/10; + return sensor; + } + else return Sensor(TYPE_DUMMY, 0, 0, "", true); + } inline void generateName() { if(type == TYPE_TEMPERATURE) name = "Temperature " + QString::number(id); else if(type == TYPE_DOOR) name = "Door " + QString::number(id); + else if(type == TYPE_BUTTON) name = "Button " + QString::number(id); else if(type == TYPE_AUDIO_OUTPUT) name = "Speakers " + QString::number(id); else if(type == TYPE_HUMIDITY) name = "Humidity " + QString::number(id); else if(type == TYPE_SUN_ALTITUDE) name = "Solar Altitude"; @@ -64,6 +78,7 @@ public: inline std::vector* getSensors(){ return &sensors_; } + public slots: void sensorGotState(const Sensor& sensor); @@ -75,3 +90,5 @@ signals: void sensorDeleted(Sensor sensor); }; + +extern SensorStore globalSensors; diff --git a/src/sun.cpp b/src/sun.cpp index 32c3dec..c6da3d5 100644 --- a/src/sun.cpp +++ b/src/sun.cpp @@ -179,9 +179,3 @@ double Sun::declination() double meanSolarAnomalyValue = meanSolarAnomaly(meanSolarNoonValue); return solarDeclination(eclipticLongitude(eqOfCenter(meanSolarAnomalyValue), meanSolarAnomalyValue)); } - - -double maximumAltitude() -{ - return 0; -} diff --git a/src/ui/actorsettingsdialog.cpp b/src/ui/actorsettingsdialog.cpp index a7f7002..9580405 100644 --- a/src/ui/actorsettingsdialog.cpp +++ b/src/ui/actorsettingsdialog.cpp @@ -1,5 +1,6 @@ #include "actorsettingsdialog.h" #include "ui_actorsettingsdialog.h" +#include "itemsettingsdialog.h" #include #include @@ -14,25 +15,25 @@ ActorSettingsDialog::ActorSettingsDialog(AlarmTime* alarm, QWidget *parent): ui->vertlayout->addWidget(widget); } -ActorSettingsDialog::ActorSettingsDialog(SensorActor* actor, SensorStore* sensors, QWidget *parent) : +ActorSettingsDialog::ActorSettingsDialog(SensorActor* actor, QWidget *parent) : QDialog(parent), actor_(actor), ui(new Ui::ActorSettingsDialog) { init(); - widget = new SensorActorWidget(actor, sensors, this); + widget = new SensorActorWidget(actor, &globalSensors, this); ui->vertlayout->addWidget(widget); } -ActorSettingsDialog::ActorSettingsDialog(Regulator* actor, SensorStore* sensors, QWidget *parent) : +ActorSettingsDialog::ActorSettingsDialog(Regulator* actor, QWidget *parent) : QDialog(parent), actor_(actor), ui(new Ui::ActorSettingsDialog) { init(); - widget = new RegulatorWdiget(actor, sensors, this); + widget = new RegulatorWdiget(actor, &globalSensors, this); ui->vertlayout->addWidget(widget); } @@ -47,6 +48,26 @@ ui(new Ui::ActorSettingsDialog) ui->vertlayout->addWidget(widget); } +ActorSettingsDialog::ActorSettingsDialog(PolynomalActor* actor, QWidget *parent) : +QDialog(parent), +actor_(actor), +ui(new Ui::ActorSettingsDialog) +{ + init(); + + widget = new PolynomalActorWidget(actor, &globalSensors, this); + ui->vertlayout->addWidget(widget); +} + +ActorSettingsDialog::ActorSettingsDialog(MultiFactorActor* actor, QWidget *parent) : +QDialog(parent), +actor_(actor), +ui(new Ui::ActorSettingsDialog) +{ + init(); + widget = new FactorActorWidget(actor, this); + ui->vertlayout->addWidget(widget); +} ActorSettingsDialog::ActorSettingsDialog(Actor* actor, QWidget *parent) : QDialog(parent), @@ -61,6 +82,7 @@ void ActorSettingsDialog::init() ui->setupUi(this); connect(ui->comboBox_action, SIGNAL(currentIndexChanged(int)), this, SLOT(changeAction(int))); connect(ui->spinBox, SIGNAL(valueChanged(int)), this, SLOT(valueChanged(int))); + connect(ui->pushButton_editItem, &QPushButton::clicked, this, &ActorSettingsDialog::editAsItem); ui->spinBox->hide(); ui->spinBox->setValue(actor_->getTriggerValue()); @@ -74,6 +96,12 @@ ActorSettingsDialog::~ActorSettingsDialog() delete ui; } +void ActorSettingsDialog::editAsItem() +{ + ItemSettingsDialog itemSettingsDiag(actor_, this); + itemSettingsDiag.exec(); +} + void ActorSettingsDialog::valueChanged(int value) { actor_->setTriggerValue(value); diff --git a/src/ui/actorsettingsdialog.h b/src/ui/actorsettingsdialog.h index 80bf4d5..48df7be 100644 --- a/src/ui/actorsettingsdialog.h +++ b/src/ui/actorsettingsdialog.h @@ -7,6 +7,8 @@ #include "actorwidgets/sensoractorwidget.h" #include "actorwidgets/timeractorwidget.h" #include "actorwidgets/regulatorwdiget.h" +#include "actorwidgets/polynomalactorwidget.h" +#include "actorwidgets/factoractorwidget.h" namespace Ui { class ActorSettingsDialog; @@ -24,9 +26,11 @@ private: public: ActorSettingsDialog(AlarmTime* actor, QWidget *parent = nullptr); - ActorSettingsDialog(SensorActor* actor, SensorStore* sensors = nullptr, QWidget *parent = nullptr); - ActorSettingsDialog(Regulator* actor, SensorStore* sensors = nullptr, QWidget *parent = nullptr); - ActorSettingsDialog(TimerActor* actor, QWidget *parent); + ActorSettingsDialog(SensorActor* actor, QWidget *parent = nullptr); + ActorSettingsDialog(Regulator* actor, QWidget *parent = nullptr); + ActorSettingsDialog(TimerActor* actor, QWidget *parent = nullptr); + ActorSettingsDialog(PolynomalActor* actor, QWidget *parent = nullptr); + ActorSettingsDialog(MultiFactorActor* actor, QWidget *parent = nullptr); ActorSettingsDialog(Actor* actor, QWidget *parent); ~ActorSettingsDialog(); @@ -35,6 +39,7 @@ public: private slots: void changeAction(int index); void valueChanged(int value); + void editAsItem(); private: diff --git a/src/ui/actorsettingsdialog.ui b/src/ui/actorsettingsdialog.ui index 0d22a9a..abead42 100644 --- a/src/ui/actorsettingsdialog.ui +++ b/src/ui/actorsettingsdialog.ui @@ -63,6 +63,13 @@ + + + + Edit as item + + + diff --git a/src/ui/actorwidgets/alarmwidget.h b/src/ui/actorwidgets/alarmwidget.h index cafbe76..a71303e 100644 --- a/src/ui/actorwidgets/alarmwidget.h +++ b/src/ui/actorwidgets/alarmwidget.h @@ -20,7 +20,7 @@ public: private slots: void toggleRepeating(int state); - void setRepeatingType(); + void setRepeatingType(); private: Ui::AlarmWidget *ui; diff --git a/src/ui/actorwidgets/factoractorwidget.cpp b/src/ui/actorwidgets/factoractorwidget.cpp new file mode 100644 index 0000000..4f27110 --- /dev/null +++ b/src/ui/actorwidgets/factoractorwidget.cpp @@ -0,0 +1,85 @@ +#include "factoractorwidget.h" +#include "ui_factoractorwidget.h" +#include "../actorsettingsdialog.h" + +FactorActorWidget::FactorActorWidget(MultiFactorActor* actor, QWidget *parent) : + QWidget(parent), + actor_(actor), + ui(new Ui::FactorActorWidget) +{ + ui->setupUi(this); + ui->comboBox->setCurrentText(actor_->getFactorDirection() ? "True" : "False"); + ui->spinBox->setValue(actor_->getPreCancleTime()); + if(actor_->getFactorActor()) ui->label_FactorActor->setText(actor_->getFactorActor()->getName()); + connect(ui->pushButton, &QPushButton::clicked, this, &FactorActorWidget::createFactorActor); + connect(ui->comboBox_Direcion, &QComboBox::currentTextChanged, this, &FactorActorWidget::setDirection); + connect(ui->spinBox, qOverload(&QSpinBox::valueChanged), this, &FactorActorWidget::setPreCancleTime); +} + +FactorActorWidget::~FactorActorWidget() +{ + delete ui; +} + +void FactorActorWidget::createFactorActor() +{ + ActorSettingsDialog* dialog = nullptr; + Actor* actor = nullptr; + + if(ui->comboBox->currentText() == "Alarm") + { + AlarmTime* alarm = new AlarmTime; + actor = alarm; + dialog = new ActorSettingsDialog(alarm, this); + } + else if(ui->comboBox->currentText() == "Sensor") + { + SensorActor* sensorActor = new SensorActor(); + actor = sensorActor; + dialog = new ActorSettingsDialog(sensorActor, this); + } + else if(ui->comboBox->currentText() == "Timer" ) + { + TimerActor* timerActor = new TimerActor(); + actor = timerActor; + dialog = new ActorSettingsDialog(timerActor, this); + } + else if(ui->comboBox->currentText() == "Regulator") + { + Regulator* regulator = new Regulator(); + actor = regulator; + dialog = new ActorSettingsDialog(regulator, this); + } + + else if(ui->comboBox->currentText() == "Polynomal") + { + PolynomalActor* polynomalActor = new PolynomalActor(); + actor = polynomalActor; + dialog = new ActorSettingsDialog(polynomalActor, this); + } + + + if(dialog != nullptr) + { + dialog->setParent(this); + dialog->show(); + if(dialog->exec() == QDialog::Accepted) + { + actor_->setFactorActor(actor); + ui->label_FactorActor->setText(actor->getName()); + } + else delete actor; + delete dialog; + } +} + +void FactorActorWidget::setDirection(const QString& type) +{ + if(type == "True") actor_->setFactorDirection(true); + else actor_->setFactorDirection(false); +} + +void FactorActorWidget::setPreCancleTime(int time) +{ + actor_->setPreCancleTime(time); +} diff --git a/src/ui/actorwidgets/factoractorwidget.h b/src/ui/actorwidgets/factoractorwidget.h new file mode 100644 index 0000000..1b2cadf --- /dev/null +++ b/src/ui/actorwidgets/factoractorwidget.h @@ -0,0 +1,30 @@ +#ifndef FACTORACTORWIDGET_H +#define FACTORACTORWIDGET_H + +#include +#include "../../actors/factoractor.h" + +namespace Ui { +class FactorActorWidget; +} + +class FactorActorWidget : public QWidget +{ + Q_OBJECT + + MultiFactorActor* actor_; + +public: + explicit FactorActorWidget(MultiFactorActor* actor, QWidget *parent = nullptr); + ~FactorActorWidget(); + +private slots: + void createFactorActor(); + void setDirection(const QString& direction); + void setPreCancleTime(int time); + +private: + Ui::FactorActorWidget *ui; +}; + +#endif // FACTORACTORWIDGET_H diff --git a/src/ui/actorwidgets/factoractorwidget.ui b/src/ui/actorwidgets/factoractorwidget.ui new file mode 100644 index 0000000..860f95f --- /dev/null +++ b/src/ui/actorwidgets/factoractorwidget.ui @@ -0,0 +1,136 @@ + + + FactorActorWidget + + + + 0 + 0 + 395 + 169 + + + + Form + + + + + + + + + Sensor + + + + + Polynomal + + + + + Alarm + + + + + Timer + + + + + Regulator + + + + + + + + None + + + + + + + + 0 + 0 + + + + Create Factor + + + + + + + Factor Direciton: + + + + + + + + True + + + + + False + + + + + + + + Current Factor: + + + + + + + + + + + Factor time tollerance + + + + + + + 10000 + + + 10 + + + + + + + + 0 + 0 + + + + Min + + + + + + + + + + diff --git a/src/ui/actorwidgets/polynomalactorwidget.cpp b/src/ui/actorwidgets/polynomalactorwidget.cpp new file mode 100644 index 0000000..509fdf4 --- /dev/null +++ b/src/ui/actorwidgets/polynomalactorwidget.cpp @@ -0,0 +1,48 @@ +#include "polynomalactorwidget.h" +#include "ui_polynomalactorwidget.h" + +PolynomalActorWidget::PolynomalActorWidget(PolynomalActor* actor, SensorStore* sensors, QWidget *parent): + QWidget(parent), + sensors_(sensors), + actor_(actor), + ui(new Ui::PolynomalActorWidget) +{ + ui->setupUi(this); + if(sensors)ui->listView->sensorsChanged(*(sensors->getSensors())); + else + { + ui->listView->hide(); + ui->label->hide(); + } + + double pow3, pow2, pow1, pow0; + + actor_->getCoeffiancts(pow3, pow2, pow1, pow0); + + ui->doubleSpinBox_pow0->setValue(pow0); + ui->doubleSpinBox_pow1->setValue(pow1); + ui->doubleSpinBox_pow2->setValue(pow2); + ui->doubleSpinBox_pow3->setValue(pow3); + + connect(ui->doubleSpinBox_pow3, &QDoubleSpinBox::editingFinished, this, &PolynomalActorWidget::setPow); + connect(ui->doubleSpinBox_pow2, &QDoubleSpinBox::editingFinished, this, &PolynomalActorWidget::setPow); + connect(ui->doubleSpinBox_pow1, &QDoubleSpinBox::editingFinished, this, &PolynomalActorWidget::setPow); + connect(ui->doubleSpinBox_pow0, &QDoubleSpinBox::editingFinished, this, &PolynomalActorWidget::setPow); + connect(ui->listView, &SensorListWidget::clicked, this, &PolynomalActorWidget::setSensor); + +} + +PolynomalActorWidget::~PolynomalActorWidget() +{ + delete ui; +} + +void PolynomalActorWidget::setPow() +{ + actor_->setCoeffiancts(ui->doubleSpinBox_pow3->value(), ui->doubleSpinBox_pow2->value(), ui->doubleSpinBox_pow1->value(), ui->doubleSpinBox_pow0->value()); +} + +void PolynomalActorWidget::setSensor(const QModelIndex &index) +{ + actor_->setSensor(sensors_->getSensors()->at(index.row())); +} diff --git a/src/ui/actorwidgets/polynomalactorwidget.h b/src/ui/actorwidgets/polynomalactorwidget.h new file mode 100644 index 0000000..dba3c55 --- /dev/null +++ b/src/ui/actorwidgets/polynomalactorwidget.h @@ -0,0 +1,30 @@ +#ifndef POLYNOMALACTORWIDGET_H +#define POLYNOMALACTORWIDGET_H + +#include +#include "../../actors/polynomalactor.h" + +namespace Ui { +class PolynomalActorWidget; +} + +class PolynomalActorWidget : public QWidget +{ + Q_OBJECT + SensorStore* sensors_; + PolynomalActor* actor_; + +public: + explicit PolynomalActorWidget(PolynomalActor* regulator, SensorStore* sensors = nullptr, QWidget *parent = nullptr); + ~PolynomalActorWidget(); + +private slots: + + void setPow(); + void setSensor(const QModelIndex &index); + +private: + Ui::PolynomalActorWidget *ui; +}; + +#endif // POLYNOMALACTORWIDGET_H diff --git a/src/ui/actorwidgets/polynomalactorwidget.ui b/src/ui/actorwidgets/polynomalactorwidget.ui new file mode 100644 index 0000000..e1733df --- /dev/null +++ b/src/ui/actorwidgets/polynomalactorwidget.ui @@ -0,0 +1,140 @@ + + + PolynomalActorWidget + + + + 0 + 0 + 420 + 306 + + + + Form + + + + + + + 0 + 0 + + + + Select Sensor + + + + + + + + 0 + 0 + + + + + + + + + + -500.000000000000000 + + + 500.000000000000000 + + + 0.000000000000000 + + + + + + + + 0 + 0 + + + + x³+ + + + + + + + -500.000000000000000 + + + 500.000000000000000 + + + + + + + + 0 + 0 + + + + x²+ + + + + + + + -500.000000000000000 + + + 500.000000000000000 + + + 1.000000000000000 + + + + + + + + 0 + 0 + + + + x+ + + + + + + + -500.000000000000000 + + + 500.000000000000000 + + + + + + + + + + SensorListWidget + QListView +
../src/ui/sensorlistwidget.h
+
+
+ + +
diff --git a/src/ui/itemcreationdialog.cpp b/src/ui/itemcreationdialog.cpp new file mode 100644 index 0000000..f3d40d3 --- /dev/null +++ b/src/ui/itemcreationdialog.cpp @@ -0,0 +1,45 @@ +#include "itemcreationdialog.h" +#include "ui_itemcreationdialog.h" + +#include "itemsettingswidgets/messageitemsettingswidget.h" + +ItemCreationDialog::ItemCreationDialog(QWidget *parent) : + QDialog(parent), + ui(new Ui::ItemCreationDialog) +{ + ui->setupUi(this); + std::shared_ptr messageItem(new MessageItem); + item = messageItem; + widget = new MessageItemSettingsWidget(messageItem, this); + ui->verticalLayout->addWidget(widget); + connect(ui->comboBox, &QComboBox::currentTextChanged, this, &ItemCreationDialog::itemTypeChanged); + connect(ui->lineEdit, &QLineEdit::textChanged, this, &ItemCreationDialog::itemNameChanged); +} + +ItemCreationDialog::~ItemCreationDialog() +{ + delete ui; + delete widget; +} + +void ItemCreationDialog::itemTypeChanged(const QString& type) +{ + ui->verticalLayout->removeWidget(widget); + delete widget; + if(type == "Message") + { + std::shared_ptr messageItem(new MessageItem); + item = messageItem; + widget = new MessageItemSettingsWidget(messageItem, this); + ui->verticalLayout_2->addWidget(widget); + } +} + +void ItemCreationDialog::itemNameChanged(const QString& name) +{ + if(item) + { + item->setName(name); + } +} + diff --git a/src/ui/itemcreationdialog.h b/src/ui/itemcreationdialog.h new file mode 100644 index 0000000..57421ae --- /dev/null +++ b/src/ui/itemcreationdialog.h @@ -0,0 +1,33 @@ +#ifndef ITEMCREATIONDIALOG_H +#define ITEMCREATIONDIALOG_H + +#include +#include +#include "../items/item.h" + +namespace Ui { +class ItemCreationDialog; +} + +class ItemCreationDialog : public QDialog +{ + Q_OBJECT + + QWidget* widget; + +public: + explicit ItemCreationDialog(QWidget *parent = nullptr); + ~ItemCreationDialog(); + + std::shared_ptr item; + +private slots: + + void itemTypeChanged(const QString& type); + void itemNameChanged(const QString& name); + +private: + Ui::ItemCreationDialog *ui; +}; + +#endif // ITEMCREATIONDIALOG_H diff --git a/src/ui/itemcreationdialog.ui b/src/ui/itemcreationdialog.ui new file mode 100644 index 0000000..f13aed6 --- /dev/null +++ b/src/ui/itemcreationdialog.ui @@ -0,0 +1,111 @@ + + + ItemCreationDialog + + + + 0 + 0 + 400 + 140 + + + + Create Item + + + + :/images/UVOSicon.bmp:/images/UVOSicon.bmp + + + + + + + + Type + + + + + + + + Message + + + + + + + + + + + + Name + + + + + + + Item + + + + + + + + + + + + Qt::Horizontal + + + QDialogButtonBox::Cancel|QDialogButtonBox::Ok + + + + + + + + + + + buttonBox + accepted() + ItemCreationDialog + accept() + + + 248 + 254 + + + 157 + 274 + + + + + buttonBox + rejected() + ItemCreationDialog + reject() + + + 316 + 260 + + + 286 + 274 + + + + + diff --git a/src/ui/itemscrollbox.cpp b/src/ui/itemscrollbox.cpp index e699c60..5a12edd 100644 --- a/src/ui/itemscrollbox.cpp +++ b/src/ui/itemscrollbox.cpp @@ -1,6 +1,7 @@ #include "itemscrollbox.h" #include "ui_relayscrollbox.h" #include "../items/auxitem.h" +#include "../items/messageitem.h" ItemScrollBox::ItemScrollBox(QWidget *parent) : QWidget(parent), @@ -24,11 +25,17 @@ void ItemScrollBox::addItem(std::weak_ptr item) { widgets_.push_back(new ItemWidget(item, true)); } + else if(dynamic_cast(workItem.get())) + { + widgets_.push_back(new ItemWidget(item, false, true)); + } else { widgets_.push_back(new ItemWidget(item)); } ui->relayWidgetVbox->addWidget(widgets_.back()); + connect(widgets_.back(), &ItemWidget::deleteRequest, this, &ItemScrollBox::deleteRequest); + connect(widgets_.back(), &ItemWidget::deleteRequest, this, &ItemScrollBox::removeItem); } } diff --git a/src/ui/itemscrollbox.h b/src/ui/itemscrollbox.h index 91274ca..18f0dff 100644 --- a/src/ui/itemscrollbox.h +++ b/src/ui/itemscrollbox.h @@ -21,6 +21,10 @@ class ItemScrollBox : public QWidget private: std::vector< ItemWidget* > widgets_; +signals: + void deleteRequest(const ItemData& item); + + public: explicit ItemScrollBox(QWidget *parent = nullptr); ~ItemScrollBox(); diff --git a/src/ui/itemsettingsdialog.cpp b/src/ui/itemsettingsdialog.cpp index 11ca109..c5df21e 100644 --- a/src/ui/itemsettingsdialog.cpp +++ b/src/ui/itemsettingsdialog.cpp @@ -5,6 +5,7 @@ #include "../actors/sensoractor.h" #include "../actors/timeractor.h" #include "../actors/regulator.h" +#include "../actors/factoractor.h" #include @@ -31,21 +32,17 @@ ItemSettingsDialog::ItemSettingsDialog(Item* item, QWidget *parent) : ui->label_address_lable->hide(); ui->label_id_lable->hide(); } - ui->checkBox_auto->setChecked(item_->actorsActive()); - connect(ui->checkBox_auto, &QCheckBox::toggled, item_, &Relay::setActorsActive); connect(ui->pushButton_add, &QPushButton::clicked, this, &ItemSettingsDialog::addActor); connect(ui->pushButton_remove, &QPushButton::clicked, this, &ItemSettingsDialog::removeActor); connect(ui->pushButton_edit, &QPushButton::clicked, this, &ItemSettingsDialog::editActor); ui->tableWidget->setHorizontalHeaderItem(0, new QTableWidgetItem("Actor")); ui->tableWidget->setHorizontalHeaderItem(1, new QTableWidgetItem("Action")); - ui->tableWidget->setHorizontalHeaderItem(2, new QTableWidgetItem("Acts on")); - ui->tableWidget->setHorizontalHeaderItem(3, new QTableWidgetItem("Enabled")); + ui->tableWidget->setHorizontalHeaderItem(2, new QTableWidgetItem("Enabled")); ui->tableWidget->horizontalHeader()->setSectionResizeMode(0, QHeaderView::Stretch); ui->tableWidget->horizontalHeader()->resizeSection(1, 60); - ui->tableWidget->horizontalHeader()->resizeSection(2, 60); - ui->tableWidget->horizontalHeader()->resizeSection(3, 75); + ui->tableWidget->horizontalHeader()->resizeSection(2, 75); loadActorList(); } @@ -63,8 +60,7 @@ void ItemSettingsDialog::loadActorList() { ui->tableWidget->setItem(i, 0, new QTableWidgetItem(item_->getActors()[i]->getName())); ui->tableWidget->setItem(i, 1, new QTableWidgetItem(item_->getActors()[i]->actionName())); - ui->tableWidget->setItem(i, 2, new QTableWidgetItem("Item")); - ui->tableWidget->setItem(i, 3, new QTableWidgetItem(item_->getActors()[i]->isActive() ? "Y" : "N")); + ui->tableWidget->setItem(i, 2, new QTableWidgetItem(item_->getActors()[i]->isActive() ? "Y" : "N")); } } @@ -79,11 +75,11 @@ void ItemSettingsDialog::addActor() actor = alarm; dialog = new ActorSettingsDialog(alarm, this); } - else if(ui->comboBox->currentText() == "Sensor" && item_->getSensorStore() != nullptr) + else if(ui->comboBox->currentText() == "Sensor") { SensorActor* sensorActor = new SensorActor(); actor = sensorActor; - dialog = new ActorSettingsDialog(sensorActor, item_->getSensorStore(), this); + dialog = new ActorSettingsDialog(sensorActor, this); } else if(ui->comboBox->currentText() == "Timer" ) { @@ -91,11 +87,25 @@ void ItemSettingsDialog::addActor() actor = timerActor; dialog = new ActorSettingsDialog(timerActor, this); } - else if(ui->comboBox->currentText() == "Regulator" && item_->getSensorStore() != nullptr) + else if(ui->comboBox->currentText() == "Regulator") { Regulator* regulator = new Regulator(); actor = regulator; - dialog = new ActorSettingsDialog(regulator, item_->getSensorStore(), this); + dialog = new ActorSettingsDialog(regulator, this); + } + + else if(ui->comboBox->currentText() == "Polynomal") + { + PolynomalActor* polynomalActor = new PolynomalActor(); + actor = polynomalActor; + dialog = new ActorSettingsDialog(polynomalActor, this); + } + + else if(ui->comboBox->currentText() == "Multi Factor") + { + MultiFactorActor* polynomalActor = new MultiFactorActor(); + actor = polynomalActor; + dialog = new ActorSettingsDialog(polynomalActor, this); } @@ -133,13 +143,17 @@ void ItemSettingsDialog::editActor() Regulator* regulator = dynamic_cast(actor); SensorActor* sensorActor = dynamic_cast(actor); TimerActor* timerActor = dynamic_cast(actor); + PolynomalActor* polynomalActor = dynamic_cast(actor); + MultiFactorActor* factorActor = dynamic_cast(actor); ActorSettingsDialog* dialog; if(alarmTime) dialog = new ActorSettingsDialog(alarmTime, this); - else if(regulator) dialog = new ActorSettingsDialog(regulator, nullptr, this); - else if(sensorActor) dialog = new ActorSettingsDialog(sensorActor, nullptr, this); + else if(regulator) dialog = new ActorSettingsDialog(regulator, this); + else if(sensorActor) dialog = new ActorSettingsDialog(sensorActor, this); else if(timerActor) dialog = new ActorSettingsDialog(timerActor, this); + else if(polynomalActor) dialog = new ActorSettingsDialog(polynomalActor, this); + else if(factorActor) dialog = new ActorSettingsDialog(factorActor, this); else dialog = new ActorSettingsDialog(actor, this); dialog->setParent(this); dialog->show(); @@ -149,7 +163,7 @@ void ItemSettingsDialog::editActor() { ui->tableWidget->item(i, 0)->setText(item_->getActors()[i]->getName()); ui->tableWidget->item(i, 1)->setText(item_->getActors()[i]->actionName()); - ui->tableWidget->item(i, 3)->setText(item_->getActors()[i]->isActive() ? "Y" : "N"); + ui->tableWidget->item(i, 2)->setText(item_->getActors()[i]->isActive() ? "Y" : "N"); } } } diff --git a/src/ui/itemsettingsdialog.ui b/src/ui/itemsettingsdialog.ui index abcf55c..940ac20 100644 --- a/src/ui/itemsettingsdialog.ui +++ b/src/ui/itemsettingsdialog.ui @@ -94,13 +94,6 @@ - - - - Actors enabled - - - @@ -143,7 +136,7 @@ 0 - 4 + 3 false @@ -163,7 +156,6 @@ - @@ -214,7 +206,7 @@ - Linear + Polynomal @@ -232,6 +224,11 @@ Timer + + + Multi Factor + +
diff --git a/src/ui/itemwidget.cpp b/src/ui/itemwidget.cpp index 63e6002..a1f47bb 100644 --- a/src/ui/itemwidget.cpp +++ b/src/ui/itemwidget.cpp @@ -5,7 +5,7 @@ #include #include -ItemWidget::ItemWidget(std::weak_ptr item, bool analog, QWidget *parent) : +ItemWidget::ItemWidget(std::weak_ptr item, bool analog, bool nameOnly, QWidget *parent) : QWidget(parent), item_(item), ui(new Ui::ItemWidget) @@ -17,10 +17,12 @@ ItemWidget::ItemWidget(std::weak_ptr item, bool analog, QWidget *parent) : ui->horizontalSpacer->changeSize(0,0); ui->checkBox->hide(); } - else + else if(nameOnly) { + ui->checkBox->hide(); ui->slider->hide(); } + else ui->slider->hide(); if(auto workingRelay = item_.lock()) { @@ -32,11 +34,20 @@ ItemWidget::ItemWidget(std::weak_ptr item, bool analog, QWidget *parent) : else connect(ui->checkBox, &QCheckBox::toggled, this, &ItemWidget::moveToState); connect(ui->pushButton, &QPushButton::clicked, this, &ItemWidget::showSettingsDialog); connect(workingRelay.get(), &Relay::valueChanged, this, &ItemWidget::stateChanged); + connect(ui->pushButton_Remove, &QPushButton::clicked, this, &ItemWidget::deleteItem); } else disable(); } +void ItemWidget::deleteItem() +{ + if(auto workingItem = item_.lock()) + { + deleteRequest(*workingItem); + } +} + void ItemWidget::moveToValue(int value) { if(auto workingItem = item_.lock()) workingItem->setValue(value); diff --git a/src/ui/itemwidget.h b/src/ui/itemwidget.h index 9c6e98f..fadb53e 100644 --- a/src/ui/itemwidget.h +++ b/src/ui/itemwidget.h @@ -18,13 +18,18 @@ private: void disable(); +signals: + + void deleteRequest(const ItemData& item); + private slots: void showSettingsDialog(); void moveToState(bool state); void moveToValue(int value); + void deleteItem(); public: - explicit ItemWidget(std::weak_ptr item, bool analog = false, QWidget *parent = nullptr); + explicit ItemWidget(std::weak_ptr item, bool analog = false, bool nameOnly = false, QWidget *parent = nullptr); std::weak_ptr getItem(); bool controles(const ItemData& relay); ~ItemWidget(); diff --git a/src/ui/itemwidget.ui b/src/ui/itemwidget.ui index 67cd982..bdbc8a6 100644 --- a/src/ui/itemwidget.ui +++ b/src/ui/itemwidget.ui @@ -67,6 +67,25 @@ + + + + + 0 + 0 + + + + + 30 + 16777215 + + + + X + + + diff --git a/src/ui/mainwindow.cpp b/src/ui/mainwindow.cpp index 230da79..7b8fa6f 100644 --- a/src/ui/mainwindow.cpp +++ b/src/ui/mainwindow.cpp @@ -1,42 +1,47 @@ #include "mainwindow.h" #include "ui_mainwindow.h" #include "itemscrollbox.h" - #include "itemsettingsdialog.h" +#include "itemcreationdialog.h" +#include "../mainobject.h" -MainWindow::MainWindow(Microcontroller *micro, PowerItem* powerItem, ItemStore* itemStore, SensorStore *sensorStore, bool master, QWidget *parent) : +MainWindow::MainWindow(MainObject * const mainObject, QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow), colorChooser(this), - _micro(micro), - _powerItem(powerItem) + _micro(&mainObject->micro), + _powerItem(&mainObject->powerItem) { ui->setupUi(this); - if(!master) connect(ui->pushButton_broadcast, &QPushButton::clicked, this, &MainWindow::sigBrodcast); + if(!mainObject->master) connect(ui->pushButton_broadcast, &QPushButton::clicked, this, &MainWindow::sigBrodcast); else ui->pushButton_broadcast->hide(); connect(ui->pushButton_power, SIGNAL(clicked()), this, SLOT(showPowerItemDialog())); //Relays - connect(ui->pushButton_refesh, SIGNAL(clicked()), _micro, SLOT(requestState())); - connect(itemStore, &ItemStore::itemAdded, ui->relayList, &ItemScrollBox::addItem); - connect(itemStore, &ItemStore::itemDeleted, ui->relayList, &ItemScrollBox::removeItem); + if(mainObject->master)connect(ui->pushButton_refesh, &QPushButton::clicked, _micro, &Microcontroller::requestState); + else connect(ui->pushButton_refesh, &QPushButton::clicked, &mainObject->broadCast, &BroadCast::requestJson); + connect(&mainObject->items, &ItemStore::itemAdded, ui->relayList, &ItemScrollBox::addItem); + connect(&mainObject->items, &ItemStore::itemDeleted, ui->relayList, &ItemScrollBox::removeItem); - for(size_t i = 0; i < itemStore->getItems()->size(); ++i) + for(size_t i = 0; i < mainObject->items.getItems()->size(); ++i) { - ui->relayList->addItem(itemStore->getItems()->at(i)); + ui->relayList->addItem(mainObject->items.getItems()->at(i)); } //Sensors - - ui->sensorListView->sensorsChanged(*(sensorStore->getSensors())); - connect(sensorStore, &SensorStore::stateChenged, ui->sensorListView, &SensorListWidget::sensorsChanged); + ui->sensorListView->setShowHidden(false); + ui->sensorListView->sensorsChanged(*globalSensors.getSensors()); + connect(&globalSensors, &SensorStore::stateChenged, ui->sensorListView, &SensorListWidget::sensorsChanged); //RGB Leds connect(&colorChooser, SIGNAL(colorSelected(const QColor)), this, SLOT(slotChangedRgb(const QColor))); connect(ui->button_quit, SIGNAL(clicked()), this, SLOT(close())); connect(ui->button_color, SIGNAL(clicked()), &colorChooser, SLOT(show())); + + connect(ui->pushButton_addItem, &QPushButton::clicked, this, &MainWindow::showItemCreationDialog); + connect(ui->relayList, &ItemScrollBox::deleteRequest, &mainObject->items, &ItemStore::removeItem); } MainWindow::~MainWindow() @@ -56,6 +61,16 @@ void MainWindow::slotChangedRgb(const QColor color) _micro->changeRgbColor(color); } +void MainWindow::showItemCreationDialog() +{ + ItemCreationDialog diag(this); + diag.show(); + if(diag.exec()) + { + createdItem(diag.item); + } +} + void MainWindow::changeHeaderLableText(QString string) { if(string.size() > 28) diff --git a/src/ui/mainwindow.h b/src/ui/mainwindow.h index c20fc87..f51615f 100644 --- a/src/ui/mainwindow.h +++ b/src/ui/mainwindow.h @@ -14,6 +14,8 @@ #include "../broadcast.h" +class MainObject; + namespace Ui { class MainWindow; @@ -24,7 +26,7 @@ class MainWindow : public QMainWindow Q_OBJECT public: - explicit MainWindow(Microcontroller *micro, PowerItem* powerItem, ItemStore* itemStore, SensorStore *sensorStore, bool master, QWidget *parent = nullptr); + explicit MainWindow(MainObject * const mainObject, QWidget *parent = nullptr); ~MainWindow(); private: @@ -39,12 +41,14 @@ private: signals: void sigBrodcast(); + void createdItem(std::shared_ptr item); private slots: //RGB void slotChangedRgb(const QColor color); void showPowerItemDialog(); + void showItemCreationDialog(); public slots: diff --git a/src/ui/mainwindow.ui b/src/ui/mainwindow.ui index 0a3f5c2..de1eca1 100644 --- a/src/ui/mainwindow.ui +++ b/src/ui/mainwindow.ui @@ -44,7 +44,7 @@ - + @@ -81,15 +81,9 @@ QAbstractItemView::NoSelection - + false - - Qt::NoPen - - - false - @@ -190,7 +184,14 @@ - Broadcast + Save + + + + + + + Add Item @@ -219,17 +220,17 @@ + + SensorListWidget + QListView +
../src/ui/sensorlistwidget.h
+
ItemScrollBox QWidget
../src/ui/itemscrollbox.h
1
- - SensorListWidget - QTableWidget -
../src/ui/sensorlistwidget.h
-
diff --git a/src/ui/sensorlistwidget.cpp b/src/ui/sensorlistwidget.cpp index dd4a885..b23f4bd 100644 --- a/src/ui/sensorlistwidget.cpp +++ b/src/ui/sensorlistwidget.cpp @@ -3,7 +3,7 @@ #include #include -SensorListWidget::SensorListWidget(QWidget *parent): QTableWidget(parent) +SensorListWidget::SensorListWidget(const bool showHidden, QWidget *parent): QTableWidget(parent), showHidden_(showHidden) { setColumnCount(2); setSelectionBehavior(QAbstractItemView::SelectRows); @@ -13,7 +13,7 @@ SensorListWidget::SensorListWidget(QWidget *parent): QTableWidget(parent) setHorizontalHeaderItem(1, new QTableWidgetItem("Value")); } -SensorListWidget::SensorListWidget(SensorStore& sensorStore, QWidget* parent): QTableWidget (parent) +SensorListWidget::SensorListWidget(SensorStore& sensorStore, const bool showHidden, QWidget* parent): QTableWidget (parent), showHidden_(showHidden) { sensorsChanged(*(sensorStore.getSensors())); } @@ -23,25 +23,47 @@ void SensorListWidget::sensorsChanged(std::vector sensors) clear(); setHorizontalHeaderItem(0, new QTableWidgetItem("Sensor")); setHorizontalHeaderItem(1, new QTableWidgetItem("Value")); - setRowCount(sensors.size()); + size_t nonHiddenCount = sensors.size(); + if(!showHidden_) + { + nonHiddenCount = 0; + for(size_t i = 0; i < sensors.size(); ++i) + { + if(!sensors[i].hidden) nonHiddenCount++; + } + } + size_t listLen = 0; + for(size_t i = 0; i < sensors.size(); ++i) if(showHidden_ || !sensors[i].hidden) ++listLen; + setRowCount(static_cast(listLen)); + size_t row = 0; for(size_t i = 0; i < sensors.size(); ++i) { - QString itemString; - itemString.append(QString::number(sensors[i].field)); - itemString.append(' '); + if(showHidden_ || !sensors[i].hidden) + { + QString itemString; + itemString.append(QString::number(sensors[i].field)); + itemString.append(' '); - if(sensors[i].type == Sensor::TYPE_DOOR) - { - if(sensors[i].field) itemString.append("\"Open\""); - else itemString.append("\"Closed\""); + if(sensors[i].type == Sensor::TYPE_DOOR) + { + if(static_cast(sensors[i].field)) itemString.append("\"Open\""); + else itemString.append("\"Closed\""); + } + else if(sensors[i].type == Sensor::TYPE_AUDIO_OUTPUT) + { + if(static_cast(sensors[i].field)) itemString.append("\"Playing\""); + else itemString.append("\"Silent\""); + } + + setItem(static_cast(row), 0, new QTableWidgetItem(sensors[i].name + (sensors[i].hidden ? "(H)" : ""))); + setItem(static_cast(row), 1, new QTableWidgetItem(itemString)); + ++row; } - else if(sensors[i].type == Sensor::TYPE_AUDIO_OUTPUT) - { - if(sensors[i].field) itemString.append("\"Playing\""); - else itemString.append("\"Silent\""); - } - setItem(i, 0, new QTableWidgetItem(sensors[i].name)); - setItem(i, 1, new QTableWidgetItem(itemString)); } } +void SensorListWidget::setShowHidden(const bool showHidden) +{ + showHidden_=showHidden; +} + diff --git a/src/ui/sensorlistwidget.h b/src/ui/sensorlistwidget.h index a5dce7d..9ace2b4 100644 --- a/src/ui/sensorlistwidget.h +++ b/src/ui/sensorlistwidget.h @@ -6,11 +6,15 @@ class SensorListWidget : public QTableWidget { Q_OBJECT + + bool showHidden_; + public: - SensorListWidget(QWidget *parent = nullptr); - SensorListWidget(SensorStore& sensorStore, QWidget* parent = nullptr); + SensorListWidget(const bool showHidden = true, QWidget *parent = nullptr); + SensorListWidget(SensorStore& sensorStore, const bool showHidden = true, QWidget* parent = nullptr); virtual ~SensorListWidget(){} + void setShowHidden(const bool showHidden); public slots: