File indexing completed on 2025-01-26 05:06:21
0001 /* 0002 SPDX-FileCopyrightText: 2014 Eike Hein <hein@kde.org> 0003 0004 SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #include "mimetypesmodel.h" 0008 0009 #include <QMimeDatabase> 0010 0011 static bool lessThan(const QMimeType &a, const QMimeType &b) 0012 { 0013 return QString::localeAwareCompare(a.name(), b.name()) < 0; 0014 } 0015 0016 MimeTypesModel::MimeTypesModel(QObject *parent) 0017 : QAbstractListModel(parent) 0018 { 0019 QMimeDatabase db; 0020 m_mimeTypesList = db.allMimeTypes(); 0021 std::stable_sort(m_mimeTypesList.begin(), m_mimeTypesList.end(), lessThan); 0022 0023 m_checkedRows = QList<bool>(m_mimeTypesList.size(), false); 0024 } 0025 0026 MimeTypesModel::~MimeTypesModel() 0027 { 0028 } 0029 0030 QHash<int, QByteArray> MimeTypesModel::roleNames() const 0031 { 0032 return { 0033 {Qt::DisplayRole, "comment"}, 0034 {Qt::UserRole, "name"}, 0035 {Qt::DecorationRole, "decoration"}, 0036 {Qt::CheckStateRole, "checked"}, 0037 }; 0038 } 0039 0040 QVariant MimeTypesModel::data(const QModelIndex &index, int role) const 0041 { 0042 if (index.row() < 0 || index.row() >= m_mimeTypesList.size()) { 0043 return QVariant(); 0044 } 0045 0046 switch (role) { 0047 case Qt::DisplayRole: 0048 return m_mimeTypesList.at(index.row()).comment(); 0049 case Qt::UserRole: 0050 return m_mimeTypesList.at(index.row()).name(); 0051 0052 case Qt::DecorationRole: { 0053 QString icon = m_mimeTypesList.at(index.row()).iconName(); 0054 0055 if (icon.isEmpty()) { 0056 icon = m_mimeTypesList.at(index.row()).genericIconName(); 0057 } 0058 0059 return icon; 0060 } 0061 0062 case Qt::CheckStateRole: 0063 return m_checkedRows.at(index.row()) ? Qt::Checked : Qt::Unchecked; 0064 } 0065 0066 return QVariant(); 0067 } 0068 0069 bool MimeTypesModel::setData(const QModelIndex &index, const QVariant &value, int role) 0070 { 0071 if (index.row() < 0 || index.row() >= m_mimeTypesList.size()) { 0072 return false; 0073 } 0074 0075 if (role == Qt::CheckStateRole) { 0076 const bool newChecked = value.toBool(); 0077 if (m_checkedRows.at(index.row()) != newChecked) { 0078 m_checkedRows[index.row()] = newChecked; 0079 Q_EMIT dataChanged(index, index, {role}); 0080 Q_EMIT checkedTypesChanged(); 0081 return true; 0082 } 0083 } 0084 0085 return false; 0086 } 0087 0088 int MimeTypesModel::indexOfType(const QString &name) const 0089 { 0090 for (int i = 0; i < m_mimeTypesList.size(); i++) { 0091 if (m_mimeTypesList.at(i).name() == name) { 0092 return i; 0093 } 0094 } 0095 return -1; 0096 } 0097 0098 QStringList MimeTypesModel::checkedTypes() const 0099 { 0100 QStringList list; 0101 0102 for (int i = 0; i < m_checkedRows.size(); ++i) { 0103 if (m_checkedRows.at(i)) { 0104 list.append(m_mimeTypesList.at(i).name()); 0105 } 0106 } 0107 0108 if (!list.isEmpty()) { 0109 return list; 0110 } 0111 0112 return QStringList(QString()); 0113 } 0114 0115 void MimeTypesModel::setCheckedTypes(const QStringList &list) 0116 { 0117 m_checkedRows = QList<bool>(m_mimeTypesList.size(), false); 0118 0119 for (const QString &name : list) { 0120 const int row = indexOfType(name); 0121 0122 if (row != -1) { 0123 m_checkedRows[row] = true; 0124 } 0125 } 0126 0127 Q_EMIT dataChanged(index(0, 0), index(m_mimeTypesList.size() - 1, 0), {Qt::CheckStateRole}); 0128 0129 Q_EMIT checkedTypesChanged(); 0130 }