From 913d7df56dd61a62291277f898fc65a32e731161 Mon Sep 17 00:00:00 2001 From: Carl Philipp Klemm Date: Sun, 22 Mar 2026 23:23:18 +0100 Subject: [PATCH] Finish lerge refactor of systems --- SHinterface.pro | 10 ++- src/broadcast.cpp | 139 ----------------------------- src/broadcast.h | 55 ------------ src/iomuliplexer.cpp | 76 ---------------- src/iomuliplexer.h | 54 ------------ src/items/auxitem.cpp | 3 +- src/items/auxitem.h | 2 +- src/items/fixeditemsource.cpp | 14 +++ src/items/fixeditemsource.h | 22 +++++ src/items/item.cpp | 88 +++++++++++++++++-- src/items/item.h | 23 ++++- src/items/itemloadersource.cpp | 38 ++++++++ src/items/itemloadersource.h | 21 +++++ src/items/itemsource.h | 1 + src/items/itemstore.cpp | 61 ++++++------- src/items/itemstore.h | 2 +- src/items/messageitem.cpp | 1 + src/items/poweritem.cpp | 6 +- src/items/poweritem.h | 2 +- src/items/relay.cpp | 2 +- src/items/rgbitem.cpp | 1 + src/items/rgbitem.h | 2 +- src/main.cpp | 66 +++----------- src/mainobject.cpp | 105 +++++++++++++++++----- src/mainobject.h | 27 +++--- src/microcontroller.cpp | 18 ++-- src/sensors/sensor.h | 2 +- src/tcpserver.cpp | 87 +++++++++++++++--- src/tcpserver.h | 24 +++-- src/ui/itemscrollbox.cpp | 15 +--- src/ui/itemwidget.cpp | 67 ++++++++------ src/ui/itemwidget.h | 2 +- src/ui/itemwidget.ui | 6 +- src/ui/mainwindow.cpp | 44 ++++++---- src/ui/mainwindow.h | 6 +- src/ui/mainwindow.ui | 156 +++++++++++++++++---------------- 36 files changed, 614 insertions(+), 634 deletions(-) delete mode 100644 src/broadcast.cpp delete mode 100644 src/broadcast.h delete mode 100644 src/iomuliplexer.cpp delete mode 100644 src/iomuliplexer.h create mode 100644 src/items/fixeditemsource.cpp create mode 100644 src/items/fixeditemsource.h create mode 100644 src/items/itemloadersource.cpp create mode 100644 src/items/itemloadersource.h diff --git a/SHinterface.pro b/SHinterface.pro index e2954c7..f4dad1d 100644 --- a/SHinterface.pro +++ b/SHinterface.pro @@ -26,8 +26,8 @@ QMAKE_CXXFLAGS += -std=c++17 -O2 SOURCES += \ src/actors/factoractor.cpp \ src/actors/polynomalactor.cpp \ + src/items/fixeditemsource.cpp \ src/tcpserver.cpp \ - src/iomuliplexer.cpp \ src/items/messageitem.cpp \ src/items/systemitem.cpp \ src/ui/actorwidgets/factoractorwidget.cpp \ @@ -69,7 +69,8 @@ SOURCES += \ src/items/itemstore.cpp \ src/items/auxitem.cpp \ src/items/rgbitem.cpp \ - src/items/itemsource.cpp + src/items/itemsource.cpp\ + src/items/itemloadersource.cpp SOURCES += \ src/main.cpp \ @@ -81,10 +82,10 @@ SOURCES += \ HEADERS += \ src/actors/factoractor.h \ src/actors/polynomalactor.h \ + src/items/fixeditemsource.h \ src/items/itemsource.h \ src/programmode.h \ src/tcpserver.h \ - src/iomuliplexer.h \ src/items/messageitem.h \ src/items/systemitem.h \ src/ui/actorwidgets/factoractorwidget.h \ @@ -126,7 +127,8 @@ HEADERS += \ src/items/itemstore.h \ src/items/auxitem.h \ src/items/rgbitem.h \ - src/items/itemsource.h + src/items/itemsource.h \ + src/items/itemloadersource.h HEADERS += \ src/microcontroller.h \ diff --git a/src/broadcast.cpp b/src/broadcast.cpp deleted file mode 100644 index ed42f16..0000000 --- a/src/broadcast.cpp +++ /dev/null @@ -1,139 +0,0 @@ -#include "broadcast.h" -#include -#include -#include -#include - -BroadCast::BroadCast(QIODevice* const iodevice, bool master ): master_(master), iodevice_(iodevice) -{ - if(iodevice_ != nullptr) connect(iodevice_, &QIODevice::readyRead, this, &BroadCast::readyRead); -} - -void BroadCast::write(const char * const buffer, const size_t length) -{ - QByteArray mBuffer("bcst: "); - for (size_t i = 0; i < length; ++i) - { - if(buffer[i] != '\n' && buffer[i] != '\0') mBuffer.push_back(buffer[i]); - else - { - mBuffer.push_back('\\'); - if(buffer[i] == '\n')mBuffer.push_back('n'); - else mBuffer.push_back('0'); - } - } - mBuffer.push_back('\n'); - if(iodevice_)iodevice_->write(mBuffer); -} - -void BroadCast::write(const QByteArray& buffer) -{ - write(buffer.data(), buffer.size()); -} - -void BroadCast::sendJson(const QJsonObject& json) -{ - QJsonDocument jsonDocument(json); - QByteArray buffer("JSON: "); - buffer.append(jsonDocument.toJson()); - write(buffer); -} - -void BroadCast::sendSensors() -{ - if(iodevice_) - { - for(auto& sensor: *globalSensors.getSensors()) - iodevice_->write("bcst: "+sensor.toString().toLatin1()+'\n'); - } -} - -void BroadCast::requestSensors() -{ - if(iodevice_) - iodevice_->write("bcst: GETSENSORS\n"); -} - -void BroadCast::requestJson() -{ - if(iodevice_) - iodevice_->write("bcst: GETJSN\n"); -} - -void BroadCast::decodeMaster(const QByteArray& buffer) -{ - if(buffer.startsWith("GETJSN")) - { - qDebug()<<"json requested"; - jsonRequested(); - } - else if(buffer.startsWith("GETSENSORS") ) - { - qDebug()<<"sensors requested"; - sendSensors(); - } -} - -void BroadCast::decode(QByteArray buffer) -{ - qDebug()<<"decodeing: "<= 6 && buffer[0] == 'J' && buffer[1] == 'S' && buffer[2] == 'O' && buffer[3] == 'N' - && buffer[4] == ':') - { - qDebug()<<"got json"; - buffer.remove(0,6); - for(int i = 0; i < buffer.size()-1; ++i) - { - if( buffer[i] == '\\' && buffer[i+1] == 'n' ) - { - buffer[i] = '\n'; - buffer.remove(i+1,1); - } - else if( buffer[i] == '\\' && buffer[i+1] == '0' ) - { - buffer[i] = '\0'; - buffer.remove(i+1,1); - } - } - - QJsonParseError error; - QJsonDocument document = QJsonDocument::fromJson(buffer, &error); - - qDebug()<<"JSON:"; - 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::sendMessage(const QString &title, const QString &body) -{ - write(QByteArray("MESG ") + title.toLatin1() + " BODY " + body.toLatin1()); -} - -void BroadCast::readyRead() -{ - buffer_.append(iodevice_->readAll()); - int newlineIndex = buffer_.indexOf('\n'); - while( newlineIndex != -1 ) - { - if(buffer_.startsWith("bcst: ")) - { - QByteArray tmp = buffer_.mid(6,newlineIndex-6); - decode(tmp); - if(master_)decodeMaster(tmp); - } - buffer_.remove(0, newlineIndex+1); - newlineIndex = buffer_.indexOf('\n'); - } -} diff --git a/src/broadcast.h b/src/broadcast.h deleted file mode 100644 index ae7e802..0000000 --- a/src/broadcast.h +++ /dev/null @@ -1,55 +0,0 @@ -#ifndef BROADCAST_H -#define BROADCAST_H -#include -#include -#include -#include -#include "sensors/sensor.h" - -class BroadCast: public QObject -{ - Q_OBJECT - -private: - - bool master_; - - static constexpr uint8_t MODE_PREPACKET = 0; - static constexpr uint8_t MODE_PACKET = 1; - - QByteArray buffer_; - - QIODevice* const iodevice_; - - void write(const char * const buffer, const size_t length); - void write(const QByteArray& buffer); - - void decode(QByteArray buffer); - void decodeMaster(const QByteArray& buffer); - -private slots: - - void readyRead(); - -public slots: - - void requestJson(); - void requestSensors(); - -signals: - - void jsonRequested(); - void gotJson(QJsonObject json); - void gotSensorState(Sensor sensor); - -public: - - BroadCast(QIODevice* const iodevice = nullptr, bool master = true); - void sendJson(const QJsonObject& json); - void sendSensors(); - void sendMessage(const QString& title, const QString& body); - -}; - -#endif // BROADCAST_H - diff --git a/src/iomuliplexer.cpp b/src/iomuliplexer.cpp deleted file mode 100644 index 84b3b1a..0000000 --- a/src/iomuliplexer.cpp +++ /dev/null @@ -1,76 +0,0 @@ -#include "iomuliplexer.h" -#include - -VirutalIODevice::VirutalIODevice(QObject* parent): QBuffer(parent) -{ - -} - -qint64 VirutalIODevice::writeData(const char *data, qint64 len) -{ - blockSignals(true); - qint64 ret = QBuffer::writeData(data, len); - blockSignals(false); - masterReadyRead(); - return ret; -} - -qint64 VirutalIODevice::masterWrite(const QByteArray &byteArray) -{ - return masterWrite(byteArray.data(), byteArray.length()); -} - -qint64 VirutalIODevice::masterWrite(const char *data, qint64 maxSize) -{ - blockSignals(true); - qint64 ret = QBuffer::writeData(data, maxSize); - blockSignals(false); - readyRead(); - return ret; -} - -IoMuliplexer::IoMuliplexer(QIODevice* mainDevice, QObject* Parent): IoMuliplexer(Parent) -{ - setIoDevice(mainDevice); -} - -IoMuliplexer::IoMuliplexer(QObject* Parent): QObject(Parent) -{ - -} - -IoMuliplexer::~IoMuliplexer() -{ - for(size_t i = 0; i < ioDevices_.size(); ++i) delete ioDevices_[i]; - ioDevices_.clear(); -} - -void IoMuliplexer::setIoDevice(QIODevice* mainDevice) -{ - mainDevice_ = mainDevice; - connect(mainDevice_, &QIODevice::readyRead, this, &IoMuliplexer::mainIsReadyRead); -} - -QIODevice* IoMuliplexer::getIoDevice() -{ - ioDevices_.push_back(new VirutalIODevice); - ioDevices_.back()->open(QIODevice::ReadWrite); - connect(ioDevices_.back(), &VirutalIODevice::masterReadyRead, this, &IoMuliplexer::clientIsReadyRead); - return ioDevices_.back(); -} - -void IoMuliplexer::clientIsReadyRead() -{ - VirutalIODevice* device = dynamic_cast(sender()); - if(device) - { - QByteArray array = device->readAll(); - mainDevice_->write(array); - } -} - -void IoMuliplexer::mainIsReadyRead() -{ - QByteArray array = mainDevice_->readAll(); - for(size_t i = 0; i < ioDevices_.size(); ++i) ioDevices_[i]->masterWrite(array); -} diff --git a/src/iomuliplexer.h b/src/iomuliplexer.h deleted file mode 100644 index 68ad624..0000000 --- a/src/iomuliplexer.h +++ /dev/null @@ -1,54 +0,0 @@ -#ifndef IOMULIPLEXER_H -#define IOMULIPLEXER_H - -#include -#include -#include -#include - -class VirutalIODevice: public QBuffer -{ - Q_OBJECT -public: - - VirutalIODevice(QObject* parent = nullptr); - virtual ~VirutalIODevice() override {} - - virtual qint64 writeData(const char *data, qint64 len) override; - - qint64 masterWrite(const QByteArray &byteArray); - qint64 masterWrite(const char *data, qint64 maxSize); - -signals: - - void masterReadyRead(); - -}; - - -class IoMuliplexer: public QObject -{ - Q_OBJECT -private: - - QIODevice* mainDevice_; - std::vector< VirutalIODevice* > ioDevices_; - -public: - explicit IoMuliplexer(QIODevice* mainDevice, QObject* Parent = nullptr); - explicit IoMuliplexer(QObject* Parent = nullptr); - - ~IoMuliplexer(); - - void setIoDevice(QIODevice* mainDevice); - - QIODevice* getIoDevice(); - -private slots: - - void mainIsReadyRead(); - void clientIsReadyRead(); - -}; - -#endif // IOMULIPLEXER_H diff --git a/src/items/auxitem.cpp b/src/items/auxitem.cpp index 42f3efd..5bee87e 100644 --- a/src/items/auxitem.cpp +++ b/src/items/auxitem.cpp @@ -3,11 +3,12 @@ AuxItem::AuxItem(Microcontroller* micro, uint32_t itemIdIn, QString name, uint8_t value, QObject* parent): Item(itemIdIn, name, value, parent), micro_(micro) { - + type_ = ITEM_VALUE_UINT; } void AuxItem::enactValue(uint8_t value) { + assert(micro_); micro_->setAuxPwm(value); } diff --git a/src/items/auxitem.h b/src/items/auxitem.h index 0483c39..658b907 100644 --- a/src/items/auxitem.h +++ b/src/items/auxitem.h @@ -13,7 +13,7 @@ protected: virtual void enactValue(uint8_t value) override; public: - AuxItem(Microcontroller* micro, uint32_t itemIdIn = QRandomGenerator::global()->generate(), QString name = "", + AuxItem(Microcontroller* micro = nullptr, uint32_t itemIdIn = QRandomGenerator::global()->generate(), QString name = "", uint8_t value = 0, QObject* parent = nullptr); virtual void store(QJsonObject& json) override; diff --git a/src/items/fixeditemsource.cpp b/src/items/fixeditemsource.cpp new file mode 100644 index 0000000..d1abc21 --- /dev/null +++ b/src/items/fixeditemsource.cpp @@ -0,0 +1,14 @@ +#include "fixeditemsource.h" + +FixedItemSource::FixedItemSource(Microcontroller* micro, QObject *parent): + ItemSource{parent}, + powerItem(new PowerItem(5487423)), + rgbItem(new RgbItem(micro, 5487422, "Rgb Lights")), + auxItem(new AuxItem(micro, 5487421, "Desk Light")) +{ +} + +void FixedItemSource::refresh() +{ + gotItems({powerItem, rgbItem, auxItem}); +} diff --git a/src/items/fixeditemsource.h b/src/items/fixeditemsource.h new file mode 100644 index 0000000..150b9a1 --- /dev/null +++ b/src/items/fixeditemsource.h @@ -0,0 +1,22 @@ +#ifndef FIXEDITEMSOURCE_H +#define FIXEDITEMSOURCE_H + +#include "itemsource.h" +#include "poweritem.h" +#include "rgbitem.h" +#include "auxitem.h" +#include "src/microcontroller.h" + +class FixedItemSource : public ItemSource +{ + Q_OBJECT + std::shared_ptr powerItem; + std::shared_ptr rgbItem; + std::shared_ptr auxItem; + +public: + explicit FixedItemSource(Microcontroller* micro, QObject *parent = nullptr); + virtual void refresh() override; +}; + +#endif // FIXEDITEMSOURCE_H diff --git a/src/items/item.cpp b/src/items/item.cpp index 2596b4f..9b80254 100644 --- a/src/items/item.cpp +++ b/src/items/item.cpp @@ -7,10 +7,14 @@ #include "relay.h" #include "messageitem.h" #include "systemitem.h" +#include "auxitem.h" +#include "poweritem.h" +#include "rgbitem.h" #include -ItemData::ItemData(uint32_t itemIdIn, QString name, uint8_t value): name_(name), value_(value), itemId_(itemIdIn) +ItemData::ItemData(uint32_t itemIdIn, QString name, uint8_t value, bool loaded, bool hidden, item_value_type_t type): + name_(name), value_(value), itemId_(itemIdIn), loaded_(loaded), hidden_(hidden), type_(type) { } @@ -48,9 +52,47 @@ void ItemData::load(const QJsonObject &json, const bool preserve) { name_ = json["Name"].toString(name_); itemId_ = static_cast(json["ItemId"].toDouble(0)); + value_ = json["Value"].toInt(); } } +bool ItemData::getLoaded() const +{ + return loaded_; +} + +void ItemData::setLoaded(bool loaded) +{ + loaded_ = loaded; +} + +bool ItemData::hasChanged(const ItemData& other) +{ + if(other != *this) + return false; + if(other.getName() != getName()) + return true; + if(other.getValue() != getValue()) + return true; + if(other.getLoaded() != getLoaded()) + return true; + return false; +} + +bool ItemData::isHidden() +{ + return hidden_; +} + +void ItemData::setHidden(bool hidden) +{ + hidden_ = hidden; +} + +item_value_type_t ItemData::getValueType() +{ + return type_; +} //item @@ -96,7 +138,8 @@ void Item::load(const QJsonObject &json, const bool preserve) if(actorsArray[i].isObject()) { std::shared_ptr actor = Actor::loadActor(actorsArray[i].toObject()); - if(actor != nullptr) addActor(actor); + if(actor != nullptr) + addActor(actor); } } } @@ -109,6 +152,7 @@ void Item::actorSetValue(uint8_t value) void Item::setValue(uint8_t value) { + qDebug()<<__func__; informValue(value); if(programMode == PROGRAM_MODE_PRIMARY || programMode == PROGRAM_MODE_HEADLESS_PRIMARY) enactValue(value); @@ -116,9 +160,12 @@ void Item::setValue(uint8_t value) void Item::informValue(uint8_t value) { - value_ = value; - valueChanged(value_); - updated(*this); + if(value_ != value) + { + value_ = value; + valueChanged(value_); + updated(*this); + } } void Item::enactValue(uint8_t value) @@ -135,14 +182,16 @@ void Item::addActor(std::shared_ptr actor) connect(this, &Item::valueChanged, actor.get(), &Actor::onValueChanged); std::shared_ptr sensorActor = std::dynamic_pointer_cast(actor); - if(sensorActor)connect(&globalSensors, &SensorStore::sensorChangedState, sensorActor.get(), &SensorActor::sensorEvent); + if(sensorActor) + connect(&globalSensors, &SensorStore::sensorChangedState, sensorActor.get(), &SensorActor::sensorEvent); std::shared_ptr regulator = std::dynamic_pointer_cast(actor); - if(regulator)connect(&globalSensors, &SensorStore::sensorChangedState, regulator.get(), &Regulator::sensorEvent); + if(regulator) + connect(&globalSensors, &SensorStore::sensorChangedState, regulator.get(), &Regulator::sensorEvent); std::shared_ptr polynomalActor = std::dynamic_pointer_cast(actor); - if(polynomalActor != nullptr )connect(&globalSensors, &SensorStore::sensorChangedState, polynomalActor.get(), - &PolynomalActor::sensorEvent); + if(polynomalActor != nullptr ) + connect(&globalSensors, &SensorStore::sensorChangedState, polynomalActor.get(), &PolynomalActor::sensorEvent); } bool Item::removeActor(std::shared_ptr actor) @@ -189,6 +238,14 @@ void Item::setActorsActive(bool in) in ? actors_[i]->makeActive() : actors_[i]->makeInactive(); } +void Item::mergeLoaded(Item& item) +{ + name_ = item.name_; + actors_.clear(); + for(std::shared_ptr actor : item.actors_) + addActor(actor); +} + std::shared_ptr Item::loadItem(const QJsonObject& json) { std::shared_ptr newItem = nullptr; @@ -206,8 +263,21 @@ std::shared_ptr Item::loadItem(const QJsonObject& json) } else if(json["Type"].toString("") == "Aux") { + newItem = std::shared_ptr(new AuxItem); + } + else if(json["Type"].toString("") == "Power") + { + newItem = std::shared_ptr(new PowerItem); + } + else if(json["Type"].toString("") == "Rgb") + { + newItem = std::shared_ptr(new RgbItem); } if(newItem) + { newItem->load(json); + newItem->setLoaded(true); + } return newItem; } + diff --git a/src/items/item.h b/src/items/item.h index bfa43f6..1aaa090 100644 --- a/src/items/item.h +++ b/src/items/item.h @@ -8,15 +8,29 @@ class Actor; +typedef enum { + ITEM_VALUE_BOOL = 0, + ITEM_VALUE_UINT, + ITEM_VALUE_NO_VALUE +} item_value_type_t; + class ItemData { protected: QString name_; uint8_t value_; uint32_t itemId_; + bool loaded_; + bool hidden_; + item_value_type_t type_; public: - ItemData(uint32_t itemIdIn = QRandomGenerator::global()->generate(), QString name = "Item", uint8_t value = 0); + ItemData(uint32_t itemIdIn = QRandomGenerator::global()->generate(), + QString name = "Item", + uint8_t value = 0, + bool loaded = false, + bool hidden = false, + item_value_type_t type = ITEM_VALUE_BOOL); inline bool operator==(const ItemData& in) const { @@ -29,8 +43,14 @@ public: uint32_t id() const; + bool hasChanged(const ItemData& other); void setName(QString name); uint8_t getValue() const; + bool getLoaded() const; + void setLoaded(bool loaded); + bool isHidden(); + void setHidden(bool hidden); + item_value_type_t getValueType(); virtual QString getName() const; virtual void store(QJsonObject& json); virtual void load(const QJsonObject& json, const bool preserve = false); @@ -72,6 +92,7 @@ public: void setOverride(const bool in); bool getOverride(); void informValue(uint8_t value); + void mergeLoaded(Item& item); virtual void store(QJsonObject& json); virtual void load(const QJsonObject& json, const bool preserve = false); diff --git a/src/items/itemloadersource.cpp b/src/items/itemloadersource.cpp new file mode 100644 index 0000000..d5e6afa --- /dev/null +++ b/src/items/itemloadersource.cpp @@ -0,0 +1,38 @@ +#include "itemloadersource.h" + +#include + +ItemLoaderSource::ItemLoaderSource(const QJsonObject& json, QObject *parent): + ItemSource{parent}, + json(json) +{ +} + +void ItemLoaderSource::refresh() +{ + std::vector> items; + const QJsonArray itemsArray(json["Items"].toArray()); + for(int i = 0; i < itemsArray.size(); ++i) + { + if(!itemsArray[i].isObject()) + continue; + + const QJsonObject itemObject = itemsArray[i].toObject(); + std::shared_ptr newItem = Item::loadItem(itemObject); + if(newItem) + { + items.push_back(newItem); + qDebug()<<"Loaded item"<getName(); + } + } + gotItems(items); +} + +void ItemLoaderSource::updateJson(const QJsonObject& json) +{ + this->json = json; +} + +ItemLoaderSource::~ItemLoaderSource() +{} + diff --git a/src/items/itemloadersource.h b/src/items/itemloadersource.h new file mode 100644 index 0000000..c31c7fd --- /dev/null +++ b/src/items/itemloadersource.h @@ -0,0 +1,21 @@ +#ifndef ITEMLOADERSOURCE_H +#define ITEMLOADERSOURCE_H + +#include + +#include "itemsource.h" + +class ItemLoaderSource : public ItemSource +{ + Q_OBJECT + + QJsonObject json; + +public: + explicit ItemLoaderSource(const QJsonObject& json = QJsonObject(), QObject *parent = nullptr); + ~ItemLoaderSource(); + void updateJson(const QJsonObject& json); + virtual void refresh() override; +}; + +#endif // ITEMLOADERSOURCE_H diff --git a/src/items/itemsource.h b/src/items/itemsource.h index 8e26c18..9fee9c1 100644 --- a/src/items/itemsource.h +++ b/src/items/itemsource.h @@ -18,6 +18,7 @@ public slots: signals: void gotItems(std::vector> items, bool inform = true); + void requestReplaceItems(std::vector> items); void updateItems(std::vector items, bool inform = true); }; diff --git a/src/items/itemstore.cpp b/src/items/itemstore.cpp index e681a97..1242afc 100644 --- a/src/items/itemstore.cpp +++ b/src/items/itemstore.cpp @@ -21,15 +21,15 @@ void ItemStore::addItem(std::shared_ptr item, bool inform) { items_.push_back(std::shared_ptr(item)); connect(item.get(), &Item::updated, this, &ItemStore::itemUpdateSlot); - qDebug()<<"Item"<getName()<<"added"; + qDebug()<<"Item"<getName()<<"added"<<(item->getLoaded() ? "from loaded" : ""); itemAdded(std::weak_ptr(items_.back())); } - else if(item->getValue() != matched->getValue()) + else { - if(inform) - matched->informValue(item->getValue()); - else - matched->setValue(item->getValue()); + if(item->getLoaded()) + matched->mergeLoaded(*item); + else if(item->getValue() != matched->getValue()) + updateItem(*item, inform); } } @@ -53,6 +53,19 @@ void ItemStore::removeItem(const ItemData& item) } } +void ItemStore::replaceItems(const std::vector>& items) +{ + addItems(items, true); + std::vector deletedItems; + for(std::shared_ptr item : items_) + { + if(std::find_if(items.begin(), items.end(), [item](const std::shared_ptr other){return *item == *other;}) == items.end()) + deletedItems.push_back(*item); + } + for(const ItemData& item : deletedItems) + removeItem(item); +} + void ItemStore::clear() { for(size_t i = 0; i < items_.size(); ++i) itemDeleted(*items_[i]); @@ -66,22 +79,25 @@ void ItemStore::updateItems(std::vector items, bool inform) updateItem(item, inform); } - void ItemStore::updateItem(const ItemData& item, bool inform) { for(unsigned i = 0; i < items_.size(); i++ ) { if(items_[i]->operator==(item)) { - if(items_[i]->getValue() != item.getValue()) + if(items_[i]->hasChanged(item)) { - if(inform) - items_[i]->informValue(item.getValue()); - else - items_[i]->setValue(item.getValue()); + if(items_[i]->getValue() != item.getValue()) + { + items_[i]->setLoaded(false); + if(inform) + items_[i]->informValue(item.getValue()); + else + items_[i]->setValue(item.getValue()); + } + qDebug()<<"Item"<getName()<<"updated"; + itemUpdated(items_[i]); } - qDebug()<<"Item"<getName()<<"updated"; - itemUpdated(items_[i]); } } } @@ -98,24 +114,8 @@ void ItemStore::store(QJsonObject& json) json["Items"] = itemsArray; } -void ItemStore::load(const QJsonObject& json) -{ - const QJsonArray itemsArray(json["Items"].toArray(QJsonArray())); - for(int i = 0; i < itemsArray.size(); ++i) - { - if(!itemsArray[i].isObject()) - continue; - - const QJsonObject itemObject = itemsArray[i].toObject(); - std::shared_ptr newItem = Item::loadItem(itemObject); - if(newItem) - addItem(newItem); - } -} - void ItemStore::itemUpdateSlot(ItemData data) { - qDebug()<<__func__; for(std::shared_ptr& item: items_) { if(*item == data) @@ -138,6 +138,7 @@ void ItemStore::registerItemSource(ItemSource* source) qDebug()<<__func__< item, bool inform = true); void addItems(const std::vector>& itemsIn, bool inform = true); + void replaceItems(const std::vector>& items); void updateItems(std::vector items, bool inform = true); void updateItem(const ItemData& item, bool inform = true); void refresh(); diff --git a/src/items/messageitem.cpp b/src/items/messageitem.cpp index 32f9d5e..151b934 100644 --- a/src/items/messageitem.cpp +++ b/src/items/messageitem.cpp @@ -7,6 +7,7 @@ MessageItem::MessageItem(uint32_t itemIdIn, QString name, uint8_t value, QObjec Item(itemIdIn, name, value, parent) { alertSound.setVolume(1.0); + type_ = ITEM_VALUE_NO_VALUE; } MessageItem::MessageItem(const ItemData& itemData, QObject *parent): diff --git a/src/items/poweritem.cpp b/src/items/poweritem.cpp index cea1470..2b2e4e1 100644 --- a/src/items/poweritem.cpp +++ b/src/items/poweritem.cpp @@ -3,11 +3,13 @@ #include #include -PowerItem::PowerItem(uint32_t itemIdIn, QString name, uint8_t value, QObject* parent): Item(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, "Shutdown Imminent", true)); PowerItem::setValue(true); + hidden_ = true; + type_ = ITEM_VALUE_NO_VALUE; } void PowerItem::enactValue(uint8_t value) diff --git a/src/items/poweritem.h b/src/items/poweritem.h index 76dea5e..dda2f35 100644 --- a/src/items/poweritem.h +++ b/src/items/poweritem.h @@ -22,7 +22,7 @@ protected: virtual void enactValue(uint8_t value) override; public: - PowerItem(uint32_t itemIdIn = QRandomGenerator::global()->generate(), QString name = "", uint8_t value = 0, + PowerItem(uint32_t itemIdIn = QRandomGenerator::global()->generate(), QString name = "Power", uint8_t value = 0, QObject* parent = nullptr); void emmitSensor() { diff --git a/src/items/relay.cpp b/src/items/relay.cpp index e0a3d30..f801e1b 100644 --- a/src/items/relay.cpp +++ b/src/items/relay.cpp @@ -9,11 +9,11 @@ Relay::Relay(uint8_t id, QString name, uint16_t address, bool state, QObject* pa id_(id), address_(address) { itemId_ = address | ((uint32_t)id << 16); - qDebug()<<"Relay "<rgbOn() : micro_->rgbOff(); } diff --git a/src/items/rgbitem.h b/src/items/rgbitem.h index 6e2a923..8e6ae8c 100644 --- a/src/items/rgbitem.h +++ b/src/items/rgbitem.h @@ -13,7 +13,7 @@ protected: virtual void enactValue(uint8_t value) override; public: - RgbItem(Microcontroller* micro, uint32_t itemIdIn = QRandomGenerator::global()->generate(), QString name = "", + RgbItem(Microcontroller* micro = nullptr, uint32_t itemIdIn = QRandomGenerator::global()->generate(), QString name = "", uint8_t value = 0, QObject* parent = nullptr); virtual void store(QJsonObject& json) override; diff --git a/src/main.cpp b/src/main.cpp index f94ff78..d201f11 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -12,54 +12,6 @@ #include "mainobject.h" #include "programmode.h" - -QJsonObject getJsonObjectFromDisk(const QString& filePath, bool* error = nullptr) -{ - QFile file; - file.setFileName(filePath); - - bool ret = file.open(QIODevice::ReadOnly); - if(!file.isOpen() || !ret) - { - std::cerr<<"Can not open config file: "<waitForConnected(1000)) { qCritical()<<"Can not connect to tcp micro"; - storeJsonObjectToDisk(json, parser.value(settingsPathOption)); + MainObject::storeJsonObjectToDisk(settingsPath, json); if(programMode == PROGRAM_MODE_PRIMARY) QMessageBox::critical(nullptr, "Error", "Can not connect to tcp micro"); return 1; @@ -145,21 +98,23 @@ int main(int argc, char *argv[]) if(!microPort->isOpen()) { qCritical()<<"Can not open serial port"< item){globalItems.addItem(item, false);}); w->show(); } @@ -167,14 +122,13 @@ int main(int argc, char *argv[]) delete w; delete microDevice; - mainObject.store(json); - storeJsonObjectToDisk(json, parser.value(settingsPathOption)); } else { SecondaryMainObject mainObject(parser.value(hostOption), parser.value(portOption).toInt()); MainWindow w(&mainObject); - //QObject::connect(&w, &MainWindow::sigSave, &mainObject, &MainObject::sendJson); + QObject::connect(&w, &MainWindow::createdItem, &globalItems, [](std::shared_ptr item){globalItems.addItem(item, false);}); + QObject::connect(&w, &MainWindow::sigSave, mainObject.tcpClient, &TcpClient::sendItems); w.show(); retVal = a.exec(); diff --git a/src/mainobject.cpp b/src/mainobject.cpp index d92e0ae..4f31e78 100644 --- a/src/mainobject.cpp +++ b/src/mainobject.cpp @@ -20,17 +20,61 @@ void MainObject::refresh() globalItems.refresh(); } -PrimaryMainObject::PrimaryMainObject(QIODevice* microDevice, QJsonObject* settings, QString host, int port, QObject *parent) : +QJsonObject MainObject::getJsonObjectFromDisk(const QString& filename, bool* error) +{ + QFile file; + file.setFileName(filename); + + bool ret = file.open(QIODevice::ReadOnly); + if(!file.isOpen() || !ret) + { + std::cerr<<"Can not open config file: "<launch(QHostAddress(host), port); + connect(&globalItems, &ItemStore::itemUpdated, tcpServer, &TcpServer::itemUpdated); } PrimaryMainObject::~PrimaryMainObject() { - store(*settings); + storeToDisk(settingsPath); } void PrimaryMainObject::store(QJsonObject &json) @@ -58,22 +109,28 @@ void PrimaryMainObject::store(QJsonObject &json) void PrimaryMainObject::load(const QJsonObject& json) { + settings = json; + itemLoader.updateJson(json); globalItems.clear(); - rgbItem->removeAllActors(); - auxItem->removeAllActors(); - powerItem->removeAllActors(); - globalItems.addItem(rgbItem); - globalItems.addItem(auxItem); - globalItems.addItem(powerItem); - globalItems.load(json); - if(json["Items"].toArray().size() >= 2) - { - rgbItem->load(json["Items"].toArray()[0].toObject()); - auxItem->load(json["Items"].toArray()[1].toObject()); - } globalItems.refresh(); } +bool PrimaryMainObject::storeToDisk(const QString& filename) +{ + store(settings); + itemLoader.updateJson(settings); + return storeJsonObjectToDisk(filename, settings); +} + +bool PrimaryMainObject::loadFromDisk(const QString& filename) +{ + bool error = false; + QJsonObject json = getJsonObjectFromDisk(filename, &error); + if(!error) + load(json); + return error; +} + SecondaryMainObject::SecondaryMainObject(QString host, int port, QObject *parent) : MainObject(parent), tcpClient(new TcpClient) @@ -84,8 +141,12 @@ SecondaryMainObject::SecondaryMainObject(QString host, int port, QObject *parent if(!tcpClient->launch(QHostAddress(host), port)) { QMessageBox::critical(nullptr, "Error", "Could not connect to "+host+":"+QString::number(port)); - exit(1); + QMetaObject::invokeMethod(this, [](){exit(1);}, Qt::QueuedConnection); } + + connect(&globalItems, &ItemStore::itemUpdated, tcpClient, &TcpClient::itemUpdated); + + globalItems.refresh(); } SecondaryMainObject::~SecondaryMainObject() diff --git a/src/mainobject.h b/src/mainobject.h index 93a787a..05b8140 100644 --- a/src/mainobject.h +++ b/src/mainobject.h @@ -14,11 +14,8 @@ #include "microcontroller.h" #include "ui/mainwindow.h" #include "sensors/sunsensor.h" -#include "items/auxitem.h" -#include "items/rgbitem.h" -#include "items/poweritem.h" -#include "iomuliplexer.h" -#include "broadcast.h" +#include "items/fixeditemsource.h" +#include "items/itemloadersource.h" #include "tcpserver.h" class MainObject : public QObject @@ -28,6 +25,8 @@ class MainObject : public QObject public: explicit MainObject(QObject *parent = nullptr); ~MainObject(); + static QJsonObject getJsonObjectFromDisk(const QString& filename, bool* error = nullptr); + static bool storeJsonObjectToDisk(const QString& filename, const QJsonObject& json); public slots: void refresh(); @@ -38,11 +37,8 @@ class PrimaryMainObject : public MainObject Q_OBJECT public: - QJsonObject* settings; - - //io - QIODevice * const microDevice = nullptr; - IoMuliplexer ioMultiplexer; + QString settingsPath; + QJsonObject settings; Microcontroller micro; TcpServer* tcpServer; @@ -50,16 +46,17 @@ public: //sensors SunSensorSource sunSensorSource; - //items - std::shared_ptr powerItem; - std::shared_ptr rgbItem; - std::shared_ptr auxItem; + //item sources + FixedItemSource fixedItems; + ItemLoaderSource itemLoader; public: - explicit PrimaryMainObject(QIODevice* microDevice, QJsonObject* settings, QString host, int port, QObject *parent = nullptr); + explicit PrimaryMainObject(QIODevice* microDevice, const QString& settingsPath, const QString& host, int port, QObject *parent = nullptr); ~PrimaryMainObject(); void store(QJsonObject& json); void load(const QJsonObject& json); + bool storeToDisk(const QString& filename); + bool loadFromDisk(const QString& filename); }; class SecondaryMainObject : public MainObject diff --git a/src/microcontroller.cpp b/src/microcontroller.cpp index 174cfd0..4833461 100644 --- a/src/microcontroller.cpp +++ b/src/microcontroller.cpp @@ -138,12 +138,10 @@ void Microcontroller::processList(const QString& buffer) if(bufferList.size() >= 8 && buffer.startsWith("ITEM NUMBER:")) { relayList.push_back(processRelayLine(buffer)); - qDebug()<<"Micro item recived:"<getName(); } else if(buffer.contains("EOL")) { listMode = false; - qDebug()<<"got relay list " << relayList.size(); gotItems(relayList); relayList.clear(); } @@ -158,13 +156,13 @@ void Microcontroller::processRelayState(const QString& buffer) void Microcontroller::processSensorState(const QString& buffer) { Sensor sensor = Sensor::sensorFromString(buffer); - if(sensor.type != Sensor::TYPE_DUMMY) gotSensorState(sensor); + if(sensor.type != Sensor::TYPE_DUMMY) + gotSensorState(sensor); } void Microcontroller::processMicroReturn() { - qDebug()<<_buffer; if(listMode) { processList(_buffer); @@ -176,8 +174,14 @@ void Microcontroller::processMicroReturn() listMode = true; relayList.clear(); } - else if(_buffer.startsWith("ITEM NUMBER:"))processRelayState(_buffer); - else if(_buffer.startsWith("SENSOR")) processSensorState(_buffer); + else if(_buffer.startsWith("ITEM NUMBER:")) + { + processRelayState(_buffer); + } + else if(_buffer.startsWith("SENSOR")) + { + processSensorState(_buffer); + } } } @@ -188,8 +192,6 @@ void Microcontroller::isReadyRead() while(_port->getChar(&charBuf)) { _buffer.push_back(charBuf); - - qDebug()<<_buffer; if(_buffer.endsWith('\n') ) { _buffer.remove('\n'); diff --git a/src/sensors/sensor.h b/src/sensors/sensor.h index 80c37ab..86c82b9 100644 --- a/src/sensors/sensor.h +++ b/src/sensors/sensor.h @@ -46,7 +46,7 @@ public: { type = json["SensorType"].toInt(0); id = json["Id"].toInt(0); - field = json["Field"].toInt(0); + field = json["Field"].toDouble(0); name = json["Name"].toString("Sensor"); lastSeen = QDateTime::fromString(json["LastSeen"].toString("")); hidden = json["Hidden"].toBool(false); diff --git a/src/tcpserver.cpp b/src/tcpserver.cpp index b6f82e2..f49150f 100644 --- a/src/tcpserver.cpp +++ b/src/tcpserver.cpp @@ -1,6 +1,7 @@ #include #include #include +#include #include "items/item.h" #include "items/itemstore.h" @@ -14,7 +15,7 @@ TcpService::TcpService(QObject* parent): QJsonObject TcpService::createMessage(const QString& type, const QJsonArray& data) { QJsonObject json; - json["MesageType"] = type; + json["MessageType"] = type; json["Data"] = data; return json; } @@ -26,17 +27,18 @@ void TcpService::sensorEvent(Sensor sensor) sensor.store(sensorjson); sensors.append(sensorjson); QJsonObject json = createMessage("SensorUpdate", sensors); - sendJson(json); } void TcpService::itemUpdated(std::weak_ptr item) { + qDebug()<<__func__; QJsonArray items; QJsonObject itemjson; item.lock()->store(itemjson); items.append(itemjson); QJsonObject json = createMessage("ItemUpdate", items); + json["FullList"] = false; sendJson(json); } @@ -67,22 +69,26 @@ void TcpService::sendItems() item->store(itemjson); items.append(itemjson); } - sendJson(createMessage("ItemUpdate", items)); + QJsonObject json = createMessage("ItemUpdate", items); + json["FullList"] = true; + sendJson(json); } void TcpService::processIncomeingJson(const QByteArray& jsonbytes) { - qDebug()<<__func__<> items; for(QJsonValueRef itemjson : data) @@ -129,7 +137,10 @@ void TcpClient::processIncomeingJson(const QByteArray& jsonbytes) QJsonObject jsonobject = itemjson.toObject(); std::shared_ptr item = Item::loadItem(jsonobject); if(item) + { + item->setLoaded(false); items.push_back(item); + } } if(!items.empty()) gotItems(items, true); @@ -140,6 +151,45 @@ void TcpClient::processIncomeingJson(const QByteArray& jsonbytes) } } +void TcpClient::processComand(const QByteArray& command) +{ + if(command.startsWith("MSG JSON LEN ")) + { + state = STATE_RECV_JSON; + recievebytes = command.mid(13).toLongLong(); + } +} + +void TcpClient::socketReadyRead() +{ + buffer += socket->readAll(); + bool remianing = true; + while(remianing) + { + remianing = false; + while(state == STATE_IDLE && buffer.contains('\n')) + { + size_t newlineIndex = buffer.indexOf('\n'); + QByteArray command = buffer.left(newlineIndex); + buffer.remove(0, newlineIndex+1); + processComand(command); + remianing = true; + } + if(state == STATE_RECV_JSON) + { + if(recievebytes <= buffer.size()) + { + QByteArray json = buffer.left(recievebytes); + buffer.remove(0, recievebytes); + recievebytes = 0; + state = STATE_IDLE; + processIncomeingJson(json); + remianing = true; + } + } + } +} + TcpClient::~TcpClient() { delete socket; @@ -149,6 +199,7 @@ TcpServer::TcpServer(QObject* parent): TcpService(parent), server(this) { + connect(&server, &QTcpServer::newConnection, this, &TcpServer::incomingConnection); } void TcpServer::sendJson(const QJsonObject& json) @@ -167,17 +218,27 @@ void TcpServer::processIncomeingJson(const QByteArray& jsonbytes) QString type = json["MessageType"].toString(); if(type == "ItemUpdate") { + qDebug()<<"Got Items"; QJsonArray data = json["Data"].toArray(); + bool FullList = json["FullList"].toBool(false); std::vector> items; for(QJsonValueRef itemjson : data) { QJsonObject jsonobject = itemjson.toObject(); std::shared_ptr item = Item::loadItem(jsonobject); + item->setLoaded(FullList); if(item) items.push_back(item); } - if(!items.empty()) + if(FullList && !items.empty()) + { + requestReplaceItems(items); + sigRequestSave(); + } + else if(!items.empty()) + { gotItems(items, false); + } } else { @@ -195,6 +256,7 @@ void TcpServer::incomingConnection() while(server.hasPendingConnections()) { QTcpSocket* client = server.nextPendingConnection(); + qDebug()<<"Got new client from"<peerAddress().toString(); if(client) { clients.push_back({client}); @@ -231,11 +293,11 @@ void TcpServer::socketDisconnect() void TcpServer::processComand(const QByteArray& command, Client& client) { - qDebug()<<__func__<readAll(); + QByteArray newChars = clients[i].socket->readAll(); + clients[i].buffer += newChars; + bool remianing = true; while(remianing) { + qDebug()< item) { if(auto workItem = item.lock()) { - if(dynamic_cast(workItem.get())) - { - 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)); - } + if(workItem->isHidden()) + return; + 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/itemwidget.cpp b/src/ui/itemwidget.cpp index c1f681d..d073e50 100644 --- a/src/ui/itemwidget.cpp +++ b/src/ui/itemwidget.cpp @@ -5,39 +5,47 @@ #include #include -ItemWidget::ItemWidget(std::weak_ptr item, bool analog, bool nameOnly, QWidget *parent) : +ItemWidget::ItemWidget(std::weak_ptr item, QWidget *parent) : QWidget(parent), item_(item), ui(new Ui::ItemWidget) { ui->setupUi(this); - if(analog) + if(auto workingItem = item_.lock()) { - ui->horizontalSpacer->changeSize(0,0); - ui->checkBox->hide(); - } - else if(nameOnly) - { - ui->checkBox->hide(); - ui->slider->hide(); - } - else ui->slider->hide(); + if(workingItem->getValueType() == ITEM_VALUE_UINT) + { + ui->horizontalSpacer->changeSize(0,0); + ui->checkBox->hide(); + } + else if(workingItem->getValueType() == ITEM_VALUE_NO_VALUE) + { + ui->checkBox->hide(); + ui->slider->hide(); + } + else + { + ui->slider->hide(); + } - if(auto workingRelay = item_.lock()) - { - ui->checkBox->setChecked(workingRelay->getValue()); + ui->checkBox->setChecked(workingItem->getValue()); - ui->label->setText(workingRelay->getName()); + ui->label->setText(workingItem->getName()); - if(analog)connect(ui->slider, &QSlider::valueChanged, this, &ItemWidget::moveToValue); - else connect(ui->checkBox, &QCheckBox::toggled, this, &ItemWidget::moveToState); + if(workingItem->getValueType() == ITEM_VALUE_UINT) + connect(ui->slider, &QSlider::valueChanged, this, &ItemWidget::moveToValue); + 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(workingItem.get(), &Relay::valueChanged, this, &ItemWidget::stateChanged); connect(ui->pushButton_Remove, &QPushButton::clicked, this, &ItemWidget::deleteItem); } - else disable(); + else + { + disable(); + } } void ItemWidget::deleteItem() @@ -50,14 +58,18 @@ void ItemWidget::deleteItem() void ItemWidget::moveToValue(int value) { - if(auto workingItem = item_.lock()) workingItem->setValue(value); - else disable(); + if(auto workingItem = item_.lock()) + workingItem->setValue(value); + else + disable(); } void ItemWidget::moveToState(bool state) { - if(auto workingItem = item_.lock()) workingItem->setValue(state); - else disable(); + if(auto workingItem = item_.lock()) + workingItem->setValue(state); + else + disable(); } void ItemWidget::disable() @@ -70,9 +82,9 @@ void ItemWidget::disable() bool ItemWidget::controles(const ItemData& relay) { - if(auto workingRelay = item_.lock()) + if(auto workingItem = item_.lock()) { - if(relay == *workingRelay) return true; + if(relay == *workingItem) return true; else return false; } return true; @@ -80,9 +92,9 @@ bool ItemWidget::controles(const ItemData& relay) void ItemWidget::showSettingsDialog() { - if(auto workingRelay = item_.lock()) + if(auto workingItem = item_.lock()) { - ItemSettingsDialog dialog(workingRelay, this); + ItemSettingsDialog dialog(workingItem, this); dialog.exec(); } else disable(); @@ -95,7 +107,6 @@ std::weak_ptr ItemWidget::getItem() void ItemWidget::stateChanged(int state) { - qDebug()<<"widget got state "<slider->blockSignals(true); ui->slider->setValue(state); ui->slider->blockSignals(false); diff --git a/src/ui/itemwidget.h b/src/ui/itemwidget.h index d4c93c6..18d2861 100644 --- a/src/ui/itemwidget.h +++ b/src/ui/itemwidget.h @@ -30,7 +30,7 @@ private slots: void deleteItem(); public: - explicit ItemWidget(std::weak_ptr item, bool analog = false, bool nameOnly = false, QWidget *parent = nullptr); + explicit ItemWidget(std::weak_ptr item, 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 bdbc8a6..91d92f5 100644 --- a/src/ui/itemwidget.ui +++ b/src/ui/itemwidget.ui @@ -30,7 +30,7 @@ - Qt::Horizontal + Qt::Orientation::Horizontal @@ -46,10 +46,10 @@ 255 - true + false - Qt::Horizontal + Qt::Orientation::Horizontal diff --git a/src/ui/mainwindow.cpp b/src/ui/mainwindow.cpp index fe02c56..cbae5f2 100644 --- a/src/ui/mainwindow.cpp +++ b/src/ui/mainwindow.cpp @@ -1,10 +1,14 @@ #include "mainwindow.h" + +#include + #include "ui_mainwindow.h" #include "itemscrollbox.h" #include "itemsettingsdialog.h" #include "itemcreationdialog.h" -#include "../mainobject.h" -#include +#include "src/mainobject.h" +#include "src/programmode.h" +#include "src/items/poweritem.h" MainWindow::MainWindow(MainObject * const mainObject, QWidget *parent) : QMainWindow(parent), @@ -24,15 +28,20 @@ MainWindow::MainWindow(MainObject * const mainObject, QWidget *parent) : for(size_t i = 0; i < globalItems.getItems()->size(); ++i) ui->relayList->addItem(globalItems.getItems()->at(i)); + if(programMode != PROGRAM_MODE_PRIMARY) + ui->label_serialRecive->setHidden(true); + //Sensors ui->sensorListView->setShowHidden(false); ui->sensorListView->sensorsChanged(*globalSensors.getSensors()); connect(&globalSensors, &SensorStore::stateChenged, ui->sensorListView, &SensorListWidget::sensorsChanged); //RGB Leds - connect(&colorChooser, SIGNAL(colorSelected(QColor)), this, SLOT(slotChangedRgb(QColor))); + connect(&colorChooser, &QColorDialog::colorSelected, this, &MainWindow::sigSetRgb); connect(ui->button_quit, SIGNAL(clicked()), this, SLOT(close())); connect(ui->button_color, SIGNAL(clicked()), &colorChooser, SLOT(show())); + if(programMode != PROGRAM_MODE_PRIMARY) + ui->button_color->hide(); connect(ui->pushButton_addItem, &QPushButton::clicked, this, &MainWindow::showItemCreationDialog); connect(ui->relayList, &ItemScrollBox::deleteRequest, &globalItems, &ItemStore::removeItem); @@ -47,25 +56,30 @@ MainWindow::~MainWindow() void MainWindow::showPowerItemDialog() { - ItemSettingsDialog diag(std::shared_ptr(_powerItem), this); - diag.show(); - diag.exec(); + std::shared_ptr powerItem; + for(std::shared_ptr item : *globalItems.getItems()) + { + powerItem = std::dynamic_pointer_cast(item); + if(powerItem) + break; + } + if(powerItem) + { + ItemSettingsDialog diag(std::shared_ptr(powerItem), this); + diag.show(); + diag.exec(); + } + else + { + QMessageBox::warning(this, "Error", "No power item found, refresh first"); + } } - -void MainWindow::slotChangedRgb(const QColor color) -{ - (void)color; - //_micro->changeRgbColor(color); -} - void MainWindow::showItemCreationDialog() { ItemCreationDialog diag(this); diag.show(); if(diag.exec()) - { createdItem(diag.item); - } } void MainWindow::changeHeaderLableText(QString string) diff --git a/src/ui/mainwindow.h b/src/ui/mainwindow.h index 186b98d..238dd41 100644 --- a/src/ui/mainwindow.h +++ b/src/ui/mainwindow.h @@ -6,8 +6,8 @@ #include #include #include -#include "src/items/poweritem.h" +#include class MainObject; @@ -29,17 +29,15 @@ private: QColorDialog colorChooser; - std::shared_ptr _powerItem; - signals: void sigSave(); void createdItem(std::shared_ptr item); + void sigSetRgb(const QColor color); private slots: //RGB - void slotChangedRgb(const QColor color); void showPowerItemDialog(); void showItemCreationDialog(); diff --git a/src/ui/mainwindow.ui b/src/ui/mainwindow.ui index 510daa8..146ac88 100644 --- a/src/ui/mainwindow.ui +++ b/src/ui/mainwindow.ui @@ -42,7 +42,7 @@ false - + @@ -114,44 +114,6 @@ 0 - - - - - 0 - 0 - - - - - 0 - 0 - - - - - 16777215 - 48 - - - - - 0 - 128 - - - - Color - - - - - - - Config Shutdown - - - @@ -194,49 +156,91 @@ 0 - - - - Refesh - - - - - - - Save - - - - - - - Add Item - - - - - - - - 0 - 0 - - - - Qt::LayoutDirection::RightToLeft - - - Quit - - - + + + + + + Config Shutdown + + + + + + + + 0 + 0 + + + + + 0 + 0 + + + + + 16777215 + 16777215 + + + + + 0 + 128 + + + + Color + + + + + + + Refesh + + + + + + + Save + + + + + + + Add Item + + + + + + + + 0 + 0 + + + + Qt::LayoutDirection::RightToLeft + + + Quit + + + + +