From 36506e18bb300a1786bd3692a0316bf4746a466d Mon Sep 17 00:00:00 2001 From: wheremyfoodat <44909372+wheremyfoodat@users.noreply.github.com> Date: Sat, 30 Sep 2023 18:35:50 +0300 Subject: [PATCH] [Qt] Implement theme selection --- include/panda_qt/main_window.hpp | 11 ++-- src/panda_qt/main_window.cpp | 89 +++++++++++++++++++++++--------- 2 files changed, 74 insertions(+), 26 deletions(-) diff --git a/include/panda_qt/main_window.hpp b/include/panda_qt/main_window.hpp index 668b17511..ebd2407ed 100644 --- a/include/panda_qt/main_window.hpp +++ b/include/panda_qt/main_window.hpp @@ -1,9 +1,9 @@ #pragma once #include +#include #include #include -#include #include #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); diff --git a/src/panda_qt/main_window.cpp b/src/panda_qt/main_window.cpp index 2c3b83a59..e32290c83 100644 --- a/src/panda_qt/main_window.cpp +++ b/src/panda_qt/main_window.cpp @@ -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(currentTheme)); + + themeSelect->setGeometry(40, 40, 100, 50); + themeSelect->show(); + connect(themeSelect, &QComboBox::currentIndexChanged, this, [&](int index) { setTheme(static_cast(index)); }); } MainWindow::~MainWindow() { delete menuBar; } -void MainWindow::setDarkTheme() { - QApplication::setStyle(QStyleFactory::create("Fusion")); - - 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); +void MainWindow::setTheme(Theme theme) { + currentTheme = theme; + + switch (theme) { + case Theme::Dark: { + QApplication::setStyle(QStyleFactory::create("Fusion")); + + 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; + } + } } \ No newline at end of file