Skip to content

Commit

Permalink
#32 - adding undefined value for open pins
Browse files Browse the repository at this point in the history
  • Loading branch information
Eslam-Samir committed Feb 18, 2016
1 parent 8c0b5ef commit e42435a
Show file tree
Hide file tree
Showing 10 changed files with 229 additions and 57 deletions.
9 changes: 9 additions & 0 deletions src/canvasmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,14 @@ int CanvasManager::selectedComponentSquare(int index) const
return calculateSquareNumber(c);
}

void CanvasManager::updateComponents()
{
foreach(Component* c, d->mComponents)
{
c->update();
}
}

void CanvasManager::selectedFromWorkspace(int index)
{
unSelectComponent();
Expand Down Expand Up @@ -360,6 +368,7 @@ void CanvasManager::deleteLine(int index)
d->canvas->removeItem(l);
d->connectionLines.removeAt(index);
delete l;
updateComponents();
}

bool CanvasManager::isDropable(QPointF position)
Expand Down
1 change: 1 addition & 0 deletions src/canvasmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ class CanvasManager : public QObject
QList<Cell> alternativePlaces(Cell c) const;
int calculateSquareNumber(Cell c) const;
int selectedComponentSquare(int index) const;
void updateComponents();

public Q_SLOTS:
void selectedFromWorkspace(int index);
Expand Down
4 changes: 2 additions & 2 deletions src/connectionline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ ConnectionLine::ConnectionLine(Pin *in, Pin *out, QGraphicsItem* parent)

if(m_out && m_in)
{
connect(m_out, SIGNAL(changed(bool)),
m_in, SLOT(updatePinValue(bool)));
connect(m_out, SIGNAL(changed(Pin::Value)),
m_in, SLOT(updatePinValue(Pin::Value)));
m_in->updatePinValue(m_out->value());
}
}
Expand Down
60 changes: 38 additions & 22 deletions src/gate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,37 +33,54 @@ class Gate::Private
Gate::Gate(Type t)
: Component(t), d(new Private)
{
d->maxInput = 2;

if(t == Gate::NotGate)
{
d->maxInput = 1;
}
else
{
d->maxInput = 2;
}
setFlag(QGraphicsItem::ItemIsMovable);
setFlag(QGraphicsItem::ItemSendsGeometryChanges);

d->in1 = new Pin(Pin::Input, this);
d->in2 = new Pin(Pin::Input, this);
d->out = new Pin(Pin::Output, this);

connect(d->in1, SIGNAL(changed(bool)),
this, SLOT(calcOutput()));

connect(d->in2, SIGNAL(changed(bool)),
connect(d->in1, SIGNAL(changed(Pin::Value)),
this, SLOT(calcOutput()));

connect(this, SIGNAL(outputChanged(bool)),
d->out, SLOT(updatePinValue(bool)));

addPins(QList<Pin*>() << d->in1 << d->in2 << d->out);
connect(this, SIGNAL(outputChanged(Pin::Value)),
d->out, SLOT(updatePinValue(Pin::Value)));

QLineF line(0,0,10,0);
QGraphicsLineItem *Li1 = new QGraphicsLineItem(line,d->in1);
QGraphicsLineItem *Li2 = new QGraphicsLineItem(line,d->in2);
QGraphicsLineItem *Lo1 = new QGraphicsLineItem(line,d->out);

d->in1->setPos(-20,5);
d->in2->setPos(-20,35);
d->out->setPos(50,22.5);
Li1->setPos(10,5);
Li2->setPos(10,5);
Lo1->setPos(-10,5);
if(d->maxInput == 1)
{
addPins(QList<Pin*>() << d->in1 << d->out);
d->in1->setPos(-20,22.5);
d->out->setPos(50,22.5);
Li1->setPos(10,5);
Lo1->setPos(-10,5);
}
else
{
d->in2 = new Pin(Pin::Input, this);
addPins(QList<Pin*>() << d->in1 << d->in2 << d->out);

connect(d->in2, SIGNAL(changed(Pin::Value)),
this, SLOT(calcOutput()));

QGraphicsLineItem *Li2 = new QGraphicsLineItem(line,d->in2);
d->in1->setPos(-20,5);
d->in2->setPos(-20,35);
d->out->setPos(50,22.5);
Li1->setPos(10,5);
Li2->setPos(10,5);
Lo1->setPos(-10,5);
}
}

Pin * Gate::in1()
Expand Down Expand Up @@ -96,9 +113,7 @@ qint16 Gate::maxInput()
void Gate::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
QGraphicsObject::mouseMoveEvent(event);
d->in1->updateConnectedLine();
d->in2->updateConnectedLine();
d->out->updateConnectedLine();
updateConnection();
// if(x() - GATE_X_MARGIN < 0)
// {
// setPos(GATE_X_MARGIN, y());
Expand Down Expand Up @@ -131,8 +146,9 @@ QRectF Gate::boundingRect() const
void Gate::updateConnection()
{
d->in1->updateConnectedLine();
d->in2->updateConnectedLine();
d->out->updateConnectedLine();
if(maxInput() > 1)
d->in2->updateConnectedLine();
}

} // namespace Logicsim
2 changes: 1 addition & 1 deletion src/gate.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class Gate : public Component
public Q_SLOTS:
virtual void calcOutput()=0;
Q_SIGNALS:
void outputChanged(bool value);
void outputChanged(Pin::Value value);

private:
class Private;
Expand Down
166 changes: 146 additions & 20 deletions src/gates.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ void AndGate::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, Q

void AndGate::calcOutput()
{
bool out_logic = true;
Pin::Value out_logic;
/*
foreach (Node * n, inputList())
{
Expand All @@ -50,10 +50,21 @@ void AndGate::calcOutput()

//out_logic = d->
//outputNode()->setValue(out);
out_logic = in1()->value() && in2()->value();
qDebug() << "calculate AND called";
qDebug() << out_logic;
emit outputChanged(out_logic);
if(in1()->value() == Pin::True && in2()->value() == Pin::True)
{
out_logic = Pin::True;
}
else if(in1()->value() == Pin::Undefined || in2()->value() == Pin::Undefined)
{
out_logic = Pin::Undefined;
}
else
{
out_logic = Pin::False;
}
qDebug() << "calculate AND called";
qDebug() << out_logic;
emit outputChanged(out_logic);
}

QString AndGate::imageUrl() const
Expand All @@ -78,12 +89,23 @@ OrGate::OrGate(const OrGate &g)

void OrGate::calcOutput()
{
bool out_logic = false;
Pin::Value out_logic;
/*foreach (Node * n, inputList())
{
out_logic |= n->value();
}*/
out_logic = in1()->value() || in2()->value();
if(in1()->value() == Pin::False && in2()->value() == Pin::False)
{
out_logic = Pin::False;
}
else if(in1()->value() == Pin::True || in2()->value() == Pin::True)
{
out_logic = Pin::True;
}
else
{
out_logic = Pin::Undefined;
}
qDebug() << "calculate OR called";
qDebug() << out_logic;

Expand Down Expand Up @@ -131,7 +153,19 @@ NotGate::NotGate(const NotGate &g)

void NotGate::calcOutput()
{
bool out_logic;
Pin::Value out_logic;
if(in1()->value() == Pin::False)
{
out_logic = Pin::True;
}
else if(in1()->value() == Pin::True)
{
out_logic = Pin::False;
}
else
{
out_logic = Pin::Undefined;
}
//bool out_logic = !(inputList()[0]->value());
//outputNode()->setValue(!out);
emit Gate::outputChanged(out_logic);
Expand Down Expand Up @@ -174,17 +208,38 @@ NandGate::NandGate(const NandGate &g)

void NandGate::calcOutput()
{
bool out_logic = true;
Pin::Value out_logic;
/*foreach (Node * n, inputList())
{
out_logic &= n->value();
}
*/
out_logic = in1()->value() && in2()->value();
if(in1()->value() == Pin::True && in2()->value() == Pin::True)
{
out_logic = Pin::True;
}
else if(in1()->value() == Pin::Undefined || in2()->value() == Pin::Undefined)
{
out_logic = Pin::Undefined;
}
else
{
out_logic = Pin::False;
}
qDebug() << "calculate NAND called";

out_logic = !out_logic;
if(out_logic == Pin::False)
{
out_logic = Pin::True;
}
else if(out_logic == Pin::True)
{
out_logic = Pin::False;
}
else
{
out_logic = Pin::Undefined;
}
qDebug() << out_logic;
//outputNode()->setValue(!out);
emit outputChanged(out_logic);
Expand Down Expand Up @@ -225,15 +280,37 @@ NorGate::NorGate(const NorGate &g)

void NorGate::calcOutput()
{
bool out_logic = false;
Pin::Value out_logic;
/*foreach (Node * n, inputList())
{
out_logic |= n->value();
}
*/
out_logic = in1()->value() || in2()->value();
out_logic = !out_logic;
if(in1()->value() == Pin::False && in2()->value() == Pin::False)
{
out_logic = Pin::False;
}
else if(in1()->value() == Pin::True || in2()->value() == Pin::True)
{
out_logic = Pin::True;
}
else
{
out_logic = Pin::Undefined;
}
if(out_logic == Pin::False)
{
out_logic = Pin::True;
}
else if(out_logic == Pin::True)
{
out_logic = Pin::False;
}
else
{
out_logic = Pin::Undefined;
}
qDebug() << "calculate NOR called";
qDebug() << out_logic;

Expand Down Expand Up @@ -275,7 +352,7 @@ XorGate::XorGate(const XorGate &g)

void XorGate::calcOutput()
{
bool out_logic;// = inputList()[0]->value();
Pin::Value out_logic;// = inputList()[0]->value();
/*outputNode()->setValue(out);
int i ;
for ( i = 1 ; i< inputList().size() ; i++);
Expand All @@ -286,7 +363,26 @@ void XorGate::calcOutput()
out = 1 ;
}
outputNode()->setValue(out);*/
out_logic = in1()->value() ^ in2()->value();
if(in1()->value() == Pin::False && in2()->value() == Pin::False)
{
out_logic = Pin::False;
}
else if(in1()->value() == Pin::True && in2()->value() == Pin::False)
{
out_logic = Pin::True;
}
else if(in1()->value() == Pin::False && in2()->value() == Pin::True)
{
out_logic = Pin::True;
}
else if(in1()->value() == Pin::True && in2()->value() == Pin::True)
{
out_logic = Pin::False;
}
else
{
out_logic = Pin::Undefined;
}
qDebug() << "calculate XOR called";
qDebug() << out_logic;
emit outputChanged(out_logic);
Expand Down Expand Up @@ -326,7 +422,7 @@ XnorGate::XnorGate(const XnorGate &g)

void XnorGate::calcOutput()
{
bool out_logic;
Pin::Value out_logic;
/*outputNode()->setValue(out);
int i ;
for( i = 1 ; i< inputList().size() ; i++)
Expand All @@ -337,8 +433,38 @@ void XnorGate::calcOutput()
out = 1 ;
}
outputNode()->setValue(!out);*/
out_logic = in1()->value() || in2()->value();
out_logic = !out_logic;
if(in1()->value() == Pin::False && in2()->value() == Pin::False)
{
out_logic = Pin::False;
}
else if(in1()->value() == Pin::True && in2()->value() == Pin::False)
{
out_logic = Pin::True;
}
else if(in1()->value() == Pin::False && in2()->value() == Pin::True)
{
out_logic = Pin::True;
}
else if(in1()->value() == Pin::True && in2()->value() == Pin::True)
{
out_logic = Pin::False;
}
else
{
out_logic = Pin::Undefined;
}
if(out_logic == Pin::False)
{
out_logic = Pin::True;
}
else if(out_logic == Pin::True)
{
out_logic = Pin::False;
}
else
{
out_logic = Pin::Undefined;
}
qDebug() << "calculate XNOR called";
qDebug() << out_logic;
emit outputChanged(out_logic);
Expand Down
Loading

0 comments on commit e42435a

Please sign in to comment.