@@ -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); // 左锚点 | ||||
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()); | |||||
} | |||||
} | } |
@@ -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); | ||||
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) { | if (option->state & QStyle::State_Selected) { | ||||
QPen pen(Qt::DashLine); | QPen pen(Qt::DashLine); | ||||
pen.setColor(Qt::blue); | pen.setColor(Qt::blue); | ||||
@@ -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); // 左 | ||||
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()); | |||||
} | |||||
} | } |
@@ -1,6 +1,6 @@ | |||||
<?xml version="1.0" encoding="UTF-8"?> | <?xml version="1.0" encoding="UTF-8"?> | ||||
<!DOCTYPE QtCreatorProject> | <!DOCTYPE QtCreatorProject> | ||||
<!-- Written by QtCreator 4.11.1, 2025-08-07T18:51:27. --> | |||||
<!-- Written by QtCreator 4.11.1, 2025-08-07T20:13:11. --> | |||||
<qtcreator> | <qtcreator> | ||||
<data> | <data> | ||||
<variable>EnvironmentId</variable> | <variable>EnvironmentId</variable> | ||||
@@ -113,19 +113,13 @@ void HMI::applyProjectToScene(const Project& proj) | |||||
Item* item = creatItem(d.type); | Item* item = creatItem(d.type); | ||||
if (!item) continue; | if (!item) continue; | ||||
item->setPos(d.x, d.y); | item->setPos(d.x, d.y); | ||||
item->setRegisterId(d.registerId); | |||||
connect(item, &Item::requestCopy, ui->graphicsView, &MyGraphicsView::onItemRequestCopy); | connect(item, &Item::requestCopy, ui->graphicsView, &MyGraphicsView::onItemRequestCopy); | ||||
connect(item, &Item::requestDelete, ui->graphicsView, &MyGraphicsView::onItemRequestDelete); | connect(item, &Item::requestDelete, ui->graphicsView, &MyGraphicsView::onItemRequestDelete); | ||||
connect(item, &Item::requestBindRegister, ui->graphicsView, &MyGraphicsView::onItemRequestBindRegister); | |||||
hmi_scene_->addItem(item); | hmi_scene_->addItem(item); | ||||
itemObjs.append(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<Item::AnchorType>(c.fromType), | |||||
// itemObjs[c.to], static_cast<Item::AnchorType>(c.toType)); | |||||
// hmi_scene_->addItem(conn); | |||||
// } | |||||
// } | |||||
} | } | ||||
void HMI::extractSceneToProject(Project& proj) | void HMI::extractSceneToProject(Project& proj) | ||||
@@ -142,6 +136,7 @@ void HMI::extractSceneToProject(Project& proj) | |||||
d.type = it->itemType(); | d.type = it->itemType(); | ||||
d.x = it->pos().x(); | d.x = it->pos().x(); | ||||
d.y = it->pos().y(); | d.y = it->pos().y(); | ||||
d.registerId = it->registerId(); | |||||
proj.items_.append(d); | proj.items_.append(d); | ||||
} | } | ||||
for (QGraphicsItem* gi : hmi_scene_->items()) { | for (QGraphicsItem* gi : hmi_scene_->items()) { | ||||
@@ -6,7 +6,7 @@ | |||||
#include <QGraphicsSceneContextMenuEvent> | #include <QGraphicsSceneContextMenuEvent> | ||||
Item::Item(const QString &type, QGraphicsItem *parent) | Item::Item(const QString &type, QGraphicsItem *parent) | ||||
: QGraphicsObject(parent), type_(type) | |||||
: QGraphicsObject(parent), type_(type), registerId_("") | |||||
{ | { | ||||
setFlags(QGraphicsItem::ItemIsMovable | | setFlags(QGraphicsItem::ItemIsMovable | | ||||
QGraphicsItem::ItemIsSelectable | | QGraphicsItem::ItemIsSelectable | | ||||
@@ -15,7 +15,7 @@ Item::Item(const QString &type, QGraphicsItem *parent) | |||||
QRectF Item::boundingRect() const | QRectF Item::boundingRect() const | ||||
{ | { | ||||
return QRectF(-22, -15, 44, 30); | |||||
return QRectF(-22, -12, 44, 32); | |||||
} | } | ||||
QPointF Item::anchorPos(AnchorType type) const | QPointF Item::anchorPos(AnchorType type) const | ||||
@@ -167,13 +167,13 @@ void MyGraphicsView::onItemRequestBindRegister(Item *item) | |||||
{ | { | ||||
bool ok = false; | bool ok = false; | ||||
QString reg = QInputDialog::getText(this, | QString reg = QInputDialog::getText(this, | ||||
"寄存器", "编号:", | |||||
"寄存器", | |||||
"编号:", | |||||
QLineEdit::Normal, | QLineEdit::Normal, | ||||
item->registerId(), | item->registerId(), | ||||
&ok); | &ok); | ||||
if (ok && !reg.isEmpty()) { | if (ok && !reg.isEmpty()) { | ||||
item->setRegisterId(reg); | item->setRegisterId(reg); | ||||
// 可选:在图元上显示寄存器编号 | |||||
item->update(); | item->update(); | ||||
} | } | ||||
} | } | ||||
@@ -118,6 +118,7 @@ void PLC::applyProjectToScene(const Project& proj) | |||||
Item* item = creatItem(d.type); | Item* item = creatItem(d.type); | ||||
if (!item) continue; | if (!item) continue; | ||||
item->setPos(d.x, d.y); | item->setPos(d.x, d.y); | ||||
item->setRegisterId(d.registerId); | |||||
connect(item, &Item::requestCopy, ui->graphicsView, &MyGraphicsView::onItemRequestCopy); | connect(item, &Item::requestCopy, ui->graphicsView, &MyGraphicsView::onItemRequestCopy); | ||||
connect(item, &Item::requestDelete, ui->graphicsView, &MyGraphicsView::onItemRequestDelete); | connect(item, &Item::requestDelete, ui->graphicsView, &MyGraphicsView::onItemRequestDelete); | ||||
connect(item, &Item::requestBindRegister, ui->graphicsView, &MyGraphicsView::onItemRequestBindRegister); | connect(item, &Item::requestBindRegister, ui->graphicsView, &MyGraphicsView::onItemRequestBindRegister); | ||||
@@ -150,6 +151,7 @@ void PLC::extractSceneToProject(Project& proj) | |||||
d.type = it->itemType(); | d.type = it->itemType(); | ||||
d.x = it->pos().x(); | d.x = it->pos().x(); | ||||
d.y = it->pos().y(); | d.y = it->pos().y(); | ||||
d.registerId = it->registerId(); | |||||
proj.items_.append(d); | proj.items_.append(d); | ||||
} | } | ||||
// (3) 存连线数据 | // (3) 存连线数据 | ||||
@@ -212,6 +214,7 @@ void PLC::btnInsertClicked() | |||||
newItem->setPos(insertPos); | newItem->setPos(insertPos); | ||||
connect(newItem, &Item::requestCopy, ui->graphicsView, &MyGraphicsView::onItemRequestCopy); | connect(newItem, &Item::requestCopy, ui->graphicsView, &MyGraphicsView::onItemRequestCopy); | ||||
connect(newItem, &Item::requestDelete, ui->graphicsView, &MyGraphicsView::onItemRequestDelete); | connect(newItem, &Item::requestDelete, ui->graphicsView, &MyGraphicsView::onItemRequestDelete); | ||||
connect(newItem, &Item::requestBindRegister, ui->graphicsView, &MyGraphicsView::onItemRequestBindRegister); | |||||
ui->graphicsView->scene()->addItem(newItem); | ui->graphicsView->scene()->addItem(newItem); | ||||
// 5. 新建两条连线 | // 5. 新建两条连线 | ||||
@@ -12,6 +12,7 @@ bool Project::saveToFile(const QString& filePath) const { | |||||
obj["type"] = item.type; | obj["type"] = item.type; | ||||
obj["x"] = item.x; | obj["x"] = item.x; | ||||
obj["y"] = item.y; | obj["y"] = item.y; | ||||
obj["registerId"] = item.registerId; | |||||
itemsArray.append(obj); | itemsArray.append(obj); | ||||
} | } | ||||
root["items"] = itemsArray; | root["items"] = itemsArray; | ||||
@@ -52,6 +53,7 @@ bool Project::loadFromFile(const QString& filePath) { | |||||
d.type = obj["type"].toString(); | d.type = obj["type"].toString(); | ||||
d.x = obj["x"].toDouble(); | d.x = obj["x"].toDouble(); | ||||
d.y = obj["y"].toDouble(); | d.y = obj["y"].toDouble(); | ||||
d.registerId = obj.contains("registerId") ? obj["registerId"].toString() : ""; | |||||
items_.append(d); | items_.append(d); | ||||
} | } | ||||
QJsonArray connArray = root["connections"].toArray(); | QJsonArray connArray = root["connections"].toArray(); | ||||
@@ -11,6 +11,7 @@ public: | |||||
struct ItemData { | struct ItemData { | ||||
QString type; | QString type; | ||||
double x, y; | double x, y; | ||||
QString registerId = ""; | |||||
}; | }; | ||||
struct ConnectionData { | struct ConnectionData { | ||||
int from, to; | int from, to; | ||||