ScreenTranslator/src/settingseditor.cpp

308 lines
10 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"
#include "tesseract.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
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);
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-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-08 17:49:15 +07:00
// correction
ui->userSubstitutionsTable->setEnabled(ui->useUserSubstitutions->isChecked());
connect(ui->useUserSubstitutions, &QCheckBox::toggled, //
ui->userSubstitutionsTable, &QTableWidget::setEnabled);
2020-02-21 00:45:53 +07:00
// translation
updateTranslationLanguages();
2020-03-20 01:47:46 +07:00
// representation
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);
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 WidgetState(this);
}
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-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();
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();
LanguageCodes langs;
if (auto lang = langs.findByName(ui->tesseractLangCombo->currentText()))
settings.sourceLanguage = lang->id;
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());
if (auto lang = langs.findByName(ui->translateLangCombo->currentText()))
settings.targetLanguage = lang->id;
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.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-15 19:30:24 +07:00
wasPortable_ = settings.isPortable();
ui->portable->setChecked(settings.isPortable());
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);
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);
LanguageCodes langs;
ui->tessdataPath->setText(settings.tessdataPath);
updateTesseractLanguages();
if (auto lang = langs.findById(settings.sourceLanguage))
2020-03-18 02:15:28 +07:00
ui->tesseractLangCombo->setCurrentText(QObject::tr(lang->name));
2020-02-21 00:45:53 +07:00
2020-03-08 17:49:15 +07:00
ui->useUserSubstitutions->setChecked(settings.useUserSubstitutions);
ui->userSubstitutionsTable->setTessdataPath(settings.tessdataPath);
2020-03-08 17:49:15 +07:00
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();
if (auto lang = langs.findById(settings.targetLanguage))
2020-03-18 02:15:28 +07:00
ui->translateLangCombo->setCurrentText(QObject::tr(lang->name));
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);
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-02-21 00:45:53 +07:00
void SettingsEditor::updateTesseractLanguages()
{
ui->tesseractLangCombo->clear();
auto names = Tesseract::availableLanguageNames(ui->tessdataPath->text());
2020-02-21 00:45:53 +07:00
if (names.isEmpty())
return;
2013-11-24 19:43:37 +07:00
2020-02-21 00:45:53 +07:00
std::sort(names.begin(), names.end());
ui->tesseractLangCombo->addItems(names);
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
const auto path = ui->translatorsPath->text();
if (path.isEmpty())
return;
QDir dir(path);
2020-02-21 00:45:53 +07:00
if (!dir.exists())
return;
2020-02-21 00:45:53 +07:00
auto files = dir.entryList({"*.js"}, QDir::Files);
std::sort(files.begin(), files.end());
ui->translatorList->addItems(files);
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
}
void SettingsEditor::updateTranslationLanguages()
{
LanguageIds names;
LanguageCodes languages;
for (const auto &bundle : languages.all()) {
if (!bundle.second.iso639_1.isEmpty())
2020-03-18 02:15:28 +07:00
names.append(QObject::tr(bundle.second.name));
}
2020-02-21 00:45:53 +07:00
ui->translateLangCombo->clear();
std::sort(names.begin(), names.end());
ui->translateLangCombo->addItems(names);
}
2020-03-15 18:10:26 +07:00
void SettingsEditor::adjustUpdatesView()
{
ui->updatesView->resizeColumnToContents(int(update::Model::Column::Name));
updateTesseractLanguages();
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);
ui->userSubstitutionsTable->setTessdataPath(settings.tessdataPath);
2020-03-15 19:30:24 +07:00
updateTesseractLanguages();
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);
}