Added smile
This commit is contained in:
122
bctoggleswitch.cpp
Normal file
122
bctoggleswitch.cpp
Normal file
@@ -0,0 +1,122 @@
|
||||
|
||||
#include <QPainter>
|
||||
#include <QPropertyAnimation>
|
||||
#include <QEasingCurve>
|
||||
#include <QEnterEvent>
|
||||
#include <QEvent>
|
||||
|
||||
#include <bctoggleswitch.h>
|
||||
|
||||
|
||||
|
||||
|
||||
BCToggleSwitch::BCToggleSwitch(QWidget *parent)
|
||||
: QAbstractButton(parent)
|
||||
, m_position(0.0f)
|
||||
{
|
||||
setCheckable(true);
|
||||
setCursor(Qt::PointingHandCursor);
|
||||
setFixedSize(44, 22); // Standardgröße
|
||||
|
||||
// Animation initialisieren
|
||||
m_animation = new QPropertyAnimation(this, "position", this);
|
||||
m_animation->setDuration(200);
|
||||
m_animation->setEasingCurve(QEasingCurve::OutQuad);
|
||||
|
||||
// Signal verknüpfen
|
||||
connect(this, &QAbstractButton::toggled, this, [this](bool checked){
|
||||
m_animation->stop();
|
||||
m_animation->setStartValue(m_position);
|
||||
m_animation->setEndValue(checked ? 1.0f : 0.0f);
|
||||
m_animation->start();
|
||||
});
|
||||
}
|
||||
|
||||
float BCToggleSwitch::position() const
|
||||
{
|
||||
return m_position;
|
||||
}
|
||||
|
||||
void BCToggleSwitch::setPosition(float pos) {
|
||||
m_position = pos;
|
||||
update(); // Trigger Repaint
|
||||
}
|
||||
|
||||
QSize BCToggleSwitch::sizeHint() const
|
||||
{
|
||||
return QSize(44, 22);
|
||||
}
|
||||
|
||||
void BCToggleSwitch::paintEvent(QPaintEvent *)
|
||||
{
|
||||
QPainter p(this);
|
||||
p.setRenderHint(QPainter::Antialiasing);
|
||||
|
||||
// --- Farben ---
|
||||
// Tipp: In einem echten Projekt diese Farben als const statics
|
||||
// oder aus der QPalette laden.
|
||||
QColor offBorderColor = QColor("#8D8D8D");
|
||||
QColor offKnobColor = QColor("#5D5D5D");
|
||||
QColor onColor = QColor("#0078D4"); // Fluent Blue
|
||||
QColor white = Qt::white;
|
||||
|
||||
QRectF rect = this->rect();
|
||||
qreal radius = rect.height() / 2.0;
|
||||
|
||||
// 1. Hintergrund (Track) zeichnen
|
||||
p.setPen(Qt::NoPen);
|
||||
|
||||
if (isChecked() || m_position > 0.5f)
|
||||
{
|
||||
// AN-Zustand: Hintergrund gefüllt
|
||||
p.setBrush(onColor);
|
||||
p.drawRoundedRect(rect, radius, radius);
|
||||
}
|
||||
else
|
||||
{
|
||||
// AUS-Zustand: Nur Rahmen
|
||||
p.setBrush(Qt::NoBrush);
|
||||
|
||||
// Hover-Status prüfen
|
||||
if (underMouse())
|
||||
p.setPen(QPen(offBorderColor.darker(120), 1.5));
|
||||
else
|
||||
p.setPen(QPen(offBorderColor, 1.5));
|
||||
|
||||
// Rechteck etwas verkleinern, damit der Rahmen nicht abgeschnitten wird
|
||||
p.drawRoundedRect(rect.adjusted(1, 1, -1, -1), radius - 1, radius - 1);
|
||||
}
|
||||
|
||||
// 2. Knopf (Thumb) zeichnen
|
||||
qreal padding = 3.0;
|
||||
qreal knobDiameter = rect.height() - (2 * padding);
|
||||
|
||||
// Interpolation der Position
|
||||
qreal startX = padding;
|
||||
qreal endX = rect.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);
|
||||
} else {
|
||||
if (underMouse()) p.setBrush(offKnobColor.darker(110));
|
||||
else p.setBrush(offKnobColor);
|
||||
}
|
||||
|
||||
p.setPen(Qt::NoPen);
|
||||
p.drawEllipse(knobRect);
|
||||
}
|
||||
|
||||
void BCToggleSwitch::enterEvent(QEnterEvent *event)
|
||||
{
|
||||
update(); // Für Hover-Effekt neu zeichnen
|
||||
QAbstractButton::enterEvent(event);
|
||||
}
|
||||
|
||||
void BCToggleSwitch::leaveEvent(QEvent *event)
|
||||
{
|
||||
update(); // Hover entfernen
|
||||
QAbstractButton::leaveEvent(event);
|
||||
}
|
||||
Reference in New Issue
Block a user