agsantos | 78726ec | 2020-08-18 17:41:05 -0400 | [diff] [blame] | 1 | /** |
| 2 | * Copyright (C) 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 | #pragma once |
| 20 | |
| 21 | #include <QAbstractItemModel> |
| 22 | |
| 23 | #include "api/pluginmodel.h" |
| 24 | |
| 25 | #include "lrcinstance.h" |
| 26 | |
| 27 | class PluginListPreferenceModel : public QAbstractListModel |
| 28 | { |
| 29 | Q_OBJECT |
| 30 | Q_PROPERTY(QString pluginId READ pluginId WRITE setPluginId) |
| 31 | Q_PROPERTY(QString preferenceKey READ preferenceKey WRITE setPreferenceKey) |
| 32 | Q_PROPERTY(QString preferenceNewValue READ preferenceNewValue WRITE setPreferenceNewValue) |
| 33 | Q_PROPERTY(int idx READ idx WRITE setIdx) |
| 34 | Q_PROPERTY(int optSize READ optSize) |
| 35 | public: |
| 36 | enum Role { PreferenceValue = Qt::UserRole + 1, PreferenceEntryValue }; |
| 37 | Q_ENUM(Role) |
| 38 | |
| 39 | explicit PluginListPreferenceModel(QObject *parent = 0); |
| 40 | ~PluginListPreferenceModel(); |
| 41 | |
| 42 | /* |
| 43 | * QAbstractListModel override. |
| 44 | */ |
| 45 | int rowCount(const QModelIndex &parent = QModelIndex()) const override; |
| 46 | int columnCount(const QModelIndex &parent) const override; |
| 47 | QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; |
| 48 | /* |
| 49 | * Override role name as access point in qml. |
| 50 | */ |
| 51 | QHash<int, QByteArray> roleNames() const override; |
| 52 | QModelIndex index(int row, int column = 0, const QModelIndex &parent = QModelIndex()) const; |
| 53 | QModelIndex parent(const QModelIndex &child) const; |
| 54 | Qt::ItemFlags flags(const QModelIndex &index) const; |
| 55 | |
| 56 | /* |
| 57 | * This function is to reset the model when there's new account added. |
| 58 | */ |
| 59 | Q_INVOKABLE void reset(); |
| 60 | /* |
| 61 | * This function is to get the current preference value |
| 62 | */ |
| 63 | Q_INVOKABLE int getCurrentSettingIndex(); |
| 64 | |
| 65 | Q_INVOKABLE void populateLists(); |
| 66 | |
| 67 | void setPreferenceNewValue(const QString preferenceNewValue) { preferenceNewValue_ = preferenceNewValue; } |
| 68 | void setPreferenceKey(const QString preferenceKey) { preferenceKey_ = preferenceKey; } |
| 69 | void setPluginId(const QString pluginId) |
| 70 | { |
| 71 | pluginId_ = pluginId; |
| 72 | populateLists(); |
| 73 | } |
| 74 | |
| 75 | void setIdx(const int index) { idx_ = index; } |
| 76 | |
| 77 | int idx() { return idx_; } |
| 78 | QString preferenceCurrentValue() { |
| 79 | return LRCInstance::pluginModel().getPluginPreferencesValues(pluginId_)[preferenceKey_]; |
| 80 | } |
| 81 | |
| 82 | QString preferenceNewValue() |
| 83 | { |
| 84 | preferenceNewValue_ = preferenceValuesList_[idx_]; |
| 85 | return preferenceNewValue_; |
| 86 | } |
| 87 | QString preferenceKey() { return preferenceKey_; } |
| 88 | QString pluginId() { return pluginId_; } |
| 89 | int optSize() { return preferenceValuesList_.size(); } |
| 90 | |
| 91 | private: |
| 92 | |
| 93 | QString pluginId_ = ""; |
| 94 | QString preferenceKey_ = ""; |
| 95 | QString preferenceNewValue_ = ""; |
| 96 | QStringList preferenceValuesList_; |
| 97 | QStringList preferenceList_; |
| 98 | int idx_ = 0; |
| 99 | }; |