fix(bluetooth): 任务栏蓝牙插件一直在刷新蓝牙设备列表

增加刷新按钮,后端设置刷新时间,刷新时间到了之后给前端发送刷新状态的Dbus信号根据状态改变按钮状态。

Log: 增加刷新按钮,每次点击刷新后,扫描设备且刷新列表持续1min;完成后,可点击再次刷新扫描蓝牙设备1min
Bug: https://pms.uniontech.com/zentao/bug-view-31369.html
This commit is contained in:
fengshaoxiong 2020-06-10 15:49:55 +08:00
parent 903778bc83
commit 2cf9f917bd
8 changed files with 100 additions and 4 deletions

View File

@ -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);
}
}

View File

@ -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<QString, const Device *> m_devices;
QMap<QString, const Device *> m_paredDev;

View File

@ -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);
}

View File

@ -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);

View File

@ -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<Adapter *>(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);
}

View File

@ -42,6 +42,7 @@ public:
void connectDevice(Device *deviceId);
bool defaultAdapterInitPowerState();
int adaptersCount();
void adapterRefresh(const Adapter *adapter);
signals:
void adapterIncreased(Adapter *adapter);

View File

@ -22,11 +22,14 @@
#include "switchitem.h"
#include "bluetoothconstants.h"
#include "bluetoothconstants.h"
#include <DHiDPIHelper>
#include <DApplicationHelper>
#include <QHBoxLayout>
#include <QFontMetrics>
#include <QLabel>
#include <QEvent>
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);

View File

@ -24,7 +24,10 @@
#define SWITCHITEM_H
#include <DSwitchButton>
#include <dloadingindicator.h>
#include <DGuiApplicationHelper>
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