MauiKit Image Tools

OCRLanguageModel.cpp
1/**
2 * SPDX-FileCopyrightText: 2022 by Alexander Stippich <a.stippich@gmx.net>
3 *
4 * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
5 */
6
7#include "OCRLanguageModel.h"
8
9#include <QLocale>
10
11OCRLanguageModel::OCRLanguageModel(QObject *parent) : QAbstractListModel(parent)
12{
13}
14
15OCRLanguageModel::~OCRLanguageModel()
16{
17}
18
19QHash<int, QByteArray> OCRLanguageModel::roleNames() const
20{
22 roles[NameRole] = "name";
23 roles[CodeRole] = "code";
24 roles[UseRole] = "use";
25 return roles;
26}
27
28int OCRLanguageModel::rowCount(const QModelIndex &) const
29{
30 return m_languages.count();
31}
32
33QVariant OCRLanguageModel::data(const QModelIndex &index, int role) const
34{
35 if (!index.isValid()) {
36 return QVariant();
37 }
38
39 if (index.row() >= m_languages.size() || index.row() < 0) {
40 return QVariant();
41 }
42
43 switch (role) {
44 case NameRole:
45 return m_languages.at(index.row()).name;
46 break;
47 case CodeRole:
48 return m_languages.at(index.row()).code;
49 break;
50 case UseRole:
51 return m_languages.at(index.row()).use;
52 break;
53 }
54 return QVariant();
55}
56
57bool OCRLanguageModel::setData(const QModelIndex &index, const QVariant &value, int role)
58{
59 if (index.row() >= 0 && index.row() < m_languages.size() && role == UseRole) {
60 m_languages[index.row()].use = value.toBool();
61 return true;
62 }
63 return false;
64}
65
66void OCRLanguageModel::setLanguages(const std::vector<std::string> &availableLanguages)
67{
69 // improve with Qt 6.3 (alpha 3 code support added)
70 for (const auto &language : availableLanguages) {
71 QString languageCode = QString::fromLocal8Bit(language.c_str());
72 if (languageCode != QStringLiteral("osd")) {
73 QLocale locale(languageCode.left(2));
74 m_languages.append({locale.nativeLanguageName(), languageCode, false});
75 }
76 }
78}
79
80std::string OCRLanguageModel::getLanguagesString() const
81{
82 std::string languageCodes;
83 for (int i = 0; i < m_languages.size(); i++) {
84 if (m_languages.at(i).use) {
85 if (languageCodes.size() > 1) {
86 languageCodes.append("+");
87 }
88 languageCodes.append(m_languages.at(i).code.toStdString());
89 }
90 }
91 if (languageCodes.empty() && m_languages.size() != 0) {
92 languageCodes.append(m_languages.constFirst().code.toStdString());
93 }
94 return languageCodes;
95}
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const const override
void append(QList< T > &&value)
const_reference at(qsizetype i) const const
const T & constFirst() const const
qsizetype count() const const
qsizetype size() const const
bool isValid() const const
int row() const const
QString fromLocal8Bit(QByteArrayView str)
QString left(qsizetype n) const const
bool toBool() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 16:59:01 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.