SILICON
Loading...
Searching...
No Matches
graphicalGates.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
18#include "graphicalGates.hpp"
19
20
21namespace SILICON::ui {
22using namespace SILICON::core;
23
24GraphicalGate::GraphicalGate(const std::shared_ptr<Gate> gate, QGraphicsItem* shape,
25 QGraphicsItem* parent, bool scanShape)
26 : GraphicalLogicComponent(gate, shape, parent, scanShape)
27{
28 isEditable = false;
30}
31
37
39{
41 return;
42
43 const auto count = associatedComponent->inputBuses().size();
44 if (count == 0 || associatedComponent->outputBuses().size() != 1)
45 throw std::logic_error("GraphicalGate requires inputs and one output");
46
47 prepareGeometryChange();
48 const int height = 20 * static_cast<int>(count);
49 getItemShape()->setTransform(QTransform::fromScale(1, height / 40.0));
50
51 if (inputPorts.size() != count || outputPorts.empty()) {
52 std::vector<PortPair> inputVec;
53 inputVec.reserve(count);
54 for (std::size_t i = 0; i < count; ++i) {
55 const QString name = count == 1 ? QStringLiteral("i")
56 : count == 2 ? (i == 0 ? QStringLiteral("a")
57 : QStringLiteral("b"))
58 : QString("i%1").arg(i);
59 inputVec.emplace_back(name, QPoint(-20, 10 + 20 * static_cast<int>(i)));
60 }
61 setPorts(inputVec, {PortPair{"o", QPoint(80, height / 2)}});
62 }
63}
64
70
71GraphicalNot::GraphicalNot(QGraphicsItem* parent)
72 : GraphicalLogicComponent(std::make_shared<NotGate>(nullptr, nullptr),
73 new ThemedSvgItem(":/gates/NOT_ANSI.svg"), parent)
74{
75 isEditable = false;
76
77 setPorts({PortPair{"i", QPoint(-20, 20)}}, {PortPair{"o", QPoint(80, 20)}});
78}
79
80} // namespace SILICON::ui
std::vector< Port * > outputPorts
Output connection ports.
QGraphicsItem * getItemShape() const
Gets the component shape.
std::vector< Port * > inputPorts
Input connection ports.
bool scanShape
Whether to scan shape alpha for port placement.
GraphicalGate(const std::shared_ptr< Gate > gate, QGraphicsItem *shape, QGraphicsItem *parent=nullptr, bool scanShape=false)
void setComponent(const Component_ptr &component) override
Sets the associated logical component.
void updatePortSizes() override
Copies associated component bus sizes into graphical ports.
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.
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.
GraphicalNot(QGraphicsItem *parent=nullptr)
std::shared_ptr< Component > Component_ptr
std::pair< QString, QPoint > PortPair