File indexing completed on 2024-05-05 05:40:28
0001 /*************************************************************************** 0002 * Copyright (C) 2021 by Renaud Guezennec * 0003 * http://www.rolisteam.org/contact * 0004 * * 0005 * This software is free software; you can redistribute it and/or modify * 0006 * it under the terms of the GNU General Public License as published by * 0007 * the Free Software Foundation; either version 2 of the License, or * 0008 * (at your option) any later version. * 0009 * * 0010 * This program is distributed in the hope that it will be useful, * 0011 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 0012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 0013 * GNU General Public License for more details. * 0014 * * 0015 * You should have received a copy of the GNU General Public License * 0016 * along with this program; if not, write to the * 0017 * Free Software Foundation, Inc., * 0018 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * 0019 ***************************************************************************/ 0020 #include "model/languagemodel.h" 0021 0022 #include "worker/iohelper.h" 0023 0024 #include <QJsonArray> 0025 #include <QJsonObject> 0026 0027 void fetchData(QList<LanguageInfo>& infos) 0028 { 0029 auto data= IOHelper::fetchLanguageModel(); 0030 0031 for(auto langRef : data) 0032 { 0033 auto lang= langRef.toObject(); 0034 LanguageInfo info; 0035 info.code= lang["code"].toString(); 0036 info.commonLanguageName= lang["commonName"].toString(); 0037 info.languageName= lang["langname"].toString(); 0038 QStringList list; 0039 auto paths= lang["path"].toArray(); 0040 for(auto const& path : qAsConst(paths)) 0041 { 0042 list << path.toString(); 0043 } 0044 info.path= list; 0045 0046 infos.append(info); 0047 } 0048 } 0049 0050 LanguageModel::LanguageModel(QObject* parent) : QAbstractListModel(parent) 0051 { 0052 0053 fetchData(m_languageInfoList); 0054 } 0055 0056 int LanguageModel::rowCount(const QModelIndex& parent) const 0057 { 0058 if(parent.isValid()) 0059 return 0; 0060 0061 return m_languageInfoList.size(); 0062 } 0063 0064 QVariant LanguageModel::data(const QModelIndex& index, int role) const 0065 { 0066 if(!index.isValid()) 0067 return QVariant(); 0068 0069 if(role != Qt::DisplayRole) 0070 return {}; 0071 0072 auto lang= m_languageInfoList[index.row()]; 0073 0074 return QString("%1 - %2").arg(lang.languageName, lang.commonLanguageName); 0075 } 0076 0077 QStringList LanguageModel::pathFromIndex(const QModelIndex& index) 0078 { 0079 if(!index.isValid()) 0080 return {}; 0081 0082 auto lang= m_languageInfoList[index.row()]; 0083 0084 return lang.path; 0085 } 0086 0087 int LanguageModel::indexSystemLocale(const QString& localeCode) 0088 { 0089 0090 int index= -1; 0091 auto it= std::find_if(std::begin(m_languageInfoList), std::end(m_languageInfoList), 0092 [localeCode](const LanguageInfo& info) { 0093 return info.code == localeCode; }); 0094 0095 if(it == std::end(m_languageInfoList)) 0096 { 0097 auto it2= std::find_if(std::begin(m_languageInfoList), std::end(m_languageInfoList), 0098 [localeCode](const LanguageInfo& info) 0099 { 0100 auto lang= localeCode.left(2); 0101 return info.code.startsWith(lang); 0102 }); 0103 0104 if(it2 != std::end(m_languageInfoList)) 0105 index= std::distance(std::begin(m_languageInfoList), it2); 0106 } 0107 else 0108 index= std::distance(std::begin(m_languageInfoList), it); 0109 0110 return index; 0111 }