KTextAddons

autocorrectionlanguagemodel.cpp
1/*
2 SPDX-FileCopyrightText: 2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7#include "autocorrectionlanguagemodel.h"
8#include <QLocale>
9
10using namespace TextAutoCorrectionWidgets;
11AutoCorrectionLanguageModel::AutoCorrectionLanguageModel(QObject *parent)
12 : QAbstractListModel{parent}
13{
14 fillModel();
15}
16
17AutoCorrectionLanguageModel::~AutoCorrectionLanguageModel() = default;
18
19int AutoCorrectionLanguageModel::rowCount(const QModelIndex &parent) const
20{
21 Q_UNUSED(parent)
22 return mLocalInfos.count();
23}
24
25QString AutoCorrectionLanguageModel::language(int index) const
26{
27 return mLocalInfos.at(index).localeCode;
28}
29
30static bool stripCountryCode(QString *languageCode)
31{
32 const int idx = languageCode->indexOf(QLatin1Char('_'));
33 if (idx != -1) {
34 *languageCode = languageCode->left(idx);
35 return true;
36 }
37 return false;
38}
39
40QVariant AutoCorrectionLanguageModel::data(const QModelIndex &index, int role) const
41{
42 if (index.row() < 0 || index.row() >= mLocalInfos.count()) {
43 return {};
44 }
45 const LocaleInfo statusInfo = mLocalInfos.at(index.row());
46 switch (role) {
47 case Qt::DisplayRole:
48 case LocaleI18n:
49 return statusInfo.localeTranslated;
50 case Locale:
51 return statusInfo.localeCode;
52 }
53 return {};
54}
55
56void AutoCorrectionLanguageModel::fillModel()
57{
58 QLocale cLocale(QLocale::C);
59 QSet<QString> insertedLanguages;
61 mLocalInfos.reserve(allLocales.count() + 1);
62 for (const QLocale &lang : allLocales) {
63 QString languageCode = lang.name();
64 if (lang != cLocale) {
65 // bug 484328 => don't translate locale name.
66#if 0
67 const QString nativeName = lang.nativeLanguageName();
68 // For some languages the native name might be empty.
69 // In this case use the non native language name as fallback.
70 // See: QTBUG-51323
71 QString languageName = nativeName.isEmpty() ? QLocale::languageToString(lang.language()) : nativeName;
72#endif
73 QString languageName = QLocale::languageToString(lang.language());
74 languageName = languageName.toLower();
75 if (!insertedLanguages.contains(languageName)) {
76 LocaleInfo info;
77 info.localeCode = languageCode;
78 info.localeTranslated = languageName;
79 mLocalInfos.append(std::move(info));
80 insertedLanguages << languageName;
81 } else if (stripCountryCode(&languageCode)) {
82 if (!insertedLanguages.contains(languageName)) {
83 LocaleInfo info;
84 info.localeCode = languageCode;
85 info.localeTranslated = languageName;
86 mLocalInfos.append(std::move(info));
87 insertedLanguages << languageName;
88 }
89 }
90 }
91 }
92}
93
94#include "moc_autocorrectionlanguagemodel.cpp"
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
qsizetype count() const const
void reserve(qsizetype size)
QString languageToString(Language language)
QList< QLocale > matchingLocales(QLocale::Language language, QLocale::Script script, QLocale::Territory territory)
int row() const const
QObject * parent() const const
bool contains(const QSet< T > &other) const const
qsizetype indexOf(QChar ch, qsizetype from, Qt::CaseSensitivity cs) const const
bool isEmpty() const const
QString left(qsizetype n) const const
QString toLower() const const
DisplayRole
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 4 2024 16:29:59 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.