Fix bug where undoing+redoing AddWire multiple times crashes

This commit is contained in:
xypwn 2020-03-28 11:15:04 +01:00
parent de9e38ddf7
commit 9414c8c426
7 changed files with 28 additions and 7 deletions

View File

@ -35,8 +35,8 @@ void CircuitBuffer::addFromScene(const QList<Part*> &parts, const QList<Wire*>&
} }
for(auto wire : wires) for(auto wire : wires)
{ {
auto wireInputPart = (Part*)wire->m_connectorInput->parentItem(); auto wireInputPart = wire->m_connectorInput->parentPart();
auto wireOutputPart = (Part*)wire->m_connectorOutput->parentItem(); auto wireOutputPart = wire->m_connectorOutput->parentPart();
// Create wireData // Create wireData
WireData wireData; WireData wireData;

View File

@ -17,6 +17,11 @@ Connector::Connector(Scene* scene, Part *parentPart, ConnectorType::ConnectorTyp
setFlag(QGraphicsItem::ItemSendsScenePositionChanges); setFlag(QGraphicsItem::ItemSendsScenePositionChanges);
} }
Part* Connector::parentPart()
{
return (Part*)parentItem();
}
QRectF Connector::boundingRect() const QRectF Connector::boundingRect() const
{ {
if(m_connectorType == ConnectorType::Output) if(m_connectorType == ConnectorType::Output)

View File

@ -9,7 +9,7 @@ class Part;
class Wire; class Wire;
class Scene; class Scene;
class Connector : public QGraphicsItem class Connector : private QGraphicsItem
{ {
public: public:
friend class Scene; friend class Scene;
@ -24,6 +24,8 @@ public:
Connector(Scene* scene, Part *parentPart, ConnectorType::ConnectorType side); Connector(Scene* scene, Part *parentPart, ConnectorType::ConnectorType side);
Part* parentPart();
QRectF boundingRect() const override; // For drawing QRectF boundingRect() const override; // For drawing
QPainterPath shape() const override; // For selection ("Hitbox") QPainterPath shape() const override; // For selection ("Hitbox")
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override; void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override;

View File

@ -52,8 +52,8 @@ bool FileHandler::save(QString filename)
fOut << "[wires]\n"; fOut << "[wires]\n";
for(auto wire : m_logic->m_wires) for(auto wire : m_logic->m_wires)
{ {
auto wireInputPart = (Part*)wire->m_connectorInput->parentItem(); auto wireInputPart = wire->m_connectorInput->parentPart();
auto wireOutputPart = (Part*)wire->m_connectorOutput->parentItem(); auto wireOutputPart = wire->m_connectorOutput->parentPart();
fOut << wireInputPart << " " << wireInputPart->m_outputs.indexOf(wire->m_connectorInput) << " " << wireOutputPart << " " << wireOutputPart->m_inputs.indexOf(wire->m_connectorOutput) << "\n"; fOut << wireInputPart << " " << wireInputPart->m_outputs.indexOf(wire->m_connectorInput) << " " << wireOutputPart << " " << wireOutputPart->m_inputs.indexOf(wire->m_connectorOutput) << "\n";
} }

View File

@ -34,6 +34,16 @@ PartType::PartType Part::partType()
return m_partType; return m_partType;
} }
QPointF Part::getPos() const
{
return pos();
}
void Part::setPos(QPointF pos)
{
QGraphicsItem::setPos(pos);
}
void Part::addInputs(int amount) void Part::addInputs(int amount)
{ {
for(int i = 0; i < amount; i++) for(int i = 0; i < amount; i++)

6
Part.h
View File

@ -9,7 +9,7 @@
class Connector; class Connector;
class Logic; class Logic;
class Part : public QGraphicsItem class Part : protected QGraphicsItem
{ {
public: public:
friend class Connector; friend class Connector;
@ -17,6 +17,7 @@ public:
friend class Logic; friend class Logic;
friend class AddPart; friend class AddPart;
friend class RemoveParts; friend class RemoveParts;
friend class AddWire;
friend class RemoveWire; friend class RemoveWire;
friend class CopyParts; friend class CopyParts;
@ -45,6 +46,9 @@ public:
PartType::PartType partType(); PartType::PartType partType();
QPointF getPos() const;
void setPos(QPointF pos);
void addInputs(int amount); void addInputs(int amount);
void addOutputs(int amount); void addOutputs(int amount);

2
Wire.h
View File

@ -6,7 +6,7 @@
class Connector; class Connector;
class Scene; class Scene;
class Wire : public QGraphicsLineItem class Wire : private QGraphicsLineItem
{ {
public: public:
friend class Connector; friend class Connector;