From adbb053276674368ddf1008ca54e887d7ca1bd79 Mon Sep 17 00:00:00 2001 From: lipengpeng Date: Fri, 8 Aug 2025 09:11:12 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=E6=96=87=E4=BB=B6=E4=BF=9D?= =?UTF-8?q?=E5=AD=98=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- coil.cpp | 25 +++++++++++++++++-------- comparator.cpp | 9 +++++++++ contact.cpp | 26 ++++++++++++++++++-------- editor.pro.user | 2 +- hmi.cpp | 11 +++-------- item.cpp | 4 ++-- mygraphicsview.cpp | 4 ++-- plc.cpp | 3 +++ project.cpp | 2 ++ project.h | 1 + 10 files changed, 58 insertions(+), 29 deletions(-) diff --git a/coil.cpp b/coil.cpp index 2ded01a..30b0c69 100644 --- a/coil.cpp +++ b/coil.cpp @@ -23,12 +23,21 @@ void Coil::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWid painter->drawEllipse(QPointF(-12, 0), 4, 4); // 左锚点 painter->drawEllipse(QPointF(12, 0), 4, 4); // 右锚点 } - if (option->state & QStyle::State_Selected) { - QPen pen(Qt::DashLine); - pen.setColor(Qt::blue); - pen.setWidth(2); - painter->setPen(pen); - painter->setBrush(Qt::NoBrush); - painter->drawRect(boundingRect()); - } + if (!registerId_.isEmpty()) { + painter->save(); + painter->setFont(QFont("Arial", 8)); + painter->setPen(Qt::black); + // 在元件底部居中绘制寄存器ID + painter->drawText(boundingRect().adjusted(0, 20, 0, 0), + Qt::AlignCenter, registerId_); + painter->restore(); + } + if (option->state & QStyle::State_Selected) { + QPen pen(Qt::DashLine); + pen.setColor(Qt::blue); + pen.setWidth(2); + painter->setPen(pen); + painter->setBrush(Qt::NoBrush); + painter->drawRect(boundingRect()); + } } diff --git a/comparator.cpp b/comparator.cpp index e1b5929..d12a36a 100644 --- a/comparator.cpp +++ b/comparator.cpp @@ -21,6 +21,15 @@ void Comparator::paint(QPainter *painter, const QStyleOptionGraphicsItem *option painter->drawEllipse(QPointF(-18, 0), 4, 4); painter->drawEllipse(QPointF(18, 0), 4, 4); } + if (!registerId_.isEmpty()) { + painter->save(); + painter->setFont(QFont("Arial", 8)); + painter->setPen(Qt::black); + // 在元件底部居中绘制寄存器ID + painter->drawText(boundingRect().adjusted(0, 20, 0, 0), + Qt::AlignCenter, registerId_); + painter->restore(); + } if (option->state & QStyle::State_Selected) { QPen pen(Qt::DashLine); pen.setColor(Qt::blue); diff --git a/contact.cpp b/contact.cpp index bf24eb4..68a821d 100644 --- a/contact.cpp +++ b/contact.cpp @@ -35,12 +35,22 @@ void Contact::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, Q painter->drawEllipse(QPointF(-18, 0), 4, 4); // 左 painter->drawEllipse(QPointF(18, 0), 4, 4); // 右 } - if (option->state & QStyle::State_Selected) { - QPen pen(Qt::DashLine); - pen.setColor(Qt::blue); - pen.setWidth(2); - painter->setPen(pen); - painter->setBrush(Qt::NoBrush); - painter->drawRect(boundingRect()); - } + + if (!registerId_.isEmpty()) { + painter->save(); + painter->setFont(QFont("Arial", 8)); + painter->setPen(Qt::black); + // 在元件底部居中绘制寄存器ID + painter->drawText(boundingRect().adjusted(0, 20, 0, 0), + Qt::AlignCenter, registerId_); + painter->restore(); + } + if (option->state & QStyle::State_Selected) { + QPen pen(Qt::DashLine); + pen.setColor(Qt::blue); + pen.setWidth(2); + painter->setPen(pen); + painter->setBrush(Qt::NoBrush); + painter->drawRect(boundingRect()); + } } diff --git a/editor.pro.user b/editor.pro.user index 8d07fb3..6933c03 100644 --- a/editor.pro.user +++ b/editor.pro.user @@ -1,6 +1,6 @@ - + EnvironmentId diff --git a/hmi.cpp b/hmi.cpp index ed7d7ff..58429f6 100644 --- a/hmi.cpp +++ b/hmi.cpp @@ -113,19 +113,13 @@ void HMI::applyProjectToScene(const Project& proj) Item* item = creatItem(d.type); if (!item) continue; item->setPos(d.x, d.y); + item->setRegisterId(d.registerId); connect(item, &Item::requestCopy, ui->graphicsView, &MyGraphicsView::onItemRequestCopy); connect(item, &Item::requestDelete, ui->graphicsView, &MyGraphicsView::onItemRequestDelete); + connect(item, &Item::requestBindRegister, ui->graphicsView, &MyGraphicsView::onItemRequestBindRegister); hmi_scene_->addItem(item); itemObjs.append(item); } -// for (const auto& c : proj.connections_) { -// if (c.from >= 0 && c.from < itemObjs.size() && c.to >= 0 && c.to < itemObjs.size()) { -// Connection* conn = new Connection( -// itemObjs[c.from], static_cast(c.fromType), -// itemObjs[c.to], static_cast(c.toType)); -// hmi_scene_->addItem(conn); -// } -// } } void HMI::extractSceneToProject(Project& proj) @@ -142,6 +136,7 @@ void HMI::extractSceneToProject(Project& proj) d.type = it->itemType(); d.x = it->pos().x(); d.y = it->pos().y(); + d.registerId = it->registerId(); proj.items_.append(d); } for (QGraphicsItem* gi : hmi_scene_->items()) { diff --git a/item.cpp b/item.cpp index c09a137..6f00f58 100644 --- a/item.cpp +++ b/item.cpp @@ -6,7 +6,7 @@ #include Item::Item(const QString &type, QGraphicsItem *parent) - : QGraphicsObject(parent), type_(type) + : QGraphicsObject(parent), type_(type), registerId_("") { setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable | @@ -15,7 +15,7 @@ Item::Item(const QString &type, QGraphicsItem *parent) QRectF Item::boundingRect() const { - return QRectF(-22, -15, 44, 30); + return QRectF(-22, -12, 44, 32); } QPointF Item::anchorPos(AnchorType type) const diff --git a/mygraphicsview.cpp b/mygraphicsview.cpp index 5d37642..64fa633 100644 --- a/mygraphicsview.cpp +++ b/mygraphicsview.cpp @@ -167,13 +167,13 @@ void MyGraphicsView::onItemRequestBindRegister(Item *item) { bool ok = false; QString reg = QInputDialog::getText(this, - "寄存器", "编号:", + "寄存器", + "编号:", QLineEdit::Normal, item->registerId(), &ok); if (ok && !reg.isEmpty()) { item->setRegisterId(reg); - // 可选:在图元上显示寄存器编号 item->update(); } } diff --git a/plc.cpp b/plc.cpp index 42d0ab8..10303f7 100644 --- a/plc.cpp +++ b/plc.cpp @@ -118,6 +118,7 @@ void PLC::applyProjectToScene(const Project& proj) Item* item = creatItem(d.type); if (!item) continue; item->setPos(d.x, d.y); + item->setRegisterId(d.registerId); connect(item, &Item::requestCopy, ui->graphicsView, &MyGraphicsView::onItemRequestCopy); connect(item, &Item::requestDelete, ui->graphicsView, &MyGraphicsView::onItemRequestDelete); connect(item, &Item::requestBindRegister, ui->graphicsView, &MyGraphicsView::onItemRequestBindRegister); @@ -150,6 +151,7 @@ void PLC::extractSceneToProject(Project& proj) d.type = it->itemType(); d.x = it->pos().x(); d.y = it->pos().y(); + d.registerId = it->registerId(); proj.items_.append(d); } // (3) 存连线数据 @@ -212,6 +214,7 @@ void PLC::btnInsertClicked() newItem->setPos(insertPos); connect(newItem, &Item::requestCopy, ui->graphicsView, &MyGraphicsView::onItemRequestCopy); connect(newItem, &Item::requestDelete, ui->graphicsView, &MyGraphicsView::onItemRequestDelete); + connect(newItem, &Item::requestBindRegister, ui->graphicsView, &MyGraphicsView::onItemRequestBindRegister); ui->graphicsView->scene()->addItem(newItem); // 5. 新建两条连线 diff --git a/project.cpp b/project.cpp index a6abe9c..8b0e0af 100644 --- a/project.cpp +++ b/project.cpp @@ -12,6 +12,7 @@ bool Project::saveToFile(const QString& filePath) const { obj["type"] = item.type; obj["x"] = item.x; obj["y"] = item.y; + obj["registerId"] = item.registerId; itemsArray.append(obj); } root["items"] = itemsArray; @@ -52,6 +53,7 @@ bool Project::loadFromFile(const QString& filePath) { d.type = obj["type"].toString(); d.x = obj["x"].toDouble(); d.y = obj["y"].toDouble(); + d.registerId = obj.contains("registerId") ? obj["registerId"].toString() : ""; items_.append(d); } QJsonArray connArray = root["connections"].toArray(); diff --git a/project.h b/project.h index 0969369..13a358d 100644 --- a/project.h +++ b/project.h @@ -11,6 +11,7 @@ public: struct ItemData { QString type; double x, y; + QString registerId = ""; }; struct ConnectionData { int from, to;