Warning, file /plasma/plasma-workspace/kcms/region_language/localelistmodel.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).
0001 /* 0002 * localelistmodel.cpp 0003 * Copyright 2014 Sebastian Kügler <sebas@kde.org> 0004 * Copyright 2021 Han Young <hanyoung@protonmail.com> 0005 * 0006 * This program is free software; you can redistribute it and/or modify 0007 * it under the terms of the GNU General Public License as published by 0008 * the Free Software Foundation; either version 2 of the License, or 0009 * (at your option) any later version. 0010 * 0011 * This program is distributed in the hope that it will be useful, 0012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 0013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0014 * GNU General Public License for more details. 0015 * 0016 * You should have received a copy of the GNU General Public License 0017 * along with this program; if not, write to the Free Software 0018 */ 0019 0020 #include "localelistmodel.h" 0021 #include "exampleutility.h" 0022 #include "kcmregionandlang.h" 0023 0024 #include <QTextCodec> 0025 0026 using namespace KCM_RegionAndLang; 0027 0028 LocaleListModel::LocaleListModel(QObject *parent) 0029 : QAbstractListModel(parent) 0030 { 0031 const QList<QLocale> m_locales = QLocale::matchingLocales(QLocale::AnyLanguage, QLocale::AnyScript, QLocale::AnyCountry); 0032 m_localeData.reserve(m_locales.size() + 1); 0033 // we use first QString for title in Unset option 0034 m_localeData.push_back(LocaleData{i18n("Default for System"), QString(), QString(), QString(), i18n("Default"), QLocale()}); 0035 for (auto &locale : m_locales) { 0036 m_localeData.push_back(LocaleData{.nativeName = locale.nativeLanguageName(), 0037 .englishName = QLocale::languageToString(locale.language()), 0038 .nativeCountryName = locale.nativeCountryName(), 0039 .englishCountryName = QLocale::countryToString(locale.country()), 0040 .countryCode = locale.name(), 0041 .locale = locale}); 0042 } 0043 } 0044 0045 int LocaleListModel::rowCount(const QModelIndex &parent) const 0046 { 0047 Q_UNUSED(parent) 0048 return m_localeData.size(); 0049 } 0050 0051 QVariant LocaleListModel::data(const QModelIndex &index, int role) const 0052 { 0053 int dataIndex = index.row(); 0054 const auto &data = m_localeData.at(dataIndex); 0055 switch (role) { 0056 case FlagIcon: { 0057 QString countryCode; 0058 const QStringList split = data.countryCode.split(QLatin1Char('_')); 0059 if (split.size() > 1) { 0060 countryCode = split[1].toLower(); 0061 } 0062 auto flagIconPath = QStandardPaths::locate(QStandardPaths::GenericDataLocation, 0063 QStringLiteral("kf" QT_STRINGIFY(QT_VERSION_MAJOR) "/locale/countries/%1/flag.png").arg(countryCode)); 0064 return flagIconPath; 0065 } 0066 case DisplayName: { 0067 // 0 is unset option, 1 is locale C 0068 if (dataIndex == 1) { 0069 return data.countryCode; 0070 } 0071 const QString clabel = !data.nativeCountryName.isEmpty() ? data.nativeCountryName : data.englishCountryName; 0072 QString languageName; 0073 if (!data.nativeName.isEmpty()) { 0074 languageName = data.nativeName; 0075 } else { 0076 languageName = data.englishName; 0077 } 0078 if (dataIndex == 0) { 0079 return languageName; 0080 } 0081 return i18nc("the first is language name, the second is the country name, like 'English (America)'", "%1 (%2)", languageName, clabel); 0082 } 0083 case LocaleName: { 0084 QString cvalue = data.countryCode; 0085 if (!cvalue.contains(QLatin1Char('.')) && cvalue != QLatin1Char('C') && cvalue != i18n("Default")) { 0086 // explicitly add the encoding, 0087 // otherwise Qt doesn't accept dead keys and garbles the output as well 0088 cvalue.append(QLatin1Char('.') + QTextCodec::codecForLocale()->name()); 0089 } 0090 return cvalue; 0091 } 0092 case Example: { 0093 switch (m_configType) { 0094 case Lang: 0095 case Language: 0096 return {}; 0097 case Numeric: 0098 return Utility::numericExample(data.locale); 0099 case Time: 0100 return Utility::timeExample(data.locale); 0101 case Currency: 0102 return Utility::monetaryExample(data.locale); 0103 case Measurement: 0104 return Utility::measurementExample(data.locale); 0105 case PaperSize: 0106 return Utility::paperSizeExample(data.locale); 0107 #ifdef LC_ADDRESS 0108 case Address: 0109 return Utility::addressExample(data.locale); 0110 case NameStyle: 0111 return Utility::nameStyleExample(data.locale); 0112 case PhoneNumbers: 0113 return Utility::phoneNumbersExample(data.locale); 0114 #endif 0115 } 0116 return {}; 0117 } 0118 case FilterRole: { 0119 return data.englishCountryName.toLower() + data.nativeCountryName.toLower() + data.nativeName.toLower() + data.englishName.toLower() 0120 + data.countryCode.toLower(); 0121 } 0122 } 0123 Q_UNREACHABLE(); 0124 return {}; 0125 } 0126 0127 QHash<int, QByteArray> LocaleListModel::roleNames() const 0128 { 0129 return {{LocaleName, QByteArrayLiteral("localeName")}, 0130 {DisplayName, QByteArrayLiteral("display")}, 0131 {FlagIcon, QByteArrayLiteral("flag")}, 0132 {Example, QByteArrayLiteral("example")}, 0133 {FilterRole, QByteArrayLiteral("filter")}}; 0134 } 0135 0136 int LocaleListModel::selectedConfig() const 0137 { 0138 return m_configType; 0139 } 0140 0141 void LocaleListModel::setSelectedConfig(int config) 0142 { 0143 if (config != m_configType) { 0144 m_configType = static_cast<SettingType>(config); 0145 } 0146 Q_EMIT selectedConfigChanged(); 0147 Q_EMIT dataChanged(createIndex(0, 0), createIndex(rowCount(), 0), QVector<int>(1, Example)); 0148 } 0149 0150 void LocaleListModel::setLang(const QString &lang) 0151 { 0152 QString tmpLang = lang; 0153 bool isC = false; 0154 if (lang.isEmpty()) { 0155 tmpLang = qgetenv("LANG"); 0156 if (tmpLang.isEmpty()) { 0157 tmpLang = QStringLiteral("C"); 0158 isC = true; 0159 } 0160 } 0161 0162 LocaleData &data = m_localeData.front(); 0163 if (isC) { 0164 data.nativeName = i18nc("@info:title, meaning the current locale is system default(which is `C`)", "System Default C"); 0165 } else { 0166 data.nativeName = 0167 i18nc("@info:title the current locale is the default for %1, %1 is the country name", "Default for %1", QLocale(tmpLang).nativeLanguageName()); 0168 } 0169 data.locale = QLocale(tmpLang); 0170 0171 Q_EMIT dataChanged(createIndex(0, 0), createIndex(0, 0)); 0172 }