Added BCValue creation, part I.
This commit is contained in:
13
bc.h
13
bc.h
@@ -66,6 +66,19 @@ template <typename E>
|
|||||||
constexpr auto to_u(E e) noexcept {
|
constexpr auto to_u(E e) noexcept {
|
||||||
return static_cast<std::underlying_type_t<E>>(e);
|
return static_cast<std::underlying_type_t<E>>(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// constants.h
|
||||||
|
#pragma once
|
||||||
|
#include <QLatin1StringView>
|
||||||
|
|
||||||
|
namespace Consts {
|
||||||
|
using namespace Qt::Literals::StringLiterals;
|
||||||
|
// 1. Compile-Time Konstante (constexpr)
|
||||||
|
// 2. Kein Heap-Speicher (Zero Allocation)
|
||||||
|
// 3. Minimale Binary-Größe (1 Byte char) <- 8 Bit!
|
||||||
|
// Beste Performance, keine Allokation, Typ-Sicher
|
||||||
|
inline constexpr auto OrgName = "source::worx"_L1;
|
||||||
|
}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
struct BC
|
struct BC
|
||||||
|
|||||||
@@ -28,8 +28,7 @@
|
|||||||
***************************************************************************/
|
***************************************************************************/
|
||||||
|
|
||||||
|
|
||||||
#include <QXmlStreamReader>
|
|
||||||
#include <QXmlStreamWriter>
|
|
||||||
|
|
||||||
#include <QFile>
|
#include <QFile>
|
||||||
#include <QFileDialog>
|
#include <QFileDialog>
|
||||||
@@ -42,43 +41,149 @@
|
|||||||
#include <QStatusBar>
|
#include <QStatusBar>
|
||||||
#include <bcvaluemanager.h>
|
#include <bcvaluemanager.h>
|
||||||
|
|
||||||
|
|
||||||
|
using namespace Qt::StringLiterals;
|
||||||
|
|
||||||
|
|
||||||
BCValueManager::BCValueManager()
|
BCValueManager::BCValueManager()
|
||||||
{}
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
BCValueManager::~BCValueManager()
|
||||||
|
{
|
||||||
|
// nothing to do here for now,
|
||||||
|
// our models are autokilled.
|
||||||
|
}
|
||||||
|
|
||||||
void BCValueManager::loadXml()
|
void BCValueManager::loadXml()
|
||||||
{
|
{
|
||||||
|
auto printAttrs = [](const QXmlStreamReader& xml)
|
||||||
|
{
|
||||||
|
QStringList parts;
|
||||||
|
for (const auto &attr : xml.attributes()) {
|
||||||
|
parts << attr.name().toString() + "=\"" + attr.value().toString() + "\"";
|
||||||
|
}
|
||||||
|
qDebug().noquote() << parts.join(" ");
|
||||||
|
};
|
||||||
/*
|
/*
|
||||||
QString fileName = QFileDialog::getOpenFileName(this, "XML öffnen", "", "XML Files (*.xml)");
|
QString fileName = QFileDialog::getOpenFileName(this, "XML öffnen", "", "XML Files (*.xml)");
|
||||||
if (fileName.isEmpty()) return;
|
if (fileName.isEmpty())
|
||||||
|
return;
|
||||||
|
*/
|
||||||
|
|
||||||
QFile file(fileName);
|
QFile file(":/bikeinfo.xml");
|
||||||
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
|
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
|
||||||
QMessageBox::warning(this, "Fehler", "Datei konnte nicht geöffnet werden.");
|
{
|
||||||
|
// __fix throw
|
||||||
|
QMessageBox::warning(nullptr, "Fehler", "Datei konnte nicht geöffnet werden.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
QList<Device> parsedDevices;
|
_xml.setDevice(&file);
|
||||||
QXmlStreamReader xml(&file);
|
|
||||||
|
|
||||||
while (!xml.atEnd() && !xml.hasError()) {
|
if (_xml.readNextStartElement())
|
||||||
QXmlStreamReader::TokenType token = xml.readNext();
|
{
|
||||||
if (token == QXmlStreamReader::StartElement && xml.name() == u"device") {
|
if (_xml.name() != "Bike"_L1 )
|
||||||
|
// fix throw
|
||||||
|
_xml.raiseError(QObject::tr("The file is not an 'Bike' file."));
|
||||||
|
}
|
||||||
|
// ??
|
||||||
|
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);
|
||||||
|
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 (xml.hasError())
|
||||||
|
{
|
||||||
|
QMessageBox::critical(nullptr, "Parsing Fehler", xml.errorString());
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
m_model->setDevices(parsedValues);
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
// create & add new model to the model map
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void BCValueManager::readDevice()
|
||||||
|
{
|
||||||
|
auto printAttrs = [](const QXmlStreamReader& xml)
|
||||||
|
{
|
||||||
|
QStringList parts;
|
||||||
|
for (const auto &attr : xml.attributes()) {
|
||||||
|
parts << attr.name().toString() + "=\"" + attr.value().toString() + "\"";
|
||||||
|
}
|
||||||
|
qDebug().noquote() << parts.join(" ");
|
||||||
|
};
|
||||||
|
|
||||||
|
printAttrs (_xml);
|
||||||
|
Q_ASSERT(_xml.isStartElement() && _xml.name() == "Device"_L1);
|
||||||
|
qDebug() << " ---------------";
|
||||||
|
|
||||||
|
BCValueList parsedValues;
|
||||||
|
|
||||||
|
//while (!_xml.atEnd() && !_xml.hasError())
|
||||||
|
while( _xml.readNextStartElement() )
|
||||||
|
{
|
||||||
|
const char* key = _xml.attributes().value("I##D"_L1).toLatin1().constData();
|
||||||
|
qDebug() << " --- blub: " << _xml.name() << " : " << key << ":" <<_xml.attributes().value("ID"_L1);
|
||||||
|
if(key)
|
||||||
|
makeValue(parsedValues);
|
||||||
|
else
|
||||||
|
qDebug() << " --- fitz";
|
||||||
|
|
||||||
|
//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;
|
Device d;
|
||||||
d.name = xml.attributes().value(u"name").toString();
|
d.name = xml.attributes().value(u"name").toString();
|
||||||
d.ip = xml.attributes().value(u"ip").toString();
|
d.ip = xml.attributes().value(u"ip").toString();
|
||||||
parsedDevices.append(d);
|
parsedValues.append(d);
|
||||||
}
|
*/
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (xml.hasError())
|
void BCValueManager::makeValue(BCValueList& parsedValues)
|
||||||
{
|
{
|
||||||
QMessageBox::critical(this, "Parsing Fehler", xml.errorString());
|
// ID='Cons_Stat_Dist_Hi' Default='' Current='' Enabled='true' UnitType='mm' Factor='0.1' Min Max
|
||||||
} else {
|
const char* ID = _xml.attributes().value("firz"_L1).toLatin1().constData();
|
||||||
m_model->setDevices(parsedDevices);
|
auto enumVal = _bcValueEnum.keyToValue64( ID);
|
||||||
|
if( enumVal.has_value() )
|
||||||
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// --- NEU: Speichern mit QXmlStreamWriter ---
|
// --- NEU: Speichern mit QXmlStreamWriter ---
|
||||||
|
|||||||
@@ -31,7 +31,12 @@
|
|||||||
#ifndef BCVALUEMANAGER_H
|
#ifndef BCVALUEMANAGER_H
|
||||||
#define BCVALUEMANAGER_H
|
#define BCVALUEMANAGER_H
|
||||||
|
|
||||||
#include <bcvalue.h>
|
#include <QMap>
|
||||||
|
#include <QXmlStreamReader>
|
||||||
|
#include <QXmlStreamWriter>
|
||||||
|
#include <QMetaEnum>
|
||||||
|
|
||||||
|
#include <bcvaluemodel.h>
|
||||||
|
|
||||||
class BCValueManager : public QObject
|
class BCValueManager : public QObject
|
||||||
{
|
{
|
||||||
@@ -41,16 +46,21 @@ public:
|
|||||||
|
|
||||||
|
|
||||||
BCValueManager();
|
BCValueManager();
|
||||||
~BCValueManager() = default;
|
virtual ~BCValueManager();
|
||||||
|
|
||||||
BCValue* makeValue();
|
|
||||||
|
|
||||||
void loadXml();
|
void loadXml();
|
||||||
void saveXml();
|
void saveXml();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|
||||||
|
void readDevice();
|
||||||
|
void makeValue(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
|
#endif // BCVALUEMANAGER_H
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
<?xml version='1.0' encoding='UTF-8'?>
|
<?xml version='1.0' encoding='UTF-8'?>
|
||||||
|
|
||||||
<Bike name='franken-wheeler'>
|
<Bike name='franken-wheeler'>
|
||||||
<Console>
|
<Device Type="Console">
|
||||||
<!-- müssen wir doppel werte hier reinschreiben? -->
|
<!-- müssen wir doppel werte hier reinschreiben? -->
|
||||||
<Value ID='Cons_Stat_Dist_Hi' Default='' Current='' Enabled='true' UnitType='mm' Factor='0.1' />
|
<Value ID='Cons_Stat_Dist_Hi' Default='' Current='' Enabled='true' UnitType='mm' Factor='0.1' />
|
||||||
<Value ID='Cons_Stat_Dist_Lo' Default='' Current='' Enabled='true' UnitType='mm'/>
|
<Value ID='Cons_Stat_Dist_Lo' Default='' Current='' Enabled='true' UnitType='mm'/>
|
||||||
@@ -125,12 +125,12 @@
|
|||||||
<Value ID='Cons_Assist_Level_Rekuperation_1' Default='' Current='' Enabled='true' UnitType='%' Factor='1.5625'/>
|
<Value ID='Cons_Assist_Level_Rekuperation_1' Default='' Current='' Enabled='true' UnitType='%' Factor='1.5625'/>
|
||||||
<Value ID='Cons_Assist_Level_Rekuperation_2' Default='' Current='' Enabled='true' UnitType='%' Factor='1.5625'/>
|
<Value ID='Cons_Assist_Level_Rekuperation_2' Default='' Current='' Enabled='true' UnitType='%' Factor='1.5625'/>
|
||||||
|
|
||||||
|
</Device>
|
||||||
|
|
||||||
</Console>
|
<Device Type="Battery">
|
||||||
<Battery>
|
</Device>
|
||||||
</Battery>
|
<Device Type="Motor">
|
||||||
<Motor>
|
</Device>
|
||||||
</Motor>
|
<Device Type="Sensor">
|
||||||
<Sensor>
|
</Device>
|
||||||
</Sensor>
|
|
||||||
</Bike>
|
</Bike>
|
||||||
|
|||||||
5
main.cpp
5
main.cpp
@@ -38,7 +38,7 @@
|
|||||||
|
|
||||||
#include <bcmainwindow.h>
|
#include <bcmainwindow.h>
|
||||||
#include <bcvalue.h>
|
#include <bcvalue.h>
|
||||||
|
#include <bcvaluemanager.h>
|
||||||
|
|
||||||
/*
|
/*
|
||||||
#include <QMetaEnum>
|
#include <QMetaEnum>
|
||||||
@@ -99,6 +99,9 @@ int main(int argc, char *argv[])
|
|||||||
qDebug() << " schön: " << xxx << " -- " << uint8_t(xxx) << " : " << yyy;
|
qDebug() << " schön: " << xxx << " -- " << uint8_t(xxx) << " : " << yyy;
|
||||||
qDebug() << " nice: " << metaEnum.enumName() << " : " <<metaEnum.name() << ": " << metaEnum.enclosingMetaObject()->className();
|
qDebug() << " nice: " << metaEnum.enumName() << " : " <<metaEnum.name() << ": " << metaEnum.enclosingMetaObject()->className();
|
||||||
|
|
||||||
|
BCValueManager myMgr;
|
||||||
|
myMgr.loadXml();
|
||||||
|
|
||||||
return app.exec();
|
return app.exec();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user