fe5de53461aad61bf19a2f87ad4d656e53e73b71
[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 &desc, const quint32 &type, const quint32 &channel, const QString &cdesc, const quint32 &volume)
20         : m_cindex(cindex), 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::desc() const
30 {
31         return m_desc;
32 }
33
34 quint32 PaControl::type() const
35 {
36         return m_type;
37 }
38
39 quint32 PaControl::channel() const
40 {
41         return m_channel;
42 }
43
44 QString PaControl::cdesc() const
45 {
46         return m_cdesc;
47 }
48
49
50 quint32 PaControl::volume() const
51 {
52         return m_volume;
53 }
54
55 // FIXME: Not all of these should be editable roles
56 void PaControl::setCIndex(const QVariant &cindex)
57 {
58         m_cindex = cindex.toUInt();
59 }
60
61 void PaControl::setDesc(const QVariant &desc)
62 {
63         m_desc = desc.toString();
64 }
65
66 void PaControl::setType(const QVariant &type)
67 {
68         m_type = type.toUInt();
69 }
70
71 void PaControl::setChannel(const QVariant &channel)
72 {
73         m_channel = channel.toUInt();
74 }
75
76 void PaControl::setCDesc(const QVariant &cdesc)
77 {
78         m_cdesc = cdesc.toString();
79 }
80
81 void PaControl::setVolume(PaControlModel *pacm, const QVariant &volume)
82 {
83         if (volume != m_volume) {
84                 m_volume = volume.toUInt();
85                 if (pacm)
86                         emit pacm->volumeChanged(type(), cindex(), channel(), m_volume);
87         }
88 }
89
90 PaControlModel::PaControlModel(QObject *parent)
91         : QAbstractListModel(parent)
92 {
93 }
94
95 void PaControlModel::addControl(const PaControl &control)
96 {
97         beginInsertRows(QModelIndex(), rowCount(), rowCount());
98         m_controls << control;
99         endInsertRows();
100 }
101
102 void PaControlModel::addOneControl(int cindex, QString desc, int type, int channel, const char *cdesc, int volume)
103 {
104         addControl(PaControl(cindex, desc, type, channel, cdesc, volume));
105 }
106
107 void PaControlModel::changeExternalVolume(uint32_t type, uint32_t cindex, uint32_t channel, uint32_t volume)
108 {
109         QList<PaControl>::iterator i;
110         int row;
111
112         for (i = m_controls.begin(), row = 0; i < m_controls.end(); ++i, ++row) {
113                 if ((i->type() == type) &&
114                     (i->cindex() == cindex) &&
115                     (i->channel() == channel)) {
116                         break;
117                 }
118         }
119
120         i->setVolume(NULL, QVariant(volume));
121         QModelIndex qmindex = index(row);
122         QVector<int> roles;
123         roles.push_back(VolumeRole);
124         emit dataChanged(qmindex, qmindex, roles);
125 }
126
127 int PaControlModel::rowCount(const QModelIndex & parent) const {
128         Q_UNUSED(parent);
129         return m_controls.count();
130 }
131
132 bool PaControlModel::setData(const QModelIndex &index, const QVariant &value, int role) {
133         if (index.row() < 0 || index.row() >= m_controls.count())
134                 return false;
135         PaControl &control = m_controls[index.row()];
136         if (role == CIndexRole)
137                 control.setCIndex(value);
138         else if (role == DescRole)
139                 control.setDesc(value);
140         else if (role == TypeRole)
141                 control.setType(value);
142         else if (role == ChannelRole)
143                 control.setChannel(value);
144         else if (role == CDescRole)
145                 control.setCDesc(value);
146         else if (role == VolumeRole)
147                 control.setVolume(this, value);
148         QVector<int> roles;
149         roles.push_back(role);
150         emit dataChanged(index, index, roles);
151         return true;
152 }
153
154 QVariant PaControlModel::data(const QModelIndex & index, int role) const {
155         if (index.row() < 0 || index.row() >= m_controls.count())
156                 return QVariant();
157
158         const PaControl &control = m_controls[index.row()];
159         if (role == CIndexRole)
160                 return control.cindex();
161         else if (role == DescRole)
162                 return control.desc();
163         else if (role == TypeRole)
164                 return control.type();
165         else if (role == ChannelRole)
166                 return control.channel();
167         else if (role == CDescRole)
168                 return control.cdesc();
169         else if (role == VolumeRole)
170                 return control.volume();
171         return QVariant();
172 }
173
174 Qt::ItemFlags PaControlModel::flags(const QModelIndex &index) const
175 {
176         if (!index.isValid())
177                 return Qt::ItemIsEnabled;
178
179         return QAbstractListModel::flags(index) | Qt::ItemIsEditable;
180 }
181
182 QHash<int, QByteArray> PaControlModel::roleNames() const {
183         QHash<int, QByteArray> roles;
184         roles[CIndexRole] = "cindex";
185         roles[DescRole] = "desc";
186         roles[TypeRole] = "type";
187         roles[ChannelRole] = "channel";
188         roles[CDescRole] = "cdesc";
189         roles[VolumeRole] = "volume";
190         return roles;
191 }