From 25e752e83bf66500c2c8ccbf7df499f8bf00288a Mon Sep 17 00:00:00 2001 From: "PANIK\\chris" Date: Sun, 11 Jan 2026 14:48:51 +0100 Subject: [PATCH] Added getter & setter for BCValue --- bcdeviceview.cpp | 13 +++++++ bcmainwindow.cpp | 2 +- bctransmitter.cpp | 12 +++--- bcvalue.cpp | 2 +- bcvalue.h | 40 ++++++++++++++++++- bcvaluedelegate.cpp | 87 ++++++++++++++++++++++++++---------------- bcvaluedelegate.h | 13 +++---- bcvalueeditor.cpp | 11 ++++-- bcvaluemodel.cpp | 11 ++++-- bcxmlloader.cpp | 2 +- resources/bikeinfo.xml | 5 +-- 11 files changed, 139 insertions(+), 59 deletions(-) diff --git a/bcdeviceview.cpp b/bcdeviceview.cpp index 10cfbca..9ac4bdf 100644 --- a/bcdeviceview.cpp +++ b/bcdeviceview.cpp @@ -46,6 +46,19 @@ BCDeviceView::BCDeviceView(QWidget *parent) // __fix! ziemlich wildes ge-pointere, hier _itemDelegate = new BCValueDelegate( _valueModel.getValueList(), this); setItemDelegateForColumn( 1, _itemDelegate ); + /* + //#e0e0e0 + setStyleSheet( R"(QTableView::item:selected + { + background-color: green; + border: none; + } + QTableView:focus + { + outline: none; + } + })"); + */ } diff --git a/bcmainwindow.cpp b/bcmainwindow.cpp index b716453..e659433 100644 --- a/bcmainwindow.cpp +++ b/bcmainwindow.cpp @@ -342,7 +342,7 @@ void BCMainWindow::onSyncDeviceView() for( const BCValuePtr& value : currentList ) { // wir setzen auf 'lesen' - value->valueFlags.setFlag( BCValue::Flag::ReadMe ); + value->getValueFlags().setFlag( BCValue::Flag::ReadMe ); // statt '_transmitter.onUpdateValue( value )' müssen wir hier // über emit requestValueUpdate() zur Thread sysnchronisation diff --git a/bctransmitter.cpp b/bctransmitter.cpp index 033ea86..e2b92a7 100644 --- a/bctransmitter.cpp +++ b/bctransmitter.cpp @@ -144,21 +144,21 @@ void BCTransmitter::onUpdateValue( BCValuePtrConst valuePtr) // Kosmetik const BCValue& value = *(valuePtr.get()); - uint32_t devID = static_cast(value.deviceID); - uint8_t regID = static_cast (value.registerID); + uint32_t devID = static_cast(value.getDeviceID()); + uint8_t regID = static_cast (value.getRegisterID()); // Für den Fehlerfall: Wir senden den alten Wert einfach zurück - uint32_t newValue = value.rawValue; + uint32_t newValue = value.getRawValue(); BCValue::Flag newState = BCValue::Flag::Failed; - if(value.valueFlags.testFlag( BCValue::Flag::WriteMe ) ) + if(value.getValueFlags().testFlag( BCValue::Flag::WriteMe ) ) { } // oder sollen wir hier beides erlauben ? readFlag & writeFlag ? // Was kommt dann zuerst? Schreiben und lesen als verify ? - else if( value.valueFlags.testFlag( BCValue::Flag::ReadMe ) ) + else if( value.getValueFlags().testFlag( BCValue::Flag::ReadMe ) ) { // wir sind hier im anderen thread! nicht einfach so reinschreiben, nur lesen TransmitResult result = value.isWord() ? readWordValue( devID, regID ) : readByteValue( devID, regID ); @@ -170,7 +170,7 @@ void BCTransmitter::onUpdateValue( BCValuePtrConst valuePtr) } } - emit valueUpdated( value.deviceID, value.indexRow, newState, newValue ); + emit valueUpdated( value.getDeviceID(), value.getIndexRow(), newState, newValue ); } /** diff --git a/bcvalue.cpp b/bcvalue.cpp index fc2f361..a8ea24d 100644 --- a/bcvalue.cpp +++ b/bcvalue.cpp @@ -64,7 +64,7 @@ bool BCValue::isReadOnly() const } -double BCValue::calcRatio() const +double BCValue::calcMinMaxRatio() const { double ratio = 0; diff --git a/bcvalue.h b/bcvalue.h index c5ffafe..33dad9a 100644 --- a/bcvalue.h +++ b/bcvalue.h @@ -67,6 +67,8 @@ class BCValue { Q_GADGET + friend class BCXmlLoader; + public: // Aus dem Type ergibt sich @@ -97,11 +99,46 @@ public: BCValue( BCDevice::ID deviceID_, BC::ID registerID_ ); QString formatValue() const; - double calcRatio() const; + double calcMinMaxRatio() const; void dumpValue() const; bool isWord() const; bool isReadOnly() const; + Flags& getValueFlags() const noexcept { return valueFlags; } + void setValueFlags(Flags newFlags) { valueFlags = newFlags; } + + BCDevice::ID getDeviceID() const noexcept { return deviceID; } + //void setDeviceID(BCDevice::ID newDeviceID) { deviceID = newDeviceID; } + + BC::ID getRegisterID() const noexcept { return registerID; } + void setRegisterID(BC::ID newRegisterID) { registerID = newRegisterID; } + + ValueType getValueType() const noexcept { return valueType; } + void setValueType(ValueType newValueType) { valueType = newValueType; } + + int getIndexRow() const noexcept { return indexRow; } + void setIndexRow(int newIndexRow) { indexRow = newIndexRow; } + + QString getLabel() const { return label; } + void setLabel(const QString &newLabel) { label = newLabel; } + + uint32_t getRawValue() const noexcept { return rawValue; } + void setRawValue(uint32_t newRawValue) const { rawValue = newRawValue; } + + QString getUnitLabel() const { return unitLabel; } + void setUnitLabel(const QString &newUnitLabel) { unitLabel = newUnitLabel; } + + double getFactor() const noexcept { return factor; } + void setFactor(double newFactor) { factor = newFactor; } + + const OptDouble getOptMin() const { return optMin; } + void setOptMin(const OptDouble &newOptMin) { optMin = newOptMin; } + + const OptDouble getOptMax() const { return optMax; } + void setOptMax(const OptDouble &newOptMax) { optMax = newOptMax; } + +protected: + mutable Flags valueFlags{BCValue::Flag::NoFlag}; BCDevice::ID deviceID{BCDevice::ID::Invalid}; BC::ID registerID{BC::ID::Invalid}; @@ -113,6 +150,7 @@ public: double factor{1}; OptDouble optMin; OptDouble optMax; + }; Q_DECLARE_OPERATORS_FOR_FLAGS(BCValue::Flags) diff --git a/bcvaluedelegate.cpp b/bcvaluedelegate.cpp index 77d12d3..6097abb 100644 --- a/bcvaluedelegate.cpp +++ b/bcvaluedelegate.cpp @@ -59,7 +59,7 @@ QWidget* BCValueDelegate::createEditor(QWidget *parent, const QStyleOptionViewIt { const BCValue& bcValue = *(_valueList[ index.row()].get()); - qDebug() << " --- Create EDITOR: " << index.row(); + qDebug() << " --- Create EDITOR: " << index.row() << " parent: " << parent->objectName(); Q_UNUSED(option) Q_UNUSED(index) @@ -121,45 +121,51 @@ void BCValueDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, c QStyledItemDelegate::setModelData(editor, model, index); } -void BCValueDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex& index) const -{ - - Q_UNUSED(index) - - QRect sliderRect = option.rect.adjusted( - option.rect.width() - 125, // Von rechts: 115px (Breite der Progress Bar) - 0, // Oben: kein Offset - -8, // Rechts: 8px Padding - 0 // Unten: kein Offset - ); - editor->setGeometry(sliderRect); // Slider nur über Progress Bar - -} - +/* QSize BCValueDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex& index) const { return QStyledItemDelegate::sizeHint(option,index); - /* + QStyleOptionViewItem opt = option; initStyleOption(&opt, index); opt.text = formatDisplayString(index); QStyle *style = opt.widget ? opt.widget->style() : QApplication::style(); return style->sizeFromContents(QStyle::CT_ItemViewItem, &opt, QSize(), opt.widget); - */ -} +} +*/ + +/* +// Cpp +void BCValueDelegate::destroyEditor(QWidget *editor, const QModelIndex &index) const +{ + // 1. Kontext sichern (bevor der Editor gelöscht wird) + // Der Editor ist meist ein Kind des Viewports + QWidget *viewport = editor->parentWidget(); + QRect rect = editor->geometry(); + + // 2. Basis-Implementierung aufrufen + // WICHTIG: Das löscht (delete) den Editor-Pointer! + QStyledItemDelegate::destroyEditor(editor, index); + + // 3. Jetzt den Bereich neu zeichnen ("Dirty Rect") + if (viewport) + { + // Wir nutzen das Rechteck, wo der Editor WAR. + qDebug() << " --- DESTROY: " << viewport->objectName() << " : " << rect; + viewport->update(rect); + } +} +*/ void BCValueDelegate::paint(QPainter *painter, const QStyleOptionViewItem& option, const QModelIndex& index) const { - qDebug() << " ---paint:" << index.row(); - // Standard-Zeichnen (Text, Hintergrund, Selection) durchführen QStyledItemDelegate::paint(painter, option, index); - int row = index.row(); - if( index.column() == 1 ) + if( index.column() == 1 ) { if( row>-1 && row <= _valueList.size() ) { @@ -174,9 +180,6 @@ void BCValueDelegate::paint(QPainter *painter, const QStyleOptionViewItem& optio } - - - void BCValueDelegate::paintHighlightRow(QPainter* painter, const QStyleOptionViewItem& option, int row) const { painter->save(); @@ -205,8 +208,23 @@ void BCValueDelegate::paintHighlightRow(QPainter* painter, const QStyleOptionVie } +void BCValueDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex& index) const +{ + + Q_UNUSED(index) + + QRect sliderRect = option.rect.adjusted( + option.rect.width() - cTextBlockOffset, // Von rechts: cTextBlockOffset (==130) px (Breite der Progress Bar) + 0, // Oben: kein Offset + -cPaddingRight, // Rechts: 8px Padding + 0 // Unten: kein Offset + ); + editor->setGeometry(sliderRect); // Slider nur über Progress Bar + +} + /** - * @brief Zeichnet eine passiven Slider, um den Wertebereich des übergebenen BCValue anzuzeigen. + * @brief Zeichnet eine passiven Slider, um den möglichen Wertebereich des übergebenen BCValue anzuzeigen. */ void BCValueDelegate::paintSliderIndicator(QPainter* painter, const QStyleOptionViewItem& option, const BCValue& bcValue) const @@ -215,17 +233,22 @@ void BCValueDelegate::paintSliderIndicator(QPainter* painter, const QStyleOption painter->save(); painter->setRenderHint(QPainter::Antialiasing); - QRect barRect = option.rect.adjusted(option.rect.width() - 130, + int newX = option.rect.width() - cTextBlockOffset; + + QRect barRect = option.rect.adjusted( newX, option.rect.height() / 2 + 1, - -8, + - option.rect.width() + cTextBlockOffset + 117 - 18, -option.rect.height() / 2 - 3); - double ratio = bcValue.calcRatio(); + + double ratio = bcValue.calcMinMaxRatio(); if( ratio) { - // Mini Progress Bar + // Mini Progress Bar: der Gesamtbereich painter->setPen(Qt::NoPen); painter->setBrush(QColor(0xE0E0E0)); + //QColor disabledText = option.palette.color(QPalette::Disabled, QPalette::Text); + //painter->setBrush(disabledText); painter->drawRoundedRect(barRect, 2, 2); QRect fillRect = barRect; @@ -256,7 +279,7 @@ void BCValueDelegate::paintSliderIndicator(QPainter* painter, const QStyleOption /* // baby-Slider-Indikator zeichnen // Anteil zwischen min und max berechnen - double ratio = bcValue.calcRatio(); + double ratio = bcValue.calcMinMaxRatio(); if( !ratio) return; diff --git a/bcvaluedelegate.h b/bcvaluedelegate.h index 4f8b45e..af5e60b 100644 --- a/bcvaluedelegate.h +++ b/bcvaluedelegate.h @@ -57,8 +57,8 @@ public: void setEditorData(QWidget *editor, const QModelIndex& index) const override; void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex& index) const override; void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex& index) const override; - - QSize sizeHint(const QStyleOptionViewItem &option,const QModelIndex& index) const override; + //void destroyEditor(QWidget *editor, const QModelIndex &index) const override; + //QSize sizeHint(const QStyleOptionViewItem &option,const QModelIndex& index) const override; void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex& index) const override; void clearAllHighlights(); @@ -68,10 +68,6 @@ public slots: void onHighlightRow(int row); -signals: - - //void viewUpdateNeeded(); - protected: void updateRow(int row); @@ -87,10 +83,13 @@ protected: QPropertyAnimation* _animation{}; - QHash _rowOpacities; QHash _rowAnimations; + static constexpr int cTextBlockOffset = 130; + static constexpr int cPaddingRight = 8; + static constexpr int cSliderWidth = 117; + }; diff --git a/bcvalueeditor.cpp b/bcvalueeditor.cpp index def2f3b..b7920b8 100644 --- a/bcvalueeditor.cpp +++ b/bcvalueeditor.cpp @@ -1,7 +1,7 @@ #include #include - +#include BCValueEditor::BCValueEditor(const BCValue& bcValue, QWidget *parent ) @@ -9,12 +9,18 @@ BCValueEditor::BCValueEditor(const BCValue& bcValue, QWidget *parent ) { setupUi(this); + // wir wollen ja modern sein _slider->setStyle(new BCSliderStyle()); setAutoFillBackground(true); + QSizePolicy sp = _commitButton->sizePolicy(); sp.setRetainSizeWhenHidden(true); // <--- Das ist der magische Schalter _commitButton->setSizePolicy(sp); + double ratio = bcValue.calcMinMaxRatio(); + _slider->setRange(0, 100); + _slider->setValue( bcValue.rawValue * ratio); + /* _slider = new QSlider(Qt::Horizontal, this); _slider->setRange(0, 100); @@ -53,13 +59,12 @@ BCValueEditor::BCValueEditor(const BCValue& bcValue, QWidget *parent ) int BCValueEditor::getValue() const { - qDebug() << " -- jajaja: "<< size(); return _slider->value(); } void BCValueEditor::setValue(int val) { - // WICHTIG: Block Signals verhindern Endlosschleifen, falls das Model + // Block Signals verhindern Endlosschleifen, falls das Model // das Widget während des Updates neu setzt (passiert manchmal bei Live-Updates). if (val != _slider->value()) { diff --git a/bcvaluemodel.cpp b/bcvaluemodel.cpp index 4ed48a9..62613eb 100644 --- a/bcvaluemodel.cpp +++ b/bcvaluemodel.cpp @@ -90,7 +90,7 @@ void BCValueModel::updateValue(int row, BCValue::Flags newState, uint32_t rawVal // Obacht hier! //value.valueFlags = state; - value.rawValue = rawValue; + value.setRawValue( rawValue ); QModelIndex idx = index(row,1); @@ -148,12 +148,12 @@ QVariant BCValueModel::data(const QModelIndex& index, int role) const const BCValue& value = *(_valueList.at( row ).get()); if( col == 0 ) - return value.label; + return value.getLabel(); if( col == 1) { if( role == Qt::DisplayRole ) - return QString("%1 %2").arg( value.formatValue(), value.unitLabel); + return QString("%1 %2").arg( value.formatValue(), value.getUnitLabel()); return value.formatValue(); } @@ -185,6 +185,8 @@ bool BCValueModel::setData(const QModelIndex& index, const QVariant& variant, in { BCValuePtr value = _valueList[index.row()]; + qDebug() << "--- YES! " << variant.toInt(); + // Wir erwarten hier nur den Value-Teil (vom Slider/Editor) // Checken ob Int oder Double if (variant.canConvert()) @@ -195,7 +197,8 @@ bool BCValueModel::setData(const QModelIndex& index, const QVariant& variant, in qDebug() << "--- YES! " << variant.toInt(); //emit makeSimonHappy(); } - value->rawValue = variant.toInt(); + // QUARK! + value->setRawValue( variant.toInt() ); } emit dataChanged(index, index, {Qt::DisplayRole, Qt::EditRole}); diff --git a/bcxmlloader.cpp b/bcxmlloader.cpp index 7f39db6..4d5c45c 100644 --- a/bcxmlloader.cpp +++ b/bcxmlloader.cpp @@ -149,7 +149,7 @@ void BCXmlLoader::loadXmlBikeDeviceData(BCDevice::ID deviceID) if(newValue) { // wir merken uns gleich den index in der Werteliste - (*newValue)->indexRow = currentValues.size(); + (*newValue)->setIndexRow( currentValues.size() ); currentValues.push_back( *newValue ); } diff --git a/resources/bikeinfo.xml b/resources/bikeinfo.xml index 86cbb10..e2631ef 100644 --- a/resources/bikeinfo.xml +++ b/resources/bikeinfo.xml @@ -2,7 +2,6 @@ - @@ -25,8 +24,8 @@ - - + +