Fix most undo/redo bugs
This commit is contained in:
		@@ -5,8 +5,8 @@
 | 
			
		||||
#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(); }
 | 
			
		||||
static bool compareToggleButtons(const ToggleButton* a, const ToggleButton* b) { return a->getPos().y() < b->getPos().y(); }
 | 
			
		||||
static bool compareLightBulbs(const LightBulb* a, const LightBulb* b) { return a->getPos().y() < b->getPos().y(); }
 | 
			
		||||
 | 
			
		||||
IntegratedCircuit::IntegratedCircuit(Logic* logic, QString filename)
 | 
			
		||||
    :Part(logic), m_filename(filename), m_icLogic(new Logic)
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										28
									
								
								Scene.cpp
									
									
									
									
									
								
							
							
						
						
									
										28
									
								
								Scene.cpp
									
									
									
									
									
								
							@@ -169,13 +169,37 @@ void Scene::stopTrackingPart(Part* part)
 | 
			
		||||
    // Remove part from tracker list
 | 
			
		||||
    m_logic->m_parts.removeOne(part);
 | 
			
		||||
    for(auto input : part->m_inputs)
 | 
			
		||||
        // Add connector from tracker list
 | 
			
		||||
        // Remove connector from tracker list
 | 
			
		||||
        m_logic->m_inputConnectors.removeOne(input);
 | 
			
		||||
    for(auto output : part->m_outputs)
 | 
			
		||||
        // Add connector from tracker list
 | 
			
		||||
        // Remove connector from tracker list
 | 
			
		||||
        m_logic->m_outputConnectors.removeOne(output);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void Scene::startTrackingWire(Wire* wire)
 | 
			
		||||
{
 | 
			
		||||
    // Show wire
 | 
			
		||||
    wire->show();
 | 
			
		||||
    // Add wire back into tracker list
 | 
			
		||||
    m_logic->m_wires.append(wire);
 | 
			
		||||
    // Add wire back into it's input connector's wire list
 | 
			
		||||
    wire->m_connectorInput->m_wires.append(wire);
 | 
			
		||||
    // Add wire back into it's input connector's wire list
 | 
			
		||||
    wire->m_connectorOutput->m_wires.append(wire);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void Scene::stopTrackingWire(Wire* wire)
 | 
			
		||||
{
 | 
			
		||||
    // Hide wire
 | 
			
		||||
    wire->hide();
 | 
			
		||||
    // Remove wire from tracker list
 | 
			
		||||
    m_logic->m_wires.removeOne(wire);
 | 
			
		||||
    // Remove wire from it's input connector's wire list
 | 
			
		||||
    wire->m_connectorInput->m_wires.removeOne(wire);
 | 
			
		||||
    // Remove wire from it's output connector's wire list
 | 
			
		||||
    wire->m_connectorOutput->m_wires.removeOne(wire);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void Scene::connectorClicked(Connector *connector)
 | 
			
		||||
{
 | 
			
		||||
    if(m_parentMainWindow->toolMode == MainWindow::Disconnect)
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										3
									
								
								Scene.h
									
									
									
									
									
								
							
							
						
						
									
										3
									
								
								Scene.h
									
									
									
									
									
								
							@@ -84,6 +84,9 @@ private:
 | 
			
		||||
    void startTrackingPart(Part* part);
 | 
			
		||||
    void stopTrackingPart(Part* part);
 | 
			
		||||
 | 
			
		||||
    void startTrackingWire(Wire* wire);
 | 
			
		||||
    void stopTrackingWire(Wire* wire);
 | 
			
		||||
 | 
			
		||||
    // Called by Connector when it is clicked, for example when creating or deleting wires
 | 
			
		||||
    void connectorClicked(Connector *connector);
 | 
			
		||||
    // Removal is done undoably
 | 
			
		||||
 
 | 
			
		||||
@@ -3,6 +3,8 @@
 | 
			
		||||
#include "../Scene.h"
 | 
			
		||||
#include "../Logic.h"
 | 
			
		||||
#include "../Wire.h"
 | 
			
		||||
#include "../Connector.h"
 | 
			
		||||
#include "../Part.h"
 | 
			
		||||
 | 
			
		||||
AddWire::AddWire(Scene* scene, Connector* connectorInput, Connector* connectorOutput)
 | 
			
		||||
    :m_scene(scene), m_connectorInput(connectorInput), m_connectorOutput(connectorOutput)
 | 
			
		||||
@@ -12,15 +14,20 @@ AddWire::AddWire(Scene* scene, Connector* connectorInput, Connector* connectorOu
 | 
			
		||||
 | 
			
		||||
AddWire::~AddWire()
 | 
			
		||||
{
 | 
			
		||||
    m_scene->m_logic->deleteWire(m_wire);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
void AddWire::redo()
 | 
			
		||||
{
 | 
			
		||||
    m_wire = m_scene->m_logic->createWire(m_connectorInput, m_connectorOutput);
 | 
			
		||||
    if(m_wire == nullptr)
 | 
			
		||||
        m_wire = m_scene->m_logic->createWire(m_connectorInput, m_connectorOutput);
 | 
			
		||||
    else
 | 
			
		||||
    {
 | 
			
		||||
        m_scene->startTrackingWire(m_wire);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void AddWire::undo()
 | 
			
		||||
{
 | 
			
		||||
    m_scene->m_logic->deleteWire(m_wire);
 | 
			
		||||
    m_scene->stopTrackingWire(m_wire);
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -6,6 +6,7 @@
 | 
			
		||||
class Scene;
 | 
			
		||||
class Wire;
 | 
			
		||||
class Connector;
 | 
			
		||||
class Part;
 | 
			
		||||
 | 
			
		||||
class AddWire : public QUndoCommand
 | 
			
		||||
{
 | 
			
		||||
@@ -18,7 +19,7 @@ public:
 | 
			
		||||
 | 
			
		||||
private:
 | 
			
		||||
    Scene* m_scene;
 | 
			
		||||
    Wire* m_wire;
 | 
			
		||||
    Wire* m_wire = nullptr;
 | 
			
		||||
    Connector* m_connectorInput;
 | 
			
		||||
    Connector* m_connectorOutput;
 | 
			
		||||
};
 | 
			
		||||
 
 | 
			
		||||
@@ -19,7 +19,7 @@ void MoveParts::redo()
 | 
			
		||||
    else
 | 
			
		||||
    {
 | 
			
		||||
        for(auto part : m_parts)
 | 
			
		||||
            part->moveBy(m_relPos.x(), m_relPos.y());
 | 
			
		||||
            part->setPos(part->getPos() + m_relPos);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -27,5 +27,5 @@ void MoveParts::redo()
 | 
			
		||||
void MoveParts::undo()
 | 
			
		||||
{
 | 
			
		||||
    for(auto part : m_parts)
 | 
			
		||||
        part->moveBy(-m_relPos.x(), -m_relPos.y());
 | 
			
		||||
        part->setPos(part->getPos() - m_relPos);
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -14,8 +14,6 @@ RemoveParts::RemoveParts(Scene* scene, const QList<Part*>& parts)
 | 
			
		||||
 | 
			
		||||
RemoveParts::~RemoveParts()
 | 
			
		||||
{
 | 
			
		||||
    for(auto part : m_parts)
 | 
			
		||||
        m_scene->m_logic->deletePart(part);
 | 
			
		||||
    delete m_wireUndoStack;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -4,18 +4,12 @@
 | 
			
		||||
#include "../Logic.h"
 | 
			
		||||
#include "../Wire.h"
 | 
			
		||||
#include "../Part.h"
 | 
			
		||||
#include "../Connector.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()
 | 
			
		||||
@@ -24,13 +18,11 @@ RemoveWire::~RemoveWire()
 | 
			
		||||
 | 
			
		||||
void RemoveWire::redo()
 | 
			
		||||
{
 | 
			
		||||
    m_scene->m_logic->deleteWire(m_wire);
 | 
			
		||||
    m_scene->stopTrackingWire(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);
 | 
			
		||||
    m_scene->startTrackingWire(m_wire);
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -19,10 +19,6 @@ public:
 | 
			
		||||
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