KTextAddons

autocorrectionlanguagemodel.cpp
1/*
2 SPDX-FileCopyrightText: 2024-2025 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 if (parent.isValid()) // flat model
22 return 0;
23 return mLocalInfos.count();
24}
25
26QString AutoCorrectionLanguageModel::language(int index) const
27{
28 return mLocalInfos.at(index).localeCode;
29}
30
31static bool stripCountryCode(QString *languageCode)
32{
33 const int idx = languageCode->indexOf(QLatin1Char('_'));
34 if (idx != -1) {
35 *languageCode = languageCode->left(idx);
36 return true;
37 }
38 return false;
39}
40
41QVariant AutoCorrectionLanguageModel::data(const QModelIndex &index, int role) const
42{
43 if (index.row() < 0 || index.row() >= mLocalInfos.count()) {
44 return {};
45 }
46 const LocaleInfo statusInfo = mLocalInfos.at(index.row());
47 switch (role) {
48 case Qt::DisplayRole:
49 case LocaleI18n:
50 return statusInfo.localeTranslated;
51 case Locale:
52 return statusInfo.localeCode;
53 }
54 return {};
55}
56
57void AutoCorrectionLanguageModel::fillModel()
58{
59 QLocale cLocale(QLocale::C);
60 QSet<QString> insertedLanguages;
62 mLocalInfos.reserve(allLocales.count() + 1);
63 for (const QLocale &lang : allLocales) {
64 QString languageCode = lang.name();
65 if (lang != cLocale) {
66 // bug 484328 => don't translate locale name.
67#if 0
68 const QString nativeName = lang.nativeLanguageName();
69 // For some languages the native name might be empty.
70 // In this case use the non native language name as fallback.
71 // See: QTBUG-51323
72 QString languageName = nativeName.isEmpty() ? QLocale::languageToString(lang.language()) : nativeName;
73#endif
74 QString languageName = QLocale::languageToString(lang.language());
75 languageName = languageName.toLower();
76 if (!insertedLanguages.contains(languageName)) {
77 LocaleInfo info;
78 info.localeCode = languageCode;
79 info.localeTranslated = languageName;
80 mLocalInfos.append(std::move(info));
81 insertedLanguages << languageName;
82 } else if (stripCountryCode(&languageCode)) {
83 if (!insertedLanguages.contains(languageName)) {
84 LocaleInfo info;
85 info.localeCode = languageCode;
86 info.localeTranslated = languageName;
87 mLocalInfos.append(std::move(info));
88 insertedLanguages << languageName;
89 }
90 }
91 }
92 }
93}
94
95#include "moc_autocorrectionlanguagemodel.cpp"
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const const=0
virtual QModelIndex parent(const QModelIndex &index) const const=0
qsizetype count() const const
QString languageToString(Language language)
QList< QLocale > matchingLocales(QLocale::Language language, QLocale::Script script, QLocale::Territory territory)
bool contains(const QSet< T > &other) const const
QString & append(QChar ch)
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-2025 The KDE developers.
Generated on Fri Mar 28 2025 11:48:40 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.