Wires now move when the component is dragged

This commit is contained in:
Robert 2020-12-03 14:55:03 +01:00
parent 78da43f51d
commit 6ca89f0995
4 changed files with 54 additions and 11 deletions

View file

@ -6,17 +6,27 @@ namespace Ui {
class Component;
}
typedef struct {
QLine* line;
enum class eType {
SOURCE,
TARGET
} type;
} Wire;
class Component : public QFrame
{
public:
explicit Component(QWidget* parent);
explicit Component(QWidget* parent, const QString& resource);
~Component();
virtual void mouseMoveEvent(QMouseEvent* event) override;
QPoint CenterPos();
void Connect(Component* component);
void Connect(Component* component, QLine* wire);
const QString& Type();
void UpdateWires();
virtual bool Action() = 0;
@ -26,4 +36,5 @@ protected:
QString type;
std::vector<Component*> connections;
std::vector<Wire*> wires;
};

View file

@ -50,5 +50,5 @@ private:
} componentType;
std::vector<Component*> components;
std::vector<QLine> wires;
std::vector<QLine*> wires;
};

View file

@ -19,6 +19,15 @@ Component::Component(QWidget* parent, const QString& resource) :
ui->label->setPixmap(QPixmap(resource));
}
Component::~Component()
{
for (Wire* w : wires)
{
delete w;
w = nullptr;
}
}
void Component::mouseMoveEvent(QMouseEvent* event)
{
event->ignore();
@ -29,12 +38,29 @@ QPoint Component::CenterPos()
return pos() + QPoint(size().width() / 2, size().height() / 2);
}
void Component::Connect(Component* component)
void Component::Connect(Component* component, QLine* wire)
{
connections.push_back(component);
this->wires.push_back(new Wire{ wire, Wire::eType::TARGET});
component->wires.push_back(new Wire{ wire, Wire::eType::SOURCE});
}
const QString& Component::Type()
{
return type;
}
void Component::UpdateWires()
{
for (Wire* wire : wires)
{
if (wire->type == Wire::eType::SOURCE)
{
wire->line->setP1(this->CenterPos());
}
else
{
wire->line->setP2(this->CenterPos());
}
}
}

View file

@ -76,6 +76,12 @@ Window::~Window()
delete c;
c = nullptr;
}
for (QLine* line : wires)
{
delete line;
line = nullptr;
}
}
void Window::mousePressEvent(QMouseEvent* event)
@ -101,7 +107,7 @@ void Window::mousePressEvent(QMouseEvent* event)
QPoint mousePos = ui->centralwidget->mapFromParent(event->pos());
mousePos.setY(mousePos.y() + ui->toolBar->height());
wires.push_back(QLine(child->CenterPos(), mousePos));
wires.push_back(new QLine(child->CenterPos(), mousePos));
// std::cout << "(" << child->x() << ", " << child->y() << ") --> (" << mousePos.x() << ", " << mousePos.y() << ")" << std::endl;
}
@ -121,7 +127,7 @@ void Window::mouseMoveEvent(QMouseEvent* event)
if (dragInfo.component != nullptr)
{
dragInfo.component->move(event->pos() - dragInfo.relativePos);
// Move wires too lol
dragInfo.component->UpdateWires();
}
if (ghostImage != nullptr)
@ -131,7 +137,7 @@ void Window::mouseMoveEvent(QMouseEvent* event)
{
QPoint mousePos = ui->centralwidget->mapFromParent(event->pos());
mousePos.setY(mousePos.y() + ui->toolBar->height());
wires.back().setP2(mousePos);
wires.back()->setP2(mousePos);
}
event->accept();
@ -144,7 +150,7 @@ void Window::mouseReleaseEvent(QMouseEvent* event)
if (componentGroup->checkedAction() == ui->actionWiring)
{
Component* connectFrom = componentAt(wires.back().p1());
Component* connectFrom = componentAt(wires.back()->p1());
Component* connectTo = componentAt(event->pos());
if (connectTo == nullptr || connectTo == connectFrom)
{
@ -152,8 +158,8 @@ void Window::mouseReleaseEvent(QMouseEvent* event)
}
else
{
wires.back().setP2(connectTo->CenterPos());
connectTo->Connect(connectFrom);
wires.back()->setP2(connectTo->CenterPos());
connectTo->Connect(connectFrom, wires.back());
}
}
else if(componentGroup->checkedAction() == ui->actionCursor)
@ -172,8 +178,8 @@ void Window::paintEvent(QPaintEvent* event)
QPainter painter(this);
painter.setPen(pen);
for(QLine line : wires)
painter.drawLine(line);
for(QLine* line : wires)
painter.drawLine(*line);
update();
}