Try BCValue as smartptr

This commit is contained in:
2026-01-02 01:43:49 +01:00
parent 43f72d1be6
commit fc5ab611bd
11 changed files with 60 additions and 69 deletions

View File

@@ -44,9 +44,6 @@
#include <QPainter>
#include "bcanimateddelegate.h"
#include "bcvalue.h"
BCAnimatedDelegate::BCAnimatedDelegate(const BCValueList& valueList, QTableView* view)
@@ -59,9 +56,9 @@ BCAnimatedDelegate::BCAnimatedDelegate(const BCValueList& valueList, QTableView*
QString BCAnimatedDelegate::displayText(const QVariant& dataValue, const QLocale& locale) const
{
// Wir prüfen, ob im Variant unser Struct steckt
if (dataValue.canConvert<const BCValue*>())
if (dataValue.canConvert<const BCValue&>())
{
const BCValue& bc = *dataValue.value<const BCValue*>();
const BCValue& bc = dataValue.value<const BCValue/>();
//qDebug() << " --- YES: " << bc.label;
// Hier bauen wir den String zusammen, den man sieht,
// wenn KEIN Editor offen ist.

View File

@@ -35,10 +35,12 @@
#include <QStyledItemDelegate>
#include <bcvalue.h>
class QPropertyAnimation;
class QVariantAnimation;
class QTableView;
class BCValueList;
class BCAnimatedDelegate : public QStyledItemDelegate
{

View File

@@ -250,13 +250,13 @@ void BCMainWindow::onSyncFromDevice()
// alle einzeln? echt jetzt?
for( const BCValue& value : currentList )
for( const BCValuePtr &value : currentList )
{
qDebug() << " --- value: " << value.label;
qDebug() << " --- begin sync of value: " << value->label;
// wir setze
// statt '_transmitter.enqueueValueCommand( value )' entkoppeln
// wir das eleganter über emit requestValueUpdate()
emit requestValueUpdate( BCValue::OpID::ReadValue, &value);
//emit requestValueUpdate( value);
}

View File

@@ -64,9 +64,7 @@ public slots:
signals:
// Internes Signal, um Daten an den Worker Thread zu senden
void requestValueUpdate( BCValue::OpID, const BCValue* cmd);
//void valuedTouched(const BCValue& cmd);
void valueTouched(int indexRow );
void requestValueUpdate( const BCValue& value);
protected:

View File

@@ -67,8 +67,9 @@ void BCTransmitter::onToggleConnectionState( bool connect )
}
void BCTransmitter::enqueueValueOp(BCValue::OpID opID, const BCValue* value)
void BCTransmitter::enqueueValueOp( const BCValue& value)
{
// Hier sind wir noch in GUI Thread
QMutexLocker locker(&_mutex);
_valueQueue.enqueue( value );
@@ -81,13 +82,13 @@ void BCTransmitter::enqueueValueOp(BCValue::OpID opID, const BCValue* value)
// damit enqueueValueOp sofort zurückkehrt (non-blocking für den Aufrufer).
//QMetaObject::invokeMethod(this, "processValueOp", Qt::QueuedConnection);
QMetaObject::invokeMethod(this, [this, opID]()
QMetaObject::invokeMethod(this, [this]()
{
processValueOp(opID);
processValueOp();
}, Qt::QueuedConnection );
}
void BCTransmitter::processValueOp( BCValue::OpID opID )
void BCTransmitter::processValueOp()
{
if (_isBusy)
@@ -97,7 +98,7 @@ void BCTransmitter::processValueOp( BCValue::OpID opID )
while (true)
{
const BCValue* currentValue{};
BCValue* currentValue{};
{
QMutexLocker locker(&_mutex);
if (_valueQueue.isEmpty())
@@ -105,23 +106,25 @@ void BCTransmitter::processValueOp( BCValue::OpID opID )
_isBusy = false;
break; // Schleife verlassen, warten auf neue Events
}
currentValue =_valueQueue.dequeue();
//currentValue =_valueQueue.dequeue();
} // Mutex wird hier freigegeben! WICHTIG: Execute ohne Lock!
/*
// Abkürzung
const BCValue& val = *currentValue;
const BCValue& value = *currentValue;
// Value ist 'under construction'
//emit valueUpdated( val.deviceID, val.indexRow, BCValue::State::Locked );
if( opID == BCValue::OpID::ReadValue )
{
QString result = currentValue->readRawValueX( *this );
emit valueUpdated( val.deviceID, val.indexRow, BCValue::State::InSync, result );
emit valueUpdated( valuel.deviceID, value.indexRow, BCValue::State::InSync, result );
}
else if( opID == BCValue::OpID::WriteValue )
{
currentValue->writeRawValueX( *this );
}
*/
// __fix
bc::processEventsFor(50);

View File

@@ -64,8 +64,8 @@ public:
public slots:
void onToggleConnectionState( bool connect );
void enqueueValueOp(BCValue::OpID opID, const BCValue* value );
void processValueOp(BCValue::OpID opID);
void enqueueValueOp(const BCValue& value );
void processValueOp();
signals:
@@ -74,7 +74,7 @@ signals:
private:
using BCDataQueue = QQueue<const BCValue*>;
using BCDataQueue = QQueue<BCValue>;
BCDataQueue _valueQueue;
QMutex _mutex;

View File

@@ -78,20 +78,16 @@ class BCValue
public:
enum class OpID : uint8_t
{
ReadValue,
WriteValue,
};
enum class State : uint8_t
{
NoState = 0x0,
ReadOnly = 0x1,
Locked = 0x2,
Failed = 0x4,
InSync = 0x8,
OK = 0x10
NoState = 0x00,
ReadMe = 0x01,
WriteMe = 0x02,
ReadOnly = 0x04,
Locked = 0x08,
Failed = 0x10,
InSync = 0x20,
OK = 0x40
};
Q_DECLARE_FLAGS(States, State )
@@ -109,13 +105,18 @@ public:
mutable QString visibleValue;
};
Q_DECLARE_OPERATORS_FOR_FLAGS(BCValue::States)
Q_DECLARE_METATYPE(const BCValue*)
Q_DECLARE_METATYPE(const BCValue&)
//Q_DECLARE_METATYPE(const BCValue&)
using BCValuePtr = std::shared_ptr<BCValue>;
//using BCValueList = QList<BCValue>;
using BCValueList = QList<BCValuePtr>;
/*
class BCValueList : public QList<BCValue>
{
@@ -160,6 +161,7 @@ public:
}
};
*/
Q_DECLARE_METATYPE(BCValueList)

View File

@@ -45,18 +45,6 @@ BCValueModel::BCValueModel(QObject *parent)
}
/**
* @brief Einen Einzelwert hinzufügen
* @param val der neue Wert
*/
void BCValueModel::addValue(const BCValue& val)
{
int row = _valueList.size();
beginInsertRows(QModelIndex(), row, row);
_valueList.append(val);
endInsertRows();
}
/**
@@ -98,12 +86,12 @@ void BCValueModel::onValueUpdated( int row, BCValue::State state, const QString&
qDebug() << " BCValueModel::onValueUpdated update: " << newVisisbleValue;
if( row > -1 && row < _valueList.size() )
{
const BCValue& value = _valueList[row];
BCValuePtr value = _valueList[row];
QModelIndex idx = index(row,1);
value.state = state;
if( !newVisisbleValue.isEmpty() && newVisisbleValue != value.visibleValue )
value->state = state;
if( !newVisisbleValue.isEmpty() && newVisisbleValue != value->visibleValue )
{
value.visibleValue = newVisisbleValue;
value->visibleValue = newVisisbleValue;
}
// wir schicken auf jeden fall einen update request
emit dataChanged(idx, idx, {Qt::DisplayRole, Qt::EditRole});
@@ -156,17 +144,17 @@ QVariant BCValueModel::data(const QModelIndex& index, int role) const
if (wrongRole || !index.isValid() || row >= _valueList.size() )
return QVariant();
const BCValue& value = _valueList.at( row );
BCValuePtr value = _valueList.at( row );
if( col == 0 )
return value.label;
return value->label;
if( col == 1)
{
if( role == Qt::DisplayRole )
return QString("%1 %2").arg( value.visibleValue, value.valueType->unitLabel);
return QString("%1 %2").arg( value->visibleValue, value->valueType->unitLabel);
return value.visibleValue;
return value->visibleValue;
}
return QVariant();
@@ -208,13 +196,13 @@ bool BCValueModel::setData(const QModelIndex& index, const QVariant& value, int
if (index.isValid() && role == Qt::EditRole)
{
BCValue& item = _valueList[index.row()];
BCValuePtr item = _valueList[index.row()];
// Wir erwarten hier nur den Value-Teil (vom Slider/Editor)
// Checken ob Int oder Double
if (value.canConvert<double>())
{
item.visibleValue = value.toString();
item->visibleValue = value.toString();
}
_valueList[index.row()] = item;

View File

@@ -52,7 +52,6 @@ public:
explicit BCValueModel(QObject *parent = nullptr);
void addValue(const BCValue& val);
void takeValueList(BCValueList& valueList);
const BCValueList& getValueList() const;

View File

@@ -176,11 +176,11 @@ void BCXmlLoader::loadXmlBikeDeviceData(BCDevice::ID deviceID)
};
// nur gültige Werte sind vorhanden und können gespeichert werden
std::optional<BCValue> newValue = makeDataValue( deviceID, params );
std::optional<BCValuePtr> newValue = makeDataValue( deviceID, params );
if(newValue)
{
// wir merken uns gleich den index in der Werteliste
(*newValue).indexRow = currentValues.size();
(*newValue)->indexRow = currentValues.size();
currentValues.push_back( *newValue );
}
@@ -195,7 +195,7 @@ void BCXmlLoader::loadXmlBikeDeviceData(BCDevice::ID deviceID)
}
std::optional<BCValue> BCXmlLoader::makeDataValue( BCDevice::ID deviceID, const BCDataParams& params )
std::optional<BCValuePtr> BCXmlLoader::makeDataValue( BCDevice::ID deviceID, const BCDataParams& params )
{
/*
@@ -221,7 +221,7 @@ std::optional<BCValue> BCXmlLoader::makeDataValue( BCDevice::ID deviceID, const
*/
std::optional<BCValue> newValue;
bool ok;
//std::optional<quint64> IDVal = s_bcValueEnum.keyToValue64( params.ID.toLatin1().constData() );
@@ -233,7 +233,7 @@ std::optional<BCValue> BCXmlLoader::makeDataValue( BCDevice::ID deviceID, const
auto valueType = BCValueType::fetchValueType(params.UnitType);
if( valueType.has_value() )
{
newValue = BCValue( *valueType, deviceID, static_cast<BC::ID>(IDVal) );
BCValuePtr newValue = std::make_shared<BCValue>( *valueType, deviceID, static_cast<BC::ID>(IDVal) );
/*
setIfExists( params.Factor, newValue.factor );
@@ -242,10 +242,12 @@ std::optional<BCValue> BCXmlLoader::makeDataValue( BCDevice::ID deviceID, const
*/
newValue->label = params.Label;
qDebug() << " --- created: " << params.Label;
return std::optional<BCValuePtr>( newValue );
}
}
return newValue;
return std::nullopt;
}
// --- NEU: Speichern mit QXmlStreamWriter ---

View File

@@ -73,7 +73,7 @@ protected:
void loadXmlBikeDeviceData( BCDevice::ID deviceID );
std::optional<BCValue> makeDataValue( BCDevice::ID deviceID, const BCDataParams& params );
std::optional<BCValuePtr> makeDataValue( BCDevice::ID deviceID, const BCDataParams& params );
QXmlStreamReader _xml;