| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | // Copyright 2021 Juan Palacios <[email protected]> |
| 3 | |
| 4 | #include "noopqmlitem.h" |
| 5 | |
| 6 | #include "core/qmlcomponentregistry.h" |
| 7 | #include "noop.h" |
| 8 | #include <QQmlApplicationEngine> |
| 9 | #include <QQmlComponent> |
| 10 | #include <QString> |
| 11 | #include <QtGlobal> |
| 12 | #include <QtQml> |
| 13 | #include <memory> |
| 14 | |
| 15 | char const *const NoopQMLItem::trStrings[] = { |
| 16 | QT_TRANSLATE_NOOP("ControlModeQMLItem", "NOOP"), |
| 17 | }; |
| 18 | |
| 19 | class NoopQMLItem::Initializer final |
| 20 | : public QMLItem::Initializer |
| 21 | , public IControl::Exporter |
| 22 | { |
| 23 | public: |
| 24 | Initializer(IQMLComponentFactory const &qmlComponentFactory, |
| 25 | QQmlApplicationEngine &qmlEngine, NoopQMLItem &qmlItem) noexcept |
| 26 | : QMLItem::Initializer(qmlComponentFactory, qmlEngine) |
| 27 | , outer_(qmlItem) |
| 28 | { |
| 29 | } |
| 30 | |
| 31 | std::optional<std::reference_wrapper<Exportable::Exporter>> |
| 32 | provideExporter(Item const &) override |
| 33 | { |
| 34 | return {}; |
| 35 | } |
| 36 | |
| 37 | void takeActive(bool active) override; |
| 38 | |
| 39 | private: |
| 40 | NoopQMLItem &outer_; |
| 41 | }; |
| 42 | |
| 43 | void NoopQMLItem::Initializer::takeActive(bool active) |
| 44 | { |
| 45 | outer_.takeActive(active); |
| 46 | } |
| 47 | |
| 48 | NoopQMLItem::NoopQMLItem() noexcept |
| 49 | { |
| 50 | setName(tr(Noop::ItemID.data())); |
| 51 | } |
| 52 | |
| 53 | void NoopQMLItem::activate(bool active) |
| 54 | { |
| 55 | takeActive(active); |
| 56 | } |
| 57 | |
| 58 | std::optional<std::reference_wrapper<Importable::Importer>> |
| 59 | NoopQMLItem::provideImporter(Item const &) |
| 60 | { |
| 61 | return {}; |
| 62 | } |
| 63 | |
| 64 | std::optional<std::reference_wrapper<Exportable::Exporter>> |
| 65 | NoopQMLItem::provideExporter(Item const &) |
| 66 | { |
| 67 | return {}; |
| 68 | } |
| 69 | |
| 70 | bool NoopQMLItem::provideActive() const |
| 71 | { |
| 72 | return active_; |
| 73 | } |
| 74 | |
| 75 | void NoopQMLItem::takeActive(bool active) |
| 76 | { |
| 77 | active_ = active; |
| 78 | setVisible(active); |
| 79 | } |
| 80 | |
| 81 | std::unique_ptr<Exportable::Exporter> |
| 82 | NoopQMLItem::initializer(IQMLComponentFactory const &qmlComponentFactory, |
| 83 | QQmlApplicationEngine &qmlEngine) |
| 84 | { |
| 85 | return std::make_unique<NoopQMLItem::Initializer>(qmlComponentFactory, |
| 86 | qmlEngine, *this); |
| 87 | } |
| 88 | |
| 89 | bool NoopQMLItem::register_() |
| 90 | { |
| 91 | QMLComponentRegistry::addQMLTypeRegisterer([]() { |
| 92 | qmlRegisterType<NoopQMLItem>("CoreCtrl.UIComponents", 1, 0, |
| 93 | Noop::ItemID.data()); |
| 94 | }); |
| 95 | |
| 96 | QMLComponentRegistry::addQMLItemProvider( |
| 97 | Noop::ItemID, [](QQmlApplicationEngine &engine) { |
| 98 | QQmlComponent component(&engine, |
| 99 | QStringLiteral("qrc:/qml/NoopForm.qml")); |
| 100 | return qobject_cast<QMLItem *>(component.create()); |
| 101 | }); |
| 102 | |
| 103 | return true; |
| 104 | } |
| 105 | |
| 106 | bool const NoopQMLItem::registered_ = NoopQMLItem::register_(); |