cutter/src/dialogs/preferences/AppearanceOptionsWidget.cpp

182 lines
6.4 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 <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"
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";
}
AppearanceOptionsWidget::AppearanceOptionsWidget(PreferencesDialog *dialog, QWidget *parent)
2018-03-21 20:32:32 +00:00
: QDialog(parent),
ui(new Ui::AppearanceOptionsWidget)
{
Q_UNUSED(dialog);
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);
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() {}
void AppearanceOptionsWidget::updateFontFromConfig()
{
QFont currentFont = Config()->getFont();
ui->fontSelectionLabel->setText(currentFont.toString());
}
void AppearanceOptionsWidget::updateThemeFromConfig()
{
2018-03-01 15:30:31 +00:00
// Disconnect currentIndexChanged because clearing the comboxBox and refiling it causes its index to change.
2018-03-21 20:32:32 +00:00
disconnect(ui->colorComboBox, SIGNAL(currentIndexChanged(int)), this,
SLOT(on_colorComboBox_currentIndexChanged(int)));
ui->themeComboBox->setCurrentIndex(Config()->getTheme());
QList<QString> 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;
ui->colorComboBox->setCurrentIndex(index);
int maxThemeLen = 0;
2018-03-21 20:32:32 +00:00
for (QString str : themes) {
int strLen = str.length();
if (strLen > maxThemeLen) {
maxThemeLen = strLen;
}
}
ui->colorComboBox->setMinimumContentsLength(maxThemeLen);
ui->colorComboBox->setSizeAdjustPolicy(QComboBox::AdjustToMinimumContentsLength);
2018-03-21 20:32:32 +00:00
connect(ui->colorComboBox, SIGNAL(currentIndexChanged(int)), this,
SLOT(on_colorComboBox_currentIndexChanged(int)));
}
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)
{
//disconnect(Config(), SIGNAL(colorsUpdated()), this, SLOT(updateThemeFromConfig()));
Config()->setTheme(index);
//connect(Config(), SIGNAL(colorsUpdated()), this, SLOT(updateThemeFromConfig()));
}
void AppearanceOptionsWidget::on_colorComboBox_currentIndexChanged(int index)
{
QString theme = ui->colorComboBox->itemText(index);
Config()->setColorTheme(theme);
}
void AppearanceOptionsWidget::on_copyButton_clicked()
{
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);
ui.get()->colorSchemePrefWidget->setNewScheme(newSchemeName);
updateThemeFromConfig();
ui.get()->colorComboBox->setCurrentIndex(ui.get()->colorComboBox->findText(newSchemeName));
}
void AppearanceOptionsWidget::on_deleteButton_clicked()
{
ColorSchemeFileWorker().deleteScheme(Config()->getCurrentTheme());
}
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();
}