From c4714bfca21470b5c42038858a5966b9acd6c6f1 Mon Sep 17 00:00:00 2001 From: "PANIK\\chris" Date: Fri, 26 Dec 2025 23:09:53 +0100 Subject: [PATCH] Added & configured actions. --- bc.h | 15 +++--- bcdatamanager.cpp | 76 ++++++++++++++----------------- bcdatamanager.h | 16 +++---- bcdatavalue.h | 6 ++- bcmainwindow.cpp | 113 +++++++++++++++++++++++++++++++++------------- bcmainwindow.h | 13 ++++-- bcmainwindow.ui | 3 ++ bcvaluemodel.cpp | 16 +++---- bcvaluemodel.h | 6 +-- 9 files changed, 159 insertions(+), 105 deletions(-) diff --git a/bc.h b/bc.h index 8ddf5ac..1867f4f 100644 --- a/bc.h +++ b/bc.h @@ -134,6 +134,8 @@ public: //{%Region Console} Invalid = 0x0, + ID_Pimp = 0xF1, // dummy id to identify the tuning page + ID_Console_Master = 0x08, // (Can Id In Master Mode) ID_Console_Slave = 0x48, // (Can Id In Slave Mode) @@ -706,12 +708,12 @@ public: //{%Region Sensor} - ID_Sensor = 0x68, - Reg_Sensor_Config_Gauge_Gain_Hi = 0x10, - Reg_Sensor_Config_Gauge_Gain_Lo = 0x11, + ID_Sensor = 0x68, + Reg_Sensor_Config_Gauge_Gain_Hi = 0x10, + Reg_Sensor_Config_Gauge_Gain_Lo = 0x11, - Reg_Sensor_Config_Ramp_Up_Steps_Hi = 0x12, - Reg_Sensor_Config_Ramp_Up_Steps_Lo = 0x13, + Reg_Sensor_Config_Ramp_Up_Steps_Hi = 0x12, + Reg_Sensor_Config_Ramp_Up_Steps_Lo = 0x13, Reg_Sensor_Config_Decay_Delay_Hi = 0x14, Reg_Sensor_Config_Decay_Delay_Lo = 0x15, @@ -772,7 +774,8 @@ public: Battery = uint8_t( BC::ID::ID_Battery), Motor = uint8_t( BC::ID::ID_Motor ), BIB = uint8_t( BC::ID::ID_Bib ), - Sensor = uint8_t( BC::ID::ID_Sensor ) + Sensor = uint8_t( BC::ID::ID_Sensor ), + Pimp = uint8_t( BC::ID::ID_Pimp ) }; Q_ENUM(ID) }; diff --git a/bcdatamanager.cpp b/bcdatamanager.cpp index 38da79f..ea01274 100644 --- a/bcdatamanager.cpp +++ b/bcdatamanager.cpp @@ -78,6 +78,17 @@ BCDataManager::~BCDataManager() } +BCTransmitter* BCDataManager::getTransmitter() +{ + return &_transmitter; +}; + +const BCValueList& BCDataManager::getCurrentValueList() +{ + return _currentValues; +} + + void BCDataManager::onCommandFinished(int id, bool success) { qDebug() << "[Manager] Command" << id << "finished. Success:" << success; @@ -128,19 +139,7 @@ void BCDataManager::onSyncFromDevice() */ } -std::optional BCDataManager::getModel(BCDevice::ID deviceID ) -{ - if( _valueModels.contains( deviceID) ) - return _valueModels[deviceID]; - return std::nullopt; -} - -BCTransmitter* BCDataManager::getTransmitter() -{ - return &_transmitter; -}; - -void BCDataManager::loadXmlBikeData() +void BCDataManager::loadXmlBikeData( const QString& fileName ) { auto printAttrs = [](const QXmlStreamReader& xml) { @@ -156,7 +155,7 @@ void BCDataManager::loadXmlBikeData() return; */ - QFile file(":/bikeinfo.xml"); + QFile file(fileName); if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { // __fix throw @@ -182,28 +181,17 @@ void BCDataManager::loadXmlBikeData() { QString deviceType = _xml.attributes().value("Type"_L1).toString(); printAttrs (_xml); + // Wir wollen die Device-ID aus dem XML Tag ermitteln const char* deviceKey = _xml.attributes().value("Type"_L1).toLatin1().constData(); - auto deviceID = _bcDeviceEnum.keyToValue64(deviceKey); + auto optDeviceID = _bcDeviceEnum.keyToValue64(deviceKey); //_currentDeviceID = BCDevice::ID( deviceID.value_or( BCDevice::ID::Invalid ) ); - if(deviceID.has_value()) + if( optDeviceID.has_value()) { - qDebug() << " --- Device: " << _xml.name() << ": " << deviceType << " : " << deviceID; - - _currentDeviceID = BCDevice::ID( deviceID.value() ); - BCValueList parsedValues; - loadXmlBikeDeviceData( parsedValues ); - if( parsedValues.count() ) - { - BCValueModel* valueModel = new BCValueModel( this ); - // hier lacht der blaue HASE - //valueModel->setValueList(parsedValues); - _valueModels.insert( _currentDeviceID, valueModel ); - } + qDebug() << " --- Device: " << _xml.name() << ": " << deviceType << " : " << optDeviceID; + BCDevice::ID currentDeviceID = BCDevice::ID( optDeviceID.value() ); + loadXmlBikeDeviceData(currentDeviceID); } - } - - _currentDeviceID = BCDevice::ID::Console; } /* @@ -221,7 +209,7 @@ void BCDataManager::loadXmlBikeData() } -void BCDataManager::loadXmlBikeDeviceData( BCValueList& parsedValues ) +void BCDataManager::loadXmlBikeDeviceData(BCDevice::ID deviceID) { auto printAttrs = [](const QXmlStreamReader& xml) { @@ -234,17 +222,18 @@ void BCDataManager::loadXmlBikeDeviceData( BCValueList& parsedValues ) printAttrs (_xml); Q_ASSERT(_xml.isStartElement() && _xml.name() == "Device"_L1); - qDebug() << " ---------------"; + qDebug() << " XXX ---------------"; + // Wertliste für neues Device leeren + _currentValues.clear(); - //while (!_xml.atEnd() && !_xml.hasError()) while( _xml.readNextStartElement() ) { if( _xml.attributes().hasAttribute(BCTags::ID) ) { //qDebug() << " --- found: " << _xml.name() << " : " << _xml.attributes().value(BCTags::ID); - QString id = _xml.attributes().value(BCTags::ID).toString(); + // füllen des Parameter packs BCDataParams params { .ID = id, @@ -253,18 +242,19 @@ void BCDataManager::loadXmlBikeDeviceData( BCValueList& parsedValues ) .UnitType = _xml.attributes().value(BCTags::UnitType).toString(), }; - // __fix! können ungültige werte erzeugt werden ? - //BCDataValue newValue = BCData::makeDataValue( _currentDeviceID, params ); - //if(newValue) - // parsedValues.push_back( newValue ); - std::optional newValue = makeDataValue( _currentDeviceID, params ); + // nur gültige Werte sind vorhanden und können gespeichert werden + std::optional newValue = makeDataValue( deviceID, params ); if(newValue) - parsedValues.push_back( *newValue ); + _currentValues.push_back( *newValue ); } - - //printAttrs (_xml); + // weiter zum nächsten Element _xml.skipCurrentElement(); } + + // Wenn dieses Device fertig geladen wurde, soll das MainWindow es abholen + if( !_currentValues.isEmpty() ) + emit valueListReady( deviceID ); + } std::optional BCDataManager::makeDataValue( BCDevice::ID deviceID, const BCDataParams& params ) diff --git a/bcdatamanager.h b/bcdatamanager.h index 20398c0..d7c9e85 100644 --- a/bcdatamanager.h +++ b/bcdatamanager.h @@ -55,15 +55,18 @@ public: std::optional getModel(BCDevice::ID deviceID ); BCTransmitter* getTransmitter(); + const BCValueList& getCurrentValueList(); public slots: - void loadXmlBikeData(); + void loadXmlBikeData( const QString& fileName ); void saveBikeData(); void onSyncFromDevice(); signals: + void valueListReady( BCDevice::ID deviceID ); + // Internes Signal, um Daten an den Worker Thread zu senden void sendValueCommand( BC::OpID, const BCDataValue* cmd); //void valuedTouched(const BCDataValue& cmd); @@ -85,18 +88,15 @@ protected: QString UnitType; }; - void loadXmlBikeDeviceData( BCValueList& parsedValues ); + void loadXmlBikeDeviceData( BCDevice::ID deviceID ); std::optional makeDataValue( BCDevice::ID deviceID, const BCDataParams& params ); - using BCDeviceModels = QMap; using BCValueTypeMap = QMap; - QXmlStreamReader _xml; - - BCDeviceModels _valueModels; - BCDevice::ID _currentDeviceID{BCDevice::ID::Invalid}; - QMetaEnum _bcDeviceEnum{QMetaEnum::fromType()}; + QXmlStreamReader _xml; + BCValueList _currentValues; + QMetaEnum _bcDeviceEnum{QMetaEnum::fromType()}; QThread _worker; BCTransmitter _transmitter; diff --git a/bcdatavalue.h b/bcdatavalue.h index 4a3f00a..671c069 100644 --- a/bcdatavalue.h +++ b/bcdatavalue.h @@ -80,6 +80,9 @@ public: void writeRawValueX( const BCAbstractTransmitter& transmitter ) const; // void reset() +// später +//protected: + //const BCValueType& valueType; //BCValueTypeCRef valueType; const BCValueType* valueType{}; @@ -87,6 +90,7 @@ public: BC::ID registerID{BC::ID::Invalid}; int rowInModel{-1}; QString label; + // ?? mutable QString visibleValue; QVariant defaultValue; @@ -95,8 +99,6 @@ public: //mutable std::optional rawValue; - - }; Q_DECLARE_METATYPE(BCDataValue*) diff --git a/bcmainwindow.cpp b/bcmainwindow.cpp index e956dfc..bfcd3a2 100644 --- a/bcmainwindow.cpp +++ b/bcmainwindow.cpp @@ -44,15 +44,59 @@ BCMainWindow::BCMainWindow(QWidget *parent) : QMainWindow(parent) { setupUi(this); + initData(); + + +} + +BCMainWindow::~BCMainWindow() +{ + + +} + +void BCMainWindow::initData() +{ + // Die Daten und auch die Datenmodelle für die Views werden + // vom DataManager verwaltet und an die Views weitergereicht. + + /* + auto setDeviceModel = [&]( BCDevice::ID deviceID, BCDevicePanel* panel ) + { + auto model = _dataManager.getModel( deviceID ); + if( model) + { + QAbstractItemView* valueView = panel->getValueView(); + valueView->setModel( *model ); + //valueView->resizeColumnsToContents(); + //_valueView->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents); + } + }; +*/ + + auto configureAction = [&]( QToolButton* button, QAction* action, BCDevice::ID deviceID ) + { + button->setDefaultAction( action); + connect( action, &QAction::triggered, this, &BCMainWindow::onActionButtonTriggered ); + connect( action, &QAction::toggled, this, &BCMainWindow::onActionButtonToggled ); + // new way + connect( action, &QAction::triggered, this, [&] + { + onShowDevicePanel( deviceID ); + }); + }; + + // Wir wollen die Devices den Views zuordnen können + _devicePanels[BCDevice::ID::Console] = _consolePanel; + _devicePanels[BCDevice::ID::Battery] = _batteryPanel; + _devicePanels[BCDevice::ID::Motor] = _motorPanel; // Die actions an die Buttons binden - _motorButton->setDefaultAction( _motorAction); - _consoleButton->setDefaultAction( _consoleAction ); - _batteryButton->setDefaultAction( _batteryAction ); - _pimpButton->setDefaultAction( _pimpAction); + configureAction(_motorButton, _motorAction, BCDevice::ID::Motor ); + configureAction(_consoleButton, _consoleAction, BCDevice::ID::Console ); + configureAction(_batteryButton, _batteryAction, BCDevice::ID::Battery ); + configureAction(_pimpButton, _pimpAction, BCDevice::ID::Pimp ); - // die Daten des eBikes laden - _dataManager.loadXmlBikeData(); // Die Daten und auch die Datenmodelle für die Views werden // vom DataManager verwaltet und an die Views weitergereicht. @@ -92,42 +136,49 @@ BCMainWindow::BCMainWindow(QWidget *parent) connect( _syncButton, &QPushButton::clicked, &_dataManager, &BCDataManager::onSyncFromDevice ); */ -} - -BCMainWindow::~BCMainWindow() -{ + // wir wollen Bescheid wissen, wenn ein Device (entspricht einem Datenmodel) + // fertig eingelesen wurde. + connect( &_dataManager, &BCDataManager::valueListReady, this, &BCMainWindow::onValueListReady ); + // die Daten des eBikes laden + _dataManager.loadXmlBikeData(":/bikeinfo.xml"_L1); } -void BCMainWindow::initData() -{ - // Die Daten und auch die Datenmodelle für die Views werden - // vom DataManager verwaltet und an die Views weitergereicht. - auto setDeviceModel = [&]( BCDevice::ID deviceID, BCDevicePanel* panel ) + +void BCMainWindow::onValueListReady( BCDevice::ID deviceID ) +{ + qDebug() << " --- onValueListReady!" << deviceID; + if( _devicePanels.contains( deviceID ) ) { - auto model = _dataManager.getModel( deviceID ); - if( model) - { - QAbstractItemView* valueView = panel->getValueView(); - valueView->setModel( *model ); - //valueView->resizeColumnsToContents(); - //_valueView->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents); - } - - - }; - - - _dataManager.loadXmlBikeData(); - - + const BCValueList& newValueList = _dataManager.getCurrentValueList(); + _devicePanels[deviceID]->setValueList( newValueList ); + } +} +void BCMainWindow::onShowDevicePanel( BCDevice::ID deviceID ) +{ + qDebug() << " --- onShowDevicePanel:" << deviceID; + if( _devicePanels.contains( deviceID ) ) + { + //const BCValueList& newValueList = _dataManager.getCurrentValueList(); + //_devicePanels[deviceID]->setValueList( newValueList ); + } +} +void BCMainWindow::onActionButtonTriggered( bool checked) +{ + qDebug() << " --- onActionButtonTriggered: " << checked; } +void BCMainWindow::onActionButtonToggled( bool checked) +{ + qDebug() << " --- onActionButtonToggled: " << checked; +} + + void BCMainWindow::onConnectButtonToggled(bool checked ) { //_dataManager.setDriverConnectionState( checked ); diff --git a/bcmainwindow.h b/bcmainwindow.h index 7e2d003..f9cd4d8 100644 --- a/bcmainwindow.h +++ b/bcmainwindow.h @@ -38,8 +38,7 @@ #include #include - -class AnimatedDelegate; +class BCDevicePanel; class BCMainWindow : public QMainWindow, public Ui_BCMainWindow { @@ -52,6 +51,10 @@ public: public slots: + void onValueListReady( BCDevice::ID deviceID ); + void onShowDevicePanel( BCDevice::ID deviceID ); + void onActionButtonTriggered( bool checked); + void onActionButtonToggled( bool checked); void onConnectButtonToggled(bool active ); protected: @@ -59,8 +62,12 @@ protected: void initData(); BCDataManager _dataManager; - AnimatedDelegate* _delegate; + // Wir brauchen eine Verbindung zwischen den Views + // und dem Device, das sie darstellen. + + using BCDevicePanels = QMap; + BCDevicePanels _devicePanels; }; diff --git a/bcmainwindow.ui b/bcmainwindow.ui index 86ca614..7b5cd02 100644 --- a/bcmainwindow.ui +++ b/bcmainwindow.ui @@ -151,6 +151,9 @@ + + true + :restart.png:restart.png diff --git a/bcvaluemodel.cpp b/bcvaluemodel.cpp index e5d978f..4f45d00 100644 --- a/bcvaluemodel.cpp +++ b/bcvaluemodel.cpp @@ -49,21 +49,19 @@ void BCValueModel::addValue(const BCDataValue& val) } -/* -void BCValueModel::setValueList(BCValueList* valueList) +const BCValueList& BCValueModel::getValueList() +{ + return _valueList; +} + + +void BCValueModel::setValueList(const BCValueList& valueList) { beginResetModel(); _valueList = valueList; endResetModel(); } -*/ -/* -BCValueList& BCValueModel::getValueList() -{ - return _valueList; -} -*/ int BCValueModel::rowCount(const QModelIndex& parent) const { diff --git a/bcvaluemodel.h b/bcvaluemodel.h index c0be104..c01e9c9 100644 --- a/bcvaluemodel.h +++ b/bcvaluemodel.h @@ -53,9 +53,8 @@ public: explicit BCValueModel(QObject *parent = nullptr); void addValue(const BCDataValue& val); - - //void setValueList(BCValueList* valueList); - //BCValueList *getValueList(); + void setValueList(const BCValueList& valueList); + const BCValueList& getValueList(); // Pure Virtual Functions von QAbstractTableModel int rowCount(const QModelIndex& parent = QModelIndex()) const override; @@ -70,6 +69,7 @@ public: private: + // Die eigentlichen Werte wohnen im tatsächlich hier, im Model. BCValueList _valueList; };