Skip to content

Commit

Permalink
[Qt] Implement theme selection
Browse files Browse the repository at this point in the history
  • Loading branch information
wheremyfoodat committed Sep 30, 2023
1 parent 837bf75 commit 36506e1
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 26 deletions.
11 changes: 8 additions & 3 deletions include/panda_qt/main_window.hpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#pragma once

#include <QApplication>
#include <QComboBox>
#include <QMenuBar>
#include <QPalette>
#include <QPushBUtton>
#include <QtWidgets>

#include "panda_qt/screen.hpp"
Expand All @@ -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);
Expand Down
89 changes: 66 additions & 23 deletions src/panda_qt/main_window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"));

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;
}
}
}

0 comments on commit 36506e1

Please sign in to comment.