diff --git a/Modules/energy/batterymodel.h b/Modules/energy/batterymodel.h --- a/Modules/energy/batterymodel.h +++ b/Modules/energy/batterymodel.h @@ -38,11 +38,11 @@ enum Roles { BatteryRole = Qt::UserRole, - UdiRole + UdiRole, + VendorRole, + ProductRole }; - - Q_INVOKABLE Solid::Battery *get(int index) const; - Q_INVOKABLE QString udi(int index) const; + Q_ENUM(Roles) QVariant data(const QModelIndex &index, int role) const override; int rowCount(const QModelIndex &parent = QModelIndex()) const override; diff --git a/Modules/energy/batterymodel.cpp b/Modules/energy/batterymodel.cpp --- a/Modules/energy/batterymodel.cpp +++ b/Modules/energy/batterymodel.cpp @@ -65,35 +65,20 @@ }); } -Solid::Battery *BatteryModel::get(int index) const -{ - if (index < 0 || index >= m_batteries.count()) { - return nullptr; - } - - Solid::Battery* battery = m_batteries.value(index).as(); - - QQmlEngine::setObjectOwnership(battery, QQmlEngine::CppOwnership); - return battery; -} - -QString BatteryModel::udi(int index) const -{ - if (index < 0 || index >= m_batteries.count()) { - return QString(); - } - - return m_batteries.at(index).udi(); -} - QVariant BatteryModel::data(const QModelIndex &index, int role) const { if (index.row() < 0 || index.row() >= m_batteries.count()) { return QVariant(); } if (role == BatteryRole) { return QVariant::fromValue(m_batteries.value(index.row()).as()); + } else if (role == ProductRole) { + const Solid::Device device = m_batteries.value(index.row()); + return device.product(); + } else if (role == VendorRole) { + const Solid::Device device = m_batteries.value(index.row()); + return device.vendor(); } else if (role == UdiRole) { return m_batteries.at(index.row()).udi(); } @@ -111,6 +96,8 @@ { return { {BatteryRole, "battery"}, + {VendorRole, "vendor"}, + {ProductRole, "product"}, {UdiRole, "udi"} }; } diff --git a/Modules/energy/kcm.cpp b/Modules/energy/kcm.cpp --- a/Modules/energy/kcm.cpp +++ b/Modules/energy/kcm.cpp @@ -51,6 +51,8 @@ qmlRegisterType(); qmlRegisterType(); qmlRegisterType("org.kde.kinfocenter.energy.private", 1, 0, "HistoryModel"); + qmlRegisterUncreatableType("org.kde.kinfocenter.energy.private", 1, 0, "BatteryModel", + QStringLiteral("Use BatteryModel")); KAboutData *about = new KAboutData(QStringLiteral("kcm_energyinfo"), i18n("Energy Consumption Statistics"), QStringLiteral("0.2"), QString(), KAboutLicense::GPL); diff --git a/Modules/energy/package/contents/ui/main.qml b/Modules/energy/package/contents/ui/main.qml --- a/Modules/energy/package/contents/ui/main.qml +++ b/Modules/energy/package/contents/ui/main.qml @@ -37,12 +37,22 @@ property QtObject currentBattery: null property string currentUdi: "" + property string currentVendor: "" + property string currentProduct: "" property bool compact: (root.width / units.gridUnit) < 25 + function initCurrentBattery() { + root.currentBattery = kcm.batteries.data(kcm.batteries.index(0, 0), BatteryModel.BatteryRole) + root.currentVendor = kcm.batteries.data(kcm.batteries.index(0, 0), BatteryModel.VendorRole) + root.currentProduct = kcm.batteries.data(kcm.batteries.index(0, 0), BatteryModel.ProductRole) + root.currentUdi = kcm.batteries.data(kcm.batteries.index(0, 0), BatteryModel.UdiRole) + } + + Component.onCompleted: initCurrentBattery() + onCurrentBatteryChanged: { if (!currentBattery) { - currentBattery = kcm.batteries.get(0) - currentUdi = kcm.batteries.udi(0) + initCurrentBattery() } } @@ -55,7 +65,11 @@ data: [ {label: i18n("Rechargeable"), value: "rechargeable"}, {label: i18n("Charge state"), value: "chargeState", modifier: "chargeState"}, - {label: i18n("Capacity degradation"), value: "capacity", unit: i18n("%"), precision: 0} + {label: i18n("Current charge"), value: "chargePercent", unit: i18n("%"), precision: 0}, + {label: i18n("Capacity degradation"), value: "capacity", unit: i18n("%"), precision: 0}, + {label: i18n("Vendor"), value: "vendor", source:"Vendor"}, + {label: i18n("Model"), value: "model", source:"Product"}, + {label: i18n("Serial Number"), value: "serial"} ] }, { @@ -74,14 +88,6 @@ {label: i18n("Temperature"), value: "temperature", unit: i18nc("Degree Celsius", "°C"), precision: 2} ] }, - { - title: i18n("Manufacturer"), - data: [ - {label: i18n("Vendor"), value: "vendor"}, - {label: i18n("Model"), value: "model"}, - {label: i18n("Serial Number"), value: "serial"} - ] - } ] function modifier_chargeState(value) { @@ -93,11 +99,6 @@ } } - Component.onCompleted: { - currentBattery = kcm.batteries.get(0) - currentUdi = kcm.batteries.udi(0) - } - implicitWidth: units.gridUnit * 30 implicitHeight: !!currentBattery ? units.gridUnit * 30 : units.gridUnit * 12 @@ -126,7 +127,10 @@ checkable: true onClicked: { root.currentUdi = model.udi + root.currentVendor = model.vendor + root.currentProduct = model.product root.currentBattery = model.battery + // override checked property checked = Qt.binding(function() { return model.battery == root.currentBattery @@ -422,7 +426,12 @@ id: valueLabel Kirigami.FormData.label: i18n("%1:", modelData.label) text: { - var value = currentBattery[modelData.value] + var value; + if (modelData.source) { + value = root["current" + modelData.source]; + } else { + value = currentBattery[modelData.value] + } // There's no "degradation" value we can look up, so // instead, process the capacity value to produce it