SILICON
Loading...
Searching...
No Matches
graphicalLogicComponent.cpp
Go to the documentation of this file.
1/*
2 Copyright (c) 2025. 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
19
20#include <stdexcept>
21
22
23namespace SILICON::ui {
24using namespace SILICON::core;
25
27 const Component_ptr& component,
28 QGraphicsItem* shape,
29 QGraphicsItem* parent, bool scanShape)
30 : GraphicalComponent(category | ItemCategory::LogicComponent, shape, parent, scanShape)
31{
32 this->associatedComponent = component;
34}
35
37 QGraphicsItem* shape,
38 QGraphicsItem* parent, bool scanShape)
39 : GraphicalLogicComponent(ItemCategory::LogicComponent, component, shape, parent,
41{
42}
43
49
61
63{
65 return;
66
67 const std::vector<Bus> componentInputs = associatedComponent->getInputs();
68 const std::vector<Bus> componentOutputs = associatedComponent->getOutputs();
69
70 for (size_t i = 0; i < inputPorts.size(); ++i)
71 inputPorts[i]->setSize(inputPortSize(i, componentInputs));
72
73 for (size_t i = 0; i < outputPorts.size() && i < componentOutputs.size(); ++i)
74 outputPorts[i]->setSize(componentOutputs[i].size());
75
76 update();
77 if (auto* diagram = qobject_cast<DiagramScene*>(scene());
78 diagram && diagram->getInteractionMode()
79 != DiagramScene::InteractionMode::SIMULATION_MODE)
80 diagram->requestConnectionsRefresh();
81}
82
84 const size_t portCount, const std::vector<Bus>& componentInputs) const
85{
86 return componentInputs.size() == portCount;
87}
88
89unsigned int
91 const std::vector<Bus>& componentInputs) const
92{
93 if (portIndex >= componentInputs.size())
94 return 1;
95
96 return static_cast<unsigned int>(componentInputs[portIndex].size());
97}
98
104
105std::optional<std::reference_wrapper<const StringPropertyOptions>>
107{
108 return associatedComponent ? associatedComponent->getStringPropertyOptions(key)
109 : std::nullopt;
110}
111
113 const PropertyValue& value)
114{
116 return;
117
118 prepareGeometryChange();
119 associatedComponent->setProperty(key, value);
120 update();
121}
122
133
134void GraphicalLogicComponent::assignInputPortBus(const unsigned int portIndex,
135 const Bus& bus) const
136{
138 return;
139
140 associatedComponent->setInput(portIndex, bus, true);
141}
142
143void GraphicalLogicComponent::setPorts(const std::vector<PortPair>& busToPortInputs,
144 const std::vector<PortPair>& busToPortOutputs)
145{
147 const std::vector<Bus> componentInputs = associatedComponent->getInputs();
148 const std::vector<Bus> componentOutputs = associatedComponent->getOutputs();
149
150 if (!acceptsInputPortCount(busToPortInputs.size(), componentInputs))
151 throw std::logic_error("Input port count mismatch with component");
152 if (componentOutputs.size() != busToPortOutputs.size())
153 throw std::logic_error("Output port count mismatch with component");
154 }
155
156 GraphicalComponent::setPorts(busToPortInputs, busToPortOutputs);
158}
159
160} // namespace SILICON::ui
Base class for all digital circuit components.
GraphicalComponent(ItemCategory category, QGraphicsItem *shape, QGraphicsItem *parent=nullptr, bool scanShape=false)
virtual void setPorts(const std::vector< PortPair > &busToPortInputs, const std::vector< PortPair > &busToPortOutputs)
Sets input and output ports.
std::vector< Port * > outputPorts
Output connection ports.
virtual const PropertyMap & getProperties() const
Properties exposed by graphical and simulation components alike.
std::vector< Port * > inputPorts
Input connection ports.
bool scanShape
Whether to scan shape alpha for port placement.
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
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.
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.