SILICON
Loading...
Searching...
No Matches
graphicalUtils.cpp
Go to the documentation of this file.
2
3#include <ui/shell/theme.hpp>
4
5
6namespace SILICON::ui {
7namespace {
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();
16 setPen(themedPen);
17 QGraphicsPathItem::paint(painter, option, widget);
18 }
19 };
20} // namespace
21using namespace SILICON::core;
22using namespace SILICON::extra;
23
25 : GraphicalLogicComponent(std::make_shared<WireSplitter>(Bus(), std::vector<Bus>{}),
26 nullptr, parent)
27{
28 setSize(associatedComponent->getPropertyValue<int>("size").value_or(2));
30};
31
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
49{
51 setSize(component ? component->getPropertyValue<int>("size").value_or(2) : 2);
53}
54
55int 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
92 : GraphicalLogicComponent(std::make_shared<WireMerger>(std::vector<Bus>{}, Bus()),
93 nullptr, parent)
94{
95 setSize(associatedComponent->getPropertyValue<int>("size").value_or(2));
97};
98
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
116{
118 setSize(component ? component->getPropertyValue<int>("size").value_or(2) : 2);
120}
121
122int 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
int setSize(int newSize)
Definition utils.cpp:215
std::string setWidths(const std::string &specification)
Definition utils.cpp:228
int setSize(int newSize)
Definition utils.cpp:109
std::string setSlices(const std::string &specification)
Definition utils.cpp:128
std::vector< Port * > outputPorts
Output connection ports.
std::vector< Port * > inputPorts
Input connection ports.
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.
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.
GraphicalWireMerger(QGraphicsItem *parent=nullptr)
void setComponent(const Component_ptr &component) override
Sets the associated logical component.
GraphicalWireSplitter(QGraphicsItem *parent=nullptr)
void setComponent(const Component_ptr &component) override
Sets the associated logical component.
static const QColor & getColor(theme::ColorToken token)
Definition theme.hpp:92
std::shared_ptr< Component > Component_ptr
std::variant< int, bool, std::string, BusValue > PropertyValue
Variant type for component properties.
std::pair< QString, QPoint > PortPair