blob: 8e76a276f36fc08b17764d54d48635aa9a628ea1 [file] [log] [blame]
agsantos78726ec2020-08-18 17:41:05 -04001/**
2 * Copyright (C) 2019-2020 by Savoir-faire Linux
3 * Author: Aline Gondim Santos <aline.gondimsantos@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 "pluginlistpreferencemodel.h"
20#include <regex>
21
22PluginListPreferenceModel::PluginListPreferenceModel(QObject* parent)
23 : QAbstractListModel(parent)
24{}
25
26PluginListPreferenceModel::~PluginListPreferenceModel() {}
27
28void
29PluginListPreferenceModel::populateLists()
30{
31 preferenceValuesList_.clear();
32 preferenceList_.clear();
33 const auto preferences = LRCInstance::pluginModel().getPluginPreferences(pluginId_);
34 for (const auto& preference : preferences) {
35 if (preference["key"] == preferenceKey_) {
36 preferenceList_ = preference["entries"].split(",");
37 preferenceValuesList_ = preference["entryValues"].split(",");
38 break;
39 }
40 }
41 getCurrentSettingIndex();
42}
43
44int
45PluginListPreferenceModel::rowCount(const QModelIndex& parent) const
46{
47 if (!parent.isValid()) {
48 /// Count
49 return preferenceList_.size();
50 }
51 /// A valid QModelIndex returns 0 as no entry has sub-elements.
52 return 0;
53}
54
55int
56PluginListPreferenceModel::columnCount(const QModelIndex& parent) const
57{
58 Q_UNUSED(parent);
59 /// Only need one column.
60 return 1;
61}
62
63QVariant
64PluginListPreferenceModel::data(const QModelIndex& index, int role) const
65{
66 if (!index.isValid() || preferenceList_.size() <= index.row()) {
67 return QVariant();
68 }
69
70 switch (role) {
71 case Role::PreferenceValue:
72 return QVariant(preferenceList_.at(index.row()));
73 case Role::PreferenceEntryValue:
74 return QVariant(preferenceValuesList_.at(index.row()));
75 }
76 return QVariant();
77}
78
79QHash<int, QByteArray>
80PluginListPreferenceModel::roleNames() const
81{
82 QHash<int, QByteArray> roles;
83 roles[PreferenceValue] = "PreferenceValue";
84 roles[PreferenceEntryValue] = "PreferenceEntryValue";
85 return roles;
86}
87
88QModelIndex
89PluginListPreferenceModel::index(int row, int column, const QModelIndex& parent) const
90{
91 Q_UNUSED(parent);
92 if (column != 0) {
93 return QModelIndex();
94 }
95
96 if (row >= 0 && row < rowCount()) {
97 return createIndex(row, column);
98 }
99 return QModelIndex();
100}
101
102QModelIndex
103PluginListPreferenceModel::parent(const QModelIndex& child) const
104{
105 Q_UNUSED(child);
106 return QModelIndex();
107}
108
109Qt::ItemFlags
110PluginListPreferenceModel::flags(const QModelIndex& index) const
111{
112 auto flags = QAbstractItemModel::flags(index) | Qt::ItemNeverHasChildren | Qt::ItemIsSelectable;
113 if (!index.isValid()) {
114 return QAbstractItemModel::flags(index);
115 }
116 return flags;
117}
118
119void
120PluginListPreferenceModel::reset()
121{
122 beginResetModel();
123 endResetModel();
124}
125
126int
127PluginListPreferenceModel::getCurrentSettingIndex()
128{
129 auto resultList = match(index(0, 0), PreferenceEntryValue, preferenceCurrentValue());
130
131 int resultRowIndex = 0;
132 if (resultList.size() > 0) {
133 resultRowIndex = resultList[0].row();
134 }
135
136 return resultRowIndex;
137}