SILICON
Toggle main menu visibility
Loading...
Searching...
No Matches
graphicalUtils.cpp
Go to the documentation of this file.
1
#include <
ui/circuit/components/graphicalUtils.hpp
>
2
3
#include <
ui/shell/theme.hpp
>
4
5
6
namespace
SILICON::ui
{
7
namespace
{
8
class
ThemedPathItem :
public
QGraphicsPathItem {
9
public
:
10
using
QGraphicsPathItem::QGraphicsPathItem;
11
void
paint(QPainter* painter,
const
QStyleOptionGraphicsItem* option,
12
QWidget* widget)
override
13
{
14
auto
themedPen = pen();
15
themedPen.setColor(
ThemeEngine::getColor
(
theme::ColorToken::Ink
));
16
setPen(themedPen);
17
QGraphicsPathItem::paint(painter, option, widget);
18
}
19
};
20
}
// namespace
21
using namespace
SILICON::core;
22
using namespace
SILICON::extra;
23
24
GraphicalWireSplitter::GraphicalWireSplitter
(QGraphicsItem* parent)
25
:
GraphicalLogicComponent
(std::make_shared<
WireSplitter
>(
Bus
(), std::vector<
Bus
>{}),
26
nullptr
, parent)
27
{
28
setSize
(
associatedComponent
->getPropertyValue<
int
>(
"size"
).value_or(2));
29
installSizeCallback
();
30
};
31
32
void
GraphicalWireSplitter::installSizeCallback
()
33
{
34
this->
associatedComponent
->setPropertyCallback(
"size"
,
35
[
this
](
const
PropertyValue
& value) {
36
int
newSize = std::get<int>(value);
37
return
this->
setSize
(newSize);
38
});
39
this->
associatedComponent
->setPropertyCallback(
40
"slices"
, [
this
](
const
PropertyValue
& value) {
41
const
auto
logical =
dynamic_cast<
WireSplitter
*
>
(
associatedComponent
.get());
42
const
auto
result = logical->
setSlices
(std::get<std::string>(value));
43
setSize
(logical->getPropertyValue<
int
>(
"size"
).value_or(2));
44
return
result;
45
});
46
}
47
48
void
GraphicalWireSplitter::setComponent
(
const
Component_ptr
& component)
49
{
50
GraphicalLogicComponent::setComponent
(component);
51
setSize
(component ? component->getPropertyValue<
int
>(
"size"
).value_or(2) : 2);
52
installSizeCallback
();
53
}
54
55
int
GraphicalWireSplitter::setSize
(
const
int
newSize)
56
{
57
if
(newSize < 1)
58
return
static_cast<
int
>
(this->
size
);
59
60
const
auto
logicalWireSplitter =
dynamic_cast<
WireSplitter
*
>
(
associatedComponent
.get());
61
logicalWireSplitter->
setSize
(newSize);
62
this->
size
= newSize;
63
64
QPainterPath path{};
65
path.moveTo(0, 0);
66
67
std::vector<PortPair>
outputPorts
;
68
const
auto
slices = logicalWireSplitter->outputSlices();
69
outputPorts
.reserve(slices.size());
70
71
for
(std::size_t i = 0; i < slices.size(); ++i) {
72
QPoint portLoc(10, -20 *
static_cast<
int
>
(i) - 10);
73
path.lineTo(portLoc);
74
75
const
auto
[offset, width] = slices[i];
76
const
auto
label = width == 1
77
? QString(
"b[%1]"
).arg(offset)
78
: QString(
"b[%1:%2]"
).arg(offset + width - 1).arg(offset);
79
outputPorts
.emplace_back(label, portLoc + QPoint(20, 0));
80
}
81
82
auto
shape =
new
ThemedPathItem(path,
this
);
83
shape->setPen(QPen(
ThemeEngine::getColor
(
theme::ColorToken::Ink
), 3));
84
85
this->
setItemShape
(shape);
86
this->
setPorts
({
PortPair
{
"b"
, QPoint(-20, 0)}},
outputPorts
);
87
88
return
newSize;
89
}
90
91
GraphicalWireMerger::GraphicalWireMerger
(QGraphicsItem* parent)
92
:
GraphicalLogicComponent
(std::make_shared<
WireMerger
>(std::vector<
Bus
>{},
Bus
()),
93
nullptr
, parent)
94
{
95
setSize
(
associatedComponent
->getPropertyValue<
int
>(
"size"
).value_or(2));
96
installSizeCallback
();
97
};
98
99
void
GraphicalWireMerger::installSizeCallback
()
100
{
101
this->
associatedComponent
->setPropertyCallback(
102
"size"
, [
this
](
const
PropertyValue
& value) {
103
const
int
newSize = std::get<int>(value);
104
return
this->
setSize
(newSize);
105
});
106
this->
associatedComponent
->setPropertyCallback(
107
"widths"
, [
this
](
const
PropertyValue
& value) {
108
const
auto
logical =
dynamic_cast<
WireMerger
*
>
(
associatedComponent
.get());
109
const
auto
result = logical->
setWidths
(std::get<std::string>(value));
110
setSize
(logical->getPropertyValue<
int
>(
"size"
).value_or(2));
111
return
result;
112
});
113
}
114
115
void
GraphicalWireMerger::setComponent
(
const
Component_ptr
& component)
116
{
117
GraphicalLogicComponent::setComponent
(component);
118
setSize
(component ? component->getPropertyValue<
int
>(
"size"
).value_or(2) : 2);
119
installSizeCallback
();
120
}
121
122
int
GraphicalWireMerger::setSize
(
const
int
newSize)
123
{
124
if
(newSize < 1)
125
return
this->
size
;
126
const
auto
logicalWireMerger =
dynamic_cast<
WireMerger
*
>
(
associatedComponent
.get());
127
logicalWireMerger->
setSize
(newSize);
128
this->
size
= newSize;
129
130
QPainterPath path{};
131
path.moveTo(0, 0);
132
133
std::vector<PortPair>
inputPorts
;
134
const
auto
& inputs = logicalWireMerger->inputBuses();
135
inputPorts
.reserve(inputs.size());
136
137
unsigned
int
offset = 0;
138
for
(std::size_t i = 0; i < inputs.size(); ++i) {
139
QPoint portLoc(-10, -20 *
static_cast<
int
>
(i) - 10);
140
path.lineTo(portLoc);
141
142
const
auto
width = inputs[i].size();
143
const
auto
label = width == 1
144
? QString(
"b[%1]"
).arg(offset)
145
: QString(
"b[%1:%2]"
).arg(offset + width - 1).arg(offset);
146
inputPorts
.emplace_back(label, portLoc + QPoint(-20, 0));
147
offset += width;
148
}
149
150
auto
shape =
new
ThemedPathItem(path,
this
);
151
shape->setPen(QPen(
ThemeEngine::getColor
(
theme::ColorToken::Ink
), 3));
152
153
this->
setItemShape
(shape);
154
this->
setPorts
(inputPorts, {
PortPair
{
"b"
, QPoint(20, 0)}});
155
156
return
newSize;
157
}
158
159
}
// namespace SILICON::ui
SILICON::core::Bus
Definition
wire.hpp:138
SILICON::extra::WireMerger
Definition
utils.hpp:59
SILICON::extra::WireMerger::setSize
int setSize(int newSize)
Definition
utils.cpp:215
SILICON::extra::WireMerger::setWidths
std::string setWidths(const std::string &specification)
Definition
utils.cpp:228
SILICON::extra::WireSplitter
Definition
utils.hpp:30
SILICON::extra::WireSplitter::setSize
int setSize(int newSize)
Definition
utils.cpp:109
SILICON::extra::WireSplitter::setSlices
std::string setSlices(const std::string &specification)
Definition
utils.cpp:128
SILICON::ui::GraphicalComponent::outputPorts
std::vector< Port * > outputPorts
Output connection ports.
Definition
graphicalComponent.hpp:245
SILICON::ui::GraphicalComponent::inputPorts
std::vector< Port * > inputPorts
Input connection ports.
Definition
graphicalComponent.hpp:242
SILICON::ui::GraphicalComponent::setItemShape
void setItemShape(QGraphicsItem *shape)
Sets the component shape graphics item.
Definition
graphicalComponent.cpp:246
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::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::GraphicalWireMerger::installSizeCallback
void installSizeCallback()
Definition
graphicalUtils.cpp:99
SILICON::ui::GraphicalWireMerger::setSize
int setSize(int newSize)
Definition
graphicalUtils.cpp:122
SILICON::ui::GraphicalWireMerger::GraphicalWireMerger
GraphicalWireMerger(QGraphicsItem *parent=nullptr)
Definition
graphicalUtils.cpp:91
SILICON::ui::GraphicalWireMerger::setComponent
void setComponent(const Component_ptr &component) override
Sets the associated logical component.
Definition
graphicalUtils.cpp:115
SILICON::ui::GraphicalWireMerger::size
unsigned int size
Definition
graphicalUtils.hpp:58
SILICON::ui::GraphicalWireSplitter::installSizeCallback
void installSizeCallback()
Definition
graphicalUtils.cpp:32
SILICON::ui::GraphicalWireSplitter::setSize
int setSize(int newSize)
Definition
graphicalUtils.cpp:55
SILICON::ui::GraphicalWireSplitter::GraphicalWireSplitter
GraphicalWireSplitter(QGraphicsItem *parent=nullptr)
Definition
graphicalUtils.cpp:24
SILICON::ui::GraphicalWireSplitter::setComponent
void setComponent(const Component_ptr &component) override
Sets the associated logical component.
Definition
graphicalUtils.cpp:48
SILICON::ui::GraphicalWireSplitter::size
unsigned int size
Definition
graphicalUtils.hpp:44
SILICON::ui::ThemeEngine::getColor
static const QColor & getColor(theme::ColorToken token)
Definition
theme.hpp:92
graphicalUtils.hpp
SILICON::core::Component_ptr
std::shared_ptr< Component > Component_ptr
Definition
component_registry.hpp:33
SILICON::core::PropertyValue
std::variant< int, bool, std::string, BusValue > PropertyValue
Variant type for component properties.
Definition
component.hpp:142
SILICON::ui::theme::ColorToken::Ink
@ Ink
Definition
theme.hpp:40
SILICON::ui
Definition
actionGroups.hpp:14
SILICON::ui::PortPair
std::pair< QString, QPoint > PortPair
Definition
graphicalComponent.hpp:42
theme.hpp
src
ui
circuit
components
graphicalUtils.cpp
Generated by
1.17.0