Initial Commit

This commit is contained in:
xypwn
2020-03-24 20:18:39 +01:00
commit dfeb7de33c
70 changed files with 4920 additions and 0 deletions

44
Parts/AndGate.cpp Normal file
View File

@@ -0,0 +1,44 @@
#include "AndGate.h"
#include "../Connector.h"
AndGate::AndGate(Logic* logic)
:Part(logic)
{
addInputs(2);
addOutputs(1);
recalculateLayout();
}
QVector<bool> AndGate::compute(QVector<bool> inputs)
{
QVector<bool> ret(1);
ret[0] = inputs[0] && inputs[1];
return ret;
}
QPainterPath AndGate::symbolPainterPath(QRect limits)
{
Q_UNUSED(limits)
QPainterPath painterPath;
// Left vertical line
painterPath.moveTo(-8, 8);
painterPath.lineTo(-8, -8);
// Upper horizontal line
painterPath.lineTo(-2, -8);
// Right arc
painterPath.cubicTo(11, -8, 11, 8, -2, 8);
// Lower horizontal line
painterPath.lineTo(-8, 8);
// Upper input line
painterPath.moveTo(-14, -4);
painterPath.lineTo(-8, -4);
// Lower input line
painterPath.moveTo(-14, 4);
painterPath.lineTo(-8, 4);
// Output line
painterPath.moveTo(8, 0);
painterPath.lineTo(14, 0);
return painterPath;
}

15
Parts/AndGate.h Normal file
View File

@@ -0,0 +1,15 @@
#ifndef ANDGATE_H
#define ANDGATE_H
#include "../Part.h"
class AndGate : public Part
{
public:
AndGate(Logic* logic);
QVector<bool> compute(QVector<bool> inputs) override;
QPainterPath symbolPainterPath(QRect limits) override;
};
#endif // ANDGATE_H

38
Parts/BufferGate.cpp Normal file
View File

@@ -0,0 +1,38 @@
#include "BufferGate.h"
#include "../Connector.h"
BufferGate::BufferGate(Logic* logic)
:Part(logic)
{
addInputs(1);
addOutputs(1);
recalculateLayout();
}
QVector<bool> BufferGate::compute(QVector<bool> inputs)
{
QVector<bool> ret(1);
ret[0] = inputs[0];
return ret;
}
QPainterPath BufferGate::symbolPainterPath(QRect limits)
{
Q_UNUSED(limits)
QPainterPath painterPath;
QPolygon triangle;
triangle.append(QPoint(-8, 8));
triangle.append(QPoint(-8, -8));
triangle.append(QPoint(8, 0));
triangle.append(QPoint(-8, 8));
painterPath.addPolygon(triangle);
// Input line
painterPath.moveTo(-14, 0);
painterPath.lineTo(-8, 0);
// Output line
painterPath.moveTo(8, 0);
painterPath.lineTo(14, 0);
return painterPath;
}

15
Parts/BufferGate.h Normal file
View File

@@ -0,0 +1,15 @@
#ifndef BUFFERGATE_H
#define BUFFERGATE_H
#include "../Part.h"
class BufferGate : public Part
{
public:
BufferGate(Logic* logic);
QVector<bool> compute(QVector<bool> inputs) override;
QPainterPath symbolPainterPath(QRect limits) override;
};
#endif // BUFFERGATE_H

26
Parts/HighConstant.cpp Normal file
View File

@@ -0,0 +1,26 @@
#include "HighConstant.h"
#include "../Connector.h"
HighConstant::HighConstant(Logic* logic)
:Part(logic, Circle)
{
addInputs(0);
addOutputs(1);
recalculateLayout();
m_brushColorNormal = Qt::GlobalColor::green;
m_brushColorSelected = Qt::GlobalColor::green;
}
QVector<bool> HighConstant::compute(QVector<bool> inputs)
{
QVector<bool> ret(1);
ret[0] = true;
return ret;
}
QPainterPath HighConstant::symbolPainterPath(QRect limits)
{
Q_UNUSED(limits)
return QPainterPath();
}

15
Parts/HighConstant.h Normal file
View File

@@ -0,0 +1,15 @@
#ifndef HIGHCONSTANT_H
#define HIGHCONSTANT_H
#include "../Part.h"
class HighConstant : public Part
{
public:
HighConstant(Logic* logic);
QVector<bool> compute(QVector<bool> inputs) override;
QPainterPath symbolPainterPath(QRect limits) override;
};
#endif // HIGHCONSTANT_H

View File

@@ -0,0 +1,56 @@
#include "IntegratedCircuit.h"
#include "../Scene.h"
#include "../Logic.h"
#include "ToggleButton.h"
#include "LightBulb.h"
static bool compareToggleButtons(const ToggleButton* a, const ToggleButton* b) { return a->y() < b->y(); }
static bool compareLightBulbs(const LightBulb* a, const LightBulb* b) { return a->y() < b->y(); }
IntegratedCircuit::IntegratedCircuit(Logic* logic, QString filename)
:Part(logic), m_filename(filename), m_icLogic(new Logic)
{
setWidth(4);
m_icLogic->loadFromFile(filename);
for(auto part : m_icLogic->m_parts)
{
if(part->partType() == PartType::IOToggleButton)
m_icLogicInputs.append((ToggleButton*)part);
else if(part->partType() == PartType::IOLightBulb)
m_icLogicOutputs.append((LightBulb*)part);
}
addInputs(m_icLogicInputs.length());
addOutputs(m_icLogicOutputs.length());
// Sort according to y position
std::sort(m_icLogicInputs.begin(), m_icLogicInputs.end(), compareToggleButtons);
std::sort(m_icLogicOutputs.begin(), m_icLogicOutputs.end(), compareLightBulbs);
recalculateLayout();
}
QString IntegratedCircuit::filename()
{
return m_filename;
}
QVector<bool> IntegratedCircuit::compute(QVector<bool> inputs)
{
for(int i = 0; i < inputs.length(); i++)
m_icLogicInputs[i]->m_toggleState = inputs[i];
m_icLogic->doLogicStep();
QVector<bool> ret(m_icLogicOutputs.length());
for(int i = 0; i < m_icLogicOutputs.length(); i++)
ret[i] = m_icLogicOutputs[i]->m_state;
return ret;
}
QPainterPath IntegratedCircuit::symbolPainterPath(QRect limits)
{
Q_UNUSED(limits)
return QPainterPath();
}

31
Parts/IntegratedCircuit.h Normal file
View File

@@ -0,0 +1,31 @@
#ifndef INTEGRATEDCIRCUIT_H
#define INTEGRATEDCIRCUIT_H
#include <QString>
#include "../Part.h"
class Logic;
class ToggleButton;
class LightBulb;
class IntegratedCircuit : public Part
{
public:
IntegratedCircuit(Logic* logic, QString filename = "");
QString filename();
QVector<bool> compute(QVector<bool> inputs) override;
QPainterPath symbolPainterPath(QRect limits) override;
private:
QString m_filename;
Logic* m_icLogic;
QVector<ToggleButton*> m_icLogicInputs;
QVector<LightBulb*> m_icLogicOutputs;
};
#endif // INTEGRATEDCIRCUIT_H

38
Parts/LightBulb.cpp Normal file
View File

@@ -0,0 +1,38 @@
#include "LightBulb.h"
#include "../Connector.h"
LightBulb::LightBulb(Logic* logic)
:Part(logic, Circle)
{
addInputs(1);
addOutputs(0);
recalculateLayout();
m_brushColorNormal = Qt::GlobalColor::black;
m_brushColorSelected = Qt::GlobalColor::black;
}
QVector<bool> LightBulb::compute(QVector<bool> inputs)
{
m_state = inputs[0];
// Set color according to input state (on or off)
if(m_state)
{
m_brushColorNormal = Qt::GlobalColor::green;
m_brushColorSelected = Qt::GlobalColor::green;
}
else
{
m_brushColorNormal = Qt::GlobalColor::black;
m_brushColorSelected = Qt::GlobalColor::black;
}
return QVector<bool>();
}
QPainterPath LightBulb::symbolPainterPath(QRect limits)
{
Q_UNUSED(limits)
return QPainterPath();
}

21
Parts/LightBulb.h Normal file
View File

@@ -0,0 +1,21 @@
#ifndef LAMP_H
#define LAMP_H
#include "../Part.h"
class LightBulb : public Part
{
public:
friend class IntegratedCircuit;
LightBulb(Logic* logic);
QVector<bool> compute(QVector<bool> inputs) override;
QPainterPath symbolPainterPath(QRect limits) override;
private:
bool m_state = false;
};
#endif // LAMP_H

26
Parts/LowConstant.cpp Normal file
View File

@@ -0,0 +1,26 @@
#include "LowConstant.h"
#include "../Connector.h"
LowConstant::LowConstant(Logic* logic)
:Part(logic, Circle)
{
addInputs(0);
addOutputs(1);
recalculateLayout();
m_brushColorNormal = Qt::GlobalColor::black;
m_brushColorSelected = Qt::GlobalColor::black;
}
QVector<bool> LowConstant::compute(QVector<bool> inputs)
{
QVector<bool> ret(1);
ret[0] = false;
return ret;
}
QPainterPath LowConstant::symbolPainterPath(QRect limits)
{
Q_UNUSED(limits)
return QPainterPath();
}

15
Parts/LowConstant.h Normal file
View File

@@ -0,0 +1,15 @@
#ifndef LOWCONSTANT_H
#define LOWCONSTANT_H
#include "../Part.h"
class LowConstant : public Part
{
public:
LowConstant(Logic* logic);
QVector<bool> compute(QVector<bool> inputs) override;
QPainterPath symbolPainterPath(QRect limits) override;
};
#endif // LOWCONSTANT_H

46
Parts/NandGate.cpp Normal file
View File

@@ -0,0 +1,46 @@
#include "NandGate.h"
#include "../Connector.h"
NandGate::NandGate(Logic* logic)
:Part(logic)
{
addInputs(2);
addOutputs(1);
recalculateLayout();
}
QVector<bool> NandGate::compute(QVector<bool> inputs)
{
QVector<bool> ret(1);
ret[0] = !(inputs[0] && inputs[1]);
return ret;
}
QPainterPath NandGate::symbolPainterPath(QRect limits)
{
Q_UNUSED(limits)
QPainterPath painterPath;
// Left vertical line
painterPath.moveTo(-8, 8);
painterPath.lineTo(-8, -8);
// Upper horizontal line
painterPath.lineTo(-2, -8);
// Right arc
painterPath.cubicTo(11, -8, 11, 8, -2, 8);
// Lower horizontal line
painterPath.lineTo(-8, 8);
// Inverting dot
painterPath.addEllipse(8.f, -1.5f, 3.f, 3.f);
// Upper input line
painterPath.moveTo(-14, -4);
painterPath.lineTo(-8, -4);
// Lower input line
painterPath.moveTo(-14, 4);
painterPath.lineTo(-8, 4);
// Output line
painterPath.moveTo(11.5f, 0.f);
painterPath.lineTo(14, 0);
return painterPath;
}

15
Parts/NandGate.h Normal file
View File

@@ -0,0 +1,15 @@
#ifndef NANDGATE_H
#define NANDGATE_H
#include "../Part.h"
class NandGate : public Part
{
public:
NandGate(Logic* logic);
QVector<bool> compute(QVector<bool> inputs) override;
QPainterPath symbolPainterPath(QRect limits) override;
};
#endif // NANDGATE_H

49
Parts/NorGate.cpp Normal file
View File

@@ -0,0 +1,49 @@
#include "NorGate.h"
#include "../Connector.h"
NorGate::NorGate(Logic* logic)
:Part(logic)
{
addInputs(2);
addOutputs(1);
recalculateLayout();
}
QVector<bool> NorGate::compute(QVector<bool> inputs)
{
QVector<bool> ret(1);
ret[0] = !(inputs[0] || inputs[1]);
return ret;
}
QPainterPath NorGate::symbolPainterPath(QRect limits)
{
Q_UNUSED(limits)
QPainterPath painterPath;
// Left arc
painterPath.moveTo(-10, -8);
painterPath.quadTo(-2, 0, -10, 8);
// Lower line
painterPath.lineTo(-2, 8);
// Lower arc
painterPath.quadTo(5, 6, 8, 0);
//
painterPath.moveTo(-10, -8);
// Upper line
painterPath.lineTo(-2, -8);
// Upper arc
painterPath.quadTo(5, -6, 8, 0);
// Inverting dot
painterPath.addEllipse(8.f, -1.5f, 3.f, 3.f);
// Upper input line
painterPath.moveTo(-14, -4);
painterPath.lineTo(-7.5f, -4.f);
// Lower input line
painterPath.moveTo(-14, 4);
painterPath.lineTo(-7.5f, 4.f);
// Output line
painterPath.moveTo(11.5f, 0.f);
painterPath.lineTo(14, 0);
return painterPath;
}

15
Parts/NorGate.h Normal file
View File

@@ -0,0 +1,15 @@
#ifndef NORGATE_H
#define NORGATE_H
#include "../Part.h"
class NorGate : public Part
{
public:
NorGate(Logic* logic);
QVector<bool> compute(QVector<bool> inputs) override;
QPainterPath symbolPainterPath(QRect limits) override;
};
#endif // NORGATE_H

39
Parts/NotGate.cpp Normal file
View File

@@ -0,0 +1,39 @@
#include "NotGate.h"
#include "../Connector.h"
NotGate::NotGate(Logic* logic)
:Part(logic)
{
addInputs(1);
addOutputs(1);
recalculateLayout();
}
QVector<bool> NotGate::compute(QVector<bool> inputs)
{
QVector<bool> ret(1);
ret[0] = !inputs[0];
return ret;
}
QPainterPath NotGate::symbolPainterPath(QRect limits)
{
Q_UNUSED(limits)
QPainterPath painterPath;
QPolygon triangle;
triangle.append(QPoint(-8, 8));
triangle.append(QPoint(-8, -8));
triangle.append(QPoint(8, 0));
triangle.append(QPoint(-8, 8));
painterPath.addPolygon(triangle);
// Inverting dot
painterPath.addEllipse(8.f, -1.5f, 3.f, 3.f);
// Input line
painterPath.moveTo(-14, 0);
painterPath.lineTo(-8, 0);
// Output line
painterPath.moveTo(11.5f, 0.f);
painterPath.lineTo(14, 0);
return painterPath;
}

15
Parts/NotGate.h Normal file
View File

@@ -0,0 +1,15 @@
#ifndef NOTGATE_H
#define NOTGATE_H
#include "../Part.h"
class NotGate : public Part
{
public:
NotGate(Logic* logic);
QVector<bool> compute(QVector<bool> inputs) override;
QPainterPath symbolPainterPath(QRect limits) override;
};
#endif // NOTGATE_H

47
Parts/OrGate.cpp Normal file
View File

@@ -0,0 +1,47 @@
#include "OrGate.h"
#include "../Connector.h"
OrGate::OrGate(Logic* logic)
:Part(logic)
{
addInputs(2);
addOutputs(1);
recalculateLayout();
}
QVector<bool> OrGate::compute(QVector<bool> inputs)
{
QVector<bool> ret(1);
ret[0] = inputs[0] || inputs[1];
return ret;
}
QPainterPath OrGate::symbolPainterPath(QRect limits)
{
Q_UNUSED(limits)
QPainterPath painterPath;
// Left arc
painterPath.moveTo(-10, -8);
painterPath.quadTo(-2, 0, -10, 8);
// Lower line
painterPath.lineTo(-2, 8);
// Lower arc
painterPath.quadTo(5, 6, 8, 0);
//
painterPath.moveTo(-10, -8);
// Upper line
painterPath.lineTo(-2, -8);
// Upper arc
painterPath.quadTo(5, -6, 8, 0);
// Upper input line
painterPath.moveTo(-14, -4);
painterPath.lineTo(-7.5f, -4.f);
// Lower input line
painterPath.moveTo(-14, 4);
painterPath.lineTo(-7.5f, 4.f);
// Output line
painterPath.moveTo(8, 0);
painterPath.lineTo(14, 0);
return painterPath;
}

15
Parts/OrGate.h Normal file
View File

@@ -0,0 +1,15 @@
#ifndef ORGATE_H
#define ORGATE_H
#include "../Part.h"
class OrGate : public Part
{
public:
OrGate(Logic* logic);
QVector<bool> compute(QVector<bool> inputs) override;
QPainterPath symbolPainterPath(QRect limits) override;
};
#endif // ORGATE_H

43
Parts/ToggleButton.cpp Normal file
View File

@@ -0,0 +1,43 @@
#include "ToggleButton.h"
#include <QGraphicsSceneMouseEvent>
ToggleButton::ToggleButton(Logic* logic)
:Part(logic, Circle)
{
addInputs(0);
addOutputs(1);
recalculateLayout();
m_brushColorNormal = Qt::GlobalColor::red;
m_brushColorSelected = Qt::GlobalColor::red;
}
QVector<bool> ToggleButton::compute(QVector<bool> inputs)
{
QVector<bool> ret(1);
ret[0] = m_toggleState;
return ret;
}
QPainterPath ToggleButton::symbolPainterPath(QRect limits)
{
Q_UNUSED(limits)
return QPainterPath();
}
void ToggleButton::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
{
m_toggleState = !m_toggleState;
if(m_toggleState)
{
m_brushColorNormal = Qt::GlobalColor::darkRed;
m_brushColorSelected = Qt::GlobalColor::darkRed;
}
else
{
m_brushColorNormal = Qt::GlobalColor::red;
m_brushColorSelected = Qt::GlobalColor::red;
}
update();
Part::mouseDoubleClickEvent(event);
}

24
Parts/ToggleButton.h Normal file
View File

@@ -0,0 +1,24 @@
#ifndef TOGGLEBUTTON_H
#define TOGGLEBUTTON_H
#include "../Part.h"
class ToggleButton : public Part
{
public:
friend class IntegratedCircuit;
ToggleButton(Logic* logic);
QVector<bool> compute(QVector<bool> inputs) override;
QPainterPath symbolPainterPath(QRect limits) override;
private:
void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) override;
QPointF m_dragBeginPos;
bool m_toggleState = false;
};
#endif // TOGGLEBUTTON_H

52
Parts/XnorGate.cpp Normal file
View File

@@ -0,0 +1,52 @@
#include "XnorGate.h"
#include "../Connector.h"
XnorGate::XnorGate(Logic* logic)
:Part(logic)
{
addInputs(2);
addOutputs(1);
recalculateLayout();
}
QVector<bool> XnorGate::compute(QVector<bool> inputs)
{
QVector<bool> ret(1);
ret[0] = !((inputs[0] || inputs[1]) && (!(inputs[0] && inputs[1])));
return ret;
}
QPainterPath XnorGate::symbolPainterPath(QRect limits)
{
Q_UNUSED(limits)
QPainterPath painterPath;
// 0th Left arc
painterPath.moveTo(-12, -8);
painterPath.quadTo(-4, 0, -12, 8);
// 1st Left arc
painterPath.moveTo(-10, -8);
painterPath.quadTo(-2, 0, -10, 8);
// Lower line
painterPath.lineTo(-2, 8);
// Lower arc
painterPath.quadTo(5, 6, 8, 0);
//
painterPath.moveTo(-10, -8);
// Upper line
painterPath.lineTo(-2, -8);
// Upper arc
painterPath.quadTo(5, -6, 8, 0);
// Inverting dot
painterPath.addEllipse(8.f, -1.5f, 3.f, 3.f);
// Upper input line
painterPath.moveTo(-14, -4);
painterPath.lineTo(-7.5f, -4.f);
// Lower input line
painterPath.moveTo(-14, 4);
painterPath.lineTo(-7.5f, 4.f);
// Output line
painterPath.moveTo(11.5f, 0.f);
painterPath.lineTo(14, 0);
return painterPath;
}

15
Parts/XnorGate.h Normal file
View File

@@ -0,0 +1,15 @@
#ifndef XNORGATE_H
#define XNORGATE_H
#include "../Part.h"
class XnorGate : public Part
{
public:
XnorGate(Logic* logic);
QVector<bool> compute(QVector<bool> inputs) override;
QPainterPath symbolPainterPath(QRect limits) override;
};
#endif // XNORGATE_H

50
Parts/XorGate.cpp Normal file
View File

@@ -0,0 +1,50 @@
#include "XorGate.h"
#include "../Connector.h"
XorGate::XorGate(Logic* logic)
:Part(logic)
{
addInputs(2);
addOutputs(1);
recalculateLayout();
}
QVector<bool> XorGate::compute(QVector<bool> inputs)
{
QVector<bool> ret(1);
ret[0] = (inputs[0] || inputs[1]) && (!(inputs[0] && inputs[1]));
return ret;
}
QPainterPath XorGate::symbolPainterPath(QRect limits)
{
Q_UNUSED(limits)
QPainterPath painterPath;
// 0th Left arc
painterPath.moveTo(-12, -8);
painterPath.quadTo(-4, 0, -12, 8);
// 1st Left arc
painterPath.moveTo(-10, -8);
painterPath.quadTo(-2, 0, -10, 8);
// Lower line
painterPath.lineTo(-2, 8);
// Lower arc
painterPath.quadTo(5, 6, 8, 0);
//
painterPath.moveTo(-10, -8);
// Upper line
painterPath.lineTo(-2, -8);
// Upper arc
painterPath.quadTo(5, -6, 8, 0);
// Upper input line
painterPath.moveTo(-14, -4);
painterPath.lineTo(-7.5f, -4.f);
// Lower input line
painterPath.moveTo(-14, 4);
painterPath.lineTo(-7.5f, 4.f);
// Output line
painterPath.moveTo(8, 0);
painterPath.lineTo(14, 0);
return painterPath;
}

15
Parts/XorGate.h Normal file
View File

@@ -0,0 +1,15 @@
#ifndef XORGATE_H
#define XORGATE_H
#include "../Part.h"
class XorGate : public Part
{
public:
XorGate(Logic* logic);
QVector<bool> compute(QVector<bool> inputs) override;
QPainterPath symbolPainterPath(QRect limits) override;
};
#endif // XORGATE_H