diff --git a/plugins/dde-dock/bluetooth/componments/adapter.cpp b/plugins/dde-dock/bluetooth/componments/adapter.cpp index dd8578b10..a9a9ca8fd 100644 --- a/plugins/dde-dock/bluetooth/componments/adapter.cpp +++ b/plugins/dde-dock/bluetooth/componments/adapter.cpp @@ -38,6 +38,7 @@ void Adapter::addDevice(const QJsonObject &deviceObj) const Device::State state = Device::State(deviceObj["State"].toInt()); const bool connectState = deviceObj["ConnectState"].toBool(); const QString bluetoothDeviceType = deviceObj["Icon"].toString(); + const int battery = deviceObj["Battery"].toInt(); removeDevice(id); @@ -52,6 +53,7 @@ void Adapter::addDevice(const QJsonObject &deviceObj) device->setRssi(rssi); device->setAdapterId(m_id); device->setDeviceType(bluetoothDeviceType); + device->setBattery(battery); m_devices[id] = device; @@ -79,6 +81,7 @@ void Adapter::updateDevice(const QJsonObject &dviceJson) const Device::State state = Device::State(dviceJson["State"].toInt()); const bool connectState = dviceJson["ConnectState"].toBool(); const QString bluetoothDeviceType = dviceJson["Icon"].toString(); + const int battery = dviceJson["Battery"].toInt(); // FIXME: Solve the problem that the device name in the Bluetooth list is blank if (name.isEmpty() && alias.isEmpty()) @@ -96,6 +99,7 @@ void Adapter::updateDevice(const QJsonObject &dviceJson) device->setConnectState(connectState); device->setState(state); device->setDeviceType(bluetoothDeviceType); + device->setBattery(battery); emit deviceNameUpdated(device); } } @@ -122,6 +126,7 @@ void Adapter::initDevicesList(const QJsonDocument &doc) const Device::State state = Device::State(deviceObj["State"].toInt()); const bool connectState = deviceObj["ConnectState"].toBool(); const QString bluetoothDeviceType = deviceObj["Icon"].toString(); + const int battery = deviceObj["Battery"].toInt(); auto device = new Device(this); device->setId(id); @@ -133,6 +138,7 @@ void Adapter::initDevicesList(const QJsonDocument &doc) device->setRssi(rssi); device->setAdapterId(adapterId); device->setDeviceType(bluetoothDeviceType); + device->setBattery(battery); m_devices[id] = device; } diff --git a/plugins/dde-dock/bluetooth/componments/bluetoothadapteritem.cpp b/plugins/dde-dock/bluetooth/componments/bluetoothadapteritem.cpp index 4a9fbcd81..604c7ac3d 100644 --- a/plugins/dde-dock/bluetooth/componments/bluetoothadapteritem.cpp +++ b/plugins/dde-dock/bluetooth/componments/bluetoothadapteritem.cpp @@ -35,6 +35,11 @@ BluetoothDeviceItem::BluetoothDeviceItem(QStyle *style, const Device *device, Pl updateDeviceState(m_device->state()); initConnect(); + + m_standardItem->updateBattery(m_device->battery()); + connect(m_device, &Device::batteryChanged, this, [this](int battery) { + m_standardItem->updateBattery(battery); + }); } BluetoothDeviceItem::~BluetoothDeviceItem() diff --git a/plugins/dde-dock/bluetooth/componments/device.cpp b/plugins/dde-dock/bluetooth/componments/device.cpp index 520b18b2e..d314e7bac 100644 --- a/plugins/dde-dock/bluetooth/componments/device.cpp +++ b/plugins/dde-dock/bluetooth/componments/device.cpp @@ -34,6 +34,7 @@ Device::Device(QObject *parent) , m_rssi(0) , m_state(StateUnavailable) , m_connectState(false) + , m_battery(0) { } @@ -101,6 +102,14 @@ void Device::setRssi(int rssi) } } +void Device::setBattery(const int battery) +{ + if (m_battery != battery) { + m_battery = battery; + Q_EMIT batteryChanged(m_battery); + } +} + void Device::setDeviceType(const QString &deviceType) { m_deviceType = deviceType2Icon[deviceType]; diff --git a/plugins/dde-dock/bluetooth/componments/device.h b/plugins/dde-dock/bluetooth/componments/device.h index 33c23cae8..6d1511be4 100644 --- a/plugins/dde-dock/bluetooth/componments/device.h +++ b/plugins/dde-dock/bluetooth/componments/device.h @@ -55,6 +55,9 @@ class Device : public QObject inline void setAdapterId(const QString &id) { m_adapterId = id; } inline const QString &getAdapterId() const { return m_adapterId; } + inline int battery() const { return m_battery; } + void setBattery(const int battery); + inline QString deviceType() const { return m_deviceType; } void setDeviceType(const QString &deviceType); @@ -65,6 +68,7 @@ class Device : public QObject void stateChanged(const State state) const; void connectStateChanged(const bool connectState) const; void rssiChanged(const int rssi) const; + void batteryChanged(const int battery) const; private: QString m_id; @@ -76,6 +80,7 @@ class Device : public QObject int m_rssi; State m_state; bool m_connectState; + int m_battery; QString m_adapterId; QString m_deviceType; }; diff --git a/plugins/dde-dock/bluetooth/resources/bluetooth.qrc b/plugins/dde-dock/bluetooth/resources/bluetooth.qrc index 180ef6bf7..333b1277e 100644 --- a/plugins/dde-dock/bluetooth/resources/bluetooth.qrc +++ b/plugins/dde-dock/bluetooth/resources/bluetooth.qrc @@ -79,5 +79,27 @@ texts/plugin_item_select_16px.svg texts/refresh_16px.svg texts/arrow-up_12px.svg + icons/battery-000-symbolic.svg + icons/battery-010-symbolic.svg + icons/battery-020-symbolic.svg + icons/battery-030-symbolic.svg + icons/battery-040-symbolic.svg + icons/battery-050-symbolic.svg + icons/battery-060-symbolic.svg + icons/battery-070-symbolic.svg + icons/battery-080-symbolic.svg + icons/battery-090-symbolic.svg + icons/battery-100-symbolic.svg + icons/battery-000-symbolic-dark.svg + icons/battery-010-symbolic-dark.svg + icons/battery-020-symbolic-dark.svg + icons/battery-030-symbolic-dark.svg + icons/battery-040-symbolic-dark.svg + icons/battery-050-symbolic-dark.svg + icons/battery-060-symbolic-dark.svg + icons/battery-070-symbolic-dark.svg + icons/battery-080-symbolic-dark.svg + icons/battery-090-symbolic-dark.svg + icons/battery-100-symbolic-dark.svg diff --git a/plugins/dde-dock/bluetooth/resources/icons/battery-000-symbolic-dark.svg b/plugins/dde-dock/bluetooth/resources/icons/battery-000-symbolic-dark.svg new file mode 100644 index 000000000..354e73513 --- /dev/null +++ b/plugins/dde-dock/bluetooth/resources/icons/battery-000-symbolic-dark.svg @@ -0,0 +1,7 @@ + + + battery-000-symbolic-dark + + + + \ No newline at end of file diff --git a/plugins/dde-dock/bluetooth/resources/icons/battery-000-symbolic.svg b/plugins/dde-dock/bluetooth/resources/icons/battery-000-symbolic.svg new file mode 100644 index 000000000..b62bd0a53 --- /dev/null +++ b/plugins/dde-dock/bluetooth/resources/icons/battery-000-symbolic.svg @@ -0,0 +1,7 @@ + + + battery-000-symbolic + + + + \ No newline at end of file diff --git a/plugins/dde-dock/bluetooth/resources/icons/battery-010-symbolic-dark.svg b/plugins/dde-dock/bluetooth/resources/icons/battery-010-symbolic-dark.svg new file mode 100644 index 000000000..827b2b4e8 --- /dev/null +++ b/plugins/dde-dock/bluetooth/resources/icons/battery-010-symbolic-dark.svg @@ -0,0 +1,8 @@ + + + battery-010-symbolic-dark + + + + + \ No newline at end of file diff --git a/plugins/dde-dock/bluetooth/resources/icons/battery-010-symbolic.svg b/plugins/dde-dock/bluetooth/resources/icons/battery-010-symbolic.svg new file mode 100644 index 000000000..2d31d6215 --- /dev/null +++ b/plugins/dde-dock/bluetooth/resources/icons/battery-010-symbolic.svg @@ -0,0 +1,8 @@ + + + battery-010-symbolic + + + + + \ No newline at end of file diff --git a/plugins/dde-dock/bluetooth/resources/icons/battery-020-symbolic-dark.svg b/plugins/dde-dock/bluetooth/resources/icons/battery-020-symbolic-dark.svg new file mode 100644 index 000000000..086c167d0 --- /dev/null +++ b/plugins/dde-dock/bluetooth/resources/icons/battery-020-symbolic-dark.svg @@ -0,0 +1,8 @@ + + + battery-020-symbolic-dark + + + + + \ No newline at end of file diff --git a/plugins/dde-dock/bluetooth/resources/icons/battery-020-symbolic.svg b/plugins/dde-dock/bluetooth/resources/icons/battery-020-symbolic.svg new file mode 100644 index 000000000..ab74252b3 --- /dev/null +++ b/plugins/dde-dock/bluetooth/resources/icons/battery-020-symbolic.svg @@ -0,0 +1,8 @@ + + + battery-020-symbolic + + + + + \ No newline at end of file diff --git a/plugins/dde-dock/bluetooth/resources/icons/battery-030-symbolic-dark.svg b/plugins/dde-dock/bluetooth/resources/icons/battery-030-symbolic-dark.svg new file mode 100644 index 000000000..98b618d83 --- /dev/null +++ b/plugins/dde-dock/bluetooth/resources/icons/battery-030-symbolic-dark.svg @@ -0,0 +1,8 @@ + + + battery-030-symbolic-dark + + + + + \ No newline at end of file diff --git a/plugins/dde-dock/bluetooth/resources/icons/battery-030-symbolic.svg b/plugins/dde-dock/bluetooth/resources/icons/battery-030-symbolic.svg new file mode 100644 index 000000000..d1516da11 --- /dev/null +++ b/plugins/dde-dock/bluetooth/resources/icons/battery-030-symbolic.svg @@ -0,0 +1,8 @@ + + + battery-030-symbolic + + + + + \ No newline at end of file diff --git a/plugins/dde-dock/bluetooth/resources/icons/battery-040-symbolic-dark.svg b/plugins/dde-dock/bluetooth/resources/icons/battery-040-symbolic-dark.svg new file mode 100644 index 000000000..3275cad66 --- /dev/null +++ b/plugins/dde-dock/bluetooth/resources/icons/battery-040-symbolic-dark.svg @@ -0,0 +1,8 @@ + + + battery-040-symbolic-dark + + + + + \ No newline at end of file diff --git a/plugins/dde-dock/bluetooth/resources/icons/battery-040-symbolic.svg b/plugins/dde-dock/bluetooth/resources/icons/battery-040-symbolic.svg new file mode 100644 index 000000000..1339d6ec9 --- /dev/null +++ b/plugins/dde-dock/bluetooth/resources/icons/battery-040-symbolic.svg @@ -0,0 +1,8 @@ + + + battery-040-symbolic + + + + + \ No newline at end of file diff --git a/plugins/dde-dock/bluetooth/resources/icons/battery-050-symbolic-dark.svg b/plugins/dde-dock/bluetooth/resources/icons/battery-050-symbolic-dark.svg new file mode 100644 index 000000000..e50c8cec8 --- /dev/null +++ b/plugins/dde-dock/bluetooth/resources/icons/battery-050-symbolic-dark.svg @@ -0,0 +1,8 @@ + + + battery-050-symbolic-dark + + + + + \ No newline at end of file diff --git a/plugins/dde-dock/bluetooth/resources/icons/battery-050-symbolic.svg b/plugins/dde-dock/bluetooth/resources/icons/battery-050-symbolic.svg new file mode 100644 index 000000000..b2f9a9c51 --- /dev/null +++ b/plugins/dde-dock/bluetooth/resources/icons/battery-050-symbolic.svg @@ -0,0 +1,8 @@ + + + battery-050-symbolic + + + + + \ No newline at end of file diff --git a/plugins/dde-dock/bluetooth/resources/icons/battery-060-symbolic-dark.svg b/plugins/dde-dock/bluetooth/resources/icons/battery-060-symbolic-dark.svg new file mode 100644 index 000000000..4545cc938 --- /dev/null +++ b/plugins/dde-dock/bluetooth/resources/icons/battery-060-symbolic-dark.svg @@ -0,0 +1,8 @@ + + + battery-060-symbolic-dark + + + + + \ No newline at end of file diff --git a/plugins/dde-dock/bluetooth/resources/icons/battery-060-symbolic.svg b/plugins/dde-dock/bluetooth/resources/icons/battery-060-symbolic.svg new file mode 100644 index 000000000..0ad8b8928 --- /dev/null +++ b/plugins/dde-dock/bluetooth/resources/icons/battery-060-symbolic.svg @@ -0,0 +1,8 @@ + + + battery-060-symbolic + + + + + \ No newline at end of file diff --git a/plugins/dde-dock/bluetooth/resources/icons/battery-070-symbolic-dark.svg b/plugins/dde-dock/bluetooth/resources/icons/battery-070-symbolic-dark.svg new file mode 100644 index 000000000..0ed6616d3 --- /dev/null +++ b/plugins/dde-dock/bluetooth/resources/icons/battery-070-symbolic-dark.svg @@ -0,0 +1,8 @@ + + + battery-070-symbolic-dark + + + + + \ No newline at end of file diff --git a/plugins/dde-dock/bluetooth/resources/icons/battery-070-symbolic.svg b/plugins/dde-dock/bluetooth/resources/icons/battery-070-symbolic.svg new file mode 100644 index 000000000..01bb8cd40 --- /dev/null +++ b/plugins/dde-dock/bluetooth/resources/icons/battery-070-symbolic.svg @@ -0,0 +1,8 @@ + + + battery-070-symbolic + + + + + \ No newline at end of file diff --git a/plugins/dde-dock/bluetooth/resources/icons/battery-080-symbolic-dark.svg b/plugins/dde-dock/bluetooth/resources/icons/battery-080-symbolic-dark.svg new file mode 100644 index 000000000..1785b640e --- /dev/null +++ b/plugins/dde-dock/bluetooth/resources/icons/battery-080-symbolic-dark.svg @@ -0,0 +1,8 @@ + + + battery-080-symbolic-dark + + + + + \ No newline at end of file diff --git a/plugins/dde-dock/bluetooth/resources/icons/battery-080-symbolic.svg b/plugins/dde-dock/bluetooth/resources/icons/battery-080-symbolic.svg new file mode 100644 index 000000000..e6d35d3a2 --- /dev/null +++ b/plugins/dde-dock/bluetooth/resources/icons/battery-080-symbolic.svg @@ -0,0 +1,8 @@ + + + battery-080-symbolic + + + + + \ No newline at end of file diff --git a/plugins/dde-dock/bluetooth/resources/icons/battery-090-symbolic-dark.svg b/plugins/dde-dock/bluetooth/resources/icons/battery-090-symbolic-dark.svg new file mode 100644 index 000000000..fac3d8458 --- /dev/null +++ b/plugins/dde-dock/bluetooth/resources/icons/battery-090-symbolic-dark.svg @@ -0,0 +1,8 @@ + + + battery-090-symbolic-dark + + + + + \ No newline at end of file diff --git a/plugins/dde-dock/bluetooth/resources/icons/battery-090-symbolic.svg b/plugins/dde-dock/bluetooth/resources/icons/battery-090-symbolic.svg new file mode 100644 index 000000000..ea3c0028c --- /dev/null +++ b/plugins/dde-dock/bluetooth/resources/icons/battery-090-symbolic.svg @@ -0,0 +1,8 @@ + + + battery-090-symbolic + + + + + \ No newline at end of file diff --git a/plugins/dde-dock/bluetooth/resources/icons/battery-100-symbolic-dark.svg b/plugins/dde-dock/bluetooth/resources/icons/battery-100-symbolic-dark.svg new file mode 100644 index 000000000..64beb419e --- /dev/null +++ b/plugins/dde-dock/bluetooth/resources/icons/battery-100-symbolic-dark.svg @@ -0,0 +1,8 @@ + + + battery-100-symbolic-dark + + + + + \ No newline at end of file diff --git a/plugins/dde-dock/bluetooth/resources/icons/battery-100-symbolic.svg b/plugins/dde-dock/bluetooth/resources/icons/battery-100-symbolic.svg new file mode 100644 index 000000000..460139c35 --- /dev/null +++ b/plugins/dde-dock/bluetooth/resources/icons/battery-100-symbolic.svg @@ -0,0 +1,8 @@ + + + battery-100-symbolic + + + + + \ No newline at end of file diff --git a/plugins/dde-dock/common/pluginitemdelegate.cpp b/plugins/dde-dock/common/pluginitemdelegate.cpp index 75b241e00..ad91f7e65 100644 --- a/plugins/dde-dock/common/pluginitemdelegate.cpp +++ b/plugins/dde-dock/common/pluginitemdelegate.cpp @@ -8,7 +8,9 @@ #include #include #include +#include +#include #include #include #include @@ -183,6 +185,30 @@ void PluginStandardItem::updateState(const PluginItemState state) } } +void PluginStandardItem::updateBattery(const int battery) +{ + if (m_battery != battery) { + m_battery = battery; + Q_EMIT batteryChanged(m_battery); + } +} + +static QString batteryIconName(int battery) +{ + if (battery <= 5) return "battery-000-symbolic"; + if (battery <= 10) return "battery-010-symbolic"; + if (battery <= 20) return "battery-020-symbolic"; + if (battery <= 30) return "battery-030-symbolic"; + if (battery <= 40) return "battery-040-symbolic"; + if (battery <= 50) return "battery-050-symbolic"; + if (battery <= 60) return "battery-060-symbolic"; + if (battery <= 70) return "battery-070-symbolic"; + if (battery <= 80) return "battery-080-symbolic"; + if (battery <= 90) return "battery-090-symbolic"; + if (battery <= 100) return "battery-100-symbolic"; + return {}; +} + PluginItemWidget::PluginItemWidget(PluginStandardItem *item, QWidget *parent) : QWidget(parent) , m_item(item) @@ -229,12 +255,29 @@ PluginItemWidget::PluginItemWidget(PluginStandardItem *item, QWidget *parent) m_spinner->hide(); m_spinner->stop(); + m_batteryPercent = new DLabel(this); + m_batteryPercent->setFont(DFontSizeManager::instance()->get(DFontSizeManager::T8)); + auto *percentOpacityEffect = new QGraphicsOpacityEffect(m_batteryPercent); + percentOpacityEffect->setOpacity(0.5); + m_batteryPercent->setGraphicsEffect(percentOpacityEffect); + m_batteryPercent->hide(); + + m_batteryIcon = new DLabel(this); + m_batteryIcon->setFixedSize(16, 16); + auto *opacityEffect = new QGraphicsOpacityEffect(m_batteryIcon); + opacityEffect->setOpacity(0.5); + m_batteryIcon->setGraphicsEffect(opacityEffect); + m_batteryIcon->hide(); + m_mainLayout->setContentsMargins(10, 0, 10, 0); m_mainLayout->setSpacing(0); m_mainLayout->addWidget(m_iconBtn, 0); m_mainLayout->addSpacing(10); m_mainLayout->addWidget(m_nameLabel, 1); m_mainLayout->addStretch(); + m_mainLayout->addWidget(m_batteryPercent, 0, Qt::AlignRight | Qt::AlignVCenter); + m_mainLayout->addSpacing(3); + m_mainLayout->addWidget(m_batteryIcon, 0, Qt::AlignRight | Qt::AlignVCenter); m_mainLayout->addSpacerItem(m_rightIconSpacerItem); m_mainLayout->addWidget(m_connBtn, 0, Qt::AlignRight | Qt::AlignVCenter); m_mainLayout->addWidget(m_disConnectBtn, 0, Qt::AlignRight | Qt::AlignVCenter); @@ -247,6 +290,10 @@ PluginItemWidget::PluginItemWidget(PluginStandardItem *item, QWidget *parent) connect(m_item, &PluginStandardItem::iconChanged, this, &PluginItemWidget::updateIcon); connect(m_item, &PluginStandardItem::nameChanged, this, &PluginItemWidget::updateName); connect(m_item, &PluginStandardItem::stateChanged, this, &PluginItemWidget::updateState); + connect(m_item, &PluginStandardItem::batteryChanged, this, &PluginItemWidget::updateBatteryDisplay); + connect(DGuiApplicationHelper::instance(), &DGuiApplicationHelper::themeTypeChanged, this, [this]() { + updateBatteryDisplay(m_item ? m_item->battery() : -1); + }); connect(m_disConnectBtn, &CommonTextButton::clicked, m_item, &PluginStandardItem::connectBtnClicked); } @@ -303,9 +350,28 @@ void PluginItemWidget::updateState(const PluginItemState state) break; } + updateBatteryDisplay(m_item ? m_item->battery() : -1); + m_mainLayout->invalidate(); } +void PluginItemWidget::updateBatteryDisplay(int battery) +{ + if (!underMouse() && m_state == PluginItemState::Connected && battery > 0) { + QString iconName = batteryIconName(battery); + if (DGuiApplicationHelper::instance()->themeType() == DGuiApplicationHelper::LightType) { + iconName += "-dark"; + } + m_batteryIcon->setPixmap(QIcon::fromTheme(iconName).pixmap(16, 16)); + m_batteryPercent->setText(QString("%1%").arg(battery)); + m_batteryIcon->show(); + m_batteryPercent->show(); + } else { + m_batteryIcon->hide(); + m_batteryPercent->hide(); + } +} + bool PluginItemWidget::event(QEvent *e) { switch (e->type()) { @@ -329,6 +395,7 @@ void PluginItemWidget::enterEvent(QEnterEvent *event) if (m_state == PluginItemState::Connected) { m_disConnectBtn->setVisible(true); m_connBtn->setVisible(false); + updateBatteryDisplay(m_item ? m_item->battery() : -1); } QWidget::enterEvent(event); } @@ -338,6 +405,7 @@ void PluginItemWidget::leaveEvent(QEvent *event) m_disConnectBtn->setVisible(false); if (m_state == PluginItemState::Connected) { m_connBtn->setVisible(true); - }; + updateBatteryDisplay(m_item ? m_item->battery() : -1); + } QWidget::leaveEvent(event); } diff --git a/plugins/dde-dock/common/pluginitemdelegate.h b/plugins/dde-dock/common/pluginitemdelegate.h index 8691f8671..365949a15 100644 --- a/plugins/dde-dock/common/pluginitemdelegate.h +++ b/plugins/dde-dock/common/pluginitemdelegate.h @@ -89,18 +89,22 @@ class PluginStandardItem : public QObject, public QStandardItem QString name() const { return m_name; } void updateState(const PluginItemState state); + void updateBattery(const int battery); PluginItemState state() const { return m_state; } + int battery() const { return m_battery; } Q_SIGNALS: void iconChanged(const QIcon &icon); void nameChanged(const QString &name); void stateChanged(const PluginItemState state); + void batteryChanged(const int battery); void connectBtnClicked(); private: QIcon m_icon; QString m_name; PluginItemState m_state; + int m_battery = -1; }; class PluginItemWidget : public QWidget @@ -114,6 +118,7 @@ public Q_SLOTS: void updateIcon(const QIcon &icon); void updateName(const QString &name); void updateState(const PluginItemState state); + void updateBatteryDisplay(int battery); protected: bool event(QEvent *e) override; @@ -131,4 +136,6 @@ public Q_SLOTS: DSpinner *m_spinner; QSpacerItem *m_rightIconSpacerItem; PluginItemState m_state; + DLabel *m_batteryIcon; + DLabel *m_batteryPercent; };