Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

125 linhas
2.7 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. void Item::setRegisterId(const QString &id)
  53. {
  54. registerId_ = id;
  55. }
  56. void Item::setRegisterValue(const QString &registerId, quint16 value)
  57. {
  58. if (registerId_ == registerId)
  59. {
  60. registerValue_ = value;
  61. update(); // 触发重绘
  62. }
  63. }
  64. void Item::MenuActions(QMenu *menu)
  65. {
  66. menu->addAction("复制");
  67. menu->addAction("删除");
  68. menu->addAction("对象");
  69. }
  70. void Item::addMenuActions(QMenu *menu)
  71. {
  72. Q_UNUSED(menu);
  73. }
  74. void Item::handleMenuAction(QAction *action)
  75. {
  76. if (action->text() == "复制") {
  77. emit requestCopy(this);
  78. }
  79. if (action->text() == "删除") {
  80. emit requestDelete(this);
  81. }
  82. if (action->text() == "对象"){
  83. emit requestBindRegister(this);
  84. }
  85. }
  86. QVariant Item::itemChange(GraphicsItemChange change, const QVariant &value)
  87. {
  88. if (change == QGraphicsItem::ItemPositionChange) {
  89. for (Connection* conn : connections_)
  90. conn->updatePosition();
  91. }
  92. return QGraphicsObject::itemChange(change, value);
  93. }
  94. void Item::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
  95. {
  96. QMenu menu;
  97. // 创建菜单
  98. MenuActions(&menu);
  99. addMenuActions(&menu);
  100. // 执行菜单
  101. QAction *selected = menu.exec(event->screenPos());
  102. // 处理菜单动作
  103. if (selected) {
  104. handleMenuAction(selected);
  105. }
  106. }