Added boolean value update
This commit is contained in:
@@ -44,7 +44,9 @@ BCToggleSwitch::BCToggleSwitch(QWidget *parent)
|
||||
{
|
||||
setCheckable(true);
|
||||
setCursor(Qt::PointingHandCursor);
|
||||
setFixedSize(44, 22); // Standardgröße
|
||||
|
||||
// Widget ist jetzt mit setGeometry / Layouts skalierbar
|
||||
setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
|
||||
|
||||
// Animation initialisieren
|
||||
m_animation = new QPropertyAnimation(this, "position", this);
|
||||
@@ -77,30 +79,38 @@ QSize BCToggleSwitch::sizeHint() const
|
||||
return QSize(44, 22);
|
||||
}
|
||||
|
||||
QSize BCToggleSwitch::minimumSizeHint() const
|
||||
{
|
||||
return QSize(30, 16);
|
||||
}
|
||||
|
||||
void BCToggleSwitch::paintEvent(QPaintEvent *)
|
||||
{
|
||||
QPainter p(this);
|
||||
p.setRenderHint(QPainter::Antialiasing);
|
||||
|
||||
// Option A: Nutze die Standard-Hintergrundfarbe des aktuellen Qt-Themes
|
||||
p.fillRect(rect(), palette().window());
|
||||
QRect ownRect = rect();
|
||||
|
||||
// Standard-Hintergrundfarbe des aktuellen Qt-Themes
|
||||
p.fillRect(ownRect, palette().window());
|
||||
|
||||
// Auf deaktivierten Zustand prüfen und die statische Methode aufrufen
|
||||
if (!isEnabled()) {
|
||||
paintToggleIndicator(&p, rect(), isChecked(), m_position, palette());
|
||||
paintToggleIndicator(&p, ownRect, isChecked(), m_position, palette());
|
||||
return;
|
||||
}
|
||||
|
||||
// --- Farben ---
|
||||
// Tipp: In einem echten Projekt diese Farben als const statics
|
||||
// oder aus der QPalette laden.
|
||||
QColor offBorderColor = QColor(0x8D8D8D);
|
||||
QColor offKnobColor = QColor(0x5D5D5D);
|
||||
QColor onColor = QColor(0x0078D4); // Fluent Blue
|
||||
QColor white = Qt::white;
|
||||
// --- Farben aus der aktuellen Palette ---
|
||||
QColor onColor = palette().color(QPalette::Highlight);
|
||||
QColor offBorderColor = palette().color(QPalette::Mid);
|
||||
QColor offKnobColor = palette().color(QPalette::Dark);
|
||||
QColor knobOnColor = palette().color(QPalette::HighlightedText);
|
||||
|
||||
QRectF rect = this->rect();
|
||||
qreal radius = rect.height() / 2.0;
|
||||
// Proportionale Werte aus der Widget-Höhe ableiten
|
||||
qreal h = ownRect.height();
|
||||
qreal radius = h / 2.0;
|
||||
qreal penW = qMax(1.0, h / 14.0); // Strichstärke skaliert mit
|
||||
qreal padding = qMax(2.0, h * 0.14); // Innenabstand skaliert mit
|
||||
|
||||
// 1. Hintergrund (Track) zeichnen
|
||||
p.setPen(Qt::NoPen);
|
||||
@@ -109,7 +119,7 @@ void BCToggleSwitch::paintEvent(QPaintEvent *)
|
||||
{
|
||||
// AN-Zustand: Hintergrund gefüllt
|
||||
p.setBrush(onColor);
|
||||
p.drawRoundedRect(rect, radius, radius);
|
||||
p.drawRoundedRect(ownRect, radius, radius);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -118,28 +128,29 @@ void BCToggleSwitch::paintEvent(QPaintEvent *)
|
||||
|
||||
// Hover-Status prüfen
|
||||
if (underMouse())
|
||||
p.setPen(QPen(offBorderColor.darker(120), 1.5));
|
||||
p.setPen(QPen(offBorderColor.darker(120), penW));
|
||||
else
|
||||
p.setPen(QPen(offBorderColor, 1.5));
|
||||
p.setPen(QPen(offBorderColor, penW));
|
||||
|
||||
// Rechteck etwas verkleinern, damit der Rahmen nicht abgeschnitten wird
|
||||
p.drawRoundedRect(rect.adjusted(1, 1, -1, -1), radius - 1, radius - 1);
|
||||
qreal inset = penW / 2.0;
|
||||
p.drawRoundedRect(QRectF(ownRect).adjusted(inset, inset, -inset, -inset),
|
||||
radius - inset, radius - inset);
|
||||
}
|
||||
|
||||
// 2. Knopf (Thumb) zeichnen
|
||||
qreal padding = 3.0;
|
||||
qreal knobDiameter = rect.height() - (2 * padding);
|
||||
qreal knobDiameter = h - (2 * padding);
|
||||
|
||||
// Interpolation der Position
|
||||
qreal startX = padding;
|
||||
qreal endX = rect.width() - knobDiameter - padding;
|
||||
qreal endX = ownRect.width() - knobDiameter - padding;
|
||||
qreal currentX = startX + (m_position * (endX - startX));
|
||||
|
||||
QRectF knobRect(currentX, padding, knobDiameter, knobDiameter);
|
||||
|
||||
if (isChecked() || m_position > 0.5f)
|
||||
{
|
||||
p.setBrush(white);
|
||||
p.setBrush(knobOnColor);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -155,18 +166,17 @@ void BCToggleSwitch::paintEvent(QPaintEvent *)
|
||||
|
||||
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ß
|
||||
// --- Farben für den deaktivierten Zustand aus der Palette ---
|
||||
QColor disabledBorderColor = palette.color(QPalette::Disabled, QPalette::Mid);
|
||||
QColor disabledKnobColor = palette.color(QPalette::Disabled, QPalette::Dark);
|
||||
QColor disabledOnColor = palette.color(QPalette::Disabled, QPalette::Highlight);
|
||||
QColor disabledKnobOnColor = palette.color(QPalette::Disabled, QPalette::HighlightedText);
|
||||
|
||||
QRectF r(rect);
|
||||
qreal radius = r.height() / 2.0;
|
||||
qreal h = r.height();
|
||||
qreal radius = h / 2.0;
|
||||
qreal penW = qMax(1.0, h / 14.0);
|
||||
qreal padding = qMax(2.0, h * 0.14);
|
||||
|
||||
// 1. Hintergrund (Track) zeichnen
|
||||
p->setPen(Qt::NoPen);
|
||||
@@ -181,18 +191,18 @@ void BCToggleSwitch::paintToggleIndicator(QPainter* p, const QRect& rect, bool i
|
||||
{
|
||||
// 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);
|
||||
p->setPen(QPen(disabledBorderColor, penW));
|
||||
qreal inset = penW / 2.0;
|
||||
p->drawRoundedRect(r.adjusted(inset, inset, -inset, -inset),
|
||||
radius - inset, radius - inset);
|
||||
}
|
||||
|
||||
// 2. Knopf (Thumb) zeichnen
|
||||
qreal padding = 3.0;
|
||||
qreal knobDiameter = r.height() - (2 * padding);
|
||||
qreal knobDiameter = h - (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 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);
|
||||
@@ -201,7 +211,7 @@ void BCToggleSwitch::paintToggleIndicator(QPainter* p, const QRect& rect, bool i
|
||||
|
||||
if (isChecked || position > 0.5f)
|
||||
{
|
||||
p->setBrush(white);
|
||||
p->setBrush(disabledKnobOnColor);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -209,8 +219,8 @@ void BCToggleSwitch::paintToggleIndicator(QPainter* p, const QRect& rect, bool i
|
||||
}
|
||||
|
||||
p->drawEllipse(knobRect);
|
||||
|
||||
}
|
||||
|
||||
void BCToggleSwitch::enterEvent(QEnterEvent *event)
|
||||
{
|
||||
update(); // Für Hover-Effekt neu zeichnen
|
||||
|
||||
Reference in New Issue
Block a user