From 258073c785b11cf9fb77d388e026e5716a879987 Mon Sep 17 00:00:00 2001 From: Gres Date: Sat, 28 Mar 2020 14:26:05 +0300 Subject: [PATCH] Move common model to separate class --- screen-translator.pro | 2 ++ src/capture/captureareaeditor.cpp | 22 +++--------- src/capture/captureareaeditor.h | 7 ++-- src/capture/captureareaselector.cpp | 6 ++-- src/capture/captureareaselector.h | 2 +- src/capture/capturer.cpp | 6 ++-- src/capture/capturer.h | 3 +- src/commonmodels.cpp | 40 ++++++++++++++++++++++ src/commonmodels.h | 23 +++++++++++++ src/manager.cpp | 6 +++- src/manager.h | 1 + src/settingseditor.cpp | 52 ++++++++++++----------------- src/settingseditor.h | 5 +-- src/stfwd.h | 1 + src/substitutionstable.cpp | 27 +++++++-------- src/substitutionstable.h | 7 ++-- 16 files changed, 131 insertions(+), 79 deletions(-) create mode 100644 src/commonmodels.cpp create mode 100644 src/commonmodels.h diff --git a/screen-translator.pro b/screen-translator.pro index fc59e0e..0b8efd6 100644 --- a/screen-translator.pro +++ b/screen-translator.pro @@ -34,6 +34,7 @@ HEADERS += \ src/capture/captureareaeditor.h \ src/capture/captureareaselector.h \ src/capture/capturer.h \ + src/commonmodels.h \ src/correct/corrector.h \ src/languagecodes.h \ src/manager.h \ @@ -65,6 +66,7 @@ SOURCES += \ src/capture/captureareaeditor.cpp \ src/capture/captureareaselector.cpp \ src/capture/capturer.cpp \ + src/commonmodels.cpp \ src/correct/corrector.cpp \ src/languagecodes.cpp \ src/main.cpp \ diff --git a/src/capture/captureareaeditor.cpp b/src/capture/captureareaeditor.cpp index 6be8d6a..086a15a 100644 --- a/src/capture/captureareaeditor.cpp +++ b/src/capture/captureareaeditor.cpp @@ -1,27 +1,22 @@ #include "captureareaeditor.h" #include "capturearea.h" #include "captureareaselector.h" -#include "debug.h" +#include "commonmodels.h" #include "languagecodes.h" -#include "settings.h" -#include "tesseract.h" -#include "translator.h" #include #include #include #include #include -#include -CaptureAreaEditor::CaptureAreaEditor(CaptureAreaSelector &selector) +CaptureAreaEditor::CaptureAreaEditor(const CommonModels &models, + CaptureAreaSelector &selector) : QWidget(&selector) , selector_(selector) , doTranslation_(new QCheckBox(tr("Translate:"), this)) , sourceLanguage_(new QComboBox(this)) , targetLanguage_(new QComboBox(this)) - , sourceLanguageModel_(std::make_unique()) - , targetLanguageModel_(std::make_unique()) { setCursor(Qt::CursorShape::ArrowCursor); @@ -36,8 +31,8 @@ CaptureAreaEditor::CaptureAreaEditor(CaptureAreaSelector &selector) layout->addWidget(doTranslation_, row, 0); layout->addWidget(targetLanguage_, row, 1); - sourceLanguage_->setModel(sourceLanguageModel_.get()); - targetLanguage_->setModel(targetLanguageModel_.get()); + sourceLanguage_->setModel(models.sourceLanguageModel()); + targetLanguage_->setModel(models.targetLanguageModel()); targetLanguage_->setEnabled(doTranslation_->isChecked()); swapLanguages->setFlat(true); @@ -56,13 +51,6 @@ CaptureAreaEditor::CaptureAreaEditor(CaptureAreaSelector &selector) CaptureAreaEditor::~CaptureAreaEditor() = default; -void CaptureAreaEditor::updateSettings(const Settings &settings) -{ - sourceLanguageModel_->setStringList( - Tesseract::availableLanguageNames(settings.tessdataPath)); - targetLanguageModel_->setStringList(Translator::availableLanguageNames()); -} - void CaptureAreaEditor::swapLanguages() { const auto target = targetLanguage_->currentText(); diff --git a/src/capture/captureareaeditor.h b/src/capture/captureareaeditor.h index a048de9..4a66704 100644 --- a/src/capture/captureareaeditor.h +++ b/src/capture/captureareaeditor.h @@ -6,18 +6,17 @@ class QCheckBox; class QComboBox; -class QStringListModel; class CaptureAreaEditor : public QWidget { Q_OBJECT public: - explicit CaptureAreaEditor(CaptureAreaSelector& selector); + explicit CaptureAreaEditor(const CommonModels& models, + CaptureAreaSelector& selector); ~CaptureAreaEditor(); void set(const CaptureArea& area); void apply(CaptureArea& area) const; - void updateSettings(const Settings& settings); private: void swapLanguages(); @@ -26,6 +25,4 @@ private: QCheckBox* doTranslation_; QComboBox* sourceLanguage_; QComboBox* targetLanguage_; - std::unique_ptr sourceLanguageModel_; - std::unique_ptr targetLanguageModel_; }; diff --git a/src/capture/captureareaselector.cpp b/src/capture/captureareaselector.cpp index 22387ec..6aab37d 100644 --- a/src/capture/captureareaselector.cpp +++ b/src/capture/captureareaselector.cpp @@ -12,11 +12,12 @@ CaptureAreaSelector::CaptureAreaSelector(Capturer &capturer, const Settings &settings, + const CommonModels &models, const QPixmap &pixmap) : capturer_(capturer) , settings_(settings) , pixmap_(pixmap) - , editor_(std::make_unique(*this)) + , editor_(std::make_unique(models, *this)) { setWindowFlags(Qt::FramelessWindowHint | Qt::NoDropShadowWindowHint | Qt::WindowStaysOnTopHint | Qt::X11BypassWindowManagerHint); @@ -63,9 +64,6 @@ Left click on selection - process)") .arg(sourceName, targetName, translationState); area_.reset(); - - SOFT_ASSERT(editor_, return ); - editor_->updateSettings(settings_); } void CaptureAreaSelector::paintEvent(QPaintEvent * /*event*/) diff --git a/src/capture/captureareaselector.h b/src/capture/captureareaselector.h index 89e014a..958cf63 100644 --- a/src/capture/captureareaselector.h +++ b/src/capture/captureareaselector.h @@ -10,7 +10,7 @@ class CaptureAreaSelector : public QWidget public: CaptureAreaSelector(Capturer &capturer, const Settings &settings, - const QPixmap &pixmap); + const CommonModels &models, const QPixmap &pixmap); ~CaptureAreaSelector(); void activate(); diff --git a/src/capture/capturer.cpp b/src/capture/capturer.cpp index 99772fd..e2ad589 100644 --- a/src/capture/capturer.cpp +++ b/src/capture/capturer.cpp @@ -10,10 +10,12 @@ #include #include -Capturer::Capturer(Manager &manager, const Settings &settings) +Capturer::Capturer(Manager &manager, const Settings &settings, + const CommonModels &models) : manager_(manager) , settings_(settings) - , selector_(std::make_unique(*this, settings_, pixmap_)) + , selector_(std::make_unique(*this, settings_, models, + pixmap_)) { } diff --git a/src/capture/capturer.h b/src/capture/capturer.h index 558e30a..abf75ef 100644 --- a/src/capture/capturer.h +++ b/src/capture/capturer.h @@ -7,7 +7,8 @@ class Capturer { public: - Capturer(Manager &manager, const Settings &settings); + Capturer(Manager &manager, const Settings &settings, + const CommonModels &models); ~Capturer(); void capture(); diff --git a/src/commonmodels.cpp b/src/commonmodels.cpp new file mode 100644 index 0000000..38a55b6 --- /dev/null +++ b/src/commonmodels.cpp @@ -0,0 +1,40 @@ +#include "commonmodels.h" +#include "settings.h" +#include "tesseract.h" +#include "translator.h" + +CommonModels::CommonModels() + : sourceLanguageModel_(std::make_unique()) + , targetLanguageModel_(std::make_unique()) +{ +} + +CommonModels::~CommonModels() = default; + +void CommonModels::update(const QString &tessdataPath) +{ + { + auto names = Tesseract::availableLanguageNames(tessdataPath); + std::sort(names.begin(), names.end()); + sourceLanguageModel_->setStringList(names); + } + + if (targetLanguageModel_->rowCount() > 0) + return; + + { + auto names = Translator::availableLanguageNames(); + std::sort(names.begin(), names.end()); + targetLanguageModel_->setStringList(names); + } +} + +QStringListModel *CommonModels::sourceLanguageModel() const +{ + return sourceLanguageModel_.get(); +} + +QStringListModel *CommonModels::targetLanguageModel() const +{ + return targetLanguageModel_.get(); +} diff --git a/src/commonmodels.h b/src/commonmodels.h new file mode 100644 index 0000000..9329281 --- /dev/null +++ b/src/commonmodels.h @@ -0,0 +1,23 @@ +#pragma once + +#include "stfwd.h" + +#include + +#include + +class CommonModels +{ +public: + CommonModels(); + ~CommonModels(); + + void update(const QString& tessdataPath); + + QStringListModel* sourceLanguageModel() const; + QStringListModel* targetLanguageModel() const; + +private: + std::unique_ptr sourceLanguageModel_; + std::unique_ptr targetLanguageModel_; +}; diff --git a/src/manager.cpp b/src/manager.cpp index 23d8308..1463825 100644 --- a/src/manager.cpp +++ b/src/manager.cpp @@ -1,5 +1,6 @@ #include "manager.h" #include "capturer.h" +#include "commonmodels.h" #include "corrector.h" #include "debug.h" #include "recognizer.h" @@ -28,11 +29,12 @@ const auto updatesUrl = Manager::Manager() : settings_(std::make_unique()) , updater_(std::make_unique(QUrl(updatesUrl))) + , models_(std::make_unique()) { SOFT_ASSERT(settings_, return ); tray_ = std::make_unique(*this, *settings_); - capturer_ = std::make_unique(*this, *settings_); + capturer_ = std::make_unique(*this, *settings_, *models_); recognizer_ = std::make_unique(*this, *settings_); translator_ = std::make_unique(*this, *settings_); corrector_ = std::make_unique(*this, *settings_); @@ -74,6 +76,8 @@ void Manager::updateSettings() setupProxy(*settings_); setupUpdates(*settings_); + models_->update(settings_->tessdataPath); + tray_->updateSettings(); capturer_->updateSettings(); recognizer_->updateSettings(); diff --git a/src/manager.h b/src/manager.h index a5678db..3f7c13d 100644 --- a/src/manager.h +++ b/src/manager.h @@ -41,5 +41,6 @@ private: std::unique_ptr representer_; std::unique_ptr updater_; std::unique_ptr updateAutoChecker_; + std::unique_ptr models_; int activeTaskCount_{0}; }; diff --git a/src/settingseditor.cpp b/src/settingseditor.cpp index 68fbe9c..db8b4c3 100644 --- a/src/settingseditor.cpp +++ b/src/settingseditor.cpp @@ -54,13 +54,18 @@ SettingsEditor::SettingsEditor(Manager &manager, update::Loader &updater) ui->proxyPassEdit->setEchoMode(QLineEdit::PasswordEchoOnEdit); } + // translation + ui->tesseractLangCombo->setModel(models_.sourceLanguageModel()); + // correction ui->userSubstitutionsTable->setEnabled(ui->useUserSubstitutions->isChecked()); + ui->userSubstitutionsTable->setSourceLanguageModel( + models_.sourceLanguageModel()); connect(ui->useUserSubstitutions, &QCheckBox::toggled, // ui->userSubstitutionsTable, &QTableWidget::setEnabled); // translation - updateTranslationLanguages(); + ui->translateLangCombo->setModel(models_.targetLanguageModel()); // representation connect(ui->dialogRadio, &QRadioButton::toggled, // @@ -154,6 +159,9 @@ Settings SettingsEditor::settings() const void SettingsEditor::setSettings(const Settings &settings) { + if (settings.isPortable() == ui->portable->isChecked()) + updateModels(settings.tessdataPath); + wasPortable_ = settings.isPortable(); ui->portable->setChecked(settings.isPortable()); @@ -174,12 +182,10 @@ void SettingsEditor::setSettings(const Settings &settings) ui->proxySaveCheck->setChecked(settings.proxySavePassword); ui->tessdataPath->setText(settings.tessdataPath); - updateTesseractLanguages(); ui->tesseractLangCombo->setCurrentText( LanguageCodes::name(settings.sourceLanguage)); ui->useUserSubstitutions->setChecked(settings.useUserSubstitutions); - ui->userSubstitutionsTable->setTessdataPath(settings.tessdataPath); ui->userSubstitutionsTable->setSubstitutions(settings.userSubstitutions); ui->doTranslationCheck->setChecked(settings.doTranslation); @@ -207,18 +213,6 @@ void SettingsEditor::updateCurrentPage() ui->pagesView->setCurrentIndex(ui->pagesList->currentIndex().row()); } -void SettingsEditor::updateTesseractLanguages() -{ - ui->tesseractLangCombo->clear(); - - auto names = Tesseract::availableLanguageNames(ui->tessdataPath->text()); - if (names.isEmpty()) - return; - - std::sort(names.begin(), names.end()); - ui->tesseractLangCombo->addItems(names); -} - void SettingsEditor::updateTranslators() { ui->translatorList->clear(); @@ -238,22 +232,14 @@ void SettingsEditor::updateTranslators() } } -void SettingsEditor::updateTranslationLanguages() -{ - ui->translateLangCombo->clear(); - - auto names = Translator::availableLanguageNames(); - if (names.isEmpty()) - return; - - std::sort(names.begin(), names.end()); - ui->translateLangCombo->addItems(names); -} - void SettingsEditor::adjustUpdatesView() { ui->updatesView->resizeColumnToContents(int(update::Model::Column::Name)); - updateTesseractLanguages(); + + if (ui->tessdataPath->text().isEmpty()) // not inited yet + return; + + updateModels(ui->tessdataPath->text()); updateTranslators(); } @@ -287,8 +273,7 @@ void SettingsEditor::handlePortableChanged() settings.setPortable(ui->portable->isChecked()); ui->tessdataPath->setText(settings.tessdataPath); ui->translatorsPath->setText(settings.translatorsDir); - ui->userSubstitutionsTable->setTessdataPath(settings.tessdataPath); - updateTesseractLanguages(); + updateModels(settings.tessdataPath); updateTranslators(); const auto portableChanged = wasPortable_ != settings.isPortable(); @@ -304,3 +289,10 @@ void SettingsEditor::updateResultFont() font.setPointSize(ui->resultFontSize->value()); ui->resultFont->setFont(font); } + +void SettingsEditor::updateModels(const QString &tessdataPath) +{ + const auto source = ui->tesseractLangCombo->currentText(); + models_.update(tessdataPath); + ui->tesseractLangCombo->setCurrentText(source); +} diff --git a/src/settingseditor.h b/src/settingseditor.h index 118f3d2..882c01b 100644 --- a/src/settingseditor.h +++ b/src/settingseditor.h @@ -2,6 +2,7 @@ #include +#include "commonmodels.h" #include "settings.h" namespace Ui @@ -23,17 +24,17 @@ public: private: void updateCurrentPage(); - void updateTesseractLanguages(); void updateTranslators(); - void updateTranslationLanguages(); void adjustUpdatesView(); void handleButtonBoxClicked(QAbstractButton *button); void handlePortableChanged(); void updateResultFont(); + void updateModels(const QString &tessdataPath); Ui::SettingsEditor *ui; Manager &manager_; update::Loader &updater_; + CommonModels models_; QStringList enabledTranslators_; bool wasPortable_{false}; }; diff --git a/src/stfwd.h b/src/stfwd.h index 6fdc23e..46a74eb 100644 --- a/src/stfwd.h +++ b/src/stfwd.h @@ -18,6 +18,7 @@ class Recognizer; class CaptureArea; class CaptureAreaSelector; class CaptureAreaEditor; +class CommonModels; namespace update { diff --git a/src/substitutionstable.cpp b/src/substitutionstable.cpp index b57160f..ab450e8 100644 --- a/src/substitutionstable.cpp +++ b/src/substitutionstable.cpp @@ -1,7 +1,6 @@ #include "substitutionstable.h" #include "debug.h" #include "languagecodes.h" -#include "tesseract.h" #include #include @@ -29,7 +28,7 @@ public: SubstitutionsTable::SubstitutionsTable(QWidget *parent) : QTableWidget(parent) - , languagesModel_(new QStringListModel(this)) + , substitutionLanguages_(new QStringListModel(this)) { setItemDelegate(new SubstitutionDelegate(this)); setColumnCount(int(Column::Count)); @@ -38,6 +37,16 @@ SubstitutionsTable::SubstitutionsTable(QWidget *parent) this, &SubstitutionsTable::handleItemChange); } +void SubstitutionsTable::setSourceLanguageModel(QStringListModel *model) +{ + sourceLanguages_ = model; + connect(model, &QStringListModel::modelReset, // + this, [this] { + if (rowCount() > 0) // must be at least 1 if inited + setSubstitutions(substitutions()); + }); +} + void SubstitutionsTable::setSubstitutions(const Substitutions &substitutions) { setRowCount(0); @@ -50,17 +59,9 @@ void SubstitutionsTable::setSubstitutions(const Substitutions &substitutions) resizeColumnsToContents(); } -void SubstitutionsTable::setTessdataPath(const QString &tessdataPath) -{ - tessdataPath_ = tessdataPath; - if (rowCount() == 0) // must be at least 1 if inited - return; - setSubstitutions(substitutions()); -} - void SubstitutionsTable::updateModel(const Substitutions &substitutions) { - auto strings = Tesseract::availableLanguageNames(tessdataPath_); + auto strings = sourceLanguages_->stringList(); if (!substitutions.empty()) { for (const auto &i : substitutions) { @@ -73,7 +74,7 @@ void SubstitutionsTable::updateModel(const Substitutions &substitutions) } std::sort(strings.begin(), strings.end()); - languagesModel_->setStringList(strings); + substitutionLanguages_->setStringList(strings); } Substitutions SubstitutionsTable::substitutions() const @@ -97,7 +98,7 @@ void SubstitutionsTable::addRow(const LanguageId &language, insertRow(row); auto combo = new QComboBox(this); - combo->setModel(languagesModel_); + combo->setModel(substitutionLanguages_); using E = Column; if (!language.isEmpty()) { diff --git a/src/substitutionstable.h b/src/substitutionstable.h index f6a1a1b..efbd05b 100644 --- a/src/substitutionstable.h +++ b/src/substitutionstable.h @@ -14,8 +14,9 @@ public: explicit SubstitutionsTable(QWidget* parent = nullptr); + void setSourceLanguageModel(QStringListModel* model); + void setSubstitutions(const Substitutions& substitutions); - void setTessdataPath(const QString& tessdataPath); Substitutions substitutions() const; private: @@ -25,6 +26,6 @@ private: std::pair at(int row) const; void updateModel(const Substitutions& substitutions); - QStringListModel* languagesModel_; - QString tessdataPath_; + QStringListModel* sourceLanguages_{nullptr}; + QStringListModel* substitutionLanguages_; };