Skip to content

Commit

Permalink
clear etc
Browse files Browse the repository at this point in the history
  • Loading branch information
gmardon committed Mar 8, 2017
1 parent 1dadb59 commit 4074a38
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 15 deletions.
2 changes: 2 additions & 0 deletions include/Editor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ namespace nts {

bool eventFilter(QObject *, QEvent *);

void clear();

private:
QGraphicsItem *itemAt(const QPointF &);

Expand Down
2 changes: 2 additions & 0 deletions include/MainWindow.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ namespace nts {

void loadFile();

void clear();

void simulate();

void addComponent(std::string name);
Expand Down
11 changes: 5 additions & 6 deletions src/editor/Editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,6 @@ namespace nts {
}
break;
}
case Qt::RightButton: {
QGraphicsItem *item = itemAt(me->scenePos());
if (item && (item->type() == Connection::Type || item->type() == Block::Type))
delete item;
break;
}
}
}
case QEvent::GraphicsSceneMouseMove: {
Expand Down Expand Up @@ -108,4 +102,9 @@ namespace nts {
this->scene->update();
return QObject::eventFilter(o, e);
}

void Editor::clear() {
this->scene->clear();
this->conn = 0;
}
}
42 changes: 33 additions & 9 deletions src/editor/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
#include "Editor.hpp"

#include "Port.hpp"
#include <typeinfo>
#include <include/c4071.hpp>
#include <include/Parser.hpp>
#include <include/Commands.hpp>

namespace nts {

Expand All @@ -32,10 +32,16 @@ namespace nts {
saveAct->setStatusTip(tr("Simulate"));
connect(simulateAct, SIGNAL(triggered()), this, SLOT(simulate()));

QAction *clearAct = new QAction(tr("&Clear"), this);
clearAct->setStatusTip(tr("Clear editor"));
connect(clearAct, SIGNAL(triggered()), this, SLOT(clear()));

fileMenu = menuBar()->addMenu(tr("&File"));
fileMenu->addAction(loadAct);
fileMenu->addAction(saveAct);
fileMenu->addSeparator();
fileMenu->addAction(clearAct);
fileMenu->addSeparator();
fileMenu->addAction(quitAct);

menuBar()->addAction(simulateAct);
Expand Down Expand Up @@ -140,40 +146,54 @@ namespace nts {
}
}

void MainWindow::clear() {
blocks = new std::vector<Block *>();
scene->clear();
this->nodesEditor->clear();
}

void MainWindow::saveFile() {
QString fname = QFileDialog::getSaveFileName();
if (fname.isEmpty())
return;

QFile f(fname);
f.open(QFile::WriteOnly);
QDataStream ds(&f);
std::vector<IComponent *> *components = new std::vector<IComponent *>();
for (const auto &block : *blocks) {
components->push_back(block->getAComponent());
}
save(fname.toStdString(), *components);
}

void MainWindow::loadFile() {
QString fname = QFileDialog::getOpenFileName();
if (fname.isEmpty())
return;

std::map<std::string, IComponent *> from = parser(fname.toStdString().c_str()); // todo add try catchs
std::map<std::string, IComponent *> from;
try {
from = parser(fname.toStdString().c_str());
} catch (std::exception exception) {
QMessageBox::information(this, "Parse error", "Cannot parse given file !");
return;
}

std::vector<AComponent *> *components = new std::vector<AComponent *>();

std::map<std::string, nts::IComponent *>::iterator it;
for (std::map<std::string, nts::IComponent *>::iterator it = from.begin(); it != from.end(); ++it)
(*components).push_back(dynamic_cast<AComponent *>((it->second)));

this->setComponents(*components);
}

void MainWindow::simulate() {
for (const auto &block : *blocks) {
if (const_cast<nts::AComponent *>(block->getAComponent())->getType() == nts::AComponent::Type::IC) {
const_cast<nts::AComponent *>(block->getAComponent())->Compute(1);
}
const_cast<nts::AComponent *>(block->getAComponent())->Compute(0);
}
}

void MainWindow::setComponents(std::vector<AComponent *> components) {
blocks = new std::vector<Block *>();
clear();

int index = 0;
for (const auto &component : components) {
Expand Down Expand Up @@ -212,6 +232,10 @@ namespace nts {
case Pin::Mode::VDD:
b->addOutputPort("VDD " + pin.getTargetPin(), const_cast<Pin *>(&pin));
break;

default:
printf("UNDEFINED PIN");
break;
}
}
}
Expand Down

0 comments on commit 4074a38

Please sign in to comment.