1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
|
// Copyright 2015 - 2025, GIBIS-UNIFESP and the wiRedPanda contributors
// SPDX-License-Identifier: GPL-3.0-or-later
#include "truth_table.h"
#include "common.h"
#include "globalproperties.h"
#include "qneport.h"
#include <QPainter>
TruthTable::TruthTable(QGraphicsItem *parent)
: GraphicElement(ElementType::TruthTable, ElementGroup::IC, ":/basic/truthtable-rotated.svg", tr("TRUTH TABLE"), tr("Truth Table"), 2, 8, 1, 8, parent)
{
if (GlobalProperties::skipInit) {
return;
}
m_defaultSkins << m_pixmapPath;
m_alternativeSkins = m_defaultSkins;
setPixmap(0);
setHasTruthTable(true);
setCanChangeSkin(true);
setHasLabel(true);
m_key.resize(2048);
m_key.fill(0);
TruthTable::updatePortsProperties();
TruthTable::generatePixmap();
}
void TruthTable::updatePortsProperties()
{
int index = 0;
const int step = GlobalProperties::gridSize / 2;
if (!m_inputPorts.isEmpty()) {
int y = 32 - (m_inputPorts.size() * step) + step;
for (auto *port : std::as_const(m_inputPorts)) {
if (!isRotatable()) {
port->setRotation(0);
}
port->setPos(0, y);
y += step * 2;
port->setName(QChar::fromLatin1(static_cast<char>('A' + index)));
++index;
}
}
index = 0;
if (!m_outputPorts.isEmpty()) {
int y = 32 - (m_outputPorts.size() * step) + step;
for (auto *port : std::as_const(m_outputPorts)) {
if (!isRotatable()) {
port->setRotation(0);
}
port->setPos(64, y);
y += step * 2;
port->setName("S" + QString::number(index));
++index;
}
}
}
void TruthTable::generatePixmap()
{
// make pixmap
const QSize size = portsBoundingRect().united(QRectF(0, 0, 64, 64)).size().toSize();
QPixmap tempPixmap(size);
tempPixmap.fill(Qt::transparent);
QPainter tmpPainter(&tempPixmap);
tmpPainter.setBrush(QColor(126, 126, 126));
tmpPainter.setPen(QPen(QBrush(QColor(78, 78, 78)), 0.5, Qt::SolidLine));
// draw package
QPoint topLeft = tempPixmap.rect().topLeft();
topLeft.setX(topLeft.x() + 7);
QSize finalSize = tempPixmap.rect().size();
finalSize.setWidth(finalSize.width() - 14);
QRectF finalRect = QRectF(topLeft, finalSize);
tmpPainter.drawRoundedRect(finalRect, 3, 3);
QPixmap panda(":/basic/truthtable-rotated.svg");
QPointF pandaOrigin = finalRect.center();
pandaOrigin.setX(pandaOrigin.x() - panda.width() / 2);
pandaOrigin.setY(pandaOrigin.y() - panda.height() / 2);
tmpPainter.drawPixmap(pandaOrigin, panda);
// draw shadow
tmpPainter.setBrush(QColor(78, 78, 78));
tmpPainter.setPen(QPen(QBrush(QColor(78, 78, 78)), 0.5, Qt::SolidLine));
QRectF shadowRect(finalRect.bottomLeft(), finalRect.bottomRight());
shadowRect.adjust(0, -3, 0, 0);
tmpPainter.drawRoundedRect(shadowRect, 3, 3);
m_pixmap = tempPixmap;
GraphicElement::update();
}
void TruthTable::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
Q_UNUSED(widget)
Q_UNUSED(option)
TruthTable::updatePortsProperties();
if (isSelected()) {
painter->save();
painter->setBrush(m_selectionBrush);
painter->setPen(QPen(m_selectionPen, 0.5, Qt::SolidLine));
painter->drawRoundedRect(portsBoundingRect().united(QRectF(0, 0, 64, 64)), 5, 5);
painter->restore();
}
const qreal bottom = portsBoundingRect().united(QRectF(0, 0, 64, 64)).bottom();
m_label->setPos(30, bottom + 5);
generatePixmap();
painter->drawPixmap(boundingRect().topLeft(), pixmap());
}
QBitArray &TruthTable::key()
{
return m_key;
}
void TruthTable::setkey(const QBitArray &key)
{
m_key = key;
}
void TruthTable::save(QDataStream &stream) const
{
GraphicElement::save(stream);
QMap<QString, QVariant> map;
map.insert("key", m_key);
stream << map;
}
void TruthTable::load(QDataStream &stream, QMap<quint64, QNEPort *> &portMap, const QVersionNumber version)
{
GraphicElement::load(stream, portMap, version);
if (version >= VERSION("4.2")) {
QMap<QString, QVariant> map; stream >> map;
if (map.contains("key")) {
setkey(map.value("key").toBitArray());
}
}
}
|