Lomiri
Loading...
Searching...
No Matches
appdrawerproxymodel.cpp
1/*
2 * Copyright (C) 2016 Canonical Ltd.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 3.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17#include "appdrawerproxymodel.h"
18
19#include <lomiri/shell/launcher/LauncherItemInterface.h>
20
21#include <QDebug>
22
23AppDrawerProxyModel::AppDrawerProxyModel(QObject *parent):
24 QSortFilterProxyModel(parent)
25{
26 setSortRole(AppDrawerModelInterface::RoleName);
27 setSortLocaleAware(true);
28 sort(0);
29
30 connect(this, &QAbstractListModel::rowsInserted, this, &AppDrawerProxyModel::countChanged);
31 connect(this, &QAbstractListModel::rowsRemoved, this, &AppDrawerProxyModel::countChanged);
32 connect(this, &QAbstractListModel::layoutChanged, this, &AppDrawerProxyModel::countChanged);
33}
34
35QAbstractItemModel *AppDrawerProxyModel::source() const
36{
37 return m_source;
38}
39
40void AppDrawerProxyModel::setSource(QAbstractItemModel *source)
41{
42 if (m_source != source) {
43 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();
49 }
50}
51
52AppDrawerProxyModel::GroupBy AppDrawerProxyModel::group() const
53{
54 return m_group;
55}
56
57void AppDrawerProxyModel::setGroup(AppDrawerProxyModel::GroupBy group)
58{
59 if (m_group != group) {
60 m_group = group;
61 Q_EMIT groupChanged();
62 invalidateFilter();
63 }
64}
65
66QString AppDrawerProxyModel::filterLetter() const
67{
68 return m_filterLetter;
69}
70
71void AppDrawerProxyModel::setFilterLetter(const QString &filterLetter)
72{
73 if (m_filterLetter != filterLetter) {
74 m_filterLetter = filterLetter;
75 Q_EMIT filterLetterChanged();
76 invalidateFilter();
77 }
78}
79
80QString AppDrawerProxyModel::filterString() const
81{
82 return m_filterString;
83}
84
85void AppDrawerProxyModel::setFilterString(const QString &filterString)
86{
87 if (m_filterString != filterString) {
88 m_filterString = filterString;
89 Q_EMIT filterStringChanged();
90 invalidateFilter();
91 }
92}
93
94AppDrawerProxyModel::SortBy AppDrawerProxyModel::sortBy() const
95{
96 return m_sortBy;
97}
98
99void AppDrawerProxyModel::setSortBy(AppDrawerProxyModel::SortBy sortBy)
100{
101 if (m_sortBy != sortBy) {
102 m_sortBy = sortBy;
103 Q_EMIT sortByChanged();
104 setSortRole(m_sortBy == SortByAToZ ? AppDrawerModelInterface::RoleName : AppDrawerModelInterface::RoleUsage);
105 sort(0);
106 }
107}
108
109int AppDrawerProxyModel::count() const
110{
111 return rowCount();
112}
113
114QVariant AppDrawerProxyModel::data(const QModelIndex &index, int role) const
115{
116 QModelIndex idx = mapToSource(index);
117 if (role == Qt::UserRole) {
118 QString name = m_source->data(idx, AppDrawerModelInterface::RoleName).toString();
119 return name.length() > 0 ? QString(name.at(0)).toUpper() : QChar();
120 }
121 return m_source->data(idx, role);
122}
123
124QHash<int, QByteArray> AppDrawerProxyModel::roleNames() const
125{
126 if (m_source) {
127 QHash<int, QByteArray> roles = m_source->roleNames();
128 roles.insert(Qt::UserRole, "letter");
129 return roles;
130 }
131 return QHash<int, QByteArray>();
132}
133
134bool AppDrawerProxyModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const
135{
136 Q_UNUSED(source_parent)
137
138 if (m_group == GroupByAToZ && source_row > 0) {
139 QString currentName = m_source->data(m_source->index(source_row, 0), AppDrawerModelInterface::RoleName).toString();
140 QChar currentLetter = currentName.length() > 0 ? currentName.at(0) : QChar();
141 QString previousName = m_source->data(m_source->index(source_row - 1,0 ), AppDrawerModelInterface::RoleName).toString();
142 QChar previousLetter = previousName.length() > 0 ? previousName.at(0) : QChar();
143 if (currentLetter.toLower() == previousLetter.toLower()) {
144 return false;
145 }
146 } else if(m_group == GroupByAll && source_row > 0) {
147 return false;
148 }
149 if (!m_filterLetter.isEmpty()) {
150 QString currentName = m_source->data(m_source->index(source_row, 0), AppDrawerModelInterface::RoleName).toString();
151 QString currentLetter = currentName.length() > 0 ? QString(currentName.at(0)) : QString();
152 if (currentLetter.toLower() != m_filterLetter.toLower()) {
153 return false;
154 }
155 }
156 if (!m_filterString.isEmpty()) {
157 QStringList allWords = m_source->data(m_source->index(source_row, 0), AppDrawerModelInterface::RoleKeywords).toStringList();
158 allWords.prepend(m_source->data(m_source->index(source_row, 0), AppDrawerModelInterface::RoleName).toString());
159 bool found = false;
160 Q_FOREACH (const QString &currentWord, allWords) {
161 if (currentWord.contains(m_filterString, Qt::CaseInsensitive)) {
162 found = true;
163 break;
164 }
165 }
166 if (!found) {
167 return false;
168 }
169 }
170 return true;
171}
172
173QString AppDrawerProxyModel::appId(int index) const
174{
175 if (index >= 0 && index < rowCount()) {
176 QModelIndex sourceIndex = mapToSource(this->index(index, 0));
177
178 AppDrawerModelInterface* adm = dynamic_cast<AppDrawerModelInterface*>(m_source);
179 if (adm) {
180 return adm->data(sourceIndex, AppDrawerModelInterface::RoleAppId).toString();
181 }
182
183 AppDrawerProxyModel* adpm = qobject_cast<AppDrawerProxyModel*>(m_source);
184 if (adpm) {
185 return adpm->appId(sourceIndex.row());
186 }
187 }
188 return QString();
189}