Editable volume and mute state in RemoteSinksModel

This commit is contained in:
Richard Liebscher 2020-08-06 21:15:51 +00:00 committed by Nicolas Fella
parent 237975da35
commit 2d523f0b8a
2 changed files with 57 additions and 30 deletions

View file

@ -78,20 +78,24 @@ void RemoteSinksModel::setDeviceId(const QString& deviceId)
this, &RemoteSinksModel::refreshSinkList); this, &RemoteSinksModel::refreshSinkList);
connect(m_dbusInterface, &OrgKdeKdeconnectDeviceRemotesystemvolumeInterface::volumeChanged, this, [this](const QString& name, int volume) { connect(m_dbusInterface, &OrgKdeKdeconnectDeviceRemotesystemvolumeInterface::volumeChanged, this, [this](const QString& name, int volume) {
for (Sink* s: qAsConst(m_sinkList)) { auto iter = std::find_if(m_sinkList.begin(), m_sinkList.end(), [&name](const Sink& s) {
if (s->name == name) { return s.name == name;
s->volume = volume; });
Q_EMIT dataChanged(index(0,0), index(m_sinkList.size() - 1, 0)); if (iter != m_sinkList.end()) {
} iter->volume = volume;
int i = std::distance(m_sinkList.begin(), iter);
Q_EMIT dataChanged(index(i, 0), index(i, 0), {VolumeRole});
} }
}); });
connect(m_dbusInterface, &OrgKdeKdeconnectDeviceRemotesystemvolumeInterface::mutedChanged, this, [this](const QString& name, bool muted) { connect(m_dbusInterface, &OrgKdeKdeconnectDeviceRemotesystemvolumeInterface::mutedChanged, this, [this](const QString& name, bool muted) {
for (Sink* s: qAsConst(m_sinkList)) { auto iter = std::find_if(m_sinkList.begin(), m_sinkList.end(), [&name](const Sink& s) {
if (s->name == name) { return s.name == name;
s->muted = muted; });
Q_EMIT dataChanged(index(0,0), index(m_sinkList.size() - 1, 0)); if (iter != m_sinkList.cend()) {
} iter->muted = muted;
int i = std::distance(m_sinkList.begin(), iter);
Q_EMIT dataChanged(index(i, 0), index(i, 0), {MutedRole});
} }
}); });
@ -111,22 +115,18 @@ void RemoteSinksModel::refreshSinkList()
return; return;
} }
const auto cmds = QJsonDocument::fromJson(m_dbusInterface->sinks()).array();
beginResetModel(); beginResetModel();
qDeleteAll(m_sinkList);
m_sinkList.clear(); m_sinkList.clear();
for (auto it = cmds.constBegin(), itEnd = cmds.constEnd(); it!=itEnd; ++it) { const auto cmds = QJsonDocument::fromJson(m_dbusInterface->sinks()).array();
const QJsonObject cont = it->toObject(); for (const QJsonValue& cmd : cmds) {
Sink* sink = new Sink(); const QJsonObject cont = cmd.toObject();
sink->name = cont.value(QStringLiteral("name")).toString(); Sink sink;
sink->description = cont.value(QStringLiteral("description")).toString(); sink.name = cont.value(QStringLiteral("name")).toString();
sink->maxVolume = cont.value(QStringLiteral("maxVolume")).toInt(); sink.description = cont.value(QStringLiteral("description")).toString();
sink->volume = cont.value(QStringLiteral("volume")).toInt(); sink.maxVolume = cont.value(QStringLiteral("maxVolume")).toInt();
sink->muted = cont.value(QStringLiteral("muted")).toBool(); sink.volume = cont.value(QStringLiteral("volume")).toInt();
sink.muted = cont.value(QStringLiteral("muted")).toBool();
m_sinkList.append(sink); m_sinkList.append(sink);
} }
@ -146,24 +146,50 @@ QVariant RemoteSinksModel::data(const QModelIndex& index, int role) const
return QVariant(); return QVariant();
} }
Sink* sink = m_sinkList[index.row()]; const Sink& sink = m_sinkList[index.row()];
switch (role) { switch (role) {
case NameRole: case NameRole:
return sink->name; return sink.name;
case DescriptionRole: case DescriptionRole:
return sink->description; return sink.description;
case MaxVolumeRole: case MaxVolumeRole:
return sink->maxVolume; return sink.maxVolume;
case VolumeRole: case VolumeRole:
return sink->volume; return sink.volume;
case MutedRole: case MutedRole:
return sink->muted; return sink.muted;
default: default:
return QVariant(); return QVariant();
} }
} }
bool RemoteSinksModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
if (!index.isValid()
|| index.row() < 0
|| index.row() >= m_sinkList.count())
{
return false;
}
if (!m_dbusInterface || !m_dbusInterface->isValid()) {
return false;
}
const QString sinkName = m_sinkList[index.row()].name;
switch (role) {
case VolumeRole:
m_dbusInterface->sendVolume(sinkName, value.toInt());
return true;
case MutedRole:
m_dbusInterface->sendMuted(sinkName, value.toBool());
return true;
default:
return false;
}
}
int RemoteSinksModel::rowCount(const QModelIndex& parent) const int RemoteSinksModel::rowCount(const QModelIndex& parent) const
{ {
if (parent.isValid()) { if (parent.isValid()) {

View file

@ -55,6 +55,7 @@ public:
void setDeviceId(const QString& deviceId); void setDeviceId(const QString& deviceId);
QVariant data(const QModelIndex& index, int role) const override; QVariant data(const QModelIndex& index, int role) const override;
bool setData(const QModelIndex& index, const QVariant& value, int role) override;
int rowCount(const QModelIndex& parent = QModelIndex()) const override; int rowCount(const QModelIndex& parent = QModelIndex()) const override;
QHash<int, QByteArray> roleNames() const override; QHash<int, QByteArray> roleNames() const override;
@ -68,7 +69,7 @@ Q_SIGNALS:
private: private:
RemoteSystemVolumeDbusInterface* m_dbusInterface; RemoteSystemVolumeDbusInterface* m_dbusInterface;
QVector<Sink*> m_sinkList; QVector<Sink> m_sinkList;
QString m_deviceId; QString m_deviceId;
}; };