Keep only available languages in correction selector
This commit is contained in:
		
							parent
							
								
									8b84fef929
								
							
						
					
					
						commit
						124d32857f
					
				@ -7,8 +7,7 @@
 | 
			
		||||
#include <tesseract/baseapi.h>
 | 
			
		||||
 | 
			
		||||
#include <QBuffer>
 | 
			
		||||
 | 
			
		||||
#include <limits>
 | 
			
		||||
#include <QDir>
 | 
			
		||||
 | 
			
		||||
#if defined(Q_OS_LINUX)
 | 
			
		||||
#include <fstream>
 | 
			
		||||
@ -159,6 +158,33 @@ const QString &Tesseract::error() const
 | 
			
		||||
  return error_;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
QStringList Tesseract::availableLanguageNames(const QString &path)
 | 
			
		||||
{
 | 
			
		||||
  if (path.isEmpty())
 | 
			
		||||
    return {};
 | 
			
		||||
 | 
			
		||||
  QDir dir(path);
 | 
			
		||||
  if (!dir.exists())
 | 
			
		||||
    return {};
 | 
			
		||||
 | 
			
		||||
  LanguageIds names;
 | 
			
		||||
  LanguageCodes languages;
 | 
			
		||||
 | 
			
		||||
  const auto files = dir.entryList({"*.traineddata"}, QDir::Files);
 | 
			
		||||
  for (const auto &file : files) {
 | 
			
		||||
    const auto lang = file.left(file.indexOf("."));
 | 
			
		||||
    if (const auto bundle = languages.findByTesseract(lang))
 | 
			
		||||
      names.append(QObject::tr(bundle->name));
 | 
			
		||||
    else
 | 
			
		||||
      names.append(lang);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  if (names.isEmpty())
 | 
			
		||||
    return {};
 | 
			
		||||
 | 
			
		||||
  return names;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
QString Tesseract::recognize(const QPixmap &source)
 | 
			
		||||
{
 | 
			
		||||
  SOFT_ASSERT(engine_, return {});
 | 
			
		||||
 | 
			
		||||
@ -23,6 +23,8 @@ public:
 | 
			
		||||
  bool isValid() const;
 | 
			
		||||
  const QString& error() const;
 | 
			
		||||
 | 
			
		||||
  static QStringList availableLanguageNames(const QString& path);
 | 
			
		||||
 | 
			
		||||
private:
 | 
			
		||||
  void init(const LanguageId& language, const QString& tessdataPath);
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -1,12 +1,12 @@
 | 
			
		||||
#include "settingseditor.h"
 | 
			
		||||
#include "languagecodes.h"
 | 
			
		||||
#include "manager.h"
 | 
			
		||||
#include "tesseract.h"
 | 
			
		||||
#include "ui_settingseditor.h"
 | 
			
		||||
#include "updates.h"
 | 
			
		||||
#include "widgetstate.h"
 | 
			
		||||
 | 
			
		||||
#include <QFileDialog>
 | 
			
		||||
#include <QNetworkProxy>
 | 
			
		||||
#include <QSortFilterProxyModel>
 | 
			
		||||
#include <QStringListModel>
 | 
			
		||||
 | 
			
		||||
@ -161,6 +161,7 @@ void SettingsEditor::setSettings(const Settings &settings)
 | 
			
		||||
    ui->tesseractLangCombo->setCurrentText(QObject::tr(lang->name));
 | 
			
		||||
 | 
			
		||||
  ui->useUserSubstitutions->setChecked(settings.useUserSubstitutions);
 | 
			
		||||
  ui->userSubstitutionsTable->setTessdataPath(settings.tessdataPath);
 | 
			
		||||
  ui->userSubstitutionsTable->setSubstitutions(settings.userSubstitutions);
 | 
			
		||||
 | 
			
		||||
  ui->doTranslationCheck->setChecked(settings.doTranslation);
 | 
			
		||||
@ -188,24 +189,7 @@ void SettingsEditor::updateTesseractLanguages()
 | 
			
		||||
{
 | 
			
		||||
  ui->tesseractLangCombo->clear();
 | 
			
		||||
 | 
			
		||||
  const auto path = ui->tessdataPath->text();
 | 
			
		||||
  if (path.isEmpty())
 | 
			
		||||
    return;
 | 
			
		||||
 | 
			
		||||
  QDir dir(path);
 | 
			
		||||
  if (!dir.exists())
 | 
			
		||||
    return;
 | 
			
		||||
 | 
			
		||||
  LanguageIds names;
 | 
			
		||||
  LanguageCodes languages;
 | 
			
		||||
 | 
			
		||||
  const auto files = dir.entryList({"*.traineddata"}, QDir::Files);
 | 
			
		||||
  for (const auto &file : files) {
 | 
			
		||||
    const auto lang = file.left(file.indexOf("."));
 | 
			
		||||
    if (const auto bundle = languages.findByTesseract(lang))
 | 
			
		||||
      names.append(QObject::tr(bundle->name));
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  auto names = Tesseract::availableLanguageNames(ui->tessdataPath->text());
 | 
			
		||||
  if (names.isEmpty())
 | 
			
		||||
    return;
 | 
			
		||||
 | 
			
		||||
@ -289,6 +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();
 | 
			
		||||
  updateTranslators();
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -1,6 +1,7 @@
 | 
			
		||||
#include "substitutionstable.h"
 | 
			
		||||
#include "debug.h"
 | 
			
		||||
#include "languagecodes.h"
 | 
			
		||||
#include "tesseract.h"
 | 
			
		||||
 | 
			
		||||
#include <QComboBox>
 | 
			
		||||
#include <QPainter>
 | 
			
		||||
@ -9,22 +10,6 @@
 | 
			
		||||
 | 
			
		||||
namespace
 | 
			
		||||
{
 | 
			
		||||
QStringList allSourceLanguages()
 | 
			
		||||
{
 | 
			
		||||
  LanguageCodes langs;
 | 
			
		||||
  const auto &allLangs = langs.all();
 | 
			
		||||
 | 
			
		||||
  QStringList result;
 | 
			
		||||
  result.reserve(allLangs.size());
 | 
			
		||||
  for (const auto &i : allLangs) {
 | 
			
		||||
    if (i.second.tesseract.isEmpty())
 | 
			
		||||
      continue;
 | 
			
		||||
    result.append(QObject::tr(i.second.name));
 | 
			
		||||
  }
 | 
			
		||||
  std::sort(result.begin(), result.end());
 | 
			
		||||
  return result;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
class SubstitutionDelegate : public QStyledItemDelegate
 | 
			
		||||
{
 | 
			
		||||
public:
 | 
			
		||||
@ -44,7 +29,7 @@ public:
 | 
			
		||||
 | 
			
		||||
SubstitutionsTable::SubstitutionsTable(QWidget *parent)
 | 
			
		||||
  : QTableWidget(parent)
 | 
			
		||||
  , languagesModel_(new QStringListModel(allSourceLanguages(), this))
 | 
			
		||||
  , languagesModel_(new QStringListModel(this))
 | 
			
		||||
{
 | 
			
		||||
  setItemDelegate(new SubstitutionDelegate(this));
 | 
			
		||||
  setColumnCount(int(Column::Count));
 | 
			
		||||
@ -55,11 +40,44 @@ SubstitutionsTable::SubstitutionsTable(QWidget *parent)
 | 
			
		||||
 | 
			
		||||
void SubstitutionsTable::setSubstitutions(const Substitutions &substitutions)
 | 
			
		||||
{
 | 
			
		||||
  setRowCount(0);
 | 
			
		||||
 | 
			
		||||
  updateModel(substitutions);
 | 
			
		||||
 | 
			
		||||
  for (const auto &i : substitutions) addRow(i.first, i.second);
 | 
			
		||||
 | 
			
		||||
  addRow();  // for editing
 | 
			
		||||
  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_);
 | 
			
		||||
 | 
			
		||||
  if (!substitutions.empty()) {
 | 
			
		||||
    LanguageCodes languages;
 | 
			
		||||
    for (const auto &i : substitutions) {
 | 
			
		||||
      auto name = i.first;
 | 
			
		||||
      if (const auto bundle = languages.findByTesseract(name))
 | 
			
		||||
        name = QObject::tr(bundle->name);
 | 
			
		||||
 | 
			
		||||
      if (!strings.contains(name))
 | 
			
		||||
        strings.append(name);
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  std::sort(strings.begin(), strings.end());
 | 
			
		||||
  languagesModel_->setStringList(strings);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
Substitutions SubstitutionsTable::substitutions() const
 | 
			
		||||
{
 | 
			
		||||
  Substitutions result;
 | 
			
		||||
@ -88,6 +106,8 @@ void SubstitutionsTable::addRow(const LanguageId &language,
 | 
			
		||||
    LanguageCodes langs;
 | 
			
		||||
    if (auto lang = langs.findById(language))
 | 
			
		||||
      combo->setCurrentText(QObject::tr(lang->name));
 | 
			
		||||
    else
 | 
			
		||||
      combo->setCurrentText(language);
 | 
			
		||||
  } else if (rowCount() > 1) {
 | 
			
		||||
    const auto previousRow = rowCount() - 2;
 | 
			
		||||
    auto previousCombo =
 | 
			
		||||
 | 
			
		||||
@ -15,6 +15,7 @@ public:
 | 
			
		||||
  explicit SubstitutionsTable(QWidget* parent = nullptr);
 | 
			
		||||
 | 
			
		||||
  void setSubstitutions(const Substitutions& substitutions);
 | 
			
		||||
  void setTessdataPath(const QString& tessdataPath);
 | 
			
		||||
  Substitutions substitutions() const;
 | 
			
		||||
 | 
			
		||||
private:
 | 
			
		||||
@ -22,6 +23,8 @@ private:
 | 
			
		||||
  void addRow(const LanguageId& language = {},
 | 
			
		||||
              const Substitution& substutution = {});
 | 
			
		||||
  std::pair<LanguageId, Substitution> at(int row) const;
 | 
			
		||||
  void updateModel(const Substitutions& substitutions);
 | 
			
		||||
 | 
			
		||||
  QStringListModel* languagesModel_;
 | 
			
		||||
  QString tessdataPath_;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user