agsantos | dc25dfa | 2020-08-28 12:04:45 -0400 | [diff] [blame] | 1 | /** |
| 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 "mediahandlerlistpreferencemodel.h" |
| 20 | #include <regex> |
| 21 | |
| 22 | MediaHandlerListPreferenceModel::MediaHandlerListPreferenceModel(QObject* parent) |
| 23 | : QAbstractListModel(parent) |
| 24 | {} |
| 25 | |
| 26 | MediaHandlerListPreferenceModel::~MediaHandlerListPreferenceModel() {} |
| 27 | |
| 28 | void |
| 29 | MediaHandlerListPreferenceModel::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 | auto entries = preference["entries"]; |
| 37 | auto entriesValues = preference["entryValues"]; |
| 38 | std::string entry = entries.toStdString(); |
| 39 | std::string entryValues = entriesValues.toStdString(); |
| 40 | std::string delimiter = ","; |
| 41 | |
| 42 | size_t pos = 0; |
| 43 | std::string token; |
| 44 | while ((pos = entry.find(delimiter)) != std::string::npos) { |
| 45 | preferenceList_.emplace_back(entry.substr(0, pos)); |
| 46 | |
| 47 | entry.erase(0, pos + delimiter.length()); |
| 48 | } |
| 49 | preferenceList_.emplace_back(entry.substr(0, pos)); |
| 50 | while ((pos = entryValues.find(delimiter)) != std::string::npos) { |
| 51 | preferenceValuesList_.emplace_back(entryValues.substr(0, pos)); |
| 52 | |
| 53 | entryValues.erase(0, pos + delimiter.length()); |
| 54 | } |
| 55 | preferenceValuesList_.emplace_back(entryValues.substr(0, pos)); |
| 56 | } |
| 57 | } |
| 58 | getCurrentSettingIndex(); |
| 59 | } |
| 60 | |
| 61 | int |
| 62 | MediaHandlerListPreferenceModel::rowCount(const QModelIndex& parent) const |
| 63 | { |
| 64 | if (!parent.isValid()) { |
| 65 | /// Count |
| 66 | return preferenceList_.size(); |
| 67 | } |
| 68 | /// A valid QModelIndex returns 0 as no entry has sub-elements. |
| 69 | return 0; |
| 70 | } |
| 71 | |
| 72 | int |
| 73 | MediaHandlerListPreferenceModel::columnCount(const QModelIndex& parent) const |
| 74 | { |
| 75 | Q_UNUSED(parent); |
| 76 | /// Only need one column. |
| 77 | return 1; |
| 78 | } |
| 79 | |
| 80 | QVariant |
| 81 | MediaHandlerListPreferenceModel::data(const QModelIndex& index, int role) const |
| 82 | { |
| 83 | if (!index.isValid() || preferenceList_.size() <= index.row()) { |
| 84 | return QVariant(); |
| 85 | } |
| 86 | |
| 87 | switch (role) { |
| 88 | case Role::PreferenceValue: |
| 89 | return QVariant(QString::fromStdString(preferenceList_.at(index.row()))); |
| 90 | case Role::PreferenceEntryValue: |
| 91 | return QVariant(QString::fromStdString(preferenceValuesList_.at(index.row()))); |
| 92 | } |
| 93 | return QVariant(); |
| 94 | } |
| 95 | |
| 96 | QHash<int, QByteArray> |
| 97 | MediaHandlerListPreferenceModel::roleNames() const |
| 98 | { |
| 99 | QHash<int, QByteArray> roles; |
| 100 | roles[PreferenceValue] = "PreferenceValue"; |
| 101 | roles[PreferenceEntryValue] = "PreferenceEntryValue"; |
| 102 | return roles; |
| 103 | } |
| 104 | |
| 105 | QModelIndex |
| 106 | MediaHandlerListPreferenceModel::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 | MediaHandlerListPreferenceModel::parent(const QModelIndex& child) const |
| 121 | { |
| 122 | Q_UNUSED(child); |
| 123 | return QModelIndex(); |
| 124 | } |
| 125 | |
| 126 | Qt::ItemFlags |
| 127 | MediaHandlerListPreferenceModel::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 | MediaHandlerListPreferenceModel::reset() |
| 138 | { |
| 139 | beginResetModel(); |
| 140 | endResetModel(); |
| 141 | } |
| 142 | |
| 143 | int |
| 144 | MediaHandlerListPreferenceModel::getCurrentSettingIndex() |
| 145 | { |
| 146 | auto resultList = match(index(0, 0), PreferenceEntryValue, preferenceCurrentValue()); |
| 147 | |
| 148 | int resultRowIndex = 0; |
| 149 | if (resultList.size() > 0) { |
| 150 | resultRowIndex = resultList[0].row(); |
| 151 | } |
| 152 | |
| 153 | return resultRowIndex; |
| 154 | } |