Auto select all translators if translation is enabled and no one is selected
This commit is contained in:
parent
0e72af3770
commit
155c3577c3
@ -11,7 +11,8 @@ CommonModels::CommonModels()
|
|||||||
|
|
||||||
CommonModels::~CommonModels() = default;
|
CommonModels::~CommonModels() = default;
|
||||||
|
|
||||||
void CommonModels::update(const QString &tessdataPath)
|
void CommonModels::update(const QString &tessdataPath,
|
||||||
|
const QString &translatorPath)
|
||||||
{
|
{
|
||||||
{
|
{
|
||||||
auto names = Tesseract::availableLanguageNames(tessdataPath);
|
auto names = Tesseract::availableLanguageNames(tessdataPath);
|
||||||
@ -19,6 +20,11 @@ void CommonModels::update(const QString &tessdataPath)
|
|||||||
sourceLanguageModel_->setStringList(names);
|
sourceLanguageModel_->setStringList(names);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
translators_ = Translator::availableTranslators(translatorPath);
|
||||||
|
std::sort(translators_.begin(), translators_.end());
|
||||||
|
}
|
||||||
|
|
||||||
if (targetLanguageModel_->rowCount() > 0)
|
if (targetLanguageModel_->rowCount() > 0)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
@ -38,3 +44,8 @@ QStringListModel *CommonModels::targetLanguageModel() const
|
|||||||
{
|
{
|
||||||
return targetLanguageModel_.get();
|
return targetLanguageModel_.get();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const QStringList &CommonModels::translators() const
|
||||||
|
{
|
||||||
|
return translators_;
|
||||||
|
}
|
||||||
|
@ -12,12 +12,14 @@ public:
|
|||||||
CommonModels();
|
CommonModels();
|
||||||
~CommonModels();
|
~CommonModels();
|
||||||
|
|
||||||
void update(const QString& tessdataPath);
|
void update(const QString& tessdataPath, const QString& translatorPath);
|
||||||
|
|
||||||
QStringListModel* sourceLanguageModel() const;
|
QStringListModel* sourceLanguageModel() const;
|
||||||
QStringListModel* targetLanguageModel() const;
|
QStringListModel* targetLanguageModel() const;
|
||||||
|
const QStringList& translators() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::unique_ptr<QStringListModel> sourceLanguageModel_;
|
std::unique_ptr<QStringListModel> sourceLanguageModel_;
|
||||||
std::unique_ptr<QStringListModel> targetLanguageModel_;
|
std::unique_ptr<QStringListModel> targetLanguageModel_;
|
||||||
|
QStringList translators_;
|
||||||
};
|
};
|
||||||
|
@ -75,7 +75,7 @@ void Corrector::processQueue()
|
|||||||
void Corrector::updateSettings()
|
void Corrector::updateSettings()
|
||||||
{
|
{
|
||||||
queue_.clear();
|
queue_.clear();
|
||||||
emit resetAuto(settings_.hunspellDir);
|
emit resetAuto(settings_.hunspellPath);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Corrector::finishCorrection(const TaskPtr &task)
|
void Corrector::finishCorrection(const TaskPtr &task)
|
||||||
|
@ -109,7 +109,7 @@ void Manager::updateSettings()
|
|||||||
setupProxy(*settings_);
|
setupProxy(*settings_);
|
||||||
setupUpdates(*settings_);
|
setupUpdates(*settings_);
|
||||||
|
|
||||||
models_->update(settings_->tessdataPath);
|
models_->update(settings_->tessdataPath, settings_->translatorsPath);
|
||||||
|
|
||||||
tray_->updateSettings();
|
tray_->updateSettings();
|
||||||
capturer_->updateSettings();
|
capturer_->updateSettings();
|
||||||
@ -120,7 +120,9 @@ void Manager::updateSettings()
|
|||||||
|
|
||||||
tray_->setCaptureLockedEnabled(capturer_->canCaptureLocked());
|
tray_->setCaptureLockedEnabled(capturer_->canCaptureLocked());
|
||||||
|
|
||||||
const auto errors = SettingsValidator().check(*settings_, *models_);
|
SettingsValidator validator;
|
||||||
|
validator.correct(*settings_, *models_);
|
||||||
|
const auto errors = validator.check(*settings_, *models_);
|
||||||
if (errors.isEmpty())
|
if (errors.isEmpty())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
@ -155,9 +157,9 @@ void Manager::setupProxy(const Settings &settings)
|
|||||||
void Manager::setupUpdates(const Settings &settings)
|
void Manager::setupUpdates(const Settings &settings)
|
||||||
{
|
{
|
||||||
updater_->setExpansions({
|
updater_->setExpansions({
|
||||||
{"$translators$", settings.translatorsDir},
|
{"$translators$", settings.translatorsPath},
|
||||||
{"$tessdata$", settings.tessdataPath},
|
{"$tessdata$", settings.tessdataPath},
|
||||||
{"$hunspell$", settings.hunspellDir},
|
{"$hunspell$", settings.hunspellPath},
|
||||||
{"$appdir$", QApplication::applicationDirPath()},
|
{"$appdir$", QApplication::applicationDirPath()},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -392,6 +392,6 @@ void Settings::setPortable(bool isPortable)
|
|||||||
QStandardPaths::AppDataLocation)) +
|
QStandardPaths::AppDataLocation)) +
|
||||||
"/assets";
|
"/assets";
|
||||||
tessdataPath = baseDataPath + "/tessdata";
|
tessdataPath = baseDataPath + "/tessdata";
|
||||||
translatorsDir = baseDataPath + "/translators";
|
translatorsPath = baseDataPath + "/translators";
|
||||||
hunspellDir = baseDataPath + "/hunspell";
|
hunspellPath = baseDataPath + "/hunspell";
|
||||||
}
|
}
|
||||||
|
@ -52,7 +52,7 @@ public:
|
|||||||
QDateTime lastUpdateCheck;
|
QDateTime lastUpdateCheck;
|
||||||
|
|
||||||
bool useHunspell{false};
|
bool useHunspell{false};
|
||||||
QString hunspellDir;
|
QString hunspellPath;
|
||||||
Substitutions userSubstitutions;
|
Substitutions userSubstitutions;
|
||||||
bool useUserSubstitutions{true};
|
bool useUserSubstitutions{true};
|
||||||
|
|
||||||
@ -66,7 +66,7 @@ public:
|
|||||||
bool ignoreSslErrors{false};
|
bool ignoreSslErrors{false};
|
||||||
LanguageId targetLanguage{"rus"};
|
LanguageId targetLanguage{"rus"};
|
||||||
std::chrono::seconds translationTimeout{15};
|
std::chrono::seconds translationTimeout{15};
|
||||||
QString translatorsDir;
|
QString translatorsPath;
|
||||||
QStringList translators{"google.js"};
|
QStringList translators{"google.js"};
|
||||||
|
|
||||||
ResultMode resultShowType{ResultMode::Widget}; // dialog
|
ResultMode resultShowType{ResultMode::Widget}; // dialog
|
||||||
|
@ -4,7 +4,6 @@
|
|||||||
#include "manager.h"
|
#include "manager.h"
|
||||||
#include "runatsystemstart.h"
|
#include "runatsystemstart.h"
|
||||||
#include "settingsvalidator.h"
|
#include "settingsvalidator.h"
|
||||||
#include "translator.h"
|
|
||||||
#include "ui_settingseditor.h"
|
#include "ui_settingseditor.h"
|
||||||
#include "updates.h"
|
#include "updates.h"
|
||||||
#include "widgetstate.h"
|
#include "widgetstate.h"
|
||||||
@ -326,14 +325,14 @@ void SettingsEditor::setSettings(const Settings &settings)
|
|||||||
ui->tesseractVersion->setCurrentIndex(int(settings.tesseractVersion));
|
ui->tesseractVersion->setCurrentIndex(int(settings.tesseractVersion));
|
||||||
|
|
||||||
ui->useHunspell->setChecked(settings.useHunspell);
|
ui->useHunspell->setChecked(settings.useHunspell);
|
||||||
ui->hunspellDir->setText(settings.hunspellDir);
|
ui->hunspellDir->setText(settings.hunspellPath);
|
||||||
ui->useUserSubstitutions->setChecked(settings.useUserSubstitutions);
|
ui->useUserSubstitutions->setChecked(settings.useUserSubstitutions);
|
||||||
ui->userSubstitutionsTable->setSubstitutions(settings.userSubstitutions);
|
ui->userSubstitutionsTable->setSubstitutions(settings.userSubstitutions);
|
||||||
|
|
||||||
ui->doTranslationCheck->setChecked(settings.doTranslation);
|
ui->doTranslationCheck->setChecked(settings.doTranslation);
|
||||||
ui->ignoreSslCheck->setChecked(settings.ignoreSslErrors);
|
ui->ignoreSslCheck->setChecked(settings.ignoreSslErrors);
|
||||||
ui->translateTimeoutSpin->setValue(settings.translationTimeout.count());
|
ui->translateTimeoutSpin->setValue(settings.translationTimeout.count());
|
||||||
ui->translatorsPath->setText(settings.translatorsDir);
|
ui->translatorsPath->setText(settings.translatorsPath);
|
||||||
ui->translateLangCombo->setCurrentText(
|
ui->translateLangCombo->setCurrentText(
|
||||||
LanguageCodes::name(settings.targetLanguage));
|
LanguageCodes::name(settings.targetLanguage));
|
||||||
updateTranslators(settings.translators);
|
updateTranslators(settings.translators);
|
||||||
@ -363,8 +362,8 @@ void SettingsEditor::updateState()
|
|||||||
Settings settings;
|
Settings settings;
|
||||||
settings.setPortable(ui->portable->isChecked());
|
settings.setPortable(ui->portable->isChecked());
|
||||||
ui->tessdataPath->setText(settings.tessdataPath);
|
ui->tessdataPath->setText(settings.tessdataPath);
|
||||||
ui->translatorsPath->setText(settings.translatorsDir);
|
ui->translatorsPath->setText(settings.translatorsPath);
|
||||||
ui->hunspellDir->setText(settings.hunspellDir);
|
ui->hunspellDir->setText(settings.hunspellPath);
|
||||||
|
|
||||||
updateModels();
|
updateModels();
|
||||||
updateTranslators(enabledTranslators());
|
updateTranslators(enabledTranslators());
|
||||||
@ -402,14 +401,10 @@ void SettingsEditor::updateCurrentPage()
|
|||||||
void SettingsEditor::updateTranslators(const QStringList &translators)
|
void SettingsEditor::updateTranslators(const QStringList &translators)
|
||||||
{
|
{
|
||||||
ui->translatorList->clear();
|
ui->translatorList->clear();
|
||||||
|
if (models_.translators().isEmpty())
|
||||||
auto names = Translator::availableTranslators(ui->translatorsPath->text());
|
|
||||||
if (names.isEmpty())
|
|
||||||
return;
|
return;
|
||||||
|
|
||||||
std::sort(names.begin(), names.end());
|
ui->translatorList->addItems(models_.translators());
|
||||||
|
|
||||||
ui->translatorList->addItems(names);
|
|
||||||
|
|
||||||
for (auto i = 0, end = ui->translatorList->count(); i < end; ++i) {
|
for (auto i = 0, end = ui->translatorList->count(); i < end; ++i) {
|
||||||
auto item = ui->translatorList->item(i);
|
auto item = ui->translatorList->item(i);
|
||||||
@ -466,7 +461,7 @@ QStringList SettingsEditor::enabledTranslators() const
|
|||||||
void SettingsEditor::updateModels()
|
void SettingsEditor::updateModels()
|
||||||
{
|
{
|
||||||
const auto source = ui->tesseractLangCombo->currentText();
|
const auto source = ui->tesseractLangCombo->currentText();
|
||||||
models_.update(ui->tessdataPath->text());
|
models_.update(ui->tessdataPath->text(), ui->translatorsPath->text());
|
||||||
if (!source.isEmpty()) {
|
if (!source.isEmpty()) {
|
||||||
ui->tesseractLangCombo->setCurrentText(source);
|
ui->tesseractLangCombo->setCurrentText(source);
|
||||||
} else if (ui->tesseractLangCombo->count() > 0) {
|
} else if (ui->tesseractLangCombo->count() > 0) {
|
||||||
@ -489,6 +484,15 @@ void SettingsEditor::pickColor(QWidget *widget)
|
|||||||
|
|
||||||
void SettingsEditor::validateSettings()
|
void SettingsEditor::validateSettings()
|
||||||
{
|
{
|
||||||
|
SettingsValidator validator;
|
||||||
|
{
|
||||||
|
auto settings = this->settings();
|
||||||
|
if (validator.correct(settings, models_)) {
|
||||||
|
setSettings(settings);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
for (auto i = 0, end = pageModel_->rowCount(); i < end; ++i) {
|
for (auto i = 0, end = pageModel_->rowCount(); i < end; ++i) {
|
||||||
const auto name = pageModel_->index(i, int(PageColumn::Name));
|
const auto name = pageModel_->index(i, int(PageColumn::Name));
|
||||||
pageModel_->setData(name, QBrush(Qt::black), Qt::ForegroundRole);
|
pageModel_->setData(name, QBrush(Qt::black), Qt::ForegroundRole);
|
||||||
@ -497,7 +501,6 @@ void SettingsEditor::validateSettings()
|
|||||||
pageModel_->setData(error, {});
|
pageModel_->setData(error, {});
|
||||||
}
|
}
|
||||||
|
|
||||||
SettingsValidator validator;
|
|
||||||
const auto errors = validator.check(settings(), models_);
|
const auto errors = validator.check(settings(), models_);
|
||||||
if (errors.isEmpty())
|
if (errors.isEmpty())
|
||||||
return;
|
return;
|
||||||
|
@ -14,7 +14,7 @@ QVector<SettingsValidator::Error> SettingsValidator::check(
|
|||||||
if (settings.sourceLanguage.isEmpty())
|
if (settings.sourceLanguage.isEmpty())
|
||||||
result.append(Error::NoSourceSet);
|
result.append(Error::NoSourceSet);
|
||||||
|
|
||||||
if (settings.doTranslation && models.targetLanguageModel()->rowCount() == 0)
|
if (settings.doTranslation && models.translators().isEmpty())
|
||||||
result.append(Error::NoTranslatorInstalled);
|
result.append(Error::NoTranslatorInstalled);
|
||||||
|
|
||||||
if (settings.doTranslation && settings.translators.isEmpty())
|
if (settings.doTranslation && settings.translators.isEmpty())
|
||||||
@ -26,6 +26,18 @@ QVector<SettingsValidator::Error> SettingsValidator::check(
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool SettingsValidator::correct(Settings &settings, const CommonModels &models)
|
||||||
|
{
|
||||||
|
auto changed = false;
|
||||||
|
|
||||||
|
if (settings.doTranslation && settings.translators.isEmpty() &&
|
||||||
|
!models.translators().isEmpty()) {
|
||||||
|
settings.translators = models.translators();
|
||||||
|
changed = true;
|
||||||
|
}
|
||||||
|
return changed;
|
||||||
|
}
|
||||||
|
|
||||||
QString SettingsValidator::toString(Error error) const
|
QString SettingsValidator::toString(Error error) const
|
||||||
{
|
{
|
||||||
switch (error) {
|
switch (error) {
|
||||||
|
@ -17,5 +17,6 @@ public:
|
|||||||
|
|
||||||
QVector<Error> check(const Settings& settings,
|
QVector<Error> check(const Settings& settings,
|
||||||
const CommonModels& models) const;
|
const CommonModels& models) const;
|
||||||
|
bool correct(Settings& settings, const CommonModels& models);
|
||||||
QString toString(Error error) const;
|
QString toString(Error error) const;
|
||||||
};
|
};
|
||||||
|
@ -132,11 +132,11 @@ void Translator::updateSettings()
|
|||||||
return;
|
return;
|
||||||
|
|
||||||
const auto loaded =
|
const auto loaded =
|
||||||
loadScripts(settings_.translatorsDir, settings_.translators);
|
loadScripts(settings_.translatorsPath, settings_.translators);
|
||||||
if (loaded.empty()) {
|
if (loaded.empty()) {
|
||||||
manager_.fatalError(
|
manager_.fatalError(
|
||||||
tr("No translators loaded from\n%1\n(%2)")
|
tr("No translators loaded from\n%1\n(%2)")
|
||||||
.arg(settings_.translatorsDir, settings_.translators.join(", ")));
|
.arg(settings_.translatorsPath, settings_.translators.join(", ")));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user