Warning, file /utilities/skanpage/src/OCRLanguageModel.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /**
0002  * SPDX-FileCopyrightText: 2022 by Alexander Stippich <a.stippich@gmx.net>
0003  *
0004  * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0005  */
0006 
0007 #include "OCRLanguageModel.h"
0008 
0009 #include <QLocale>
0010 
0011 OCRLanguageModel::OCRLanguageModel(QObject *parent) : QAbstractListModel(parent)
0012 {
0013 }
0014 
0015 OCRLanguageModel::~OCRLanguageModel()
0016 {
0017 }
0018 
0019 QHash<int, QByteArray> OCRLanguageModel::roleNames() const
0020 {
0021     QHash<int, QByteArray> roles;
0022     roles[NameRole] = "name";
0023     roles[CodeRole] = "code";
0024     roles[UseRole] = "use";
0025     return roles;
0026 }
0027 
0028 int OCRLanguageModel::rowCount(const QModelIndex &) const
0029 {
0030     return m_languages.count();
0031 }
0032     
0033 QVariant OCRLanguageModel::data(const QModelIndex &index, int role) const
0034 {
0035     if (!index.isValid()) {
0036         return QVariant();
0037     }
0038 
0039     if (index.row() >= m_languages.size() || index.row() < 0) {
0040         return QVariant();
0041     }
0042     
0043     switch (role) {
0044     case NameRole:
0045         return m_languages.at(index.row()).name;
0046         break;
0047     case CodeRole:
0048         return m_languages.at(index.row()).code;
0049         break;
0050     case UseRole:
0051         return m_languages.at(index.row()).use;
0052         break;
0053     }
0054     return QVariant();
0055 }
0056 
0057 bool OCRLanguageModel::setData(const QModelIndex &index, const QVariant &value, int role)
0058 {
0059     if (index.row() >= 0 && index.row() < m_languages.size() && role == UseRole) {
0060         m_languages[index.row()].use = value.toBool();
0061         return true;
0062     }
0063     return false;
0064 }
0065 
0066 void OCRLanguageModel::setLanguages(const std::vector<std::string> &availableLanguages)
0067 {
0068     beginResetModel();
0069     // improve with Qt 6.3 (alpha 3 code support added)
0070     for (const auto &language : availableLanguages) {
0071         QString languageCode = QString::fromLocal8Bit(language.c_str());
0072         if (languageCode != QStringLiteral("osd")) {
0073             QLocale locale(languageCode.left(2));
0074             m_languages.append({locale.nativeLanguageName(), languageCode, false});
0075         }
0076     }
0077     endResetModel();
0078 }
0079 
0080 std::string OCRLanguageModel::getLanguagesString() const
0081 {
0082     std::string languageCodes;
0083     for (int i = 0; i < m_languages.size(); i++) {
0084         if (m_languages.at(i).use) {
0085             if (languageCodes.size() > 1) {
0086                 languageCodes.append("+");
0087             }
0088             languageCodes.append(m_languages.at(i).code.toStdString());
0089         }
0090     }
0091     if (languageCodes.empty() && m_languages.size() != 0) {
0092         languageCodes.append(m_languages.constFirst().code.toStdString());
0093     }
0094     return languageCodes;
0095 }