Added simulation functionality
This commit is contained in:
parent
79c5d147a3
commit
78da43f51d
23 changed files with 385 additions and 21 deletions
|
@ -12,8 +12,18 @@ public:
|
|||
explicit Component(QWidget* parent);
|
||||
explicit Component(QWidget* parent, const QString& resource);
|
||||
|
||||
void mouseMoveEvent(QMouseEvent* event) override;
|
||||
virtual void mouseMoveEvent(QMouseEvent* event) override;
|
||||
|
||||
private:
|
||||
QPoint CenterPos();
|
||||
void Connect(Component* component);
|
||||
const QString& Type();
|
||||
|
||||
virtual bool Action() = 0;
|
||||
|
||||
|
||||
protected:
|
||||
Ui::Component* ui;
|
||||
|
||||
QString type;
|
||||
std::vector<Component*> connections;
|
||||
};
|
|
@ -1,6 +1,7 @@
|
|||
#pragma once
|
||||
|
||||
#include <qmainwindow.h>
|
||||
#include <qpen.h>
|
||||
|
||||
namespace Ui {
|
||||
class Window;
|
||||
|
@ -20,12 +21,14 @@ protected:
|
|||
void mousePressEvent(QMouseEvent* event) override;
|
||||
void mouseMoveEvent(QMouseEvent* event) override;
|
||||
void mouseReleaseEvent(QMouseEvent* event) override;
|
||||
void paintEvent(QPaintEvent* event) override;
|
||||
|
||||
private:
|
||||
void ToggleSimulation();
|
||||
void ToggleComponentPlacer();
|
||||
void LoadGhostLabel(const QString& resource);
|
||||
Component* componentAt(const QPoint& point);
|
||||
Component* createComponent(const QPoint& position);
|
||||
|
||||
Ui::Window* ui;
|
||||
bool simulating;
|
||||
|
@ -34,10 +37,18 @@ private:
|
|||
Component* component;
|
||||
QPoint relativePos;
|
||||
} dragInfo;
|
||||
bool mouseDown;
|
||||
|
||||
GhostLabel* ghostImage;
|
||||
QString resourcePath;
|
||||
QPen pen;
|
||||
|
||||
QActionGroup* componentGroup;
|
||||
|
||||
enum class eComponent {
|
||||
NOT, AND, NAND, OR, NOR, XOR, XNOR, SWITCH, LAMP
|
||||
} componentType;
|
||||
|
||||
std::vector<Component*> components;
|
||||
std::vector<QLine> wires;
|
||||
};
|
14
include/components/ANDgate.hpp
Normal file
14
include/components/ANDgate.hpp
Normal file
|
@ -0,0 +1,14 @@
|
|||
#pragma once
|
||||
|
||||
#include <QWidget.h>
|
||||
#include "../Component.hpp"
|
||||
|
||||
class ANDGate : public Component
|
||||
{
|
||||
public:
|
||||
ANDGate(QWidget* parent) : Component(parent, ":/components/and.png") { type = "and"; }
|
||||
|
||||
bool Action() override {
|
||||
return (connections[0]->Action() && connections[1]->Action());
|
||||
}
|
||||
};
|
24
include/components/Lamp.hpp
Normal file
24
include/components/Lamp.hpp
Normal file
|
@ -0,0 +1,24 @@
|
|||
#pragma once
|
||||
|
||||
#include <QWidget.h>
|
||||
#include "../Component.hpp"
|
||||
|
||||
class Lamp : public Component
|
||||
{
|
||||
public:
|
||||
Lamp(QWidget* parent) : Component(parent, ":/components/lamp_off.png") { type = "lamp"; }
|
||||
|
||||
bool Action() override {
|
||||
return connections[0]->Action();
|
||||
}
|
||||
|
||||
void Turn(bool state) {
|
||||
if (state)
|
||||
ui->label->setPixmap(QPixmap(":/components/lamp_on.png"));
|
||||
else
|
||||
ui->label->setPixmap(QPixmap(":/components/lamp_off.png"));
|
||||
}
|
||||
|
||||
private:
|
||||
bool state = false;
|
||||
};
|
14
include/components/NANDgate.hpp
Normal file
14
include/components/NANDgate.hpp
Normal file
|
@ -0,0 +1,14 @@
|
|||
#pragma once
|
||||
|
||||
#include <QWidget.h>
|
||||
#include "../Component.hpp"
|
||||
|
||||
class NANDGate : public Component
|
||||
{
|
||||
public:
|
||||
NANDGate(QWidget* parent) : Component(parent, ":/components/nand.png") { type = "nand"; }
|
||||
|
||||
bool Action() override {
|
||||
return !(connections[0]->Action() && connections[1]->Action());
|
||||
}
|
||||
};
|
14
include/components/NORgate.hpp
Normal file
14
include/components/NORgate.hpp
Normal file
|
@ -0,0 +1,14 @@
|
|||
#pragma once
|
||||
|
||||
#include <QWidget.h>
|
||||
#include "../Component.hpp"
|
||||
|
||||
class NORGate : public Component
|
||||
{
|
||||
public:
|
||||
NORGate(QWidget* parent) : Component(parent, ":/components/nor.png") { type = "nor"; }
|
||||
|
||||
bool Action() override {
|
||||
return !(connections[0]->Action() || connections[1]->Action());
|
||||
}
|
||||
};
|
14
include/components/NOTgate.hpp
Normal file
14
include/components/NOTgate.hpp
Normal file
|
@ -0,0 +1,14 @@
|
|||
#pragma once
|
||||
|
||||
#include <QWidget.h>
|
||||
#include "../Component.hpp"
|
||||
|
||||
class NOTGate : public Component
|
||||
{
|
||||
public:
|
||||
NOTGate(QWidget* parent) : Component(parent, ":/components/not.png") { type = "not"; }
|
||||
|
||||
bool Action() override {
|
||||
return !connections[0]->Action();
|
||||
}
|
||||
};
|
14
include/components/ORgate.hpp
Normal file
14
include/components/ORgate.hpp
Normal file
|
@ -0,0 +1,14 @@
|
|||
#pragma once
|
||||
|
||||
#include <QWidget.h>
|
||||
#include "../Component.hpp"
|
||||
|
||||
class ORGate : public Component
|
||||
{
|
||||
public:
|
||||
ORGate(QWidget* parent) : Component(parent, ":/components/or.png") { type = "or"; }
|
||||
|
||||
bool Action() override {
|
||||
return (connections[0]->Action() || connections[1]->Action());
|
||||
}
|
||||
};
|
26
include/components/Switch.hpp
Normal file
26
include/components/Switch.hpp
Normal file
|
@ -0,0 +1,26 @@
|
|||
#pragma once
|
||||
|
||||
#include <QWidget.h>
|
||||
#include "ui_Component.h"
|
||||
#include "../Component.hpp"
|
||||
|
||||
class Switch : public Component
|
||||
{
|
||||
public:
|
||||
Switch(QWidget* parent) : Component(parent, ":/components/switch_off.png") { type = "switch"; }
|
||||
|
||||
bool Action() override {
|
||||
return state;
|
||||
}
|
||||
|
||||
void Toggle() {
|
||||
state = !state;
|
||||
if (state)
|
||||
ui->label->setPixmap(QPixmap(":/components/switch_on.png"));
|
||||
else
|
||||
ui->label->setPixmap(QPixmap(":/components/switch_off.png"));
|
||||
}
|
||||
|
||||
private:
|
||||
bool state = false;
|
||||
};
|
14
include/components/XNORgate.hpp
Normal file
14
include/components/XNORgate.hpp
Normal file
|
@ -0,0 +1,14 @@
|
|||
#pragma once
|
||||
|
||||
#include <QWidget.h>
|
||||
#include "../Component.hpp"
|
||||
|
||||
class XNORGate : public Component
|
||||
{
|
||||
public:
|
||||
XNORGate(QWidget* parent) : Component(parent, ":/components/xnor.png") { type = "xnor"; }
|
||||
|
||||
bool Action() override {
|
||||
return !((connections[0]->Action() || connections[1]->Action()) && !(connections[0]->Action() && connections[1]->Action()));
|
||||
}
|
||||
};
|
14
include/components/XORgate.hpp
Normal file
14
include/components/XORgate.hpp
Normal file
|
@ -0,0 +1,14 @@
|
|||
#pragma once
|
||||
|
||||
#include <QWidget.h>
|
||||
#include "../Component.hpp"
|
||||
|
||||
class XORGate : public Component
|
||||
{
|
||||
public:
|
||||
XORGate(QWidget* parent) : Component(parent, ":/components/xor.png") { type = "xor"; }
|
||||
|
||||
bool Action() override {
|
||||
return ((connections[0]->Action() || connections[1]->Action()) && !(connections[0]->Action() && connections[1]->Action()));
|
||||
}
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue