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