SILICON
Loading...
Searching...
No Matches
graphicalFlipFlops.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
19
20#include <ui/shell/theme.hpp>
21
22#include <QGraphicsRectItem>
23#include <QPainter>
24#include <QTextOption>
25
26
27namespace SILICON::ui {
28using namespace SILICON::core;
29
30namespace {
31
32constexpr QRectF FlipFlopBodyRect(0.0, 0.0, 80.0, 80.0);
33
34std::shared_ptr<DFlipFlop> makeDFlipFlop()
35{
36 return std::make_shared<DFlipFlop>(Wire_ptr{}, Wire_ptr{}, Wire_ptr{}, Wire_ptr{},
37 Wire_ptr{}, Wire_ptr{});
38}
39
40std::shared_ptr<EFlipFlop> makeEFlipFlop()
41{
42 return std::make_shared<EFlipFlop>(Wire_ptr{}, Wire_ptr{}, Wire_ptr{}, Wire_ptr{},
43 Wire_ptr{}, Wire_ptr{}, Wire_ptr{});
44}
45
46std::shared_ptr<DLatch> makeDLatch()
47{
48 return std::make_shared<DLatch>(Wire_ptr{}, Wire_ptr{}, Wire_ptr{}, Wire_ptr{});
49}
50
51std::shared_ptr<JKFlipFlop> makeJKFlipFlop()
52{
53 return std::make_shared<JKFlipFlop>(Wire_ptr{}, Wire_ptr{}, Wire_ptr{}, Wire_ptr{},
54 Wire_ptr{}, Wire_ptr{}, Wire_ptr{});
55}
56
57class FlipFlopShape : public QGraphicsRectItem {
58public:
59 explicit FlipFlopShape(QGraphicsItem* parent = nullptr)
60 : QGraphicsRectItem(FlipFlopBodyRect, parent)
61 {
62 }
63
64 void paint(QPainter* painter, const QStyleOptionGraphicsItem* option,
65 QWidget* widget) override
66 {
67 Q_UNUSED(option);
68 Q_UNUSED(widget);
69
71
72 painter->setRenderHint(QPainter::Antialiasing, false);
73 painter->setPen(QPen(ink, 3));
75 painter->drawRect(rect());
76 }
77};
78
79} // namespace
80
82 QGraphicsItem* parent)
83 : GraphicalLogicComponent(component, new FlipFlopShape(parent), parent)
84{
85 isEditable = false;
86 printPortNames = true;
87}
88
90 : GraphicalFlipFlop(makeDFlipFlop(), parent)
91{
92 setPorts({PortPair{"d", QPoint(-20, 20)}, PortPair{"clk", QPoint(-20, 60)},
93 PortPair{"clr", QPoint(40, -20)}, PortPair{"pre", QPoint(40, 100)}},
94 {PortPair{"q", QPoint(100, 20)}, PortPair{"!q", QPoint(100, 60)}});
95}
96
98 : GraphicalFlipFlop(makeEFlipFlop(), parent)
99{
100 setPorts({PortPair{"d", QPoint(-20, 20)}, PortPair{"en", QPoint(-20, 40)},
101 PortPair{"clk", QPoint(-20, 60)}, PortPair{"clr", QPoint(40, -20)},
102 PortPair{"pre", QPoint(40, 100)}},
103 {PortPair{"q", QPoint(100, 20)}, PortPair{"!q", QPoint(100, 60)}});
104}
105
107 : GraphicalFlipFlop(makeDLatch(), parent)
108{
109 setPorts({PortPair{"d", QPoint(-20, 20)}, PortPair{"en", QPoint(-20, 60)}},
110 {PortPair{"q", QPoint(100, 20)}, PortPair{"!q", QPoint(100, 60)}});
111}
112
114 : GraphicalFlipFlop(makeJKFlipFlop(), parent)
115{
116 setPorts({PortPair{"j", QPoint(-20, 20)}, PortPair{"k", QPoint(-20, 40)},
117 PortPair{"clk", QPoint(-20, 60)}, PortPair{"clr", QPoint(40, -20)},
118 PortPair{"pre", QPoint(40, 100)}},
119 {PortPair{"q", QPoint(100, 20)}, PortPair{"!q", QPoint(100, 60)}});
120}
121
122} // namespace SILICON::ui
bool printPortNames
Whether to print port names.
GraphicalDFlipFlop(QGraphicsItem *parent=nullptr)
GraphicalDLatch(QGraphicsItem *parent=nullptr)
GraphicalEFlipFlop(QGraphicsItem *parent=nullptr)
GraphicalFlipFlop(const Component_ptr &component, QGraphicsItem *parent=nullptr)
GraphicalJKFlipFlop(QGraphicsItem *parent=nullptr)
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)
static const QColor & getColor(theme::ColorToken token)
Definition theme.hpp:92
std::shared_ptr< Component > Component_ptr
std::shared_ptr< Wire > Wire_ptr
Definition wire.hpp:116
std::pair< QString, QPoint > PortPair