File indexing completed on 2024-12-08 07:55:45

0001 // SPDX-FileCopyrightText: 2022 Devin Lin <devin@kde.org>
0002 // SPDX-License-Identifier: GPL-2.0-or-later
0003 
0004 #include "fontlistmodel.h"
0005 
0006 #include <QFontDatabase>
0007 
0008 FontListModel *FontListModel::self()
0009 {
0010     static FontListModel *singleton = new FontListModel();
0011     return singleton;
0012 }
0013 
0014 FontListModel::FontListModel(QObject *parent)
0015     : QAbstractListModel{parent}
0016 {
0017     m_fontList = QFontDatabase::families(QFontDatabase::Any);
0018 }
0019 
0020 int FontListModel::rowCount(const QModelIndex &parent) const
0021 {
0022     Q_UNUSED(parent)
0023     return m_fontList.count();
0024 }
0025 
0026 QVariant FontListModel::data(const QModelIndex &index, int role) const
0027 {
0028     if (!index.isValid() || index.row() >= m_fontList.size() || index.row() < 0) {
0029         return {};
0030     }
0031 
0032     switch (role) {
0033     case NameRole:
0034         return m_fontList[index.row()];
0035     }
0036     return {};
0037 }
0038 
0039 QHash<int, QByteArray> FontListModel::roleNames() const
0040 {
0041     return {{NameRole, "name"}};
0042 }
0043 
0044 FontListSearchModel *FontListSearchModel::self()
0045 {
0046     static auto singleton = new FontListSearchModel();
0047     return singleton;
0048 }
0049 
0050 FontListSearchModel::FontListSearchModel(QObject *parent)
0051     : QSortFilterProxyModel(parent)
0052 {
0053     setFilterCaseSensitivity(Qt::CaseInsensitive);
0054     setSourceModel(FontListModel::self());
0055     setFilterRole(FontListModel::NameRole);
0056 }