| 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 "qquickfontlistmodel_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 QQuickFontListModelPrivate |
| 49 | { |
| 50 | Q_DECLARE_PUBLIC(QQuickFontListModel) |
| 51 | |
| 52 | public: |
| 53 | QQuickFontListModelPrivate(QQuickFontListModel *q) |
| 54 | : q_ptr(q), ws(QFontDatabase::Any) |
| 55 | , options(QFontDialogOptions::create()) |
| 56 | {} |
| 57 | |
| 58 | QQuickFontListModel *q_ptr; |
| 59 | QFontDatabase db; |
| 60 | QFontDatabase::WritingSystem ws; |
| 61 | QSharedPointer<QFontDialogOptions> options; |
| 62 | QStringList families; |
| 63 | QHash<int, QByteArray> roleNames; |
| 64 | ~QQuickFontListModelPrivate() {} |
| 65 | void init(); |
| 66 | }; |
| 67 | |
| 68 | |
| 69 | void QQuickFontListModelPrivate::init() |
| 70 | { |
| 71 | Q_Q(QQuickFontListModel); |
| 72 | |
| 73 | families = db.families(); |
| 74 | |
| 75 | emit q->rowCountChanged(); |
| 76 | emit q->writingSystemChanged(); |
| 77 | } |
| 78 | |
| 79 | QQuickFontListModel::QQuickFontListModel(QObject *parent) |
| 80 | : QAbstractListModel(parent), d_ptr(new QQuickFontListModelPrivate(this)) |
| 81 | { |
| 82 | Q_D(QQuickFontListModel); |
| 83 | d->roleNames[FontFamilyRole] = "family" ; |
| 84 | d->init(); |
| 85 | } |
| 86 | |
| 87 | QQuickFontListModel::~QQuickFontListModel() |
| 88 | { |
| 89 | } |
| 90 | |
| 91 | QVariant QQuickFontListModel::data(const QModelIndex &index, int role) const |
| 92 | { |
| 93 | Q_D(const QQuickFontListModel); |
| 94 | QVariant rv; |
| 95 | |
| 96 | if (index.row() >= d->families.size()) |
| 97 | return rv; |
| 98 | |
| 99 | switch (role) |
| 100 | { |
| 101 | case FontFamilyRole: |
| 102 | rv = d->families.at(i: index.row()); |
| 103 | break; |
| 104 | default: |
| 105 | break; |
| 106 | } |
| 107 | return rv; |
| 108 | } |
| 109 | |
| 110 | QHash<int, QByteArray> QQuickFontListModel::roleNames() const |
| 111 | { |
| 112 | Q_D(const QQuickFontListModel); |
| 113 | return d->roleNames; |
| 114 | } |
| 115 | |
| 116 | int QQuickFontListModel::rowCount(const QModelIndex &parent) const |
| 117 | { |
| 118 | Q_D(const QQuickFontListModel); |
| 119 | Q_UNUSED(parent); |
| 120 | return d->families.size(); |
| 121 | } |
| 122 | |
| 123 | QModelIndex QQuickFontListModel::index(int row, int , const QModelIndex &) const |
| 124 | { |
| 125 | return createIndex(arow: row, acolumn: 0); |
| 126 | } |
| 127 | |
| 128 | QString QQuickFontListModel::writingSystem() const |
| 129 | { |
| 130 | Q_D(const QQuickFontListModel); |
| 131 | return QFontDatabase::writingSystemName(writingSystem: d->ws); |
| 132 | } |
| 133 | |
| 134 | void QQuickFontListModel::setWritingSystem(const QString &wSystem) |
| 135 | { |
| 136 | Q_D(QQuickFontListModel); |
| 137 | |
| 138 | if (wSystem == writingSystem()) |
| 139 | return; |
| 140 | |
| 141 | QList<QFontDatabase::WritingSystem> wss; |
| 142 | wss << QFontDatabase::Any; |
| 143 | wss << d->db.writingSystems(); |
| 144 | for (QFontDatabase::WritingSystem ws : qAsConst(t&: wss)) { |
| 145 | if (wSystem == QFontDatabase::writingSystemName(writingSystem: ws)) { |
| 146 | d->ws = ws; |
| 147 | updateFamilies(); |
| 148 | return; |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | void QQuickFontListModel::updateFamilies() |
| 154 | { |
| 155 | Q_D(QQuickFontListModel); |
| 156 | |
| 157 | beginResetModel(); |
| 158 | const QFontDialogOptions::FontDialogOptions scalableMask = (QFontDialogOptions::FontDialogOptions)(QFontDialogOptions::ScalableFonts | QFontDialogOptions::NonScalableFonts); |
| 159 | const QFontDialogOptions::FontDialogOptions spacingMask = (QFontDialogOptions::FontDialogOptions)(QFontDialogOptions::ProportionalFonts | QFontDialogOptions::MonospacedFonts); |
| 160 | const QFontDialogOptions::FontDialogOptions options = d->options->options(); |
| 161 | |
| 162 | d->families.clear(); |
| 163 | const auto families = d->db.families(writingSystem: d->ws); |
| 164 | for (const QString &family : families) { |
| 165 | if ((options & scalableMask) && (options & scalableMask) != scalableMask) { |
| 166 | if (bool(options & QFontDialogOptions::ScalableFonts) != d->db.isSmoothlyScalable(family)) |
| 167 | continue; |
| 168 | } |
| 169 | if ((options & spacingMask) && (options & spacingMask) != spacingMask) { |
| 170 | if (bool(options & QFontDialogOptions::MonospacedFonts) != d->db.isFixedPitch(family)) |
| 171 | continue; |
| 172 | } |
| 173 | d->families << family; |
| 174 | } |
| 175 | endResetModel(); |
| 176 | } |
| 177 | |
| 178 | bool QQuickFontListModel::scalableFonts() const |
| 179 | { |
| 180 | Q_D(const QQuickFontListModel); |
| 181 | return d->options->testOption(option: QFontDialogOptions::ScalableFonts); |
| 182 | } |
| 183 | |
| 184 | bool QQuickFontListModel::nonScalableFonts() const |
| 185 | { |
| 186 | Q_D(const QQuickFontListModel); |
| 187 | return d->options->testOption(option: QFontDialogOptions::NonScalableFonts); |
| 188 | } |
| 189 | |
| 190 | bool QQuickFontListModel::monospacedFonts() const |
| 191 | { |
| 192 | Q_D(const QQuickFontListModel); |
| 193 | return d->options->testOption(option: QFontDialogOptions::MonospacedFonts); |
| 194 | } |
| 195 | |
| 196 | bool QQuickFontListModel::proportionalFonts() const |
| 197 | { |
| 198 | Q_D(const QQuickFontListModel); |
| 199 | return d->options->testOption(option: QFontDialogOptions::ProportionalFonts); |
| 200 | } |
| 201 | |
| 202 | QJSValue QQuickFontListModel::get(int idx) const |
| 203 | { |
| 204 | Q_D(const QQuickFontListModel); |
| 205 | |
| 206 | QJSEngine *engine = qmlEngine(this); |
| 207 | |
| 208 | if (idx < 0 || idx >= count()) |
| 209 | return engine->newObject(); |
| 210 | |
| 211 | QJSValue result = engine->newObject(); |
| 212 | int count = d->roleNames.count(); |
| 213 | for (int i = 0; i < count; ++i) |
| 214 | result.setProperty(name: QString(d->roleNames[Qt::UserRole + i + 1]), value: data(index: index(row: idx, 0), role: Qt::UserRole + i + 1).toString()); |
| 215 | |
| 216 | return result; |
| 217 | } |
| 218 | |
| 219 | QJSValue QQuickFontListModel::pointSizes() |
| 220 | { |
| 221 | QJSEngine *engine = qmlEngine(this); |
| 222 | |
| 223 | QList<int> pss = QFontDatabase::standardSizes(); |
| 224 | int size = pss.size(); |
| 225 | |
| 226 | QJSValue result = engine->newArray(length: size); |
| 227 | for (int i = 0; i < size; ++i) |
| 228 | result.setProperty(arrayIndex: i, value: pss.at(i)); |
| 229 | |
| 230 | return result; |
| 231 | } |
| 232 | |
| 233 | void QQuickFontListModel::classBegin() |
| 234 | { |
| 235 | } |
| 236 | |
| 237 | void QQuickFontListModel::componentComplete() |
| 238 | { |
| 239 | } |
| 240 | |
| 241 | void QQuickFontListModel::setScalableFonts(bool arg) |
| 242 | { |
| 243 | Q_D(QQuickFontListModel); |
| 244 | d->options->setOption(option: QFontDialogOptions::ScalableFonts, on: arg); |
| 245 | updateFamilies(); |
| 246 | emit scalableFontsChanged(); |
| 247 | } |
| 248 | |
| 249 | void QQuickFontListModel::setNonScalableFonts(bool arg) |
| 250 | { |
| 251 | Q_D(QQuickFontListModel); |
| 252 | d->options->setOption(option: QFontDialogOptions::NonScalableFonts, on: arg); |
| 253 | updateFamilies(); |
| 254 | emit nonScalableFontsChanged(); |
| 255 | } |
| 256 | |
| 257 | void QQuickFontListModel::setMonospacedFonts(bool arg) |
| 258 | { |
| 259 | Q_D(QQuickFontListModel); |
| 260 | d->options->setOption(option: QFontDialogOptions::MonospacedFonts, on: arg); |
| 261 | updateFamilies(); |
| 262 | emit monospacedFontsChanged(); |
| 263 | } |
| 264 | |
| 265 | void QQuickFontListModel::setProportionalFonts(bool arg) |
| 266 | { |
| 267 | Q_D(QQuickFontListModel); |
| 268 | d->options->setOption(option: QFontDialogOptions::ProportionalFonts, on: arg); |
| 269 | updateFamilies(); |
| 270 | emit proportionalFontsChanged(); |
| 271 | } |
| 272 | |
| 273 | QT_END_NAMESPACE |
| 274 | |