17#include "appdrawerproxymodel.h"
19#include <lomiri/shell/launcher/LauncherItemInterface.h>
23AppDrawerProxyModel::AppDrawerProxyModel(QObject *parent):
24 QSortFilterProxyModel(parent)
26 setSortRole(AppDrawerModelInterface::RoleName);
27 setSortLocaleAware(
true);
30 connect(
this, &QAbstractListModel::rowsInserted,
this, &AppDrawerProxyModel::countChanged);
31 connect(
this, &QAbstractListModel::rowsRemoved,
this, &AppDrawerProxyModel::countChanged);
32 connect(
this, &QAbstractListModel::layoutChanged,
this, &AppDrawerProxyModel::countChanged);
35QAbstractItemModel *AppDrawerProxyModel::source()
const
40void AppDrawerProxyModel::setSource(QAbstractItemModel *source)
42 if (m_source != source) {
44 setSourceModel(m_source);
45 setSortRole(m_sortBy == SortByAToZ ? AppDrawerModelInterface::RoleName : AppDrawerModelInterface::RoleUsage);
46 connect(m_source, &QAbstractItemModel::rowsRemoved,
this, &AppDrawerProxyModel::invalidate);
47 connect(m_source, &QAbstractItemModel::rowsInserted,
this, &AppDrawerProxyModel::invalidate);
48 Q_EMIT sourceChanged();
52AppDrawerProxyModel::GroupBy AppDrawerProxyModel::group()
const
57void AppDrawerProxyModel::setGroup(AppDrawerProxyModel::GroupBy group)
59 if (m_group != group) {
61 Q_EMIT groupChanged();
66QString AppDrawerProxyModel::filterLetter()
const
68 return m_filterLetter;
71void AppDrawerProxyModel::setFilterLetter(
const QString &filterLetter)
73 if (m_filterLetter != filterLetter) {
74 m_filterLetter = filterLetter;
75 Q_EMIT filterLetterChanged();
80QString AppDrawerProxyModel::filterString()
const
82 return m_filterString;
85void AppDrawerProxyModel::setFilterString(
const QString &filterString)
87 const QString filterStringOptimised = removeDiacritics(filterString);
88 if (m_filterString != filterStringOptimised) {
89 m_filterString = filterStringOptimised;
90 Q_EMIT filterStringChanged();
95AppDrawerProxyModel::SortBy AppDrawerProxyModel::sortBy()
const
100void AppDrawerProxyModel::setSortBy(AppDrawerProxyModel::SortBy sortBy)
102 if (m_sortBy != sortBy) {
104 Q_EMIT sortByChanged();
105 setSortRole(m_sortBy == SortByAToZ ? AppDrawerModelInterface::RoleName : AppDrawerModelInterface::RoleUsage);
110int AppDrawerProxyModel::count()
const
115QVariant AppDrawerProxyModel::data(
const QModelIndex &index,
int role)
const
117 QModelIndex idx = mapToSource(index);
118 if (role == Qt::UserRole) {
119 QString name = m_source->data(idx, AppDrawerModelInterface::RoleName).toString();
120 return name.length() > 0 ? QString(name.at(0)).toUpper() : QChar();
122 return m_source->data(idx, role);
125QHash<int, QByteArray> AppDrawerProxyModel::roleNames()
const
128 QHash<int, QByteArray> roles = m_source->roleNames();
129 roles.insert(Qt::UserRole,
"letter");
132 return QHash<int, QByteArray>();
135bool AppDrawerProxyModel::filterAcceptsRow(
int source_row,
const QModelIndex &source_parent)
const
137 Q_UNUSED(source_parent)
139 if (m_group == GroupByAToZ && source_row > 0) {
140 QString currentName = m_source->data(m_source->index(source_row, 0), AppDrawerModelInterface::RoleName).toString();
141 QChar currentLetter = currentName.length() > 0 ? currentName.at(0) : QChar();
142 QString previousName = m_source->data(m_source->index(source_row - 1,0 ), AppDrawerModelInterface::RoleName).toString();
143 QChar previousLetter = previousName.length() > 0 ? previousName.at(0) : QChar();
144 if (currentLetter.toLower() == previousLetter.toLower()) {
147 }
else if(m_group == GroupByAll && source_row > 0) {
150 if (!m_filterLetter.isEmpty()) {
151 QString currentName = m_source->data(m_source->index(source_row, 0), AppDrawerModelInterface::RoleName).toString();
152 QString currentLetter = currentName.length() > 0 ? QString(currentName.at(0)) : QString();
153 if (currentLetter.toLower() != m_filterLetter.toLower()) {
157 if (!m_filterString.isEmpty()) {
158 QStringList allWords = m_source->data(m_source->index(source_row, 0), AppDrawerModelInterface::RoleKeywords).toStringList();
159 allWords.prepend(m_source->data(m_source->index(source_row, 0), AppDrawerModelInterface::RoleName).toString());
161 Q_FOREACH (
const QString ¤tWord, allWords) {
162 if (removeDiacritics(currentWord).contains(m_filterString, Qt::CaseInsensitive)) {
174QString AppDrawerProxyModel::removeDiacritics(
const QString &input)
const
176 QString normalized = input.normalized(QString::NormalizationForm_D);
178 QString result = normalized;
179 result.remove(QRegularExpression(
"[\\p{M}]"));
184QString AppDrawerProxyModel::appId(
int index)
const
186 if (index >= 0 && index < rowCount()) {
187 QModelIndex sourceIndex = mapToSource(this->index(index, 0));
189 AppDrawerModelInterface* adm =
dynamic_cast<AppDrawerModelInterface*
>(m_source);
191 return adm->data(sourceIndex, AppDrawerModelInterface::RoleAppId).toString();
194 AppDrawerProxyModel* adpm = qobject_cast<AppDrawerProxyModel*>(m_source);
196 return adpm->appId(sourceIndex.row());