Skip to content

Commit

Permalink
WIP: support show battery percentage
Browse files Browse the repository at this point in the history
Log:
  • Loading branch information
Decodetalkers committed Sep 28, 2023
1 parent 2736c47 commit 6af087a
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/plugin-bluetooth/operation/bluetoothadapter.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
5 changes: 5 additions & 0 deletions src/plugin-bluetooth/operation/bluetoothdevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions src/plugin-bluetooth/operation/bluetoothworker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<BluetoothAdapter *>(result);
Expand All @@ -243,6 +244,7 @@ void BluetoothWorker::addDevice(const QString &json)
BluetoothDevice *device = const_cast<BluetoothDevice *>(result1);
if (!device)
device = new BluetoothDevice(adapter);
device->setBattery(battery);
adapter->inflateDevice(device, obj);
adapter->addDevice(device);
}
Expand Down
66 changes: 62 additions & 4 deletions src/plugin-bluetooth/window/bluetoothdevicemodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ struct BluetoothDeviceItemAction
{
const BluetoothDevice *device;
DViewItemAction *spinnerAction;
DViewItemAction *percentageText;
DViewItemAction *percentageIcon;
DViewItemAction *textAction;
DViewItemAction *spaceAction;
DViewItemAction *iconAction;
Expand All @@ -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<const void *>(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);
}
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions src/plugin-bluetooth/window/bluetoothdevicemodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 6af087a

Please sign in to comment.