From 6af087ab1792bec8c2fff4de34f1cd404cc40b36 Mon Sep 17 00:00:00 2001 From: chenhongtao Date: Wed, 26 Jul 2023 17:08:40 +0800 Subject: [PATCH] WIP: support show battery percentage Log: --- .../operation/bluetoothadapter.h | 1 + .../operation/bluetoothdevice.h | 5 ++ .../operation/bluetoothworker.cpp | 2 + .../window/bluetoothdevicemodel.cpp | 66 +++++++++++++++++-- .../window/bluetoothdevicemodel.h | 1 + 5 files changed, 71 insertions(+), 4 deletions(-) diff --git a/src/plugin-bluetooth/operation/bluetoothadapter.h b/src/plugin-bluetooth/operation/bluetoothadapter.h index e37eea440b..5f4b84ffe5 100644 --- a/src/plugin-bluetooth/operation/bluetoothadapter.h +++ b/src/plugin-bluetooth/operation/bluetoothadapter.h @@ -38,6 +38,7 @@ class BluetoothAdapter : public QObject void inflate(const QJsonObject &obj); void inflateDevice(BluetoothDevice *device, const QJsonObject &deviceObj); + public Q_SLOTS: void addDevice(const BluetoothDevice *device); void removeDevice(const QString &deviceId); diff --git a/src/plugin-bluetooth/operation/bluetoothdevice.h b/src/plugin-bluetooth/operation/bluetoothdevice.h index 81fbfd41f8..906a004e83 100644 --- a/src/plugin-bluetooth/operation/bluetoothdevice.h +++ b/src/plugin-bluetooth/operation/bluetoothdevice.h @@ -30,6 +30,10 @@ class BluetoothDevice : public QObject void setId(const QString &id); + inline int battery() const { return m_battery; } + + inline void setBattery(const int battery) { m_battery = battery; } + inline QString address() const { return m_address; } void setAddress(const QString &addr); @@ -85,6 +89,7 @@ class BluetoothDevice : public QObject bool m_connecting; bool m_connectState; State m_state; + int m_battery = 0; }; QDebug &operator<<(QDebug &stream, const BluetoothDevice *device); diff --git a/src/plugin-bluetooth/operation/bluetoothworker.cpp b/src/plugin-bluetooth/operation/bluetoothworker.cpp index 497ae66047..45f22ab2e4 100644 --- a/src/plugin-bluetooth/operation/bluetoothworker.cpp +++ b/src/plugin-bluetooth/operation/bluetoothworker.cpp @@ -235,6 +235,7 @@ void BluetoothWorker::addDevice(const QString &json) QJsonObject obj = doc.object(); const QString adapterId = obj["AdapterPath"].toString(); const QString id = obj["Path"].toString(); + const int battery = obj["Battery"].toInt(); const BluetoothAdapter *result = m_model->adapterById(adapterId); BluetoothAdapter *adapter = const_cast(result); @@ -243,6 +244,7 @@ void BluetoothWorker::addDevice(const QString &json) BluetoothDevice *device = const_cast(result1); if (!device) device = new BluetoothDevice(adapter); + device->setBattery(battery); adapter->inflateDevice(device, obj); adapter->addDevice(device); } diff --git a/src/plugin-bluetooth/window/bluetoothdevicemodel.cpp b/src/plugin-bluetooth/window/bluetoothdevicemodel.cpp index c8ee0d109d..22a26f81ae 100644 --- a/src/plugin-bluetooth/window/bluetoothdevicemodel.cpp +++ b/src/plugin-bluetooth/window/bluetoothdevicemodel.cpp @@ -24,6 +24,8 @@ struct BluetoothDeviceItemAction { const BluetoothDevice *device; DViewItemAction *spinnerAction; + DViewItemAction *percentageText; + DViewItemAction *percentageIcon; DViewItemAction *textAction; DViewItemAction *spaceAction; DViewItemAction *iconAction; @@ -33,20 +35,29 @@ struct BluetoothDeviceItemAction explicit BluetoothDeviceItemAction(const BluetoothDevice *_device) : device(_device) + , spinnerAction( - new DViewItemAction(Qt::AlignLeft | Qt::AlignCenter, QSize(), QSize(), false)) - , textAction(new DViewItemAction(Qt::AlignLeft, QSize(), QSize(), true)) + new DViewItemAction(Qt::AlignVCenter , QSize(), QSize(), false)) + , percentageText( + new DViewItemAction(Qt::AlignVCenter , QSize(), QSize(), false)) + , percentageIcon( + new DViewItemAction(Qt::AlignVCenter, QSize(), QSize(), false)) + , textAction(new DViewItemAction(Qt::AlignVCenter, QSize(), QSize(), true)) , spaceAction( - new DViewItemAction(Qt::AlignCenter | Qt::AlignRight, QSize(), QSize(), false)) - , iconAction(new DViewItemAction(Qt::AlignCenter | Qt::AlignRight, QSize(), QSize(), true)) + new DViewItemAction(Qt::AlignVCenter, QSize(), QSize(), false)) + , iconAction(new DViewItemAction(Qt::AlignVCenter, QSize(), QSize(), true)) , loadingIndicator(nullptr) , item(new DStandardItem()) { iconAction->setData(static_cast(device)); + actionList.append(percentageText); + actionList.append(percentageIcon); actionList.append(spinnerAction); actionList.append(textAction); actionList.append(spaceAction); actionList.append(iconAction); + percentageText->setVisible(false); + percentageIcon->setVisible(false); spinnerAction->setVisible(false); item->setActionList(Qt::Edge::RightEdge, actionList); } @@ -167,6 +178,7 @@ QVariant BluetoothDeviceModel::data(const QModelIndex &index, int role) const int row = index.row(); const BluetoothDevice *device = m_data.at(row)->device; + switch (role) { case Qt::EditRole: case Qt::DisplayRole: @@ -320,7 +332,53 @@ void BluetoothDeviceModel::updateItem(BluetoothDeviceItemAction *item) item->iconAction->setVisible(m_paired); if (m_paired) { item->iconAction->setIcon(m_parent->style()->standardIcon(QStyle::SP_ArrowRight)); + int battery = device->battery(); + if (battery != 0) { + item->percentageIcon->setVisible(true); + item->percentageIcon->setIcon(getBatteryIcon(battery)); + item->percentageText->setVisible(true); + item->percentageText->setText(QString("%1%").arg(battery)); + return; + } + } + item->percentageIcon->setVisible(false); + item->percentageText->setVisible(false); +} + + + +QIcon BluetoothDeviceModel::getBatteryIcon(int percentage) +{ + /* 0-5%、6-10%、11%-20%、21-30%、31-40%、41-50%、51-60%、61%-70%、71-80%、81-90%、91-100% */ + QString percentageStr; + if (percentage <= 5) { + percentageStr = "000"; + } else if (percentage <= 10) { + percentageStr = "010"; + } else if (percentage <= 20) { + percentageStr = "020"; + } else if (percentage <= 30) { + percentageStr = "030"; + } else if (percentage <= 40) { + percentageStr = "040"; + } else if (percentage <= 50) { + percentageStr = "050"; + } else if (percentage <= 60) { + percentageStr = "060"; + } else if (percentage <= 70) { + percentageStr = "070"; + } else if (percentage <= 80) { + percentageStr = "080"; + } else if (percentage <= 90) { + percentageStr = "090"; + } else if (percentage <= 100) { + percentageStr = "100"; + } else { + percentageStr = "unknow"; } + + return QIcon::fromTheme(QString("battery-%1-symbolic").arg(percentageStr)); + } void BluetoothDeviceModel::showAnonymous(bool show) diff --git a/src/plugin-bluetooth/window/bluetoothdevicemodel.h b/src/plugin-bluetooth/window/bluetoothdevicemodel.h index 4949bd51b1..af494256bf 100644 --- a/src/plugin-bluetooth/window/bluetoothdevicemodel.h +++ b/src/plugin-bluetooth/window/bluetoothdevicemodel.h @@ -41,6 +41,7 @@ class BluetoothDeviceModel : public QAbstractItemModel public Q_SLOTS: void showAnonymous(bool show); + QIcon getBatteryIcon(int percentage); private Q_SLOTS: void addDevice(const BluetoothDevice *device);