blob: 6ae5ae031d23091b181b09e2f400f2f2a620a6c7 [file] [log] [blame]
agsantos655d8e22020-08-10 17:36:47 -04001/**
2 * Copyright (C) 2020 by Savoir-faire Linux
3 * Author: Aline Gondim Santos <aline.gondimsantos@savoirfairelinux.com>
Sébastien Blin1f915762020-08-03 13:27:42 -04004 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#include "preferenceitemlistmodel.h"
20#include <map>
21
agsantos78726ec2020-08-18 17:41:05 -040022std::map<QString, int> mapType {{QString("List"), PreferenceItemListModel::Type::LIST},
23 {QString("UserList"), PreferenceItemListModel::Type::USERLIST}};
Sébastien Blin1f915762020-08-03 13:27:42 -040024
agsantose1672082020-08-10 12:49:56 -040025PreferenceItemListModel::PreferenceItemListModel(QObject* parent)
Sébastien Blin1f915762020-08-03 13:27:42 -040026 : QAbstractListModel(parent)
27{}
28
29PreferenceItemListModel::~PreferenceItemListModel() {}
30
31int
agsantose1672082020-08-10 12:49:56 -040032PreferenceItemListModel::rowCount(const QModelIndex& parent) const
Sébastien Blin1f915762020-08-03 13:27:42 -040033{
34 if (!parent.isValid()) {
agsantos78726ec2020-08-18 17:41:05 -040035 /// Count.
Sébastien Blin1f915762020-08-03 13:27:42 -040036 return LRCInstance::pluginModel().getPluginPreferences(pluginId_).size();
37 }
agsantos78726ec2020-08-18 17:41:05 -040038 /// A valid QModelIndex returns 0 as no entry has sub-elements.
Sébastien Blin1f915762020-08-03 13:27:42 -040039 return 0;
40}
41
42int
agsantose1672082020-08-10 12:49:56 -040043PreferenceItemListModel::columnCount(const QModelIndex& parent) const
Sébastien Blin1f915762020-08-03 13:27:42 -040044{
45 Q_UNUSED(parent);
agsantos78726ec2020-08-18 17:41:05 -040046 /// Only need one column.
Sébastien Blin1f915762020-08-03 13:27:42 -040047 return 1;
48}
49
50QVariant
agsantose1672082020-08-10 12:49:56 -040051PreferenceItemListModel::data(const QModelIndex& index, int role) const
Sébastien Blin1f915762020-08-03 13:27:42 -040052{
53 auto preferenceList = LRCInstance::pluginModel().getPluginPreferences(pluginId_);
54 if (!index.isValid() || preferenceList.size() <= index.row()) {
55 return QVariant();
agsantose1672082020-08-10 12:49:56 -040056 }
Sébastien Blin1f915762020-08-03 13:27:42 -040057
58 auto details = preferenceList.at(index.row());
59 int type = Type::DEFAULT;
60 auto it = mapType.find(details["type"]);
agsantose1672082020-08-10 12:49:56 -040061 if (it != mapType.end()) {
Sébastien Blin1f915762020-08-03 13:27:42 -040062 type = mapType[details["type"]];
63 }
agsantos78726ec2020-08-18 17:41:05 -040064 QString preferenceCurrent = LRCInstance::pluginModel().getPluginPreferencesValues(
65 pluginId_)[details["key"]];
Sébastien Blin1f915762020-08-03 13:27:42 -040066
67 switch (role) {
agsantose1672082020-08-10 12:49:56 -040068 case Role::PreferenceKey:
69 return QVariant(details["key"]);
70 case Role::PreferenceName:
71 return QVariant(details["title"]);
72 case Role::PreferenceSummary:
73 return QVariant(details["summary"]);
74 case Role::PreferenceType:
75 return QVariant(type);
agsantos78726ec2020-08-18 17:41:05 -040076 case Role::PluginId:
77 return QVariant(pluginId_);
78 case Role::PreferenceCurrentValue:
79 return QVariant(preferenceCurrent);
Sébastien Blin1f915762020-08-03 13:27:42 -040080 }
81 return QVariant();
82}
83
84QHash<int, QByteArray>
85PreferenceItemListModel::roleNames() const
86{
87 QHash<int, QByteArray> roles;
88 roles[PreferenceKey] = "PreferenceKey";
89 roles[PreferenceName] = "PreferenceName";
90 roles[PreferenceSummary] = "PreferenceSummary";
91 roles[PreferenceType] = "PreferenceType";
agsantos78726ec2020-08-18 17:41:05 -040092 roles[PluginId] = "PluginId";
93 roles[PreferenceCurrentValue] = "PreferenceCurrentValue";
Sébastien Blin1f915762020-08-03 13:27:42 -040094 return roles;
95}
96
97QModelIndex
agsantose1672082020-08-10 12:49:56 -040098PreferenceItemListModel::index(int row, int column, const QModelIndex& parent) const
Sébastien Blin1f915762020-08-03 13:27:42 -040099{
100 Q_UNUSED(parent);
101 if (column != 0) {
102 return QModelIndex();
103 }
104
105 if (row >= 0 && row < rowCount()) {
106 return createIndex(row, column);
107 }
108 return QModelIndex();
109}
110
111QModelIndex
agsantose1672082020-08-10 12:49:56 -0400112PreferenceItemListModel::parent(const QModelIndex& child) const
Sébastien Blin1f915762020-08-03 13:27:42 -0400113{
114 Q_UNUSED(child);
115 return QModelIndex();
116}
117
118Qt::ItemFlags
agsantose1672082020-08-10 12:49:56 -0400119PreferenceItemListModel::flags(const QModelIndex& index) const
Sébastien Blin1f915762020-08-03 13:27:42 -0400120{
121 auto flags = QAbstractItemModel::flags(index) | Qt::ItemNeverHasChildren | Qt::ItemIsSelectable;
122 if (!index.isValid()) {
123 return QAbstractItemModel::flags(index);
124 }
125 return flags;
126}
127
128void
129PreferenceItemListModel::reset()
130{
131 beginResetModel();
132 endResetModel();
133}
134
135QString
136PreferenceItemListModel::pluginId() const
137{
138 return pluginId_;
139}
140
141void
agsantose1672082020-08-10 12:49:56 -0400142PreferenceItemListModel::setPluginId(const QString& pluginId)
Sébastien Blin1f915762020-08-03 13:27:42 -0400143{
144 pluginId_ = pluginId;
145}
agsantose1672082020-08-10 12:49:56 -0400146
147int
148PreferenceItemListModel::preferencesCount()
149{
150 return LRCInstance::pluginModel().getPluginPreferences(pluginId_).size();
151}