| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | // Copyright 2019 Juan Palacios <[email protected]> |
| 3 | |
| 4 | #include "graphitemxmlparser.h" |
| 5 | |
| 6 | #include <memory> |
| 7 | |
| 8 | class GraphItemXMLParser::Initializer final |
| 9 | : public GraphItemProfilePart::Exporter |
| 10 | { |
| 11 | public: |
| 12 | Initializer(GraphItemXMLParser &outer) noexcept |
| 13 | : outer_(outer) |
| 14 | { |
| 15 | } |
| 16 | |
| 17 | std::optional<std::reference_wrapper<Exportable::Exporter>> |
| 18 | provideExporter(Item const &) override |
| 19 | { |
| 20 | return {}; |
| 21 | } |
| 22 | |
| 23 | void takeActive(bool active) override; |
| 24 | void takeColor(std::string const &color) override; |
| 25 | |
| 26 | private: |
| 27 | GraphItemXMLParser &outer_; |
| 28 | }; |
| 29 | |
| 30 | void GraphItemXMLParser::Initializer::takeActive(bool active) |
| 31 | { |
| 32 | outer_.active_ = outer_.activeDefault_ = active; |
| 33 | } |
| 34 | |
| 35 | void GraphItemXMLParser::Initializer::takeColor(std::string const &color) |
| 36 | { |
| 37 | outer_.colorDefault_ = color; |
| 38 | } |
| 39 | |
| 40 | GraphItemXMLParser::GraphItemXMLParser(std::string_view id) noexcept |
| 41 | : ProfilePartXMLParser(id, *this, *this) |
| 42 | { |
| 43 | } |
| 44 | |
| 45 | std::unique_ptr<Exportable::Exporter> |
| 46 | GraphItemXMLParser::factory(IProfilePartXMLParserProvider const &) |
| 47 | { |
| 48 | return nullptr; |
| 49 | } |
| 50 | |
| 51 | std::unique_ptr<Exportable::Exporter> GraphItemXMLParser::initializer() |
| 52 | { |
| 53 | return std::make_unique<GraphItemXMLParser::Initializer>(*this); |
| 54 | } |
| 55 | |
| 56 | std::optional<std::reference_wrapper<Exportable::Exporter>> |
| 57 | GraphItemXMLParser::provideExporter(Item const &) |
| 58 | { |
| 59 | return {}; |
| 60 | } |
| 61 | |
| 62 | std::optional<std::reference_wrapper<Importable::Importer>> |
| 63 | GraphItemXMLParser::provideImporter(Item const &) |
| 64 | { |
| 65 | return {}; |
| 66 | } |
| 67 | |
| 68 | void GraphItemXMLParser::takeActive(bool active) |
| 69 | { |
| 70 | active_ = active; |
| 71 | } |
| 72 | |
| 73 | bool GraphItemXMLParser::provideActive() const |
| 74 | { |
| 75 | return active_; |
| 76 | } |
| 77 | |
| 78 | void GraphItemXMLParser::takeColor(const std::string &) |
| 79 | { |
| 80 | } |
| 81 | |
| 82 | std::string const &GraphItemXMLParser::provideColor() const |
| 83 | { |
| 84 | return colorDefault_; |
| 85 | } |
| 86 | |
| 87 | void GraphItemXMLParser::appendTo(pugi::xml_node &parentNode) |
| 88 | { |
| 89 | auto itemNode = parentNode.append_child(ID().c_str()); |
| 90 | itemNode.append_attribute("active") = active_; |
| 91 | } |
| 92 | |
| 93 | void GraphItemXMLParser::resetAttributes() |
| 94 | { |
| 95 | active_ = activeDefault_; |
| 96 | } |
| 97 | |
| 98 | void GraphItemXMLParser::loadPartFrom(pugi::xml_node const &parentNode) |
| 99 | { |
| 100 | auto itemNode = parentNode.find_child( |
| 101 | [&](pugi::xml_node const &node) { return node.name() == ID(); }); |
| 102 | |
| 103 | active_ = itemNode.attribute("active").as_bool(activeDefault_); |
| 104 | } |