on 2fa3cce help sign in
unity8/plugins/Utils/appdrawerproxymodel.cpp
145 lines 4.6 kB history raw
1#include "appdrawerproxymodel.h"
3#include <QDebug>
5AppDrawerProxyModel::AppDrawerProxyModel(QObject *parent):
6 QSortFilterProxyModel(parent)
7{
8 setSortRole(AppDrawerModelInterface::RoleName);
9 setSortLocaleAware(true);
10 sort(0);
12 connect(this, &QAbstractListModel::rowsInserted, this, &AppDrawerProxyModel::countChanged);
13 connect(this, &QAbstractListModel::rowsRemoved, this, &AppDrawerProxyModel::countChanged);
14 connect(this, &QAbstractListModel::layoutChanged, this, &AppDrawerProxyModel::countChanged);
17QAbstractItemModel *AppDrawerProxyModel::source() const
19 return m_source;
22void AppDrawerProxyModel::setSource(QAbstractItemModel *source)
24 if (m_source != source) {
25 m_source = source;
26 setSourceModel(m_source);
27 setSortRole(m_sortBy == SortByAToZ ? AppDrawerModelInterface::RoleName : AppDrawerModelInterface::RoleUsage);
28 connect(m_source, &QAbstractItemModel::rowsRemoved, this, &AppDrawerProxyModel::invalidateFilter);
29 connect(m_source, &QAbstractItemModel::rowsInserted, this, &AppDrawerProxyModel::invalidateFilter);
30 Q_EMIT sourceChanged();
31 }
34AppDrawerProxyModel::GroupBy AppDrawerProxyModel::group() const
36 return m_group;
39void AppDrawerProxyModel::setGroup(AppDrawerProxyModel::GroupBy group)
41 if (m_group != group) {
42 m_group = group;
43 Q_EMIT groupChanged();
44 invalidateFilter();
45 }
48QString AppDrawerProxyModel::filterLetter() const
50 return m_filterLetter;
53void AppDrawerProxyModel::setFilterLetter(const QString &filterLetter)
55 if (m_filterLetter != filterLetter) {
56 m_filterLetter = filterLetter;
57 Q_EMIT filterLetterChanged();
58 invalidateFilter();
59 }
62QString AppDrawerProxyModel::filterString() const
64 return m_filterString;
67void AppDrawerProxyModel::setFilterString(const QString &filterString)
69 if (m_filterString != filterString) {
70 m_filterString = filterString;
71 Q_EMIT filterStringChanged();
72 invalidateFilter();
73 }
76AppDrawerProxyModel::SortBy AppDrawerProxyModel::sortBy() const
78 return m_sortBy;
81void AppDrawerProxyModel::setSortBy(AppDrawerProxyModel::SortBy sortBy)
83 if (m_sortBy != sortBy) {
84 m_sortBy = sortBy;
85 Q_EMIT sortByChanged();
86 setSortRole(m_sortBy == SortByAToZ ? AppDrawerModelInterface::RoleName : AppDrawerModelInterface::RoleUsage);
87 sort(0);
88 }
91int AppDrawerProxyModel::count() const
93 return rowCount();
96QVariant AppDrawerProxyModel::data(const QModelIndex &index, int role) const
98 QModelIndex idx = mapToSource(index);
99 if (role == Qt::UserRole) {
100 QString name = m_source->data(idx, AppDrawerModelInterface::RoleName).toString();
101 return name.length() > 0 ? QString(name.at(0)).toUpper() : QChar();
102 }
103 return m_source->data(idx, role);
106QHash<int, QByteArray> AppDrawerProxyModel::roleNames() const
108 if (m_source) {
109 QHash<int, QByteArray> roles = m_source->roleNames();
110 roles.insert(Qt::UserRole, "letter");
111 return roles;
112 }
113 return QHash<int, QByteArray>();
116bool AppDrawerProxyModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const
118 Q_UNUSED(source_parent)
120 if (m_group == GroupByAToZ && source_row > 0) {
121 QString currentName = m_source->data(m_source->index(source_row, 0), AppDrawerModelInterface::RoleName).toString();
122 QChar currentLetter = currentName.length() > 0 ? currentName.at(0) : QChar();
123 QString previousName = m_source->data(m_source->index(source_row - 1,0 ), AppDrawerModelInterface::RoleName).toString();
124 QChar previousLetter = previousName.length() > 0 ? previousName.at(0) : QChar();
125 if (currentLetter.toLower() == previousLetter.toLower()) {
126 return false;
127 }
128 } else if(m_group == GroupByAll && source_row > 0) {
129 return false;
130 }
131 if (!m_filterLetter.isEmpty()) {
132 QString currentName = m_source->data(m_source->index(source_row, 0), AppDrawerModelInterface::RoleName).toString();
133 QString currentLetter = currentName.length() > 0 ? QString(currentName.at(0)) : QString();
134 if (currentLetter.toLower() != m_filterLetter.toLower()) {
135 return false;
136 }
137 }
138 if (!m_filterString.isEmpty()) {
139 QString currentLabel = m_source->data(m_source->index(source_row, 0), AppDrawerModelInterface::RoleName).toString().toLower();
140 if (!currentLabel.startsWith(m_filterString.toLower())) {
141 return false;
142 }
143 }
144 return true;