SILICON
Loading...
Searching...
No Matches
graphicalLogicComponent.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 <string>
21#include <string_view>
22#include <utility>
23#include <vector>
24
25#include <QGraphicsItem>
26#include <QPainter>
27#include <QPoint>
28#include <QRect>
29
30#include <core/component.hpp>
31#include <core/wire.hpp>
32
35
36
37namespace SILICON::ui {
38using namespace SILICON::core;
39
56 Q_OBJECT
57protected:
60
63
65 bool isEditable = false;
66
69
71 virtual void updatePortSizes();
72
74 [[nodiscard]] virtual bool
75 acceptsInputPortCount(size_t portCount, const std::vector<Bus>& componentInputs) const;
76
78 [[nodiscard]] virtual unsigned int
79 inputPortSize(size_t portIndex, const std::vector<Bus>& componentInputs) const;
80
81protected:
82 explicit GraphicalLogicComponent(ItemCategory category, const Component_ptr& component,
83 QGraphicsItem* shape, QGraphicsItem* parent,
84 bool scanShape = false);
85
86public:
95 GraphicalLogicComponent(const Component_ptr& component, QGraphicsItem* shape,
96 QGraphicsItem* parent, bool scanShape = false);
97
98 ~GraphicalLogicComponent() override;
99
106 void setPorts(const std::vector<PortPair>& busToPortInputs,
107 const std::vector<PortPair>& busToPortOutputs) override;
108
114 virtual void assignInputPortBus(unsigned int portIndex, const Bus& bus) const;
115
120 [[nodiscard]] Component_ptr getComponent() const { return associatedComponent; }
121
128 void applyProperty(std::string_view key, const PropertyValue& value) override;
129 [[nodiscard]] const PropertyMap& getProperties() const override;
130 [[nodiscard]] std::optional<std::reference_wrapper<const StringPropertyOptions>>
131 getStringPropertyOptions(std::string_view key) const override;
132
137 virtual void setComponent(const Component_ptr& component);
138
139 [[nodiscard]] std::string getTypeName() const override
140 {
142 return std::string(associatedComponent->typeName());
143 }
144 return "Unknown";
145 }
146};
147
148} // namespace SILICON::ui
GraphicalComponent(ItemCategory category, QGraphicsItem *shape, QGraphicsItem *parent=nullptr, bool scanShape=false)
bool scanShape
Whether to scan shape alpha for port placement.
Component_ptr getComponent() const
Gets the associated logical component.
virtual void updatePortSizes()
Copies associated component bus sizes into graphical ports.
void setPorts(const std::vector< PortPair > &busToPortInputs, const std::vector< PortPair > &busToPortOutputs) override
Sets ports and validates against component I/O.
void applyProperty(std::string_view key, const PropertyValue &value) override
Applies a component property while notifying Qt about possible geometry changes.
std::optional< std::reference_wrapper< const StringPropertyOptions > > getStringPropertyOptions(std::string_view key) const override
bool isEditable
Whether the component allows property editing.
virtual unsigned int inputPortSize(size_t portIndex, const std::vector< Bus > &componentInputs) const
Returns the visible size for an input port.
GraphicalLogicComponent(ItemCategory category, const Component_ptr &component, QGraphicsItem *shape, QGraphicsItem *parent, bool scanShape=false)
virtual void assignInputPortBus(unsigned int portIndex, const Bus &bus) const
Assigns a bus connected to a visible input port.
virtual void setComponent(const Component_ptr &component)
Sets the associated logical component.
virtual bool acceptsInputPortCount(size_t portCount, const std::vector< Bus > &componentInputs) const
Checks whether a visible input port count is valid for this component.
uint64_t componentIOListenerId
Listener id for associated component I/O changes.
std::string getTypeName() const override
Gets the component type name for serialization.
void refreshComponentIOListener()
Installs the listener that keeps graphical ports synced to bus sizes.
const PropertyMap & getProperties() const override
Properties exposed by graphical and simulation components alike.
Component_ptr associatedComponent
The logical component this graphical component represents.
std::shared_ptr< Component > Component_ptr
std::variant< int, bool, std::string, BusValue > PropertyValue
Variant type for component properties.
std::unordered_map< std::string, PropertyValue, TransparentStringHash, TransparentStringEqual > PropertyMap
Map from property names to their values.