[Qt] Implement theme selection

This commit is contained in:
wheremyfoodat 2023-09-30 18:35:50 +03:00
parent 837bf7524f
commit 36506e18bb
2 changed files with 72 additions and 24 deletions

View file

@ -1,9 +1,9 @@
#pragma once
#include <QApplication>
#include <QComboBox>
#include <QMenuBar>
#include <QPalette>
#include <QPushBUtton>
#include <QtWidgets>
#include "panda_qt/screen.hpp"
@ -12,11 +12,16 @@ class MainWindow : public QMainWindow {
Q_OBJECT
private:
enum class Theme : int {
System = 0, Light = 1, Dark = 2,
};
QComboBox* themeSelect = nullptr;
QMenuBar* menuBar = nullptr;
QPushButton* themeButton = nullptr;
ScreenWidget screen;
void setDarkTheme();
Theme currentTheme;
void setTheme(Theme theme);
public:
MainWindow(QApplication* app, QWidget* parent = nullptr);

View file

@ -14,31 +14,74 @@ MainWindow::MainWindow(QApplication* app, QWidget* parent) : QMainWindow(parent)
auto pandaMenu = menuBar->addMenu(tr("PANDA"));
auto pandaAction = pandaMenu->addAction(tr("panda..."));
auto themeButton = new QPushButton(tr("Dark theme"), this);
themeButton->setGeometry(40, 40, 100, 400);
themeButton->show();
connect(themeButton, &QPushButton::pressed, this, [&]() { setDarkTheme(); });
// Set up theme selection
setTheme(Theme::Dark);
themeSelect = new QComboBox(this);
themeSelect->addItem("System");
themeSelect->addItem("Light");
themeSelect->addItem("Dark");
themeSelect->setCurrentIndex(static_cast<int>(currentTheme));
themeSelect->setGeometry(40, 40, 100, 50);
themeSelect->show();
connect(themeSelect, &QComboBox::currentIndexChanged, this, [&](int index) { setTheme(static_cast<Theme>(index)); });
}
MainWindow::~MainWindow() { delete menuBar; }
void MainWindow::setDarkTheme() {
QApplication::setStyle(QStyleFactory::create("Fusion"));
void MainWindow::setTheme(Theme theme) {
currentTheme = theme;
QPalette p;
p.setColor(QPalette::Window, QColor(53, 53, 53));
p.setColor(QPalette::WindowText, Qt::white);
p.setColor(QPalette::Base, QColor(25, 25, 25));
p.setColor(QPalette::AlternateBase, QColor(53, 53, 53));
p.setColor(QPalette::ToolTipBase, Qt::white);
p.setColor(QPalette::ToolTipText, Qt::white);
p.setColor(QPalette::Text, Qt::white);
p.setColor(QPalette::Button, QColor(53, 53, 53));
p.setColor(QPalette::ButtonText, Qt::white);
p.setColor(QPalette::BrightText, Qt::red);
p.setColor(QPalette::Link, QColor(42, 130, 218));
switch (theme) {
case Theme::Dark: {
QApplication::setStyle(QStyleFactory::create("Fusion"));
p.setColor(QPalette::Highlight, QColor(42, 130, 218));
p.setColor(QPalette::HighlightedText, Qt::black);
qApp->setPalette(p);
QPalette p;
p.setColor(QPalette::Window, QColor(53, 53, 53));
p.setColor(QPalette::WindowText, Qt::white);
p.setColor(QPalette::Base, QColor(25, 25, 25));
p.setColor(QPalette::AlternateBase, QColor(53, 53, 53));
p.setColor(QPalette::ToolTipBase, Qt::white);
p.setColor(QPalette::ToolTipText, Qt::white);
p.setColor(QPalette::Text, Qt::white);
p.setColor(QPalette::Button, QColor(53, 53, 53));
p.setColor(QPalette::ButtonText, Qt::white);
p.setColor(QPalette::BrightText, Qt::red);
p.setColor(QPalette::Link, QColor(42, 130, 218));
p.setColor(QPalette::Highlight, QColor(42, 130, 218));
p.setColor(QPalette::HighlightedText, Qt::black);
qApp->setPalette(p);
break;
}
case Theme::Light: {
QApplication::setStyle(QStyleFactory::create("Fusion"));
QPalette p;
p.setColor(QPalette::Window, Qt::white);
p.setColor(QPalette::WindowText, Qt::black);
p.setColor(QPalette::Base, QColor(243, 243, 243));
p.setColor(QPalette::AlternateBase, Qt::white);
p.setColor(QPalette::ToolTipBase, Qt::black);
p.setColor(QPalette::ToolTipText, Qt::black);
p.setColor(QPalette::Text, Qt::black);
p.setColor(QPalette::Button, Qt::white);
p.setColor(QPalette::ButtonText, Qt::black);
p.setColor(QPalette::BrightText, Qt::red);
p.setColor(QPalette::Link, QColor(42, 130, 218));
p.setColor(QPalette::Highlight, QColor(42, 130, 218));
p.setColor(QPalette::HighlightedText, Qt::white);
qApp->setPalette(p);
break;
}
case Theme::System: {
qApp->setPalette(this->style()->standardPalette());
qApp->setStyle(QStyleFactory::create("WindowsVista"));
qApp->setStyleSheet("");
break;
}
}
}