2020-01-27 02:01:08 +07:00
|
|
|
#include "settingseditor.h"
|
2020-02-21 00:45:53 +07:00
|
|
|
#include "languagecodes.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>
|
2015-10-10 18:45:57 +07:00
|
|
|
#include <QNetworkProxy>
|
2020-03-15 18:10:26 +07:00
|
|
|
#include <QSortFilterProxyModel>
|
2020-02-21 00:45:53 +07:00
|
|
|
#include <QStringListModel>
|
|
|
|
|
2020-03-15 18:10:26 +07:00
|
|
|
SettingsEditor::SettingsEditor(update::Loader &updater)
|
2020-02-21 00:45:53 +07:00
|
|
|
: ui(new Ui::SettingsEditor)
|
2020-03-15 18:10:26 +07:00
|
|
|
, updater_(updater)
|
2020-02-21 00:45:53 +07:00
|
|
|
{
|
|
|
|
ui->setupUi(this);
|
|
|
|
|
|
|
|
{
|
|
|
|
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
|
2020-02-21 00:45:53 +07:00
|
|
|
|
2020-03-08 17:49:15 +07:00
|
|
|
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();
|
|
|
|
|
|
|
|
// updates
|
2020-03-15 18:10:26 +07:00
|
|
|
QMap<AutoUpdate, QString> updateTypes;
|
|
|
|
updateTypes.insert(AutoUpdate::Disabled, tr("Disabled"));
|
|
|
|
updateTypes.insert(AutoUpdate::Daily, tr("Daily"));
|
|
|
|
updateTypes.insert(AutoUpdate::Weekly, tr("Weekly"));
|
|
|
|
updateTypes.insert(AutoUpdate::Monthly, tr("Monthly"));
|
|
|
|
ui->updateCombo->addItems(updateTypes.values());
|
|
|
|
|
|
|
|
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;
|
|
|
|
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();
|
|
|
|
|
2020-03-08 01:21:25 +07:00
|
|
|
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());
|
2020-03-08 01:21:25 +07:00
|
|
|
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;
|
|
|
|
return settings;
|
2013-11-23 13:48:34 +07:00
|
|
|
}
|
|
|
|
|
2020-02-21 00:45:53 +07:00
|
|
|
void SettingsEditor::setSettings(const Settings &settings)
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
|
2020-03-08 01:21:25 +07:00
|
|
|
LanguageCodes langs;
|
2020-03-15 18:25:27 +07:00
|
|
|
ui->tessdataPath->setText(settings.tessdataPath);
|
|
|
|
updateTesseractLanguages();
|
2020-03-08 01:21:25 +07:00
|
|
|
if (auto lang = langs.findById(settings.sourceLanguage))
|
|
|
|
ui->tesseractLangCombo->setCurrentText(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->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());
|
2020-03-15 18:25:27 +07:00
|
|
|
ui->translatorsPath->setText(settings.translatorsDir);
|
2020-03-15 18:10:26 +07:00
|
|
|
enabledTranslators_ = settings.translators;
|
|
|
|
updateTranslators();
|
2020-03-08 01:21:25 +07:00
|
|
|
if (auto lang = langs.findById(settings.targetLanguage))
|
|
|
|
ui->translateLangCombo->setCurrentText(lang->name);
|
2020-02-21 00:45:53 +07:00
|
|
|
|
|
|
|
ui->trayRadio->setChecked(settings.resultShowType == ResultMode::Tooltip);
|
|
|
|
ui->dialogRadio->setChecked(settings.resultShowType == ResultMode::Widget);
|
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();
|
|
|
|
|
2020-03-15 18:25:27 +07:00
|
|
|
const auto path = ui->tessdataPath->text();
|
2020-03-12 00:39:02 +07:00
|
|
|
if (path.isEmpty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
QDir dir(path);
|
2020-02-21 00:45:53 +07:00
|
|
|
if (!dir.exists())
|
2013-11-24 19:43:37 +07:00
|
|
|
return;
|
|
|
|
|
2020-02-21 00:45:53 +07:00
|
|
|
LanguageIds names;
|
|
|
|
LanguageCodes languages;
|
2015-10-10 17:33:18 +07:00
|
|
|
|
2020-02-21 00:45:53 +07:00
|
|
|
const auto files = dir.entryList({"*.traineddata"}, QDir::Files);
|
|
|
|
for (const auto &file : files) {
|
|
|
|
const auto lang = file.left(file.indexOf("."));
|
|
|
|
if (const auto bundle = languages.findByTesseract(lang))
|
|
|
|
names.append(bundle->name);
|
2015-10-10 03:34:24 +07:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2020-03-15 18:25:27 +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;
|
2015-10-10 17:33:18 +07:00
|
|
|
|
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);
|
2015-10-10 17:33:18 +07:00
|
|
|
}
|
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())
|
|
|
|
names.append(bundle.second.name);
|
2015-10-10 17:33:18 +07:00
|
|
|
}
|
2020-02-21 00:45:53 +07:00
|
|
|
|
|
|
|
ui->translateLangCombo->clear();
|
|
|
|
std::sort(names.begin(), names.end());
|
|
|
|
ui->translateLangCombo->addItems(names);
|
2015-10-10 17:33:18 +07:00
|
|
|
}
|
2020-03-15 18:10:26 +07:00
|
|
|
|
|
|
|
void SettingsEditor::adjustUpdatesView()
|
|
|
|
{
|
|
|
|
ui->updatesView->resizeColumnToContents(int(update::Model::Column::Name));
|
|
|
|
updateTesseractLanguages();
|
|
|
|
updateTranslators();
|
|
|
|
}
|