| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | // Copyright 2019 Juan Palacios <[email protected]> |
| 3 | |
| 4 | #include "controlmodeqmlitem.h" |
| 5 | |
| 6 | #include "controlmode.h" |
| 7 | #include "core/item.h" |
| 8 | #include <QQuickItem> |
| 9 | #include <memory> |
| 10 | #include <utility> |
| 11 | |
| 12 | class ControlModeQMLItem::Initializer final |
| 13 | : public QMLItem::Initializer |
| 14 | , public ControlMode::Exporter |
| 15 | { |
| 16 | public: |
| 17 | Initializer(IQMLComponentFactory const &qmlComponentFactory, |
| 18 | QQmlApplicationEngine &qmlEngine, |
| 19 | ControlModeQMLItem &qmlItem) noexcept |
| 20 | : QMLItem::Initializer(qmlComponentFactory, qmlEngine) |
| 21 | , outer_(qmlItem) |
| 22 | { |
| 23 | } |
| 24 | |
| 25 | std::optional<std::reference_wrapper<Exportable::Exporter>> |
| 26 | provideExporter(Item const &i) override; |
| 27 | |
| 28 | void takeActive(bool active) override; |
| 29 | |
| 30 | void takeMode(std::string const &mode) override; |
| 31 | void takeModes(std::vector<std::string> const &modes) override; |
| 32 | |
| 33 | private: |
| 34 | ControlModeQMLItem &outer_; |
| 35 | }; |
| 36 | |
| 37 | std::optional<std::reference_wrapper<Exportable::Exporter>> |
| 38 | ControlModeQMLItem::Initializer::provideExporter(Item const &i) |
| 39 | { |
| 40 | return initializer(i.ID(), &outer_).first; |
| 41 | } |
| 42 | |
| 43 | void ControlModeQMLItem::Initializer::takeActive(bool active) |
| 44 | { |
| 45 | outer_.takeActive(active); |
| 46 | } |
| 47 | |
| 48 | void ControlModeQMLItem::Initializer::takeMode(std::string const &mode) |
| 49 | { |
| 50 | outer_.takeMode(mode); |
| 51 | } |
| 52 | |
| 53 | void ControlModeQMLItem::Initializer::takeModes( |
| 54 | std::vector<std::string> const &modes) |
| 55 | { |
| 56 | outer_.takeModes(modes); |
| 57 | } |
| 58 | |
| 59 | ControlModeQMLItem::ControlModeQMLItem(std::string_view id) noexcept |
| 60 | { |
| 61 | setName(tr(id.data())); |
| 62 | } |
| 63 | |
| 64 | void ControlModeQMLItem::changeMode(QString const &mode) |
| 65 | { |
| 66 | auto newMode = mode.toStdString(); |
| 67 | if (mode_ != newMode) { |
| 68 | std::swap(mode_, newMode); |
| 69 | emit modeChanged(mode); |
| 70 | emit settingsChanged(); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | std::optional<std::reference_wrapper<Importable::Importer>> |
| 75 | ControlModeQMLItem::provideImporter(Item const &i) |
| 76 | { |
| 77 | auto item = this->findChild<QQuickItem *>(QString::fromStdString(i.ID())); |
| 78 | if (item == nullptr) |
| 79 | return {}; |
| 80 | |
| 81 | return dynamic_cast<Importable::Importer &>(*item); |
| 82 | } |
| 83 | |
| 84 | std::optional<std::reference_wrapper<Exportable::Exporter>> |
| 85 | ControlModeQMLItem::provideExporter(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<Exportable::Exporter &>(*item); |
| 92 | } |
| 93 | |
| 94 | bool ControlModeQMLItem::provideActive() const |
| 95 | { |
| 96 | return active_; |
| 97 | } |
| 98 | |
| 99 | std::string const &ControlModeQMLItem::provideMode() const |
| 100 | { |
| 101 | return mode_; |
| 102 | } |
| 103 | |
| 104 | void ControlModeQMLItem::takeActive(bool active) |
| 105 | { |
| 106 | active_ = active; |
| 107 | setVisible(active); |
| 108 | } |
| 109 | |
| 110 | void ControlModeQMLItem::takeMode(std::string const &mode) |
| 111 | { |
| 112 | if (mode_ != mode) { |
| 113 | mode_ = mode; |
| 114 | emit modeChanged(QString::fromUtf8(mode_.data())); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | std::unique_ptr<Exportable::Exporter> |
| 119 | ControlModeQMLItem::initializer(IQMLComponentFactory const &qmlComponentFactory, |
| 120 | QQmlApplicationEngine &qmlEngine) |
| 121 | { |
| 122 | return std::make_unique<ControlModeQMLItem::Initializer>(qmlComponentFactory, |
| 123 | qmlEngine, *this); |
| 124 | } |
| 125 | |
| 126 | void ControlModeQMLItem::activate(bool active) |
| 127 | { |
| 128 | takeActive(active); |
| 129 | } |
| 130 | |
| 131 | void ControlModeQMLItem::takeModes(std::vector<std::string> const &modes) |
| 132 | { |
| 133 | QList<QString> modeTextVector; |
| 134 | for (auto mode : modes) { |
| 135 | modeTextVector.push_back(QString::fromStdString(mode)); |
| 136 | modeTextVector.push_back(tr(mode.data())); |
| 137 | } |
| 138 | emit modesChanged(modeTextVector); |
| 139 | } |