Created new painter bug.

This commit is contained in:
2026-01-22 22:16:19 +01:00
parent ab4abd214e
commit 37fcc5e888
8 changed files with 110 additions and 150 deletions

View File

@@ -31,6 +31,8 @@
#include <bcsliderstyle.h>
#include <bcvalueeditor.h>
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,62 +131,12 @@ void BCSliderStyle::drawHorizontalFluentSlider(QPainter* painter, const QStyleOp
const QColor& bgColor) const
{
QRect groove = slider->rect;
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);
qDebug() << " ---WTF: " << groove;
// Handle (Thumb) - Fluent style is more subtle
int handleSize = 16;
QRect thumbRect(handle.center().x() - handleSize / 2,
handle.center().y() - handleSize / 2,
handleSize, handleSize);
// Hover effect - subtle glow
if (slider->state & State_MouseOver)
{
painter->setBrush(QColor(activeColor.red(), activeColor.green(),
activeColor.blue(), 30));
int glowSize = 18;
QRect glow(handle.center().x() - glowSize / 2,
handle.center().y() - glowSize / 2,
glowSize, glowSize);
painter->drawEllipse(glow);
}
// Thumb
painter->setBrush(bgColor);
painter->setPen(QPen(activeColor, 2));
painter->drawEllipse(thumbRect);
// Inner circle for pressed state
if (slider->state & State_Sunken) {
int innerSize = 6;
QRect inner(handle.center().x() - innerSize / 2,
handle.center().y() - innerSize / 2,
innerSize, innerSize);
painter->setPen(Qt::NoPen);
painter->setBrush(activeColor);
painter->drawEllipse(inner);
}
}
void BCSliderStyle::drawSliderIndicator( QPainter* painter, QRect& groove )
{
BCValueEditor::paintSliderIndicator(painter, groove, 0.5 );
/*
int grooveHeight = 4;
// Track sollte im Widget-Zentrum sein, nicht im groove-Zentrum
@@ -200,4 +156,44 @@ void BCSliderStyle::drawSliderIndicator( QPainter* painter, QRect& groove )
painter->setBrush(activeColor);
painter->drawRoundedRect(activeTrack, grooveHeight / 2, grooveHeight / 2);
*/
// Handle (Thumb) - Fluent style is more subtle
int handleSize = 16;
QRect thumbRect(handle.center().x() - handleSize / 2,
handle.center().y() - handleSize / 2,
handleSize, handleSize);
// Hover effect - subtle glow
if (slider->state & State_MouseOver)
{
painter->setBrush(QColor(activeColor.red(), activeColor.green(),
activeColor.blue(), 30));
int glowSize = 18;
QRect glow(handle.center().x() - glowSize / 2,
handle.center().y() - glowSize / 2,
glowSize, glowSize);
painter->drawEllipse(glow);
}
// Thumb
painter->setBrush(bgColor);
painter->setPen(QPen(activeColor, 2));
painter->drawEllipse(thumbRect);
// Inner circle for pressed state
if (slider->state & State_Sunken) {
int innerSize = 6;
QRect inner(handle.center().x() - innerSize / 2,
handle.center().y() - innerSize / 2,
innerSize, innerSize);
painter->setPen(Qt::NoPen);
painter->setBrush(activeColor);
painter->drawEllipse(inner);
}
}

View File

@@ -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 );
};

View File

@@ -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;

View File

@@ -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;

View File

@@ -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( row>-1 && row <= _valueList.size() )
{
if( index.column() != 1 )
return;
if( row<0 || row >= _valueList.size() )
return;
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: " <<option.rect;
BCValueEditor::paintSliderIndicator(painter, option.rect, bcValue.calcMinMaxRatio() );
}
}
if(_rowOpacities.contains(row))
paintHighlightRow(painter,option,index.row());
}
}
@@ -175,17 +176,10 @@ 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
);
QRect sliderRect = BCValueEditor::updateEditorRect( option.rect );
editor->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

View File

@@ -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<int, qreal> _rowOpacities;
QHash<int, QVariantAnimation*> _rowAnimations;
static constexpr int cTextBlockOffset = 130;
static constexpr int cPaddingRight = 8;
static constexpr int cSliderWidth = 117;
};

View File

@@ -1,7 +1,6 @@
#include <bcsliderstyle.h>
#include <bcvalueeditor.h>
#include <bcvalue.h>
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
);
}

View File

@@ -3,19 +3,14 @@
#include <QWidget>
#include <bcvalue.h>
#include <ui_bcvalueeditor.h>
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