File indexing completed on 2024-04-28 05:36:34
0001 /* 0002 * SPDX-FileCopyrightText: 2009 Ben Cooksley <bcooksley@kde.org> 0003 * SPDX-FileCopyrightText: 2007 Will Stephenson <wstephenson@kde.org> 0004 * 0005 * SPDX-License-Identifier: GPL-2.0-or-later 0006 */ 0007 0008 #include "MenuProxyModel.h" 0009 0010 #include "MenuItem.h" 0011 #include "MenuModel.h" 0012 0013 #include <KPluginMetaData> 0014 0015 MenuProxyModel::MenuProxyModel(QObject *parent) 0016 : KCategorizedSortFilterProxyModel(parent) 0017 , m_filterHighlightsEntries(true) 0018 { 0019 setSortRole(MenuModel::UserSortRole); 0020 setFilterRole(MenuModel::UserFilterRole); 0021 setFilterCaseSensitivity(Qt::CaseInsensitive); 0022 } 0023 0024 QHash<int, QByteArray> MenuProxyModel::roleNames() const 0025 { 0026 QHash<int, QByteArray> names = KCategorizedSortFilterProxyModel::roleNames(); 0027 names[KCategorizedSortFilterProxyModel::CategoryDisplayRole] = "categoryDisplayRole"; 0028 return names; 0029 } 0030 0031 bool MenuProxyModel::lessThan(const QModelIndex &left, const QModelIndex &right) const 0032 { 0033 if (isCategorizedModel()) { 0034 return KCategorizedSortFilterProxyModel::lessThan(left, right); 0035 } 0036 0037 QVariant leftWeight = left.data(MenuModel::UserSortRole); 0038 QVariant rightWeight = right.data(MenuModel::UserSortRole); 0039 0040 if (leftWeight.toInt() == rightWeight.toInt()) { 0041 return left.data().toString() < right.data().toString(); 0042 } 0043 0044 return leftWeight.toInt() < rightWeight.toInt(); 0045 } 0046 0047 bool MenuProxyModel::subSortLessThan(const QModelIndex &left, const QModelIndex &right) const 0048 { 0049 if (isCategorizedModel()) { 0050 QVariant leftWeight = left.data(MenuModel::UserSortRole); 0051 QVariant rightWeight = right.data(MenuModel::UserSortRole); 0052 0053 if (!leftWeight.isValid() || !rightWeight.isValid()) { 0054 return KCategorizedSortFilterProxyModel::subSortLessThan(left, right); 0055 } else { 0056 if (leftWeight.toInt() == rightWeight.toInt()) { 0057 return left.data().toString() < right.data().toString(); 0058 } else { 0059 return leftWeight.toInt() < rightWeight.toInt(); 0060 } 0061 } 0062 } 0063 return KCategorizedSortFilterProxyModel::subSortLessThan(left, right); 0064 } 0065 0066 bool MenuProxyModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const 0067 { 0068 if (!m_filterHighlightsEntries) { 0069 // Don't show empty categories 0070 QModelIndex index = sourceModel()->index(source_row, 0, source_parent); 0071 auto mItem = index.data(Qt::UserRole).value<MenuItem *>(); 0072 if (mItem->menu() && mItem->children().isEmpty()) { 0073 return false; 0074 } 0075 if (mItem->metaData().pluginId() == QLatin1String("kcm_landingpage")) { 0076 return false; 0077 } else { 0078 return KCategorizedSortFilterProxyModel::filterAcceptsRow(source_row, source_parent); 0079 } 0080 } 0081 0082 QModelIndex index = sourceModel()->index(source_row, 0, source_parent); 0083 auto mItem = index.data(Qt::UserRole).value<MenuItem *>(); 0084 0085 if (mItem->metaData().pluginId() == QLatin1String("kcm_landingpage")) { 0086 return false; 0087 } 0088 0089 // accept only systemsettings categories that have children 0090 if (mItem->children().isEmpty() && mItem->isSystemsettingsCategory()) { 0091 return false; 0092 } else { 0093 return true; // Items matching the regexp are disabled, not hidden 0094 } 0095 } 0096 0097 void MenuProxyModel::setFilterHighlightsEntries(bool highlight) 0098 { 0099 m_filterHighlightsEntries = highlight; 0100 } 0101 0102 bool MenuProxyModel::filterHighlightsEntries() const 0103 { 0104 return m_filterHighlightsEntries; 0105 } 0106 0107 Qt::ItemFlags MenuProxyModel::flags(const QModelIndex &index) const 0108 { 0109 if (!index.isValid()) { 0110 return Qt::NoItemFlags; 0111 } 0112 0113 QString matchText = index.data(MenuModel::UserFilterRole).toString(); 0114 QRegularExpression pattern = KCategorizedSortFilterProxyModel::filterRegularExpression(); 0115 0116 if (!matchText.contains(pattern)) { 0117 return Qt::NoItemFlags; 0118 } else { 0119 return Qt::ItemIsEnabled | Qt::ItemIsSelectable; 0120 } 0121 } 0122 0123 void MenuProxyModel::setFilterRegularExpression(const QString &pattern) 0124 { 0125 if (pattern == filterRegularExpression()) { 0126 return; 0127 } 0128 Q_EMIT layoutAboutToBeChanged(); 0129 KCategorizedSortFilterProxyModel::setFilterRegularExpression(pattern); 0130 Q_EMIT layoutChanged(); 0131 Q_EMIT filterRegularExpressionChanged(); 0132 } 0133 0134 QString MenuProxyModel::filterRegularExpression() const 0135 { 0136 return KCategorizedSortFilterProxyModel::filterRegularExpression().pattern(); 0137 } 0138 0139 void MenuProxyModel::setFilterRegularExpression(const QRegularExpression ®Exp) 0140 { 0141 Q_EMIT layoutAboutToBeChanged(); 0142 KCategorizedSortFilterProxyModel::setFilterRegularExpression(regExp); 0143 Q_EMIT layoutChanged(); 0144 } 0145 0146 #include "moc_MenuProxyModel.cpp"