ScreenTranslator/src/settingseditor.cpp

346 lines
12 KiB
C++
Raw Normal View History

2020-01-27 02:01:08 +07:00
#include "settingseditor.h"
2020-02-21 00:45:53 +07:00
#include "languagecodes.h"
#include "manager.h"
2020-03-20 02:35:42 +07:00
#include "runatsystemstart.h"
#include "tesseract.h"
#include "translator.h"
2020-01-27 02:01:08 +07:00
#include "ui_settingseditor.h"
2020-03-15 18:10:26 +07:00
#include "updates.h"
2020-02-21 00:45:53 +07:00
#include "widgetstate.h"
2013-11-23 13:48:34 +07:00
#include <QColorDialog>
2020-02-21 00:45:53 +07:00
#include <QFileDialog>
2020-03-15 18:10:26 +07:00
#include <QSortFilterProxyModel>
2020-02-21 00:45:53 +07:00
#include <QStringListModel>
SettingsEditor::SettingsEditor(Manager &manager, update::Loader &updater)
2020-02-21 00:45:53 +07:00
: ui(new Ui::SettingsEditor)
, manager_(manager)
2020-03-15 18:10:26 +07:00
, updater_(updater)
2020-02-21 00:45:53 +07:00
{
ui->setupUi(this);
ui->translatorDebugCheck->hide();
connect(ui->buttonBox, &QDialogButtonBox::clicked, //
this, &SettingsEditor::handleButtonBoxClicked);
2020-03-15 19:30:24 +07:00
connect(ui->portable, &QCheckBox::toggled, //
this, &SettingsEditor::handlePortableChanged);
2020-03-20 02:35:42 +07:00
ui->runAtSystemStart->setEnabled(service::RunAtSystemStart::isAvailable());
2020-02-21 00:45:53 +07:00
{
auto model = new QStringListModel(this);
model->setStringList({tr("General"), tr("Recognition"), tr("Correction"),
tr("Translation"), tr("Representation"),
tr("Update")});
ui->pagesList->setModel(model);
auto selection = ui->pagesList->selectionModel();
connect(selection, &QItemSelectionModel::currentRowChanged, //
this, &SettingsEditor::updateCurrentPage);
}
2020-03-07 00:53:53 +07:00
{
QMap<ProxyType, QString> proxyTypes;
proxyTypes.insert(ProxyType::Disabled, tr("Disabled"));
proxyTypes.insert(ProxyType::System, tr("System"));
proxyTypes.insert(ProxyType::Socks5, tr("SOCKS 5"));
proxyTypes.insert(ProxyType::Http, tr("HTTP"));
ui->proxyTypeCombo->addItems(proxyTypes.values());
QRegExp urlRegexp(
R"(^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$)");
ui->proxyHostEdit->setValidator(
new QRegExpValidator(urlRegexp, ui->proxyHostEdit));
ui->proxyPassEdit->setEchoMode(QLineEdit::PasswordEchoOnEdit);
}
2020-02-21 00:45:53 +07:00
2020-03-28 18:26:05 +07:00
// translation
ui->tesseractLangCombo->setModel(models_.sourceLanguageModel());
2020-03-08 17:49:15 +07:00
// correction
ui->userSubstitutionsTable->setEnabled(ui->useUserSubstitutions->isChecked());
2020-03-28 18:26:05 +07:00
ui->userSubstitutionsTable->setSourceLanguageModel(
models_.sourceLanguageModel());
2020-03-08 17:49:15 +07:00
connect(ui->useUserSubstitutions, &QCheckBox::toggled, //
ui->userSubstitutionsTable, &QTableWidget::setEnabled);
2020-02-21 00:45:53 +07:00
// translation
2020-03-28 18:26:05 +07:00
ui->translateLangCombo->setModel(models_.targetLanguageModel());
2020-02-21 00:45:53 +07:00
2020-03-20 01:47:46 +07:00
// representation
ui->fontColor->setAutoFillBackground(true);
ui->backgroundColor->setAutoFillBackground(true);
ui->backgroundColor->setText(tr("Sample text"));
2020-03-20 01:47:46 +07:00
connect(ui->dialogRadio, &QRadioButton::toggled, //
ui->resultWindow, &QTableWidget::setEnabled);
connect(ui->resultFont, &QFontComboBox::currentFontChanged, //
this, &SettingsEditor::updateResultFont);
connect(ui->resultFontSize, qOverload<int>(&QSpinBox::valueChanged), //
this, &SettingsEditor::updateResultFont);
connect(ui->fontColor, &QPushButton::clicked, //
this, [this] { pickColor(ColorContext::Font); });
connect(ui->backgroundColor, &QPushButton::clicked, //
this, [this] { pickColor(ColorContext::Bagkround); });
2020-03-20 01:47:46 +07:00
2020-02-21 00:45:53 +07:00
// updates
2020-03-15 18:10:26 +07:00
auto updatesProxy = new QSortFilterProxyModel(this);
updatesProxy->setSourceModel(updater_.model());
ui->updatesView->setModel(updatesProxy);
ui->updatesView->setItemDelegateForColumn(int(update::Model::Column::Action),
new update::ActionDelegate(this));
#ifndef DEVELOP
ui->updatesView->hideColumn(int(update::Model::Column::Files));
#endif
adjustUpdatesView();
connect(updater_.model(), &QAbstractItemModel::modelReset, //
this, &SettingsEditor::adjustUpdatesView);
connect(&updater_, &update::Loader::updated, //
this, &SettingsEditor::adjustUpdatesView);
connect(ui->checkUpdates, &QPushButton::clicked, //
&updater_, &update::Loader::checkForUpdates);
connect(ui->applyUpdates, &QPushButton::clicked, //
&updater_, &update::Loader::applyUserActions);
2020-02-21 00:45:53 +07:00
new service::WidgetState(this);
2020-02-21 00:45:53 +07:00
}
2013-11-24 19:43:37 +07:00
2020-02-21 00:45:53 +07:00
SettingsEditor::~SettingsEditor()
{
delete ui;
}
2015-10-10 18:45:57 +07:00
2020-02-21 00:45:53 +07:00
Settings SettingsEditor::settings() const
{
Settings settings;
2020-03-15 19:30:24 +07:00
settings.setPortable(ui->portable->isChecked());
2020-03-20 02:35:42 +07:00
settings.runAtSystemStart = ui->runAtSystemStart->isChecked();
2020-02-21 00:45:53 +07:00
settings.captureHotkey = ui->captureEdit->keySequence().toString();
settings.repeatCaptureHotkey =
ui->repeatCaptureEdit->keySequence().toString();
settings.showLastHotkey = ui->repeatEdit->keySequence().toString();
settings.clipboardHotkey = ui->clipboardEdit->keySequence().toString();
settings.captureLockedHotkey =
ui->captureLockedEdit->keySequence().toString();
2020-02-21 00:45:53 +07:00
2020-03-09 15:29:51 +07:00
settings.showMessageOnStart = ui->showOnStart->isChecked();
2020-03-07 00:53:53 +07:00
settings.proxyType = ProxyType(ui->proxyTypeCombo->currentIndex());
settings.proxyHostName = ui->proxyHostEdit->text();
settings.proxyPort = ui->proxyPortSpin->value();
settings.proxyUser = ui->proxyUserEdit->text();
settings.proxyPassword = ui->proxyPassEdit->text();
settings.proxySavePassword = ui->proxySaveCheck->isChecked();
2020-03-25 01:38:05 +07:00
settings.sourceLanguage =
LanguageCodes::idForName(ui->tesseractLangCombo->currentText());
2020-02-21 00:45:53 +07:00
2020-03-08 17:49:15 +07:00
settings.useUserSubstitutions = ui->useUserSubstitutions->isChecked();
settings.userSubstitutions = ui->userSubstitutionsTable->substitutions();
2020-02-21 00:45:53 +07:00
settings.doTranslation = ui->doTranslationCheck->isChecked();
settings.ignoreSslErrors = ui->ignoreSslCheck->isChecked();
settings.debugMode = ui->translatorDebugCheck->isChecked();
settings.translationTimeout =
std::chrono::seconds(ui->translateTimeoutSpin->value());
2020-03-25 01:38:05 +07:00
settings.targetLanguage =
LanguageCodes::idForName(ui->translateLangCombo->currentText());
2020-02-21 00:45:53 +07:00
settings.translators.clear();
for (auto i = 0, end = ui->translatorList->count(); i < end; ++i) {
auto item = ui->translatorList->item(i);
if (item->checkState() == Qt::Checked)
settings.translators.append(item->text());
}
settings.resultShowType =
ui->trayRadio->isChecked() ? ResultMode::Tooltip : ResultMode::Widget;
2020-03-20 01:47:46 +07:00
settings.fontFamily = ui->resultFont->currentFont().family();
settings.fontSize = ui->resultFontSize->value();
settings.fontColor = ui->fontColor->palette().color(QPalette::Button);
settings.backgroundColor =
ui->backgroundColor->palette().color(QPalette::Button);
settings.showRecognized = ui->showRecognized->isChecked();
settings.showCaptured = ui->showCaptured->isChecked();
2020-03-18 01:32:36 +07:00
settings.autoUpdateIntervalDays = ui->autoUpdateInterval->value();
2020-02-21 00:45:53 +07:00
return settings;
2013-11-23 13:48:34 +07:00
}
2020-02-21 00:45:53 +07:00
void SettingsEditor::setSettings(const Settings &settings)
{
2020-03-28 18:26:05 +07:00
if (settings.isPortable() == ui->portable->isChecked())
updateModels(settings.tessdataPath);
2020-03-15 19:30:24 +07:00
wasPortable_ = settings.isPortable();
ui->portable->setChecked(settings.isPortable());
2020-03-20 02:35:42 +07:00
ui->runAtSystemStart->setChecked(settings.runAtSystemStart);
2020-02-21 00:45:53 +07:00
ui->captureEdit->setKeySequence(settings.captureHotkey);
ui->repeatCaptureEdit->setKeySequence(settings.repeatCaptureHotkey);
ui->repeatEdit->setKeySequence(settings.showLastHotkey);
ui->clipboardEdit->setKeySequence(settings.clipboardHotkey);
ui->captureLockedEdit->setKeySequence(settings.captureLockedHotkey);
2020-02-21 00:45:53 +07:00
2020-03-09 15:29:51 +07:00
ui->showOnStart->setChecked(settings.showMessageOnStart);
2020-03-07 00:53:53 +07:00
ui->proxyTypeCombo->setCurrentIndex(int(settings.proxyType));
ui->proxyHostEdit->setText(settings.proxyHostName);
ui->proxyPortSpin->setValue(settings.proxyPort);
ui->proxyUserEdit->setText(settings.proxyUser);
ui->proxyPassEdit->setText(settings.proxyPassword);
ui->proxySaveCheck->setChecked(settings.proxySavePassword);
ui->tessdataPath->setText(settings.tessdataPath);
2020-03-25 01:38:05 +07:00
ui->tesseractLangCombo->setCurrentText(
LanguageCodes::name(settings.sourceLanguage));
2020-02-21 00:45:53 +07:00
2020-03-08 17:49:15 +07:00
ui->useUserSubstitutions->setChecked(settings.useUserSubstitutions);
ui->userSubstitutionsTable->setSubstitutions(settings.userSubstitutions);
2020-02-21 00:45:53 +07:00
ui->doTranslationCheck->setChecked(settings.doTranslation);
ui->ignoreSslCheck->setChecked(settings.ignoreSslErrors);
ui->translatorDebugCheck->setChecked(settings.debugMode);
ui->translateTimeoutSpin->setValue(settings.translationTimeout.count());
ui->translatorsPath->setText(settings.translatorsDir);
2020-03-15 18:10:26 +07:00
enabledTranslators_ = settings.translators;
updateTranslators();
2020-03-25 01:38:05 +07:00
ui->translateLangCombo->setCurrentText(
LanguageCodes::name(settings.targetLanguage));
2020-02-21 00:45:53 +07:00
ui->trayRadio->setChecked(settings.resultShowType == ResultMode::Tooltip);
ui->dialogRadio->setChecked(settings.resultShowType == ResultMode::Widget);
2020-03-20 01:47:46 +07:00
ui->resultFont->setCurrentFont(QFont(settings.fontFamily));
ui->resultFontSize->setValue(settings.fontSize);
{
QPalette palette(this->palette());
palette.setColor(QPalette::Button, settings.fontColor);
ui->fontColor->setPalette(palette);
palette.setColor(QPalette::ButtonText, settings.fontColor);
palette.setColor(QPalette::Button, settings.backgroundColor);
ui->backgroundColor->setPalette(palette);
}
ui->showRecognized->setChecked(settings.showRecognized);
ui->showCaptured->setChecked(settings.showCaptured);
2020-03-18 01:32:36 +07:00
ui->autoUpdateInterval->setValue(settings.autoUpdateIntervalDays);
2013-11-23 13:48:34 +07:00
}
2013-11-24 19:43:37 +07:00
2020-02-21 00:45:53 +07:00
void SettingsEditor::updateCurrentPage()
{
ui->pagesView->setCurrentIndex(ui->pagesList->currentIndex().row());
2013-11-24 19:43:37 +07:00
}
2020-03-15 18:10:26 +07:00
void SettingsEditor::updateTranslators()
2020-02-21 00:45:53 +07:00
{
ui->translatorList->clear();
2013-11-24 19:43:37 +07:00
auto names = Translator::availableTranslators(ui->translatorsPath->text());
if (names.isEmpty())
2020-02-21 00:45:53 +07:00
return;
std::sort(names.begin(), names.end());
ui->translatorList->addItems(names);
2020-02-21 00:45:53 +07:00
for (auto i = 0, end = ui->translatorList->count(); i < end; ++i) {
auto item = ui->translatorList->item(i);
2020-03-15 18:10:26 +07:00
item->setCheckState(enabledTranslators_.contains(item->text())
? Qt::Checked
: Qt::Unchecked);
}
2020-02-21 00:45:53 +07:00
}
2020-03-28 18:26:05 +07:00
void SettingsEditor::adjustUpdatesView()
2020-02-21 00:45:53 +07:00
{
2020-03-28 18:26:05 +07:00
ui->updatesView->resizeColumnToContents(int(update::Model::Column::Name));
2020-02-21 00:45:53 +07:00
2020-03-28 18:26:05 +07:00
if (ui->tessdataPath->text().isEmpty()) // not inited yet
return;
2020-02-21 00:45:53 +07:00
2020-03-28 18:26:05 +07:00
updateModels(ui->tessdataPath->text());
2020-03-15 18:10:26 +07:00
updateTranslators();
}
void SettingsEditor::handleButtonBoxClicked(QAbstractButton *button)
{
if (!button)
return;
if (button == ui->buttonBox->button(QDialogButtonBox::Ok)) {
accept();
return;
}
if (button == ui->buttonBox->button(QDialogButtonBox::Cancel)) {
reject();
return;
}
if (button == ui->buttonBox->button(QDialogButtonBox::Apply)) {
2020-03-15 19:30:24 +07:00
const auto settings = this->settings();
manager_.applySettings(settings);
if (settings.isPortable() != wasPortable_) {
wasPortable_ = settings.isPortable();
handlePortableChanged();
}
return;
}
}
2020-03-15 19:30:24 +07:00
void SettingsEditor::handlePortableChanged()
{
Settings settings;
settings.setPortable(ui->portable->isChecked());
ui->tessdataPath->setText(settings.tessdataPath);
ui->translatorsPath->setText(settings.translatorsDir);
2020-03-28 18:26:05 +07:00
updateModels(settings.tessdataPath);
2020-03-15 19:30:24 +07:00
updateTranslators();
const auto portableChanged = wasPortable_ != settings.isPortable();
ui->pageUpdate->setEnabled(!portableChanged);
ui->pageUpdate->setToolTip(portableChanged
? tr("Portable changed. Apply settings first")
: QString());
}
2020-03-20 01:47:46 +07:00
void SettingsEditor::updateResultFont()
{
auto font = ui->resultFont->currentFont();
font.setPointSize(ui->resultFontSize->value());
ui->resultFont->setFont(font);
}
2020-03-28 18:26:05 +07:00
void SettingsEditor::updateModels(const QString &tessdataPath)
{
const auto source = ui->tesseractLangCombo->currentText();
models_.update(tessdataPath);
ui->tesseractLangCombo->setCurrentText(source);
}
void SettingsEditor::pickColor(ColorContext context)
{
const auto widget =
context == ColorContext::Font ? ui->fontColor : ui->backgroundColor;
const auto original = widget->palette().color(QPalette::Button);
const auto color = QColorDialog::getColor(original, this);
if (!color.isValid())
return;
QPalette palette(widget->palette());
palette.setColor(QPalette::Button, color);
widget->setPalette(palette);
if (context == ColorContext::Bagkround)
return;
palette = ui->backgroundColor->palette();
palette.setColor(QPalette::ButtonText, color);
ui->backgroundColor->setPalette(palette);
ui->backgroundColor->update();
}