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
This commit is contained in:
optizone8 2018-11-11 12:11:50 +00:00 committed by Itay Cohen
parent bd82f2cf45
commit 5a62bd6dc7
6 changed files with 113 additions and 39 deletions

@ -1 +1 @@
Subproject commit 35a5c42a52cff8524f065c205e3f1982302c583d Subproject commit 0c0fee7b48283643a9ba9e855a32e2503f88a63e

View File

@ -8,6 +8,11 @@
#include "common/ColorSchemeFileSaver.h" #include "common/ColorSchemeFileSaver.h"
const QList<CutterQtTheme> kCutterQtThemesList = {
{ "Default", LightFlag },
{ "Dark", DarkFlag }
};
Configuration *Configuration::mPtr = nullptr; Configuration *Configuration::mPtr = nullptr;
/*! /*!
@ -226,22 +231,33 @@ void Configuration::setFont(const QFont &font)
emit fontsUpdated(); emit fontsUpdated();
} }
QString Configuration::getLastThemeOf(const CutterQtTheme &currQtTheme) const
{
return s.value("lastThemeOf." + currQtTheme.name,
Config()->getCurrentTheme()).toString();
}
void Configuration::setTheme(int theme) void Configuration::setTheme(int theme)
{ {
if (theme >= kCutterQtThemesList.size() ||
theme < 0) {
theme = 0;
}
s.setValue("ColorPalette", theme); s.setValue("ColorPalette", theme);
switch (theme) { QString themeName = kCutterQtThemesList[theme].name;
case 0:
if (themeName == "Default") {
loadDefaultTheme(); loadDefaultTheme();
break; } else if (themeName == "Dark") {
case 1:
loadDarkTheme(); loadDarkTheme();
break; } else {
default:
loadDefaultTheme(); loadDefaultTheme();
} }
emit colorsUpdated(); emit colorsUpdated();
} }
QString Configuration::getLogoFile() QString Configuration::getLogoFile()
{ {
return logoFile; return logoFile;
@ -257,6 +273,11 @@ void Configuration::setColor(const QString &name, const QColor &color)
s.setValue("colors." + name, 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 const QColor Configuration::getColor(const QString &name) const
{ {
if (s.contains("colors." + name)) { if (s.contains("colors." + name)) {

View File

@ -8,6 +8,18 @@
#define Config() (Configuration::instance()) #define Config() (Configuration::instance())
#define ConfigColor(x) Config()->getColor(x) #define ConfigColor(x) Config()->getColor(x)
enum ColorFlags {
LightFlag = 1,
DarkFlag = 2
};
struct CutterQtTheme {
QString name;
ColorFlags flag;
};
extern const QList<CutterQtTheme> kCutterQtThemesList;
class Configuration : public QObject class Configuration : public QObject
{ {
Q_OBJECT Q_OBJECT
@ -45,11 +57,13 @@ public:
void setFont(const QFont &font); void setFont(const QFont &font);
// Colors // Colors
void setLastThemeOf(const CutterQtTheme &currQtTheme, const QString& theme);
QString getLastThemeOf(const CutterQtTheme &currQtTheme) const;
const QColor getColor(const QString &name) const; const QColor getColor(const QString &name) const;
void setTheme(int theme); void setTheme(int theme);
int getTheme() int getTheme()
{ {
return s.value("ColorPalette").toInt(); return s.value("ColorPalette", 0).toInt();
} }
QString getDirProjects(); QString getDirProjects();

View File

@ -3,6 +3,7 @@
#include <QFontDialog> #include <QFontDialog>
#include <QTranslator> #include <QTranslator>
#include <QInputDialog> #include <QInputDialog>
#include <QSignalBlocker>
#include <QComboBox> #include <QComboBox>
#include "PreferencesDialog.h" #include "PreferencesDialog.h"
@ -15,13 +16,26 @@
#include "common/ColorSchemeFileSaver.h" #include "common/ColorSchemeFileSaver.h"
#include "widgets/ColorSchemePrefWidget.h" #include "widgets/ColorSchemePrefWidget.h"
static const QHash<QString, ColorFlags> 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() QStringList findLanguages()
{ {
QDir dir(QCoreApplication::applicationDirPath() + QDir::separator() + QDir dir(QCoreApplication::applicationDirPath() + QDir::separator() +
"translations"); "translations");
QStringList fileNames = dir.entryList(QStringList("cutter_*.qm"), QDir::Files, QStringList fileNames = dir.entryList(QStringList("cutter_*.qm"), QDir::Files,
QDir::Name); QDir::Name);
QStringList languages; QStringList languages;
QString currLanguageName; QString currLanguageName;
auto allLocales = QLocale::matchingLocales(QLocale::AnyLanguage, QLocale::AnyScript, auto allLocales = QLocale::matchingLocales(QLocale::AnyLanguage, QLocale::AnyScript,
@ -43,6 +57,7 @@ QStringList findLanguages()
return languages << "English"; return languages << "English";
} }
AppearanceOptionsWidget::AppearanceOptionsWidget(PreferencesDialog *dialog, QWidget *parent) AppearanceOptionsWidget::AppearanceOptionsWidget(PreferencesDialog *dialog, QWidget *parent)
: QDialog(parent), : QDialog(parent),
ui(new Ui::AppearanceOptionsWidget) ui(new Ui::AppearanceOptionsWidget)
@ -69,11 +84,6 @@ AppearanceOptionsWidget::AppearanceOptionsWidget(PreferencesDialog *dialog, QWid
connect(Config(), &Configuration::fontsUpdated, this, connect(Config(), &Configuration::fontsUpdated, this,
&AppearanceOptionsWidget::updateFontFromConfig); &AppearanceOptionsWidget::updateFontFromConfig);
connect(ui.get()->colorComboBox, &QComboBox::currentTextChanged, [&](const QString & name) {
static_cast<ColorSchemePrefWidget *>(ui.get()->colorSchemePrefWidget)->setNewScheme(name);
});
static_cast<ColorSchemePrefWidget *>
(ui.get()->colorSchemePrefWidget)->setNewScheme(Config()->getCurrentTheme());
} }
AppearanceOptionsWidget::~AppearanceOptionsWidget() {} AppearanceOptionsWidget::~AppearanceOptionsWidget() {}
@ -87,18 +97,38 @@ void AppearanceOptionsWidget::updateFontFromConfig()
void AppearanceOptionsWidget::updateThemeFromConfig() void AppearanceOptionsWidget::updateThemeFromConfig()
{ {
// Disconnect currentIndexChanged because clearing the comboxBox and refiling it causes its index to change. // Disconnect currentIndexChanged because clearing the comboxBox and refiling it causes its index to change.
disconnect(ui->colorComboBox, SIGNAL(currentIndexChanged(int)), this, QSignalBlocker signalBlockerColorBox(ui->colorComboBox);
SLOT(on_colorComboBox_currentIndexChanged(int))); QSignalBlocker signalBlockerThemeBox(ui->themeComboBox);
ui->themeComboBox->setCurrentIndex(Config()->getTheme()); 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<QString> themes = Core()->getColorThemes(); QList<QString> themes = Core()->getColorThemes();
ui->colorComboBox->clear(); ui->colorComboBox->clear();
ui->colorComboBox->addItem("default"); for (QString theme : themes) {
for (QString str : themes) if (ColorSchemeFileWorker().isCustomScheme(theme) ||
ui->colorComboBox->addItem(str); (kCutterQtThemesList[curQtThemeIndex].flag & kRelevantSchemes[theme])) {
QString curTheme = Config()->getCurrentTheme(); ui->colorComboBox->addItem(theme);
int index = themes.indexOf(curTheme) + 1; }
}
QString curTheme = Config()->getLastThemeOf(kCutterQtThemesList[curQtThemeIndex]);
int index = ui->colorComboBox->findText(curTheme);
if (index == -1) {
index = 0;
}
ui->colorComboBox->setCurrentIndex(index); ui->colorComboBox->setCurrentIndex(index);
curTheme = ui->colorComboBox->currentText();
Config()->setColorTheme(curTheme);
ui->colorSchemePrefWidget->updateSchemeFromConfig();
int maxThemeLen = 0; int maxThemeLen = 0;
for (QString str : themes) { for (QString str : themes) {
int strLen = str.length(); int strLen = str.length();
@ -108,8 +138,6 @@ void AppearanceOptionsWidget::updateThemeFromConfig()
} }
ui->colorComboBox->setMinimumContentsLength(maxThemeLen); ui->colorComboBox->setMinimumContentsLength(maxThemeLen);
ui->colorComboBox->setSizeAdjustPolicy(QComboBox::AdjustToMinimumContentsLength); ui->colorComboBox->setSizeAdjustPolicy(QComboBox::AdjustToMinimumContentsLength);
connect(ui->colorComboBox, SIGNAL(currentIndexChanged(int)), this,
SLOT(on_colorComboBox_currentIndexChanged(int)));
} }
void AppearanceOptionsWidget::on_fontSelectionButton_clicked() void AppearanceOptionsWidget::on_fontSelectionButton_clicked()
@ -125,15 +153,23 @@ void AppearanceOptionsWidget::on_fontSelectionButton_clicked()
void AppearanceOptionsWidget::on_themeComboBox_currentIndexChanged(int index) void AppearanceOptionsWidget::on_themeComboBox_currentIndexChanged(int index)
{ {
//disconnect(Config(), SIGNAL(colorsUpdated()), this, SLOT(updateThemeFromConfig()));
Config()->setTheme(index); Config()->setTheme(index);
//connect(Config(), SIGNAL(colorsUpdated()), this, SLOT(updateThemeFromConfig())); updateThemeFromConfig();
} }
void AppearanceOptionsWidget::on_colorComboBox_currentIndexChanged(int index) void AppearanceOptionsWidget::on_colorComboBox_currentIndexChanged(int index)
{ {
QString theme = ui->colorComboBox->itemText(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); Config()->setColorTheme(theme);
ui->colorSchemePrefWidget->updateSchemeFromConfig();
} }
void AppearanceOptionsWidget::on_copyButton_clicked() void AppearanceOptionsWidget::on_copyButton_clicked()
@ -149,14 +185,23 @@ void AppearanceOptionsWidget::on_copyButton_clicked()
return; return;
ColorSchemeFileWorker().copy(Config()->getCurrentTheme(), newSchemeName); ColorSchemeFileWorker().copy(Config()->getCurrentTheme(), newSchemeName);
Config()->setColorTheme(newSchemeName); Config()->setColorTheme(newSchemeName);
ui.get()->colorSchemePrefWidget->setNewScheme(newSchemeName); ui->colorSchemePrefWidget->updateSchemeFromConfig();
updateThemeFromConfig(); updateThemeFromConfig();
ui.get()->colorComboBox->setCurrentIndex(ui.get()->colorComboBox->findText(newSchemeName));
} }
void AppearanceOptionsWidget::on_deleteButton_clicked() 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) void AppearanceOptionsWidget::onLanguageComboBoxCurrentIndexChanged(int index)

View File

@ -442,13 +442,7 @@ void ColorSchemePrefWidget::newColor()
static_cast<ColorSettingsModel *>(ui->preferencesListView->model())->setColor(currCO.optionName, static_cast<ColorSettingsModel *>(ui->preferencesListView->model())->setColor(currCO.optionName,
d.selectedColor()); d.selectedColor());
ui->preferencesListView->setStandardColors();
static_cast<ColorViewButton *>(QObject::sender())->setColor(d.selectedColor());
if (currCO.optionName == standardBackgroundOptionName) {
static_cast<PreferencesListView *>(ui->preferencesListView)->setStandardColors();
} else {
static_cast<PreferencesListView *>(ui->preferencesListView)->setStandardColors();
}
} }
void ColorSchemePrefWidget::indexChanged(const QModelIndex &ni) void ColorSchemePrefWidget::indexChanged(const QModelIndex &ni)
@ -456,9 +450,9 @@ void ColorSchemePrefWidget::indexChanged(const QModelIndex &ni)
ui->colorViewFore->setColor(ni.data(Qt::UserRole).value<ColorOption>().color); ui->colorViewFore->setColor(ni.data(Qt::UserRole).value<ColorOption>().color);
} }
void ColorSchemePrefWidget::setNewScheme(const QString &schemeName) void ColorSchemePrefWidget::updateSchemeFromConfig()
{ {
isEditable = ColorSchemeFileWorker().isCustomScheme(schemeName); isEditable = ColorSchemeFileWorker().isCustomScheme(Config()->getCurrentTheme());
static_cast<ColorSettingsModel *>(ui->preferencesListView->model())->updateScheme(); static_cast<ColorSettingsModel *>(ui->preferencesListView->model())->updateScheme();
} }

View File

@ -21,7 +21,7 @@ public:
public slots: public slots:
void apply(); void apply();
void setNewScheme(const QString &schemeName); void updateSchemeFromConfig();
private slots: private slots:
void newColor(); void newColor();