diff options
Diffstat (limited to 'src/qtdevicesettings/localesettingsplugin')
15 files changed, 967 insertions, 0 deletions
diff --git a/src/qtdevicesettings/localesettingsplugin/Language.qml b/src/qtdevicesettings/localesettingsplugin/Language.qml new file mode 100644 index 0000000..e630b00 --- /dev/null +++ b/src/qtdevicesettings/localesettingsplugin/Language.qml @@ -0,0 +1,111 @@ +/**************************************************************************** +** +** Copyright (C) 2015 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Device Utilities module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** 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 http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +import QtQuick 2.5 +import QtQuick.Layouts 1.1 +import QtQuick.Controls 1.4 +import QtQuick.Controls.Styles.Flat 1.0 as Flat +import com.theqtcompany.settings.common 1.0 +import com.theqtcompany.settings.locale 1.0 + +Item { + id: root + property string title: qsTr("Language and Region") + + Column { + id: content + anchors.fill: parent + anchors.margins: Math.round(40 * Flat.FlatStyle.scaleFactor) + + GroupBox { + id: groupBox + width: parent.width + title: qsTr("Regional Format") + Layout.fillWidth: true + property var currentRegion: Qt.locale(LocaleManager.locale) + + ColumnLayout { + spacing: Math.round(10 * Flat.FlatStyle.scaleFactor) + width: parent.width + Layout.fillWidth: true + + Component { + id: regionSelect + RegionSelect { } + } + + GroupBox { + width: parent.width + title: { + if (groupBox.currentRegion.name === "C" || groupBox.currentRegion.name === "POSIX") { + return qsTr("Default"); + } + else if (groupBox.currentRegion.name !== "") { + return qsTr("%L1/%L2").arg(groupBox.currentRegion.nativeLanguageName).arg(groupBox.currentRegion.nativeCountryName) + } + else { + return qsTr("Region not set"); + } + } + + flat: true + + ColumnLayout { + spacing: Math.round(10 * Flat.FlatStyle.scaleFactor) + Layout.fillWidth: true + width: parent.width + + TextLabel { + text: Date().toLocaleString(groupBox.currentRegion) + } + + TextLabel { + text: Number(2343.34).toLocaleString(groupBox.currentRegion) + } + + TextLabel { + text: Number(41334.34).toLocaleCurrencyString(groupBox.currentRegion) + } + } + } + + Button { + text: qsTr("Change region") + onClicked: stackView.push(regionSelect) + } + } + } + } +} diff --git a/src/qtdevicesettings/localesettingsplugin/RegionSelect.qml b/src/qtdevicesettings/localesettingsplugin/RegionSelect.qml new file mode 100644 index 0000000..9247630 --- /dev/null +++ b/src/qtdevicesettings/localesettingsplugin/RegionSelect.qml @@ -0,0 +1,96 @@ +/**************************************************************************** +** +** Copyright (C) 2015 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Device Utilities module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** 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 http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +import QtQuick 2.5 +import QtQuick.Layouts 1.1 +import QtQuick.Controls 1.4 +import QtQuick.Controls.Styles.Flat 1.0 as Flat +import com.theqtcompany.settings.common 1.0 +import com.theqtcompany.settings.locale 1.0 + +Item { + id: root + property string title: qsTr("Select language") + + Component.onCompleted: country.text = LocaleFilter.filter + + Column { + id: content + anchors.fill: parent + anchors.margins: Math.round(20 * Flat.FlatStyle.scaleFactor) + spacing: Math.round(10 * Flat.FlatStyle.scaleFactor) + Row { + spacing: Math.round(10 * Flat.FlatStyle.scaleFactor) + + TextLabel { + text: qsTr("Search region: ") + } + + TextField { + id: country + text: "" + onTextChanged: LocaleFilter.filter = country.text + } + } + + TableView { + width: parent.width + height: Math.round(parent.height - 40 * Flat.FlatStyle.scaleFactor) + Layout.fillWidth: true + model: LocaleFilter + headerVisible: true + + TableViewColumn { + role: "language" + title: qsTr("Language") + width: Math.round(parent.width * 0.66) + } + + TableViewColumn { + role: "country" + title: qsTr("Region") + width: Math.round(parent.width * 0.33) + } + + onClicked: { + var val = model.itemFromRow(row); + if (val !== "") { + LocaleManager.locale = val; + stackView.pop(); + } + } + } + } +} diff --git a/src/qtdevicesettings/localesettingsplugin/icons.qrc b/src/qtdevicesettings/localesettingsplugin/icons.qrc new file mode 100644 index 0000000..af8a564 --- /dev/null +++ b/src/qtdevicesettings/localesettingsplugin/icons.qrc @@ -0,0 +1,4 @@ +<RCC> + <qresource prefix="/"> + </qresource> +</RCC> diff --git a/src/qtdevicesettings/localesettingsplugin/locale.xml b/src/qtdevicesettings/localesettingsplugin/locale.xml new file mode 100644 index 0000000..b722456 --- /dev/null +++ b/src/qtdevicesettings/localesettingsplugin/locale.xml @@ -0,0 +1,12 @@ +<?xml version="1.0" encoding="utf-8"?> +<node name="/Service" xmlns:tp="http://telepathy.freedesktop.org/wiki/DbusSpec#extensions-v0"> +<node> + <interface name="org.freedesktop.locale1"> + <property name="Locale" type="as" access="read"/> + <method name="SetLocale"> + <arg name="locale" type="as" direction="in"/> + <arg name="user_interaction" type="b" direction="in"/> + </method> + </interface> +</node> + diff --git a/src/qtdevicesettings/localesettingsplugin/localefiltermodel.cpp b/src/qtdevicesettings/localesettingsplugin/localefiltermodel.cpp new file mode 100644 index 0000000..027caba --- /dev/null +++ b/src/qtdevicesettings/localesettingsplugin/localefiltermodel.cpp @@ -0,0 +1,96 @@ +/**************************************************************************** +** +** Copyright (C) 2015 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Device Utilities module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** 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 http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#include "localemodel.h" +#include "localefiltermodel.h" + +LocaleFilterModel::LocaleFilterModel(QObject* parent) + :QSortFilterProxyModel(parent) +{ + connect(this, &LocaleFilterModel::filterChanged, this, &LocaleFilterModel::invalidate); +} + +LocaleFilterModel::~LocaleFilterModel() +{ + +} + +QString LocaleFilterModel::filter() const +{ + return m_filter; +} + +void LocaleFilterModel::setFilter(const QString& aFilter) +{ + m_filter = aFilter; + emit filterChanged(); +} + +bool LocaleFilterModel::filterAcceptsRow( int source_row, const QModelIndex& source_parent ) const +{ + bool ret = false; + if (this->sourceModel()) + { + QModelIndex index = this->sourceModel()->index( source_row, 0, source_parent ); + if (index.isValid()) + { + QVariant nameRole = index.data(LocaleModel::Country); + if (nameRole.isValid()) + { + QString name = nameRole.toString(); + if (name.contains(m_filter, Qt::CaseInsensitive)) { + ret = true; + } + } + } + } + return ret; +} + +QVariant LocaleFilterModel::itemFromRow(const int row) const +{ + QModelIndex idx = index(row, 0); + QModelIndex mapped = mapToSource(idx); + if (mapped.isValid()) + { + QVariant nameRole = mapped.data(LocaleModel::Code); + if (nameRole.isValid()) + { + return nameRole; + } + } + return QVariant::fromValue(QString("")); +} + diff --git a/src/qtdevicesettings/localesettingsplugin/localefiltermodel.h b/src/qtdevicesettings/localesettingsplugin/localefiltermodel.h new file mode 100644 index 0000000..f4dce1b --- /dev/null +++ b/src/qtdevicesettings/localesettingsplugin/localefiltermodel.h @@ -0,0 +1,59 @@ +/**************************************************************************** +** +** Copyright (C) 2015 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Device Utilities module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** 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 http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#ifndef LOCALEFILTERMODEL_H +#define LOCALEFILTERMODEL_H + +#include <QSortFilterProxyModel> + +class LocaleFilterModel : public QSortFilterProxyModel +{ + Q_OBJECT + Q_PROPERTY(QString filter READ filter WRITE setFilter NOTIFY filterChanged) +public: + explicit LocaleFilterModel(QObject* parent); + virtual ~LocaleFilterModel(); + bool filterAcceptsRow( int source_row, const QModelIndex& source_parent ) const override; + QString filter() const; + void setFilter(const QString& aFilter); + Q_INVOKABLE QVariant itemFromRow(const int row) const; +signals: + void filterChanged(); +private: + QString m_filter; + +}; + +#endif // LOCALEFILTERMODEL_H diff --git a/src/qtdevicesettings/localesettingsplugin/localemodel.cpp b/src/qtdevicesettings/localesettingsplugin/localemodel.cpp new file mode 100644 index 0000000..be24e02 --- /dev/null +++ b/src/qtdevicesettings/localesettingsplugin/localemodel.cpp @@ -0,0 +1,167 @@ +/**************************************************************************** +** +** Copyright (C) 2015 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Device Utilities module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** 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 http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QLocale> +#include <QQmlPropertyMap> +#include <QFuture> +#include <QFutureWatcher> +#include <QThread> +#include <QtConcurrent/QtConcurrentRun> +#include "localemodel.h" + + +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()); + } +} + +QString LocaleItem::language() const +{ + return m_language; +} + +QString LocaleItem::country() const +{ + return m_country; +} + +QString LocaleItem::code() const +{ + return m_code; +} + +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())); +} + +void LocaleModel::modelReady() +{ + beginResetModel(); + sort(0); + endResetModel(); +} + +void LocaleModel::generateModel(LocaleModel* model) +{ + QList<QLocale> allLocales = QLocale::matchingLocales(QLocale::AnyLanguage, QLocale::AnyScript, QLocale::AnyCountry); + + for (const QLocale &locale : allLocales) { + if (locale.name() != "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(); +} + +LocaleModel::~LocaleModel() +{ + qDeleteAll(m_items); +} + +QHash<int, QByteArray> LocaleModel::roleNames() const +{ + return m_roleNames; +} + +int LocaleModel::rowCount(const QModelIndex & parent) const +{ + Q_UNUSED(parent); + return m_items.count(); +} + +QVariant LocaleModel::data(const QModelIndex & index, int role) const +{ + if (!index.isValid()) return QVariant(); + + LocaleItem *item = m_items[index.row()]; + + switch (role) { + case Country: + return item->country(); + break; + case Language: + return item->language(); + break; + case Code: + return item->code(); + break; + default: + return ""; + } +} + +bool LocaleModel::variantLessThan(const LocaleItem* v1, const LocaleItem* v2) +{ + return v1->language() < v2->language(); +} + +void LocaleModel::sort(int column, Qt::SortOrder order) +{ + Q_UNUSED(column); + Q_UNUSED(order); + qSort(m_items.begin(), m_items.end(), LocaleModel::variantLessThan); +} diff --git a/src/qtdevicesettings/localesettingsplugin/localemodel.h b/src/qtdevicesettings/localesettingsplugin/localemodel.h new file mode 100644 index 0000000..b83286e --- /dev/null +++ b/src/qtdevicesettings/localesettingsplugin/localemodel.h @@ -0,0 +1,96 @@ +/**************************************************************************** +** +** Copyright (C) 2015 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Device Utilities module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** 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 http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#ifndef LOCALEMODEL_H +#define LOCALEMODEL_H + +#include <QObject> +#include <QAbstractListModel> +#include <QLocale> +#include <QMutex> + +class LocaleItem : public QObject +{ + Q_OBJECT + Q_PROPERTY(QString code READ code CONSTANT) + Q_PROPERTY(QString country READ country CONSTANT) + Q_PROPERTY(QString language READ language CONSTANT) +public: + explicit LocaleItem(const QLocale &locale, QObject *parent=0); + QString country() const; + QString language() const; + QString code() const; + +private: + QString m_country; + QString m_language; + QString m_code; +}; + +class LocaleModel : public QAbstractListModel +{ + Q_OBJECT + +public: + explicit LocaleModel(QObject *parent=0); + virtual ~LocaleModel(); + // from QAbstractItemModel + int rowCount(const QModelIndex & parent = QModelIndex()) const; + QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const; + QHash<int, QByteArray> roleNames() const; + Q_INVOKABLE QVariant itemFromRow(const int row) const; + void sort(int column, Qt::SortOrder order=Qt::AscendingOrder); + static bool variantLessThan(const LocaleItem* v1, const LocaleItem* v2); + + enum Roles { + Language = Qt::DisplayPropertyRole, + Country = Qt::UserRole, + Code + }; + + static void generateModel(LocaleModel* model); + +signals: + void addItem(LocaleItem* item); + +private slots: + void addNewItem(QObject* item); + void modelReady(); + +private: + QList<LocaleItem*> m_items; + QHash<int, QByteArray> m_roleNames; +}; +#endif // LOCALEMODEL_H diff --git a/src/qtdevicesettings/localesettingsplugin/localesettingsplugin.pro b/src/qtdevicesettings/localesettingsplugin/localesettingsplugin.pro new file mode 100644 index 0000000..8ff9157 --- /dev/null +++ b/src/qtdevicesettings/localesettingsplugin/localesettingsplugin.pro @@ -0,0 +1,36 @@ +TEMPLATE = lib +CONFIG += plugin +QT += qml dbus + +uri = com.theqtcompany.settings.locale + +DBUS_INTERFACES = locale.xml + +DESTDIR = imports/Locale +TARGET = localesettingsplugin + +HEADERS = \ + systemlocale.h \ + systemlocale_p.h \ + localemodel.h \ + localefiltermodel.h +SOURCES += plugin.cpp \ + systemlocale.cpp \ + localemodel.cpp \ + localefiltermodel.cpp + +pluginfiles.files += \ + qmldir \ + Language.qml \ + RegionSelect.qml + +installPath = $$[QT_INSTALL_QML]/$$replace(uri, \\., /) + +target.path = $$installPath +pluginfiles.path += $$installPath +INSTALLS += target pluginfiles + + +RESOURCES += \ + icons.qrc + diff --git a/src/qtdevicesettings/localesettingsplugin/plugin.cpp b/src/qtdevicesettings/localesettingsplugin/plugin.cpp new file mode 100644 index 0000000..a050615 --- /dev/null +++ b/src/qtdevicesettings/localesettingsplugin/plugin.cpp @@ -0,0 +1,75 @@ +/**************************************************************************** +** +** Copyright (C) 2015 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Device Utilities module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** 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 http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtQml/QQmlExtensionPlugin> +#include <QtQml/qqml.h> +#include <qcoreapplication.h> +#include <QQmlEngine> +#include <QQmlContext> + +#include "systemlocale.h" +#include "localemodel.h" +#include "localefiltermodel.h" + +template <typename T> +QObject *instance(QQmlEngine *engine, QJSEngine *) { + T *t = new T(engine); + t->setObjectName(T::staticMetaObject.className()); + return t; +} + +class LanguageSettingsQmlPlugin : public QQmlExtensionPlugin +{ + Q_OBJECT + Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QQmlExtensionInterface") + +public: + void registerTypes(const char *uri) + { + qmlRegisterSingletonType<SystemLocale>(uri, 1, 0, "LocaleManager", &instance<SystemLocale>); + } + + void initializeEngine(QQmlEngine * engine, const char * uri) + { + Q_ASSERT(QLatin1String(uri) == QLatin1String("com.theqtcompany.settings.locale")); + LocaleModel*localeModel = new LocaleModel(engine); + LocaleFilterModel* filter = new LocaleFilterModel(engine); + filter->setSourceModel(localeModel); + engine->rootContext()->setContextProperty("LocaleFilter", filter); + } +}; + +#include "plugin.moc" diff --git a/src/qtdevicesettings/localesettingsplugin/qml.qrc b/src/qtdevicesettings/localesettingsplugin/qml.qrc new file mode 100644 index 0000000..27fef3d --- /dev/null +++ b/src/qtdevicesettings/localesettingsplugin/qml.qrc @@ -0,0 +1,6 @@ +<RCC> + <qresource prefix="/"> + <file>Language.qml</file> + <file>RegionSelect.qml</file> + </qresource> +</RCC> diff --git a/src/qtdevicesettings/localesettingsplugin/qmldir b/src/qtdevicesettings/localesettingsplugin/qmldir new file mode 100644 index 0000000..e915482 --- /dev/null +++ b/src/qtdevicesettings/localesettingsplugin/qmldir @@ -0,0 +1,3 @@ +module com.theqtcompany.settings.locale +Language 1.0 Language.qml +plugin localesettingsplugin diff --git a/src/qtdevicesettings/localesettingsplugin/systemlocale.cpp b/src/qtdevicesettings/localesettingsplugin/systemlocale.cpp new file mode 100644 index 0000000..21a1105 --- /dev/null +++ b/src/qtdevicesettings/localesettingsplugin/systemlocale.cpp @@ -0,0 +1,57 @@ +/**************************************************************************** +** +** Copyright (C) 2015 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Device Utilities module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** 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 http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#include <QLocale> +#include "systemlocale.h" +#include "systemlocale_p.h" + +SystemLocale::SystemLocale(QObject *parent) + :QObject(parent) + ,d_ptr(new SystemLocalePrivate(this)) +{ +} + +QString SystemLocale::locale() const +{ + Q_D(const SystemLocale); + return d->locale(); +} + +void SystemLocale::setLocale(const QString& aLocale) +{ + Q_D(SystemLocale); + d->setLocale(aLocale); + emit localeChanged(); +} diff --git a/src/qtdevicesettings/localesettingsplugin/systemlocale.h b/src/qtdevicesettings/localesettingsplugin/systemlocale.h new file mode 100644 index 0000000..c02fdd3 --- /dev/null +++ b/src/qtdevicesettings/localesettingsplugin/systemlocale.h @@ -0,0 +1,62 @@ +/**************************************************************************** +** +** Copyright (C) 2015 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Device Utilities module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** 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 http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#ifndef LOCALE_H +#define LOCALE_H + +#include <QObject> + +class SystemLocalePrivate; + +class SystemLocale : public QObject +{ + Q_OBJECT + Q_PROPERTY(QString locale READ locale WRITE setLocale NOTIFY localeChanged) +public: + explicit SystemLocale(QObject *parent = 0); + QString locale() const; + void setLocale(const QString& aLocale); +signals: + void localeChanged(); +public slots: + +protected: + SystemLocalePrivate *d_ptr; + + Q_DISABLE_COPY(SystemLocale) + Q_DECLARE_PRIVATE(SystemLocale) +}; + +#endif // LOCALE_H diff --git a/src/qtdevicesettings/localesettingsplugin/systemlocale_p.h b/src/qtdevicesettings/localesettingsplugin/systemlocale_p.h new file mode 100644 index 0000000..417b958 --- /dev/null +++ b/src/qtdevicesettings/localesettingsplugin/systemlocale_p.h @@ -0,0 +1,87 @@ +/**************************************************************************** +** +** Copyright (C) 2015 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Device Utilities module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** 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 http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#ifndef LOCALE_P_H +#define LOCALE_P_H + +#include "systemlocale.h" +#include "locale_interface.h" + +class SystemLocalePrivate +{ + Q_DECLARE_PUBLIC(SystemLocale) +public: + SystemLocalePrivate(SystemLocale *qq) + :q_ptr(qq) + { + m_localeInterface = new OrgFreedesktopLocale1Interface(QStringLiteral("org.freedesktop.locale1"), + QStringLiteral("/org/freedesktop/locale1"), + QDBusConnection::systemBus(), qq); + + QStringList locale = m_localeInterface->locale(); + foreach (QString l, locale) { + QStringList nameValue = l.split('='); + if (nameValue.length() == 2) + m_localeCache[nameValue.at(0)] = nameValue.at(1); + } + } + + QString locale() const { + return m_localeCache["LANG"]; + } + + void setLocale(const QString& aLocale) { + m_localeCache["LANG"] = aLocale; + updateLocale(); + } + + void updateLocale() { + QStringList newLocale; + QMap<QString, QString>::iterator i; + for (i = m_localeCache.begin(); i != m_localeCache.end(); ++i) { + QString val = i.key() + "=" + i.value(); + newLocale.append(val); + } + m_localeInterface->SetLocale(newLocale, true); + } + + +private: + QMap<QString, QString> m_localeCache; + OrgFreedesktopLocale1Interface *m_localeInterface; + SystemLocale *q_ptr; +}; + +#endif // LOCALE_P_H |