1
0
Fork 0
mirror of https://git.jami.net/savoirfairelinux/jami-client-qt.git synced 2025-07-24 17:35:43 +02:00
jami-client-qt/src/audiodevicemodel.cpp
Andreas Traczyk 28aa5aab6d settings: use default audio devices
Refactors audio settings to avoid list model code duplication. This code
could be greatly simplified by using string lists, however the combo-box
component design is highly dependent on QAbstractItemModel based models.

Also translates the handlebarred strings upon presentation, which is
currently used to translate the "Default" prefix for the first device
item.

Gitlab: #346
Change-Id: I5ed282d29cc4ec6a090a9cdf47b0459f0db2a99b
2021-03-22 15:59:24 -04:00

123 lines
3.3 KiB
C++

/*
* Copyright (C) 2021 by Savoir-faire Linux
* Author: Andreas Traczyk <andreas.traczyk@savoirfairelinux.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "audiodevicemodel.h"
#include "lrcinstance.h"
AudioDeviceModel::AudioDeviceModel(QObject* parent)
: QAbstractListModel(parent)
{
connect(this, &AudioDeviceModel::typeChanged, this, &AudioDeviceModel::reset);
}
int
AudioDeviceModel::rowCount(const QModelIndex& parent) const
{
if (!parent.isValid()) {
return devices_.size();
}
return 0;
}
int
AudioDeviceModel::columnCount(const QModelIndex& parent) const
{
Q_UNUSED(parent);
return 1;
}
QVariant
AudioDeviceModel::data(const QModelIndex& index, int role) const
{
if (!index.isValid() || devices_.size() <= index.row()) {
return QVariant();
}
switch (role) {
case Qt::DisplayRole:
case Role::DeviceName: {
auto deviceName = devices_.at(index.row());
QRegularExpression re("{{(.*?)}}");
QRegularExpressionMatch match = re.match(deviceName);
if (match.hasMatch() && re.captureCount() > 0) {
deviceName.replace(match.captured(0), QObject::tr(match.captured(1).toUtf8()));
}
return QVariant(deviceName.toUtf8());
}
case Role::RawDeviceName:
return QVariant(devices_.at(index.row()));
default:
break;
}
return QVariant();
}
QHash<int, QByteArray>
AudioDeviceModel::roleNames() const
{
QHash<int, QByteArray> roles;
roles[DeviceName] = "DeviceName";
roles[RawDeviceName] = "RawDeviceName";
return roles;
}
QModelIndex
AudioDeviceModel::index(int row, int column, const QModelIndex&) const
{
if (column != 0) {
return QModelIndex();
}
if (row >= 0 && row < rowCount()) {
return createIndex(row, column);
}
return QModelIndex();
}
QModelIndex
AudioDeviceModel::parent(const QModelIndex&) const
{
return QModelIndex();
}
Qt::ItemFlags
AudioDeviceModel::flags(const QModelIndex& index) const
{
auto flags = QAbstractItemModel::flags(index) | Qt::ItemNeverHasChildren | Qt::ItemIsSelectable;
if (!index.isValid()) {
return QAbstractItemModel::flags(index);
}
return flags;
}
void
AudioDeviceModel::reset()
{
beginResetModel();
devices_ = type_ == Type::Record ? LRCInstance::avModel().getAudioInputDevices()
: LRCInstance::avModel().getAudioOutputDevices();
endResetModel();
}
int
AudioDeviceModel::getCurrentIndex()
{
QString currentId = LRCInstance::avModel().getInputDevice();
auto resultList = match(index(0, 0), Qt::DisplayRole, QVariant(currentId));
return resultList.size() > 0 ? resultList[0].row() : 0;
}