Imported Upstream version 1.1.0
[gammaray-debian.git] / tools / localeinspector / localemodel.cpp
blob7e39de58b61ef550b84299ff90018c1a76a895dc
1 /*
2 This file is part of GammaRay, the Qt application inspection and
3 manipulation tool.
5 Copyright (C) 2011 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com
6 Author: Stephen Kelly <stephen.kelly@kdab.com>
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "localemodel.h"
24 #include "localedataaccessor.h"
26 #include <QtCore/QLocale>
27 #include <QtCore/QDebug>
29 using namespace GammaRay;
31 LocaleModel::LocaleModel(QObject *parent)
32 : QAbstractTableModel(parent)
34 init();
35 connect(LocaleDataAccessorRegistry::instance(), SIGNAL(accessorsChanged()), SLOT(reinit()));
38 int LocaleModel::columnCount(const QModelIndex &parent) const
40 if (parent.isValid()) {
41 return 0;
43 return m_localeData.size();
46 QVariant LocaleModel::data(const QModelIndex &index, int role) const
48 if (!index.isValid() ||
49 index.row() >= m_locales.size() ||
50 index.column() >= m_localeData.size()) {
51 return QVariant();
54 const QLocale l = m_locales.at(index.row());
55 return m_localeData.at(index.column())->data(l, role);
58 QVariant LocaleModel::headerData(int section, Qt::Orientation orientation, int role) const
60 if (role != Qt::DisplayRole) {
61 return QAbstractItemModel::headerData(section, orientation, role);
63 if (orientation == Qt::Vertical) {
64 return QAbstractItemModel::headerData(section, orientation, role);
66 LocaleDataAccessor *d = m_localeData.at(section);
67 return d->accessorName();
70 void LocaleModel::init()
72 m_localeData = LocaleDataAccessorRegistry::enabledAccessors();
74 #if QT_VERSION >= QT_VERSION_CHECK(4, 8, 0)
75 m_locales =
76 QLocale::matchingLocales(QLocale::AnyLanguage,
77 QLocale::AnyScript, QLocale::AnyCountry).toVector();
78 #else
79 m_locales.clear();
80 QLocale::Language l = QLocale::C;
81 while (QLocale::languageToString(l) != QLatin1String("Unknown"))
83 QList<QLocale::Country> countries = QLocale::countriesForLanguage(l);
84 Q_FOREACH (const QLocale::Country &c, countries) {
85 m_locales.append(QLocale(l, c));
87 l = (QLocale::Language)((int)(l) + 1);
89 #endif
92 void LocaleModel::reinit()
94 beginResetModel();
95 init();
96 endResetModel();
99 int LocaleModel::rowCount(const QModelIndex &parent) const
101 if (parent.isValid()) {
102 return 0;
104 return m_locales.size();
107 #include "localemodel.moc"