2 * Copyright (C) 2016,2017 Konsulko Group
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "pacontrolmodel.h"
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)
24 quint32 PaControl::cindex() const
29 QString PaControl::name() const
31 QStringList list = m_name.split(".");
36 QString PaControl::desc() const
41 quint32 PaControl::type() const
46 quint32 PaControl::channel() const
51 QString PaControl::cdesc() const
57 quint32 PaControl::volume() const
62 // FIXME: Not all of these should be editable roles
63 void PaControl::setCIndex(const QVariant &cindex)
65 m_cindex = cindex.toUInt();
68 void PaControl::setName(const QVariant &name)
70 m_name = name.toString();
73 void PaControl::setDesc(const QVariant &desc)
75 m_desc = desc.toString();
78 void PaControl::setType(const QVariant &type)
80 m_type = type.toUInt();
83 void PaControl::setChannel(const QVariant &channel)
85 m_channel = channel.toUInt();
88 void PaControl::setCDesc(const QVariant &cdesc)
90 m_cdesc = cdesc.toString();
93 void PaControl::setVolume(PaControlModel *pacm, const QVariant &volume)
95 if (volume != m_volume) {
96 m_volume = volume.toUInt();
98 emit pacm->volumeChanged(type(), cindex(), channel(), m_volume);
102 PaControlModel::PaControlModel(QObject *parent)
103 : QAbstractListModel(parent)
107 void PaControlModel::addControl(const PaControl &control)
109 beginInsertRows(QModelIndex(), rowCount(), rowCount());
110 m_controls << control;
114 void PaControlModel::addOneControl(int cindex, QString name, QString desc, int type, int channel, const char *cdesc, int volume)
116 addControl(PaControl(cindex, name, desc, type, channel, cdesc, volume));
119 void PaControlModel::changeExternalVolume(uint32_t type, uint32_t cindex, uint32_t channel, uint32_t volume)
121 QList<PaControl>::iterator i;
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)) {
132 i->setVolume(NULL, QVariant(volume));
133 QModelIndex qmindex = index(row);
135 roles.push_back(VolumeRole);
136 emit dataChanged(qmindex, qmindex, roles);
139 int PaControlModel::rowCount(const QModelIndex & parent) const {
141 return m_controls.count();
144 bool PaControlModel::setData(const QModelIndex &index, const QVariant &value, int role) {
145 if (index.row() < 0 || index.row() >= m_controls.count())
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);
163 roles.push_back(role);
164 emit dataChanged(index, index, roles);
168 QVariant PaControlModel::data(const QModelIndex & index, int role) const {
169 if (index.row() < 0 || index.row() >= m_controls.count())
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();
190 Qt::ItemFlags PaControlModel::flags(const QModelIndex &index) const
192 if (!index.isValid())
193 return Qt::ItemIsEnabled;
195 return QAbstractListModel::flags(index) | Qt::ItemIsEditable;
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";