You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

146 lines
3.1 KiB

  1. #include "item.h"
  2. #include "connection.h"
  3. #include <QMenu>
  4. #include <QPainter>
  5. #include <QStyleOptionGraphicsItem>
  6. #include <QGraphicsSceneContextMenuEvent>
  7. Item::Item(const QString &type, QGraphicsItem *parent)
  8. : QGraphicsObject(parent), type_(type), registerId_("")
  9. {
  10. setFlags(QGraphicsItem::ItemIsMovable |
  11. QGraphicsItem::ItemIsSelectable |
  12. QGraphicsItem::ItemSendsGeometryChanges);
  13. }
  14. QRectF Item::boundingRect() const
  15. {
  16. return QRectF(-22, -12, 44, 32);
  17. }
  18. QPointF Item::anchorPos(AnchorType type) const
  19. {
  20. // 对于"按钮"和"指示灯"类型,返回无效坐标
  21. if (type_ == "按钮" || type_ == "指示灯") {
  22. return QPointF(std::numeric_limits<qreal>::quiet_NaN(),
  23. std::numeric_limits<qreal>::quiet_NaN());
  24. }
  25. // 其他类型正常返回连接点位置
  26. return mapToScene(type == Left ? QPointF(-18, 0) : QPointF(18, 0));
  27. }
  28. void Item::addConnection(Connection* conn)
  29. {
  30. // 对于"按钮"和"指示灯"类型,不允许添加连接
  31. if (type_ == "按钮" || type_ == "指示灯")
  32. {
  33. return;
  34. }
  35. if (!connections_.contains(conn))
  36. {
  37. connections_.append(conn);
  38. }
  39. }
  40. void Item::removeConnection(Connection* conn)
  41. {
  42. connections_.removeAll(conn);
  43. }
  44. QList<Connection *> Item::connections()
  45. {
  46. return connections_;
  47. }
  48. QString Item::itemType()
  49. {
  50. return type_;
  51. }
  52. bool Item::setRegisterId(const QString &id)
  53. {
  54. if(registerId_.isEmpty())
  55. {
  56. registerId_ = id;
  57. return true;
  58. }
  59. return false;
  60. }
  61. void Item::setRegisterValue(const QString &registerId, quint16 value)
  62. {
  63. if (registerId_ == registerId)
  64. {
  65. registerValue_ = value;
  66. update(); // 触发重绘
  67. }
  68. }
  69. QStringList Item::resetRegister()
  70. {
  71. QStringList unboundRegs;
  72. if (!registerId_.isEmpty()) {
  73. unboundRegs << registerId_;
  74. registerId_.clear();
  75. registerValue_ = 0;
  76. update();
  77. }
  78. return unboundRegs;
  79. }
  80. void Item::MenuActions(QMenu *menu)
  81. {
  82. menu->addAction("复制");
  83. menu->addAction("删除");
  84. menu->addAction("对象");
  85. menu->addAction("重置");
  86. }
  87. void Item::addMenuActions(QMenu *menu)
  88. {
  89. Q_UNUSED(menu);
  90. }
  91. void Item::handleMenuAction(QAction *action)
  92. {
  93. if (action->text() == "复制") {
  94. emit requestCopy(this);
  95. }
  96. if (action->text() == "删除") {
  97. emit requestDelete(this);
  98. }
  99. if (action->text() == "对象"){
  100. emit requestBindRegister(this);
  101. }
  102. if (action->text() == "重置"){
  103. emit requestReset(this);
  104. }
  105. }
  106. QVariant Item::itemChange(GraphicsItemChange change, const QVariant &value)
  107. {
  108. if (change == QGraphicsItem::ItemPositionChange) {
  109. for (Connection* conn : connections_)
  110. conn->updatePosition();
  111. }
  112. return QGraphicsObject::itemChange(change, value);
  113. }
  114. void Item::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
  115. {
  116. QMenu menu;
  117. // 创建菜单
  118. MenuActions(&menu);
  119. addMenuActions(&menu);
  120. // 执行菜单
  121. QAction *selected = menu.exec(event->screenPos());
  122. // 处理菜单动作
  123. if (selected) {
  124. handleMenuAction(selected);
  125. }
  126. }