Added smile
This commit is contained in:
@@ -53,6 +53,7 @@ SOURCES += \
|
||||
bcdrivertinycan.cpp \
|
||||
bcsliderstyle.cpp \
|
||||
bcthemeswitchbutton.cpp \
|
||||
bctoggleswitch.cpp \
|
||||
bctransmitter.cpp \
|
||||
bcvalue.cpp \
|
||||
bcvaluemodel.cpp \
|
||||
@@ -72,6 +73,7 @@ HEADERS += \
|
||||
bcmainwindow.h \
|
||||
bcsliderstyle.h \
|
||||
bcthemeswitchbutton.h \
|
||||
bctoggleswitch.h \
|
||||
bctransmitter.h \
|
||||
bcvalue.h \
|
||||
bcvaluemodel.h \
|
||||
|
||||
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);
|
||||
}
|
||||
37
bctoggleswitch.h
Normal file
37
bctoggleswitch.h
Normal file
@@ -0,0 +1,37 @@
|
||||
#ifndef BCTOGGLESWITCH_H
|
||||
#define BCTOGGLESWITCH_H
|
||||
|
||||
#include <QAbstractButton>
|
||||
|
||||
// Vorwärtsdeklaration spart Include-Zeit
|
||||
class QPropertyAnimation;
|
||||
|
||||
class BCToggleSwitch : public QAbstractButton
|
||||
{
|
||||
Q_OBJECT
|
||||
// Property für die Animation (0.0 bis 1.0)
|
||||
Q_PROPERTY(float position READ position WRITE setPosition)
|
||||
|
||||
public:
|
||||
|
||||
explicit BCToggleSwitch(QWidget *parent = nullptr);
|
||||
|
||||
// Getter & Setter für die Animations-Property
|
||||
float position() const;
|
||||
void setPosition(float pos);
|
||||
|
||||
QSize sizeHint() const override;
|
||||
|
||||
protected:
|
||||
|
||||
void paintEvent(QPaintEvent *event) override;
|
||||
void enterEvent(QEnterEvent *event) override;
|
||||
void leaveEvent(QEvent *event) override;
|
||||
|
||||
float m_position; // 0.0 = Aus, 1.0 = An
|
||||
QPropertyAnimation *m_animation;
|
||||
};
|
||||
|
||||
|
||||
|
||||
#endif // BCTOGGLESWITCH_H
|
||||
Reference in New Issue
Block a user