diff --git a/bcsliderstyle.cpp b/bcsliderstyle.cpp index dff41bf..a338b40 100644 --- a/bcsliderstyle.cpp +++ b/bcsliderstyle.cpp @@ -31,6 +31,8 @@ #include +#include + BCSliderStyle::BCSliderStyle() : QProxyStyle() @@ -71,9 +73,11 @@ QRect BCSliderStyle::subControlRect(ComplexControl cc, const QStyleOptionComplex QRect rect = slider->rect; int handleSize = 16; - if (sc == SC_SliderHandle) { + if (sc == SC_SliderHandle) + { // Handle Position korrekt berechnen - if (slider->orientation == Qt::Horizontal) { + if (slider->orientation == Qt::Horizontal) + { int range = slider->maximum - slider->minimum; int pos = slider->sliderPosition - slider->minimum; int pixelRange = rect.width() - handleSize; @@ -82,7 +86,9 @@ QRect BCSliderStyle::subControlRect(ComplexControl cc, const QStyleOptionComplex return QRect(rect.x() + pixelPos, rect.center().y() - handleSize / 2, handleSize, handleSize); - } else { + } + else + { int range = slider->maximum - slider->minimum; int pos = slider->sliderPosition - slider->minimum; int pixelRange = rect.height() - handleSize; @@ -125,7 +131,13 @@ void BCSliderStyle::drawHorizontalFluentSlider(QPainter* painter, const QStyleOp const QColor& bgColor) const { QRect groove = slider->rect; + QRect handle = subControlRect(CC_Slider, slider, SC_SliderHandle, nullptr); + qDebug() << " ---WTF: " << groove; + + + BCValueEditor::paintSliderIndicator(painter, groove, 0.5 ); +/* int grooveHeight = 4; // Track sollte im Widget-Zentrum sein, nicht im groove-Zentrum int grooveY = groove.center().y() - grooveHeight / 2; @@ -143,6 +155,10 @@ void BCSliderStyle::drawHorizontalFluentSlider(QPainter* painter, const QStyleOp QRect activeTrack(groove.left(), grooveY, activeWidth, grooveHeight); painter->setBrush(activeColor); painter->drawRoundedRect(activeTrack, grooveHeight / 2, grooveHeight / 2); + */ + + + // Handle (Thumb) - Fluent style is more subtle int handleSize = 16; @@ -160,6 +176,8 @@ void BCSliderStyle::drawHorizontalFluentSlider(QPainter* painter, const QStyleOp handle.center().y() - glowSize / 2, glowSize, glowSize); painter->drawEllipse(glow); + + } // Thumb @@ -179,25 +197,3 @@ void BCSliderStyle::drawHorizontalFluentSlider(QPainter* painter, const QStyleOp } } -void BCSliderStyle::drawSliderIndicator( QPainter* painter, QRect& groove ) -{ - /* - int grooveHeight = 4; - // Track sollte im Widget-Zentrum sein, nicht im groove-Zentrum - int grooveY = groove.center().y() - grooveHeight / 2; - - // Full background track - QRect fullTrack(groove.left(), grooveY, groove.width(), grooveHeight); - painter->setPen(Qt::NoPen); - painter->setBrush(inactiveColor.lighter(150)); - painter->drawRoundedRect(fullTrack, grooveHeight / 2, grooveHeight / 2); - - QRect handle = subControlRect(CC_Slider, slider, SC_SliderHandle, nullptr); - - // Active track (filled portion) - int activeWidth = handle.center().x() - groove.left(); - QRect activeTrack(groove.left(), grooveY, activeWidth, grooveHeight); - painter->setBrush(activeColor); - painter->drawRoundedRect(activeTrack, grooveHeight / 2, grooveHeight / 2); - */ -} diff --git a/bcsliderstyle.h b/bcsliderstyle.h index d72d6a5..4903119 100644 --- a/bcsliderstyle.h +++ b/bcsliderstyle.h @@ -76,8 +76,7 @@ public: const QColor& inactiveColor, const QColor& bgColor) const; - - static void drawSliderIndicator( QPainter* painter, QRect& groove ); + static void paintSliderIndicator(QPainter* painter, const QRect& rect, double ratio ); }; diff --git a/bcvalue.cpp b/bcvalue.cpp index c5be90a..ac0ec25 100644 --- a/bcvalue.cpp +++ b/bcvalue.cpp @@ -207,9 +207,9 @@ double BCValue::calcMinMaxRatio() const } -bool BCValue::valuesForSlider( int& value, int& min, int& max ) const +bool BCValue::valuesForSlider(ValueRange& valueRange) const { - value = min = max = 0; + valueRange.value = valueRange.min = valueRange.max = 0; // min & max sind vorraussetzung für den slider if( !_optMin.has_value() || !_optMax.has_value() ) @@ -219,10 +219,10 @@ bool BCValue::valuesForSlider( int& value, int& min, int& max ) const // und max liegt weil wir das schon bei setRawValue // überprüft haben. - value = _rawValue * _factor; - min = _optMin.value(); - max = _optMax.value(); - + valueRange.value = _rawValue * _factor; + valueRange.min = _optMin.value(); + valueRange.max = _optMax.value(); + valueRange.ratio = calcMinMaxRatio(); return true; diff --git a/bcvalue.h b/bcvalue.h index af8fa62..9fc37fe 100644 --- a/bcvalue.h +++ b/bcvalue.h @@ -108,6 +108,14 @@ public: //QMEMBER _PROPERTY(OptDouble MEMBER _optMin) //QMEMBER _PROPERTY(OptDouble MEMBER _optMax) + struct ValueRange + { + int value{0}; + int min{0}; + int max{0}; + double ratio{1}; + }; + BCValue( BCDevice::ID deviceID, BC::ID registerID ); QString formatValue() const; @@ -133,7 +141,7 @@ public: QString label() const; QString unitLabel() const; - bool valuesForSlider( int& value, int& min, int& max ) const; + bool valuesForSlider( ValueRange& valueRange ) const; QString toString() const; diff --git a/bcvaluedelegate.cpp b/bcvaluedelegate.cpp index bdc843f..1608652 100644 --- a/bcvaluedelegate.cpp +++ b/bcvaluedelegate.cpp @@ -62,8 +62,8 @@ QWidget* BCValueDelegate::createEditor(QWidget *parent, const QStyleOptionViewIt const BCValue& bcValue = *(_valueList[ index.row()].get()); - BCValueEditorParams params; - bool hasData = bcValue.valuesForSlider( params.value, params.min, params.max ); + BCValue::ValueRange params; + bool hasData = bcValue.valuesForSlider( params ); if( !hasData ) return nullptr; @@ -123,25 +123,26 @@ void BCValueDelegate::paint(QPainter *painter, const QStyleOptionViewItem& optio QStyledItemDelegate::paint(painter, option, index); int row = index.row(); - if( index.column() == 1 ) + if( index.column() != 1 ) + return; + if( row<0 || row >= _valueList.size() ) + return; + + const BCValue& bcValue = *(_valueList[ index.row()].get()); + if( !bcValue.isReadOnly() ) { - if( row>-1 && row <= _valueList.size() ) + if( bcValue.valueType() == BCValue::ValueType::Bool ) + paintButtonIndicator(painter, option, bcValue); + else { - const BCValue& bcValue = *(_valueList[ index.row()].get()); - if( !bcValue.isReadOnly() ) - { - if( bcValue.valueType() == BCValue::ValueType::Bool ) - paintButtonIndicator(painter,option,bcValue); - else - paintSliderIndicator(painter,option,bcValue); - - } - + qDebug() << " ---WTF1: " <setGeometry(sliderRect); // Slider nur über Progress Bar - } void BCValueDelegate::paintButtonIndicator(QPainter* painter, const QStyleOptionViewItem& option, const BCValue& bcValue) const @@ -193,79 +187,6 @@ void BCValueDelegate::paintButtonIndicator(QPainter* painter, const QStyleOption } -void BCValueDelegate::paintSliderIndicator(QPainter* painter, const QStyleOptionViewItem& option, const BCValue& bcValue) const -{ - double ratio = bcValue.calcMinMaxRatio(); - - int value,min,max; - bcValue.valuesForSlider( value, min, max ); - - qDebug() << " --- paint: " << bcValue.label() << " value: " << value << " min: " << min << " max: " << max << " ratio:" << ratio*100.0 << '%'; - - // Text und kleiner Slider-Indikator zeichnen - painter->save(); - painter->setRenderHint(QPainter::Antialiasing); - - int adjX = option.rect.width() - cTextBlockOffset; - - // Mini Progress Bar: der Gesamtbereich - QRect barRect = option.rect.adjusted( adjX, 12, -10-24, -12 ); - painter->setPen(Qt::NoPen); - painter->setBrush(QColor(0xE0E0E0)); - painter->drawRoundedRect(barRect, 2, 2); - - // Mini Progress Bar: der Wertebereich - barRect.setWidth( ratio * barRect.width() ); - painter->setBrush(QColor(0x0078D4)); - painter->drawRoundedRect(barRect, 2, 2); - - painter->restore(); - -} - - -/** - * @brief Zeichnet eine passiven Slider, um den möglichen Wertebereich des übergebenen BCValue anzuzeigen. - */ - -void BCValueDelegate::paintSliderIndicatorXX(QPainter* painter, const QStyleOptionViewItem& option, const BCValue& bcValue) const -{ - // Text und kleiner Slider-Indikator zeichnen - painter->save(); - painter->setRenderHint(QPainter::Antialiasing); - - int newX = option.rect.width() - cTextBlockOffset; - - QRect barRect = option.rect.adjusted( newX, - option.rect.height() / 2 + 1, - - option.rect.width() + cTextBlockOffset + 117 - 18, - -option.rect.height() / 2 - 3); - - - double ratio = bcValue.calcMinMaxRatio(); - qDebug() << " --- paint: " << bcValue.label() << ":" << ratio; - if(ratio) - { - // 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; - // ein wert darf näturlich nie über 100% eingestellt werden - ratio = qBound(0.0,ratio,1.0); - - fillRect.setWidth(barRect.width() * ratio); - painter->setBrush(QColor(0x0078D4)); - painter->drawRoundedRect(fillRect, 2, 2); - } - - painter->restore(); - -} - /** * @brief Startet die Animation für die übergebene Zeile diff --git a/bcvaluedelegate.h b/bcvaluedelegate.h index d57d990..6402b42 100644 --- a/bcvaluedelegate.h +++ b/bcvaluedelegate.h @@ -69,9 +69,7 @@ protected: void updateRow(int row); void paintHighlightRow(QPainter* painter, const QStyleOptionViewItem& option, int row) const; - void paintSliderIndicator(QPainter* painter, const QStyleOptionViewItem& option, const BCValue& bcValue) const; - void paintSliderIndicatorXX(QPainter* painter, const QStyleOptionViewItem& option, const BCValue& bcValue) const; - + void paintSliderIndicator(QPainter* painter, const QRect &rect, double ratio) const; void paintButtonIndicator(QPainter* painter, const QStyleOptionViewItem& option, const BCValue& bcValue) const; // Das ist ein Quickhack, der Delegate sollte @@ -86,9 +84,6 @@ protected: 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 6a6b81a..5347e63 100644 --- a/bcvalueeditor.cpp +++ b/bcvalueeditor.cpp @@ -1,7 +1,6 @@ #include #include -#include BCValueEditor::BCValueEditor( QWidget *parent ) @@ -38,9 +37,7 @@ int BCValueEditor::value() const return _slider->value(); } - - -void BCValueEditor::setValueAndRange( const BCValueEditorParams& params ) +void BCValueEditor::setValueAndRange( const BCValue::ValueRange& params ) { _slider->setRange( params.min, params.max); // Block Signals verhindern Endlosschleifen, falls das Model @@ -53,3 +50,41 @@ void BCValueEditor::setValueAndRange( const BCValueEditorParams& params ) } } + + +/** + * @brief Zeichnet eine passiven Slider, um den möglichen Wertebereich des übergebenen BCValue anzuzeigen. + */ + +void BCValueEditor::paintSliderIndicator(QPainter* painter, const QRect& rect, double ratio ) +{ + // Kleinen Slider-Indikator zeichnen + painter->save(); + painter->setRenderHint(QPainter::Antialiasing); + + int adjX = rect.width() - cTextBlockOffset; + + // Mini Progress Bar: der Gesamtbereich + QRect barRect = rect.adjusted( adjX, 12, -10-24, -12 ); + painter->setPen(Qt::NoPen); + painter->setBrush(QColor(0xE0E0E0)); + painter->drawRoundedRect(barRect, 2, 2); + + // Mini Progress Bar: der Wertebereich + barRect.setWidth( ratio * barRect.width() ); + painter->setBrush(QColor(0x0078D4)); + painter->drawRoundedRect(barRect, 2, 2); + + painter->restore(); +} + + +QRect BCValueEditor::updateEditorRect( const QRect& rect) +{ + return rect.adjusted( + rect.width() - cTextBlockOffset, // Von rechts: cTextBlockOffset (==130) px (Breite der Progress Bar) + 0, // Oben: kein Offset + -cPaddingRight, // Rechts: 8px Padding + 0 // Unten: kein Offset + ); +} diff --git a/bcvalueeditor.h b/bcvalueeditor.h index d6e10fe..c73b363 100644 --- a/bcvalueeditor.h +++ b/bcvalueeditor.h @@ -3,19 +3,14 @@ #include + +#include #include class QSlider; class QPushButton; class BCValue; -struct BCValueEditorParams -{ - int value{0}; - int min{0}; - int max{0}; -}; - class BCValueEditor : public QWidget, private Ui::BCValueEditor { Q_OBJECT @@ -25,13 +20,24 @@ public: explicit BCValueEditor(QWidget *parent = nullptr); int value() const; - void setValueAndRange( const BCValueEditorParams& params ); + void setValueAndRange( const BCValue::ValueRange& params ); + + // helper functions + static QRect updateEditorRect( const QRect& rect); + static void paintSliderIndicator(QPainter* painter, const QRect& rect, double ratio ); + signals: void valueChanged(int value); void valueCommited(int value); +protected: + + static constexpr int cTextBlockOffset = 130; + static constexpr int cPaddingRight = 8; + static constexpr int cSliderWidth = 117; + }; #endif // BCValueEditor_H