2018-10-10 09:37:24 +00:00
|
|
|
#include <QDir>
|
2017-12-14 15:14:33 +00:00
|
|
|
#include <QLabel>
|
|
|
|
#include <QFontDialog>
|
2018-10-31 16:07:53 +00:00
|
|
|
#include <QTranslator>
|
2018-10-10 09:37:24 +00:00
|
|
|
#include <QInputDialog>
|
2018-11-11 12:11:50 +00:00
|
|
|
#include <QSignalBlocker>
|
2017-12-14 15:14:33 +00:00
|
|
|
|
2018-02-26 22:25:23 +00:00
|
|
|
#include <QComboBox>
|
2017-12-14 15:14:33 +00:00
|
|
|
#include "PreferencesDialog.h"
|
2018-10-17 07:30:56 +00:00
|
|
|
#include "AppearanceOptionsWidget.h"
|
|
|
|
#include "ui_AppearanceOptionsWidget.h"
|
2017-12-14 15:14:33 +00:00
|
|
|
|
2018-10-17 07:55:53 +00:00
|
|
|
#include "common/Helpers.h"
|
|
|
|
#include "common/Configuration.h"
|
2017-12-14 15:14:33 +00:00
|
|
|
|
2018-10-17 07:55:53 +00:00
|
|
|
#include "common/ColorSchemeFileSaver.h"
|
2018-10-10 09:37:24 +00:00
|
|
|
#include "widgets/ColorSchemePrefWidget.h"
|
|
|
|
|
2018-11-11 12:11:50 +00:00
|
|
|
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 }
|
|
|
|
};
|
|
|
|
|
2018-10-31 16:07:53 +00:00
|
|
|
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,
|
|
|
|
QLocale::AnyCountry);
|
|
|
|
|
|
|
|
for (auto i : fileNames) {
|
|
|
|
QString localeName = i.mid(sizeof("cutter_") - 1, 2);
|
|
|
|
for (auto j : allLocales) {
|
|
|
|
if (j.name().startsWith(localeName)) {
|
|
|
|
currLanguageName = j.nativeLanguageName();
|
|
|
|
currLanguageName = currLanguageName.at(0).toUpper() +
|
|
|
|
currLanguageName.right(currLanguageName.length() - 1);
|
|
|
|
languages << currLanguageName;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return languages << "English";
|
|
|
|
}
|
|
|
|
|
2018-11-11 12:11:50 +00:00
|
|
|
|
2018-10-17 07:30:56 +00:00
|
|
|
AppearanceOptionsWidget::AppearanceOptionsWidget(PreferencesDialog *dialog, QWidget *parent)
|
2018-03-21 20:32:32 +00:00
|
|
|
: QDialog(parent),
|
2018-10-17 07:30:56 +00:00
|
|
|
ui(new Ui::AppearanceOptionsWidget)
|
2017-12-14 15:14:33 +00:00
|
|
|
{
|
2018-07-18 10:15:10 +00:00
|
|
|
Q_UNUSED(dialog);
|
2017-12-14 15:14:33 +00:00
|
|
|
ui->setupUi(this);
|
|
|
|
|
|
|
|
updateFontFromConfig();
|
|
|
|
updateThemeFromConfig();
|
|
|
|
|
2018-10-31 16:07:53 +00:00
|
|
|
QStringList langs = findLanguages();
|
|
|
|
ui->languageComboBox->addItems(langs);
|
|
|
|
|
|
|
|
QString curr = Config()->getCurrLocale().nativeLanguageName();
|
|
|
|
curr = curr.at(0).toUpper() + curr.right(curr.length() - 1);
|
|
|
|
if (!langs.contains(curr)) {
|
|
|
|
curr = "English";
|
|
|
|
}
|
|
|
|
ui->languageComboBox->setCurrentText(curr);
|
|
|
|
connect(ui->languageComboBox,
|
|
|
|
static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged),
|
|
|
|
this,
|
|
|
|
&AppearanceOptionsWidget::onLanguageComboBoxCurrentIndexChanged);
|
|
|
|
|
|
|
|
connect(Config(), &Configuration::fontsUpdated, this,
|
|
|
|
&AppearanceOptionsWidget::updateFontFromConfig);
|
2017-12-14 15:14:33 +00:00
|
|
|
}
|
|
|
|
|
2018-10-17 07:30:56 +00:00
|
|
|
AppearanceOptionsWidget::~AppearanceOptionsWidget() {}
|
2017-12-14 15:14:33 +00:00
|
|
|
|
2018-10-17 07:30:56 +00:00
|
|
|
void AppearanceOptionsWidget::updateFontFromConfig()
|
2017-12-14 15:14:33 +00:00
|
|
|
{
|
|
|
|
QFont currentFont = Config()->getFont();
|
|
|
|
ui->fontSelectionLabel->setText(currentFont.toString());
|
|
|
|
}
|
|
|
|
|
2018-10-17 07:30:56 +00:00
|
|
|
void AppearanceOptionsWidget::updateThemeFromConfig()
|
2017-12-14 15:14:33 +00:00
|
|
|
{
|
2018-03-01 15:30:31 +00:00
|
|
|
// Disconnect currentIndexChanged because clearing the comboxBox and refiling it causes its index to change.
|
2018-11-11 12:11:50 +00:00
|
|
|
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);
|
2018-02-26 22:25:23 +00:00
|
|
|
|
|
|
|
QList<QString> themes = Core()->getColorThemes();
|
|
|
|
ui->colorComboBox->clear();
|
2018-11-11 12:11:50 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-02-26 22:25:23 +00:00
|
|
|
ui->colorComboBox->setCurrentIndex(index);
|
2018-11-11 12:11:50 +00:00
|
|
|
curTheme = ui->colorComboBox->currentText();
|
|
|
|
Config()->setColorTheme(curTheme);
|
|
|
|
ui->colorSchemePrefWidget->updateSchemeFromConfig();
|
2018-03-11 10:29:37 +00:00
|
|
|
int maxThemeLen = 0;
|
2018-03-21 20:32:32 +00:00
|
|
|
for (QString str : themes) {
|
2018-03-11 10:29:37 +00:00
|
|
|
int strLen = str.length();
|
|
|
|
if (strLen > maxThemeLen) {
|
|
|
|
maxThemeLen = strLen;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ui->colorComboBox->setMinimumContentsLength(maxThemeLen);
|
|
|
|
ui->colorComboBox->setSizeAdjustPolicy(QComboBox::AdjustToMinimumContentsLength);
|
2017-12-14 15:14:33 +00:00
|
|
|
}
|
|
|
|
|
2018-10-17 07:30:56 +00:00
|
|
|
void AppearanceOptionsWidget::on_fontSelectionButton_clicked()
|
2017-12-14 15:14:33 +00:00
|
|
|
{
|
|
|
|
QFont currentFont = Config()->getFont();
|
|
|
|
bool ok;
|
2018-10-10 09:37:24 +00:00
|
|
|
QFont newFont = QFontDialog::getFont(&ok, currentFont, this, QString(),
|
|
|
|
QFontDialog::DontUseNativeDialog);
|
2017-12-14 15:14:33 +00:00
|
|
|
if (ok) {
|
|
|
|
Config()->setFont(newFont);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-17 07:30:56 +00:00
|
|
|
void AppearanceOptionsWidget::on_themeComboBox_currentIndexChanged(int index)
|
2017-12-14 15:14:33 +00:00
|
|
|
{
|
2018-06-22 15:57:26 +00:00
|
|
|
Config()->setTheme(index);
|
2018-11-11 12:11:50 +00:00
|
|
|
updateThemeFromConfig();
|
2018-02-26 22:25:23 +00:00
|
|
|
}
|
|
|
|
|
2018-10-17 07:30:56 +00:00
|
|
|
void AppearanceOptionsWidget::on_colorComboBox_currentIndexChanged(int index)
|
2018-02-26 22:25:23 +00:00
|
|
|
{
|
|
|
|
QString theme = ui->colorComboBox->itemText(index);
|
2018-11-11 12:11:50 +00:00
|
|
|
|
|
|
|
uint curQtThemeIndex = Config()->getTheme();
|
|
|
|
if (curQtThemeIndex >= kCutterQtThemesList.size()) {
|
|
|
|
curQtThemeIndex = 0;
|
|
|
|
Config()->setTheme(curQtThemeIndex);
|
|
|
|
}
|
|
|
|
|
|
|
|
Config()->setLastThemeOf(kCutterQtThemesList[curQtThemeIndex], theme);
|
2018-02-26 22:25:23 +00:00
|
|
|
Config()->setColorTheme(theme);
|
2018-11-11 12:11:50 +00:00
|
|
|
ui->colorSchemePrefWidget->updateSchemeFromConfig();
|
2017-12-14 15:14:33 +00:00
|
|
|
}
|
2018-10-10 09:37:24 +00:00
|
|
|
|
2018-10-17 07:30:56 +00:00
|
|
|
void AppearanceOptionsWidget::on_copyButton_clicked()
|
2018-10-10 09:37:24 +00:00
|
|
|
{
|
|
|
|
QString newSchemeName;
|
|
|
|
do {
|
|
|
|
newSchemeName = QInputDialog::getText(this, tr("Enter scheme name"),
|
|
|
|
tr("Name:"), QLineEdit::Normal,
|
|
|
|
QDir::home().dirName());
|
|
|
|
} while (ColorSchemeFileWorker().isNameEngaged(newSchemeName) && !newSchemeName.isEmpty());
|
|
|
|
|
|
|
|
if (newSchemeName.isEmpty())
|
|
|
|
return;
|
|
|
|
ColorSchemeFileWorker().copy(Config()->getCurrentTheme(), newSchemeName);
|
|
|
|
Config()->setColorTheme(newSchemeName);
|
2018-11-11 12:11:50 +00:00
|
|
|
ui->colorSchemePrefWidget->updateSchemeFromConfig();
|
2018-10-10 09:37:24 +00:00
|
|
|
updateThemeFromConfig();
|
|
|
|
}
|
|
|
|
|
2018-10-17 07:30:56 +00:00
|
|
|
void AppearanceOptionsWidget::on_deleteButton_clicked()
|
2018-10-10 09:37:24 +00:00
|
|
|
{
|
2018-11-11 12:11:50 +00:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
2018-10-10 09:37:24 +00:00
|
|
|
}
|
2018-10-31 16:07:53 +00:00
|
|
|
|
|
|
|
void AppearanceOptionsWidget::onLanguageComboBoxCurrentIndexChanged(int index)
|
|
|
|
{
|
|
|
|
QString language = ui->languageComboBox->itemText(index).toLower();
|
|
|
|
auto allLocales = QLocale::matchingLocales(QLocale::AnyLanguage, QLocale::AnyScript,
|
|
|
|
QLocale::AnyCountry);
|
|
|
|
|
|
|
|
for (auto &it : allLocales) {
|
|
|
|
if (it.nativeLanguageName().toLower() == language) {
|
|
|
|
Config()->setLocale(it);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QMessageBox mb;
|
|
|
|
mb.setWindowTitle(tr("Language settings"));
|
|
|
|
mb.setText(tr("Language will be changed after next application start."));
|
|
|
|
mb.setIcon(QMessageBox::Information);
|
|
|
|
mb.setStandardButtons(QMessageBox::Ok);
|
|
|
|
mb.exec();
|
|
|
|
}
|