| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | // Copyright 2019 Juan Palacios <[email protected]> |
| 3 | |
| 4 | #include "sysmodelqmlitem.h" |
| 5 | |
| 6 | #include "components/igpu.h" |
| 7 | #include "components/igpuprofilepart.h" |
| 8 | #include "info/igpuinfo.h" |
| 9 | #include "iprofilepart.h" |
| 10 | #include "isyscomponent.h" |
| 11 | #include "isyscomponentprofilepart.h" |
| 12 | #include "isysmodel.h" |
| 13 | #include "item.h" |
| 14 | #include "qmlcomponentregistry.h" |
| 15 | #include <QQmlApplicationEngine> |
| 16 | #include <QQmlComponent> |
| 17 | #include <QString> |
| 18 | #include <QtQml> |
| 19 | #include <utility> |
| 20 | |
| 21 | class SysModelQMLItem::Initializer final |
| 22 | : public QMLItem::Initializer |
| 23 | , public ISysModel::Exporter |
| 24 | { |
| 25 | public: |
| 26 | Initializer(IQMLComponentFactory const &qmlComponentFactory, |
| 27 | QQmlApplicationEngine &qmlEngine, SysModelQMLItem &qmlItem) noexcept |
| 28 | : QMLItem::Initializer(qmlComponentFactory, qmlEngine) |
| 29 | , outer_(qmlItem) |
| 30 | { |
| 31 | } |
| 32 | |
| 33 | std::optional<std::reference_wrapper<Exportable::Exporter>> |
| 34 | provideExporter(Item const &i) override; |
| 35 | |
| 36 | private: |
| 37 | SysModelQMLItem &outer_; |
| 38 | }; |
| 39 | |
| 40 | std::optional<std::reference_wrapper<Exportable::Exporter>> |
| 41 | SysModelQMLItem::Initializer::provideExporter(Item const &i) |
| 42 | { |
| 43 | auto &itemID = i.ID(); |
| 44 | if (itemID == ISysModel::ItemID) |
| 45 | return *this; |
| 46 | else { |
| 47 | auto [componentInitializer, qmlItemPtr] = initializer(itemID, &outer_); |
| 48 | if (componentInitializer.has_value()) { |
| 49 | auto &key = dynamic_cast<ISysComponent const &>(i).key(); |
| 50 | outer_.components_.emplace(key, qmlItemPtr); |
| 51 | return componentInitializer; |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | return {}; |
| 56 | } |
| 57 | |
| 58 | void SysModelQMLItem::activate(bool) |
| 59 | { |
| 60 | } |
| 61 | |
| 62 | std::optional<std::reference_wrapper<Importable::Importer>> |
| 63 | SysModelQMLItem::provideImporter(Item const &i) |
| 64 | { |
| 65 | if (i.ID() == IProfile::ItemID) |
| 66 | return *this; |
| 67 | |
| 68 | auto &key = dynamic_cast<ISysComponentProfilePart const &>(i).key(); |
| 69 | auto const iter = components_.find(key); |
| 70 | if (iter != components_.cend()) |
| 71 | return dynamic_cast<Importable::Importer &>(*iter->second); |
| 72 | |
| 73 | return {}; |
| 74 | } |
| 75 | |
| 76 | std::optional<std::reference_wrapper<Exportable::Exporter>> |
| 77 | SysModelQMLItem::provideExporter(Item const &i) |
| 78 | { |
| 79 | if (i.ID() == IProfile::ItemID) |
| 80 | return *this; |
| 81 | |
| 82 | auto &key = dynamic_cast<ISysComponentProfilePart const &>(i).key(); |
| 83 | auto const iter = components_.find(key); |
| 84 | if (iter != components_.cend()) |
| 85 | return dynamic_cast<Exportable::Exporter &>(*iter->second); |
| 86 | |
| 87 | return {}; |
| 88 | } |
| 89 | |
| 90 | bool SysModelQMLItem::provideActive() const |
| 91 | { |
| 92 | return profileActive_; |
| 93 | } |
| 94 | |
| 95 | IProfile::Info const &SysModelQMLItem::provideInfo() const |
| 96 | { |
| 97 | return profileInfo_; |
| 98 | } |
| 99 | |
| 100 | void SysModelQMLItem::takeActive(bool active) |
| 101 | { |
| 102 | profileActive_ = active; |
| 103 | } |
| 104 | |
| 105 | void SysModelQMLItem::takeInfo(IProfile::Info const &info) |
| 106 | { |
| 107 | profileInfo_ = info; |
| 108 | } |
| 109 | |
| 110 | std::unique_ptr<Exportable::Exporter> |
| 111 | SysModelQMLItem::initializer(IQMLComponentFactory const &qmlComponentFactory, |
| 112 | QQmlApplicationEngine &qmlEngine) |
| 113 | { |
| 114 | return std::make_unique<SysModelQMLItem::Initializer>(qmlComponentFactory, |
| 115 | qmlEngine, *this); |
| 116 | } |
| 117 | |
| 118 | bool SysModelQMLItem::register_() |
| 119 | { |
| 120 | QMLComponentRegistry::addQMLTypeRegisterer([]() { |
| 121 | qmlRegisterType<SysModelQMLItem>("CoreCtrl.UIComponents", 1, 0, |
| 122 | ISysModel::ItemID.data()); |
| 123 | }); |
| 124 | |
| 125 | QMLComponentRegistry::addQMLItemProvider( |
| 126 | ISysModel::ItemID, [](QQmlApplicationEngine &engine) { |
| 127 | QQmlComponent component(&engine, |
| 128 | QStringLiteral("qrc:/qml/SysModelForm.qml")); |
| 129 | return qobject_cast<QMLItem *>(component.create()); |
| 130 | }); |
| 131 | |
| 132 | return true; |
| 133 | } |
| 134 | |
| 135 | bool const SysModelQMLItem::registered_ = SysModelQMLItem::register_(); |