mixer: add clearer pulseaudio control naming
[apps/mixer.git] / app / pacontrolmodel.cpp
1 /*
2  * Copyright (C) 2016,2017 Konsulko Group
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "pacontrolmodel.h"
18
19 PaControl::PaControl(const quint32 &cindex, const QString &name, const QString &desc, const quint32 &type, const quint32 &channel, const QString &cdesc, const quint32 &volume)
20         : m_cindex(cindex), m_name(name), m_desc(desc), m_type(type), m_channel(channel), m_cdesc(cdesc), m_volume(volume)
21 {
22 }
23
24 quint32 PaControl::cindex() const
25 {
26         return m_cindex;
27 }
28
29 QString PaControl::name() const
30 {
31         QStringList list = m_name.split(".");
32
33         return list.at(1);
34 }
35
36 QString PaControl::desc() const
37 {
38         return m_desc;
39 }
40
41 quint32 PaControl::type() const
42 {
43         return m_type;
44 }
45
46 quint32 PaControl::channel() const
47 {
48         return m_channel;
49 }
50
51 QString PaControl::cdesc() const
52 {
53         return m_cdesc;
54 }
55
56
57 quint32 PaControl::volume() const
58 {
59         return m_volume;
60 }
61
62 // FIXME: Not all of these should be editable roles
63 void PaControl::setCIndex(const QVariant &cindex)
64 {
65         m_cindex = cindex.toUInt();
66 }
67
68 void PaControl::setName(const QVariant &name)
69 {
70         m_name = name.toString();
71 }
72
73 void PaControl::setDesc(const QVariant &desc)
74 {
75         m_desc = desc.toString();
76 }
77
78 void PaControl::setType(const QVariant &type)
79 {
80         m_type = type.toUInt();
81 }
82
83 void PaControl::setChannel(const QVariant &channel)
84 {
85         m_channel = channel.toUInt();
86 }
87
88 void PaControl::setCDesc(const QVariant &cdesc)
89 {
90         m_cdesc = cdesc.toString();
91 }
92
93 void PaControl::setVolume(PaControlModel *pacm, const QVariant &volume)
94 {
95         if (volume != m_volume) {
96                 m_volume = volume.toUInt();
97                 if (pacm)
98                         emit pacm->volumeChanged(type(), cindex(), channel(), m_volume);
99         }
100 }
101
102 PaControlModel::PaControlModel(QObject *parent)
103         : QAbstractListModel(parent)
104 {
105 }
106
107 void PaControlModel::addControl(const PaControl &control)
108 {
109         beginInsertRows(QModelIndex(), rowCount(), rowCount());
110         m_controls << control;
111         endInsertRows();
112 }
113
114 void PaControlModel::addOneControl(int cindex, QString name, QString desc, int type, int channel, const char *cdesc, int volume)
115 {
116         addControl(PaControl(cindex, name, desc, type, channel, cdesc, volume));
117 }
118
119 void PaControlModel::changeExternalVolume(uint32_t type, uint32_t cindex, uint32_t channel, uint32_t volume)
120 {
121         QList<PaControl>::iterator i;
122         int row;
123
124         for (i = m_controls.begin(), row = 0; i < m_controls.end(); ++i, ++row) {
125                 if ((i->type() == type) &&
126                     (i->cindex() == cindex) &&
127                     (i->channel() == channel)) {
128                         break;
129                 }
130         }
131
132         i->setVolume(NULL, QVariant(volume));
133         QModelIndex qmindex = index(row);
134         QVector<int> roles;
135         roles.push_back(VolumeRole);
136         emit dataChanged(qmindex, qmindex, roles);
137 }
138
139 int PaControlModel::rowCount(const QModelIndex & parent) const {
140         Q_UNUSED(parent);
141         return m_controls.count();
142 }
143
144 bool PaControlModel::setData(const QModelIndex &index, const QVariant &value, int role) {
145         if (index.row() < 0 || index.row() >= m_controls.count())
146                 return false;
147         PaControl &control = m_controls[index.row()];
148         if (role == CIndexRole)
149                 control.setCIndex(value);
150         else if (role == NameRole)
151                 control.setName(value);
152         else if (role == DescRole)
153                 control.setDesc(value);
154         else if (role == TypeRole)
155                 control.setType(value);
156         else if (role == ChannelRole)
157                 control.setChannel(value);
158         else if (role == CDescRole)
159                 control.setCDesc(value);
160         else if (role == VolumeRole)
161                 control.setVolume(this, value);
162         QVector<int> roles;
163         roles.push_back(role);
164         emit dataChanged(index, index, roles);
165         return true;
166 }
167
168 QVariant PaControlModel::data(const QModelIndex & index, int role) const {
169         if (index.row() < 0 || index.row() >= m_controls.count())
170                 return QVariant();
171
172         const PaControl &control = m_controls[index.row()];
173         if (role == CIndexRole)
174                 return control.cindex();
175         else if (role == NameRole)
176                 return control.name();
177         else if (role == DescRole)
178                 return control.desc();
179         else if (role == TypeRole)
180                 return control.type();
181         else if (role == ChannelRole)
182                 return control.channel();
183         else if (role == CDescRole)
184                 return control.cdesc();
185         else if (role == VolumeRole)
186                 return control.volume();
187         return QVariant();
188 }
189
190 Qt::ItemFlags PaControlModel::flags(const QModelIndex &index) const
191 {
192         if (!index.isValid())
193                 return Qt::ItemIsEnabled;
194
195         return QAbstractListModel::flags(index) | Qt::ItemIsEditable;
196 }
197
198 QHash<int, QByteArray> PaControlModel::roleNames() const {
199         QHash<int, QByteArray> roles;
200         roles[CIndexRole] = "cindex";
201         roles[NameRole] = "name";
202         roles[DescRole] = "desc";
203         roles[TypeRole] = "type";
204         roles[ChannelRole] = "channel";
205         roles[CDescRole] = "cdesc";
206         roles[VolumeRole] = "volume";
207         return roles;
208 }