blob: 733cc6d751b913d0d16a154867b277ca6b4707e5 [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 "audiooutputdevicemodel.h"
20
21AudioOutputDeviceModel::AudioOutputDeviceModel(QObject *parent)
22 : QAbstractListModel(parent)
23{}
24
25AudioOutputDeviceModel::~AudioOutputDeviceModel() {}
26
27int
28AudioOutputDeviceModel::rowCount(const QModelIndex &parent) const
29{
30 if (!parent.isValid()) {
31 /*
32 * Count.
33 */
34 return LRCInstance::avModel().getAudioOutputDevices().size();
35 }
36 /*
37 * A valid QModelIndex returns 0 as no entry has sub-elements.
38 */
39 return 0;
40}
41
42int
43AudioOutputDeviceModel::columnCount(const QModelIndex &parent) const
44{
45 Q_UNUSED(parent);
46 /*
47 * Only need one column.
48 */
49 return 1;
50}
51
52QVariant
53AudioOutputDeviceModel::data(const QModelIndex &index, int role) const
54{
55 auto deviceList = LRCInstance::avModel().getAudioOutputDevices();
56 if (!index.isValid() || deviceList.size() <= index.row()) {
57 return QVariant();
58 }
59
60 switch (role) {
61 case Role::Device_ID:
62 return QVariant(deviceList.at(index.row()));
63 case Role::ID_UTF8:
64 return QVariant(deviceList.at(index.row()).toUtf8());
65 }
66 return QVariant();
67}
68
69QHash<int, QByteArray>
70AudioOutputDeviceModel::roleNames() const
71{
72 QHash<int, QByteArray> roles;
73 roles[Device_ID] = "Device_ID";
74 roles[ID_UTF8] = "ID_UTF8";
75 return roles;
76}
77
78QModelIndex
79AudioOutputDeviceModel::index(int row, int column, const QModelIndex &parent) const
80{
81 Q_UNUSED(parent);
82 if (column != 0) {
83 return QModelIndex();
84 }
85
86 if (row >= 0 && row < rowCount()) {
87 return createIndex(row, column);
88 }
89 return QModelIndex();
90}
91
92QModelIndex
93AudioOutputDeviceModel::parent(const QModelIndex &child) const
94{
95 Q_UNUSED(child);
96 return QModelIndex();
97}
98
99Qt::ItemFlags
100AudioOutputDeviceModel::flags(const QModelIndex &index) const
101{
102 auto flags = QAbstractItemModel::flags(index) | Qt::ItemNeverHasChildren | Qt::ItemIsSelectable;
103 if (!index.isValid()) {
104 return QAbstractItemModel::flags(index);
105 }
106 return flags;
107}
108
109void
110AudioOutputDeviceModel::reset()
111{
112 beginResetModel();
113 endResetModel();
114}
115
116int
117AudioOutputDeviceModel::getCurrentSettingIndex()
118{
119 QString currentId = LRCInstance::avModel().getOutputDevice();
120 auto resultList = match(index(0, 0), Device_ID, QVariant(currentId));
121
122 int resultRowIndex = 0;
123 if (resultList.size() > 0) {
124 resultRowIndex = resultList[0].row();
125 }
126
127 return resultRowIndex;
128}
129
130int
131AudioOutputDeviceModel::getCurrentRingtoneDeviceIndex()
132{
133 QString currentId = LRCInstance::avModel().getRingtoneDevice();
134 auto resultList = match(index(0, 0), Device_ID, QVariant(currentId));
135
136 int resultRowIndex = 0;
137 if (resultList.size() > 0) {
138 resultRowIndex = resultList[0].row();
139 }
140
141 return resultRowIndex;
142}