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 &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)
24 quint32 PaControl::cindex() const
29 QString PaControl::desc() const
34 quint32 PaControl::type() const
39 quint32 PaControl::channel() const
44 QString PaControl::cdesc() const
50 quint32 PaControl::volume() const
55 // FIXME: Not all of these should be editable roles
56 void PaControl::setCIndex(const QVariant &cindex)
58 m_cindex = cindex.toUInt();
61 void PaControl::setDesc(const QVariant &desc)
63 m_desc = desc.toString();
66 void PaControl::setType(const QVariant &type)
68 m_type = type.toUInt();
71 void PaControl::setChannel(const QVariant &channel)
73 m_channel = channel.toUInt();
76 void PaControl::setCDesc(const QVariant &cdesc)
78 m_cdesc = cdesc.toString();
81 void PaControl::setVolume(PaControlModel *pacm, const QVariant &volume)
83 if (volume != m_volume) {
84 m_volume = volume.toUInt();
85 emit pacm->volumeChanged(type(), cindex(), channel(), m_volume);
89 PaControlModel::PaControlModel(QObject *parent)
90 : QAbstractListModel(parent)
94 void PaControlModel::addControl(const PaControl &control)
96 beginInsertRows(QModelIndex(), rowCount(), rowCount());
97 m_controls << control;
101 void PaControlModel::addOneControl(int cindex, QString desc, int type, int channel, const char *cdesc, int volume)
103 addControl(PaControl(cindex, desc, type, channel, cdesc, volume));
106 int PaControlModel::rowCount(const QModelIndex & parent) const {
108 return m_controls.count();
111 bool PaControlModel::setData(const QModelIndex &index, const QVariant &value, int role) {
112 if (index.row() < 0 || index.row() >= m_controls.count())
114 PaControl &control = m_controls[index.row()];
115 if (role == CIndexRole)
116 control.setCIndex(value);
117 else if (role == DescRole)
118 control.setDesc(value);
119 else if (role == TypeRole)
120 control.setType(value);
121 else if (role == ChannelRole)
122 control.setChannel(value);
123 else if (role == CDescRole)
124 control.setCDesc(value);
125 else if (role == VolumeRole)
126 control.setVolume(this, value);
127 emit dataChanged(index, index);
131 QVariant PaControlModel::data(const QModelIndex & index, int role) const {
132 if (index.row() < 0 || index.row() >= m_controls.count())
135 const PaControl &control = m_controls[index.row()];
136 if (role == CIndexRole)
137 return control.cindex();
138 else if (role == DescRole)
139 return control.desc();
140 else if (role == TypeRole)
141 return control.type();
142 else if (role == ChannelRole)
143 return control.channel();
144 else if (role == CDescRole)
145 return control.cdesc();
146 else if (role == VolumeRole)
147 return control.volume();
151 Qt::ItemFlags PaControlModel::flags(const QModelIndex &index) const
153 if (!index.isValid())
154 return Qt::ItemIsEnabled;
156 return QAbstractListModel::flags(index) | Qt::ItemIsEditable;
159 QHash<int, QByteArray> PaControlModel::roleNames() const {
160 QHash<int, QByteArray> roles;
161 roles[CIndexRole] = "cindex";
162 roles[DescRole] = "desc";
163 roles[TypeRole] = "type";
164 roles[ChannelRole] = "channel";
165 roles[CDescRole] = "cdesc";
166 roles[VolumeRole] = "volume";