• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdesdk API Reference
  • KDE Home
  • Contact Us
 

lokalize

  • sources
  • kde-4.14
  • kdesdk
  • lokalize
  • src
  • common
languagelistmodel.cpp
Go to the documentation of this file.
1 /* ****************************************************************************
2  This file is part of Lokalize
3 
4  Copyright (C) 2009 by Nick Shaforostoff <shafff@ukr.net>
5 
6  This program is free software; you can redistribute it and/or
7  modify it under the terms of the GNU General Public License as
8  published by the Free Software Foundation; either version 2 of
9  the License or (at your option) version 3 or any later version
10  accepted by the membership of KDE e.V. (or its successor approved
11  by the membership of KDE e.V.), which shall act as a proxy
12  defined in Section 14 of version 3 of the license.
13 
14  This program is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU General Public License for more details.
18 
19  You should have received a copy of the GNU General Public License
20  along with this program. If not, see <http://www.gnu.org/licenses/>.
21 
22 **************************************************************************** */
23 
24 #include "languagelistmodel.h"
25 #include <kglobal.h>
26 #include <kdebug.h>
27 #include <kstandarddirs.h>
28 #include <kiconloader.h>
29 #include <kicon.h>
30 #include <klocale.h>
31 
32 #include <QStringBuilder>
33 #include <QCoreApplication>
34 #include <QSortFilterProxyModel>
35 
36 
37 
38 LanguageListModel* LanguageListModel::_instance=0;
39 LanguageListModel* LanguageListModel::_emptyLangInstance=0;
40 void LanguageListModel::cleanupLanguageListModel()
41 {
42  delete LanguageListModel::_instance; LanguageListModel::_instance = 0;
43  delete LanguageListModel::_emptyLangInstance; LanguageListModel::_emptyLangInstance = 0;
44 }
45 
46 LanguageListModel* LanguageListModel::instance()
47 {
48  if (_instance==0 )
49  {
50  _instance=new LanguageListModel();
51  qAddPostRoutine(LanguageListModel::cleanupLanguageListModel);
52  }
53  return _instance;
54 }
55 
56 LanguageListModel* LanguageListModel::emptyLangInstance()
57 {
58  if (_emptyLangInstance==0 )
59  _emptyLangInstance=new LanguageListModel(EmptyLang);
60  return _emptyLangInstance;
61 }
62 
63 
64 LanguageListModel::LanguageListModel(ModelType type, QObject* parent)
65  : QStringListModel(KGlobal::locale()->allLanguagesList(),parent)
66  , m_sortModel(new QSortFilterProxyModel(this))
67 {
68  if (type==EmptyLang) insertRows(rowCount(), 1);
69  KIconLoader::global()->addExtraDesktopThemes();
70  //kWarning()<<KIconLoader::global()->hasContext(KIconLoader::International);
71  //kDebug()<<KIconLoader::global()->queryIconsByContext(KIconLoader::NoGroup,KIconLoader::International);
72  m_sortModel->setSourceModel(this);
73  m_sortModel->sort(0);
74 }
75 
76 QVariant LanguageListModel::data(const QModelIndex& index, int role) const
77 {
78  if (role==Qt::DecorationRole)
79  {
80  static QMap<QString,QVariant> iconCache;
81 
82  QString langCode=stringList().at(index.row());
83  if (!iconCache.contains(langCode))
84  {
85  // NOTE workaround for QTBUG-9370 - it will be removed later
86  QString code;
87  if(langCode == "mn")
88  code = "mn_MN";
89  else if(langCode == "ne")
90  code = "ne_NP";
91  else
92  code=QLocale(langCode).name();
93  QString path;
94  if (code.contains('_')) code=QString::fromRawData(code.unicode()+3, 2).toLower();
95  if (code!="C")
96  {
97  static QString flagPath("l10n/%1/flag.png");
98  path=KStandardDirs::locate("locale", flagPath.arg(code));
99  }
100  iconCache[langCode]=QIcon(path);
101  }
102  return iconCache.value(langCode);
103  }
104  else if (role==Qt::DisplayRole)
105  {
106  const QString& code=stringList().at(index.row());
107  if (code.isEmpty()) return code;
108  //kDebug()<<"languageCodeToName"<<code;
109  return QVariant::fromValue<QString>(KGlobal::locale()->languageCodeToName(code)%" ("%code%")");
110  }
111  return QStringListModel::data(index, role);
112 }
113 
114 QFlags< Qt::ItemFlag > LanguageListModel::flags(const QModelIndex& index) const
115 {
116  return QStringListModel::flags(index);
117 }
118 
119 int LanguageListModel::sortModelRowForLangCode(const QString& langCode)
120 {
121  return m_sortModel->mapFromSource(index(stringList().indexOf(langCode))).row();
122 }
123 
124 QString LanguageListModel::langCodeForSortModelRow(int row)
125 {
126  return stringList().at(m_sortModel->mapToSource(m_sortModel->index(row,0)).row());
127 }
QSortFilterProxyModel::index
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const
LanguageListModel::instance
static LanguageListModel * instance()
Definition: languagelistmodel.cpp:46
QModelIndex
QMap::contains
bool contains(const Key &key) const
QSortFilterProxyModel::sort
virtual void sort(int column, Qt::SortOrder order)
QSortFilterProxyModel::setSourceModel
virtual void setSourceModel(QAbstractItemModel *sourceModel)
QStringListModel::flags
virtual Qt::ItemFlags flags(const QModelIndex &index) const
QList::at
const T & at(int i) const
QMap
languagelistmodel.h
LanguageListModel::langCodeForSortModelRow
QString langCodeForSortModelRow(int)
Definition: languagelistmodel.cpp:124
LanguageListModel::data
QVariant data(const QModelIndex &index, int role) const
Definition: languagelistmodel.cpp:76
LanguageListModel
Definition: languagelistmodel.h:32
QStringListModel::rowCount
virtual int rowCount(const QModelIndex &parent) const
QFlags
QStringListModel::stringList
QStringList stringList() const
QString::fromRawData
QString fromRawData(const QChar *unicode, int size)
QStringListModel::data
virtual QVariant data(const QModelIndex &index, int role) const
QObject
QAbstractListModel::index
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const
QString::isEmpty
bool isEmpty() const
QModelIndex::row
int row() const
QStringListModel::insertRows
virtual bool insertRows(int row, int count, const QModelIndex &parent)
QLocale::name
QString name() const
QString
QStringListModel
LanguageListModel::sortModelRowForLangCode
int sortModelRowForLangCode(const QString &)
Definition: languagelistmodel.cpp:119
QLocale
QString::contains
bool contains(QChar ch, Qt::CaseSensitivity cs) const
QSortFilterProxyModel
QString::unicode
const QChar * unicode() const
QSortFilterProxyModel::mapToSource
virtual QModelIndex mapToSource(const QModelIndex &proxyIndex) const
QSortFilterProxyModel::mapFromSource
virtual QModelIndex mapFromSource(const QModelIndex &sourceIndex) const
LanguageListModel::flags
QFlags< Qt::ItemFlag > flags(const QModelIndex &index) const
Definition: languagelistmodel.cpp:114
LanguageListModel::emptyLangInstance
static LanguageListModel * emptyLangInstance()
Definition: languagelistmodel.cpp:56
QString::arg
QString arg(qlonglong a, int fieldWidth, int base, const QChar &fillChar) const
QIcon
QMap::value
const T value(const Key &key) const
QVariant
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:40:07 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

lokalize

Skip menu "lokalize"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdesdk API Reference

Skip menu "kdesdk API Reference"
  • kapptemplate
  • kcachegrind
  • kompare
  • lokalize
  • umbrello
  •   umbrello

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal