Move functionality from datamanager to mainwindow.
This commit is contained in:
@@ -51,88 +51,17 @@ BCDataManager::BCDataManager(QObject *parent)
|
|||||||
{
|
{
|
||||||
|
|
||||||
//qRegisterMetaType<BCDataValue*>("BCDataValue*");
|
//qRegisterMetaType<BCDataValue*>("BCDataValue*");
|
||||||
qRegisterMetaType<BCDataValue*>();
|
//qRegisterMetaType<BCDataValue*>();
|
||||||
|
|
||||||
_transmitter.moveToThread(&_worker);
|
|
||||||
|
|
||||||
connect(this, &BCDataManager::sendValueCommand, &_transmitter, &BCTransmitter::enqueueValueOp);
|
|
||||||
|
|
||||||
// B) Ergebnisse empfangen (Runner -> Manager)
|
|
||||||
//connect(&_transmitter, &BCTransmitter::commandFinished, this, &BCDataManager::onCommandFinished);
|
|
||||||
//connect(&_transmitter, &BCTransmitter::messageLogged, this, &BCDataManager::onRunnerMessage);
|
|
||||||
|
|
||||||
// C) Aufräumen: Wenn Thread endet, lösche den Runner
|
|
||||||
connect(&_worker, &QThread::finished, &_transmitter, &QObject::deleteLater);
|
|
||||||
|
|
||||||
// 5. Thread starten
|
|
||||||
_worker.start();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
BCDataManager::~BCDataManager()
|
BCDataManager::~BCDataManager()
|
||||||
{
|
{
|
||||||
// nothing to do here for now,
|
|
||||||
// our models are autokilled.
|
|
||||||
|
|
||||||
_worker.quit(); // Event Loop stoppen
|
|
||||||
_worker.wait(); // Warten bis Thread wirklich fertig ist
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
BCTransmitter* BCDataManager::getTransmitter()
|
|
||||||
{
|
|
||||||
return &_transmitter;
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
void BCDataManager::onCommandFinished(int id, bool success)
|
|
||||||
{
|
|
||||||
qDebug() << "[Manager] Command" << id << "finished. Success:" << success;
|
|
||||||
}
|
|
||||||
|
|
||||||
void BCDataManager::onRunnerMessage(const QString &msg)
|
|
||||||
{
|
|
||||||
qDebug() << "[Worker says]:" << msg;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void BCDataManager::onSyncFromDevice()
|
|
||||||
{
|
|
||||||
qDebug() << " ---Syncing";
|
|
||||||
/*
|
|
||||||
if( _currentDeviceID != BCDevice::ID::Invalid )
|
|
||||||
{
|
|
||||||
if( _valueModels.contains(_currentDeviceID) )
|
|
||||||
{
|
|
||||||
|
|
||||||
BCValueModel* model = _valueModels[_currentDeviceID];
|
|
||||||
BCValueList& currentList = model->getValueList();
|
|
||||||
|
|
||||||
//BCDataValue& value = currentList[4];
|
|
||||||
|
|
||||||
for( const BCDataValue& value : currentList )
|
|
||||||
{
|
|
||||||
qDebug() << " --- value: " << value.label;
|
|
||||||
|
|
||||||
// statt '_transmitter.enqueueValueCommand( value )' entkoppeln
|
|
||||||
// wir das eleganter über emit sendValueCommand()
|
|
||||||
|
|
||||||
//_transmitter.enqueueValueCommand( value );
|
|
||||||
emit sendValueCommand( BC::OpID::ReadValue, &value);
|
|
||||||
|
|
||||||
emit valueTouched( value.rowInModel );
|
|
||||||
|
|
||||||
bc::processEventsFor(500);
|
|
||||||
|
|
||||||
//QApplication::processEvents();
|
|
||||||
// Thread schlafen lassen (Simulation einer blockierenden Operation)
|
|
||||||
//QThread::msleep(500);
|
|
||||||
|
|
||||||
}
|
|
||||||
} // if contains
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
}
|
|
||||||
|
|
||||||
void BCDataManager::loadXmlBikeData( const QString& fileName )
|
void BCDataManager::loadXmlBikeData( const QString& fileName )
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -54,30 +54,17 @@ public:
|
|||||||
virtual ~BCDataManager();
|
virtual ~BCDataManager();
|
||||||
|
|
||||||
std::optional<BCValueModel*> getModel(BCDevice::ID deviceID );
|
std::optional<BCValueModel*> getModel(BCDevice::ID deviceID );
|
||||||
BCTransmitter* getTransmitter();
|
|
||||||
|
|
||||||
|
|
||||||
public slots:
|
public slots:
|
||||||
|
|
||||||
void loadXmlBikeData( const QString& fileName );
|
void loadXmlBikeData( const QString& fileName );
|
||||||
void saveBikeData();
|
void saveBikeData();
|
||||||
void onSyncFromDevice();
|
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
|
|
||||||
void valueListReady( BCDevice::ID deviceID, BCValueList valueList );
|
void valueListReady( BCDevice::ID deviceID, BCValueList valueList );
|
||||||
|
|
||||||
// Internes Signal, um Daten an den Worker Thread zu senden
|
|
||||||
void sendValueCommand( BC::OpID, const BCDataValue* cmd);
|
|
||||||
//void valuedTouched(const BCDataValue& cmd);
|
|
||||||
void valueTouched(int rowInModel );
|
|
||||||
|
|
||||||
private slots:
|
|
||||||
|
|
||||||
// Slots für Rückmeldungen vom Runner
|
|
||||||
void onCommandFinished(int id, bool success);
|
|
||||||
void onRunnerMessage(const QString &msg);
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|
||||||
struct BCDataParams
|
struct BCDataParams
|
||||||
@@ -97,8 +84,7 @@ protected:
|
|||||||
QXmlStreamReader _xml;
|
QXmlStreamReader _xml;
|
||||||
QMetaEnum _bcDeviceEnum{QMetaEnum::fromType<BCDevice::ID>()};
|
QMetaEnum _bcDeviceEnum{QMetaEnum::fromType<BCDevice::ID>()};
|
||||||
|
|
||||||
QThread _worker;
|
|
||||||
BCTransmitter _transmitter;
|
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -44,14 +44,17 @@ BCDataValue::BCDataValue(const BCValueType* valueType_, BCDevice::ID deviceID_,
|
|||||||
visibleValue = "--";
|
visibleValue = "--";
|
||||||
}
|
}
|
||||||
|
|
||||||
void BCDataValue::readRawValueX( const BCAbstractTransmitter& transmitter ) const
|
QString BCDataValue::readRawValueX( const BCAbstractTransmitter& transmitter ) const
|
||||||
{
|
{
|
||||||
qDebug() << " --- READ X!";
|
qDebug() << " --- READ X!";
|
||||||
|
|
||||||
uint32_t devID = static_cast<uint32_t>(deviceID);
|
uint32_t devID = static_cast<uint32_t>(deviceID);
|
||||||
uint8_t regID = static_cast<uint8_t> (registerID);
|
uint8_t regID = static_cast<uint8_t> (registerID);
|
||||||
|
|
||||||
visibleValue = valueType->createStringValue( transmitter, devID, regID );
|
// wir sind hier im anderen thread! nicht einfach so reinschreiben
|
||||||
|
//visibleValue = valueType->createStringValue( transmitter, devID, regID );
|
||||||
|
|
||||||
|
return valueType->createStringValue( transmitter, devID, regID );
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -76,7 +76,7 @@ public:
|
|||||||
|
|
||||||
BCDataValue( const BCValueType* valueType_, BCDevice::ID deviceID_, BC::ID registerID_ );
|
BCDataValue( const BCValueType* valueType_, BCDevice::ID deviceID_, BC::ID registerID_ );
|
||||||
|
|
||||||
void readRawValueX( const BCAbstractTransmitter& transmitter ) const;
|
QString readRawValueX( const BCAbstractTransmitter& transmitter ) const;
|
||||||
void writeRawValueX( const BCAbstractTransmitter& transmitter ) const;
|
void writeRawValueX( const BCAbstractTransmitter& transmitter ) const;
|
||||||
// void reset()
|
// void reset()
|
||||||
|
|
||||||
|
|||||||
@@ -31,7 +31,32 @@
|
|||||||
</widget>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
<item>
|
<item>
|
||||||
<widget class="QTableView" name="_valueView"/>
|
<widget class="QTableView" name="_valueView">
|
||||||
|
<property name="frameShape">
|
||||||
|
<enum>QFrame::Shape::NoFrame</enum>
|
||||||
|
</property>
|
||||||
|
<property name="frameShadow">
|
||||||
|
<enum>QFrame::Shadow::Plain</enum>
|
||||||
|
</property>
|
||||||
|
<property name="lineWidth">
|
||||||
|
<number>0</number>
|
||||||
|
</property>
|
||||||
|
<property name="verticalScrollBarPolicy">
|
||||||
|
<enum>Qt::ScrollBarPolicy::ScrollBarAsNeeded</enum>
|
||||||
|
</property>
|
||||||
|
<property name="horizontalScrollBarPolicy">
|
||||||
|
<enum>Qt::ScrollBarPolicy::ScrollBarAlwaysOff</enum>
|
||||||
|
</property>
|
||||||
|
<property name="sizeAdjustPolicy">
|
||||||
|
<enum>QAbstractScrollArea::SizeAdjustPolicy::AdjustToContents</enum>
|
||||||
|
</property>
|
||||||
|
<property name="showGrid">
|
||||||
|
<bool>false</bool>
|
||||||
|
</property>
|
||||||
|
<attribute name="horizontalHeaderVisible">
|
||||||
|
<bool>true</bool>
|
||||||
|
</attribute>
|
||||||
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
</layout>
|
</layout>
|
||||||
</widget>
|
</widget>
|
||||||
|
|||||||
@@ -48,18 +48,18 @@ BCMainWindow::BCMainWindow(QWidget *parent)
|
|||||||
qRegisterMetaType<QList<BCDataValue>>("BCValueList");
|
qRegisterMetaType<QList<BCDataValue>>("BCValueList");
|
||||||
|
|
||||||
setupUi(this);
|
setupUi(this);
|
||||||
initData();
|
initMainWindow();
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
BCMainWindow::~BCMainWindow()
|
BCMainWindow::~BCMainWindow()
|
||||||
{
|
{
|
||||||
|
_worker.quit(); // Event Loop stoppen
|
||||||
|
_worker.wait(); // Warten bis Thread wirklich fertig ist
|
||||||
}
|
}
|
||||||
|
|
||||||
void BCMainWindow::initData()
|
void BCMainWindow::initMainWindow()
|
||||||
{
|
{
|
||||||
// Die Daten und auch die Datenmodelle für die Views werden
|
// Die Daten und auch die Datenmodelle für die Views werden
|
||||||
// vom DataManager verwaltet und an die Views weitergereicht.
|
// vom DataManager verwaltet und an die Views weitergereicht.
|
||||||
@@ -104,8 +104,6 @@ void BCMainWindow::initData()
|
|||||||
// Problem: alle Panels bekommen alle Datenmodelle angeboten.
|
// Problem: alle Panels bekommen alle Datenmodelle angeboten.
|
||||||
connect( &_dataManager, &BCDataManager::valueListReady, currentPanel, &BCDevicePanel::onValueListReady );
|
connect( &_dataManager, &BCDataManager::valueListReady, currentPanel, &BCDevicePanel::onValueListReady );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// Wir wollen die Devices den Views zuordnen können
|
// Wir wollen die Devices den Views zuordnen können
|
||||||
@@ -140,14 +138,33 @@ void BCMainWindow::initData()
|
|||||||
|
|
||||||
// besser: model::emit dataChanged
|
// besser: model::emit dataChanged
|
||||||
// also: emit dataChanged(index, index, {Qt::DisplayRole, Qt::EditRole, ValueRole});
|
// also: emit dataChanged(index, index, {Qt::DisplayRole, Qt::EditRole, ValueRole});
|
||||||
connect( &_dataManager, &BCDataManager::valueTouched, _delegate, &BCItemDelegate::onHighlightRow );
|
connect( &_dataManager, &BCMainWindow::valueTouched, _delegate, &BCItemDelegate::onHighlightRow );
|
||||||
connect( _connectButton, &QPushButton::clicked, transmitter, &BCTransmitter::onToggleConnectionState );
|
connect( _connectButton, &QPushButton::clicked, transmitter, &BCTransmitter::onToggleConnectionState );
|
||||||
connect( _syncButton, &QPushButton::clicked, &_dataManager, &BCDataManager::onSyncFromDevice );
|
connect( _syncButton, &QPushButton::clicked, &_dataManager, &BCMainWindow::onSyncFromDevice );
|
||||||
*/
|
*/
|
||||||
|
//_delegate->onHighlightRow(2); // 2 Sekunden Fade
|
||||||
|
|
||||||
|
// besser: model::emit dataChanged
|
||||||
|
// also: emit dataChanged(index, index, {Qt::DisplayRole, Qt::EditRole, ValueRole});
|
||||||
|
//connect( &_dataManager, &BCMainWindow::valueTouched, _delegate, &BCItemDelegate::onHighlightRow );
|
||||||
|
connect( _connectButton, &QPushButton::clicked, &_transmitter, &BCTransmitter::onToggleConnectionState );
|
||||||
|
connect( _syncButton, &QPushButton::clicked, this, &BCMainWindow::onSyncFromDevice );
|
||||||
// die Daten des eBikes laden
|
// die Daten des eBikes laden
|
||||||
_dataManager.loadXmlBikeData(":/bikeinfo.xml"_L1);
|
_dataManager.loadXmlBikeData(":/bikeinfo.xml"_L1);
|
||||||
|
|
||||||
|
_transmitter.moveToThread(&_worker);
|
||||||
|
|
||||||
|
connect(this, &BCMainWindow::sendValueCommand, &_transmitter, &BCTransmitter::enqueueValueOp);
|
||||||
|
|
||||||
|
// B) Ergebnisse empfangen (Runner -> Manager)
|
||||||
|
//connect(&_transmitter, &BCTransmitter::commandFinished, this, &BCDataManager::onCommandFinished);
|
||||||
|
//connect(&_transmitter, &BCTransmitter::messageLogged, this, &BCDataManager::onRunnerMessage);
|
||||||
|
|
||||||
|
// C) Aufräumen: Wenn Thread endet, lösche den Runner
|
||||||
|
connect(&_worker, &QThread::finished, &_transmitter, &QObject::deleteLater);
|
||||||
|
|
||||||
|
// 5. Thread starten
|
||||||
|
_worker.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -168,6 +185,7 @@ void BCMainWindow::onValueListReady( BCDevice::ID deviceID )
|
|||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
void BCMainWindow::onShowDevicePanel( BCDevice::ID deviceID )
|
void BCMainWindow::onShowDevicePanel( BCDevice::ID deviceID )
|
||||||
{
|
{
|
||||||
qDebug() << " --- onShowDevicePanel:" << deviceID;
|
qDebug() << " --- onShowDevicePanel:" << deviceID;
|
||||||
@@ -199,3 +217,53 @@ void BCMainWindow::onConnectButtonToggled(bool checked )
|
|||||||
{
|
{
|
||||||
//_dataManager.setDriverConnectionState( checked );
|
//_dataManager.setDriverConnectionState( checked );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void BCMainWindow::onCommandFinished(int id, bool success)
|
||||||
|
{
|
||||||
|
qDebug() << "[Manager] Command" << id << "finished. Success:" << success;
|
||||||
|
}
|
||||||
|
|
||||||
|
void BCMainWindow::onRunnerMessage(const QString &msg)
|
||||||
|
{
|
||||||
|
qDebug() << "[Worker says]:" << msg;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
void BCMainWindow::onSyncFromDevice()
|
||||||
|
{
|
||||||
|
qDebug() << " ---Syncing";
|
||||||
|
/*
|
||||||
|
if( _currentDeviceID != BCDevice::ID::Invalid )
|
||||||
|
{
|
||||||
|
if( _valueModels.contains(_currentDeviceID) )
|
||||||
|
{
|
||||||
|
|
||||||
|
BCValueModel* model = _valueModels[_currentDeviceID];
|
||||||
|
BCValueList& currentList = model->getValueList();
|
||||||
|
|
||||||
|
//BCDataValue& value = currentList[4];
|
||||||
|
|
||||||
|
for( const BCDataValue& value : currentList )
|
||||||
|
{
|
||||||
|
qDebug() << " --- value: " << value.label;
|
||||||
|
|
||||||
|
// statt '_transmitter.enqueueValueCommand( value )' entkoppeln
|
||||||
|
// wir das eleganter über emit sendValueCommand()
|
||||||
|
|
||||||
|
//_transmitter.enqueueValueCommand( value );
|
||||||
|
emit sendValueCommand( BC::OpID::ReadValue, &value);
|
||||||
|
|
||||||
|
emit valueTouched( value.rowInModel );
|
||||||
|
|
||||||
|
bc::processEventsFor(500);
|
||||||
|
|
||||||
|
//QApplication::processEvents();
|
||||||
|
// Thread schlafen lassen (Simulation einer blockierenden Operation)
|
||||||
|
//QThread::msleep(500);
|
||||||
|
|
||||||
|
}
|
||||||
|
} // if contains
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
}
|
||||||
|
|||||||
@@ -47,7 +47,7 @@ class BCMainWindow : public QMainWindow, public Ui_BCMainWindow
|
|||||||
public:
|
public:
|
||||||
|
|
||||||
BCMainWindow(QWidget *parent = nullptr);
|
BCMainWindow(QWidget *parent = nullptr);
|
||||||
~BCMainWindow();
|
virtual ~BCMainWindow();
|
||||||
|
|
||||||
public slots:
|
public slots:
|
||||||
|
|
||||||
@@ -57,9 +57,21 @@ public slots:
|
|||||||
void onActionButtonToggled( bool checked);
|
void onActionButtonToggled( bool checked);
|
||||||
void onConnectButtonToggled(bool active );
|
void onConnectButtonToggled(bool active );
|
||||||
|
|
||||||
|
// Slots für Rückmeldungen vom Runner
|
||||||
|
void onCommandFinished(int id, bool success);
|
||||||
|
void onRunnerMessage(const QString &msg);
|
||||||
|
void onSyncFromDevice();
|
||||||
|
|
||||||
|
signals:
|
||||||
|
|
||||||
|
// Internes Signal, um Daten an den Worker Thread zu senden
|
||||||
|
void sendValueCommand( BC::OpID, const BCDataValue* cmd);
|
||||||
|
//void valuedTouched(const BCDataValue& cmd);
|
||||||
|
void valueTouched(int rowInModel );
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|
||||||
void initData();
|
void initMainWindow();
|
||||||
|
|
||||||
BCDataManager _dataManager;
|
BCDataManager _dataManager;
|
||||||
|
|
||||||
@@ -70,6 +82,8 @@ protected:
|
|||||||
BCDevicePanels _devicePanels;
|
BCDevicePanels _devicePanels;
|
||||||
BCDevicePanel* _currentPanel{};
|
BCDevicePanel* _currentPanel{};
|
||||||
|
|
||||||
|
QThread _worker;
|
||||||
|
BCTransmitter _transmitter;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // BCMAINWINDOW_H
|
#endif // BCMAINWINDOW_H
|
||||||
|
|||||||
@@ -128,10 +128,24 @@ void BCTransmitter::processValueOp( BC::OpID opID )
|
|||||||
} // Mutex wird hier freigegeben! WICHTIG: Execute ohne Lock!
|
} // Mutex wird hier freigegeben! WICHTIG: Execute ohne Lock!
|
||||||
|
|
||||||
|
|
||||||
if( opID == BC::OpID::ReadValue )
|
try
|
||||||
currentValue->readRawValueX( *this );
|
{
|
||||||
else if( opID == BC::OpID::WriteValue )
|
if( opID == BC::OpID::ReadValue )
|
||||||
currentValue->writeRawValueX( *this );
|
{
|
||||||
|
QString result = currentValue->readRawValueX( *this );
|
||||||
|
}
|
||||||
|
|
||||||
|
else if( opID == BC::OpID::WriteValue )
|
||||||
|
{
|
||||||
|
currentValue->writeRawValueX( *this );
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
catch (...)
|
||||||
|
{
|
||||||
|
qDebug() << " --- OUCH!";
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//emit commandFinished(cmd.id, true);
|
//emit commandFinished(cmd.id, true);
|
||||||
|
|||||||
Reference in New Issue
Block a user