Skip to content

Commit

Permalink
fix: Adjust sound combobox mode data
Browse files Browse the repository at this point in the history
Adjust sound combobox mode data

Log: Adjust sound combobox mode data
pms: BUG-291893
  • Loading branch information
swq authored and swq11 committed Dec 26, 2024
1 parent efa159c commit 8b7d173
Show file tree
Hide file tree
Showing 10 changed files with 128 additions and 154 deletions.
69 changes: 39 additions & 30 deletions src/plugin-sound/operation/soundDeviceModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,11 @@ SoundDeviceModel::SoundDeviceModel(QObject *parent)

void SoundDeviceModel::clearData()
{
if (m_soundDeviceDatas.count() < 1) {
if (m_ports.count() < 1) {
return;
}
for (SoundDeviceData *soundDeviceData : m_soundDeviceDatas) {
delete soundDeviceData;
}

m_ports.clear();
m_soundDeviceDatas.clear();
}

void SoundDeviceModel::addData(Port *port)
Expand All @@ -27,12 +24,6 @@ void SoundDeviceModel::addData(Port *port)
return;
}
beginInsertRows(QModelIndex(), rowCount(), rowCount());
SoundDeviceData *data = new SoundDeviceData;
data->setName(port->name() + "(" + port->cardName() + ")");
data->setIschecked(port->isEnabled());
data->setCardId(port->cardId());
data->setPortId(port->id());
m_soundDeviceDatas.append(data);
m_ports.append(port);
endInsertRows();
}
Expand All @@ -44,56 +35,74 @@ void SoundDeviceModel::removeData(Port *port)
}
int index = m_ports.indexOf(port);
beginRemoveRows(QModelIndex(), index, index);

m_soundDeviceDatas.remove(index);
endRemoveRows();
m_ports.remove(index);
endRemoveRows();

}


int SoundDeviceModel::getRowCount()

Check warning on line 44 in src/plugin-sound/operation/soundDeviceModel.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

The function 'getRowCount' is never used.
{
return m_soundDeviceDatas.count();
return m_ports.count();
}

SoundDeviceData *SoundDeviceModel::getSoundDeviceData(int index)
int SoundDeviceModel::getCurrentIndex() const

Check warning on line 49 in src/plugin-sound/operation/soundDeviceModel.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

The function 'getCurrentIndex' is never used.
{
if (m_soundDeviceDatas.count() < index || index < 0) {
for (int index = 0; index< m_ports.count(); index++) {
if (m_ports.at(index)->isActive()) {
return index;
}
}

return 0;
}

Port *SoundDeviceModel::getSoundDeviceData(int index)

Check warning on line 60 in src/plugin-sound/operation/soundDeviceModel.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

The function 'getSoundDeviceData' is never used.
{
if (m_ports.count() < index || index < 0) {

Check warning on line 62 in src/plugin-sound/operation/soundDeviceModel.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Either the condition 'm_ports.count()<index' is redundant or 'index' can have the value m_ports.size(). Expression 'm_ports.at(index)' cause access out of bounds.
return nullptr;
}

return m_soundDeviceDatas.at(index);
return m_ports.at(index);
}

void SoundDeviceModel::updateSoundDeviceData(Port *port)

Check warning on line 69 in src/plugin-sound/operation/soundDeviceModel.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

The function 'updateSoundDeviceData' is never used.
{
for (int index = 0; index < m_soundDeviceDatas.count(); index++) {
if (m_soundDeviceDatas[index]->getPortId() == port->id()) {
m_soundDeviceDatas[index]->setIschecked(port->isEnabled());
m_soundDeviceDatas[index]->setCardId(port->cardId());
m_soundDeviceDatas[index]->setName(port->name() + "(" + port->cardName() + ")");
for (int index = 0; index < m_ports.count(); index++) {
if (m_ports[index]->id() == port->id()) {
QModelIndex modelIndex = createIndex(index, 0);
emit dataChanged(modelIndex, modelIndex, { IsChecked });
emit dataChanged(modelIndex, modelIndex, {});
break;
}
}
}

void SoundDeviceModel::updateAllSoundDeviceData()

Check warning on line 80 in src/plugin-sound/operation/soundDeviceModel.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

The function 'updateAllSoundDeviceData' is never used.
{
for (int index = 0; index < m_ports.count(); index++) {
QModelIndex modelIndex = createIndex(index, 0);
emit dataChanged(modelIndex, modelIndex, {});
}
}

int SoundDeviceModel::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return m_soundDeviceDatas.count();
return m_ports.count();
}

QVariant SoundDeviceModel::data(const QModelIndex &index, int role) const
{
if (index.row() < 0 || index.row() >= m_soundDeviceDatas.count())
if (index.row() < 0 || index.row() >= m_ports.count())
return QVariant();

const SoundDeviceData* soundDeviceData = m_soundDeviceDatas[index.row()];
const Port* port = m_ports[index.row()];
if (role == NameRole)
return soundDeviceData->name();
else if (role == IsChecked)
return soundDeviceData->ischecked();
return port->name() + "(" + port->cardName() + ")";
else if (role == IsEnabled)
return port->isEnabled();
else if (role == IsActive)
return port->isActive();

return QVariant();
}
12 changes: 8 additions & 4 deletions src/plugin-sound/operation/soundDeviceModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ class SoundDeviceModel: public QAbstractListModel {
public:
enum soundEffectsRoles{
NameRole = Qt::UserRole + 1,
IsChecked
IsEnabled,
IsActive,
};

explicit SoundDeviceModel(QObject *parent = nullptr);
Expand All @@ -29,8 +30,11 @@ class SoundDeviceModel: public QAbstractListModel {
void removeData(Port* port);
int getRowCount();

SoundDeviceData* getSoundDeviceData(int index);
int getCurrentIndex() const;

Port* getSoundDeviceData(int index);
void updateSoundDeviceData(Port* port);
void updateAllSoundDeviceData();

protected:

Expand All @@ -40,13 +44,13 @@ class SoundDeviceModel: public QAbstractListModel {
QHash<int, QByteArray> roleNames() const override {
QHash<int, QByteArray> roles;
roles[NameRole] = "name";
roles[IsChecked] = "isChecked";
roles[IsEnabled] = "isEnabled";
roles[IsActive] = "isActive";
return roles;
}


private:
QList<SoundDeviceData*> m_soundDeviceDatas;
QList<Port*> m_ports;
};

Expand Down
4 changes: 2 additions & 2 deletions src/plugin-sound/operation/soundeffectsmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ void SoundEffectsModel::updateSoundEffectsData(int index, bool enable)

m_soundEffectsData[index]->setChecked(enable);
QModelIndex modelIndex = createIndex(index, 0);
emit dataChanged(modelIndex, modelIndex, { IsChecked });
emit dataChanged(modelIndex, modelIndex, { IsEnabled });
}

void SoundEffectsModel::updateSoundEffectsAniIcon(int index, QString path)
Expand Down Expand Up @@ -84,7 +84,7 @@ QVariant SoundEffectsModel::data(const QModelIndex &index, int role) const
return soundEffectsData->name();
else if (role == DisplayTextRole)
return soundEffectsData->dispalyText();
else if (role == IsChecked)
else if (role == IsEnabled)
return soundEffectsData->checked();
else if (role == AniIconPath) {
return soundEffectsData->aniIconPath();
Expand Down
4 changes: 2 additions & 2 deletions src/plugin-sound/operation/soundeffectsmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class SoundEffectsModel : public QAbstractListModel
enum soundEffectsRoles{
NameRole = Qt::UserRole + 1,
DisplayTextRole,
IsChecked,
IsEnabled,
AniIconPath
};

Expand All @@ -46,7 +46,7 @@ class SoundEffectsModel : public QAbstractListModel
QHash<int, QByteArray> roles;
roles[NameRole] = "name";
roles[DisplayTextRole] = "dispalyText";
roles[IsChecked] = "isChecked";
roles[IsEnabled] = "isEnabled";
roles[AniIconPath] = "aniIconPath";
return roles;
}
Expand Down
90 changes: 17 additions & 73 deletions src/plugin-sound/operation/soundmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,24 +109,11 @@ SoundModel::~SoundModel()
}
}

Port *SoundModel::activeinPutPort() const
{
return m_activeinPutPort;
}

void SoundModel::setActiveinPutPort(Port *newActiveinPutPort)
{
m_activeinPutPort = newActiveinPutPort;
setInPutPortComboIndex(m_inPutPortCombo.indexOf(m_activeinPutPort->name() + "(" + m_activeinPutPort->cardName() + ")"));
}

void SoundModel::updatePortCombo()
{
QStringList outPutPortCombo;
QStringList inPutPortCombo;

Port* inputActivePort = nullptr;
Port* outputActivePort = nullptr;
for (Port* port : m_ports) {
if (port->isEnabled()) {
switch (port->direction()) {
Expand All @@ -138,41 +125,10 @@ void SoundModel::updatePortCombo()
break;
}
}

if (port->isActive()) {
switch (port->direction()) {
case Port::In:
inputActivePort= port;
break;
case Port::Out:
outputActivePort = port;
break;
}
}
}

setInPutPortCombo(inPutPortCombo);
setOutPutPortCombo(outPutPortCombo);

if (inputActivePort)
setActiveinPutPort(inputActivePort);
if (outputActivePort)
setActiveOutPutPort(outputActivePort);
}

Port * SoundModel::getPortForComboIndex(int index, int portType)
{
QList<Port*> ports = portType == Port::In ? m_inputPorts : m_outputPorts;
QStringList portCombo = portType == Port::In ? m_inPutPortCombo : m_outPutPortCombo;
if (portCombo.count()) {
for (Port* port : ports) {
if (port->name() + "(" + port->cardName() + ")" == portCombo.at(index)) {
return port;
}
}
}

return nullptr;
}

int SoundModel::inPutPortComboIndex() const
Expand Down Expand Up @@ -201,18 +157,6 @@ void SoundModel::setInPutPortCombo(const QStringList &newInPutPortCombo)
emit inPutPortComboChanged();
}

Port *SoundModel::activeOutPutPort() const
{
return m_activeOutPutPort;
}

void SoundModel::setActiveOutPutPort(Port *newActiveOutPutPort)
{
m_activeOutPutPort = newActiveOutPutPort;
setOutPutPortComboIndex(m_outPutPortCombo.indexOf(m_activeOutPutPort->name() + "(" + m_activeOutPutPort->cardName() + ")"));
setShowBluetoothMode(m_activeOutPutPort->isBluetoothPort());
}

int SoundModel::outPutPortComboIndex() const
{
return m_outPutPortComboIndex;
Expand Down Expand Up @@ -298,12 +242,6 @@ void SoundModel::setMicrophoneFeedback(double microphoneFeedback)

void SoundModel::setPort(Port *port)
{
if (port->direction() == Port::Direction::Out) {
setActiveOutPutPort(port);
}
if (port->direction() == Port::Direction::In) {
setActiveinPutPort(port);
}
Q_EMIT setPortChanged(port);
}

Expand Down Expand Up @@ -598,24 +536,30 @@ void SoundModel::initSoundDeviceModel(Port::Direction direction)
}
}

SoundDeviceData *SoundModel::getSoundDeviceData(int index, int portType)
Port *SoundModel::getSoundDeviceData(int index, int portType)
{
SoundDeviceModel *soundDeviceModel =
portType == Port::In ? soundInputDeviceModel() : soundOutputDeviceModel();
SoundDeviceModel *soundDeviceModel = portType == Port::In ? soundInputDeviceModel() : soundOutputDeviceModel();
return soundDeviceModel ? soundDeviceModel->getSoundDeviceData(index) : nullptr;
}

void SoundModel::updateSoundDeviceModel(Port *port)
{
if (port->direction() == Port::In ) {
m_soundInputDeviceModel->updateSoundDeviceData(port);
return;
}
SoundDeviceModel *soundDeviceModel =
port->direction() == Port::In ? soundInputDeviceModel() : soundOutputDeviceModel();

if (port->direction() == Port::Out) {
m_soundOutputDeviceModel->updateSoundDeviceData(port);
return;
}
soundDeviceModel->updateSoundDeviceData(port);
}

void SoundModel::updateAllDeviceModel()
{
m_soundOutputDeviceModel->updateAllSoundDeviceData();
m_soundInputDeviceModel->updateAllSoundDeviceData();
}

void SoundModel::updateActiveComboIndex()
{
setInPutPortComboIndex(m_soundInputDeviceModel->getCurrentIndex());
setOutPutPortComboIndex(m_soundOutputDeviceModel->getCurrentIndex());
}

void SoundModel::setOutPutCount(int newOutPutCount)
Expand Down
11 changes: 3 additions & 8 deletions src/plugin-sound/operation/soundmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -167,22 +167,19 @@ class SoundModel : public QObject

// 初始化输入设备ui数据
void initSoundDeviceModel(Port::Direction direction);
SoundDeviceData* getSoundDeviceData(int index, int portType);
Port* getSoundDeviceData(int index, int portType);
void updateSoundDeviceModel(Port* port);
void updateAllDeviceModel();
void updateActiveComboIndex();

int outPutPortComboIndex() const;
void setOutPutPortComboIndex(int newOutPutPortComboIndex);
Port *activeOutPutPort() const;
void setActiveOutPutPort(Port *newActiveOutPutPort);
QStringList inPutPortCombo() const;
void setInPutPortCombo(const QStringList &newInPutPortCombo);
int inPutPortComboIndex() const;
void setInPutPortComboIndex(int newInPutPortComboIndex);
Port *activeinPutPort() const;
void setActiveinPutPort(Port *newActiveinPutPort);

void updatePortCombo();
Port* getPortForComboIndex(int index, int portType);

Q_INVOKABLE SoundEffectsModel* soundEffectsModel() const;
Q_INVOKABLE QString getListName(int index) const;
Expand Down Expand Up @@ -319,12 +316,10 @@ class SoundModel : public QObject

QStringList m_outPutPortCombo;
int m_outPutPortComboIndex;
Port* m_activeOutPutPort;
bool m_outPutPortComboEnable;

QStringList m_inPutPortCombo;
int m_inPutPortComboIndex;
Port* m_activeinPutPort;
bool m_inPutPortComboEnable;

SoundEffectsModel* m_soundEffectsModel;
Expand Down
Loading

0 comments on commit 8b7d173

Please sign in to comment.