From 5a62bd6dc791a6714e323409987024f1e007623f Mon Sep 17 00:00:00 2001 From: optizone8 <42874998+optizone8@users.noreply.github.com> Date: Sun, 11 Nov 2018 12:11:50 +0000 Subject: [PATCH] relevant themes(#816) and delete improvement (#846) * change constants to enum * code improvements * remove redundant lambda usage * add themes * bug fix * bug fix 2 * first start bug fix * big refactoring * merge conflict fixes * removes debug information --- radare2 | 2 +- src/common/Configuration.cpp | 33 +++++-- src/common/Configuration.h | 16 +++- .../preferences/AppearanceOptionsWidget.cpp | 87 ++++++++++++++----- src/widgets/ColorSchemePrefWidget.cpp | 12 +-- src/widgets/ColorSchemePrefWidget.h | 2 +- 6 files changed, 113 insertions(+), 39 deletions(-) diff --git a/radare2 b/radare2 index 35a5c42a..0c0fee7b 160000 --- a/radare2 +++ b/radare2 @@ -1 +1 @@ -Subproject commit 35a5c42a52cff8524f065c205e3f1982302c583d +Subproject commit 0c0fee7b48283643a9ba9e855a32e2503f88a63e diff --git a/src/common/Configuration.cpp b/src/common/Configuration.cpp index 95d55ccd..b2293d42 100644 --- a/src/common/Configuration.cpp +++ b/src/common/Configuration.cpp @@ -8,6 +8,11 @@ #include "common/ColorSchemeFileSaver.h" +const QList kCutterQtThemesList = { + { "Default", LightFlag }, + { "Dark", DarkFlag } +}; + Configuration *Configuration::mPtr = nullptr; /*! @@ -226,22 +231,33 @@ void Configuration::setFont(const QFont &font) emit fontsUpdated(); } +QString Configuration::getLastThemeOf(const CutterQtTheme &currQtTheme) const +{ + return s.value("lastThemeOf." + currQtTheme.name, + Config()->getCurrentTheme()).toString(); +} + void Configuration::setTheme(int theme) { + if (theme >= kCutterQtThemesList.size() || + theme < 0) { + theme = 0; + } s.setValue("ColorPalette", theme); - switch (theme) { - case 0: + QString themeName = kCutterQtThemesList[theme].name; + + if (themeName == "Default") { loadDefaultTheme(); - break; - case 1: + } else if (themeName == "Dark") { loadDarkTheme(); - break; - default: + } else { loadDefaultTheme(); } + emit colorsUpdated(); } + QString Configuration::getLogoFile() { return logoFile; @@ -257,6 +273,11 @@ void Configuration::setColor(const QString &name, const QColor &color) s.setValue("colors." + name, color); } +void Configuration::setLastThemeOf(const CutterQtTheme &currQtTheme, const QString &theme) +{ + s.setValue("lastThemeOf." + currQtTheme.name, theme); +} + const QColor Configuration::getColor(const QString &name) const { if (s.contains("colors." + name)) { diff --git a/src/common/Configuration.h b/src/common/Configuration.h index a3c396d1..8a9bb4f2 100644 --- a/src/common/Configuration.h +++ b/src/common/Configuration.h @@ -8,6 +8,18 @@ #define Config() (Configuration::instance()) #define ConfigColor(x) Config()->getColor(x) +enum ColorFlags { + LightFlag = 1, + DarkFlag = 2 +}; + +struct CutterQtTheme { + QString name; + ColorFlags flag; +}; + +extern const QList kCutterQtThemesList; + class Configuration : public QObject { Q_OBJECT @@ -45,11 +57,13 @@ public: void setFont(const QFont &font); // Colors + void setLastThemeOf(const CutterQtTheme &currQtTheme, const QString& theme); + QString getLastThemeOf(const CutterQtTheme &currQtTheme) const; const QColor getColor(const QString &name) const; void setTheme(int theme); int getTheme() { - return s.value("ColorPalette").toInt(); + return s.value("ColorPalette", 0).toInt(); } QString getDirProjects(); diff --git a/src/dialogs/preferences/AppearanceOptionsWidget.cpp b/src/dialogs/preferences/AppearanceOptionsWidget.cpp index b19198ad..c4922cd3 100644 --- a/src/dialogs/preferences/AppearanceOptionsWidget.cpp +++ b/src/dialogs/preferences/AppearanceOptionsWidget.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include #include "PreferencesDialog.h" @@ -15,13 +16,26 @@ #include "common/ColorSchemeFileSaver.h" #include "widgets/ColorSchemePrefWidget.h" +static const QHash kRelevantSchemes = { + { "ayu", DarkFlag }, + { "consonance", DarkFlag }, + { "darkda", DarkFlag }, + { "onedark", DarkFlag }, + { "solarized", DarkFlag }, + { "zenburn", DarkFlag }, + { "cutter", LightFlag }, + { "dark", LightFlag }, + { "matrix", LightFlag }, + { "tango", LightFlag }, + { "white", LightFlag } +}; + QStringList findLanguages() { QDir dir(QCoreApplication::applicationDirPath() + QDir::separator() + "translations"); QStringList fileNames = dir.entryList(QStringList("cutter_*.qm"), QDir::Files, QDir::Name); - QStringList languages; QString currLanguageName; auto allLocales = QLocale::matchingLocales(QLocale::AnyLanguage, QLocale::AnyScript, @@ -43,6 +57,7 @@ QStringList findLanguages() return languages << "English"; } + AppearanceOptionsWidget::AppearanceOptionsWidget(PreferencesDialog *dialog, QWidget *parent) : QDialog(parent), ui(new Ui::AppearanceOptionsWidget) @@ -69,11 +84,6 @@ AppearanceOptionsWidget::AppearanceOptionsWidget(PreferencesDialog *dialog, QWid connect(Config(), &Configuration::fontsUpdated, this, &AppearanceOptionsWidget::updateFontFromConfig); - connect(ui.get()->colorComboBox, &QComboBox::currentTextChanged, [&](const QString & name) { - static_cast(ui.get()->colorSchemePrefWidget)->setNewScheme(name); - }); - static_cast - (ui.get()->colorSchemePrefWidget)->setNewScheme(Config()->getCurrentTheme()); } AppearanceOptionsWidget::~AppearanceOptionsWidget() {} @@ -87,18 +97,38 @@ void AppearanceOptionsWidget::updateFontFromConfig() void AppearanceOptionsWidget::updateThemeFromConfig() { // Disconnect currentIndexChanged because clearing the comboxBox and refiling it causes its index to change. - disconnect(ui->colorComboBox, SIGNAL(currentIndexChanged(int)), this, - SLOT(on_colorComboBox_currentIndexChanged(int))); - ui->themeComboBox->setCurrentIndex(Config()->getTheme()); + QSignalBlocker signalBlockerColorBox(ui->colorComboBox); + QSignalBlocker signalBlockerThemeBox(ui->themeComboBox); + ui->themeComboBox->clear(); + for (auto &it : kCutterQtThemesList) { + ui->themeComboBox->addItem(it.name); + } + uint curQtThemeIndex = Config()->getTheme(); + if (curQtThemeIndex >= kCutterQtThemesList.size()) { + curQtThemeIndex = 0; + Config()->setTheme(curQtThemeIndex); + } + ui->themeComboBox->setCurrentIndex(curQtThemeIndex); QList themes = Core()->getColorThemes(); ui->colorComboBox->clear(); - ui->colorComboBox->addItem("default"); - for (QString str : themes) - ui->colorComboBox->addItem(str); - QString curTheme = Config()->getCurrentTheme(); - int index = themes.indexOf(curTheme) + 1; + for (QString theme : themes) { + if (ColorSchemeFileWorker().isCustomScheme(theme) || + (kCutterQtThemesList[curQtThemeIndex].flag & kRelevantSchemes[theme])) { + ui->colorComboBox->addItem(theme); + } + } + + QString curTheme = Config()->getLastThemeOf(kCutterQtThemesList[curQtThemeIndex]); + int index = ui->colorComboBox->findText(curTheme); + if (index == -1) { + index = 0; + } + ui->colorComboBox->setCurrentIndex(index); + curTheme = ui->colorComboBox->currentText(); + Config()->setColorTheme(curTheme); + ui->colorSchemePrefWidget->updateSchemeFromConfig(); int maxThemeLen = 0; for (QString str : themes) { int strLen = str.length(); @@ -108,8 +138,6 @@ void AppearanceOptionsWidget::updateThemeFromConfig() } ui->colorComboBox->setMinimumContentsLength(maxThemeLen); ui->colorComboBox->setSizeAdjustPolicy(QComboBox::AdjustToMinimumContentsLength); - connect(ui->colorComboBox, SIGNAL(currentIndexChanged(int)), this, - SLOT(on_colorComboBox_currentIndexChanged(int))); } void AppearanceOptionsWidget::on_fontSelectionButton_clicked() @@ -125,15 +153,23 @@ void AppearanceOptionsWidget::on_fontSelectionButton_clicked() void AppearanceOptionsWidget::on_themeComboBox_currentIndexChanged(int index) { - //disconnect(Config(), SIGNAL(colorsUpdated()), this, SLOT(updateThemeFromConfig())); Config()->setTheme(index); - //connect(Config(), SIGNAL(colorsUpdated()), this, SLOT(updateThemeFromConfig())); + updateThemeFromConfig(); } void AppearanceOptionsWidget::on_colorComboBox_currentIndexChanged(int index) { QString theme = ui->colorComboBox->itemText(index); + + uint curQtThemeIndex = Config()->getTheme(); + if (curQtThemeIndex >= kCutterQtThemesList.size()) { + curQtThemeIndex = 0; + Config()->setTheme(curQtThemeIndex); + } + + Config()->setLastThemeOf(kCutterQtThemesList[curQtThemeIndex], theme); Config()->setColorTheme(theme); + ui->colorSchemePrefWidget->updateSchemeFromConfig(); } void AppearanceOptionsWidget::on_copyButton_clicked() @@ -149,14 +185,23 @@ void AppearanceOptionsWidget::on_copyButton_clicked() return; ColorSchemeFileWorker().copy(Config()->getCurrentTheme(), newSchemeName); Config()->setColorTheme(newSchemeName); - ui.get()->colorSchemePrefWidget->setNewScheme(newSchemeName); + ui->colorSchemePrefWidget->updateSchemeFromConfig(); updateThemeFromConfig(); - ui.get()->colorComboBox->setCurrentIndex(ui.get()->colorComboBox->findText(newSchemeName)); } void AppearanceOptionsWidget::on_deleteButton_clicked() { - ColorSchemeFileWorker().deleteScheme(Config()->getCurrentTheme()); + if (ColorSchemeFileWorker().isCustomScheme(Config()->getCurrentTheme())) { + QMessageBox mb; + mb.setWindowTitle(tr("Delete")); + mb.setText(tr("Are you sure you want to delete theme ") + Config()->getCurrentTheme()); + mb.setIcon(QMessageBox::Question); + mb.setStandardButtons(QMessageBox::Yes | QMessageBox::No); + if (mb.exec() == QMessageBox::Yes) { + ColorSchemeFileWorker().deleteScheme(Config()->getCurrentTheme()); + updateThemeFromConfig(); + } + } } void AppearanceOptionsWidget::onLanguageComboBoxCurrentIndexChanged(int index) diff --git a/src/widgets/ColorSchemePrefWidget.cpp b/src/widgets/ColorSchemePrefWidget.cpp index 304153cd..3aa2ae13 100644 --- a/src/widgets/ColorSchemePrefWidget.cpp +++ b/src/widgets/ColorSchemePrefWidget.cpp @@ -442,13 +442,7 @@ void ColorSchemePrefWidget::newColor() static_cast(ui->preferencesListView->model())->setColor(currCO.optionName, d.selectedColor()); - - static_cast(QObject::sender())->setColor(d.selectedColor()); - if (currCO.optionName == standardBackgroundOptionName) { - static_cast(ui->preferencesListView)->setStandardColors(); - } else { - static_cast(ui->preferencesListView)->setStandardColors(); - } + ui->preferencesListView->setStandardColors(); } void ColorSchemePrefWidget::indexChanged(const QModelIndex &ni) @@ -456,9 +450,9 @@ void ColorSchemePrefWidget::indexChanged(const QModelIndex &ni) ui->colorViewFore->setColor(ni.data(Qt::UserRole).value().color); } -void ColorSchemePrefWidget::setNewScheme(const QString &schemeName) +void ColorSchemePrefWidget::updateSchemeFromConfig() { - isEditable = ColorSchemeFileWorker().isCustomScheme(schemeName); + isEditable = ColorSchemeFileWorker().isCustomScheme(Config()->getCurrentTheme()); static_cast(ui->preferencesListView->model())->updateScheme(); } diff --git a/src/widgets/ColorSchemePrefWidget.h b/src/widgets/ColorSchemePrefWidget.h index ea02125f..daf81d25 100644 --- a/src/widgets/ColorSchemePrefWidget.h +++ b/src/widgets/ColorSchemePrefWidget.h @@ -21,7 +21,7 @@ public: public slots: void apply(); - void setNewScheme(const QString &schemeName); + void updateSchemeFromConfig(); private slots: void newColor();