agsantos | 655d8e2 | 2020-08-10 17:36:47 -0400 | [diff] [blame] | 1 | /** |
| 2 | * Copyright (C) 2020 by Savoir-faire Linux |
| 3 | * Author: Aline Gondim Santos <aline.gondimsantos@savoirfairelinux.com> |
Sébastien Blin | 1f91576 | 2020-08-03 13:27:42 -0400 | [diff] [blame] | 4 | * |
| 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 | |
agsantos | 78726ec | 2020-08-18 17:41:05 -0400 | [diff] [blame] | 22 | std::map<QString, int> mapType {{QString("List"), PreferenceItemListModel::Type::LIST}, |
| 23 | {QString("UserList"), PreferenceItemListModel::Type::USERLIST}}; |
Sébastien Blin | 1f91576 | 2020-08-03 13:27:42 -0400 | [diff] [blame] | 24 | |
agsantos | e167208 | 2020-08-10 12:49:56 -0400 | [diff] [blame] | 25 | PreferenceItemListModel::PreferenceItemListModel(QObject* parent) |
Sébastien Blin | 1f91576 | 2020-08-03 13:27:42 -0400 | [diff] [blame] | 26 | : QAbstractListModel(parent) |
| 27 | {} |
| 28 | |
| 29 | PreferenceItemListModel::~PreferenceItemListModel() {} |
| 30 | |
| 31 | int |
agsantos | e167208 | 2020-08-10 12:49:56 -0400 | [diff] [blame] | 32 | PreferenceItemListModel::rowCount(const QModelIndex& parent) const |
Sébastien Blin | 1f91576 | 2020-08-03 13:27:42 -0400 | [diff] [blame] | 33 | { |
| 34 | if (!parent.isValid()) { |
agsantos | 78726ec | 2020-08-18 17:41:05 -0400 | [diff] [blame] | 35 | /// Count. |
Sébastien Blin | 1f91576 | 2020-08-03 13:27:42 -0400 | [diff] [blame] | 36 | return LRCInstance::pluginModel().getPluginPreferences(pluginId_).size(); |
| 37 | } |
agsantos | 78726ec | 2020-08-18 17:41:05 -0400 | [diff] [blame] | 38 | /// A valid QModelIndex returns 0 as no entry has sub-elements. |
Sébastien Blin | 1f91576 | 2020-08-03 13:27:42 -0400 | [diff] [blame] | 39 | return 0; |
| 40 | } |
| 41 | |
| 42 | int |
agsantos | e167208 | 2020-08-10 12:49:56 -0400 | [diff] [blame] | 43 | PreferenceItemListModel::columnCount(const QModelIndex& parent) const |
Sébastien Blin | 1f91576 | 2020-08-03 13:27:42 -0400 | [diff] [blame] | 44 | { |
| 45 | Q_UNUSED(parent); |
agsantos | 78726ec | 2020-08-18 17:41:05 -0400 | [diff] [blame] | 46 | /// Only need one column. |
Sébastien Blin | 1f91576 | 2020-08-03 13:27:42 -0400 | [diff] [blame] | 47 | return 1; |
| 48 | } |
| 49 | |
| 50 | QVariant |
agsantos | e167208 | 2020-08-10 12:49:56 -0400 | [diff] [blame] | 51 | PreferenceItemListModel::data(const QModelIndex& index, int role) const |
Sébastien Blin | 1f91576 | 2020-08-03 13:27:42 -0400 | [diff] [blame] | 52 | { |
| 53 | auto preferenceList = LRCInstance::pluginModel().getPluginPreferences(pluginId_); |
| 54 | if (!index.isValid() || preferenceList.size() <= index.row()) { |
| 55 | return QVariant(); |
agsantos | e167208 | 2020-08-10 12:49:56 -0400 | [diff] [blame] | 56 | } |
Sébastien Blin | 1f91576 | 2020-08-03 13:27:42 -0400 | [diff] [blame] | 57 | |
| 58 | auto details = preferenceList.at(index.row()); |
| 59 | int type = Type::DEFAULT; |
| 60 | auto it = mapType.find(details["type"]); |
agsantos | e167208 | 2020-08-10 12:49:56 -0400 | [diff] [blame] | 61 | if (it != mapType.end()) { |
Sébastien Blin | 1f91576 | 2020-08-03 13:27:42 -0400 | [diff] [blame] | 62 | type = mapType[details["type"]]; |
| 63 | } |
agsantos | 78726ec | 2020-08-18 17:41:05 -0400 | [diff] [blame] | 64 | QString preferenceCurrent = LRCInstance::pluginModel().getPluginPreferencesValues( |
| 65 | pluginId_)[details["key"]]; |
Sébastien Blin | 1f91576 | 2020-08-03 13:27:42 -0400 | [diff] [blame] | 66 | |
| 67 | switch (role) { |
agsantos | e167208 | 2020-08-10 12:49:56 -0400 | [diff] [blame] | 68 | 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); |
agsantos | 78726ec | 2020-08-18 17:41:05 -0400 | [diff] [blame] | 76 | case Role::PluginId: |
| 77 | return QVariant(pluginId_); |
| 78 | case Role::PreferenceCurrentValue: |
| 79 | return QVariant(preferenceCurrent); |
Sébastien Blin | 1f91576 | 2020-08-03 13:27:42 -0400 | [diff] [blame] | 80 | } |
| 81 | return QVariant(); |
| 82 | } |
| 83 | |
| 84 | QHash<int, QByteArray> |
| 85 | PreferenceItemListModel::roleNames() const |
| 86 | { |
| 87 | QHash<int, QByteArray> roles; |
| 88 | roles[PreferenceKey] = "PreferenceKey"; |
| 89 | roles[PreferenceName] = "PreferenceName"; |
| 90 | roles[PreferenceSummary] = "PreferenceSummary"; |
| 91 | roles[PreferenceType] = "PreferenceType"; |
agsantos | 78726ec | 2020-08-18 17:41:05 -0400 | [diff] [blame] | 92 | roles[PluginId] = "PluginId"; |
| 93 | roles[PreferenceCurrentValue] = "PreferenceCurrentValue"; |
Sébastien Blin | 1f91576 | 2020-08-03 13:27:42 -0400 | [diff] [blame] | 94 | return roles; |
| 95 | } |
| 96 | |
| 97 | QModelIndex |
agsantos | e167208 | 2020-08-10 12:49:56 -0400 | [diff] [blame] | 98 | PreferenceItemListModel::index(int row, int column, const QModelIndex& parent) const |
Sébastien Blin | 1f91576 | 2020-08-03 13:27:42 -0400 | [diff] [blame] | 99 | { |
| 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 | |
| 111 | QModelIndex |
agsantos | e167208 | 2020-08-10 12:49:56 -0400 | [diff] [blame] | 112 | PreferenceItemListModel::parent(const QModelIndex& child) const |
Sébastien Blin | 1f91576 | 2020-08-03 13:27:42 -0400 | [diff] [blame] | 113 | { |
| 114 | Q_UNUSED(child); |
| 115 | return QModelIndex(); |
| 116 | } |
| 117 | |
| 118 | Qt::ItemFlags |
agsantos | e167208 | 2020-08-10 12:49:56 -0400 | [diff] [blame] | 119 | PreferenceItemListModel::flags(const QModelIndex& index) const |
Sébastien Blin | 1f91576 | 2020-08-03 13:27:42 -0400 | [diff] [blame] | 120 | { |
| 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 | |
| 128 | void |
| 129 | PreferenceItemListModel::reset() |
| 130 | { |
| 131 | beginResetModel(); |
| 132 | endResetModel(); |
| 133 | } |
| 134 | |
| 135 | QString |
| 136 | PreferenceItemListModel::pluginId() const |
| 137 | { |
| 138 | return pluginId_; |
| 139 | } |
| 140 | |
| 141 | void |
agsantos | e167208 | 2020-08-10 12:49:56 -0400 | [diff] [blame] | 142 | PreferenceItemListModel::setPluginId(const QString& pluginId) |
Sébastien Blin | 1f91576 | 2020-08-03 13:27:42 -0400 | [diff] [blame] | 143 | { |
| 144 | pluginId_ = pluginId; |
| 145 | } |
agsantos | e167208 | 2020-08-10 12:49:56 -0400 | [diff] [blame] | 146 | |
| 147 | int |
| 148 | PreferenceItemListModel::preferencesCount() |
| 149 | { |
| 150 | return LRCInstance::pluginModel().getPluginPreferences(pluginId_).size(); |
| 151 | } |