| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | // Copyright 2019 Juan Palacios <[email protected]> |
| 3 | |
| 4 | #include "cpuqmlitem.h" |
| 5 | |
| 6 | #include "core/info/icpuinfo.h" |
| 7 | #include "core/iqmlcomponentfactory.h" |
| 8 | #include "core/qmlcomponentregistry.h" |
| 9 | #include "icpu.h" |
| 10 | #include "sensors/graphitem.h" |
| 11 | #include "sensors/isensor.h" |
| 12 | #include <QObject> |
| 13 | #include <QQmlApplicationEngine> |
| 14 | #include <QQmlComponent> |
| 15 | #include <QQmlEngine> |
| 16 | #include <QQuickItem> |
| 17 | #include <QString> |
| 18 | #include <Qt> |
| 19 | #include <memory> |
| 20 | #include <utility> |
| 21 | |
| 22 | class CPUQMLItem::Initializer final |
| 23 | : public QMLItem::Initializer |
| 24 | , public ICPU::Exporter |
| 25 | { |
| 26 | public: |
| 27 | Initializer(IQMLComponentFactory const &qmlComponentFactory, |
| 28 | QQmlApplicationEngine &qmlEngine, CPUQMLItem &qmlItem) noexcept |
| 29 | : QMLItem::Initializer(qmlComponentFactory, qmlEngine) |
| 30 | , outer_(qmlItem) |
| 31 | { |
| 32 | } |
| 33 | |
| 34 | std::optional<std::reference_wrapper<Exportable::Exporter>> |
| 35 | provideExporter(Item const &i) override; |
| 36 | |
| 37 | void takeActive(bool active) override; |
| 38 | void takeInfo(ICPUInfo const &info) override; |
| 39 | void takeSensor(ISensor const &sensor) override; |
| 40 | |
| 41 | private: |
| 42 | CPUQMLItem &outer_; |
| 43 | }; |
| 44 | |
| 45 | std::optional<std::reference_wrapper<Exportable::Exporter>> |
| 46 | CPUQMLItem::Initializer::provideExporter(Item const &i) |
| 47 | { |
| 48 | return initializer(i.ID(), &outer_).first; |
| 49 | } |
| 50 | |
| 51 | void CPUQMLItem::Initializer::takeActive(bool active) |
| 52 | { |
| 53 | outer_.takeActive(active); |
| 54 | } |
| 55 | |
| 56 | void CPUQMLItem::Initializer::takeInfo(ICPUInfo const &info) |
| 57 | { |
| 58 | auto name(info.info(ICPUInfo::Keys::modelName)); |
| 59 | if (!name.empty()) |
| 60 | name.append("\n"); |
| 61 | name.append("[CPU ").append(std::to_string(info.physicalId())).append("]"); |
| 62 | outer_.setName(QString::fromStdString(name)); |
| 63 | |
| 64 | outer_.takePhysicalId(info.physicalId()); |
| 65 | } |
| 66 | |
| 67 | void CPUQMLItem::Initializer::takeSensor(ISensor const &sensor) |
| 68 | { |
| 69 | auto graphItem = dynamic_cast<GraphItem *>( |
| 70 | qmlComponentFactory_.createQuickItem(sensor.ID(), &outer_, "SensorGraph")); |
| 71 | if (graphItem != nullptr) { |
| 72 | connect(graphItem, &GraphItem::settingsChanged, &outer_, |
| 73 | &QMLItem::settingsChanged, Qt::UniqueConnection); |
| 74 | graphItem->init(&sensor); |
| 75 | emit outer_.newGraphItem(graphItem); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | void CPUQMLItem::activate(bool active) |
| 80 | { |
| 81 | takeActive(active); |
| 82 | } |
| 83 | |
| 84 | std::optional<std::reference_wrapper<Importable::Importer>> |
| 85 | CPUQMLItem::provideImporter(Item const &i) |
| 86 | { |
| 87 | auto item = this->findChild<QQuickItem *>(QString::fromStdString(i.ID())); |
| 88 | if (item == nullptr) |
| 89 | return {}; |
| 90 | |
| 91 | return dynamic_cast<Importable::Importer &>(*item); |
| 92 | } |
| 93 | |
| 94 | std::optional<std::reference_wrapper<Exportable::Exporter>> |
| 95 | CPUQMLItem::provideExporter(Item const &i) |
| 96 | { |
| 97 | auto item = this->findChild<QQuickItem *>(QString::fromStdString(i.ID())); |
| 98 | if (item == nullptr) |
| 99 | return {}; |
| 100 | |
| 101 | return dynamic_cast<Exportable::Exporter &>(*item); |
| 102 | } |
| 103 | |
| 104 | bool CPUQMLItem::provideActive() const |
| 105 | { |
| 106 | return active_; |
| 107 | } |
| 108 | |
| 109 | int CPUQMLItem::providePhysicalId() const |
| 110 | { |
| 111 | return physicalId_; |
| 112 | } |
| 113 | |
| 114 | void CPUQMLItem::takeActive(bool active) |
| 115 | { |
| 116 | active_ = active; |
| 117 | setEnabled(active); |
| 118 | } |
| 119 | |
| 120 | void CPUQMLItem::takePhysicalId(int id) |
| 121 | { |
| 122 | physicalId_ = id; |
| 123 | } |
| 124 | |
| 125 | std::unique_ptr<Exportable::Exporter> |
| 126 | CPUQMLItem::initializer(IQMLComponentFactory const &qmlComponentFactory, |
| 127 | QQmlApplicationEngine &qmlEngine) |
| 128 | { |
| 129 | return std::make_unique<CPUQMLItem::Initializer>(qmlComponentFactory, |
| 130 | qmlEngine, *this); |
| 131 | } |
| 132 | |
| 133 | bool CPUQMLItem::register_() |
| 134 | { |
| 135 | QMLComponentRegistry::addQMLTypeRegisterer([]() { |
| 136 | qmlRegisterType<CPUQMLItem>("CoreCtrl.UIComponents", 1, 0, |
| 137 | ICPU::ItemID.data()); |
| 138 | }); |
| 139 | |
| 140 | QMLComponentRegistry::addQMLItemProvider( |
| 141 | ICPU::ItemID, [](QQmlApplicationEngine &engine) { |
| 142 | QQmlComponent component(&engine, |
| 143 | QStringLiteral("qrc:/qml/CPUForm.qml")); |
| 144 | return qobject_cast<QMLItem *>(component.create()); |
| 145 | }); |
| 146 | |
| 147 | return true; |
| 148 | } |
| 149 | |
| 150 | bool const CPUQMLItem::registered_ = CPUQMLItem::register_(); |