SILICON
Loading...
Searching...
No Matches
gui_component_factory.hpp
Go to the documentation of this file.
1/*
2 Copyright (c) 2026. Giulio Cocconi
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#pragma once
19
20#include <QGraphicsItem>
21
23
24#include <memory>
26
27#include <functional>
28#include <map>
29#include <optional>
30#include <stdexcept>
31#include <string>
32#include <string_view>
33#include <vector>
34
35
36namespace SILICON::ui {
38
56public:
63 using Factory = std::function<std::unique_ptr<GraphicalComponent>(QGraphicsItem*)>;
64
66 std::string coreType;
67 std::optional<SILICON::core::ComponentMetadata> graphicalMetadata = std::nullopt;
68 };
69
71
77
83
91 void registerType(std::string type, Factory factory);
92 void registerType(std::string type, Factory factory, EntryMetadata metadata);
93
102 std::unique_ptr<GraphicalComponent> create(std::string_view type,
103 QGraphicsItem* parent = nullptr) const;
104
114 std::unique_ptr<GraphicalComponent>
115 createForCoreType(std::string_view coreType, QGraphicsItem* parent = nullptr) const;
116
121 std::vector<std::string> availableTypes() const;
122
123 const EntryMetadata& metadata(std::string_view type) const;
124
130 bool hasType(std::string_view type) const;
131
132private:
138
139 std::map<std::string, Entry, std::less<>> factories;
140};
141
142} // namespace SILICON::ui
static GUIComponentFactory & instance()
Gets the global singleton factory instance.
const EntryMetadata & metadata(std::string_view type) const
std::map< std::string, Entry, std::less<> > factories
static GUIComponentFactory empty()
Creates an empty factory without registered types.
std::function< std::unique_ptr< GraphicalComponent >(QGraphicsItem *)> Factory
Factory function type for creating components.
std::unique_ptr< GraphicalComponent > createForCoreType(std::string_view coreType, QGraphicsItem *parent=nullptr) const
Creates the graphical component registered for a core component type.
std::vector< std::string > availableTypes() const
Gets all available component types.
void registerType(std::string type, Factory factory)
Registers a component type.
bool hasType(std::string_view type) const
Checks if a type is registered.
std::unique_ptr< GraphicalComponent > create(std::string_view type, QGraphicsItem *parent=nullptr) const
Creates a component by type name.
Base class for graphical circuit components.
std::optional< SILICON::core::ComponentMetadata > graphicalMetadata
Map of type names to factory functions.