SILICON
Loading...
Searching...
No Matches
graphicalMemory.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
18#include "graphicalMemory.hpp"
19
20#include <memory>
21#include <stdexcept>
22
23#include <QGraphicsRectItem>
24#include <QPainter>
25#include <QTextOption>
26
28#include <ui/shell/theme.hpp>
29
30namespace SILICON::ui {
31using namespace SILICON::core;
32
33namespace {
34
35 constexpr QRectF RomBodyRect(0.0, 0.0, 100.0, 80.0);
36
37 class RomShape final : public QGraphicsRectItem {
38 public:
39 explicit RomShape(QGraphicsItem* parent = nullptr)
40 : QGraphicsRectItem(RomBodyRect, parent)
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
50 painter->save();
51 painter->setRenderHint(QPainter::Antialiasing, false);
52 painter->setPen(QPen(ThemeEngine::getColor(theme::ColorToken::Ink), 3));
54 painter->drawRect(rect());
55 painter->setPen(QPen(ThemeEngine::getColor(theme::ColorToken::Ink)));
56 painter->setFont(QFont("Quicksand", 10, QFont::Bold));
57 painter->drawText(rect(), QStringLiteral("ROM"), QTextOption(Qt::AlignCenter));
58 painter->restore();
59 }
60 };
61
62 std::shared_ptr<ROM> makeRom()
63 {
64 return std::make_shared<ROM>(Bus(1), std::make_shared<Wire>(),
65 std::make_shared<Wire>(), Bus(8));
66 }
67
68} // namespace
69
70GraphicalROM::GraphicalROM(QGraphicsItem* parent)
71 : GraphicalLogicComponent(makeRom(), new RomShape(), parent)
72{
73 isEditable = true;
74 printPortNames = true;
75 setPorts({PortPair{"addr", QPoint(-20, 20)}, PortPair{"CS", QPoint(-20, 40)},
76 PortPair{"OE", QPoint(-20, 60)}},
77 {PortPair{"d", QPoint(120, 40)}});
78}
79
84
96
98{
100 const auto rom = std::dynamic_pointer_cast<ROM>(component);
101 if (!rom)
102 throw std::logic_error("GraphicalROM requires a ROM component");
103
104 if (documents)
106}
107
108void GraphicalROM::applyProperty(const std::string_view key, const PropertyValue& value)
109{
110 if (key != "binaryContents") {
112 return;
113 }
114
115 const auto rom = std::dynamic_pointer_cast<ROM>(associatedComponent);
116 if (!rom)
117 return;
118
119 const auto& slug = std::get<std::string>(value);
120 prepareGeometryChange();
121 rom->setBinaryDocument(slug, resolveBinaryContents(slug));
122 update();
123}
124
126{
127 if (documents == newDocuments)
128 return;
130 documents = newDocuments;
133}
134
135std::string GraphicalROM::currentSlug() const
136{
138 return {};
139 return associatedComponent->getPropertyValue<std::string>("binaryContents")
140 .value_or(std::string());
141}
142
144{
145 if (!documents || documentListenerId != 0)
146 return;
148 documents->addListener([this](const SILICON::project::DocumentChange& change) {
149 const auto slug = currentSlug();
150 const bool affectsConfiguredDocument =
152 && *change.path
156 || affectsConfiguredDocument)
158 });
159}
160
167
169{
170 if (const auto rom = std::dynamic_pointer_cast<ROM>(associatedComponent))
171 rom->refreshBinaryContents(resolveBinaryContents(currentSlug()));
172}
173
174std::shared_ptr<const std::string>
175GraphicalROM::resolveBinaryContents(const std::string_view slug) const
176{
178 return nullptr;
179
182 const auto* document = documents->find(path);
183 if (!document)
184 return nullptr;
185
186 return std::make_shared<const std::string>(document->getContents());
187}
188
189} // namespace SILICON::ui
bool printPortNames
Whether to print port names.
void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) override
Handles double-clicks not consumed by a component-specific action.
bool openDocumentOnDoubleClick(QGraphicsSceneMouseEvent *event, const std::string &path)
Queues document navigation for a left double-click in normal mode.
void setPorts(const std::vector< PortPair > &busToPortInputs, const std::vector< PortPair > &busToPortOutputs) override
Sets ports and validates against component I/O.
void applyProperty(std::string_view key, const PropertyValue &value) override
Applies a component property while notifying Qt about possible geometry changes.
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.
const SILICON::project::DocumentStore * documents
GraphicalROM(QGraphicsItem *parent=nullptr)
void applyProperty(std::string_view key, const SILICON::core::PropertyValue &value) override
Applies a component property while notifying Qt about possible geometry changes.
void setDocumentStore(const SILICON::project::DocumentStore *documents)
std::shared_ptr< const std::string > resolveBinaryContents(std::string_view slug) const
void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) override
void setComponent(const SILICON::core::Component_ptr &component) override
Sets the associated logical component.
std::string currentSlug() const
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::string documentPathForSlug(const DocumentType type, const std::string_view slug)
bool isValidDocumentSlug(const std::string_view slug)
std::pair< QString, QPoint > PortPair
std::optional< std::string > path