Initial Commit
This commit is contained in:
		
							
								
								
									
										42
									
								
								UndoCommands/AddPart.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										42
									
								
								UndoCommands/AddPart.cpp
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,42 @@
 | 
			
		||||
#include "AddPart.h"
 | 
			
		||||
 | 
			
		||||
#include "RemoveWire.h"
 | 
			
		||||
 | 
			
		||||
#include "../Part.h"
 | 
			
		||||
#include "../Connector.h"
 | 
			
		||||
#include "../Scene.h"
 | 
			
		||||
#include "../Logic.h"
 | 
			
		||||
#include "../Parts/IntegratedCircuit.h"
 | 
			
		||||
 | 
			
		||||
#include <QDebug>
 | 
			
		||||
 | 
			
		||||
AddPart::AddPart(Scene* scene, PartType::PartType partType, QPointF pos, QString icFilename)
 | 
			
		||||
    :m_scene(scene), m_partType(partType), m_pos(pos), m_icFilename(icFilename)
 | 
			
		||||
{
 | 
			
		||||
    setText("Add Part");
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
AddPart::~AddPart()
 | 
			
		||||
{
 | 
			
		||||
    m_scene->m_logic->deletePart(m_part);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void AddPart::redo()
 | 
			
		||||
{
 | 
			
		||||
    if(!m_part)
 | 
			
		||||
    {
 | 
			
		||||
        if(m_partType == PartType::IntegratedCircuit)
 | 
			
		||||
            m_part = m_scene->m_logic->createIC(m_icFilename, m_pos);
 | 
			
		||||
        else
 | 
			
		||||
            m_part = m_scene->m_logic->createPart(m_partType, m_pos);
 | 
			
		||||
    }
 | 
			
		||||
    else
 | 
			
		||||
    {
 | 
			
		||||
        m_scene->startTrackingPart(m_part);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void AddPart::undo()
 | 
			
		||||
{
 | 
			
		||||
    m_scene->stopTrackingPart(m_part);
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										30
									
								
								UndoCommands/AddPart.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										30
									
								
								UndoCommands/AddPart.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,30 @@
 | 
			
		||||
#ifndef ADDPART_H
 | 
			
		||||
#define ADDPART_H
 | 
			
		||||
 | 
			
		||||
#include <QUndoCommand>
 | 
			
		||||
#include <QPointF>
 | 
			
		||||
 | 
			
		||||
#include "../ePartType.h"
 | 
			
		||||
 | 
			
		||||
class Scene;
 | 
			
		||||
class Part;
 | 
			
		||||
class RemovePart;
 | 
			
		||||
 | 
			
		||||
class AddPart : public QUndoCommand
 | 
			
		||||
{
 | 
			
		||||
public:
 | 
			
		||||
    AddPart(Scene* scene, PartType::PartType partType, QPointF pos, QString icFilename = "");
 | 
			
		||||
    ~AddPart();
 | 
			
		||||
 | 
			
		||||
    void redo() override;
 | 
			
		||||
    void undo() override;
 | 
			
		||||
 | 
			
		||||
private:
 | 
			
		||||
    Scene* m_scene;
 | 
			
		||||
    Part* m_part = nullptr;
 | 
			
		||||
    PartType::PartType m_partType;
 | 
			
		||||
    QPointF m_pos;
 | 
			
		||||
    QString m_icFilename;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
#endif // ADDPART_H
 | 
			
		||||
							
								
								
									
										26
									
								
								UndoCommands/AddWire.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										26
									
								
								UndoCommands/AddWire.cpp
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,26 @@
 | 
			
		||||
#include "AddWire.h"
 | 
			
		||||
 | 
			
		||||
#include "../Scene.h"
 | 
			
		||||
#include "../Logic.h"
 | 
			
		||||
#include "../Wire.h"
 | 
			
		||||
 | 
			
		||||
AddWire::AddWire(Scene* scene, Connector* connectorInput, Connector* connectorOutput)
 | 
			
		||||
    :m_scene(scene), m_connectorInput(connectorInput), m_connectorOutput(connectorOutput)
 | 
			
		||||
{
 | 
			
		||||
    setText("Add Wire");
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
AddWire::~AddWire()
 | 
			
		||||
{
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
void AddWire::redo()
 | 
			
		||||
{
 | 
			
		||||
    m_wire = m_scene->m_logic->createWire(m_connectorInput, m_connectorOutput);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void AddWire::undo()
 | 
			
		||||
{
 | 
			
		||||
    m_scene->m_logic->deleteWire(m_wire);
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										26
									
								
								UndoCommands/AddWire.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										26
									
								
								UndoCommands/AddWire.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,26 @@
 | 
			
		||||
#ifndef ADDWIRE_H
 | 
			
		||||
#define ADDWIRE_H
 | 
			
		||||
 | 
			
		||||
#include <QUndoCommand>
 | 
			
		||||
 | 
			
		||||
class Scene;
 | 
			
		||||
class Wire;
 | 
			
		||||
class Connector;
 | 
			
		||||
 | 
			
		||||
class AddWire : public QUndoCommand
 | 
			
		||||
{
 | 
			
		||||
public:
 | 
			
		||||
    AddWire(Scene* scene, Connector* connectorInput, Connector* connectorOutput);
 | 
			
		||||
    ~AddWire();
 | 
			
		||||
 | 
			
		||||
    void redo() override;
 | 
			
		||||
    void undo() override;
 | 
			
		||||
 | 
			
		||||
private:
 | 
			
		||||
    Scene* m_scene;
 | 
			
		||||
    Wire* m_wire;
 | 
			
		||||
    Connector* m_connectorInput;
 | 
			
		||||
    Connector* m_connectorOutput;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
#endif // ADDWIRE_H
 | 
			
		||||
							
								
								
									
										59
									
								
								UndoCommands/CopyParts.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										59
									
								
								UndoCommands/CopyParts.cpp
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,59 @@
 | 
			
		||||
#include "CopyParts.h"
 | 
			
		||||
 | 
			
		||||
#include <QMap>
 | 
			
		||||
 | 
			
		||||
#include "../Scene.h"
 | 
			
		||||
#include "../Logic.h"
 | 
			
		||||
#include "../Part.h"
 | 
			
		||||
#include "../Connector.h"
 | 
			
		||||
#include "../CircuitBuffer.h"
 | 
			
		||||
 | 
			
		||||
#include "RemoveWire.h"
 | 
			
		||||
 | 
			
		||||
CopyParts::CopyParts(Scene* scene, const CircuitBuffer& toCopy, QPointF relPos)
 | 
			
		||||
    :m_scene(scene), m_toCopy(toCopy), m_relPos(relPos), m_wireUndoStack(new QUndoStack)
 | 
			
		||||
{
 | 
			
		||||
    setText("Copy Parts with offset " + QString::number(relPos.x()) + ", " + QString::number(relPos.y()));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
CopyParts::~CopyParts()
 | 
			
		||||
{
 | 
			
		||||
    for(auto part : m_copiedParts)
 | 
			
		||||
        m_scene->m_logic->deletePart(part);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void CopyParts::redo()
 | 
			
		||||
{
 | 
			
		||||
    if(m_isFirstRedo)
 | 
			
		||||
    {
 | 
			
		||||
        m_isFirstRedo = false;
 | 
			
		||||
 | 
			
		||||
        m_copiedParts = m_toCopy.addIntoScene(m_scene, m_relPos).first;
 | 
			
		||||
    }
 | 
			
		||||
    else
 | 
			
		||||
    {
 | 
			
		||||
        for(auto part : m_copiedParts)
 | 
			
		||||
            m_scene->startTrackingPart(part);
 | 
			
		||||
        // Add wires back in
 | 
			
		||||
        while(m_wireUndoStack->canUndo())
 | 
			
		||||
                m_wireUndoStack->undo();
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void CopyParts::undo()
 | 
			
		||||
{
 | 
			
		||||
    for(auto part : m_copiedParts)
 | 
			
		||||
    {
 | 
			
		||||
        m_scene->stopTrackingPart(part);
 | 
			
		||||
        for(auto input : part->m_inputs)
 | 
			
		||||
        {
 | 
			
		||||
            for(auto wire : input->m_wires)
 | 
			
		||||
                m_wireUndoStack->push(new RemoveWire(m_scene, wire));
 | 
			
		||||
        }
 | 
			
		||||
        for(auto output : part->m_outputs)
 | 
			
		||||
        {
 | 
			
		||||
            for(auto wire : output->m_wires)
 | 
			
		||||
                m_wireUndoStack->push(new RemoveWire(m_scene, wire));
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										43
									
								
								UndoCommands/CopyParts.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										43
									
								
								UndoCommands/CopyParts.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,43 @@
 | 
			
		||||
#ifndef COPYPARTS_H
 | 
			
		||||
#define COPYPARTS_H
 | 
			
		||||
 | 
			
		||||
#include <QUndoStack>
 | 
			
		||||
#include <QUndoCommand>
 | 
			
		||||
#include <QList>
 | 
			
		||||
#include <QPointF>
 | 
			
		||||
 | 
			
		||||
#include "../ePartType.h"
 | 
			
		||||
 | 
			
		||||
class Scene;
 | 
			
		||||
class Part;
 | 
			
		||||
class Wire;
 | 
			
		||||
 | 
			
		||||
class CircuitBuffer;
 | 
			
		||||
 | 
			
		||||
class CopyParts : public QUndoCommand
 | 
			
		||||
{
 | 
			
		||||
public:
 | 
			
		||||
    typedef QPair<PartType::PartType, QPointF> PartData;
 | 
			
		||||
    typedef QPair<PartData*, PartData*> WireData;
 | 
			
		||||
 | 
			
		||||
    friend class Scene;
 | 
			
		||||
 | 
			
		||||
    CopyParts(Scene* scene, const CircuitBuffer& toCopy, QPointF relPos);
 | 
			
		||||
    ~CopyParts();
 | 
			
		||||
 | 
			
		||||
    void redo() override;
 | 
			
		||||
    void undo() override;
 | 
			
		||||
 | 
			
		||||
private:
 | 
			
		||||
    Scene* m_scene;
 | 
			
		||||
    // Since m_toCopy get used immediately, there shouldn't be any segfaults due to any parts that have changed
 | 
			
		||||
    const CircuitBuffer& m_toCopy;
 | 
			
		||||
    QList<Part*> m_copiedParts;
 | 
			
		||||
    QPointF m_relPos;
 | 
			
		||||
 | 
			
		||||
    bool m_isFirstRedo = true;
 | 
			
		||||
 | 
			
		||||
    QUndoStack* m_wireUndoStack;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
#endif // COPYPARTS_H
 | 
			
		||||
							
								
								
									
										31
									
								
								UndoCommands/MoveParts.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										31
									
								
								UndoCommands/MoveParts.cpp
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,31 @@
 | 
			
		||||
#include "MoveParts.h"
 | 
			
		||||
 | 
			
		||||
#include "../Part.h"
 | 
			
		||||
 | 
			
		||||
MoveParts::MoveParts(Scene* scene, const QList<Part*>& parts, QPointF relPos)
 | 
			
		||||
    :m_scene(scene), m_parts(parts), m_relPos(relPos)
 | 
			
		||||
{
 | 
			
		||||
    setText("Move parts by " + QString::number(relPos.x()) + ", " + QString::number(relPos.y()));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
MoveParts::~MoveParts()
 | 
			
		||||
{
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void MoveParts::redo()
 | 
			
		||||
{
 | 
			
		||||
    if(m_isFirstRedo)
 | 
			
		||||
        m_isFirstRedo = false;
 | 
			
		||||
    else
 | 
			
		||||
    {
 | 
			
		||||
        for(auto part : m_parts)
 | 
			
		||||
            part->moveBy(m_relPos.x(), m_relPos.y());
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
void MoveParts::undo()
 | 
			
		||||
{
 | 
			
		||||
    for(auto part : m_parts)
 | 
			
		||||
        part->moveBy(-m_relPos.x(), -m_relPos.y());
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										27
									
								
								UndoCommands/MoveParts.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										27
									
								
								UndoCommands/MoveParts.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,27 @@
 | 
			
		||||
#ifndef MOVEPARTS_H
 | 
			
		||||
#define MOVEPARTS_H
 | 
			
		||||
 | 
			
		||||
#include <QUndoCommand>
 | 
			
		||||
#include <QPointF>
 | 
			
		||||
 | 
			
		||||
class Scene;
 | 
			
		||||
class Part;
 | 
			
		||||
 | 
			
		||||
class MoveParts : public QUndoCommand
 | 
			
		||||
{
 | 
			
		||||
public:
 | 
			
		||||
    MoveParts(Scene* scene, const QList<Part*>& parts, QPointF relPos);
 | 
			
		||||
    ~MoveParts();
 | 
			
		||||
 | 
			
		||||
    void redo() override;
 | 
			
		||||
    void undo() override;
 | 
			
		||||
 | 
			
		||||
private:
 | 
			
		||||
    Scene* m_scene;
 | 
			
		||||
    QList<Part*> m_parts;
 | 
			
		||||
    QPointF m_relPos;
 | 
			
		||||
 | 
			
		||||
    bool m_isFirstRedo = true;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
#endif // MOVEPARTS_H
 | 
			
		||||
							
								
								
									
										52
									
								
								UndoCommands/RemoveParts.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										52
									
								
								UndoCommands/RemoveParts.cpp
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,52 @@
 | 
			
		||||
#include "RemoveParts.h"
 | 
			
		||||
 | 
			
		||||
#include "../Scene.h"
 | 
			
		||||
#include "../Part.h"
 | 
			
		||||
#include "../Connector.h"
 | 
			
		||||
#include "../Logic.h"
 | 
			
		||||
#include "RemoveWire.h"
 | 
			
		||||
 | 
			
		||||
RemoveParts::RemoveParts(Scene* scene, const QList<Part*>& parts)
 | 
			
		||||
    :m_scene(scene), m_parts(parts), m_wireUndoStack(new QUndoStack)
 | 
			
		||||
{
 | 
			
		||||
    setText("Remove Parts");
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
RemoveParts::~RemoveParts()
 | 
			
		||||
{
 | 
			
		||||
    for(auto part : m_parts)
 | 
			
		||||
        m_scene->m_logic->deletePart(part);
 | 
			
		||||
    delete m_wireUndoStack;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void RemoveParts::redo()
 | 
			
		||||
{
 | 
			
		||||
    for(auto part : m_parts)
 | 
			
		||||
    {
 | 
			
		||||
        for(auto input : part->m_inputs)
 | 
			
		||||
        {
 | 
			
		||||
            for(auto wire : input->m_wires)
 | 
			
		||||
                // Remove wire
 | 
			
		||||
                m_wireUndoStack->push(new RemoveWire(m_scene, wire));
 | 
			
		||||
        }
 | 
			
		||||
        for(auto output : part->m_outputs)
 | 
			
		||||
        {
 | 
			
		||||
            for(auto wire : output->m_wires)
 | 
			
		||||
                // Remove wire
 | 
			
		||||
                m_wireUndoStack->push(new RemoveWire(m_scene, wire));
 | 
			
		||||
        }
 | 
			
		||||
        m_scene->stopTrackingPart(part);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void RemoveParts::undo()
 | 
			
		||||
{
 | 
			
		||||
    for(auto part : m_parts)
 | 
			
		||||
    {
 | 
			
		||||
        m_scene->startTrackingPart(part);
 | 
			
		||||
 | 
			
		||||
        // Add wires back in
 | 
			
		||||
        while(m_wireUndoStack->canUndo())
 | 
			
		||||
                m_wireUndoStack->undo();
 | 
			
		||||
        }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										27
									
								
								UndoCommands/RemoveParts.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										27
									
								
								UndoCommands/RemoveParts.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,27 @@
 | 
			
		||||
#ifndef REMOVEPARTS_H
 | 
			
		||||
#define REMOVEPARTS_H
 | 
			
		||||
 | 
			
		||||
#include <QUndoCommand>
 | 
			
		||||
#include <QList>
 | 
			
		||||
 | 
			
		||||
class Scene;
 | 
			
		||||
class Part;
 | 
			
		||||
class Connector;
 | 
			
		||||
 | 
			
		||||
class RemoveParts : public QUndoCommand
 | 
			
		||||
{
 | 
			
		||||
public:
 | 
			
		||||
    RemoveParts(Scene* scene, const QList<Part*>& parts);
 | 
			
		||||
    ~RemoveParts();
 | 
			
		||||
 | 
			
		||||
    void redo() override;
 | 
			
		||||
    void undo() override;
 | 
			
		||||
 | 
			
		||||
private:
 | 
			
		||||
    Scene* m_scene;
 | 
			
		||||
    QList<Part*> m_parts;
 | 
			
		||||
 | 
			
		||||
    QUndoStack* m_wireUndoStack;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
#endif // REMOVEPARTS_H
 | 
			
		||||
							
								
								
									
										36
									
								
								UndoCommands/RemoveWire.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										36
									
								
								UndoCommands/RemoveWire.cpp
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,36 @@
 | 
			
		||||
#include "RemoveWire.h"
 | 
			
		||||
 | 
			
		||||
#include "../Scene.h"
 | 
			
		||||
#include "../Logic.h"
 | 
			
		||||
#include "../Wire.h"
 | 
			
		||||
#include "../Part.h"
 | 
			
		||||
#include "../Connector.h"
 | 
			
		||||
 | 
			
		||||
RemoveWire::RemoveWire(Scene* scene, Wire* wire)
 | 
			
		||||
    :m_scene(scene), m_wire(wire)
 | 
			
		||||
{
 | 
			
		||||
    setText("Remove Wire");
 | 
			
		||||
 | 
			
		||||
    m_wireInputPart = (Part*)wire->m_connectorInput->parentItem();
 | 
			
		||||
    m_wireInputConnectorIdx = m_wireInputPart->m_outputs.indexOf(wire->m_connectorInput);
 | 
			
		||||
 | 
			
		||||
    m_wireOutputPart = (Part*)wire->m_connectorOutput->parentItem();
 | 
			
		||||
    m_wireOutputConnectorIdx = m_wireOutputPart->m_inputs.indexOf(wire->m_connectorOutput);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
RemoveWire::~RemoveWire()
 | 
			
		||||
{
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void RemoveWire::redo()
 | 
			
		||||
{
 | 
			
		||||
    m_scene->m_logic->deleteWire(m_wire);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
void RemoveWire::undo()
 | 
			
		||||
{
 | 
			
		||||
    Connector* inputConnector = m_wireInputPart->m_outputs[m_wireInputConnectorIdx];
 | 
			
		||||
    Connector* outputConnector = m_wireOutputPart->m_inputs[m_wireOutputConnectorIdx];
 | 
			
		||||
    m_wire = m_scene->m_logic->createWire(inputConnector, outputConnector);
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										28
									
								
								UndoCommands/RemoveWire.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										28
									
								
								UndoCommands/RemoveWire.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,28 @@
 | 
			
		||||
#ifndef REMOVEWIRE_H
 | 
			
		||||
#define REMOVEWIRE_H
 | 
			
		||||
 | 
			
		||||
#include <QUndoCommand>
 | 
			
		||||
 | 
			
		||||
class Scene;
 | 
			
		||||
class Wire;
 | 
			
		||||
class Part;
 | 
			
		||||
 | 
			
		||||
class RemoveWire : public QUndoCommand
 | 
			
		||||
{
 | 
			
		||||
public:    
 | 
			
		||||
    RemoveWire(Scene* scene, Wire* wire);
 | 
			
		||||
    ~RemoveWire();
 | 
			
		||||
 | 
			
		||||
    void redo() override;
 | 
			
		||||
    void undo() override;
 | 
			
		||||
 | 
			
		||||
private:  
 | 
			
		||||
    Scene* m_scene;
 | 
			
		||||
    Wire* m_wire;
 | 
			
		||||
    Part* m_wireInputPart;
 | 
			
		||||
    int m_wireInputConnectorIdx;
 | 
			
		||||
    Part* m_wireOutputPart;
 | 
			
		||||
    int m_wireOutputConnectorIdx;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
#endif // REMOVEWIRE_H
 | 
			
		||||
		Reference in New Issue
	
	Block a user