[OpenDocString] kdeconnect-kde (cpp)
battery_action.cpp
BatteryAction::BatteryAction(DeviceDbusInterface *device)
    : QAction(nullptr)
    , m_batteryIface(device->id())
{
    setCharge(m_batteryIface.charge());
    setCharging(m_batteryIface.isCharging());

    connect(&m_batteryIface, &BatteryDbusInterface::refreshedProxy, this, [this] {
        setCharge(m_batteryIface.charge());
        setCharging(m_batteryIface.isCharging());
    });

    BatteryAction::update();
}
This constructs a battery action object from an existing device and a battery interface. It connects to the battery interface and updates the battery status.
void BatteryAction::update()
{
    if (m_charge < 0)
        setText(i18n("No Battery"));
    else if (m_charging)
        setText(i18n("Battery: %1% (Charging)", m_charge));
    else
        setText(i18n("Battery: %1%", m_charge));

    // set icon name
    QString iconName = QStringLiteral("battery");
    if (m_charge < 0) {
        iconName += QStringLiteral("-missing");
    } else {
        int val = int(m_charge / 10) * 10;
        QString numberPaddedString = QStringLiteral("%1").arg(val, 3, 10, QLatin1Char('0'));
        iconName += QStringLiteral("-") + numberPaddedString;
    }

    if (m_charging) {
        iconName += QStringLiteral("-charging");
    }

    setIcon(QIcon::fromTheme(iconName));
}
Sets the text and icon name by reading the battery value. It first sets the text and icon name, and adds a number of 10% argument to the battery icon.
void BatteryAction::setCharge(int charge)
{
    m_charge = charge;
    update();
}
Sets the charge value by setting the internal value and updates the internal state.
void BatteryAction::setCharging(bool charging)
{
    m_charging = charging;
    update();
}
Sets the charging state of the action.