From 20bb074f0187da237dbed08b9741f67c4c143c28 Mon Sep 17 00:00:00 2001 From: fpc_diesel Date: Thu, 14 May 2020 13:13:25 +0800 Subject: [PATCH] refactor(bluetooth&network):code self test MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit task21255 [任务栏]蓝牙和网络插件部分的代码自检 (cherry picked from commit 581050c5b811ccbd36202d74b0c8007d5d4b29f4) --- plugins/bluetooth/bluetoothapplet.cpp | 51 ++++++++++--------- plugins/bluetooth/bluetoothapplet.h | 10 ++-- plugins/bluetooth/bluetoothitem.cpp | 16 +++--- plugins/bluetooth/bluetoothitem.h | 4 +- plugins/bluetooth/componments/adapter.cpp | 20 ++++---- plugins/bluetooth/componments/adapter.h | 2 +- plugins/bluetooth/componments/adapteritem.cpp | 33 ++++++------ plugins/bluetooth/componments/adapteritem.h | 9 ++-- .../bluetooth/componments/adaptersmanager.cpp | 12 ++--- plugins/bluetooth/componments/deviceitem.cpp | 10 ++-- plugins/bluetooth/componments/deviceitem.h | 2 +- plugins/bluetooth/componments/switchitem.cpp | 3 +- plugins/bluetooth/componments/switchitem.h | 3 +- 13 files changed, 88 insertions(+), 87 deletions(-) diff --git a/plugins/bluetooth/bluetoothapplet.cpp b/plugins/bluetooth/bluetoothapplet.cpp index 0b8d45ceb..dd9a8af53 100644 --- a/plugins/bluetooth/bluetoothapplet.cpp +++ b/plugins/bluetooth/bluetoothapplet.cpp @@ -30,6 +30,10 @@ #include "componments/bluetoothconstants.h" #include + +#include +#include + DGUI_USE_NAMESPACE extern void initFontColor(QWidget *widget) @@ -60,7 +64,7 @@ BluetoothApplet::BluetoothApplet(QWidget *parent) { m_line->setVisible(false); - auto defaultFont = font(); + QFont defaultFont = font(); auto titlefont = QFont(defaultFont.family(), defaultFont.pointSize() + 2); m_appletName->setText(tr("Bluetooth")); @@ -97,7 +101,7 @@ BluetoothApplet::BluetoothApplet(QWidget *parent) void BluetoothApplet::setAdapterPowered(bool powered) { - for (auto adapterItem : m_adapterItems) { + for (AdapterItem *adapterItem : m_adapterItems) { if (adapterItem) adapterItem->setPowered(powered); } @@ -113,15 +117,14 @@ bool BluetoothApplet::hasAadapter() return m_adaptersManager->adaptersCount(); } -void BluetoothApplet::onPowerChanged(bool state) +void BluetoothApplet::onPowerChanged() { - Q_UNUSED(state) bool powerState = false; - for (auto adapterItem : m_adapterItems) { - if (adapterItem->isPowered()) { - powerState = true; - break; - } + for (AdapterItem *adapterItem : m_adapterItems) { + if (adapterItem->isPowered()) { + powerState = true; + break; + } } emit powerChanged(powerState); } @@ -129,7 +132,7 @@ void BluetoothApplet::onPowerChanged(bool state) void BluetoothApplet::onDeviceStateChanged() { Device::State deviceState = Device::StateUnavailable; - for (auto adapterItem : m_adapterItems) { + for (AdapterItem *adapterItem : m_adapterItems) { if (Device::StateAvailable == adapterItem->currentDeviceState()) { deviceState = Device::StateAvailable; continue; @@ -152,24 +155,24 @@ void BluetoothApplet::addAdapter(Adapter *adapter) emit justHasAdapter(); } - auto adapterId = adapter->id(); - auto adatpterItem = new AdapterItem(m_adaptersManager, adapter); -// m_adapterItems[adapterId] = adatpterItem; -// m_centrealLayout->addWidget(adatpterItem); -// getDevieInitState(adatpterItem); + QString adapterId = adapter->id(); + auto adatpterItem = new AdapterItem(m_adaptersManager, adapter, this); + m_adapterItems[adapterId] = adatpterItem; + m_centrealLayout->addWidget(adatpterItem); + getDevieInitState(adatpterItem); -// connect(adatpterItem, &AdapterItem::deviceStateChanged, this, &BluetoothApplet::onDeviceStateChanged); -// connect(adatpterItem, &AdapterItem::powerChanged, this, &BluetoothApplet::onPowerChanged); -// connect(adatpterItem, &AdapterItem::sizeChange, this, &BluetoothApplet::updateView); + connect(adatpterItem, &AdapterItem::deviceStateChanged, this, &BluetoothApplet::onDeviceStateChanged); + connect(adatpterItem, &AdapterItem::powerChanged, this, &BluetoothApplet::onPowerChanged); + connect(adatpterItem, &AdapterItem::sizeChange, this, &BluetoothApplet::updateView); -// updateView(); + updateView(); } void BluetoothApplet::removeAdapter(Adapter *adapter) { if (adapter) { - auto adapterId = adapter->id(); - auto adapterItem = m_adapterItems.value(adapterId); + QString adapterId = adapter->id(); + AdapterItem *adapterItem = m_adapterItems.value(adapterId); if (adapterItem) { m_centrealLayout->removeWidget(adapterItem); delete adapterItem; @@ -185,7 +188,7 @@ void BluetoothApplet::updateView() { int contentHeight = 0; int itemCount = 0; - for (auto adapterItem : m_adapterItems) { + for (AdapterItem *adapterItem : m_adapterItems) { if (adapterItem) { contentHeight += adapterItem->viewHeight(); if (adapterItem->isPowered()) @@ -193,7 +196,7 @@ void BluetoothApplet::updateView() } } - auto adaptersCnt = m_adapterItems.size(); + int adaptersCnt = m_adapterItems.size(); if (adaptersCnt > 1) { m_line->setVisible(true); m_appletName->setVisible(true); @@ -218,7 +221,7 @@ void BluetoothApplet::getDevieInitState(AdapterItem *item) Device::State deviceState = item->initDeviceState(); Device::State otherDeviceState = Device::StateUnavailable; - for (auto adapterItem : m_adapterItems) { + for (AdapterItem *adapterItem : m_adapterItems) { if (adapterItem != item) { if (Device::StateAvailable == adapterItem->currentDeviceState()) { otherDeviceState = Device::StateAvailable; diff --git a/plugins/bluetooth/bluetoothapplet.h b/plugins/bluetooth/bluetoothapplet.h index 657e4a575..065bc58c0 100644 --- a/plugins/bluetooth/bluetoothapplet.h +++ b/plugins/bluetooth/bluetoothapplet.h @@ -26,9 +26,9 @@ #include "componments/device.h" #include -#include -#include +class QLabel; +class QVBoxLayout; class Adapter; class AdapterItem; class HorizontalSeparator; @@ -43,17 +43,17 @@ public: bool hasAadapter(); public slots : - void addAdapter(Adapter *constadapter); + void addAdapter(Adapter *adapter); void removeAdapter(Adapter *adapter); signals: - void powerChanged(bool state); + void powerChanged(bool powered); void deviceStateChanged(const Device::State state); void noAdapter(); void justHasAdapter(); private slots: - void onPowerChanged(bool state); + void onPowerChanged(); void onDeviceStateChanged(); private: diff --git a/plugins/bluetooth/bluetoothitem.cpp b/plugins/bluetooth/bluetoothitem.cpp index ae7fe3e98..8d4b01e54 100644 --- a/plugins/bluetooth/bluetoothitem.cpp +++ b/plugins/bluetooth/bluetoothitem.cpp @@ -33,8 +33,8 @@ #include // menu actions -#define SHIFT "shift" -#define SETTINGS "settings" +#define SHIFT "shift" +#define SETTINGS "settings" DWIDGET_USE_NAMESPACE DGUI_USE_NAMESPACE @@ -134,7 +134,7 @@ void BluetoothItem::refreshIcon() m_timer->start(); stateString = "waiting"; iconString = QString("bluetooth-%1-symbolic").arg(stateString); - const auto ratio = devicePixelRatioF(); + const qreal ratio = devicePixelRatioF(); int iconSize = PLUGIN_ICON_MAX_SIZE; if (height() <= PLUGIN_BACKGROUND_MIN_SIZE && DGuiApplicationHelper::instance()->themeType() == DGuiApplicationHelper::LightType) iconString.append(PLUGIN_MIN_ICON_NAME); @@ -155,7 +155,7 @@ void BluetoothItem::refreshIcon() m_timer->stop(); iconString = QString("bluetooth-%1-symbolic").arg(stateString); - const auto ratio = devicePixelRatioF(); + const qreal ratio = devicePixelRatioF(); int iconSize = PLUGIN_ICON_MAX_SIZE; if (height() <= PLUGIN_BACKGROUND_MIN_SIZE && DGuiApplicationHelper::instance()->themeType() == DGuiApplicationHelper::LightType) iconString.append(PLUGIN_MIN_ICON_NAME); @@ -170,9 +170,9 @@ bool BluetoothItem::hasAdapter() return m_applet->hasAadapter(); } -void BluetoothItem::resizeEvent(QResizeEvent *e) +void BluetoothItem::resizeEvent(QResizeEvent *event) { - QWidget::resizeEvent(e); + QWidget::resizeEvent(event); const Dock::Position position = qApp->property(PROP_POSITION).value(); // 保持横纵比 @@ -187,9 +187,9 @@ void BluetoothItem::resizeEvent(QResizeEvent *e) refreshIcon(); } -void BluetoothItem::paintEvent(QPaintEvent *e) +void BluetoothItem::paintEvent(QPaintEvent *event) { - QWidget::paintEvent(e); + QWidget::paintEvent(event); QPainter painter(this); const QRectF &rf = QRectF(rect()); diff --git a/plugins/bluetooth/bluetoothitem.h b/plugins/bluetooth/bluetoothitem.h index 7e17bfd6e..9ac31d022 100644 --- a/plugins/bluetooth/bluetoothitem.h +++ b/plugins/bluetooth/bluetoothitem.h @@ -49,8 +49,8 @@ public: bool hasAdapter(); protected: - void resizeEvent(QResizeEvent *e); - void paintEvent(QPaintEvent *e); + void resizeEvent(QResizeEvent *event); + void paintEvent(QPaintEvent *event); signals: void requestContextMenu() const; diff --git a/plugins/bluetooth/componments/adapter.cpp b/plugins/bluetooth/componments/adapter.cpp index d76e26d05..79cf71f56 100644 --- a/plugins/bluetooth/componments/adapter.cpp +++ b/plugins/bluetooth/componments/adapter.cpp @@ -72,7 +72,7 @@ void Adapter::addDevice(const QJsonObject &deviceObj) void Adapter::removeDevice(const QString &deviceId) { - auto constDevice = m_devices.value(deviceId); + const Device *constDevice = m_devices.value(deviceId); auto device = const_cast(constDevice); if (device) { m_devices.remove(deviceId); @@ -82,15 +82,15 @@ void Adapter::removeDevice(const QString &deviceId) } } -void Adapter::updateDevice(const QJsonObject &json) +void Adapter::updateDevice(const QJsonObject &dviceJson) { - const QString id = json["Path"].toString(); - const QString name = json["Alias"].toString(); - const bool paired = json["Paired"].toBool(); - const int rssi = json["RSSI"].toInt(); - const Device::State state = Device::State(json["State"].toInt()); + const QString id = dviceJson["Path"].toString(); + const QString name = dviceJson["Alias"].toString(); + const bool paired = dviceJson["Paired"].toBool(); + const int rssi = dviceJson["RSSI"].toInt(); + const Device::State state = Device::State(dviceJson["State"].toInt()); - auto constdevice = m_devices.value(id); + const Device *constdevice = m_devices.value(id); auto device = const_cast(constdevice); if (device) { device->setId(id); @@ -142,9 +142,9 @@ void Adapter::setPowered(bool powered) void Adapter::initDevicesList(const QJsonDocument &doc) { - auto arr = doc.array(); + QJsonArray arr = doc.array(); for (QJsonValue val : arr) { - auto deviceObj = val.toObject(); + QJsonObject deviceObj = val.toObject(); const QString adapterId = deviceObj["AdapterPath"].toString(); const QString id = deviceObj["Path"].toString(); const QString name = deviceObj["Alias"].toString(); diff --git a/plugins/bluetooth/componments/adapter.h b/plugins/bluetooth/componments/adapter.h index efad96230..5e89b5dba 100644 --- a/plugins/bluetooth/componments/adapter.h +++ b/plugins/bluetooth/componments/adapter.h @@ -51,7 +51,7 @@ public: void initDevicesList(const QJsonDocument &doc); void addDevice(const QJsonObject &deviceObj); void removeDevice(const QString &deviceId); - void updateDevice(const QJsonObject &json); + void updateDevice(const QJsonObject &dviceJson); void removeAllDevices(); const QMap &paredDevices() const; int paredDevicesCount() const; diff --git a/plugins/bluetooth/componments/adapteritem.cpp b/plugins/bluetooth/componments/adapteritem.cpp index d8e9b896b..5781dab2a 100644 --- a/plugins/bluetooth/componments/adapteritem.cpp +++ b/plugins/bluetooth/componments/adapteritem.cpp @@ -29,6 +29,9 @@ #include +#include +#include + extern void initFontColor(QWidget *widget); AdapterItem::AdapterItem(AdaptersManager *adapterManager, Adapter *adapter, QWidget *parent) @@ -67,15 +70,15 @@ AdapterItem::AdapterItem(AdaptersManager *adapterManager, Adapter *adapter, QWid m_centralWidget->setAutoFillBackground(false); viewport()->setAutoFillBackground(false); - auto myDevices = adapter->devices(); - for (auto constDevice : myDevices) { + QMap myDevices = adapter->devices(); + for (const Device *constDevice : myDevices) { auto device = const_cast(constDevice); if (device) createDeviceItem(device); } m_initDeviceState = Device::StateUnavailable; - for (auto constDevice : myDevices) { + for (const Device *constDevice : myDevices) { auto device = const_cast(constDevice); if (device) { if (device->state() == Device::StateAvailable) { @@ -133,7 +136,7 @@ void AdapterItem::deviceItemPaired(const bool paired) { auto device = qobject_cast(sender()); if (device) { - auto deviceItem = m_deviceItems.value(device->id()); + DeviceItem *deviceItem = m_deviceItems.value(device->id()); if (paired) { m_sortUnConnect.removeOne(deviceItem); m_sortConnected << deviceItem; @@ -149,8 +152,8 @@ void AdapterItem::deviceRssiChanged() { auto device = qobject_cast(sender()); if (device) { - auto deviceItem = m_deviceItems.value(device->id()); - auto state = device->state(); + DeviceItem *deviceItem = m_deviceItems.value(device->id()); + Device::State state = device->state(); if (deviceItem && Device::StateConnected == state) qSort(m_sortConnected); else @@ -164,7 +167,7 @@ void AdapterItem::removeDeviceItem(const Device *device) if (!device) return; - auto deviceItem = m_deviceItems.value(device->id()); + DeviceItem *deviceItem = m_deviceItems.value(device->id()); if (deviceItem) { m_deviceItems.remove(device->id()); m_sortConnected.removeOne(deviceItem); @@ -196,7 +199,7 @@ void AdapterItem::deviceChangeState(const Device::State state) { auto device = qobject_cast(sender()); if (device) { - auto deviceItem = m_deviceItems.value(device->id()); + DeviceItem *deviceItem = m_deviceItems.value(device->id()); if (deviceItem) { switch (state) { case Device::StateUnavailable: { @@ -254,7 +257,7 @@ void AdapterItem::createDeviceItem(Device *device) if (!device) return; - auto deviceId = device->id(); + QString deviceId = device->id(); auto deviceItem = new DeviceItem(device->name(), this); deviceItem->setDevice(device); m_deviceItems[deviceId] = deviceItem; @@ -273,14 +276,14 @@ void AdapterItem::createDeviceItem(Device *device) void AdapterItem::updateView() { - auto contentHeight = m_switchItem->height(); + int contentHeight = m_switchItem->height(); contentHeight += (m_deviceLayout->count() - 3) * ITEMHEIGHT; m_centralWidget->setFixedHeight(contentHeight); setFixedHeight(contentHeight); emit sizeChange(); } -void AdapterItem::showDevices(bool change) +void AdapterItem::showDevices(bool powered) { if (m_sortConnected.size()) qSort(m_sortConnected); @@ -291,15 +294,15 @@ void AdapterItem::showDevices(bool change) deviceItems << m_sortConnected << m_sortUnConnect; for (DeviceItem *deviceItem : deviceItems) { - if (change) + if (powered) m_deviceLayout->addWidget(deviceItem); else m_deviceLayout->removeWidget(deviceItem); - deviceItem->setVisible(change); + deviceItem->setVisible(powered); } - auto itemCount = m_deviceItems.size(); - m_line->setVisible(change); + int itemCount = m_deviceItems.size(); + m_line->setVisible(powered); m_openControlCenter->setVisible(!itemCount); updateView(); } diff --git a/plugins/bluetooth/componments/adapteritem.h b/plugins/bluetooth/componments/adapteritem.h index 9ab4d2cb5..d01ef3c40 100644 --- a/plugins/bluetooth/componments/adapteritem.h +++ b/plugins/bluetooth/componments/adapteritem.h @@ -26,10 +26,9 @@ #include "device.h" #include -#include -#include -#include +class QLabel; +class QVBoxLayout; class HorizontalSeparator; class Adapter; class SwitchItem; @@ -65,7 +64,7 @@ private slots: private: void createDeviceItem(Device *device); void updateView(); - void showDevices(bool change); + void showDevices(bool powered); private: QWidget *m_centralWidget; @@ -77,7 +76,7 @@ private: Adapter *m_adapter; SwitchItem *m_switchItem; - QMap m_deviceItems; + QMap m_deviceItems; Device::State m_initDeviceState; Device::State m_currentDeviceState; QList m_sortConnected; diff --git a/plugins/bluetooth/componments/adaptersmanager.cpp b/plugins/bluetooth/componments/adaptersmanager.cpp index 2340ba1ba..2a273909d 100644 --- a/plugins/bluetooth/componments/adaptersmanager.cpp +++ b/plugins/bluetooth/componments/adaptersmanager.cpp @@ -35,6 +35,7 @@ AdaptersManager::AdaptersManager(QObject *parent) "/com/deepin/daemon/Bluetooth", QDBusConnection::sessionBus(), this)) + , m_defaultAdapterState(false) { connect(m_bluetoothInter, &DBusBluetooth::AdapterAdded, this, &AdaptersManager::addAdapter); connect(m_bluetoothInter, &DBusBluetooth::AdapterRemoved, this, &AdaptersManager::removeAdapter); @@ -77,10 +78,7 @@ AdaptersManager::AdaptersManager(QObject *parent) for (int index = 0; index < arr.size(); index++) { auto *adapter = new Adapter(this); adapterAdd(adapter, arr[index].toObject()); - if (!index) { - adapter->setCurrent(true); - m_defaultAdapterState = adapter->powered(); - } + m_defaultAdapterState |= adapter->powered(); } } @@ -179,7 +177,7 @@ void AdaptersManager::onDevicePropertiesChanged(const QString &json) { const QJsonDocument doc = QJsonDocument::fromJson(json.toUtf8()); const QJsonObject obj = doc.object(); - for (auto constAdapter : m_adapters) { + for (const Adapter *constAdapter : m_adapters) { auto adapter = const_cast(constAdapter); if (adapter) adapter->updateDevice(obj); @@ -254,7 +252,7 @@ void AdaptersManager::adapterAdd(Adapter *adapter, const QJsonObject &adpterObj) if (!call.isError()) { QDBusReply reply = call.reply(); const QString replyStr = reply.value(); - auto doc = QJsonDocument::fromJson(replyStr.toUtf8()); + QJsonDocument doc = QJsonDocument::fromJson(replyStr.toUtf8()); adapter->initDevicesList(doc); emit this->adapterIncreased(adapter); } else { @@ -264,7 +262,7 @@ void AdaptersManager::adapterAdd(Adapter *adapter, const QJsonObject &adpterObj) delete watcher; }); - auto id = adapter->id(); + QString id = adapter->id(); if (!id.isEmpty()) { m_adapters[id] = adapter; } diff --git a/plugins/bluetooth/componments/deviceitem.cpp b/plugins/bluetooth/componments/deviceitem.cpp index 54472fc01..98bf84478 100644 --- a/plugins/bluetooth/componments/deviceitem.cpp +++ b/plugins/bluetooth/componments/deviceitem.cpp @@ -86,11 +86,11 @@ bool DeviceItem::operator <(const DeviceItem &item) return this->device()->rssi() < item.device()->rssi(); } -void DeviceItem::setDevice(Device *d) +void DeviceItem::setDevice(Device *device) { - if (d) { - m_device = d; - changeState(d->state()); + if (device) { + m_device = device; + changeState(device->state()); } } @@ -105,7 +105,6 @@ void DeviceItem::enterEvent(QEvent *event) QWidget::enterEvent(event); if (m_device) { if (Device::StateConnected == m_device->state()) { -// m_state->setPixmap(QPixmap(":/notify_close_press@2x.png")); m_state->setPixmap(QPixmap(":/disconnect" + m_statSuffix)); } } @@ -116,7 +115,6 @@ void DeviceItem::leaveEvent(QEvent *event) QWidget::enterEvent(event); if (m_device) { if (Device::StateConnected == m_device->state()) { -// m_state->setPixmap(QPixmap(":/list_select@2x.png")); m_state->setPixmap(QPixmap(":/select" + m_statSuffix)); } } diff --git a/plugins/bluetooth/componments/deviceitem.h b/plugins/bluetooth/componments/deviceitem.h index 22b81a85b..7e5d595cb 100644 --- a/plugins/bluetooth/componments/deviceitem.h +++ b/plugins/bluetooth/componments/deviceitem.h @@ -41,7 +41,7 @@ public: inline void setTitle(const QString &name) { m_title->setText(name); } - void setDevice(Device *d); + void setDevice(Device *device); inline Device *device() { return m_device; } inline const Device *device() const { return m_device; } diff --git a/plugins/bluetooth/componments/switchitem.cpp b/plugins/bluetooth/componments/switchitem.cpp index 3f28488a0..93be987a6 100644 --- a/plugins/bluetooth/componments/switchitem.cpp +++ b/plugins/bluetooth/componments/switchitem.cpp @@ -26,6 +26,7 @@ #include #include +#include extern void initFontColor(QWidget *widget); @@ -40,7 +41,7 @@ SwitchItem::SwitchItem(QWidget *parent) m_switchBtn->setFixedWidth(SWITCHBUTTONWIDTH); setFixedHeight(CONTROLHEIGHT); - auto switchLayout = new QHBoxLayout(this); + auto switchLayout = new QHBoxLayout; switchLayout->setSpacing(0); switchLayout->setMargin(0); switchLayout->addSpacing(MARGIN); diff --git a/plugins/bluetooth/componments/switchitem.h b/plugins/bluetooth/componments/switchitem.h index 674aa1463..82eec4a2b 100644 --- a/plugins/bluetooth/componments/switchitem.h +++ b/plugins/bluetooth/componments/switchitem.h @@ -25,10 +25,9 @@ #include -#include - DWIDGET_USE_NAMESPACE +class QLabel; class SwitchItem : public QWidget { Q_OBJECT