fix(bluetooth): 修复蓝牙设备名称更新后未实时刷新的问题

修复任务栏的蓝牙设备列表偶尔出现空白设备名称的问题
修复蓝牙设备名称更新后控制中心实时刷新而任务栏未实时刷新的问题

Bug: https://pms.uniontech.com/zentao/bug-view-59120.html
Log: 蓝牙列表中部分设备名称显示空白和蓝牙设备名称更新后没有实时刷新
Change-Id: Iaa27f5a8422a9c804635a99de77a814611344088
This commit is contained in:
myj 2021-03-04 09:58:05 +08:00 committed by mayijian
parent 339016cd51
commit 6a28c55153
11 changed files with 74 additions and 51 deletions

View File

@ -53,11 +53,11 @@ BluetoothItem::BluetoothItem(QWidget *parent)
m_tipsLabel->setVisible(false);
refreshIcon();
connect(m_applet, &BluetoothApplet::powerChanged, [ & ] (bool powered) {
connect(m_applet, &BluetoothApplet::powerChanged, [ & ](bool powered) {
m_adapterPowered = powered;
refreshIcon();
});
connect(m_applet, &BluetoothApplet::deviceStateChanged, [ & ] (const Device* device) {
connect(m_applet, &BluetoothApplet::deviceStateChanged, [ & ](const Device *device) {
m_devState = device->state();
refreshIcon();
refreshTips();
@ -116,8 +116,7 @@ void BluetoothItem::invokeMenuItem(const QString menuId, const bool checked)
if (menuId == SHIFT) {
m_applet->setAdapterPowered(!m_adapterPowered);
}
else if (menuId == SETTINGS)
} else if (menuId == SETTINGS) {
DDBusSender()
.service("com.deepin.dde.ControlCenter")
.interface("com.deepin.dde.ControlCenter")
@ -125,6 +124,7 @@ void BluetoothItem::invokeMenuItem(const QString menuId, const bool checked)
.method(QString("ShowModule"))
.arg(QString("bluetooth"))
.call();
}
}
void BluetoothItem::refreshIcon()

View File

@ -49,11 +49,11 @@ void BluetoothPlugin::init(PluginProxyInterface *proxyInter)
m_bluetoothItem = new BluetoothItem;
connect(m_bluetoothItem, &BluetoothItem::justHasAdapter, [&]{
connect(m_bluetoothItem, &BluetoothItem::justHasAdapter, [&] {
m_enableState = true;
refreshPluginItemsVisible();
});
connect(m_bluetoothItem, &BluetoothItem::noAdapter, [&]{
connect(m_bluetoothItem, &BluetoothItem::noAdapter, [&] {
m_enableState = false;
refreshPluginItemsVisible();
});

View File

@ -100,6 +100,10 @@ void Adapter::updateDevice(const QJsonObject &dviceJson)
const bool connectState = dviceJson["ConnectState"].toBool();
const QString bluetoothDeviceType = dviceJson["Icon"].toString();
// FIXME: Solve the problem that the device name in the Bluetooth list is blank
if (name.isEmpty() && alias.isEmpty())
return ;
const Device *constdevice = m_devices.value(id);
auto device = const_cast<Device *>(constdevice);
if (device) {
@ -112,6 +116,7 @@ void Adapter::updateDevice(const QJsonObject &dviceJson)
device->setConnectState(connectState);
device->setState(state);
device->setDeviceType(bluetoothDeviceType);
emit deviceNameUpdated(device);
}
}

View File

@ -63,6 +63,7 @@ Q_SIGNALS:
void nameChanged(const QString &name) const;
void deviceAdded(const Device *device) const;
void deviceRemoved(const Device *device) const;
void deviceNameUpdated(const Device *device) const;
void poweredChanged(const bool powered) const;
void discoveringChanged(const bool discover) const;

View File

@ -117,18 +117,18 @@ void AdaptersManager::connectDevice(const Device *device, Adapter *adapter)
if (device) {
QDBusObjectPath path(device->id());
switch (device->state()) {
case Device::StateUnavailable: {
m_bluetoothInter->ConnectDevice(path, QDBusObjectPath(adapter->id()));
qDebug() << "connect to device: " << device->alias();
}
break;
case Device::StateAvailable:
break;
case Device::StateConnected: {
m_bluetoothInter->DisconnectDevice(path);
qDebug() << "disconnect device: " << device->alias();
}
break;
case Device::StateUnavailable: {
m_bluetoothInter->ConnectDevice(path, QDBusObjectPath(adapter->id()));
qDebug() << "connect to device: " << device->alias();
}
break;
case Device::StateAvailable:
break;
case Device::StateConnected: {
m_bluetoothInter->DisconnectDevice(path);
qDebug() << "disconnect device: " << device->alias();
}
break;
}
}
}

View File

@ -102,7 +102,7 @@ void BluetoothDeviceItem::updateDeviceState(Device::State state)
m_loading->start();
m_stateAction->setVisible(true);
m_standarditem->setCheckState(Qt::Unchecked);
} else if (state == Device::StateConnected){
} else if (state == Device::StateConnected) {
m_loading->stop();
m_stateAction->setVisible(false);
m_standarditem->setCheckState(Qt::Checked);
@ -146,7 +146,7 @@ void BluetoothAdapterItem::onConnectDevice(const QModelIndex &index)
return;
DStandardItem *deviceitem = dynamic_cast<DStandardItem *>(deviceModel->item(index.row()));
foreach(const auto item, m_deviceItems) {
foreach (const auto item, m_deviceItems) {
if (item->standardItem() == deviceitem) {
emit connectDevice(item->device(), m_adapter);
}
@ -201,7 +201,7 @@ void BluetoothAdapterItem::initData()
if (!m_adapter->powered())
return;
foreach(const auto device, m_adapter->devices()) {
foreach (const auto device, m_adapter->devices()) {
if (!m_deviceItems.contains(device->id()))
onDeviceAdded(device);
}
@ -212,7 +212,7 @@ void BluetoothAdapterItem::initData()
void BluetoothAdapterItem::onDeviceAdded(const Device *device)
{
int insertRow = 0;
foreach(const auto item, m_deviceItems) {
foreach (const auto item, m_deviceItems) {
if (item->device()->connectState()) {
insertRow++;
}
@ -232,7 +232,7 @@ void BluetoothAdapterItem::onDeviceAdded(const Device *device)
void BluetoothAdapterItem::onDeviceRemoved(const Device *device)
{
if(m_deviceItems.isEmpty())
if (m_deviceItems.isEmpty())
return;
m_deviceModel->removeRow(m_deviceItems.value(device->id())->standardItem()->row());
@ -241,6 +241,20 @@ void BluetoothAdapterItem::onDeviceRemoved(const Device *device)
emit deviceCountChanged();
}
void BluetoothAdapterItem::onDeviceNameUpdated(const Device *device)
{
if (m_deviceItems.isEmpty())
return;
// 修复蓝牙设备列表中,设备名称更新后未实时刷新的问题
if (m_deviceItems.contains(device->id())) {
BluetoothDeviceItem *item = m_deviceItems[device->id()];
if (item && !item->device()->alias().isEmpty()) {
item->updateDeviceState(item->device()->state());
}
}
}
void BluetoothAdapterItem::initUi()
{
m_refreshBtn->setFixedSize(24, 24);
@ -287,9 +301,10 @@ void BluetoothAdapterItem::initConnect()
connect(DApplicationHelper::instance(), &DApplicationHelper::themeTypeChanged, this, &BluetoothAdapterItem::updateIconTheme);
connect(m_adapter, &Adapter::deviceAdded, this, &BluetoothAdapterItem::onDeviceAdded);
connect(m_adapter, &Adapter::deviceRemoved, this, &BluetoothAdapterItem::onDeviceRemoved);
connect(m_adapter, &Adapter::deviceNameUpdated, this, &BluetoothAdapterItem::onDeviceNameUpdated);
connect(m_adapter, &Adapter::nameChanged, this, &BluetoothAdapterItem::onAdapterNameChanged);
connect(m_deviceListview, &DListView::clicked, this, &BluetoothAdapterItem::onConnectDevice);
connect(m_adapter, &Adapter::discoveringChanged, this, [ = ] (bool state) {
connect(m_adapter, &Adapter::discoveringChanged, this, [ = ](bool state) {
if (state) {
m_refreshBtn->startRotate();
} else {
@ -301,7 +316,7 @@ void BluetoothAdapterItem::initConnect()
emit requestRefreshAdapter(m_adapter);
});
connect(m_adapter, &Adapter::poweredChanged, this, [ = ] (bool state) {
connect(m_adapter, &Adapter::poweredChanged, this, [ = ](bool state) {
initData();
m_refreshBtn->setVisible(state);
m_deviceListview->setVisible(state);
@ -309,7 +324,7 @@ void BluetoothAdapterItem::initConnect()
m_adapterStateBtn->setEnabled(true);
emit adapterPowerChanged();
});
connect(m_adapterStateBtn, &DSwitchButton::clicked, this, [ = ] (bool state){
connect(m_adapterStateBtn, &DSwitchButton::clicked, this, [ = ](bool state) {
qDeleteAll(m_deviceItems);
m_deviceItems.clear();
m_deviceModel->clear();

View File

@ -69,7 +69,7 @@ public slots:
signals:
void requestTopDeviceItem(DStandardItem *item);
void deviceStateChanged(const Device* device);
void deviceStateChanged(const Device *device);
private:
void initActionList();
@ -100,10 +100,12 @@ public slots:
void onDeviceAdded(const Device *device);
// 移除蓝牙设备
void onDeviceRemoved(const Device *device);
// 蓝牙设备名称更新
void onDeviceNameUpdated(const Device *device);
// 连接蓝牙设备
void onConnectDevice(const QModelIndex &index);
// 将已连接的蓝牙设备放到列表第一个
void onTopDeviceItem(DStandardItem* item);
void onTopDeviceItem(DStandardItem *item);
// 设置蓝牙适配器名称
void onAdapterNameChanged(const QString name);
void updateIconTheme(DGuiApplicationHelper::ColorType type);
@ -114,7 +116,7 @@ signals:
void requestRefreshAdapter(Adapter *adapter);
void connectDevice(const Device *device, Adapter *adapter);
void deviceCountChanged();
void deviceStateChanged(const Device* device);
void deviceStateChanged(const Device *device);
private:
void initData();

View File

@ -97,7 +97,7 @@ BluetoothApplet::BluetoothApplet(QWidget *parent)
bool BluetoothApplet::poweredInitState()
{
foreach(const auto adapter, m_adapterItems) {
foreach (const auto adapter, m_adapterItems) {
if (adapter->adapter()->powered()) {
return true;
}
@ -182,7 +182,7 @@ void BluetoothApplet::onSetAdapterPower(Adapter *adapter, bool state)
void BluetoothApplet::updateBluetoothPowerState()
{
foreach(const auto item, m_adapterItems) {
foreach (const auto item, m_adapterItems) {
if (item->adapter()->powered()) {
emit powerChanged(true);
return;
@ -243,7 +243,7 @@ void BluetoothApplet::updateSize()
{
int hetght = 0;
int count = 0;
foreach(const auto item, m_adapterItems) {
foreach (const auto item, m_adapterItems) {
hetght += TitleHeight + TitleSpace;
if (item->adapter()->powered()) {
count += item->currentDeviceCount();

View File

@ -82,7 +82,7 @@ signals:
void noAdapter();
void justHasAdapter();
void powerChanged(bool state);
void deviceStateChanged(const Device* device);
void deviceStateChanged(const Device *device);
public slots:
// 蓝牙适配器增加

View File

@ -24,23 +24,23 @@
#include <QDateTime>
QMap<QString,QString> Device::deviceType2Icon = {
{"unknow","other"},
{"computer","pc"},
{"phone","phone"},
{"video-display","vidicon"},
{"multimedia-player","tv"},
{"scanner","scaner"},
{"input-keyboard","keyboard"},
{"input-mouse","mouse"},
{"input-gaming","other"},
{"input-tablet","touchpad"},
{"audio-card","pheadset"},
{"network-wireless","lan"},
{"camera-video","vidicon"},
{"printer","print"},
{"camera-photo","camera"},
{"modem","other"}
QMap<QString, QString> Device::deviceType2Icon = {
{"unknow", "other"},
{"computer", "pc"},
{"phone", "phone"},
{"video-display", "vidicon"},
{"multimedia-player", "tv"},
{"scanner", "scaner"},
{"input-keyboard", "keyboard"},
{"input-mouse", "mouse"},
{"input-gaming", "other"},
{"input-tablet", "touchpad"},
{"audio-card", "pheadset"},
{"network-wireless", "lan"},
{"camera-video", "vidicon"},
{"printer", "print"},
{"camera-photo", "camera"},
{"modem", "other"}
};
Device::Device(QObject *parent)

View File

@ -39,7 +39,7 @@ public:
Q_ENUM(State)
private:
static QMap<QString,QString> deviceType2Icon;
static QMap<QString, QString> deviceType2Icon;
public:
explicit Device(QObject *parent = nullptr);