Move common model to separate class

This commit is contained in:
Gres 2020-03-28 14:26:05 +03:00
parent 87987f4a71
commit 258073c785
16 changed files with 131 additions and 79 deletions

View File

@ -34,6 +34,7 @@ HEADERS += \
src/capture/captureareaeditor.h \ src/capture/captureareaeditor.h \
src/capture/captureareaselector.h \ src/capture/captureareaselector.h \
src/capture/capturer.h \ src/capture/capturer.h \
src/commonmodels.h \
src/correct/corrector.h \ src/correct/corrector.h \
src/languagecodes.h \ src/languagecodes.h \
src/manager.h \ src/manager.h \
@ -65,6 +66,7 @@ SOURCES += \
src/capture/captureareaeditor.cpp \ src/capture/captureareaeditor.cpp \
src/capture/captureareaselector.cpp \ src/capture/captureareaselector.cpp \
src/capture/capturer.cpp \ src/capture/capturer.cpp \
src/commonmodels.cpp \
src/correct/corrector.cpp \ src/correct/corrector.cpp \
src/languagecodes.cpp \ src/languagecodes.cpp \
src/main.cpp \ src/main.cpp \

View File

@ -1,27 +1,22 @@
#include "captureareaeditor.h" #include "captureareaeditor.h"
#include "capturearea.h" #include "capturearea.h"
#include "captureareaselector.h" #include "captureareaselector.h"
#include "debug.h" #include "commonmodels.h"
#include "languagecodes.h" #include "languagecodes.h"
#include "settings.h"
#include "tesseract.h"
#include "translator.h"
#include <QCheckBox> #include <QCheckBox>
#include <QComboBox> #include <QComboBox>
#include <QGridLayout> #include <QGridLayout>
#include <QLabel> #include <QLabel>
#include <QPushButton> #include <QPushButton>
#include <QStringListModel>
CaptureAreaEditor::CaptureAreaEditor(CaptureAreaSelector &selector) CaptureAreaEditor::CaptureAreaEditor(const CommonModels &models,
CaptureAreaSelector &selector)
: QWidget(&selector) : QWidget(&selector)
, selector_(selector) , selector_(selector)
, doTranslation_(new QCheckBox(tr("Translate:"), this)) , doTranslation_(new QCheckBox(tr("Translate:"), this))
, sourceLanguage_(new QComboBox(this)) , sourceLanguage_(new QComboBox(this))
, targetLanguage_(new QComboBox(this)) , targetLanguage_(new QComboBox(this))
, sourceLanguageModel_(std::make_unique<QStringListModel>())
, targetLanguageModel_(std::make_unique<QStringListModel>())
{ {
setCursor(Qt::CursorShape::ArrowCursor); setCursor(Qt::CursorShape::ArrowCursor);
@ -36,8 +31,8 @@ CaptureAreaEditor::CaptureAreaEditor(CaptureAreaSelector &selector)
layout->addWidget(doTranslation_, row, 0); layout->addWidget(doTranslation_, row, 0);
layout->addWidget(targetLanguage_, row, 1); layout->addWidget(targetLanguage_, row, 1);
sourceLanguage_->setModel(sourceLanguageModel_.get()); sourceLanguage_->setModel(models.sourceLanguageModel());
targetLanguage_->setModel(targetLanguageModel_.get()); targetLanguage_->setModel(models.targetLanguageModel());
targetLanguage_->setEnabled(doTranslation_->isChecked()); targetLanguage_->setEnabled(doTranslation_->isChecked());
swapLanguages->setFlat(true); swapLanguages->setFlat(true);
@ -56,13 +51,6 @@ CaptureAreaEditor::CaptureAreaEditor(CaptureAreaSelector &selector)
CaptureAreaEditor::~CaptureAreaEditor() = default; CaptureAreaEditor::~CaptureAreaEditor() = default;
void CaptureAreaEditor::updateSettings(const Settings &settings)
{
sourceLanguageModel_->setStringList(
Tesseract::availableLanguageNames(settings.tessdataPath));
targetLanguageModel_->setStringList(Translator::availableLanguageNames());
}
void CaptureAreaEditor::swapLanguages() void CaptureAreaEditor::swapLanguages()
{ {
const auto target = targetLanguage_->currentText(); const auto target = targetLanguage_->currentText();

View File

@ -6,18 +6,17 @@
class QCheckBox; class QCheckBox;
class QComboBox; class QComboBox;
class QStringListModel;
class CaptureAreaEditor : public QWidget class CaptureAreaEditor : public QWidget
{ {
Q_OBJECT Q_OBJECT
public: public:
explicit CaptureAreaEditor(CaptureAreaSelector& selector); explicit CaptureAreaEditor(const CommonModels& models,
CaptureAreaSelector& selector);
~CaptureAreaEditor(); ~CaptureAreaEditor();
void set(const CaptureArea& area); void set(const CaptureArea& area);
void apply(CaptureArea& area) const; void apply(CaptureArea& area) const;
void updateSettings(const Settings& settings);
private: private:
void swapLanguages(); void swapLanguages();
@ -26,6 +25,4 @@ private:
QCheckBox* doTranslation_; QCheckBox* doTranslation_;
QComboBox* sourceLanguage_; QComboBox* sourceLanguage_;
QComboBox* targetLanguage_; QComboBox* targetLanguage_;
std::unique_ptr<QStringListModel> sourceLanguageModel_;
std::unique_ptr<QStringListModel> targetLanguageModel_;
}; };

View File

@ -12,11 +12,12 @@
CaptureAreaSelector::CaptureAreaSelector(Capturer &capturer, CaptureAreaSelector::CaptureAreaSelector(Capturer &capturer,
const Settings &settings, const Settings &settings,
const CommonModels &models,
const QPixmap &pixmap) const QPixmap &pixmap)
: capturer_(capturer) : capturer_(capturer)
, settings_(settings) , settings_(settings)
, pixmap_(pixmap) , pixmap_(pixmap)
, editor_(std::make_unique<CaptureAreaEditor>(*this)) , editor_(std::make_unique<CaptureAreaEditor>(models, *this))
{ {
setWindowFlags(Qt::FramelessWindowHint | Qt::NoDropShadowWindowHint | setWindowFlags(Qt::FramelessWindowHint | Qt::NoDropShadowWindowHint |
Qt::WindowStaysOnTopHint | Qt::X11BypassWindowManagerHint); Qt::WindowStaysOnTopHint | Qt::X11BypassWindowManagerHint);
@ -63,9 +64,6 @@ Left click on selection - process)")
.arg(sourceName, targetName, translationState); .arg(sourceName, targetName, translationState);
area_.reset(); area_.reset();
SOFT_ASSERT(editor_, return );
editor_->updateSettings(settings_);
} }
void CaptureAreaSelector::paintEvent(QPaintEvent * /*event*/) void CaptureAreaSelector::paintEvent(QPaintEvent * /*event*/)

View File

@ -10,7 +10,7 @@ class CaptureAreaSelector : public QWidget
public: public:
CaptureAreaSelector(Capturer &capturer, const Settings &settings, CaptureAreaSelector(Capturer &capturer, const Settings &settings,
const QPixmap &pixmap); const CommonModels &models, const QPixmap &pixmap);
~CaptureAreaSelector(); ~CaptureAreaSelector();
void activate(); void activate();

View File

@ -10,10 +10,12 @@
#include <QPainter> #include <QPainter>
#include <QScreen> #include <QScreen>
Capturer::Capturer(Manager &manager, const Settings &settings) Capturer::Capturer(Manager &manager, const Settings &settings,
const CommonModels &models)
: manager_(manager) : manager_(manager)
, settings_(settings) , settings_(settings)
, selector_(std::make_unique<CaptureAreaSelector>(*this, settings_, pixmap_)) , selector_(std::make_unique<CaptureAreaSelector>(*this, settings_, models,
pixmap_))
{ {
} }

View File

@ -7,7 +7,8 @@
class Capturer class Capturer
{ {
public: public:
Capturer(Manager &manager, const Settings &settings); Capturer(Manager &manager, const Settings &settings,
const CommonModels &models);
~Capturer(); ~Capturer();
void capture(); void capture();

40
src/commonmodels.cpp Normal file
View File

@ -0,0 +1,40 @@
#include "commonmodels.h"
#include "settings.h"
#include "tesseract.h"
#include "translator.h"
CommonModels::CommonModels()
: sourceLanguageModel_(std::make_unique<QStringListModel>())
, targetLanguageModel_(std::make_unique<QStringListModel>())
{
}
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();
}

23
src/commonmodels.h Normal file
View File

@ -0,0 +1,23 @@
#pragma once
#include "stfwd.h"
#include <QStringListModel>
#include <memory>
class CommonModels
{
public:
CommonModels();
~CommonModels();
void update(const QString& tessdataPath);
QStringListModel* sourceLanguageModel() const;
QStringListModel* targetLanguageModel() const;
private:
std::unique_ptr<QStringListModel> sourceLanguageModel_;
std::unique_ptr<QStringListModel> targetLanguageModel_;
};

View File

@ -1,5 +1,6 @@
#include "manager.h" #include "manager.h"
#include "capturer.h" #include "capturer.h"
#include "commonmodels.h"
#include "corrector.h" #include "corrector.h"
#include "debug.h" #include "debug.h"
#include "recognizer.h" #include "recognizer.h"
@ -28,11 +29,12 @@ const auto updatesUrl =
Manager::Manager() Manager::Manager()
: settings_(std::make_unique<Settings>()) : settings_(std::make_unique<Settings>())
, updater_(std::make_unique<update::Loader>(QUrl(updatesUrl))) , updater_(std::make_unique<update::Loader>(QUrl(updatesUrl)))
, models_(std::make_unique<CommonModels>())
{ {
SOFT_ASSERT(settings_, return ); SOFT_ASSERT(settings_, return );
tray_ = std::make_unique<TrayIcon>(*this, *settings_); tray_ = std::make_unique<TrayIcon>(*this, *settings_);
capturer_ = std::make_unique<Capturer>(*this, *settings_); capturer_ = std::make_unique<Capturer>(*this, *settings_, *models_);
recognizer_ = std::make_unique<Recognizer>(*this, *settings_); recognizer_ = std::make_unique<Recognizer>(*this, *settings_);
translator_ = std::make_unique<Translator>(*this, *settings_); translator_ = std::make_unique<Translator>(*this, *settings_);
corrector_ = std::make_unique<Corrector>(*this, *settings_); corrector_ = std::make_unique<Corrector>(*this, *settings_);
@ -74,6 +76,8 @@ void Manager::updateSettings()
setupProxy(*settings_); setupProxy(*settings_);
setupUpdates(*settings_); setupUpdates(*settings_);
models_->update(settings_->tessdataPath);
tray_->updateSettings(); tray_->updateSettings();
capturer_->updateSettings(); capturer_->updateSettings();
recognizer_->updateSettings(); recognizer_->updateSettings();

View File

@ -41,5 +41,6 @@ private:
std::unique_ptr<Representer> representer_; std::unique_ptr<Representer> representer_;
std::unique_ptr<update::Loader> updater_; std::unique_ptr<update::Loader> updater_;
std::unique_ptr<update::AutoChecker> updateAutoChecker_; std::unique_ptr<update::AutoChecker> updateAutoChecker_;
std::unique_ptr<CommonModels> models_;
int activeTaskCount_{0}; int activeTaskCount_{0};
}; };

View File

@ -54,13 +54,18 @@ SettingsEditor::SettingsEditor(Manager &manager, update::Loader &updater)
ui->proxyPassEdit->setEchoMode(QLineEdit::PasswordEchoOnEdit); ui->proxyPassEdit->setEchoMode(QLineEdit::PasswordEchoOnEdit);
} }
// translation
ui->tesseractLangCombo->setModel(models_.sourceLanguageModel());
// correction // correction
ui->userSubstitutionsTable->setEnabled(ui->useUserSubstitutions->isChecked()); ui->userSubstitutionsTable->setEnabled(ui->useUserSubstitutions->isChecked());
ui->userSubstitutionsTable->setSourceLanguageModel(
models_.sourceLanguageModel());
connect(ui->useUserSubstitutions, &QCheckBox::toggled, // connect(ui->useUserSubstitutions, &QCheckBox::toggled, //
ui->userSubstitutionsTable, &QTableWidget::setEnabled); ui->userSubstitutionsTable, &QTableWidget::setEnabled);
// translation // translation
updateTranslationLanguages(); ui->translateLangCombo->setModel(models_.targetLanguageModel());
// representation // representation
connect(ui->dialogRadio, &QRadioButton::toggled, // connect(ui->dialogRadio, &QRadioButton::toggled, //
@ -154,6 +159,9 @@ Settings SettingsEditor::settings() const
void SettingsEditor::setSettings(const Settings &settings) void SettingsEditor::setSettings(const Settings &settings)
{ {
if (settings.isPortable() == ui->portable->isChecked())
updateModels(settings.tessdataPath);
wasPortable_ = settings.isPortable(); wasPortable_ = settings.isPortable();
ui->portable->setChecked(settings.isPortable()); ui->portable->setChecked(settings.isPortable());
@ -174,12 +182,10 @@ void SettingsEditor::setSettings(const Settings &settings)
ui->proxySaveCheck->setChecked(settings.proxySavePassword); ui->proxySaveCheck->setChecked(settings.proxySavePassword);
ui->tessdataPath->setText(settings.tessdataPath); ui->tessdataPath->setText(settings.tessdataPath);
updateTesseractLanguages();
ui->tesseractLangCombo->setCurrentText( ui->tesseractLangCombo->setCurrentText(
LanguageCodes::name(settings.sourceLanguage)); LanguageCodes::name(settings.sourceLanguage));
ui->useUserSubstitutions->setChecked(settings.useUserSubstitutions); ui->useUserSubstitutions->setChecked(settings.useUserSubstitutions);
ui->userSubstitutionsTable->setTessdataPath(settings.tessdataPath);
ui->userSubstitutionsTable->setSubstitutions(settings.userSubstitutions); ui->userSubstitutionsTable->setSubstitutions(settings.userSubstitutions);
ui->doTranslationCheck->setChecked(settings.doTranslation); ui->doTranslationCheck->setChecked(settings.doTranslation);
@ -207,18 +213,6 @@ void SettingsEditor::updateCurrentPage()
ui->pagesView->setCurrentIndex(ui->pagesList->currentIndex().row()); 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() void SettingsEditor::updateTranslators()
{ {
ui->translatorList->clear(); 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() void SettingsEditor::adjustUpdatesView()
{ {
ui->updatesView->resizeColumnToContents(int(update::Model::Column::Name)); ui->updatesView->resizeColumnToContents(int(update::Model::Column::Name));
updateTesseractLanguages();
if (ui->tessdataPath->text().isEmpty()) // not inited yet
return;
updateModels(ui->tessdataPath->text());
updateTranslators(); updateTranslators();
} }
@ -287,8 +273,7 @@ void SettingsEditor::handlePortableChanged()
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.translatorsDir);
ui->userSubstitutionsTable->setTessdataPath(settings.tessdataPath); updateModels(settings.tessdataPath);
updateTesseractLanguages();
updateTranslators(); updateTranslators();
const auto portableChanged = wasPortable_ != settings.isPortable(); const auto portableChanged = wasPortable_ != settings.isPortable();
@ -304,3 +289,10 @@ void SettingsEditor::updateResultFont()
font.setPointSize(ui->resultFontSize->value()); font.setPointSize(ui->resultFontSize->value());
ui->resultFont->setFont(font); ui->resultFont->setFont(font);
} }
void SettingsEditor::updateModels(const QString &tessdataPath)
{
const auto source = ui->tesseractLangCombo->currentText();
models_.update(tessdataPath);
ui->tesseractLangCombo->setCurrentText(source);
}

View File

@ -2,6 +2,7 @@
#include <QDialog> #include <QDialog>
#include "commonmodels.h"
#include "settings.h" #include "settings.h"
namespace Ui namespace Ui
@ -23,17 +24,17 @@ public:
private: private:
void updateCurrentPage(); void updateCurrentPage();
void updateTesseractLanguages();
void updateTranslators(); void updateTranslators();
void updateTranslationLanguages();
void adjustUpdatesView(); void adjustUpdatesView();
void handleButtonBoxClicked(QAbstractButton *button); void handleButtonBoxClicked(QAbstractButton *button);
void handlePortableChanged(); void handlePortableChanged();
void updateResultFont(); void updateResultFont();
void updateModels(const QString &tessdataPath);
Ui::SettingsEditor *ui; Ui::SettingsEditor *ui;
Manager &manager_; Manager &manager_;
update::Loader &updater_; update::Loader &updater_;
CommonModels models_;
QStringList enabledTranslators_; QStringList enabledTranslators_;
bool wasPortable_{false}; bool wasPortable_{false};
}; };

View File

@ -18,6 +18,7 @@ class Recognizer;
class CaptureArea; class CaptureArea;
class CaptureAreaSelector; class CaptureAreaSelector;
class CaptureAreaEditor; class CaptureAreaEditor;
class CommonModels;
namespace update namespace update
{ {

View File

@ -1,7 +1,6 @@
#include "substitutionstable.h" #include "substitutionstable.h"
#include "debug.h" #include "debug.h"
#include "languagecodes.h" #include "languagecodes.h"
#include "tesseract.h"
#include <QComboBox> #include <QComboBox>
#include <QPainter> #include <QPainter>
@ -29,7 +28,7 @@ public:
SubstitutionsTable::SubstitutionsTable(QWidget *parent) SubstitutionsTable::SubstitutionsTable(QWidget *parent)
: QTableWidget(parent) : QTableWidget(parent)
, languagesModel_(new QStringListModel(this)) , substitutionLanguages_(new QStringListModel(this))
{ {
setItemDelegate(new SubstitutionDelegate(this)); setItemDelegate(new SubstitutionDelegate(this));
setColumnCount(int(Column::Count)); setColumnCount(int(Column::Count));
@ -38,6 +37,16 @@ SubstitutionsTable::SubstitutionsTable(QWidget *parent)
this, &SubstitutionsTable::handleItemChange); 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) void SubstitutionsTable::setSubstitutions(const Substitutions &substitutions)
{ {
setRowCount(0); setRowCount(0);
@ -50,17 +59,9 @@ void SubstitutionsTable::setSubstitutions(const Substitutions &substitutions)
resizeColumnsToContents(); 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) void SubstitutionsTable::updateModel(const Substitutions &substitutions)
{ {
auto strings = Tesseract::availableLanguageNames(tessdataPath_); auto strings = sourceLanguages_->stringList();
if (!substitutions.empty()) { if (!substitutions.empty()) {
for (const auto &i : substitutions) { for (const auto &i : substitutions) {
@ -73,7 +74,7 @@ void SubstitutionsTable::updateModel(const Substitutions &substitutions)
} }
std::sort(strings.begin(), strings.end()); std::sort(strings.begin(), strings.end());
languagesModel_->setStringList(strings); substitutionLanguages_->setStringList(strings);
} }
Substitutions SubstitutionsTable::substitutions() const Substitutions SubstitutionsTable::substitutions() const
@ -97,7 +98,7 @@ void SubstitutionsTable::addRow(const LanguageId &language,
insertRow(row); insertRow(row);
auto combo = new QComboBox(this); auto combo = new QComboBox(this);
combo->setModel(languagesModel_); combo->setModel(substitutionLanguages_);
using E = Column; using E = Column;
if (!language.isEmpty()) { if (!language.isEmpty()) {

View File

@ -14,8 +14,9 @@ public:
explicit SubstitutionsTable(QWidget* parent = nullptr); explicit SubstitutionsTable(QWidget* parent = nullptr);
void setSourceLanguageModel(QStringListModel* model);
void setSubstitutions(const Substitutions& substitutions); void setSubstitutions(const Substitutions& substitutions);
void setTessdataPath(const QString& tessdataPath);
Substitutions substitutions() const; Substitutions substitutions() const;
private: private:
@ -25,6 +26,6 @@ private:
std::pair<LanguageId, Substitution> at(int row) const; std::pair<LanguageId, Substitution> at(int row) const;
void updateModel(const Substitutions& substitutions); void updateModel(const Substitutions& substitutions);
QStringListModel* languagesModel_; QStringListModel* sourceLanguages_{nullptr};
QString tessdataPath_; QStringListModel* substitutionLanguages_;
}; };