From 075aadc665f9cc9d7faf6ea374d158ec20182b8e Mon Sep 17 00:00:00 2001 From: "PANIK\\chris" Date: Tue, 31 Mar 2026 18:18:37 +0200 Subject: [PATCH] Added fake toggle switch. --- bc.h | 3 +++ bctoggleswitch.cpp | 64 +++++++++++++++++++++++++++++++++++++++++++++ bctoggleswitch.h | 3 +++ bcvalue.cpp | 5 ++++ bcvalue.h | 1 + bcvaluedelegate.cpp | 27 +++++++++---------- 6 files changed, 89 insertions(+), 14 deletions(-) diff --git a/bc.h b/bc.h index 42a03f4..ca5b07b 100644 --- a/bc.h +++ b/bc.h @@ -61,6 +61,9 @@ namespace BCTags inline constexpr auto Min = "Min"_L1; inline constexpr auto Max = "Max"_L1; inline constexpr auto Factor = "Factor"_L1; + + inline constexpr auto Yes = "Yes"_L1; + inline constexpr auto No = "No"_L1; } /** diff --git a/bctoggleswitch.cpp b/bctoggleswitch.cpp index e08410e..2178046 100644 --- a/bctoggleswitch.cpp +++ b/bctoggleswitch.cpp @@ -85,6 +85,12 @@ void BCToggleSwitch::paintEvent(QPaintEvent *) // Option A: Nutze die Standard-Hintergrundfarbe des aktuellen Qt-Themes 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 --- // Tipp: In einem echten Projekt diese Farben als const statics // oder aus der QPalette laden. @@ -147,6 +153,64 @@ void BCToggleSwitch::paintEvent(QPaintEvent *) 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) { update(); // Für Hover-Effekt neu zeichnen diff --git a/bctoggleswitch.h b/bctoggleswitch.h index d76fa01..38cab0d 100644 --- a/bctoggleswitch.h +++ b/bctoggleswitch.h @@ -22,6 +22,9 @@ public: 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: void paintEvent(QPaintEvent *event) override; diff --git a/bcvalue.cpp b/bcvalue.cpp index 7451abe..ee59add 100644 --- a/bcvalue.cpp +++ b/bcvalue.cpp @@ -69,6 +69,11 @@ bool BCValue::isBoolean() const return valueType() == BCValue::ValueType::Bool; } +bool BCValue::isChecked() const +{ + return isBoolean() && rawValue() == 1; +} + bool BCValue::testFlag( BCValue::Flag flag ) const { return _valueFlags.testFlag( flag ); diff --git a/bcvalue.h b/bcvalue.h index 9fcb690..0988519 100644 --- a/bcvalue.h +++ b/bcvalue.h @@ -120,6 +120,7 @@ public: bool isWord() const; bool isReadOnly() const; bool isBoolean() const; + bool isChecked() const; bool testFlag( Flag flag ) const; void setFlag( Flag flag, bool state=true ) const; diff --git a/bcvaluedelegate.cpp b/bcvaluedelegate.cpp index 801ba2d..1687119 100644 --- a/bcvaluedelegate.cpp +++ b/bcvaluedelegate.cpp @@ -116,11 +116,10 @@ void BCValueDelegate::updateEditorGeometry(QWidget* editor, const QStyleOptionVi if( !bcValue.isBoolean()) { editorRect = clipToSliderRect( option.rect ); - } else { - editorRect = option.rect.adjusted(2,4,0,0); + editorRect = option.rect.adjusted(option.rect.width() - cTextBlockOffset,4,0,0); } editor->setGeometry(editorRect); } @@ -153,14 +152,11 @@ void BCValueDelegate::paint(QPainter *painter, const QStyleOptionViewItem& optio QStyleOptionViewItem opt = option; initStyleOption(&opt, index); - const BCValue& bcValue = *(_valueList[ index.row()].get()); - //qDebug() << " --- RO?: " << bcValue.label() << " Ro: " << bcValue.isReadOnly() << " Bool: " << bcValue.isBoolean() << ": " << index.flags(); + const BCValue& bcValue = *(_valueList[ index.row()].get()); if( bcValue.isBoolean() ) { - paintBooleanValue( painter, opt, bcValue ); - //opt.text = bcValue.rawValue() == 1 ? "Yes" : "No"; - //painter->drawText( opt.rect, bcValue.rawValue() == 1 ? "Yes" : "No" ); + paintBooleanValue( painter, opt, bcValue ); } else { @@ -168,17 +164,20 @@ void BCValueDelegate::paint(QPainter *painter, const QStyleOptionViewItem& optio QStyledItemDelegate::paint(painter, opt, index); } - if( !bcValue.isReadOnly() ) { // Wir zeichnen boolean Values an toggle switches - if( bcValue.isBoolean() ) - paintPlainToggleSwitch(painter, opt, bcValue); + if (bcValue.isBoolean()) + { + //paintPlainToggleSwitch(painter, opt, bcValue); + } else - paintPlainSliderIndicator(painter, opt.rect, bcValue.calcMinMaxRatio() ); + { + paintPlainSliderIndicator(painter, opt.rect, bcValue.calcMinMaxRatio()); + } + } - if(_rowOpacities.contains(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); // 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!) // 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 { - + BCToggleSwitch::paintToggleIndicator(painter, option.rect, bcValue.isChecked(), 20, option.widget->palette()); }