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/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 \

View File

@ -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 <QCheckBox>
#include <QComboBox>
#include <QGridLayout>
#include <QLabel>
#include <QPushButton>
#include <QStringListModel>
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<QStringListModel>())
, targetLanguageModel_(std::make_unique<QStringListModel>())
{
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();

View File

@ -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<QStringListModel> sourceLanguageModel_;
std::unique_ptr<QStringListModel> targetLanguageModel_;
};

View File

@ -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<CaptureAreaEditor>(*this))
, editor_(std::make_unique<CaptureAreaEditor>(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*/)

View File

@ -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();

View File

@ -10,10 +10,12 @@
#include <QPainter>
#include <QScreen>
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<CaptureAreaSelector>(*this, settings_, pixmap_))
, selector_(std::make_unique<CaptureAreaSelector>(*this, settings_, models,
pixmap_))
{
}

View File

@ -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();

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 "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<Settings>())
, updater_(std::make_unique<update::Loader>(QUrl(updatesUrl)))
, models_(std::make_unique<CommonModels>())
{
SOFT_ASSERT(settings_, return );
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_);
translator_ = std::make_unique<Translator>(*this, *settings_);
corrector_ = std::make_unique<Corrector>(*this, *settings_);
@ -74,6 +76,8 @@ void Manager::updateSettings()
setupProxy(*settings_);
setupUpdates(*settings_);
models_->update(settings_->tessdataPath);
tray_->updateSettings();
capturer_->updateSettings();
recognizer_->updateSettings();

View File

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

View File

@ -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);
}

View File

@ -2,6 +2,7 @@
#include <QDialog>
#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};
};

View File

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

View File

@ -1,7 +1,6 @@
#include "substitutionstable.h"
#include "debug.h"
#include "languagecodes.h"
#include "tesseract.h"
#include <QComboBox>
#include <QPainter>
@ -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()) {

View File

@ -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<LanguageId, Substitution> at(int row) const;
void updateModel(const Substitutions& substitutions);
QStringListModel* languagesModel_;
QString tessdataPath_;
QStringListModel* sourceLanguages_{nullptr};
QStringListModel* substitutionLanguages_;
};