SILICON
Toggle main menu visibility
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
21
namespace
SILICON::ui
{
22
using namespace
SILICON::core;
23
24
GraphicalGate::GraphicalGate
(
const
std::shared_ptr<Gate> gate, QGraphicsItem* shape,
25
QGraphicsItem* parent,
bool
scanShape
)
26
:
GraphicalLogicComponent
(gate, shape, parent,
scanShape
)
27
{
28
isEditable
=
false
;
29
updateGatePorts
();
30
}
31
32
void
GraphicalGate::setComponent
(
const
Component_ptr
& component)
33
{
34
GraphicalLogicComponent::setComponent
(component);
35
updateGatePorts
();
36
}
37
38
void
GraphicalGate::updateGatePorts
()
39
{
40
if
(!
associatedComponent
)
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
65
void
GraphicalGate::updatePortSizes
()
66
{
67
updateGatePorts
();
68
GraphicalLogicComponent::updatePortSizes
();
69
}
70
71
GraphicalNot::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
SILICON::core::NotGate
Definition
gates.hpp:112
SILICON::ui::GraphicalComponent::outputPorts
std::vector< Port * > outputPorts
Output connection ports.
Definition
graphicalComponent.hpp:245
SILICON::ui::GraphicalComponent::getItemShape
QGraphicsItem * getItemShape() const
Gets the component shape.
Definition
graphicalComponent.hpp:208
SILICON::ui::GraphicalComponent::inputPorts
std::vector< Port * > inputPorts
Input connection ports.
Definition
graphicalComponent.hpp:242
SILICON::ui::GraphicalComponent::scanShape
bool scanShape
Whether to scan shape alpha for port placement.
Definition
graphicalComponent.hpp:256
SILICON::ui::GraphicalGate::GraphicalGate
GraphicalGate(const std::shared_ptr< Gate > gate, QGraphicsItem *shape, QGraphicsItem *parent=nullptr, bool scanShape=false)
Definition
graphicalGates.cpp:24
SILICON::ui::GraphicalGate::setComponent
void setComponent(const Component_ptr &component) override
Sets the associated logical component.
Definition
graphicalGates.cpp:32
SILICON::ui::GraphicalGate::updateGatePorts
void updateGatePorts()
Definition
graphicalGates.cpp:38
SILICON::ui::GraphicalGate::updatePortSizes
void updatePortSizes() override
Copies associated component bus sizes into graphical ports.
Definition
graphicalGates.cpp:65
SILICON::ui::GraphicalLogicComponent::updatePortSizes
virtual void updatePortSizes()
Copies associated component bus sizes into graphical ports.
Definition
graphicalLogicComponent.cpp:62
SILICON::ui::GraphicalLogicComponent::setPorts
void setPorts(const std::vector< PortPair > &busToPortInputs, const std::vector< PortPair > &busToPortOutputs) override
Sets ports and validates against component I/O.
Definition
graphicalLogicComponent.cpp:143
SILICON::ui::GraphicalLogicComponent::isEditable
bool isEditable
Whether the component allows property editing.
Definition
graphicalLogicComponent.hpp:65
SILICON::ui::GraphicalLogicComponent::GraphicalLogicComponent
GraphicalLogicComponent(ItemCategory category, const Component_ptr &component, QGraphicsItem *shape, QGraphicsItem *parent, bool scanShape=false)
Definition
graphicalLogicComponent.cpp:26
SILICON::ui::GraphicalLogicComponent::setComponent
virtual void setComponent(const Component_ptr &component)
Sets the associated logical component.
Definition
graphicalLogicComponent.cpp:123
SILICON::ui::GraphicalLogicComponent::associatedComponent
Component_ptr associatedComponent
The logical component this graphical component represents.
Definition
graphicalLogicComponent.hpp:59
SILICON::ui::GraphicalNot::GraphicalNot
GraphicalNot(QGraphicsItem *parent=nullptr)
Definition
graphicalGates.cpp:71
SILICON::ui::ThemedSvgItem
Definition
themedSvgItem.hpp:18
graphicalGates.hpp
nullptr
SILICON::core::Component_ptr
std::shared_ptr< Component > Component_ptr
Definition
component_registry.hpp:33
SILICON::ui
Definition
actionGroups.hpp:14
SILICON::ui::PortPair
std::pair< QString, QPoint > PortPair
Definition
graphicalComponent.hpp:42
src
ui
circuit
components
graphicalGates.cpp
Generated by
1.17.0