From 6a28c55153d6eddbb2ce9204bae374a12ffb21e5 Mon Sep 17 00:00:00 2001 From: myj Date: Thu, 4 Mar 2021 09:58:05 +0800 Subject: [PATCH] =?UTF-8?q?fix(bluetooth):=20=E4=BF=AE=E5=A4=8D=E8=93=9D?= =?UTF-8?q?=E7=89=99=E8=AE=BE=E5=A4=87=E5=90=8D=E7=A7=B0=E6=9B=B4=E6=96=B0?= =?UTF-8?q?=E5=90=8E=E6=9C=AA=E5=AE=9E=E6=97=B6=E5=88=B7=E6=96=B0=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修复任务栏的蓝牙设备列表偶尔出现空白设备名称的问题 修复蓝牙设备名称更新后控制中心实时刷新而任务栏未实时刷新的问题 Bug: https://pms.uniontech.com/zentao/bug-view-59120.html Log: 蓝牙列表中部分设备名称显示空白和蓝牙设备名称更新后没有实时刷新 Change-Id: Iaa27f5a8422a9c804635a99de77a814611344088 --- plugins/bluetooth/bluetoothitem.cpp | 8 ++--- plugins/bluetooth/bluetoothplugin.cpp | 4 +-- plugins/bluetooth/componments/adapter.cpp | 5 +++ plugins/bluetooth/componments/adapter.h | 1 + .../bluetooth/componments/adaptersmanager.cpp | 24 ++++++------- .../componments/bluetoothadapteritem.cpp | 31 ++++++++++++----- .../componments/bluetoothadapteritem.h | 8 +++-- .../bluetooth/componments/bluetoothapplet.cpp | 6 ++-- .../bluetooth/componments/bluetoothapplet.h | 2 +- plugins/bluetooth/componments/device.cpp | 34 +++++++++---------- plugins/bluetooth/componments/device.h | 2 +- 11 files changed, 74 insertions(+), 51 deletions(-) diff --git a/plugins/bluetooth/bluetoothitem.cpp b/plugins/bluetooth/bluetoothitem.cpp index 62f0b024d..428575b52 100644 --- a/plugins/bluetooth/bluetoothitem.cpp +++ b/plugins/bluetooth/bluetoothitem.cpp @@ -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() diff --git a/plugins/bluetooth/bluetoothplugin.cpp b/plugins/bluetooth/bluetoothplugin.cpp index 61d9410ce..0f98860b4 100644 --- a/plugins/bluetooth/bluetoothplugin.cpp +++ b/plugins/bluetooth/bluetoothplugin.cpp @@ -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(); }); diff --git a/plugins/bluetooth/componments/adapter.cpp b/plugins/bluetooth/componments/adapter.cpp index 8cb8d266a..1017a0156 100644 --- a/plugins/bluetooth/componments/adapter.cpp +++ b/plugins/bluetooth/componments/adapter.cpp @@ -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(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); } } diff --git a/plugins/bluetooth/componments/adapter.h b/plugins/bluetooth/componments/adapter.h index b00654e6b..2682350a9 100644 --- a/plugins/bluetooth/componments/adapter.h +++ b/plugins/bluetooth/componments/adapter.h @@ -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; diff --git a/plugins/bluetooth/componments/adaptersmanager.cpp b/plugins/bluetooth/componments/adaptersmanager.cpp index 53670b1ac..240fd03d5 100644 --- a/plugins/bluetooth/componments/adaptersmanager.cpp +++ b/plugins/bluetooth/componments/adaptersmanager.cpp @@ -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; } } } diff --git a/plugins/bluetooth/componments/bluetoothadapteritem.cpp b/plugins/bluetooth/componments/bluetoothadapteritem.cpp index 2b86ed04b..d2124109f 100644 --- a/plugins/bluetooth/componments/bluetoothadapteritem.cpp +++ b/plugins/bluetooth/componments/bluetoothadapteritem.cpp @@ -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(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(); diff --git a/plugins/bluetooth/componments/bluetoothadapteritem.h b/plugins/bluetooth/componments/bluetoothadapteritem.h index 48fc3d47d..96ab8a626 100644 --- a/plugins/bluetooth/componments/bluetoothadapteritem.h +++ b/plugins/bluetooth/componments/bluetoothadapteritem.h @@ -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(); diff --git a/plugins/bluetooth/componments/bluetoothapplet.cpp b/plugins/bluetooth/componments/bluetoothapplet.cpp index 2e163ac8f..71ada4098 100644 --- a/plugins/bluetooth/componments/bluetoothapplet.cpp +++ b/plugins/bluetooth/componments/bluetoothapplet.cpp @@ -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(); diff --git a/plugins/bluetooth/componments/bluetoothapplet.h b/plugins/bluetooth/componments/bluetoothapplet.h index 19e50d773..9c2a6c8da 100644 --- a/plugins/bluetooth/componments/bluetoothapplet.h +++ b/plugins/bluetooth/componments/bluetoothapplet.h @@ -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: // 蓝牙适配器增加 diff --git a/plugins/bluetooth/componments/device.cpp b/plugins/bluetooth/componments/device.cpp index 4a903fde0..5a17d72ac 100644 --- a/plugins/bluetooth/componments/device.cpp +++ b/plugins/bluetooth/componments/device.cpp @@ -24,23 +24,23 @@ #include -QMap 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 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) diff --git a/plugins/bluetooth/componments/device.h b/plugins/bluetooth/componments/device.h index 4aa2c3147..fd2e05b42 100644 --- a/plugins/bluetooth/componments/device.h +++ b/plugins/bluetooth/componments/device.h @@ -39,7 +39,7 @@ public: Q_ENUM(State) private: - static QMap deviceType2Icon; + static QMap deviceType2Icon; public: explicit Device(QObject *parent = nullptr);