| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the Qt Quick Dialogs module of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** GNU Lesser General Public License Usage |
| 18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
| 19 | ** General Public License version 3 as published by the Free Software |
| 20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
| 21 | ** packaging of this file. Please review the following information to |
| 22 | ** ensure the GNU Lesser General Public License version 3 requirements |
| 23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
| 24 | ** |
| 25 | ** GNU General Public License Usage |
| 26 | ** Alternatively, this file may be used under the terms of the GNU |
| 27 | ** General Public License version 2.0 or (at your option) the GNU General |
| 28 | ** Public license version 3 or any later version approved by the KDE Free |
| 29 | ** Qt Foundation. The licenses are as published by the Free Software |
| 30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
| 31 | ** included in the packaging of this file. Please review the following |
| 32 | ** information to ensure the GNU General Public License requirements will |
| 33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
| 34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
| 35 | ** |
| 36 | ** $QT_END_LICENSE$ |
| 37 | ** |
| 38 | ****************************************************************************/ |
| 39 | |
| 40 | #include "qquickwritingsystemlistmodel_p.h" |
| 41 | #include <QtGui/qfontdatabase.h> |
| 42 | #include <QtQml/qqmlcontext.h> |
| 43 | #include <QtQml/qqml.h> |
| 44 | #include <QtQml/qqmlengine.h> |
| 45 | |
| 46 | QT_BEGIN_NAMESPACE |
| 47 | |
| 48 | class QQuickWritingSystemListModelPrivate |
| 49 | { |
| 50 | Q_DECLARE_PUBLIC(QQuickWritingSystemListModel) |
| 51 | |
| 52 | public: |
| 53 | QQuickWritingSystemListModelPrivate(QQuickWritingSystemListModel *q) |
| 54 | : q_ptr(q) |
| 55 | {} |
| 56 | |
| 57 | QQuickWritingSystemListModel *q_ptr; |
| 58 | QList<QFontDatabase::WritingSystem> wss; |
| 59 | QHash<int, QByteArray> roleNames; |
| 60 | ~QQuickWritingSystemListModelPrivate() {} |
| 61 | void init(); |
| 62 | }; |
| 63 | |
| 64 | |
| 65 | void QQuickWritingSystemListModelPrivate::init() |
| 66 | { |
| 67 | Q_Q(QQuickWritingSystemListModel); |
| 68 | wss << QFontDatabase::Any; |
| 69 | QFontDatabase db; |
| 70 | wss << db.writingSystems(); |
| 71 | |
| 72 | emit q->rowCountChanged(); |
| 73 | emit q->writingSystemsChanged(); |
| 74 | } |
| 75 | |
| 76 | QQuickWritingSystemListModel::QQuickWritingSystemListModel(QObject *parent) |
| 77 | : QAbstractListModel(parent), d_ptr(new QQuickWritingSystemListModelPrivate(this)) |
| 78 | { |
| 79 | Q_D(QQuickWritingSystemListModel); |
| 80 | d->roleNames[WritingSystemNameRole] = "name" ; |
| 81 | d->roleNames[WritingSystemSampleRole] = "sample" ; |
| 82 | d->init(); |
| 83 | } |
| 84 | |
| 85 | QQuickWritingSystemListModel::~QQuickWritingSystemListModel() |
| 86 | { |
| 87 | } |
| 88 | |
| 89 | QVariant QQuickWritingSystemListModel::data(const QModelIndex &index, int role) const |
| 90 | { |
| 91 | Q_D(const QQuickWritingSystemListModel); |
| 92 | QVariant rv; |
| 93 | |
| 94 | if (index.row() >= d->wss.size()) |
| 95 | return rv; |
| 96 | |
| 97 | switch (role) |
| 98 | { |
| 99 | case WritingSystemNameRole: |
| 100 | rv = QFontDatabase::writingSystemName(writingSystem: d->wss.at(i: index.row())); |
| 101 | break; |
| 102 | case WritingSystemSampleRole: |
| 103 | rv = QFontDatabase::writingSystemSample(writingSystem: d->wss.at(i: index.row())); |
| 104 | break; |
| 105 | default: |
| 106 | break; |
| 107 | } |
| 108 | return rv; |
| 109 | } |
| 110 | |
| 111 | QHash<int, QByteArray> QQuickWritingSystemListModel::roleNames() const |
| 112 | { |
| 113 | Q_D(const QQuickWritingSystemListModel); |
| 114 | return d->roleNames; |
| 115 | } |
| 116 | |
| 117 | int QQuickWritingSystemListModel::rowCount(const QModelIndex &parent) const |
| 118 | { |
| 119 | Q_D(const QQuickWritingSystemListModel); |
| 120 | Q_UNUSED(parent); |
| 121 | return d->wss.size(); |
| 122 | } |
| 123 | |
| 124 | QModelIndex QQuickWritingSystemListModel::index(int row, int , const QModelIndex &) const |
| 125 | { |
| 126 | return createIndex(arow: row, acolumn: 0); |
| 127 | } |
| 128 | |
| 129 | QStringList QQuickWritingSystemListModel::writingSystems() const |
| 130 | { |
| 131 | Q_D(const QQuickWritingSystemListModel); |
| 132 | QStringList result; |
| 133 | result.reserve(alloc: d->wss.size()); |
| 134 | for (QFontDatabase::WritingSystem ws : qAsConst(t: d->wss)) |
| 135 | result.append(t: QFontDatabase::writingSystemName(writingSystem: ws)); |
| 136 | |
| 137 | return result; |
| 138 | } |
| 139 | |
| 140 | QJSValue QQuickWritingSystemListModel::get(int idx) const |
| 141 | { |
| 142 | Q_D(const QQuickWritingSystemListModel); |
| 143 | |
| 144 | QJSEngine *engine = qmlEngine(this); |
| 145 | |
| 146 | if (idx < 0 || idx >= count()) |
| 147 | return engine->newObject(); |
| 148 | |
| 149 | QJSValue result = engine->newObject(); |
| 150 | int count = d->roleNames.count(); |
| 151 | for (int i = 0; i < count; ++i) |
| 152 | result.setProperty(name: QString(d->roleNames[Qt::UserRole + i + 1]), value: data(index: index(row: idx, 0), role: Qt::UserRole + i + 1).toString()); |
| 153 | |
| 154 | return result; |
| 155 | |
| 156 | } |
| 157 | |
| 158 | void QQuickWritingSystemListModel::classBegin() |
| 159 | { |
| 160 | } |
| 161 | |
| 162 | void QQuickWritingSystemListModel::componentComplete() |
| 163 | { |
| 164 | } |
| 165 | |
| 166 | QT_END_NAMESPACE |
| 167 | |