|
- #include "item.h"
- #include "connection.h"
- #include <QMenu>
- #include <QPainter>
- #include <QStyleOptionGraphicsItem>
- #include <QGraphicsSceneContextMenuEvent>
-
- Item::Item(const QString &type, QGraphicsItem *parent)
- : QGraphicsObject(parent), type_(type), registerId_("")
- {
- setFlags(QGraphicsItem::ItemIsMovable |
- QGraphicsItem::ItemIsSelectable |
- QGraphicsItem::ItemSendsGeometryChanges);
- }
-
- QRectF Item::boundingRect() const
- {
- return QRectF(-22, -12, 44, 32);
- }
-
- QPointF Item::anchorPos(AnchorType type) const
- {
- // 对于"按钮"和"指示灯"类型,返回无效坐标
- if (type_ == "按钮" || type_ == "指示灯") {
- return QPointF(std::numeric_limits<qreal>::quiet_NaN(),
- std::numeric_limits<qreal>::quiet_NaN());
- }
-
- // 其他类型正常返回连接点位置
- return mapToScene(type == Left ? QPointF(-18, 0) : QPointF(18, 0));
- }
-
- void Item::addConnection(Connection* conn)
- {
- // 对于"按钮"和"指示灯"类型,不允许添加连接
- if (type_ == "按钮" || type_ == "指示灯")
- {
- return;
- }
- if (!connections_.contains(conn))
- {
- connections_.append(conn);
- }
- }
-
- void Item::removeConnection(Connection* conn)
- {
- connections_.removeAll(conn);
- }
-
- QList<Connection *> Item::connections()
- {
- return connections_;
- }
-
- QString Item::itemType()
- {
- return type_;
- }
-
- bool Item::setRegisterId(const QString &id)
- {
- if(registerId_.isEmpty())
- {
- registerId_ = id;
- return true;
- }
- return false;
- }
-
- void Item::setRegisterValue(const QString ®isterId, quint16 value)
- {
- if (registerId_ == registerId)
- {
- registerValue_ = value;
- update(); // 触发重绘
- }
- }
-
- QStringList Item::resetRegister()
- {
- QStringList unboundRegs;
- if (!registerId_.isEmpty()) {
- unboundRegs << registerId_;
- registerId_.clear();
- registerValue_ = 0;
- update();
- }
- return unboundRegs;
- }
-
- void Item::MenuActions(QMenu *menu)
- {
- menu->addAction("复制");
- menu->addAction("删除");
- menu->addAction("对象");
- menu->addAction("重置");
- }
-
- void Item::addMenuActions(QMenu *menu)
- {
- Q_UNUSED(menu);
- }
-
- void Item::handleMenuAction(QAction *action)
- {
- if (action->text() == "复制") {
- emit requestCopy(this);
- }
- if (action->text() == "删除") {
- emit requestDelete(this);
- }
- if (action->text() == "对象"){
- emit requestBindRegister(this);
- }
- if (action->text() == "重置"){
- emit requestReset(this);
- }
- }
-
- QVariant Item::itemChange(GraphicsItemChange change, const QVariant &value)
- {
- if (change == QGraphicsItem::ItemPositionChange) {
- for (Connection* conn : connections_)
- conn->updatePosition();
- }
- return QGraphicsObject::itemChange(change, value);
- }
-
- void Item::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
- {
- QMenu menu;
-
- // 创建菜单
- MenuActions(&menu);
- addMenuActions(&menu);
-
- // 执行菜单
- QAction *selected = menu.exec(event->screenPos());
-
- // 处理菜单动作
- if (selected) {
- handleMenuAction(selected);
- }
- }
|