diff --git a/include/Block.hpp b/include/Block.hpp index ac86ef7..76a038e 100644 --- a/include/Block.hpp +++ b/include/Block.hpp @@ -17,11 +17,11 @@ namespace nts { Block(QGraphicsItem *parent = 0); - Port *addPort(const QString &name, const Pin* pin, bool isOutput, int flags = 0, int ptr = 0); + Port *addPort(const QString &name, Pin* pin, bool isOutput, int flags = 0, int ptr = 0); - void addInputPort(const QString &name, const Pin *pin); + void addInputPort(const QString &name, Pin *pin); - void addOutputPort(const QString &name, const Pin *pin); + void addOutputPort(const QString &name, Pin *pin); void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); @@ -29,15 +29,17 @@ namespace nts { Port *getPortFromPinId(size_t index); + size_t getPinIdFromPin(Pin *pin); + Block *clone(); QVector ports(); int type() const { return Type; } - const AComponent *getAComponent(); + AComponent *getAComponent(); - void setAComponent(const AComponent *component); + void setAComponent(AComponent *component); protected: QVariant itemChange(GraphicsItemChange change, const QVariant &value); @@ -47,7 +49,7 @@ namespace nts { int vertMargin; int width; int height; - const AComponent *aComponent; + AComponent *aComponent; }; } #endif // QNEBLOCK_H diff --git a/include/Connection.hpp b/include/Connection.hpp index 7b961db..c0e5d26 100644 --- a/include/Connection.hpp +++ b/include/Connection.hpp @@ -2,6 +2,7 @@ #define QNECONNECTION_H #include +#include namespace nts { @@ -25,6 +26,10 @@ namespace nts { void setPort2(Port *p); + Port *getPort1(); + + Port *getPort2(); + void updatePosFromPorts(); void updatePath(); @@ -35,6 +40,7 @@ namespace nts { int type() const { return Type; } + void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); private: ::QPointF pos1; ::QPointF pos2; diff --git a/include/MainWindow.hpp b/include/MainWindow.hpp index 5d153bf..6edf7c4 100644 --- a/include/MainWindow.hpp +++ b/include/MainWindow.hpp @@ -3,6 +3,7 @@ #include #include +#include #include "AComponent.hpp" #include "IComponent.hpp" #include "Pin.hpp" @@ -30,6 +31,8 @@ namespace nts { void loadFile(); + void simulate(); + void addComponent(std::string name); private: @@ -37,7 +40,7 @@ namespace nts { QMenu *fileMenu; EditorView *view; QGraphicsScene *scene; - std::list *blocks; + std::vector *blocks; std::vector getPorts(AComponent *component); }; diff --git a/include/Port.hpp b/include/Port.hpp index 86c40ed..37bb41d 100644 --- a/include/Port.hpp +++ b/include/Port.hpp @@ -43,7 +43,7 @@ namespace nts { int type() const { return Type; } - Block *block() const; + Block *block(); quint64 ptr(); @@ -51,9 +51,9 @@ namespace nts { bool isConnected(Port *); - void setPin(const Pin* pin); + void setPin(Pin* pin); - const Pin *getPin(); + Pin *getPin(); protected: QVariant itemChange(GraphicsItemChange change, const QVariant &value); @@ -67,7 +67,7 @@ namespace nts { QVector m_connections; int m_portFlags; quint64 m_ptr; - const Pin *pin; + Pin *pin; }; } #endif // QNEPORT_H diff --git a/src/components/Pin.cpp b/src/components/Pin.cpp index 396bf47..eef86c3 100644 --- a/src/components/Pin.cpp +++ b/src/components/Pin.cpp @@ -1,42 +1,45 @@ #include "Pin.hpp" -namespace nts -{ - Pin::Pin(Mode _mode) : mode(_mode), component(0), state(UNDEFINED), target_pin(0) - { +namespace nts { + Pin::Pin(Mode _mode) : mode(_mode), component(0), state(UNDEFINED), target_pin(0) { } - Pin::Pin(const Pin &other) : mode(other.getMode()), component(&other.getComponent()), state(other.getState()), target_pin(other.getTargetPin()) - { + Pin::Pin(const Pin &other) : mode(other.getMode()), component(&other.getComponent()), state(other.getState()), + target_pin(other.getTargetPin()) { } - Pin::~Pin() - { + Pin::~Pin() { } - const Pin &Pin::operator=(const Pin &other) - { + const Pin &Pin::operator=(const Pin &other) { this->component = &other.getComponent(); this->state = other.getState(); this->target_pin = other.getTargetPin(); return (*this); } - void Pin::setComponent(IComponent &_component) {this->component = &_component;} + void Pin::setComponent(IComponent &_component) { + printf("setComponent %p\n", &_component); + this->component = &_component; + } - void Pin::setTarget(std::size_t _target_pin) {this->target_pin = _target_pin;} + void Pin::setTarget(std::size_t _target_pin) { this->target_pin = _target_pin; } - void Pin::setState(Tristate _state) {this->state = _state;} + void Pin::setState(Tristate _state) { this->state = _state; } - void Pin::setMode(Mode _mode) {this->mode = _mode;} + void Pin::setMode(Mode _mode) { this->mode = _mode; } - IComponent &Pin::getComponent() const {return (*this->component);} + IComponent &Pin::getComponent() const { return (*this->component); } - std::size_t Pin::getTargetPin() const {return (this->target_pin);} + std::size_t Pin::getTargetPin() const { return (this->target_pin); } - Tristate Pin::getState() const {return (this->state);} + Tristate Pin::getState() const { return (this->state); } - nts::Tristate Pin::compute() {return (this->component->Compute(this->target_pin));} + nts::Tristate Pin::compute() { + if (this->component == NULL) + return nts::Tristate::UNDEFINED; + return (this->component->Compute(this->target_pin)); + } - Pin::Mode Pin::getMode() const {return (this->mode);} + Pin::Mode Pin::getMode() const { return (this->mode); } } diff --git a/src/editor/Editor.cpp b/src/editor/Editor.cpp index a7817d0..b47bf06 100644 --- a/src/editor/Editor.cpp +++ b/src/editor/Editor.cpp @@ -81,6 +81,17 @@ namespace nts { !port1->isConnected(port2)) { conn->setPos2(port2->scenePos()); conn->setPort2(port2); + + if (conn->getPort1()->getPin()->getMode() == Pin::Mode::O && conn->getPort2()->getPin()->getMode() == Pin::Mode::I) { + conn->getPort2()->getPin()->setComponent(*conn->getPort1()->block()->getAComponent()); + conn->getPort2()->getPin()->setTarget(conn->getPort1()->block()->getPinIdFromPin(conn->getPort1()->getPin())); + conn->getPort2()->getPin()->setState(conn->getPort2()->getPin()->compute()); + } else if (conn->getPort1()->getPin()->getMode() == Pin::Mode::I && conn->getPort2()->getPin()->getMode() == Pin::Mode::O) { + conn->getPort1()->getPin()->setComponent(*conn->getPort2()->block()->getAComponent()); + conn->getPort1()->getPin()->setTarget(conn->getPort2()->block()->getPinIdFromPin(conn->getPort2()->getPin())); + conn->getPort1()->getPin()->setState(conn->getPort1()->getPin()->compute()); + } +// TODO need to support all conn->updatePath(); conn = 0; return true; diff --git a/src/editor/MainWindow.cpp b/src/editor/MainWindow.cpp index 1f179fa..ae1a679 100644 --- a/src/editor/MainWindow.cpp +++ b/src/editor/MainWindow.cpp @@ -41,12 +41,18 @@ namespace nts { saveAct->setStatusTip(tr("Save a file")); connect(saveAct, SIGNAL(triggered()), this, SLOT(saveFile())); + QAction *simulateAct = new QAction(tr("&Simulate"), this); + saveAct->setStatusTip(tr("Simulate")); + connect(simulateAct, SIGNAL(triggered()), this, SLOT(simulate())); + fileMenu = menuBar()->addMenu(tr("&File")); fileMenu->addAction(loadAct); fileMenu->addAction(saveAct); fileMenu->addSeparator(); fileMenu->addAction(quitAct); + menuBar()->addAction(simulateAct); + setWindowTitle(tr("Nanotekspice")); view = new EditorView(); @@ -61,7 +67,7 @@ namespace nts { nodesEditor = new Editor(this); nodesEditor->install(scene); - blocks = new std::list(); + blocks = new std::vector(); this->setMinimumWidth(480); this->setMinimumHeight(640); @@ -108,7 +114,7 @@ namespace nts { Block *b = new Block(0); scene->addItem(b); - blocks->push_front(b); + blocks->push_back(b); b->setAComponent(component); b->addPort(QString::fromStdString(component->getName()), NULL, 0, Port::NamePort); @@ -116,30 +122,30 @@ namespace nts { b->setPos(pos.x(), pos.y() - 10); - for (const auto &pin : component->getPins()) { + for (auto &pin : component->getPins()) { switch (pin.getMode()) { case Pin::Mode::U: - b->addInputPort("UNDEFINED", &pin); + b->addInputPort("UNDEFINED", const_cast(&pin)); break; case Pin::Mode::I: - b->addInputPort("IN " + pin.getTargetPin(), &pin); + b->addInputPort("IN " + pin.getTargetPin(), const_cast(&pin)); break; case Pin::Mode::O: - b->addOutputPort("OUT " + pin.getTargetPin(), &pin); + b->addOutputPort("OUT " + pin.getTargetPin(), const_cast(&pin)); break; case Pin::Mode::IO: - b->addOutputPort("IN/OUT" + pin.getTargetPin(), &pin); + b->addOutputPort("IN/OUT" + pin.getTargetPin(), const_cast(&pin)); break; case Pin::Mode::VSS: - b->addOutputPort("VSS " + pin.getTargetPin(), &pin); + b->addOutputPort("VSS " + pin.getTargetPin(), const_cast(&pin)); break; case Pin::Mode::VDD: - b->addOutputPort("VDD " + pin.getTargetPin(), &pin); + b->addOutputPort("VDD " + pin.getTargetPin(), const_cast(&pin)); break; } } @@ -165,8 +171,16 @@ namespace nts { QDataStream ds(&f); } + void MainWindow::simulate() { + for (const auto &block : *blocks) { + if (const_cast(block->getAComponent())->getType() == nts::AComponent::Type::IC) { + const_cast(block->getAComponent())->Compute(1); + } + } + } + void MainWindow::setComponents(std::vector components) { - blocks = new std::list(); + blocks = new std::vector(); int index = 0; for (const auto &component : components) { @@ -174,7 +188,7 @@ namespace nts { Block *b = new Block(0); b->setPos(120 * index, index / 3 * 120); scene->addItem(b); - blocks->push_front(b); + blocks->push_back(b); b->setAComponent(component); b->addPort(QString::fromStdString(component->getName()), NULL, 0, Port::NamePort); @@ -183,27 +197,27 @@ namespace nts { for (const auto &pin : component->getPins()) { switch (pin.getMode()) { case Pin::Mode::U: - b->addInputPort("UNDEFINED", &pin); + b->addInputPort("UNDEFINED", const_cast(&pin)); break; case Pin::Mode::I: - b->addInputPort("IN " + pin.getTargetPin(), &pin); + b->addInputPort("IN " + pin.getTargetPin(), const_cast(&pin)); break; case Pin::Mode::O: - b->addOutputPort("OUT " + pin.getTargetPin(), &pin); + b->addOutputPort("OUT " + pin.getTargetPin(), const_cast(&pin)); break; case Pin::Mode::IO: - b->addOutputPort("IN/OUT" + pin.getTargetPin(), &pin); + b->addOutputPort("IN/OUT" + pin.getTargetPin(), const_cast(&pin)); break; case Pin::Mode::VSS: - b->addOutputPort("VSS " + pin.getTargetPin(), &pin); + b->addOutputPort("VSS " + pin.getTargetPin(), const_cast(&pin)); break; case Pin::Mode::VDD: - b->addOutputPort("VDD " + pin.getTargetPin(), &pin); + b->addOutputPort("VDD " + pin.getTargetPin(), const_cast(&pin)); break; } } diff --git a/src/editor/elements/Block.cpp b/src/editor/elements/Block.cpp index a18d414..1ff6741 100644 --- a/src/editor/elements/Block.cpp +++ b/src/editor/elements/Block.cpp @@ -25,7 +25,7 @@ namespace nts { height = vertMargin; } - Port *Block::addPort(const QString &name, const Pin* pin, bool isOutput, int flags, int ptr) { + Port *Block::addPort(const QString &name, Pin* pin, bool isOutput, int flags, int ptr) { Port *port = new Port(this); port->setName(name); port->setIsOutput(isOutput); @@ -62,11 +62,11 @@ namespace nts { return port; } - void Block::addInputPort(const QString &name, const Pin *pin) { + void Block::addInputPort(const QString &name, Pin *pin) { addPort(name, pin, false); } - void Block::addOutputPort(const QString &name, const Pin *pin) { + void Block::addOutputPort(const QString &name, Pin *pin) { addPort(name, pin, true); } @@ -84,6 +84,16 @@ namespace nts { return getPortFromPin(&(this->getAComponent()->getPins()[index - 1])); } + size_t Block::getPinIdFromPin(Pin *pin) { + size_t pin_id = 0; + for (const auto &port : ports()) { + if (port->getPin() == pin) + return pin_id; + pin_id++; + } + return -1; + } + void Block::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) { Q_UNUSED(option) Q_UNUSED(widget) @@ -128,11 +138,11 @@ namespace nts { return value; } - void Block::setAComponent(const AComponent *component) { + void Block::setAComponent(AComponent *component) { this->aComponent = component; } - const AComponent *Block::getAComponent() { + AComponent *Block::getAComponent() { return this->aComponent; } } \ No newline at end of file diff --git a/src/editor/elements/Connection.cpp b/src/editor/elements/Connection.cpp index 63009da..3fb8292 100644 --- a/src/editor/elements/Connection.cpp +++ b/src/editor/elements/Connection.cpp @@ -2,8 +2,6 @@ #include "Port.hpp" -#include -#include #include namespace nts { @@ -42,6 +40,14 @@ namespace nts { m_port2->connections().append(this); } + Port *Connection::getPort1() { + return this->m_port1; + } + + Port *Connection::getPort2() { + return this->m_port2; + } + void Connection::updatePosFromPorts() { pos1 = m_port1->scenePos(); pos2 = m_port2->scenePos(); @@ -70,4 +76,27 @@ namespace nts { Port *Connection::port2() const { return m_port2; } + + void Connection::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) { + Q_UNUSED(option) + Q_UNUSED(widget) + + if ((m_port2 == NULL)) { + painter->setPen(QPen(Qt::black, 2)); + painter->setBrush(Qt::NoBrush); + + painter->drawPath(path()); + return; + } + + if (m_port2->getPin()->getState() == TRUE) { + painter->setPen(QPen(Qt::red, 2)); + painter->setBrush(Qt::NoBrush); + } else { + painter->setPen(QPen(Qt::black, 2)); + painter->setBrush(Qt::NoBrush); + } + + painter->drawPath(path()); + } } \ No newline at end of file diff --git a/src/editor/elements/Port.cpp b/src/editor/elements/Port.cpp index c075e6d..dceb972 100644 --- a/src/editor/elements/Port.cpp +++ b/src/editor/elements/Port.cpp @@ -58,11 +58,11 @@ namespace nts { return isOutput_; } - void Port::setPin(const Pin* pin) { + void Port::setPin(Pin* pin) { this->pin = pin; } - const Pin *Port::getPin() { + Pin *Port::getPin() { return this->pin; } @@ -86,7 +86,7 @@ namespace nts { } } - Block *Port::block() const { + Block *Port::block() { return m_block; }