|
|
@@ -9,6 +9,7 @@ |
|
|
|
#include <QMouseEvent> |
|
|
|
#include <QDropEvent> |
|
|
|
#include <QGraphicsView> |
|
|
|
#include <QMessageBox> |
|
|
|
|
|
|
|
MainWindow::MainWindow(QWidget *parent) |
|
|
|
: QMainWindow(parent) |
|
|
@@ -103,3 +104,55 @@ void MainWindow::createComponents() |
|
|
|
it->setData(Qt::UserRole, c.name); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
void MainWindow::on_listWidget_currentTextChanged(const QString &text) |
|
|
|
{ |
|
|
|
selectedComponentType = text; |
|
|
|
} |
|
|
|
|
|
|
|
void MainWindow::on_pushButton_clicked() |
|
|
|
{ |
|
|
|
// 1. 找场景中被选中的连线 |
|
|
|
QList<QGraphicsItem*> selectedItems = ui->graphicsView->scene()->selectedItems(); |
|
|
|
Connection* selectedConn = nullptr; |
|
|
|
for (QGraphicsItem* item : selectedItems) { |
|
|
|
selectedConn = dynamic_cast<Connection*>(item); |
|
|
|
if (selectedConn) break; |
|
|
|
} |
|
|
|
if (!selectedConn) { |
|
|
|
QMessageBox::warning(this, "提示", "请先选中一条连线"); |
|
|
|
return; |
|
|
|
} |
|
|
|
if (selectedComponentType.isEmpty()) { |
|
|
|
QMessageBox::warning(this, "提示", "请先在列表中选择要插入的组件"); |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
// 2. 计算插入点(中点) |
|
|
|
QLineF lf = selectedConn->line(); |
|
|
|
QPointF insertPos = (lf.p1() + lf.p2()) / 2; |
|
|
|
|
|
|
|
// 3. 删除原连线 |
|
|
|
Item* from = selectedConn->from_; |
|
|
|
Item* to = selectedConn->to_; |
|
|
|
Item::AnchorType fromType = selectedConn->fromType_; |
|
|
|
Item::AnchorType toType = selectedConn->toType_; |
|
|
|
|
|
|
|
from->removeConnection(selectedConn); |
|
|
|
to->removeConnection(selectedConn); |
|
|
|
ui->graphicsView->scene()->removeItem(selectedConn); |
|
|
|
delete selectedConn; |
|
|
|
|
|
|
|
// 4. 插入新元件 |
|
|
|
Item* newItem = new Item(selectedComponentType); |
|
|
|
newItem->setPos(insertPos); |
|
|
|
connect(newItem, &Item::requestCopy, ui->graphicsView, &MyGraphicsView::onItemRequestCopy); |
|
|
|
connect(newItem, &Item::requestDelete, ui->graphicsView, &MyGraphicsView::onItemRequestDelete); |
|
|
|
ui->graphicsView->scene()->addItem(newItem); |
|
|
|
|
|
|
|
// 5. 新建两条连线 |
|
|
|
Connection* c1 = new Connection(from, fromType, newItem, Item::Left); |
|
|
|
ui->graphicsView->scene()->addItem(c1); |
|
|
|
Connection* c2 = new Connection(newItem, Item::Right, to, toType); |
|
|
|
ui->graphicsView->scene()->addItem(c2); |
|
|
|
} |