blob: dcc072f87073c7dbb61c81233539a6808a0b09b0 [file] [log] [blame]
Sébastien Blin1f915762020-08-03 13:27:42 -04001/*
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 "pluginitemlistmodel.h"
20
21PluginItemListModel::PluginItemListModel(QObject *parent)
22 : QAbstractListModel(parent)
23{}
24
25PluginItemListModel::~PluginItemListModel() {}
26
27int
28PluginItemListModel::rowCount(const QModelIndex &parent) const
29{
30 if (!parent.isValid()) {
31 /*
32 * Count.
33 */
34 return LRCInstance::pluginModel().listAvailablePlugins().size();
35 }
36 /*
37 * A valid QModelIndex returns 0 as no entry has sub-elements.
38 */
39 return 0;
40}
41
42int
43PluginItemListModel::columnCount(const QModelIndex &parent) const
44{
45 Q_UNUSED(parent);
46 /*
47 * Only need one column.
48 */
49 return 1;
50}
51
52QVariant
53PluginItemListModel::data(const QModelIndex &index, int role) const
54{
55 auto pluginList = LRCInstance::pluginModel().listAvailablePlugins();
56 if (!index.isValid() || pluginList.size() <= index.row()) {
57 return QVariant();
58 }
59
60 auto details = LRCInstance::pluginModel().getPluginDetails(pluginList.at(index.row()));
61
62 switch (role) {
63 case Role::PluginName:
64 return QVariant(details.name);
65 case Role::PluginId:
66 return QVariant(pluginList.at(index.row()));
67 case Role::PluginIcon:
68 return QVariant(details.iconPath);
69 case Role::IsLoaded:
70 return QVariant(details.loaded);
71 }
72 return QVariant();
73}
74
75QHash<int, QByteArray>
76PluginItemListModel::roleNames() const
77{
78 QHash<int, QByteArray> roles;
79 roles[PluginName] = "PluginName";
80 roles[PluginId] = "PluginId";
81 roles[PluginIcon] = "PluginIcon";
82 roles[IsLoaded] = "IsLoaded";
83
84 return roles;
85}
86
87QModelIndex
88PluginItemListModel::index(int row, int column, const QModelIndex &parent) const
89{
90 Q_UNUSED(parent);
91 if (column != 0) {
92 return QModelIndex();
93 }
94
95 if (row >= 0 && row < rowCount()) {
96 return createIndex(row, column);
97 }
98 return QModelIndex();
99}
100
101QModelIndex
102PluginItemListModel::parent(const QModelIndex &child) const
103{
104 Q_UNUSED(child);
105 return QModelIndex();
106}
107
108Qt::ItemFlags
109PluginItemListModel::flags(const QModelIndex &index) const
110{
111 auto flags = QAbstractItemModel::flags(index) | Qt::ItemNeverHasChildren | Qt::ItemIsSelectable;
112 if (!index.isValid()) {
113 return QAbstractItemModel::flags(index);
114 }
115 return flags;
116}
117
118void
119PluginItemListModel::reset()
120{
121 beginResetModel();
122 endResetModel();
123}