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