SILICON
Loading...
Searching...
No Matches
graphicalArithmetic.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
19
20#include <ui/shell/icons.hpp>
21#include <ui/shell/theme.hpp>
23
24#include <QFont>
25#include <QTextOption>
26
27#include <stdexcept>
28#include <utility>
29#include <variant>
30
31namespace SILICON::ui {
32using namespace SILICON::core;
33using namespace SILICON::extra;
34
35namespace {
36
37 class UnaryArithmeticShape : public QGraphicsRectItem {
38 public:
39 explicit UnaryArithmeticShape(QString iconName, QGraphicsItem* parent = nullptr)
40 : QGraphicsRectItem(0, 0, 20, 90, parent), iconName(std::move(iconName))
41 {
42 }
43
44 void paint(QPainter* painter, const QStyleOptionGraphicsItem* option,
45 QWidget* widget) override
46 {
47 Q_UNUSED(option);
48 Q_UNUSED(widget);
49
51
52 painter->setRenderHint(QPainter::Antialiasing, false);
53 painter->setPen(QPen(ink, 3));
55 painter->drawRect(rect());
56
57 const int iconWidth = rect().width() - 5;
58 const QPoint leftPoint =
59 rect().center().toPoint() - QPoint(iconWidth / 2, iconWidth / 2);
60 painter->setPen(QPen(ink));
61 Icon(iconName)
62 .paint(painter, leftPoint.x(), leftPoint.y(), iconWidth, iconWidth);
63 }
64
65 private:
66 QString iconName;
67 };
68
69 class ComparatorShape : public ThemedSvgItem {
70 public:
71 explicit ComparatorShape(QString mode, QGraphicsItem* parent = nullptr)
72 : ThemedSvgItem(":/other_components/COMPARATOR.svg", parent), mode(std::move(mode))
73 {
74 }
75
76 void setMode(QString mode)
77 {
78 this->mode = std::move(mode);
79 update();
80 }
81
82 void paint(QPainter* painter, const QStyleOptionGraphicsItem* option,
83 QWidget* widget) override
84 {
85 ThemedSvgItem::paint(painter, option, widget);
86
87 painter->save();
88 painter->setPen(QPen(ThemeEngine::getColor(theme::ColorToken::Ink)));
89 painter->setFont(QFont("Quicksand", 12, QFont::Bold));
90 painter->drawText(QRect(QPoint(40, 30), QPoint(60, 40)), mode, QTextOption(Qt::AlignCenter));
91 painter->restore();
92 }
93
94 private:
95 QString mode;
96 };
97
98 std::shared_ptr<Extender> makeExtender()
99 {
100 return std::make_shared<Extender>(Bus(4), Bus(8));
101 }
102
103 std::shared_ptr<Complementer> makeComplementer()
104 {
105 return std::make_shared<Complementer>(Bus(4), Bus(4));
106 }
107
108std::shared_ptr<HalfAdder> makeHalfAdder()
109{
110 return std::make_shared<HalfAdder>(std::array<Wire_ptr, 2>{Wire_ptr{}, Wire_ptr{}},
111 Wire_ptr{}, Wire_ptr{});
112}
113
114std::shared_ptr<FullAdder> makeFullAdder()
115{
116 return std::make_shared<FullAdder>(std::array<Wire_ptr, 2>{Wire_ptr{}, Wire_ptr{}},
117 Wire_ptr{}, Wire_ptr{}, Wire_ptr{});
118}
119
120std::shared_ptr<Comparator> makeComparator()
121{
122 return std::make_shared<Comparator>(std::array<Bus, 2>{Bus(4), Bus(4)}, Wire_ptr{});
123}
124
125std::shared_ptr<AdderNBits> makeAdderNBits()
126{
127 constexpr unsigned short DEFAULT_SIZE = 4;
128 return std::make_shared<AdderNBits>(
129 std::array<Bus, 2>{Bus(DEFAULT_SIZE), Bus(DEFAULT_SIZE)}, Bus(DEFAULT_SIZE),
130 Wire_ptr{});
131}
132
133std::shared_ptr<Shifter> makeShifter()
134{
135 constexpr unsigned short DEFAULT_SIZE = 4;
136 return std::make_shared<Shifter>(Bus(DEFAULT_SIZE), Bus(DEFAULT_SIZE),
137 Bus(DEFAULT_SIZE));
138}
139
140} // namespace
141
143 : GraphicalLogicComponent(makeExtender(), new UnaryArithmeticShape("expand", parent),
144 parent)
145{
146 setPorts({PortPair{"n", QPoint(10, -20)}}, {PortPair{"o", QPoint(10, 110)}});
147}
148
150 : GraphicalLogicComponent(makeComplementer(), new UnaryArithmeticShape("minus", parent),
151 parent)
152{
153 setPorts({PortPair{"n", QPoint(10, -20)}}, {PortPair{"o", QPoint(10, 110)}});
154}
155
157 : GraphicalLogicComponent(makeHalfAdder(),
158 new ThemedSvgItem(":/other_components/H_ADDER.svg"), parent,
159 true)
160{
161 printPortNames = true;
162 setPorts({PortPair{"a", QPoint(20, -20)}, PortPair{"b", QPoint(80, -20)}},
163 {PortPair{"sum", QPoint(50, 120)}, PortPair{"co", QPoint(-10, 40)}});
164}
165
167 : GraphicalLogicComponent(makeFullAdder(),
168 new ThemedSvgItem(":/other_components/F_ADDER.svg"), parent,
169 true)
170{
171 printPortNames = true;
172 setPorts({PortPair{"a", QPoint(20, -20)}, PortPair{"b", QPoint(80, -20)},
173 PortPair{"ci", QPoint(110, 40)}},
174 {PortPair{"sum", QPoint(50, 120)}, PortPair{"co", QPoint(-10, 40)}});
175}
176
179 makeAdderNBits(), new ThemedSvgItem(":/other_components/ADDER.svg"), parent, true)
180{
181 printPortNames = true;
182 setPorts({PortPair{"a", QPoint(20, -20)}, PortPair{"b", QPoint(80, -20)}},
183 {PortPair{"sum", QPoint(50, 120)}, PortPair{"of", QPoint(110, 40)}});
184}
185
188 std::make_shared<Multiplier>(std::array<Bus, 2>{Bus(4), Bus(4)}, Bus(4)),
189 new ThemedSvgItem(":/other_components/MULTIPLIER.svg"), parent, true)
190{
191 printPortNames = true;
192 setPorts({PortPair{"a", QPoint(20, -20)}, PortPair{"b", QPoint(80, -20)}},
193 {PortPair{"product", QPoint(50, 120)}});
194}
195
198 std::make_shared<Divider>(std::array<Bus, 2>{Bus(4), Bus(4)}, Bus(4), Bus(4)),
199 new ThemedSvgItem(":/other_components/DIVIDER.svg"), parent, true)
200{
201 printPortNames = true;
202 setPorts({PortPair{"a", QPoint(20, -20)}, PortPair{"b", QPoint(80, -20)}},
203 {PortPair{"div", QPoint(50, 120)}, PortPair{"mod", QPoint(110, 40)}});
204}
205
208 makeShifter(), new ThemedSvgItem(":/other_components/SHIFTR.svg"), parent, true)
209{
211 setPorts({PortPair{"value", QPoint(-20, 20)}, PortPair{"amount", QPoint(40, -20)}},
212 {PortPair{"result", QPoint(90, 20)}});
213}
214
216{
218 return;
219
220 associatedComponent->setPropertyCallback("mode", [this](const PropertyValue& value) {
221 return applyMode(std::get<std::string>(value));
222 });
223}
224
225std::string GraphicalShifter::applyMode(std::string mode)
226{
227 if (mode != Shifter::LEFT_MODE && mode != Shifter::RIGHT_MODE)
228 throw std::invalid_argument("Shifter mode must be 'left' or 'right'");
229
231 ? ":/other_components/SHIFTL.svg"
232 : ":/other_components/SHIFTR.svg"));
233 return mode;
234}
235
237{
239 if (!component)
240 return;
241
243 applyMode(component->getPropertyValue<std::string>("mode")
244 .value_or(std::string(Shifter::RIGHT_MODE)));
245}
246
248 : GraphicalLogicComponent(makeComparator(), new ComparatorShape("==", parent), parent,
249 true)
250{
251 printPortNames = true;
253 setPorts({PortPair{"a", QPoint(20, -20)}, PortPair{"b", QPoint(80, -20)}},
254 {PortPair{"", QPoint(50, 70)}});
255}
256
258{
260 return;
261
262 associatedComponent->setPropertyCallback("mode", [this](const PropertyValue& value) {
263 return applyMode(std::get<std::string>(value));
264 });
265}
266
267std::string GraphicalComparator::applyMode(std::string mode)
268{
269 if (auto* shape = dynamic_cast<ComparatorShape*>(getItemShape()))
270 shape->setMode(QString::fromStdString(mode));
271 return mode;
272}
273
275{
277 if (!component)
278 return;
279
281 applyMode(component->getPropertyValue<std::string>("mode").value_or("=="));
282}
283
284} // namespace SILICON::ui
static constexpr std::string_view RIGHT_MODE
static constexpr std::string_view LEFT_MODE
GraphicalAdderNBits(QGraphicsItem *parent=nullptr)
void setComponent(const Component_ptr &component) override
Sets the associated logical component.
GraphicalComparator(QGraphicsItem *parent=nullptr)
std::string applyMode(std::string mode)
GraphicalComplementer(QGraphicsItem *parent=nullptr)
QGraphicsItem * getItemShape() const
Gets the component shape.
bool printPortNames
Whether to print port names.
void setItemShape(QGraphicsItem *shape)
Sets the component shape graphics item.
GraphicalDivider(QGraphicsItem *parent=nullptr)
GraphicalExtender(QGraphicsItem *parent=nullptr)
GraphicalFullAdder(QGraphicsItem *parent=nullptr)
GraphicalHalfAdder(QGraphicsItem *parent=nullptr)
void setPorts(const std::vector< PortPair > &busToPortInputs, const std::vector< PortPair > &busToPortOutputs) override
Sets ports and validates against component I/O.
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.
GraphicalMultiplier(QGraphicsItem *parent=nullptr)
GraphicalShifter(QGraphicsItem *parent=nullptr)
void setComponent(const Component_ptr &component) override
Sets the associated logical component.
std::string applyMode(std::string mode)
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