blob: cf097ad48cc0193fa853fd33759084acfac95fe2 [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 "audiocodeclistmodel.h"
20
21AudioCodecListModel::AudioCodecListModel(QObject *parent)
22 : QAbstractListModel(parent)
23{}
24
25AudioCodecListModel::~AudioCodecListModel() {}
26
27int
28AudioCodecListModel::rowCount(const QModelIndex &parent) const
29{
30 if (!parent.isValid()) {
31 return LRCInstance::getCurrentAccountInfo().codecModel->getAudioCodecs().size();
32 }
33 return 0;
34}
35
36int
37AudioCodecListModel::columnCount(const QModelIndex &parent) const
38{
39 Q_UNUSED(parent);
40 /*
41 * Only need one column.
42 */
43 return 1;
44}
45
46QVariant
47AudioCodecListModel::data(const QModelIndex &index, int role) const
48{
49 auto audioCodecList = LRCInstance::getCurrentAccountInfo().codecModel->getAudioCodecs();
50 if (!index.isValid() || audioCodecList.size() <= index.row()) {
51 return QVariant();
52 }
53
54 switch (role) {
55 case Role::AudioCodecName:
56 return QVariant(audioCodecList.at(index.row()).name);
57 case Role::IsEnabled:
58 return QVariant(audioCodecList.at(index.row()).enabled);
59 case Role::AudioCodecID:
60 return QVariant(audioCodecList.at(index.row()).id);
61 case Role::Samplerate:
62 return QVariant(audioCodecList.at(index.row()).samplerate);
63 }
64 return QVariant();
65}
66
67QHash<int, QByteArray>
68AudioCodecListModel::roleNames() const
69{
70 QHash<int, QByteArray> roles;
71 roles[AudioCodecName] = "AudioCodecName";
72 roles[IsEnabled] = "IsEnabled";
73 roles[AudioCodecID] = "AudioCodecID";
74 roles[Samplerate] = "Samplerate";
75 return roles;
76}
77
78QModelIndex
79AudioCodecListModel::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
93AudioCodecListModel::parent(const QModelIndex &child) const
94{
95 Q_UNUSED(child);
96 return QModelIndex();
97}
98
99Qt::ItemFlags
100AudioCodecListModel::flags(const QModelIndex &index) const
101{
102 auto flags = QAbstractItemModel::flags(index) | Qt::ItemNeverHasChildren | Qt::ItemIsSelectable
103 | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled;
104 if (!index.isValid()) {
105 return QAbstractItemModel::flags(index);
106 }
107 return flags;
108}