279 lines
6.7 KiB
C++
279 lines
6.7 KiB
C++
// ThemeToggleWidget.h
|
|
#ifndef THEMETOGGLEWIDGET_H
|
|
#define THEMETOGGLEWIDGET_H
|
|
|
|
#include <QWidget>
|
|
#include <QPushButton>
|
|
#include <QHBoxLayout>
|
|
#include <QPainter>
|
|
#include <QPropertyAnimation>
|
|
#include <QFile>
|
|
#include <QApplication>
|
|
#include <QDebug>
|
|
|
|
class ThemeToggleWidget : public QWidget
|
|
{
|
|
Q_OBJECT
|
|
Q_PROPERTY(int position READ position WRITE setPosition)
|
|
|
|
public:
|
|
|
|
explicit ThemeToggleWidget(QWidget* parent = nullptr)
|
|
: QWidget(parent), m_isDarkMode(true), m_position(0) {
|
|
|
|
setupUI();
|
|
applyDarkTheme();
|
|
|
|
}
|
|
|
|
bool isDarkMode() const { return m_isDarkMode; }
|
|
|
|
void setDarkMode(bool dark)
|
|
{
|
|
if (m_isDarkMode != dark)
|
|
{
|
|
m_isDarkMode = dark;
|
|
animateToggle();
|
|
applyTheme();
|
|
emit themeChanged(m_isDarkMode);
|
|
}
|
|
}
|
|
|
|
signals:
|
|
|
|
void themeChanged(bool isDarkMode);
|
|
|
|
private slots:
|
|
|
|
void toggleTheme()
|
|
{
|
|
setDarkMode(!m_isDarkMode);
|
|
}
|
|
|
|
public:
|
|
|
|
void setupUI()
|
|
{
|
|
QHBoxLayout* layout = new QHBoxLayout(this);
|
|
layout->setContentsMargins(0, 0, 0, 0);
|
|
layout->setSpacing(0);
|
|
|
|
// Toggle Button
|
|
m_toggleButton = new QPushButton(this);
|
|
m_toggleButton->setFixedSize(64, 28);
|
|
m_toggleButton->setCursor(Qt::PointingHandCursor);
|
|
m_toggleButton->setFlat(true);
|
|
|
|
connect(m_toggleButton, &QPushButton::clicked, this, &ThemeToggleWidget::toggleTheme);
|
|
|
|
layout->addWidget(m_toggleButton);
|
|
|
|
setFixedSize(64, 28);
|
|
}
|
|
|
|
void paintEvent(QPaintEvent*) override
|
|
{
|
|
qDebug() << " --- paint!";
|
|
|
|
QPainter painter(this);
|
|
painter.setRenderHint(QPainter::Antialiasing);
|
|
|
|
int thumbX = 2 + m_position;
|
|
/*
|
|
// Track background
|
|
QColor trackColor = m_isDarkMode ? QColor(0x3A3A3A) : QColor(0xE1DFDD);
|
|
painter.setPen(Qt::NoPen);
|
|
painter.setBrush(trackColor);
|
|
painter.drawRoundedRect(rect(), 14, 14);
|
|
|
|
// Animated thumb
|
|
int thumbX = 2 + m_position;
|
|
QColor thumbColor = m_isDarkMode ? QColor(0x1F1F1F) : QColor(0xFFFFFF);
|
|
|
|
// Shadow
|
|
painter.setBrush(QColor(0, 0, 0, 30));
|
|
painter.drawEllipse(thumbX + 1, 3, 22, 22);
|
|
|
|
// Thumb
|
|
painter.setBrush(thumbColor);
|
|
painter.drawEllipse(thumbX, 2, 24, 24);
|
|
|
|
|
|
// Icon on thumb
|
|
painter.setPen(m_isDarkMode ? QColor(0xFFD700) : QColor(0xFFA500));
|
|
QFont iconFont = font();
|
|
iconFont.setPointSize(12);
|
|
painter.setFont(iconFont);
|
|
*/
|
|
|
|
QString icon = "FITZ!";// m_isDarkMode ? "🌙" : "☀️";
|
|
painter.drawText(QRect(0, 2, 24, 24), Qt::AlignLeft, icon);
|
|
}
|
|
|
|
int position() const { return m_position; }
|
|
void setPosition(int pos) {
|
|
m_position = pos;
|
|
update();
|
|
}
|
|
|
|
void animateToggle()
|
|
{
|
|
QPropertyAnimation* anim = new QPropertyAnimation(this, "position");
|
|
anim->setDuration(200);
|
|
anim->setEasingCurve(QEasingCurve::InOutQuad);
|
|
anim->setStartValue(m_position);
|
|
anim->setEndValue(m_isDarkMode ? 36 : 0);
|
|
anim->start(QAbstractAnimation::DeleteWhenStopped);
|
|
}
|
|
|
|
void applyTheme()
|
|
{
|
|
if (m_isDarkMode) {
|
|
applyDarkTheme();
|
|
} else {
|
|
applyLightTheme();
|
|
}
|
|
}
|
|
|
|
void applyDarkTheme()
|
|
{
|
|
QFile file(":/styles/fluent_dark.qss");
|
|
if (file.open(QFile::ReadOnly)) {
|
|
qApp->setStyleSheet(file.readAll());
|
|
} else {
|
|
// Fallback: Inline Dark Theme
|
|
qApp->setStyleSheet(R"(
|
|
* {
|
|
background-color: #202020;
|
|
color: #FFFFFF;
|
|
}
|
|
QPushButton {
|
|
background-color: #2B2B2B;
|
|
border: 1px solid #3F3F3F;
|
|
border-radius: 4px;
|
|
padding: 6px 16px;
|
|
}
|
|
QPushButton:hover {
|
|
background-color: #3A3A3A;
|
|
}
|
|
QStatusBar {
|
|
background-color: #2B2B2B;
|
|
color: #B0B0B0;
|
|
border-top: 1px solid #3F3F3F;
|
|
}
|
|
)");
|
|
}
|
|
}
|
|
|
|
void applyLightTheme() {
|
|
QFile file(":/styles/fluent_light.qss");
|
|
if (file.open(QFile::ReadOnly)) {
|
|
qApp->setStyleSheet(file.readAll());
|
|
} else {
|
|
// Fallback: Inline Light Theme
|
|
qApp->setStyleSheet(R"(
|
|
* {
|
|
background-color: #F3F3F3;
|
|
color: #000000;
|
|
}
|
|
QPushButton {
|
|
background-color: #FFFFFF;
|
|
border: 1px solid #E1DFDD;
|
|
border-radius: 4px;
|
|
padding: 6px 16px;
|
|
}
|
|
QPushButton:hover {
|
|
background-color: #F9F9F9;
|
|
}
|
|
QStatusBar {
|
|
background-color: #FFFFFF;
|
|
color: #605E5C;
|
|
border-top: 1px solid #E1DFDD;
|
|
}
|
|
)");
|
|
}
|
|
}
|
|
|
|
bool m_isDarkMode;
|
|
int m_position;
|
|
QPushButton* m_toggleButton;
|
|
};
|
|
|
|
// ============================================================================
|
|
// Alternative: Kompaktere Version ohne Animation
|
|
// ============================================================================
|
|
|
|
class SimpleThemeToggle : public QPushButton
|
|
{
|
|
Q_OBJECT
|
|
|
|
public:
|
|
explicit SimpleThemeToggle(QWidget* parent = nullptr)
|
|
: QPushButton(parent), m_isDarkMode(true) {
|
|
|
|
setFixedSize(48, 28);
|
|
setCursor(Qt::PointingHandCursor);
|
|
setFlat(true);
|
|
updateIcon();
|
|
|
|
connect(this, &QPushButton::clicked, this, &SimpleThemeToggle::toggleTheme);
|
|
|
|
// Tooltip
|
|
setToolTip("Toggle Dark/Light Mode");
|
|
}
|
|
|
|
bool isDarkMode() const { return m_isDarkMode; }
|
|
|
|
signals:
|
|
void themeChanged(bool isDarkMode);
|
|
|
|
private slots:
|
|
void toggleTheme() {
|
|
m_isDarkMode = !m_isDarkMode;
|
|
updateIcon();
|
|
applyTheme();
|
|
emit themeChanged(m_isDarkMode);
|
|
}
|
|
|
|
private:
|
|
|
|
void updateIcon()
|
|
{
|
|
QString icon = m_isDarkMode ? "☀️" : "🌙";
|
|
setText(icon);
|
|
|
|
// Style
|
|
QString style = QString(
|
|
"QPushButton {"
|
|
" background-color: %1;"
|
|
" border: 1px solid %2;"
|
|
" border-radius: 14px;"
|
|
" font-size: 16pt;"
|
|
" padding: 0px;"
|
|
"}"
|
|
"QPushButton:hover {"
|
|
" background-color: %3;"
|
|
"}"
|
|
).arg(m_isDarkMode ? "#2B2B2B" : "#FFFFFF")
|
|
.arg(m_isDarkMode ? "#3F3F3F" : "#E1DFDD")
|
|
.arg(m_isDarkMode ? "#3A3A3A" : "#F9F9F9");
|
|
|
|
setStyleSheet(style);
|
|
}
|
|
|
|
void applyTheme()
|
|
{
|
|
if (m_isDarkMode) {
|
|
// Dark theme
|
|
qApp->setStyleSheet("* { background-color: #202020; color: #FFFFFF; }");
|
|
} else {
|
|
// Light theme
|
|
qApp->setStyleSheet("* { background-color: #F3F3F3; color: #000000; }");
|
|
}
|
|
}
|
|
|
|
bool m_isDarkMode;
|
|
};
|
|
|
|
#endif
|