| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | // Copyright 2019 Juan Palacios <[email protected]> |
| 3 | |
| 4 | #include "gpuqmlitem.h" |
| 5 | |
| 6 | #include "core/info/igpuinfo.h" |
| 7 | #include "core/iqmlcomponentfactory.h" |
| 8 | #include "core/qmlcomponentregistry.h" |
| 9 | #include "igpu.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 GPUQMLItem::Initializer final |
| 23 | : public QMLItem::Initializer |
| 24 | , public IGPU::Exporter |
| 25 | { |
| 26 | public: |
| 27 | Initializer(IQMLComponentFactory const &qmlComponentFactory, |
| 28 | QQmlApplicationEngine &qmlEngine, GPUQMLItem &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(IGPUInfo const &info) override; |
| 39 | void takeSensor(ISensor const &sensor) override; |
| 40 | |
| 41 | private: |
| 42 | GPUQMLItem &outer_; |
| 43 | }; |
| 44 | |
| 45 | std::optional<std::reference_wrapper<Exportable::Exporter>> |
| 46 | GPUQMLItem::Initializer::provideExporter(Item const &i) |
| 47 | { |
| 48 | return initializer(i.ID(), &outer_).first; |
| 49 | } |
| 50 | |
| 51 | void GPUQMLItem::Initializer::takeActive(bool active) |
| 52 | { |
| 53 | outer_.takeActive(active); |
| 54 | } |
| 55 | |
| 56 | void GPUQMLItem::Initializer::takeInfo(IGPUInfo const &info) |
| 57 | { |
| 58 | auto name(info.info(IGPUInfo::Keys::subdeviceName)); |
| 59 | if (!name.empty()) |
| 60 | name.append("\n"); |
| 61 | name.append("[GPU ").append(std::to_string(info.index())).append("]"); |
| 62 | outer_.setName(QString::fromStdString(name)); |
| 63 | |
| 64 | outer_.takeIndex(info.index()); |
| 65 | } |
| 66 | |
| 67 | void GPUQMLItem::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 GPUQMLItem::activate(bool active) |
| 80 | { |
| 81 | takeActive(active); |
| 82 | } |
| 83 | |
| 84 | std::optional<std::reference_wrapper<Importable::Importer>> |
| 85 | GPUQMLItem::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 | GPUQMLItem::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 GPUQMLItem::provideActive() const |
| 105 | { |
| 106 | return active_; |
| 107 | } |
| 108 | |
| 109 | int GPUQMLItem::provideIndex() const |
| 110 | { |
| 111 | return index_; |
| 112 | } |
| 113 | |
| 114 | std::string const &GPUQMLItem::provideDeviceID() const |
| 115 | { |
| 116 | return deviceID_; |
| 117 | } |
| 118 | |
| 119 | std::string const &GPUQMLItem::provideRevision() const |
| 120 | { |
| 121 | return revision_; |
| 122 | } |
| 123 | |
| 124 | std::optional<std::string> GPUQMLItem::provideUniqueID() const |
| 125 | { |
| 126 | return uniqueID_; |
| 127 | } |
| 128 | |
| 129 | void GPUQMLItem::takeActive(bool active) |
| 130 | { |
| 131 | active_ = active; |
| 132 | setEnabled(active); |
| 133 | } |
| 134 | |
| 135 | void GPUQMLItem::takeIndex(int index) |
| 136 | { |
| 137 | index_ = index; |
| 138 | } |
| 139 | |
| 140 | void GPUQMLItem::takeDeviceID(std::string const &deviceID) |
| 141 | { |
| 142 | deviceID_ = deviceID; |
| 143 | } |
| 144 | |
| 145 | void GPUQMLItem::takeRevision(std::string const &revision) |
| 146 | { |
| 147 | revision_ = revision; |
| 148 | } |
| 149 | |
| 150 | void GPUQMLItem::takeUniqueID(std::optional<std::string> uniqueID) |
| 151 | { |
| 152 | uniqueID_ = uniqueID; |
| 153 | } |
| 154 | |
| 155 | std::unique_ptr<Exportable::Exporter> |
| 156 | GPUQMLItem::initializer(IQMLComponentFactory const &qmlComponentFactory, |
| 157 | QQmlApplicationEngine &qmlEngine) |
| 158 | { |
| 159 | return std::make_unique<GPUQMLItem::Initializer>(qmlComponentFactory, |
| 160 | qmlEngine, *this); |
| 161 | } |
| 162 | |
| 163 | bool GPUQMLItem::register_() |
| 164 | { |
| 165 | QMLComponentRegistry::addQMLTypeRegisterer([]() { |
| 166 | qmlRegisterType<GPUQMLItem>("CoreCtrl.UIComponents", 1, 0, |
| 167 | IGPU::ItemID.data()); |
| 168 | }); |
| 169 | |
| 170 | QMLComponentRegistry::addQMLItemProvider( |
| 171 | IGPU::ItemID, [](QQmlApplicationEngine &engine) { |
| 172 | QQmlComponent component(&engine, |
| 173 | QStringLiteral("qrc:/qml/GPUForm.qml")); |
| 174 | return qobject_cast<QMLItem *>(component.create()); |
| 175 | }); |
| 176 | |
| 177 | return true; |
| 178 | } |
| 179 | |
| 180 | bool const GPUQMLItem::registered_ = GPUQMLItem::register_(); |