From 5b84e58ecb01a3c8de46820f512f5b2c9fff0953 Mon Sep 17 00:00:00 2001 From: wh201906 Date: Sun, 19 Feb 2023 00:18:28 +0800 Subject: [PATCH] UI: More configurable items Code part Fonts, opacity and theme are configurable now Add dark theme for night owls --- src/main.cpp | 31 +++++++++++ src/ui/mainwindow.cpp | 121 +++++++++++++++++++++++++++++++++++++----- src/ui/mainwindow.h | 12 ++++- src/ui/mainwindow.ui | 13 ----- 4 files changed, 150 insertions(+), 27 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 9b6054f..05ab4ef 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -31,6 +31,9 @@ int main(int argc, char *argv[]) QString languageFile = settings->value("extPath").toString(); QString languageName = settings->value("name").toString(); settings->endGroup(); + settings->beginGroup("UI"); + QString theme = settings->value("Theme_Name").toString(); + settings->endGroup(); if(languageName == "") { if(Util::chooseLanguage(settings, &w)) @@ -55,7 +58,35 @@ int main(int argc, char *argv[]) a.installTranslator(translator); else QMessageBox::information(&w, "Error", "Can't load " + languageFile + " as translation file."); + + QFile* themeFile = new QFile(); + QTextStream* themeStream = new QTextStream(); + QString qssString = a.styleSheet(); // default behavior + if(theme == "(none)") + ; + else if(theme == "qdss_dark") + { + themeFile->setFileName(":/qdarkstyle/dark/darkstyle.qss"); + themeFile->open(QFile::ReadOnly | QFile::Text); + themeStream->setDevice(themeFile); + qssString = themeStream->readAll(); + } + else if(theme == "qdss_light") + { + themeFile->setFileName(":/qdarkstyle/light/lightstyle.qss"); + themeFile->open(QFile::ReadOnly | QFile::Text); + themeStream->setDevice(themeFile); + qssString = themeStream->readAll(); + } + a.setStyleSheet(qssString); + delete themeFile; + delete themeStream; + themeFile = nullptr; + themeStream = nullptr; + delete settings; + settings = nullptr; + w.initUI(); w.show(); return a.exec(); diff --git a/src/ui/mainwindow.cpp b/src/ui/mainwindow.cpp index 5de7884..c756b3b 100644 --- a/src/ui/mainwindow.cpp +++ b/src/ui/mainwindow.cpp @@ -528,18 +528,6 @@ void MainWindow::on_MF_trailerDecoderButton_clicked() decDialog->show(); } -void MainWindow::on_MF_fontButton_clicked() -{ - bool isOK = false; - QFont font = QFontDialog::getFont(&isOK, ui->MF_keyWidget->font(), this, tr("Plz select the font of data widget and key widget")); - - if(isOK) - { - ui->MF_keyWidget->setFont(font); - ui->MF_dataWidget->setFont(font); - } -} - void MainWindow::on_MF_dataWidget_itemChanged(QTableWidgetItem *item) { ui->MF_dataWidget->blockSignals(true); @@ -1076,6 +1064,10 @@ void MainWindow::uiInit() ui->MF_keyWidget->installEventFilter(this); ui->MF_dataWidget->installEventFilter(this); + ui->Set_UI_Theme_nameBox->addItem(tr("(None)"), "(none)"); + ui->Set_UI_Theme_nameBox->addItem(tr("Dark"), "qdss_dark"); + ui->Set_UI_Theme_nameBox->addItem(tr("Light"), "qdss_light"); + settings->beginGroup("UI_grpbox_preference"); QStringList boxNames = settings->allKeys(); @@ -1147,6 +1139,49 @@ void MainWindow::uiInit() ui->Set_Client_configFileBox->blockSignals(false); on_Set_Client_configFileBox_currentIndexChanged(ui->Set_Client_configFileBox->currentIndex()); + settings->beginGroup("UI"); + ui->Set_UI_Opacity_Box->setValue(settings->value("Opacity", 100).toInt()); + int themeId = ui->Set_UI_Theme_nameBox->findData(settings->value("Theme_Name", "(none)").toString()); + ui->Set_UI_Theme_nameBox->setCurrentIndex((themeId == -1) ? 0 : themeId); + + // QApplication::font() might return wrong result + // If fonts are not specified in config file, don't touch them. + QString tmpFontName; + int tmpFontSize; + bool fontValid = false, dataFontValid = false, CMDFontValid = false; + tmpFontName = settings->value("Font_Name", "").toString(); + tmpFontSize = settings->value("Font_Size", -1).toInt(); + if(!tmpFontName.isEmpty() && tmpFontSize != -1 && tmpFontName == QFont(tmpFontName).family()) + { + ui->Set_UI_Font_nameBox->setCurrentFont(QFont(tmpFontName)); + ui->Set_UI_Font_sizeBox->setValue(tmpFontSize); + fontValid = true; + } + // The default values should be the same as MF_dataWidget's and MF_keyWidget's. + tmpFontName = settings->value("DataFont_Name", "Consolas").toString(); + tmpFontSize = settings->value("DataFont_Size", 12).toInt(); + if(!tmpFontName.isEmpty() && tmpFontSize != -1 && tmpFontName == QFont(tmpFontName).family()) + { + ui->Set_UI_DataFont_nameBox->setCurrentFont(QFont(tmpFontName)); + ui->Set_UI_DataFont_sizeBox->setValue(tmpFontSize); + dataFontValid = true; + } + tmpFontName = settings->value("CMDFont_Name", "").toString(); + tmpFontSize = settings->value("CMDFont_Size", -1).toInt(); + if(!tmpFontName.isEmpty() && tmpFontSize != -1 && tmpFontName == QFont(tmpFontName).family()) + { + ui->Set_UI_CMDFont_nameBox->setCurrentFont(QFont(tmpFontName)); + ui->Set_UI_CMDFont_sizeBox->setValue(tmpFontSize); + CMDFontValid = true; + } + settings->endGroup(); + + if(fontValid) + on_Set_UI_Font_setButton_clicked(); + if(dataFontValid) + on_Set_UI_DataFont_setButton_clicked(); + if(CMDFontValid) + on_Set_UI_CMDFont_setButton_clicked(); ui->MF_RW_keyTypeBox->addItem("A", Mifare::KEY_A); ui->MF_RW_keyTypeBox->addItem("B", Mifare::KEY_B); @@ -1184,6 +1219,8 @@ void MainWindow::signalInit() connect(ui->MF_sniffGroupBox, &QGroupBox::clicked, this, &MainWindow::on_GroupBox_clicked); connect(stopButton, &QPushButton::clicked, this, &MainWindow::on_stopButton_clicked); + + connect(ui->Set_UI_Opacity_slider, &QSlider::valueChanged, ui->Set_UI_Opacity_Box, &QSpinBox::setValue); } void MainWindow::setStatusBar(QLabel * target, const QString& text) @@ -1496,3 +1533,63 @@ void MainWindow::on_Set_Client_configFileBox_currentIndexChanged(int index) settings->endGroup(); } + +void MainWindow::on_Set_UI_Opacity_Box_valueChanged(int arg1) +{ + ui->Set_UI_Opacity_slider->blockSignals(true); + ui->Set_UI_Opacity_slider->setValue(arg1); + setWindowOpacity(arg1 / 100.0); + settings->beginGroup("UI"); + settings->setValue("Opacity", ui->Set_UI_Opacity_Box->value()); + settings->endGroup(); + ui->Set_UI_Opacity_slider->blockSignals(false); +} + + +void MainWindow::on_Set_UI_Theme_setButton_clicked() +{ + settings->beginGroup("UI"); + settings->setValue("Theme_Name", ui->Set_UI_Theme_nameBox->currentData().toString()); + settings->endGroup(); +} + + +void MainWindow::on_Set_UI_Font_setButton_clicked() +{ + QFont font = ui->Set_UI_Font_nameBox->currentFont(); + font.setPointSize(ui->Set_UI_Font_sizeBox->value()); + QApplication::setFont(font, "QWidget"); + + settings->beginGroup("UI"); + settings->setValue("Font_Name", ui->Set_UI_Font_nameBox->currentFont().family()); + settings->setValue("Font_Size", ui->Set_UI_Font_sizeBox->value()); + settings->endGroup(); +} + + +void MainWindow::on_Set_UI_DataFont_setButton_clicked() +{ + QFont font = ui->Set_UI_DataFont_nameBox->currentFont(); + font.setPointSize(ui->Set_UI_DataFont_sizeBox->value()); + ui->MF_dataWidget->setFont(font); + ui->MF_keyWidget->setFont(font); + + settings->beginGroup("UI"); + settings->setValue("DataFont_Name", ui->Set_UI_DataFont_nameBox->currentFont().family()); + settings->setValue("DataFont_Size", ui->Set_UI_DataFont_sizeBox->value()); + settings->endGroup(); +} + + +void MainWindow::on_Set_UI_CMDFont_setButton_clicked() +{ + QFont font = ui->Set_UI_CMDFont_nameBox->currentFont(); + font.setPointSize(ui->Set_UI_CMDFont_sizeBox->value()); + ui->Raw_outputEdit->setFont(font); + + settings->beginGroup("UI"); + settings->setValue("CMDFont_Name", ui->Set_UI_CMDFont_nameBox->currentFont().family()); + settings->setValue("CMDFont_Size", ui->Set_UI_CMDFont_sizeBox->value()); + settings->endGroup(); +} + diff --git a/src/ui/mainwindow.h b/src/ui/mainwindow.h index 82cd37d..b66915b 100644 --- a/src/ui/mainwindow.h +++ b/src/ui/mainwindow.h @@ -128,8 +128,6 @@ private slots: void on_MF_keyWidget_itemChanged(QTableWidgetItem *item); - void on_MF_fontButton_clicked(); - void on_MF_UID_wipeButton_clicked(); void on_MF_UID_aboutUIDButton_clicked(); @@ -212,6 +210,16 @@ private slots: void on_Set_Client_configFileBox_currentIndexChanged(int index); + void on_Set_UI_Opacity_Box_valueChanged(int arg1); + + void on_Set_UI_Theme_setButton_clicked(); + + void on_Set_UI_Font_setButton_clicked(); + + void on_Set_UI_DataFont_setButton_clicked(); + + void on_Set_UI_CMDFont_setButton_clicked(); + private: Ui::MainWindow* ui; QButtonGroup* MFCardTypeBtnGroup; diff --git a/src/ui/mainwindow.ui b/src/ui/mainwindow.ui index b94e633..ca2a943 100644 --- a/src/ui/mainwindow.ui +++ b/src/ui/mainwindow.ui @@ -315,19 +315,6 @@ - - - - - 40 - 0 - - - - Set Fonts - - -