From 448f0771258d7b0d21a3fa3eb92f1e49e86e1aba Mon Sep 17 00:00:00 2001 From: lipengpeng Date: Wed, 6 Aug 2025 19:15:23 +0800 Subject: [PATCH] =?UTF-8?q?PLC=E6=A8=A1=E5=9D=97=E7=9A=84=E7=BC=96?= =?UTF-8?q?=E8=BE=91=E7=95=8C=E9=9D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- connection.cpp | 12 +++++++++++ connection.h | 1 + item.h | 1 - mainwindow.cpp | 53 ++++++++++++++++++++++++++++++++++++++++++++++++ mainwindow.h | 6 ++++++ mainwindow.ui | 13 ++++++++++++ mygraphicsview.h | 2 +- 7 files changed, 86 insertions(+), 2 deletions(-) diff --git a/connection.cpp b/connection.cpp index 305c18c..7be132f 100644 --- a/connection.cpp +++ b/connection.cpp @@ -1,6 +1,7 @@ #include "connection.h" #include +#include Connection::Connection(Item* from, Item::AnchorType fromType, Item* to, Item::AnchorType toType, QGraphicsItem* parent) @@ -19,3 +20,14 @@ void Connection::updatePosition() { setLine(QLineF(from_->anchorPos(fromType_), to_->anchorPos(toType_))); } + +void Connection::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *) +{ + QPen pen = this->pen(); + if (isSelected()) { + pen.setColor(Qt::red); // 选中变红 + pen.setWidth(3); + } + painter->setPen(pen); + painter->drawLine(line()); +} diff --git a/connection.h b/connection.h index e171bfa..868c44f 100644 --- a/connection.h +++ b/connection.h @@ -11,6 +11,7 @@ public: Item* to, Item::AnchorType toType, QGraphicsItem* parent = nullptr); void updatePosition(); + void paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *) override; Item *from_, *to_; Item::AnchorType fromType_, toType_; diff --git a/item.h b/item.h index fa9b2ca..900a31d 100644 --- a/item.h +++ b/item.h @@ -35,7 +35,6 @@ protected: private: QString type_; - QColor color_; QList connections_; }; diff --git a/mainwindow.cpp b/mainwindow.cpp index 83e85f7..a3993c8 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -9,6 +9,7 @@ #include #include #include +#include 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 selectedItems = ui->graphicsView->scene()->selectedItems(); + Connection* selectedConn = nullptr; + for (QGraphicsItem* item : selectedItems) { + selectedConn = dynamic_cast(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); +} diff --git a/mainwindow.h b/mainwindow.h index d0ae3a3..e473d57 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -19,11 +19,17 @@ public: protected: bool eventFilter(QObject *obj, QEvent *event) override; +private slots: + void on_listWidget_currentTextChanged(const QString ¤tText); + + void on_pushButton_clicked(); + private: void createComponents(); Ui::MainWindow *ui; QGraphicsScene *m_scene; + QString selectedComponentType; }; #endif // MAINWINDOW_H diff --git a/mainwindow.ui b/mainwindow.ui index 971f2e0..863f94c 100644 --- a/mainwindow.ui +++ b/mainwindow.ui @@ -37,6 +37,19 @@ QGraphicsView::NoDrag + + + + 30 + 510 + 93 + 28 + + + + 插入 + + diff --git a/mygraphicsview.h b/mygraphicsview.h index 81817a6..420a07f 100644 --- a/mygraphicsview.h +++ b/mygraphicsview.h @@ -40,7 +40,7 @@ private: }; static ClipInfo clipboard_; -private slots: +public slots: void onItemRequestCopy(Item*); void onItemRequestDelete(Item*); };