diff --git a/Modules/energy/batterymodel.h b/Modules/energy/batterymodel.h --- a/Modules/energy/batterymodel.h +++ b/Modules/energy/batterymodel.h @@ -42,11 +42,7 @@ VendorRole, ProductRole }; - - Q_INVOKABLE Solid::Battery *get(int index) const; - Q_INVOKABLE QString vendor(int index) const; - Q_INVOKABLE QString product(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,45 +65,6 @@ }); } -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(); -} - -QString BatteryModel::vendor(int index) const { - - if (index < 0 || index >= m_batteries.count()) { - return QString(); - } - const Solid::Device device = m_batteries.value(index); - return device.vendor(); -} - -QString BatteryModel::product(int index) const { - - if (index < 0 || index >= m_batteries.count()) { - return QString(); - } - const Solid::Device device = m_batteries.value(index); - return device.product(); -} - QVariant BatteryModel::data(const QModelIndex &index, int role) const { if (index.row() < 0 || index.row() >= m_batteries.count()) { 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 @@ -41,12 +41,18 @@ property string currentProduct: "" property bool compact: (root.width / units.gridUnit) < 25 + function initCurrentBattery() { + currentBattery = kcm.batteries.data(kcm.batteries.index(0, 0), BatteryModel.BatteryRole) + currentVendor = kcm.batteries.data(kcm.batteries.index(0, 0), BatteryModel.VendorRole) + currentProduct = kcm.batteries.data(kcm.batteries.index(0, 0), BatteryModel.ProductRole) + currentUdi = kcm.batteries.data(kcm.batteries.index(0, 0), BatteryModel.UdiRole) + } + + Component.onCompleted: initCurrentBattery() + onCurrentBatteryChanged: { if (!currentBattery) { - currentBattery = kcm.batteries.battery(0) - currentVendor = kcm.batteries.vendor(0) - currentProduct = kcm.batteries.product(0) - currentUdi = kcm.batteries.udi(0) + initCurrentBattery() } } @@ -93,13 +99,6 @@ } } - Component.onCompleted: { - currentBattery = kcm.batteries.get(0) - currentVendor = kcm.batteries.vendor(0) - currentProduct = kcm.batteries.product(0) - currentUdi = kcm.batteries.udi(0) - } - implicitWidth: units.gridUnit * 30 implicitHeight: !!currentBattery ? units.gridUnit * 30 : units.gridUnit * 12