Added fake toggle switch.

This commit is contained in:
2026-03-31 18:18:37 +02:00
parent 5db2677d76
commit 075aadc665
6 changed files with 89 additions and 14 deletions

3
bc.h
View File

@@ -61,6 +61,9 @@ namespace BCTags
inline constexpr auto Min = "Min"_L1; inline constexpr auto Min = "Min"_L1;
inline constexpr auto Max = "Max"_L1; inline constexpr auto Max = "Max"_L1;
inline constexpr auto Factor = "Factor"_L1; inline constexpr auto Factor = "Factor"_L1;
inline constexpr auto Yes = "Yes"_L1;
inline constexpr auto No = "No"_L1;
} }
/** /**

View File

@@ -85,6 +85,12 @@ void BCToggleSwitch::paintEvent(QPaintEvent *)
// Option A: Nutze die Standard-Hintergrundfarbe des aktuellen Qt-Themes // Option A: Nutze die Standard-Hintergrundfarbe des aktuellen Qt-Themes
p.fillRect(rect(), palette().window()); p.fillRect(rect(), palette().window());
// Auf deaktivierten Zustand prüfen und die statische Methode aufrufen
if (!isEnabled()) {
paintToggleIndicator(&p, rect(), isChecked(), m_position, palette());
return;
}
// --- Farben --- // --- Farben ---
// Tipp: In einem echten Projekt diese Farben als const statics // Tipp: In einem echten Projekt diese Farben als const statics
// oder aus der QPalette laden. // oder aus der QPalette laden.
@@ -147,6 +153,64 @@ void BCToggleSwitch::paintEvent(QPaintEvent *)
p.drawEllipse(knobRect); p.drawEllipse(knobRect);
} }
void BCToggleSwitch::paintToggleIndicator(QPainter* p, const QRect& rect, bool isChecked, float position, const QPalette& palette)
{
// --- Farben für den deaktivierten Zustand ---
QColor disabledBorderColor = palette.color(QPalette::Disabled, QPalette::Window);
if (!disabledBorderColor.isValid() || disabledBorderColor == Qt::black) {
disabledBorderColor = QColor(0xCCCCCC); // Fallback auf ein neutrales Hellgrau
}
QColor disabledKnobColor = QColor(0xAAAAAA);
QColor disabledOnColor = QColor(0x99C2E3); // Entsättigtes, blasses "Fluent Blue"
QColor white = QColor(0xF0F0F0); // Leicht abgedunkeltes Weiß
QRectF r(rect);
qreal radius = r.height() / 2.0;
// 1. Hintergrund (Track) zeichnen
p->setPen(Qt::NoPen);
if (isChecked || position > 0.5f)
{
// AN-Zustand (deaktiviert)
p->setBrush(disabledOnColor);
p->drawRoundedRect(r, radius, radius);
}
else
{
// AUS-Zustand (deaktiviert)
p->setBrush(Qt::NoBrush);
p->setPen(QPen(disabledBorderColor, 1.5));
// Rechteck etwas verkleinern, damit der Rahmen nicht abgeschnitten wird
p->drawRoundedRect(r.adjusted(1, 1, -1, -1), radius - 1, radius - 1);
}
// 2. Knopf (Thumb) zeichnen
qreal padding = 3.0;
qreal knobDiameter = r.height() - (2 * padding);
// X- und Y-Offsets des übergebenen Rects berücksichtigen!
qreal startX = r.x() + padding;
qreal endX = r.x() + r.width() - knobDiameter - padding;
qreal currentX = startX + (position * (endX - startX));
QRectF knobRect(currentX, r.y() + padding, knobDiameter, knobDiameter);
p->setPen(Qt::NoPen);
if (isChecked || position > 0.5f)
{
p->setBrush(white);
}
else
{
p->setBrush(disabledKnobColor);
}
p->drawEllipse(knobRect);
}
void BCToggleSwitch::enterEvent(QEnterEvent *event) void BCToggleSwitch::enterEvent(QEnterEvent *event)
{ {
update(); // Für Hover-Effekt neu zeichnen update(); // Für Hover-Effekt neu zeichnen

View File

@@ -22,6 +22,9 @@ public:
QSize sizeHint() const override; QSize sizeHint() const override;
// Statische Methode zum Zeichnen des passiven/deaktivierten Zustands
static void paintToggleIndicator(QPainter* p, const QRect& rect, bool isChecked, float position, const QPalette& palette);
protected: protected:
void paintEvent(QPaintEvent *event) override; void paintEvent(QPaintEvent *event) override;

View File

@@ -69,6 +69,11 @@ bool BCValue::isBoolean() const
return valueType() == BCValue::ValueType::Bool; return valueType() == BCValue::ValueType::Bool;
} }
bool BCValue::isChecked() const
{
return isBoolean() && rawValue() == 1;
}
bool BCValue::testFlag( BCValue::Flag flag ) const bool BCValue::testFlag( BCValue::Flag flag ) const
{ {
return _valueFlags.testFlag( flag ); return _valueFlags.testFlag( flag );

View File

@@ -120,6 +120,7 @@ public:
bool isWord() const; bool isWord() const;
bool isReadOnly() const; bool isReadOnly() const;
bool isBoolean() const; bool isBoolean() const;
bool isChecked() const;
bool testFlag( Flag flag ) const; bool testFlag( Flag flag ) const;
void setFlag( Flag flag, bool state=true ) const; void setFlag( Flag flag, bool state=true ) const;

View File

@@ -116,11 +116,10 @@ void BCValueDelegate::updateEditorGeometry(QWidget* editor, const QStyleOptionVi
if( !bcValue.isBoolean()) if( !bcValue.isBoolean())
{ {
editorRect = clipToSliderRect( option.rect ); editorRect = clipToSliderRect( option.rect );
} }
else else
{ {
editorRect = option.rect.adjusted(2,4,0,0); editorRect = option.rect.adjusted(option.rect.width() - cTextBlockOffset,4,0,0);
} }
editor->setGeometry(editorRect); editor->setGeometry(editorRect);
} }
@@ -153,14 +152,11 @@ void BCValueDelegate::paint(QPainter *painter, const QStyleOptionViewItem& optio
QStyleOptionViewItem opt = option; QStyleOptionViewItem opt = option;
initStyleOption(&opt, index); initStyleOption(&opt, index);
const BCValue& bcValue = *(_valueList[ index.row()].get()); const BCValue& bcValue = *(_valueList[ index.row()].get());
//qDebug() << " --- RO?: " << bcValue.label() << " Ro: " << bcValue.isReadOnly() << " Bool: " << bcValue.isBoolean() << ": " << index.flags();
if( bcValue.isBoolean() ) if( bcValue.isBoolean() )
{ {
paintBooleanValue( painter, opt, bcValue ); paintBooleanValue( painter, opt, bcValue );
//opt.text = bcValue.rawValue() == 1 ? "Yes" : "No";
//painter->drawText( opt.rect, bcValue.rawValue() == 1 ? "Yes" : "No" );
} }
else else
{ {
@@ -168,17 +164,20 @@ void BCValueDelegate::paint(QPainter *painter, const QStyleOptionViewItem& optio
QStyledItemDelegate::paint(painter, opt, index); QStyledItemDelegate::paint(painter, opt, index);
} }
if( !bcValue.isReadOnly() ) if( !bcValue.isReadOnly() )
{ {
// Wir zeichnen boolean Values an toggle switches // Wir zeichnen boolean Values an toggle switches
if( bcValue.isBoolean() ) if (bcValue.isBoolean())
paintPlainToggleSwitch(painter, opt, bcValue); {
//paintPlainToggleSwitch(painter, opt, bcValue);
}
else else
paintPlainSliderIndicator(painter, opt.rect, bcValue.calcMinMaxRatio() ); {
paintPlainSliderIndicator(painter, opt.rect, bcValue.calcMinMaxRatio());
}
} }
if(_rowOpacities.contains(row)) if(_rowOpacities.contains(row))
paintHighlightRow(painter, opt,index.row()); paintHighlightRow(painter, opt,index.row());
@@ -190,7 +189,7 @@ void BCValueDelegate::paintBooleanValue( QPainter *painter, const QStyleOptionVi
QRect textRect = option.rect.adjusted( 2,0,0,0); QRect textRect = option.rect.adjusted( 2,0,0,0);
// 3. Den tatsächlichen Wert aus dem Model auslesen // 3. Den tatsächlichen Wert aus dem Model auslesen
QString text = bcValue.rawValue() == 1 ? "Yes" : "No"; QString text = bcValue.rawValue() == 1 ? BCTags::Yes : BCTags::No;
// 3. Die korrekte Textfarbe ermitteln (extrem wichtig für die UX!) // 3. Die korrekte Textfarbe ermitteln (extrem wichtig für die UX!)
// Wenn die Zeile markiert ist (Selected), muss der Text meist weiß sein, // Wenn die Zeile markiert ist (Selected), muss der Text meist weiß sein,
@@ -312,7 +311,7 @@ void BCValueDelegate::paintHighlightRow(QPainter* painter, const QStyleOptionVie
void BCValueDelegate::paintPlainToggleSwitch(QPainter* painter, const QStyleOptionViewItem& option, const BCValue& bcValue) const void BCValueDelegate::paintPlainToggleSwitch(QPainter* painter, const QStyleOptionViewItem& option, const BCValue& bcValue) const
{ {
BCToggleSwitch::paintToggleIndicator(painter, option.rect, bcValue.isChecked(), 20, option.widget->palette());
} }