QVariant EnabledLocalesModel::data( const QModelIndex& index, int role ) const { if ( index.row() < 0 || index.row() >= m_locales.count() ) return QVariant(); const QString& localeCode = m_locales[index.row()]; Locale locale( localeCode.toLatin1() ); // Get language and country in current system locale UnicodeString uDisplayLanguage; UnicodeString uDisplayCountry; locale.getDisplayLanguage( locale, uDisplayLanguage ); locale.getDisplayCountry( locale, uDisplayCountry ); // Capitalize language and country UErrorCode status; BreakIterator* titleIterator = BreakIterator::createTitleInstance( locale, status ); uDisplayLanguage = uDisplayLanguage.toTitle( titleIterator ); uDisplayCountry = uDisplayCountry.toTitle( titleIterator ); QString displayLanguage = unicodeStringToQString( uDisplayLanguage ); QString displayCountry = unicodeStringToQString( uDisplayCountry ); switch ( role ) { case Qt::DisplayRole: return QString( "%1 - %2 (%3)" ).arg( displayLanguage ).arg( displayCountry ).arg( localeCode ); case LocaleCodeRole: return localeCode; case CountryRole: return displayCountry; case LanguageRole: return displayLanguage; case AddressRole: return address(); case CollateRole: return collate(); case CtypeRole: return ctype(); case IdentificationRole: return identification(); case LangRole: return lang(); case LanguageLcRole: return language(); case MeasurementRole: return measurement(); case MonetaryRole: return monetary(); case MessagesRole: return messages(); case NameRole: return name(); case NumericRole: return numeric(); case PaperRole: return paper(); case TelephoneRole: return telephone(); case TimeRole: return time(); } return QVariant(); }
void SupportedLocalesModel::init( SupportedLocalesItem* parent ) { QStringList localeList { LanguageCommon::supportedLocales() }; for ( const QString localeString : localeList ) { Locale locale( localeString.toLatin1() ); // Get language and country in current system locale UnicodeString uDisplayLanguage; UnicodeString uDisplayCountry; locale.getDisplayLanguage( locale, uDisplayLanguage ); locale.getDisplayCountry( locale, uDisplayCountry ); // Capitalize language and country UErrorCode status; BreakIterator* titleIterator = BreakIterator::createTitleInstance( locale, status ); uDisplayLanguage = uDisplayLanguage.toTitle( titleIterator ); uDisplayCountry = uDisplayCountry.toTitle( titleIterator ); QString language = locale.getLanguage(); QString country = locale.getCountry(); QString displayLanguage = unicodeStringToQString( uDisplayLanguage ); QString displayCountry = unicodeStringToQString( uDisplayCountry ); // Search if we already added this language to the tree QModelIndexList languageIndexList = match( index( 0,0 ), KeyRole, language, -1, Qt::MatchFixedString ); SupportedLocalesItem* languageItem; QModelIndex languageIndex; if ( languageIndexList.count() == 0 ) { // Not found, add the language to the root languageItem = new SupportedLocalesItem( language, displayLanguage, parent ); parent->appendChild( languageItem ); } else { Q_ASSERT( languageIndexList.count() == 1 ); // Found, convert index to a item languageIndex = languageIndexList.first(); languageItem = static_cast<SupportedLocalesItem*>( languageIndex.internalPointer() ); } // Search if we already added this country to this language QModelIndexList countryIndexList = match( languageIndex.child( 0,0 ), KeyRole, country, -1, Qt::MatchFixedString ); SupportedLocalesItem* countryItem; QModelIndex countryIndex; if ( countryIndexList.count() == 0 ) { // Not found, add the country to the language countryItem = new SupportedLocalesItem( country, displayCountry, languageItem ); languageItem->appendChild( countryItem ); } else { Q_ASSERT( countryIndexList.count() == 1 ); // Found, convert index to a item countryIndex = countryIndexList.first(); countryItem = static_cast<SupportedLocalesItem*>( countryIndex.internalPointer() ); } // Add the locale code to the language SupportedLocalesItem* localeItem = new SupportedLocalesItem( localeString, localeString, countryItem ); countryItem->appendChild( localeItem ); } }