SILICON
Loading...
Searching...
No Matches
graphicalRegister.cpp
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#include "graphicalRegister.hpp"
19
20#include <stdexcept>
21#include <utility>
22#include <variant>
23
24#include <QGraphicsRectItem>
25#include <QPainter>
26#include <QTextOption>
27
28#include <ui/shell/theme.hpp>
29
30
31namespace SILICON::ui {
32using namespace SILICON::core;
33
34namespace {
35
36constexpr QRectF RegisterBodyRect(0.0, 0.0, 100.0, 80.0);
37
38std::shared_ptr<Register> makeRegister()
39{
40 return std::make_shared<Register>(Bus(2), Wire_ptr{}, Wire_ptr{}, Wire_ptr{}, Bus(2));
41}
42
43QString registerLabel(const std::string& inputType, const std::string& outputType)
44{
45 const QString inputPrefix = inputType == Register::PARALLEL_TYPE ? "Pi" : "Si";
46 const QString outputSuffix = outputType == Register::PARALLEL_TYPE ? "Po" : "So";
47 return inputPrefix + outputSuffix + "\nRegister";
48}
49
50class RegisterShape : public QGraphicsRectItem {
51public:
52 explicit RegisterShape(QString label, QGraphicsItem* parent = nullptr)
53 : QGraphicsRectItem(RegisterBodyRect, parent), label(std::move(label))
54 {
55 }
56
57 void paint(QPainter* painter, const QStyleOptionGraphicsItem* option,
58 QWidget* widget) override
59 {
60 Q_UNUSED(option);
61 Q_UNUSED(widget);
62
63 painter->save();
64 painter->setRenderHint(QPainter::Antialiasing, false);
65 painter->setPen(QPen(ThemeEngine::getColor(theme::ColorToken::Ink), 3));
67 painter->drawRect(rect());
68
69 painter->setPen(QPen(ThemeEngine::getColor(theme::ColorToken::Ink)));
70 painter->setFont(QFont("Quicksand", 8, QFont::Bold));
71 painter->drawText(rect().adjusted(6, 26, -6, -6), label,
72 QTextOption(Qt::AlignCenter));
73 painter->restore();
74 }
75
76private:
77 QString label;
78};
79
80} // namespace
81
83 : GraphicalLogicComponent(makeRegister(),
84 new RegisterShape(registerLabel("Parallel", "Parallel")),
85 parent)
86{
87 isEditable = true;
88 printPortNames = true;
91}
92
94{
95 auto* reg = dynamic_cast<Register*>(associatedComponent.get());
96 if (!reg)
97 throw std::logic_error("GraphicalRegister requires a Register component");
98 return reg;
99}
100
102{
104 return;
105
106 associatedComponent->setPropertyCallback("size", [this](const PropertyValue& value) {
107 return applySize(std::get<int>(value));
108 });
109 associatedComponent->setPropertyCallback("inputType", [this](const PropertyValue& value) {
110 return applyInputType(std::get<std::string>(value));
111 });
112 associatedComponent->setPropertyCallback("outputType", [this](const PropertyValue& value) {
113 return applyOutputType(std::get<std::string>(value));
114 });
115}
116
118{
119 const std::string inputType =
120 associatedComponent->getPropertyValue<std::string>("inputType")
121 .value_or(std::string(Register::PARALLEL_TYPE));
122 const std::string outputType =
123 associatedComponent->getPropertyValue<std::string>("outputType")
124 .value_or(std::string(Register::PARALLEL_TYPE));
125
126 updateLayout(inputType, outputType);
127}
128
129void GraphicalRegister::updateLayout(const std::string& inputType,
130 const std::string& outputType)
131{
132 setItemShape(new RegisterShape(registerLabel(inputType, outputType)));
133
134 const bool needsLoad = inputType == Register::PARALLEL_TYPE && outputType == Register::SERIAL_TYPE;
135
136 const QString dataInput = inputType == Register::PARALLEL_TYPE ? "d" : "si";
137 const QString dataOutput = outputType == Register::PARALLEL_TYPE ? "q" : "so";
138
139 std::vector<PortPair> inputs{PortPair{dataInput, QPoint(-20, 20)},
140 PortPair{"clk", QPoint(-20, 40)},
141 PortPair{"en", QPoint(-20, 60)},
142 PortPair{"clr", needsLoad ? QPoint(80, -20) : QPoint(50, -20)}};
143 if (needsLoad)
144 inputs.push_back(PortPair{"ld", QPoint(50, -20)});
145
146 setPorts(inputs, {PortPair{dataOutput, QPoint(120, 40)}});
147}
148
150{
151 const int appliedSize = getComponentAsRegister()->setSize(size);
152 updateLayout();
153 return appliedSize;
154}
155
156std::string GraphicalRegister::applyInputType(const std::string& inputType)
157{
158 const std::string appliedInputType = getComponentAsRegister()->setInputType(inputType);
159 updateLayout(appliedInputType,
160 associatedComponent->getPropertyValue<std::string>("outputType")
161 .value_or(std::string(Register::PARALLEL_TYPE)));
162 return appliedInputType;
163}
164
165std::string GraphicalRegister::applyOutputType(const std::string& outputType)
166{
167 const std::string appliedOutputType =
169 updateLayout(associatedComponent->getPropertyValue<std::string>("inputType")
170 .value_or(std::string(Register::PARALLEL_TYPE)),
171 appliedOutputType);
172 return appliedOutputType;
173}
174
176{
178 if (!component)
179 return;
180
182 getComponentAsRegister()->setSize(component->getPropertyValue<int>("size").value_or(2));
184 component->getPropertyValue<std::string>("inputType")
185 .value_or(std::string(Register::PARALLEL_TYPE)));
187 component->getPropertyValue<std::string>("outputType")
188 .value_or(std::string(Register::PARALLEL_TYPE)));
189 updateLayout();
190}
191
192} // namespace SILICON::ui
std::string setInputType(std::string inputType)
Definition register.cpp:147
static constexpr std::string_view SERIAL_TYPE
Definition register.hpp:33
std::string setOutputType(std::string outputType)
Definition register.cpp:153
int setSize(int size)
Definition register.cpp:138
static constexpr std::string_view PARALLEL_TYPE
Definition register.hpp:32
bool printPortNames
Whether to print port names.
void setItemShape(QGraphicsItem *shape)
Sets the component shape graphics item.
void setPorts(const std::vector< PortPair > &busToPortInputs, const std::vector< PortPair > &busToPortOutputs) override
Sets ports and validates against component I/O.
bool isEditable
Whether the component allows property editing.
GraphicalLogicComponent(ItemCategory category, const Component_ptr &component, QGraphicsItem *shape, QGraphicsItem *parent, bool scanShape=false)
virtual void setComponent(const Component_ptr &component)
Sets the associated logical component.
Component_ptr associatedComponent
The logical component this graphical component represents.
GraphicalRegister(QGraphicsItem *parent=nullptr)
std::string applyOutputType(const std::string &outputType)
std::string applyInputType(const std::string &inputType)
void setComponent(const Component_ptr &component) override
Sets the associated logical component.
Register * getComponentAsRegister() const
static const QColor & getColor(theme::ColorToken token)
Definition theme.hpp:92
std::shared_ptr< Component > Component_ptr
std::shared_ptr< Wire > Wire_ptr
Definition wire.hpp:116
std::variant< int, bool, std::string, BusValue > PropertyValue
Variant type for component properties.
std::pair< QString, QPoint > PortPair