File indexing completed on 2023-12-03 08:26:47
0001 /*************************************************************************** 0002 * Copyright (C) 2009 Matthias Fuchs <mat69@gmx.net> * 0003 * * 0004 * This program is free software; you can redistribute it and/or modify * 0005 * it under the terms of the GNU General Public License as published by * 0006 * the Free Software Foundation; either version 2 of the License, or * 0007 * (at your option) any later version. * 0008 * * 0009 * This program is distributed in the hope that it will be useful, * 0010 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 0011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 0012 * GNU General Public License for more details. * 0013 * * 0014 * You should have received a copy of the GNU General Public License * 0015 * along with this program; if not, write to the * 0016 * Free Software Foundation, Inc., * 0017 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA . * 0018 ***************************************************************************/ 0019 0020 #include "localemodels.h" 0021 0022 #include <KCountry> 0023 #include <KLanguageName> 0024 0025 #include <QLocale> 0026 #include <QStandardPaths> 0027 0028 CountryModel::CountryModel(QObject *parent) 0029 : QAbstractListModel(parent) 0030 { 0031 } 0032 0033 QVariant CountryModel::data(const QModelIndex &index, int role) const 0034 { 0035 if (!index.isValid()) { 0036 return QVariant(); 0037 } 0038 0039 if (role == Qt::DisplayRole) { 0040 return m_countryNames.value(index.row()); 0041 } else if (role == Qt::UserRole) { 0042 return m_countryCodes.value(index.row()); 0043 } 0044 0045 return QVariant(); 0046 } 0047 0048 int CountryModel::rowCount(const QModelIndex &parent) const 0049 { 0050 if (parent.isValid()) { 0051 return 0; 0052 } 0053 0054 return m_countryCodes.count(); 0055 } 0056 0057 void CountryModel::setupModelData() 0058 { 0059 beginResetModel(); 0060 for (int c = 1; c <= QLocale::LastCountry; ++c) { 0061 QString countryCode; 0062 const auto country = static_cast<QLocale::Country>(c); 0063 QLocale locale(QLocale::AnyLanguage, country); 0064 if (locale.country() == country) { 0065 const QString localeName = locale.name(); 0066 const auto idx = localeName.indexOf(QLatin1Char('_')); 0067 if (idx != -1) { 0068 countryCode = localeName.mid(idx + 1); 0069 } 0070 } 0071 const QString countryName = KCountry::fromAlpha2(countryCode).name(); 0072 0073 if (!countryName.isEmpty()) { 0074 m_countryCodes.append(countryCode); 0075 m_countryNames.append(countryName); 0076 } 0077 } 0078 endResetModel(); 0079 } 0080 0081 LanguageModel::LanguageModel(QObject *parent) 0082 : QAbstractListModel(parent) 0083 { 0084 } 0085 0086 QVariant LanguageModel::data(const QModelIndex &index, int role) const 0087 { 0088 if (!index.isValid()) { 0089 return QVariant(); 0090 } else if (role == Qt::DisplayRole) { 0091 return m_languageNames.value(index.row()); 0092 } 0093 0094 return QVariant(); 0095 } 0096 0097 int LanguageModel::rowCount(const QModelIndex &parent) const 0098 { 0099 if (parent.isValid()) { 0100 return 0; 0101 } 0102 0103 return m_languageNames.count(); 0104 } 0105 0106 void LanguageModel::setupModelData() 0107 { 0108 beginResetModel(); 0109 for (int l = 1; l <= QLocale::LastLanguage; ++l) { 0110 const auto lang = static_cast<QLocale::Language>(l); 0111 QLocale locale(lang); 0112 if (locale.language() == lang) { 0113 const QString localeName = locale.name(); 0114 const QString languageName = KLanguageName::nameForCode(localeName); 0115 if (!languageName.isEmpty()) { 0116 m_languageNames.append(languageName); 0117 } 0118 } 0119 } 0120 endResetModel(); 0121 } 0122 0123 #include "moc_localemodels.cpp"