cutter/src/dialogs/preferences/AppearanceOptionsWidget.cpp

196 lines
6.5 KiB
C++
Raw Normal View History

#include <QDir>
#include <QLabel>
#include <QFontDialog>
2018-10-31 16:07:53 +00:00
#include <QTranslator>
#include <QInputDialog>
#include <QSignalBlocker>
#include <QComboBox>
#include "PreferencesDialog.h"
#include "AppearanceOptionsWidget.h"
#include "ui_AppearanceOptionsWidget.h"
2018-10-17 07:55:53 +00:00
#include "common/Helpers.h"
#include "common/Configuration.h"
2018-10-17 07:55:53 +00:00
#include "common/ColorSchemeFileSaver.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 }
};
2019-03-16 12:41:45 +00:00
AppearanceOptionsWidget::AppearanceOptionsWidget(PreferencesDialog *dialog)
: QDialog(dialog),
ui(new Ui::AppearanceOptionsWidget)
{
ui->setupUi(this);
updateFontFromConfig();
updateThemeFromConfig(false);
2019-01-19 20:54:02 +00:00
QStringList langs = Config()->getAvailableTranslations();
2018-10-31 16:07:53 +00:00
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);
}
AppearanceOptionsWidget::~AppearanceOptionsWidget() {}
void AppearanceOptionsWidget::updateFontFromConfig()
{
QFont currentFont = Config()->getFont();
ui->fontSelectionLabel->setText(currentFont.toString());
}
void AppearanceOptionsWidget::updateThemeFromConfig(bool qtThemeChanged)
{
2018-03-01 15:30:31 +00:00
// Disconnect currentIndexChanged because clearing the comboxBox and refiling it causes its index to change.
QSignalBlocker signalBlockerColorBox(ui->colorComboBox);
QSignalBlocker signalBlockerThemeBox(ui->themeComboBox);
Q_UNUSED(signalBlockerColorBox);
Q_UNUSED(signalBlockerThemeBox);
ui->themeComboBox->clear();
for (auto &it : kCutterQtThemesList) {
ui->themeComboBox->addItem(it.name);
}
int curQtThemeIndex = Config()->getTheme();
if (curQtThemeIndex >= kCutterQtThemesList.size()) {
curQtThemeIndex = 0;
Config()->setTheme(curQtThemeIndex);
}
ui->themeComboBox->setCurrentIndex(curQtThemeIndex);
QList<QString> themes = Core()->getColorThemes();
ui->colorComboBox->clear();
for (const QString &theme : themes) {
if (ColorSchemeFileWorker().isCustomScheme(theme) ||
(kCutterQtThemesList[curQtThemeIndex].flag & kRelevantSchemes[theme])) {
ui->colorComboBox->addItem(theme);
}
}
QString curTheme = qtThemeChanged
? Config()->getLastThemeOf(kCutterQtThemesList[curQtThemeIndex])
: Config()->getColorTheme();
const int index = ui->colorComboBox->findText(curTheme);
ui->colorComboBox->setCurrentIndex(index == -1 ? 0 : index);
if (qtThemeChanged || index == -1) {
curTheme = ui->colorComboBox->currentText();
Config()->setColorTheme(curTheme);
}
ui->colorSchemePrefWidget->updateSchemeFromConfig();
int maxThemeLen = 0;
for (const QString &str : themes) {
int strLen = str.length();
if (strLen > maxThemeLen) {
maxThemeLen = strLen;
}
}
ui->colorComboBox->setMinimumContentsLength(maxThemeLen);
ui->colorComboBox->setSizeAdjustPolicy(QComboBox::AdjustToMinimumContentsLength);
}
void AppearanceOptionsWidget::on_fontSelectionButton_clicked()
{
QFont currentFont = Config()->getFont();
bool ok;
QFont newFont = QFontDialog::getFont(&ok, currentFont, this, QString(),
QFontDialog::DontUseNativeDialog);
if (ok) {
Config()->setFont(newFont);
}
}
void AppearanceOptionsWidget::on_themeComboBox_currentIndexChanged(int index)
{
Config()->setTheme(index);
updateThemeFromConfig();
}
void AppearanceOptionsWidget::on_colorComboBox_currentIndexChanged(int index)
{
QString theme = ui->colorComboBox->itemText(index);
int 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()
{
QString newSchemeName;
do {
newSchemeName = QInputDialog::getText(this, tr("Enter scheme name"),
tr("Name:"), QLineEdit::Normal,
QDir::home().dirName());
} while ((!newSchemeName.isEmpty() && ColorSchemeFileWorker().isNameEngaged(newSchemeName))
|| newSchemeName.contains(QRegExp("[^\\w.()\\[\\]_-]"))
|| newSchemeName.startsWith('.'));
if (newSchemeName.isEmpty())
return;
ColorSchemeFileWorker().copy(Config()->getColorTheme(), newSchemeName);
Config()->setColorTheme(newSchemeName);
ui->colorSchemePrefWidget->updateSchemeFromConfig();
updateThemeFromConfig(false);
}
void AppearanceOptionsWidget::on_deleteButton_clicked()
{
if (ColorSchemeFileWorker().isCustomScheme(Config()->getColorTheme())) {
QMessageBox mb;
mb.setWindowTitle(tr("Delete"));
mb.setText(tr("Are you sure you want to delete theme ") + Config()->getColorTheme());
mb.setIcon(QMessageBox::Question);
mb.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
if (mb.exec() == QMessageBox::Yes) {
ColorSchemeFileWorker().deleteScheme(Config()->getColorTheme());
updateThemeFromConfig(false);
}
}
}
2018-10-31 16:07:53 +00:00
void AppearanceOptionsWidget::onLanguageComboBoxCurrentIndexChanged(int index)
{
QString language = ui->languageComboBox->itemText(index).toLower();
if (Config()->setLocaleByName(language)) {
QMessageBox::information(this,
tr("Language settings"),
tr("Language will be changed after next application start."));
return;
}
qWarning() << tr("Cannot set language, not found in available ones");
2018-10-31 16:07:53 +00:00
}