diff --git a/BionxControl.pro b/BionxControl.pro index c11699f..78dbac0 100644 --- a/BionxControl.pro +++ b/BionxControl.pro @@ -26,15 +26,14 @@ windows SOURCES += \ bc.cpp \ + bcdatamodel.cpp \ + bcitemdelegate.cpp \ + bctransmitter.cpp \ lib/can_drv_win.c \ bccandriver.cpp \ bccandrivertinycan.cpp \ - bccommand.cpp \ - bccommandrunner.cpp \ bcvalue.cpp \ - bcvaluedelegate.cpp \ bcvaluemanager.cpp \ - bcvaluemodel.cpp \ main.cpp \ bcmainwindow.cpp @@ -42,13 +41,12 @@ HEADERS += \ bc.h \ bccandriver.h \ bccandrivertinycan.h \ - bccommand.h \ - bccommandrunner.h \ + bcdatamodel.h \ + bcitemdelegate.h \ bcmainwindow.h \ + bctransmitter.h \ bcvalue.h \ - bcvaluedelegate.h \ - bcvaluemanager.h \ - bcvaluemodel.h + bcvaluemanager.h FORMS += \ bcmainwindow.ui diff --git a/bccommand.cpp b/bccommand.cpp deleted file mode 100644 index 9f92b1d..0000000 --- a/bccommand.cpp +++ /dev/null @@ -1,49 +0,0 @@ -/*************************************************************************** - - BionxControl - Copyright © 2025 christoph holzheuer - christoph.holzheuer@gmail.com - - Using: - - BigXionFlasher USB V 0.2.4 rev. 97 - © 2011-2013 by Thomas Koenig - @see www.bigxionflasher.org - - Bionx Bike Info - © 2018 Thorsten Schmidt (tschmidt@ts-soft.de) - @see www.ts-soft.de - - mhs_can_drv.c 3.00 - © 2011 - 2015 by MHS-Elektronik GmbH & Co. KG, Germany - Demlehner Klaus, info@mhs-elektronik.de - @see www.mhs-elektronik.de - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 3 of the License, or - (at your option) any later version. - @see https://github.com/bikemike/bionx-bikeinfo - -***************************************************************************/ - - -#include - - -BCCommand::BCCommand(BCDevice::ID deviceID_, BC::ID targetID_) - : deviceID{deviceID_}, targetID{targetID_} -{ -} - -void BCCommand::setLongValue( uint8_t value) -{ - -} - -uint8_t BCCommand::getLongValue() -{ - return 0; -} - - diff --git a/bccommand.h b/bccommand.h deleted file mode 100644 index f97a66b..0000000 --- a/bccommand.h +++ /dev/null @@ -1,59 +0,0 @@ -/*************************************************************************** - - BionxControl - Copyright © 2025 christoph holzheuer - christoph.holzheuer@gmail.com - - Using: - - BigXionFlasher USB V 0.2.4 rev. 97 - © 2011-2013 by Thomas Koenig - @see www.bigxionflasher.org - - Bionx Bike Info - © 2018 Thorsten Schmidt (tschmidt@ts-soft.de) - @see www.ts-soft.de - - mhs_can_drv.c 3.00 - © 2011 - 2015 by MHS-Elektronik GmbH & Co. KG, Germany - Demlehner Klaus, info@mhs-elektronik.de - @see www.mhs-elektronik.de - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 3 of the License, or - (at your option) any later version. - @see https://github.com/bikemike/bionx-bikeinfo - -***************************************************************************/ - - -#ifndef BCCOMMAND_H -#define BCCOMMAND_H - -#include -#include - - - -struct BCCommand -{ - - BCCommand( BCDevice::ID deviceID, BC::ID targetID ); - - void setLongValue( uint8_t value); - uint8_t getLongValue(); - - QString label; - BCDevice::ID deviceID{BCDevice::ID::Invalid}; - BC::ID targetID{BC::ID::Invalid}; - -}; - -class BCCommandRunner; - - - - - -#endif // BCCOMMAND_H diff --git a/bccommandrunner.cpp b/bccommandrunner.cpp deleted file mode 100644 index 91a2758..0000000 --- a/bccommandrunner.cpp +++ /dev/null @@ -1,30 +0,0 @@ -/*************************************************************************** - - BionxControl - Copyright © 2025 christoph holzheuer - christoph.holzheuer@gmail.com - - Using: - - BigXionFlasher USB V 0.2.4 rev. 97 - © 2011-2013 by Thomas Koenig - @see www.bigxionflasher.org - - Bionx Bike Info - © 2018 Thorsten Schmidt (tschmidt@ts-soft.de) - @see www.ts-soft.de - - mhs_can_drv.c 3.00 - © 2011 - 2015 by MHS-Elektronik GmbH & Co. KG, Germany - Demlehner Klaus, info@mhs-elektronik.de - @see www.mhs-elektronik.de - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 3 of the License, or - (at your option) any later version. - @see https://github.com/bikemike/bionx-bikeinfo - -***************************************************************************/ - -#include diff --git a/bccommandrunner.h b/bccommandrunner.h deleted file mode 100644 index 0f2066d..0000000 --- a/bccommandrunner.h +++ /dev/null @@ -1,81 +0,0 @@ -/*************************************************************************** - - BionxControl - Copyright © 2025 christoph holzheuer - christoph.holzheuer@gmail.com - - Using: - - BigXionFlasher USB V 0.2.4 rev. 97 - © 2011-2013 by Thomas Koenig - @see www.bigxionflasher.org - - Bionx Bike Info - © 2018 Thorsten Schmidt (tschmidt@ts-soft.de) - @see www.ts-soft.de - - mhs_can_drv.c 3.00 - © 2011 - 2015 by MHS-Elektronik GmbH & Co. KG, Germany - Demlehner Klaus, info@mhs-elektronik.de - @see www.mhs-elektronik.de - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 3 of the License, or - (at your option) any later version. - @see https://github.com/bikemike/bionx-bikeinfo - -***************************************************************************/ - - -#ifndef BCCOMMANDRUNNER_H -#define BCCOMMANDRUNNER_H - -#include - - -#include -#include -#include -#include -#include -#include -#include "bccommand.h" - -class Worker : public QObject -{ - Q_OBJECT - -public slots: - - void doWork(const QString ¶meter) - { - QString result; - /* ... here is the expensive or blocking operation ... */ - emit resultReady(result); - } - -signals: - - void resultReady(const QString &result); -}; - -/* -class BCCommandRunner : public QObject -{ - Q_OBJECT - -public: - - BCCommandRunner(); - virtual ~BCCommandRunner() = default; - -protected: -}; -*/ - - - - - -#endif // BCCOMMANDRUNNER_H diff --git a/bcvaluemodel.cpp b/bcdatamodel.cpp similarity index 98% rename from bcvaluemodel.cpp rename to bcdatamodel.cpp index 133923f..369ffcf 100644 --- a/bcvaluemodel.cpp +++ b/bcdatamodel.cpp @@ -28,7 +28,7 @@ ***************************************************************************/ -#include +#include diff --git a/bcvaluemodel.h b/bcdatamodel.h similarity index 95% rename from bcvaluemodel.h rename to bcdatamodel.h index 441e5fd..1d86e72 100644 --- a/bcvaluemodel.h +++ b/bcdatamodel.h @@ -28,8 +28,8 @@ ***************************************************************************/ -#ifndef BCVALUEMODEL_H -#define BCVALUEMODEL_H +#ifndef BCDATAMODEL_H +#define BCDATAMODEL_H #include @@ -60,4 +60,4 @@ private: }; -#endif // BCVALUEMODEL_H +#endif // BCDATAMODEL_H diff --git a/bcvaluedelegate.cpp b/bcitemdelegate.cpp similarity index 83% rename from bcvaluedelegate.cpp rename to bcitemdelegate.cpp index 4b53d78..31bcd98 100644 --- a/bcvaluedelegate.cpp +++ b/bcitemdelegate.cpp @@ -4,19 +4,19 @@ #include #include -#include "bcvaluedelegate.h" +#include "bcitemdelegate.h" #include "bcvalue.h" #include "qapplication.h" -BCValueDelegate::BCValueDelegate(QObject *parent) +BCItemDelegate::BCItemDelegate(QObject *parent) : QStyledItemDelegate(parent) { } -QString BCValueDelegate::displayText(const QVariant& dataValue, const QLocale& locale) const +QString BCItemDelegate::displayText(const QVariant& dataValue, const QLocale& locale) const { // Wir prüfen, ob im Variant unser Struct steckt if (dataValue.canConvert()) @@ -34,7 +34,7 @@ QString BCValueDelegate::displayText(const QVariant& dataValue, const QLocale& l } -QWidget *BCValueDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const +QWidget *BCItemDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const { QVariant rawData = index.data(Qt::EditRole); if (!rawData.canConvert()) @@ -82,7 +82,7 @@ QWidget *BCValueDelegate::createEditor(QWidget *parent, const QStyleOptionViewIt return QStyledItemDelegate::createEditor(parent, option, index); } -void BCValueDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const +void BCItemDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const { // Daten vom Model in den Editor laden BCValue bc = index.data(Qt::EditRole).value(); @@ -101,7 +101,7 @@ void BCValueDelegate::setEditorData(QWidget *editor, const QModelIndex &index) c } } -void BCValueDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const +void BCItemDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const { // Daten vom Editor zurück ins Model speichern (Beim Schließen) QSlider *slider = editor->findChild("slider"); @@ -114,12 +114,12 @@ void BCValueDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, c } } -void BCValueDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const +void BCItemDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const { editor->setGeometry(option.rect); } -QSize BCValueDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const +QSize BCItemDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const { return QStyledItemDelegate::sizeHint(option,index); /* @@ -132,7 +132,7 @@ QSize BCValueDelegate::sizeHint(const QStyleOptionViewItem &option, const QModel */ } -QString BCValueDelegate::formatDisplayString(const QModelIndex &index) const +QString BCItemDelegate::formatDisplayString(const QModelIndex &index) const { if (!index.isValid()) return QString(); diff --git a/bcvaluedelegate.h b/bcitemdelegate.h similarity index 80% rename from bcvaluedelegate.h rename to bcitemdelegate.h index 8077fa4..d47a355 100644 --- a/bcvaluedelegate.h +++ b/bcitemdelegate.h @@ -1,17 +1,17 @@ -// BCValueDelegate.h -#ifndef BCVALUEDELEGATE_H -#define BCVALUEDELEGATE_H +// BCItemDelegate.h +#ifndef BCITEMDELEGATE_H +#define BCITEMDELEGATE_H #include -class BCValueDelegate : public QStyledItemDelegate +class BCItemDelegate : public QStyledItemDelegate { Q_OBJECT public: - explicit BCValueDelegate(QObject *parent = nullptr); + explicit BCItemDelegate(QObject *parent = nullptr); // WICHTIG: Zuständig für die normale Anzeige (ohne Editor) QString displayText(const QVariant& dataValue, const QLocale& locale) const override; @@ -29,4 +29,4 @@ private: }; -#endif // BCVALUEDELEGATE_H +#endif // BCITEMDELEGATE_H diff --git a/bcmainwindow.cpp b/bcmainwindow.cpp index 6ba0144..57c97f1 100644 --- a/bcmainwindow.cpp +++ b/bcmainwindow.cpp @@ -28,7 +28,7 @@ ***************************************************************************/ #include -#include +#include #include BCMainWindow::BCMainWindow(QWidget *parent) @@ -39,7 +39,7 @@ BCMainWindow::BCMainWindow(QWidget *parent) auto model = _valueManager.getModel( "Console"_L1 ); if( model) _valueView->setModel( *model ); - _valueView->setItemDelegate( new BCValueDelegate( _valueView) ); + _valueView->setItemDelegate( new BCItemDelegate( _valueView) ); connect( _connectButton, &QPushButton::clicked, &_valueManager, &BCValueManager::onToggleConnectionState ); } diff --git a/bcvaluemanager.cpp b/bcvaluemanager.cpp index e6842a6..68ad719 100644 --- a/bcvaluemanager.cpp +++ b/bcvaluemanager.cpp @@ -47,15 +47,56 @@ using namespace Qt::StringLiterals; BCValueManager::BCValueManager() { + qRegisterMetaType("BCValue"); + _transmitter.moveToThread(&_worker); + + // 4. Verbindungen herstellen (Signal/Slot über Thread-Grenzen) + + // A) Befehl senden (Manager -> Runner) + connect(this, &BCValueManager::newCommandArrived, &_transmitter, &BCTransmitter::addCommand); + + // B) Ergebnisse empfangen (Runner -> Manager) + connect(&_transmitter, &BCTransmitter::commandFinished, this, &BCValueManager::onCommandFinished); + connect(&_transmitter, &BCTransmitter::messageLogged, this, &BCValueManager::onRunnerMessage); + + // C) Aufräumen: Wenn Thread endet, lösche den Runner + connect(&_worker, &QThread::finished, &_transmitter, &QObject::deleteLater); + + // 5. Thread starten + _worker.start(); } BCValueManager::~BCValueManager() { // nothing to do here for now, // our models are autokilled. + + _worker.quit(); // Event Loop stoppen + _worker.wait(); // Warten bis Thread wirklich fertig ist } + +void BCValueManager::transmitValue(const BCValue& value) +{ + // Wir rufen keine Methode am Runner direkt auf! + // Wir emitten ein Signal. Qt kümmert sich um den Thread-Wechsel. + + emit newCommandArrived(value); + +} + +void BCValueManager::onCommandFinished(int id, bool success) +{ + qDebug() << "[Manager] Command" << id << "finished. Success:" << success; +} + +void BCValueManager::onRunnerMessage(const QString &msg) +{ + qDebug() << "[Worker says]:" << msg; +} + + void BCValueManager::onToggleConnectionState( bool connect ) { if( connect ) @@ -69,7 +110,8 @@ void BCValueManager::onToggleConnectionState( bool connect ) { qDebug() << "Console not responding"; } - else { + else + { /* swVersion = getValue(CONSOLE, CONSOLE_REF_SW); printf( "Console information:" _NL diff --git a/bcvaluemanager.h b/bcvaluemanager.h index 05a777a..63758d9 100644 --- a/bcvaluemanager.h +++ b/bcvaluemanager.h @@ -36,8 +36,10 @@ #include #include -#include +#include +#include #include +#include class BCValueManager : public QObject { @@ -45,13 +47,16 @@ class BCValueManager : public QObject public: - BCValueManager(); + BCValueManager( ); virtual ~BCValueManager(); std::optional getModel(const QString& key ); BCValue makeValue(BCDevice::ID deviceID, const BCValueParams& params ); + // Public API für die Applikation + void transmitValue( const BCValue& value); + public slots: void loadXml(); @@ -60,6 +65,14 @@ public slots: signals: + // Internes Signal, um Daten an den Worker Thread zu senden + void newCommandArrived(BCValue cmd); + +private slots: + + // Slots für Rückmeldungen vom Runner + void onCommandFinished(int id, bool success); + void onRunnerMessage(const QString &msg); protected: @@ -71,6 +84,10 @@ protected: QMetaEnum _bcDeviceEnum{QMetaEnum::fromType()}; BCCanDriverTinyCan _canDriver; + + QThread _worker; + BCTransmitter _transmitter; + }; #endif // BCVALUEMANAGER_H