Added ValueModel, first run.

This commit is contained in:
2025-12-16 22:42:35 +01:00
parent afb5828a65
commit 4c5e42fcfd
8 changed files with 110 additions and 76 deletions

1
bc.h
View File

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

View File

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

View File

@@ -32,15 +32,13 @@
#include <QMainWindow>
QT_BEGIN_NAMESPACE
namespace Ui
{
class BCMainWindow;
}
QT_END_NAMESPACE
#include <ui_bcmainwindow.h>
#include <bcvaluemanager.h>
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

View File

@@ -27,6 +27,7 @@
***************************************************************************/
#include <QMetaEnum>
#include <bcvalue.h>
@@ -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<BC::ID>()};
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;
}

View File

@@ -34,15 +34,15 @@
#include <QString>
#include <QList>
#include <QVariant>
#include <bc.h>
using optDouble = std::optional<double>;
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 );
};

View File

@@ -50,15 +50,19 @@ BCValueManager::BCValueManager()
}
BCValueManager::~BCValueManager()
{
// nothing to do here for now,
// our models are autokilled.
}
std::optional<BCValueModel*> 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()
{

View File

@@ -44,23 +44,23 @@ class BCValueManager : public QObject
public:
BCValueManager();
virtual ~BCValueManager();
void loadXml();
void saveXml();
std::optional<BCValueModel*> getModel(const QString& key );
protected:
void readDevice();
void makeValue(BCValueList& parsedValues);
void readDevice( BCValueList& parsedValues );
QXmlStreamReader _xml;
QMap<QString,BCValueModel*> _valueModels;
BCDevice::ID _currentDeviceID{BCDevice::ID::Invalid};
QMetaEnum _bcDeviceEnum{QMetaEnum::fromType<BCDevice::ID>()};
QMetaEnum _bcValueEnum{QMetaEnum::fromType<BC::ID>()};
};
#endif // BCVALUEMANAGER_H

View File

@@ -99,9 +99,10 @@ int main(int argc, char *argv[])
qDebug() << " schön: " << xxx << " -- " << uint8_t(xxx) << " : " << yyy;
qDebug() << " nice: " << metaEnum.enumName() << " : " <<metaEnum.name() << ": " << metaEnum.enclosingMetaObject()->className();
BCValueManager myMgr;
//BCValueManager myMgr;
//myMgr.loadXml();
return app.exec();
}