diff --git a/bc.h b/bc.h index cc5cbf6..e886e9d 100644 --- a/bc.h +++ b/bc.h @@ -750,6 +750,7 @@ namespace BCTags inline constexpr auto ID = "ID"_L1; inline constexpr auto Default = "Default"_L1; inline constexpr auto Current = "Current"_L1; + inline constexpr auto Enabled = "Enabled"_L1; inline constexpr auto UnitType = "UnitType"_L1; inline constexpr auto Min = "Min"_L1; inline constexpr auto Max = "Max"_L1; diff --git a/bcmainwindow.cpp b/bcmainwindow.cpp index 5f09d5d..d873d38 100644 --- a/bcmainwindow.cpp +++ b/bcmainwindow.cpp @@ -32,12 +32,16 @@ BCMainWindow::BCMainWindow(QWidget *parent) : QMainWindow(parent) - , ui(new Ui::BCMainWindow) { - ui->setupUi(this); + setupUi(this); + _valueManager.loadXml(); + auto model = _valueManager.getModel( "Console"_L1 ); + if( model) + _valueView->setModel( *model ); + } BCMainWindow::~BCMainWindow() { - delete ui; + } diff --git a/bcmainwindow.h b/bcmainwindow.h index a47df35..6ef6153 100644 --- a/bcmainwindow.h +++ b/bcmainwindow.h @@ -32,15 +32,13 @@ #include -QT_BEGIN_NAMESPACE -namespace Ui -{ -class BCMainWindow; -} -QT_END_NAMESPACE +#include +#include -class BCMainWindow : public QMainWindow + + +class BCMainWindow : public QMainWindow, public Ui_BCMainWindow { Q_OBJECT @@ -49,9 +47,13 @@ public: BCMainWindow(QWidget *parent = nullptr); ~BCMainWindow(); -private: +public slots: + +protected: + + BCValueManager _valueManager; + - Ui::BCMainWindow *ui; }; #endif // BCMAINWINDOW_H diff --git a/bcvalue.cpp b/bcvalue.cpp index e9324d3..21fd74e 100644 --- a/bcvalue.cpp +++ b/bcvalue.cpp @@ -27,6 +27,7 @@ ***************************************************************************/ +#include #include @@ -47,10 +48,39 @@ uint8_t BCValue::getLongValue() } -BCValue* BCValue::makeValue(BCDevice::ID deviceI, const BCValueParams& params ) +BCValue* BCValue::makeValue( BCDevice::ID deviceID, const BCValueParams& params ) { + auto setIfExists = [&]( QStringView source, optDouble& target ) + { + if( !source.isEmpty() ) + { + bool ok; + double testVal = source.toDouble(&ok); + if (ok) + target = testVal; + } + }; + static QMetaEnum s_bcValueEnum{QMetaEnum::fromType()}; - return nullptr; + /* + Wir brauchen: + - eine gültige ID + + */ + BCValue* newValue{}; + + auto IDVal = s_bcValueEnum.keyToValue64( params.ID.toLatin1().constData() ); + if( IDVal.has_value() ) + { + newValue = new BCValue( deviceID, BC::ID( IDVal.value() ) ); + setIfExists( params.Factor, newValue->factor ); + setIfExists( params.Min, newValue->min ); + setIfExists( params.Max, newValue->max ); + newValue->defaultValue.setValue( params.Default ); + newValue->value.setValue( params.Current ); + } + + return newValue; } diff --git a/bcvalue.h b/bcvalue.h index 072c387..1c6bd6d 100644 --- a/bcvalue.h +++ b/bcvalue.h @@ -34,15 +34,15 @@ #include #include +#include #include +using optDouble = std::optional; struct BCValueParams { - BCDevice::ID DeviceID{BCDevice::ID::Invalid}; - BC::ID ID{BC::ID::Invalid}; - QString IDString; + QString ID; QString Default; QString Current; QString Enabled; @@ -74,12 +74,17 @@ public: uint8_t getLongValue(); QString label; + BCDevice::ID deviceID{BCDevice::ID::Invalid}; BC::ID targetID{BC::ID::Invalid}; - double value{-1}; + optDouble min; + optDouble max; + optDouble factor; + QVariant defaultValue; + QVariant value; - static BCValue* makeValue( const BCValueParams& params ); + static BCValue* makeValue( BCDevice::ID deviceID, const BCValueParams& params ); }; diff --git a/bcvaluemanager.cpp b/bcvaluemanager.cpp index 90fc2b4..87caf07 100644 --- a/bcvaluemanager.cpp +++ b/bcvaluemanager.cpp @@ -50,15 +50,19 @@ BCValueManager::BCValueManager() } - - - BCValueManager::~BCValueManager() { // nothing to do here for now, // our models are autokilled. } +std::optional BCValueManager::getModel(const QString& key ) +{ + if( _valueModels.contains( key) ) + return _valueModels[key]; + return std::nullopt; +} + void BCValueManager::loadXml() { auto printAttrs = [](const QXmlStreamReader& xml) @@ -94,19 +98,30 @@ void BCValueManager::loadXml() // ?? Q_ASSERT(_xml.isStartElement() && _xml.name() == "Bike"_L1); - while (!_xml.atEnd() && !_xml.hasError()) { QXmlStreamReader::TokenType token = _xml.readNext(); if (token == QXmlStreamReader::StartElement) { - qDebug() << " --- Device: " << _xml.name() << ": " << _xml.attributes().value("Type"_L1); + QString deviceType = _xml.attributes().value("Type"_L1).toString(); + qDebug() << " --- Device: " << _xml.name() << ": " << deviceType; printAttrs (_xml); const char* deviceKey = _xml.attributes().value("Type"_L1).toLatin1().constData(); auto deviceID = _bcDeviceEnum.keyToValue64(deviceKey); //_currentDeviceID = BCDevice::ID( deviceID.value_or( BCDevice::ID::Invalid ) ); _currentDeviceID = deviceID.has_value() ? BCDevice::ID( deviceID.value() ) : BCDevice::ID::Invalid; - readDevice(); + if(deviceID.has_value()) + { + BCValueList parsedValues; + readDevice( parsedValues ); + if( parsedValues.count() ) + { + BCValueModel* valueModel = new BCValueModel( this ); + valueModel->setValueList(parsedValues); + _valueModels.insert( deviceType, valueModel ); + } + } + } } @@ -125,7 +140,7 @@ void BCValueManager::loadXml() } -void BCValueManager::readDevice() +void BCValueManager::readDevice( BCValueList& parsedValues ) { auto printAttrs = [](const QXmlStreamReader& xml) { @@ -136,66 +151,42 @@ void BCValueManager::readDevice() qDebug().noquote() << parts.join(" "); }; - printAttrs (_xml); + printAttrs (_xml); Q_ASSERT(_xml.isStartElement() && _xml.name() == "Device"_L1); - qDebug() << " ---------------"; + qDebug() << " ---------------"; - BCValueList parsedValues; //while (!_xml.atEnd() && !_xml.hasError()) while( _xml.readNextStartElement() ) { if( _xml.attributes().hasAttribute(BCTags::ID) ) { - makeValue(parsedValues); + //qDebug() << " --- found: " << _xml.name() << " : " << _xml.attributes().value(BCTags::ID); + + BCValueParams params + { + .ID = _xml.attributes().value(BCTags::ID).toString(), + .Default = _xml.attributes().value(BCTags::Default).toString(), + .Current = _xml.attributes().value(BCTags::Current).toString(), + .Enabled = _xml.attributes().value(BCTags::Enabled).toString(), + .UnitType = _xml.attributes().value(BCTags::UnitType).toString(), + .Min = _xml.attributes().value(BCTags::Min).toString(), + .Max = _xml.attributes().value(BCTags::Max).toString(), + .Factor = _xml.attributes().value(BCTags::Factor).toString() + }; + + BCValue* newValue = BCValue::makeValue( _currentDeviceID, params ); + if(newValue) + parsedValues.push_back( BCValue::makeValue( _currentDeviceID, params ) ); + } //printAttrs (_xml); _xml.skipCurrentElement(); - /* - if (token == QXmlStreamReader::StartElement ) - { - qDebug() << " --- moo: " << _xml.name(); - //Q_ASSERT(_xml.name() == "Value"_L1); - //makeValue(); - //d.name = xml.attributes().value(u"name") - } - */ - } - - //qDebug()xml.attributes().value(u"name").toString(); - /* - Device d; - d.name = xml.attributes().value(u"name").toString(); - d.ip = xml.attributes().value(u"ip").toString(); - parsedValues.append(d); - */ - -} - -void BCValueManager::makeValue(BCValueList& parsedValues) -{ - const char* IDKey = _xml.attributes().value(BCTags::ID).toLatin1().constData(); - qDebug() << " --- found: " << _xml.name() << " : " << IDKey; - auto IDVal = _bcValueEnum.keyToValue64( IDKey ); - - if( IDVal.has_value() ) - { - BCValueParams params - { - .DeviceID = _currentDeviceID, - .ID = BC::ID( IDVal.value() ), - .Default = _xml.attributes().value(BCTags::Default).toString(), - .Current = _xml.attributes().value(BCTags::Current).toString(), - .UnitType = _xml.attributes().value(BCTags::UnitType).toString(), - .Min = _xml.attributes().value(BCTags::Min).toString(), - .Max = _xml.attributes().value(BCTags::Max).toString(), - .Factor = _xml.attributes().value(BCTags::Factor).toString() - }; - parsedValues.push_back( BCValue::makeValue( params ) ); } } + // --- NEU: Speichern mit QXmlStreamWriter --- void BCValueManager::saveXml() { diff --git a/bcvaluemanager.h b/bcvaluemanager.h index bac0093..805f218 100644 --- a/bcvaluemanager.h +++ b/bcvaluemanager.h @@ -44,23 +44,23 @@ class BCValueManager : public QObject public: - BCValueManager(); virtual ~BCValueManager(); void loadXml(); void saveXml(); + std::optional getModel(const QString& key ); + protected: - void readDevice(); - void makeValue(BCValueList& parsedValues); + void readDevice( BCValueList& parsedValues ); QXmlStreamReader _xml; QMap _valueModels; BCDevice::ID _currentDeviceID{BCDevice::ID::Invalid}; QMetaEnum _bcDeviceEnum{QMetaEnum::fromType()}; - QMetaEnum _bcValueEnum{QMetaEnum::fromType()}; + }; #endif // BCVALUEMANAGER_H diff --git a/main.cpp b/main.cpp index 92908c8..712c507 100644 --- a/main.cpp +++ b/main.cpp @@ -99,9 +99,10 @@ int main(int argc, char *argv[]) qDebug() << " schön: " << xxx << " -- " << uint8_t(xxx) << " : " << yyy; qDebug() << " nice: " << metaEnum.enumName() << " : " <className(); - BCValueManager myMgr; + //BCValueManager myMgr; //myMgr.loadXml(); + return app.exec(); }