summaryrefslogtreecommitdiffstats
path: root/src/localesettings/localemodel.cpp
blob: cd411f3b4edada590148bead3b2aefe50a55e83a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Device Utilities module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:GPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 or (at your option) any later version
** approved by the KDE Free Qt Foundation. The licenses are as published by
** the Free Software Foundation and appearing in the file LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include <QLocale>
#include <QFuture>
#include <QFutureWatcher>
#include <QThread>
#include <QtConcurrent/QtConcurrentRun>
#include "localemodel.h"

QT_BEGIN_NAMESPACE

/*!
    \class LocaleItem
    \inmodule QtDeviceUtilities

    \brief The LocaleItem class represents a locale.

    This class holds the name, language, and country code of a locale.

    If available, the native country name and language are used. For example,
    \e Deutsch and \e Deutschland for the German locale.

    \sa QLocale, LocaleModel
*/

/*!
    \property LocaleItem::code
    \brief The locale code string.

    The locale code is in the format \e language_country, where \e language is
    a lowercase, two-letter ISO 639 language code, and \e country is an
    uppercase, two- or three-letter ISO 3166 country code.

    \sa QLocale::name()
*/

/*!
    \property LocaleItem::country
    \brief The name of the country.

    \sa QLocale::Country
*/

/*!
    \property LocaleItem::language
    \brief The name of the language.

    \sa QLocale::Language
*/


/*!
    Creates the locale item \a locale with the parent \a parent.
*/
LocaleItem::LocaleItem(const QLocale& locale, QObject *parent)
    :QObject(parent)
{
    m_code = locale.name();
    m_country = locale.nativeCountryName();
    if (m_country.isEmpty()) {
        m_country = locale.countryToString(locale.country());
    }

    m_language = locale.nativeLanguageName();
    if (m_language.isEmpty()) {
        m_language = locale.languageToString(locale.language());
    }
}

/*!
    Returns the language of the country.
*/
QString LocaleItem::language() const
{
    return m_language;
}

/*!
    Returns the name of the country.
*/
QString LocaleItem::country() const
{
    return m_country;
}

/*!
    Returns the country code of the country.
*/
QString LocaleItem::code() const
{
    return m_code;
}

/*!
    \class LocaleModel
    \inmodule QtDeviceUtilities

    \brief The LocaleModel class provides a model for the available locales.

    Each item in the model has a set of data elements associated with it, each
    with its own role. The roles are used by the view to indicate to the model
    which type of data it needs. Custom models should return data in these
    types.

    The data in a locale model can be filtered according to the country code,
    name, or language.

    \sa LocaleItem, LocaleFilterModel
*/

/*!
    \enum LocaleModel::Roles

    This enum holds the role of the locale item.

    For user roles, it is up to the developer to decide which types to use and
    ensure that components use the correct types when accessing and setting
    data.

    \value  Language
            The language of the country.
    \value  Country
            The name of the country.
    \value  Code
            The locale code string in the format \e language_country.

    \sa Qt::UserRole
*/

/*!
    \fn LocaleModel::addItem(LocaleItem* item)

    This signal is emitted when the locale item \a item is added to the locale
    model.
*/

/*!
    Creates a locale model with the parent \a parent.
*/
LocaleModel::LocaleModel(QObject *parent)
    : QAbstractListModel(parent)
{
    m_roleNames.insert(Qt::UserRole, "modelData");
    m_roleNames.insert(Country, "country");
    m_roleNames.insert(Language, "language");
    m_roleNames.insert(Code, "code");

    QFutureWatcher<void> *watcher = new QFutureWatcher<void>(this);
    QFuture<void> future = QtConcurrent::run(LocaleModel::generateModel, this);
    watcher->setFuture(future);
    connect(watcher, SIGNAL(finished()), this, SLOT(modelReady()));
}

/*!
    This signal is emitted when the locale model has been reset.
*/
void LocaleModel::modelReady()
{
    beginResetModel();
    sort(0);
    endResetModel();

    emit ready();
}

/*!
    Creates the locale model \a model.
*/
void LocaleModel::generateModel(LocaleModel* model)
{
    QList<QLocale> allLocales = QLocale::matchingLocales(QLocale::AnyLanguage, QLocale::AnyScript, QLocale::AnyCountry);

    for (const QLocale &locale : allLocales) {
        if (locale.name() != QStringLiteral("C")) {
            LocaleItem *l = new LocaleItem(locale);
            l->moveToThread(model->thread());
            QMetaObject::invokeMethod(model, "addNewItem", Q_ARG( QObject*, qobject_cast<QObject*>(l)));
        }
    }
}

void LocaleModel::addNewItem(QObject *item)
{
   beginInsertRows(QModelIndex(), m_items.count(), m_items.count());
   LocaleItem* newItem = qobject_cast<LocaleItem*>(item);
   if (newItem)
       m_items.append(newItem);
   endInsertRows();
}

/*!
    Deletes the locale model.
*/
LocaleModel::~LocaleModel()
{
    qDeleteAll(m_items);
}

/*!
    Returns an array of user roles.

    \sa Roles
*/
QHash<int, QByteArray> LocaleModel::roleNames() const
{
    return m_roleNames;
}

/*!
    Returns the number of rows in the locale model.
*/
int LocaleModel::rowCount(const QModelIndex & parent) const
{
    Q_UNUSED(parent);
    return m_items.count();
}

/*!
    Returns the locale item at \a index in the locale model for \a role.

    This item can be assigned to LocaleManager::locale(), when the user selects
    a locale from a list.

    \sa LocaleItem, Roles
*/
QVariant LocaleModel::data(const QModelIndex & index, int role) const
{
    if (!index.isValid()) return QVariant();

    LocaleItem *item = m_items[index.row()];

    switch (role) {
    case Qt::UserRole:
        return QVariant::fromValue(static_cast<QObject*>(item));
        break;
    case Country:
        return item->country();
        break;
    case Language:
        return item->language();
        break;
    case Code:
        return item->code();
        break;
    default:
        return QVariant();
    }
}

/*!
    Returns whether the locale item has more than one languages specified.

    If the language variant of the locale item if \a v1 is less than \a v2 ##?

*/
bool LocaleModel::variantLessThan(const LocaleItem* v1, const LocaleItem* v2)
{
    return v1->language() < v2->language();
}

/*!
    Sets the sorting order of the items in the locale model to \a order.

    The sort order can be either \l {Qt::AscendingOrder}{ascending} or
    \l {Qt::DescendingOrder}{descending}.
*/
void LocaleModel::sort(int column, Qt::SortOrder order)
{
    Q_UNUSED(column);
    Q_UNUSED(order);
    std::sort(m_items.begin(), m_items.end(), LocaleModel::variantLessThan);
}

/*!
    Returns the index for the country \a country in the locale model.

    The index is used by item views, delegates, and selection models to locate
    an item in the model.
*/
QModelIndex LocaleModel::indexForCountry(const QString &country) const
{
    for (int i = 0; i < m_items.count(); i++) {
        LocaleItem *item = m_items.at(i);
        if (item->country() == country ||
                item->language() == country) {
            return index(i);
        }
    }
    return QModelIndex();
}

QT_END_NAMESPACE