Sébastien Blin | 1f91576 | 2020-08-03 13:27:42 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019-2020 by Savoir-faire Linux |
| 3 | * Author: Yang Wang <yang.wang@savoirfairelinux.com> |
| 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 | |
| 22 | std::map<QString, int> mapType {{QString("List"), PreferenceItemListModel::Type::LIST}}; |
| 23 | |
| 24 | PreferenceItemListModel::PreferenceItemListModel(QObject *parent) |
| 25 | : QAbstractListModel(parent) |
| 26 | {} |
| 27 | |
| 28 | PreferenceItemListModel::~PreferenceItemListModel() {} |
| 29 | |
| 30 | int |
| 31 | PreferenceItemListModel::rowCount(const QModelIndex &parent) const |
| 32 | { |
| 33 | if (!parent.isValid()) { |
| 34 | /* |
| 35 | * Count. |
| 36 | */ |
| 37 | return LRCInstance::pluginModel().getPluginPreferences(pluginId_).size(); |
| 38 | } |
| 39 | /* |
| 40 | * A valid QModelIndex returns 0 as no entry has sub-elements. |
| 41 | */ |
| 42 | return 0; |
| 43 | } |
| 44 | |
| 45 | int |
| 46 | PreferenceItemListModel::columnCount(const QModelIndex &parent) const |
| 47 | { |
| 48 | Q_UNUSED(parent); |
| 49 | /* |
| 50 | * Only need one column. |
| 51 | */ |
| 52 | return 1; |
| 53 | } |
| 54 | |
| 55 | QVariant |
| 56 | PreferenceItemListModel::data(const QModelIndex &index, int role) const |
| 57 | { |
| 58 | auto preferenceList = LRCInstance::pluginModel().getPluginPreferences(pluginId_); |
| 59 | if (!index.isValid() || preferenceList.size() <= index.row()) { |
| 60 | return QVariant(); |
| 61 | } |
| 62 | |
| 63 | auto details = preferenceList.at(index.row()); |
| 64 | int type = Type::DEFAULT; |
| 65 | auto it = mapType.find(details["type"]); |
| 66 | if (it != mapType.end()) |
| 67 | { |
| 68 | type = mapType[details["type"]]; |
| 69 | } |
| 70 | |
| 71 | switch (role) { |
| 72 | case Role::PreferenceKey: |
| 73 | return QVariant(details["key"]); |
| 74 | case Role::PreferenceName: |
| 75 | return QVariant(details["title"]); |
| 76 | case Role::PreferenceSummary: |
| 77 | return QVariant(details["summary"]); |
| 78 | case Role::PreferenceType: |
| 79 | return QVariant(type); |
| 80 | case Role::PreferenceDefaultValue: |
| 81 | return QVariant(details["defaultValue"]); |
| 82 | case Role::PreferenceEntries: |
| 83 | return QVariant(details["entries"]); |
| 84 | case Role::PreferenceEntryValues: |
| 85 | return QVariant(details["entryValues"]); |
| 86 | } |
| 87 | return QVariant(); |
| 88 | } |
| 89 | |
| 90 | QHash<int, QByteArray> |
| 91 | PreferenceItemListModel::roleNames() const |
| 92 | { |
| 93 | QHash<int, QByteArray> roles; |
| 94 | roles[PreferenceKey] = "PreferenceKey"; |
| 95 | roles[PreferenceName] = "PreferenceName"; |
| 96 | roles[PreferenceSummary] = "PreferenceSummary"; |
| 97 | roles[PreferenceType] = "PreferenceType"; |
| 98 | roles[PreferenceDefaultValue] = "PreferenceDefaultValue"; |
| 99 | roles[PreferenceEntries] = "PreferenceEntries"; |
| 100 | roles[PreferenceEntryValues] = "PreferenceEntryValues"; |
| 101 | |
| 102 | return roles; |
| 103 | } |
| 104 | |
| 105 | QModelIndex |
| 106 | PreferenceItemListModel::index(int row, int column, const QModelIndex &parent) const |
| 107 | { |
| 108 | Q_UNUSED(parent); |
| 109 | if (column != 0) { |
| 110 | return QModelIndex(); |
| 111 | } |
| 112 | |
| 113 | if (row >= 0 && row < rowCount()) { |
| 114 | return createIndex(row, column); |
| 115 | } |
| 116 | return QModelIndex(); |
| 117 | } |
| 118 | |
| 119 | QModelIndex |
| 120 | PreferenceItemListModel::parent(const QModelIndex &child) const |
| 121 | { |
| 122 | Q_UNUSED(child); |
| 123 | return QModelIndex(); |
| 124 | } |
| 125 | |
| 126 | Qt::ItemFlags |
| 127 | PreferenceItemListModel::flags(const QModelIndex &index) const |
| 128 | { |
| 129 | auto flags = QAbstractItemModel::flags(index) | Qt::ItemNeverHasChildren | Qt::ItemIsSelectable; |
| 130 | if (!index.isValid()) { |
| 131 | return QAbstractItemModel::flags(index); |
| 132 | } |
| 133 | return flags; |
| 134 | } |
| 135 | |
| 136 | void |
| 137 | PreferenceItemListModel::reset() |
| 138 | { |
| 139 | beginResetModel(); |
| 140 | endResetModel(); |
| 141 | } |
| 142 | |
| 143 | QString |
| 144 | PreferenceItemListModel::pluginId() const |
| 145 | { |
| 146 | return pluginId_; |
| 147 | } |
| 148 | |
| 149 | void |
| 150 | PreferenceItemListModel::setPluginId(const QString &pluginId) |
| 151 | { |
| 152 | pluginId_ = pluginId; |
| 153 | } |