From 2cf9f917bd503759ef276ba14910fd9f9e8a37fb Mon Sep 17 00:00:00 2001 From: fengshaoxiong Date: Wed, 10 Jun 2020 15:49:55 +0800 Subject: [PATCH] =?UTF-8?q?fix(bluetooth):=20=E4=BB=BB=E5=8A=A1=E6=A0=8F?= =?UTF-8?q?=E8=93=9D=E7=89=99=E6=8F=92=E4=BB=B6=E4=B8=80=E7=9B=B4=E5=9C=A8?= =?UTF-8?q?=E5=88=B7=E6=96=B0=E8=93=9D=E7=89=99=E8=AE=BE=E5=A4=87=E5=88=97?= =?UTF-8?q?=E8=A1=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 增加刷新按钮,后端设置刷新时间,刷新时间到了之后给前端发送刷新状态的Dbus信号根据状态改变按钮状态。 Log: 增加刷新按钮,每次点击刷新后,扫描设备且刷新列表持续1min;完成后,可点击再次刷新扫描蓝牙设备1min Bug: https://pms.uniontech.com/zentao/bug-view-31369.html --- plugins/bluetooth/componments/adapter.cpp | 10 ++++ plugins/bluetooth/componments/adapter.h | 5 ++ plugins/bluetooth/componments/adapteritem.cpp | 13 +++++ plugins/bluetooth/componments/adapteritem.h | 1 + .../bluetooth/componments/adaptersmanager.cpp | 13 +++-- .../bluetooth/componments/adaptersmanager.h | 1 + plugins/bluetooth/componments/switchitem.cpp | 51 ++++++++++++++++++- plugins/bluetooth/componments/switchitem.h | 10 ++++ 8 files changed, 100 insertions(+), 4 deletions(-) diff --git a/plugins/bluetooth/componments/adapter.cpp b/plugins/bluetooth/componments/adapter.cpp index 191e14172..a04616833 100644 --- a/plugins/bluetooth/componments/adapter.cpp +++ b/plugins/bluetooth/componments/adapter.cpp @@ -34,6 +34,7 @@ Adapter::Adapter(QObject *parent) , m_name("") , m_powered(false) , m_current(false) + , m_discover(false) { } @@ -185,3 +186,12 @@ void Adapter::setId(const QString &id) { m_id = id; } + +void Adapter::setDiscover(bool discover) +{ + if (discover != m_discover) { + m_discover = discover; + Q_EMIT discoveringChanged(discover); + } +} + diff --git a/plugins/bluetooth/componments/adapter.h b/plugins/bluetooth/componments/adapter.h index 5e89b5dba..13d375b55 100644 --- a/plugins/bluetooth/componments/adapter.h +++ b/plugins/bluetooth/componments/adapter.h @@ -48,6 +48,9 @@ public: inline bool isCurrent() { return m_current; } inline void setCurrent(bool c) { m_current = c; } + inline bool discover() {return m_discover;} + void setDiscover(bool discover); + void initDevicesList(const QJsonDocument &doc); void addDevice(const QJsonObject &deviceObj); void removeDevice(const QString &deviceId); @@ -61,6 +64,7 @@ Q_SIGNALS: void deviceAdded(const Device *device) const; void deviceRemoved(const Device *device) const; void poweredChanged(const bool powered) const; + void discoveringChanged(const bool discover) const; private: void divideDevice(const Device *device); @@ -70,6 +74,7 @@ private: QString m_name; bool m_powered; bool m_current; + bool m_discover; QMap m_devices; QMap m_paredDev; diff --git a/plugins/bluetooth/componments/adapteritem.cpp b/plugins/bluetooth/componments/adapteritem.cpp index e17cfe203..945ec6530 100644 --- a/plugins/bluetooth/componments/adapteritem.cpp +++ b/plugins/bluetooth/componments/adapteritem.cpp @@ -48,6 +48,8 @@ AdapterItem::AdapterItem(AdaptersManager *adapterManager, Adapter *adapter, QWid m_switchItem->setTitle(adapter->name()); m_switchItem->setChecked(adapter->powered(),false); + m_switchItem->setLoading(adapter->discover()); + m_adaptersManager->setAdapterPowered(m_adapter, adapter->powered()); m_deviceLayout->addWidget(m_switchItem); m_deviceLayout->addWidget(m_line); @@ -86,6 +88,7 @@ AdapterItem::AdapterItem(AdaptersManager *adapterManager, Adapter *adapter, QWid } connect(m_switchItem, &SwitchItem::checkedChanged, this, &AdapterItem::showAndConnect); + connect(m_switchItem, &SwitchItem::refresh, this, &AdapterItem::refresh); connect(m_switchItem, &SwitchItem::justUpdateView, [&](bool checked){ showDevices(checked); emit powerChanged(checked); @@ -97,6 +100,10 @@ AdapterItem::AdapterItem(AdaptersManager *adapterManager, Adapter *adapter, QWid m_switchItem->setChecked(powered, false); }); + connect(adapter, &Adapter::discoveringChanged, m_switchItem, [=](const bool discovering){ + m_switchItem->setLoading(discovering); + }); + showDevices(adapter->powered()); } @@ -306,3 +313,9 @@ void AdapterItem::showDevices(bool powered) m_line->setVisible(powered); updateView(); } + +void AdapterItem::refresh() +{ + m_adaptersManager->adapterRefresh(m_adapter); +} + diff --git a/plugins/bluetooth/componments/adapteritem.h b/plugins/bluetooth/componments/adapteritem.h index aa7d7861f..9ceb6fa01 100644 --- a/plugins/bluetooth/componments/adapteritem.h +++ b/plugins/bluetooth/componments/adapteritem.h @@ -60,6 +60,7 @@ private slots: void addDeviceItem(const Device *constDevice); void deviceChangeState(const Device::State state); void moveDeviceItem(Device::State state, DeviceItem *item); + void refresh(); private: void createDeviceItem(Device *device); diff --git a/plugins/bluetooth/componments/adaptersmanager.cpp b/plugins/bluetooth/componments/adaptersmanager.cpp index 2a273909d..9761a014a 100644 --- a/plugins/bluetooth/componments/adaptersmanager.cpp +++ b/plugins/bluetooth/componments/adaptersmanager.cpp @@ -162,13 +162,10 @@ void AdaptersManager::onAdapterPropertiesChanged(const QString &json) const QJsonDocument doc = QJsonDocument::fromJson(json.toUtf8()); const QJsonObject obj = doc.object(); const QString id = obj["Path"].toString(); - const bool isDiscovering = obj["Discovering"].toBool(); QDBusObjectPath dPath(id); Adapter *adapter = const_cast(m_adapters[id]); if (adapter) { - if (!isDiscovering) - m_bluetoothInter->SetAdapterDiscovering(dPath, true); inflateAdapter(adapter, obj); } } @@ -276,8 +273,18 @@ void AdaptersManager::inflateAdapter(Adapter *adapter, const QJsonObject &adapte const QString path = adapterObj["Path"].toString(); const QString alias = adapterObj["Alias"].toString(); const bool powered = adapterObj["Powered"].toBool(); + const bool discovering = adapterObj["Discovering"].toBool(); adapter->setId(path); adapter->setName(alias); adapter->setPowered(powered); + adapter->setDiscover(discovering); +} + +void AdaptersManager::adapterRefresh(const Adapter *adapter) +{ + QDBusObjectPath dPath(adapter->id()); + m_bluetoothInter->SetAdapterDiscoverableTimeout(dPath, 60 * 5); + m_bluetoothInter->SetAdapterDiscoverable(dPath, true); + m_bluetoothInter->RequestDiscovery(dPath); } diff --git a/plugins/bluetooth/componments/adaptersmanager.h b/plugins/bluetooth/componments/adaptersmanager.h index ecb4e5f99..6f44368ce 100644 --- a/plugins/bluetooth/componments/adaptersmanager.h +++ b/plugins/bluetooth/componments/adaptersmanager.h @@ -42,6 +42,7 @@ public: void connectDevice(Device *deviceId); bool defaultAdapterInitPowerState(); int adaptersCount(); + void adapterRefresh(const Adapter *adapter); signals: void adapterIncreased(Adapter *adapter); diff --git a/plugins/bluetooth/componments/switchitem.cpp b/plugins/bluetooth/componments/switchitem.cpp index e7d1e9e89..2ac3f0944 100644 --- a/plugins/bluetooth/componments/switchitem.cpp +++ b/plugins/bluetooth/componments/switchitem.cpp @@ -22,11 +22,14 @@ #include "switchitem.h" #include "bluetoothconstants.h" -#include "bluetoothconstants.h" + +#include +#include #include #include #include +#include extern void initFontColor(QWidget *widget); @@ -40,6 +43,24 @@ SwitchItem::SwitchItem(QWidget *parent) m_switchBtn->setFixedWidth(SWITCHBUTTONWIDTH); + const QPixmap pixmap = DHiDPIHelper::loadNxPixmap(":/wireless/resources/wireless/refresh_dark.svg"); + + m_loadingIndicator = new DLoadingIndicator; + m_loadingIndicator->setSmooth(true); + m_loadingIndicator->setAniDuration(500); + m_loadingIndicator->setAniEasingCurve(QEasingCurve::InOutCirc); + m_loadingIndicator->installEventFilter(this); + m_loadingIndicator->setFixedSize(pixmap.size() / devicePixelRatioF()); + m_loadingIndicator->viewport()->setAutoFillBackground(false); + m_loadingIndicator->setFrameShape(QFrame::NoFrame); + m_loadingIndicator->installEventFilter(this); + + auto themeChanged = [&](DApplicationHelper::ColorType themeType){ + Q_UNUSED(themeType) + setLoadIndicatorIcon(); + }; + themeChanged(DApplicationHelper::instance()->themeType()); + setFixedHeight(CONTROLHEIGHT); auto switchLayout = new QHBoxLayout; switchLayout->setSpacing(0); @@ -47,6 +68,8 @@ SwitchItem::SwitchItem(QWidget *parent) switchLayout->addSpacing(MARGIN); switchLayout->addWidget(m_title); switchLayout->addStretch(); + switchLayout->addWidget(m_loadingIndicator); + switchLayout->addSpacing(MARGIN); switchLayout->addWidget(m_switchBtn); switchLayout->addSpacing(MARGIN); setLayout(switchLayout); @@ -55,6 +78,7 @@ SwitchItem::SwitchItem(QWidget *parent) m_checkState = change; emit checkedChanged(change); }); + connect(DGuiApplicationHelper::instance(), &DGuiApplicationHelper::themeTypeChanged, themeChanged); } void SwitchItem::setChecked(const bool checked,bool notify) @@ -78,6 +102,31 @@ void SwitchItem::setTitle(const QString &title) m_title->setText(strTitle); } +bool SwitchItem::eventFilter(QObject *obj, QEvent *event) +{ + if (obj == m_loadingIndicator) { + if (event->type() == QEvent::MouseButtonPress) { + if(!m_loadingIndicator->loading()) + Q_EMIT refresh(); + } + } + return false; +} + +void SwitchItem::setLoading(const bool bloading) +{ + m_loadingIndicator->setLoading(bloading); +} + +void SwitchItem::setLoadIndicatorIcon() +{ + QString filePath = ":/wireless/resources/wireless/refresh.svg"; + if(DGuiApplicationHelper::instance()->themeType() == DGuiApplicationHelper::LightType) + filePath = ":/wireless/resources/wireless/refresh_dark.svg"; + const QPixmap pixmap = DHiDPIHelper::loadNxPixmap(filePath); + m_loadingIndicator->setImageSource(pixmap); +} + //void SwitchItem::mousePressEvent(QMouseEvent *event) //{ // emit clicked(m_adapterId); diff --git a/plugins/bluetooth/componments/switchitem.h b/plugins/bluetooth/componments/switchitem.h index 54d4d004b..e07c91b36 100644 --- a/plugins/bluetooth/componments/switchitem.h +++ b/plugins/bluetooth/componments/switchitem.h @@ -24,7 +24,10 @@ #define SWITCHITEM_H #include +#include +#include +DGUI_USE_NAMESPACE DWIDGET_USE_NAMESPACE class QLabel; @@ -35,6 +38,8 @@ public: explicit SwitchItem(QWidget *parent = nullptr); void setChecked(const bool checked = true,bool notify = false); void setTitle(const QString &title); + void setLoading(const bool bloading); + void setLoadIndicatorIcon(); inline bool checkState() { return m_checkState; } inline bool isdefault() { return m_default; } @@ -43,12 +48,17 @@ public: signals: void checkedChanged(bool checked); void justUpdateView(bool checked); + void refresh(); + +protected: + bool eventFilter(QObject *obj,QEvent *event) Q_DECL_OVERRIDE; private: QLabel *m_title; DSwitchButton *m_switchBtn; bool m_default; bool m_checkState; + DLoadingIndicator *m_loadingIndicator; }; #endif // SWITCHITEM_H