Added ValueModel, first run.
This commit is contained in:
1
bc.h
1
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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
38
bcvalue.cpp
38
bcvalue.cpp
@@ -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 )
|
||||
{
|
||||
|
||||
|
||||
|
||||
return nullptr;
|
||||
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>()};
|
||||
|
||||
/*
|
||||
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;
|
||||
}
|
||||
|
||||
15
bcvalue.h
15
bcvalue.h
@@ -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 );
|
||||
|
||||
};
|
||||
|
||||
|
||||
@@ -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)
|
||||
{
|
||||
@@ -140,62 +155,38 @@ void BCValueManager::readDevice()
|
||||
Q_ASSERT(_xml.isStartElement() && _xml.name() == "Device"_L1);
|
||||
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);
|
||||
|
||||
//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() ),
|
||||
.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()
|
||||
};
|
||||
parsedValues.push_back( BCValue::makeValue( params ) );
|
||||
|
||||
BCValue* newValue = BCValue::makeValue( _currentDeviceID, params );
|
||||
if(newValue)
|
||||
parsedValues.push_back( BCValue::makeValue( _currentDeviceID, params ) );
|
||||
|
||||
}
|
||||
|
||||
//printAttrs (_xml);
|
||||
_xml.skipCurrentElement();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// --- NEU: Speichern mit QXmlStreamWriter ---
|
||||
void BCValueManager::saveXml()
|
||||
{
|
||||
|
||||
@@ -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
|
||||
|
||||
3
main.cpp
3
main.cpp
@@ -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();
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user