File indexing completed on 2024-04-28 16:42:55

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     QFontDatabase database;
0018     m_fontList = database.families(QFontDatabase::Any);
0019 }
0020 
0021 int FontListModel::rowCount(const QModelIndex &parent) const
0022 {
0023     Q_UNUSED(parent)
0024     return m_fontList.count();
0025 }
0026  
0027 QVariant FontListModel::data(const QModelIndex &index, int role) const
0028 {
0029     if (!index.isValid() || index.row() >= m_fontList.size() || index.row() < 0) {
0030         return {};
0031     }
0032     
0033     switch (role) {
0034         case NameRole:
0035             return m_fontList[index.row()];
0036     }
0037     return {};
0038 }
0039 
0040 QHash<int, QByteArray> FontListModel::roleNames() const
0041 {
0042     return {{NameRole, "name"}};
0043 }
0044 
0045 FontListSearchModel *FontListSearchModel::self()
0046 {
0047     static auto singleton = new FontListSearchModel();
0048     return singleton;
0049 }
0050 
0051 FontListSearchModel::FontListSearchModel(QObject *parent)
0052     : QSortFilterProxyModel(parent)
0053 {
0054     setFilterCaseSensitivity(Qt::CaseInsensitive);
0055     setSourceModel(FontListModel::self());
0056     setFilterRole(FontListModel::NameRole);
0057 }