综合平台编辑器
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.

121 regels
3.0 KiB

  1. #include "customgraphics.h"
  2. #include <QPainter>
  3. #include <QGraphicsSceneMouseEvent>
  4. #include <QMenu>
  5. #include <QGraphicsScene>
  6. // HmiComponent 基类实现
  7. HmiComponent::HmiComponent(QGraphicsItem *parent) : QGraphicsObject(parent)
  8. {
  9. setFlag(QGraphicsItem::ItemIsMovable, true);
  10. setFlag(QGraphicsItem::ItemIsSelectable, true);
  11. setAcceptHoverEvents(true);
  12. }
  13. void HmiComponent::setColor(const QColor& color) {
  14. if (m_color != color) {
  15. m_color = color;
  16. update();
  17. }
  18. }
  19. QColor HmiComponent::color() const {
  20. return m_color;
  21. }
  22. void HmiComponent::setComponentName(const QString& name) {
  23. m_componentName = name;
  24. }
  25. QString HmiComponent::componentName() const {
  26. return m_componentName;
  27. }
  28. void HmiComponent::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
  29. {
  30. Q_UNUSED(option);
  31. Q_UNUSED(widget);
  32. painter->setRenderHint(QPainter::Antialiasing);
  33. paintShape(painter);
  34. if (isSelected()) {
  35. painter->setBrush(Qt::NoBrush);
  36. painter->setPen(QPen(Qt::darkRed, 2, Qt::DashLine));
  37. painter->drawRect(boundingRect());
  38. }
  39. }
  40. void HmiComponent::mousePressEvent(QGraphicsSceneMouseEvent *event)
  41. {
  42. // 右键不触发选中信号,留给 contextMenuEvent 处理
  43. if (event->button() == Qt::LeftButton) {
  44. emit selected(this);
  45. }
  46. QGraphicsObject::mousePressEvent(event);
  47. }
  48. // 实现 contextMenuEvent
  49. void HmiComponent::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
  50. {
  51. // 右键点击时,先清空场景中的其他选中项,然后选中当前项
  52. scene()->clearSelection();
  53. setSelected(true);
  54. emit selected(this); // 同样可以发出选中信号,以便更新属性面板等
  55. QMenu menu;
  56. QAction *copyAction = menu.addAction("复制");
  57. QAction *deleteAction = menu.addAction("删除");
  58. // 使用 connect 将菜单动作的触发连接到信号的发射,“复制”和“删除”
  59. connect(copyAction, &QAction::triggered, this, [this]() {
  60. emit copyRequested(this);
  61. });
  62. connect(deleteAction, &QAction::triggered, this, [this]() {
  63. emit deleteRequested(this);
  64. });
  65. // 在鼠标光标位置显示菜单
  66. menu.exec(event->screenPos());
  67. }
  68. // HmiButton 类实现
  69. HmiButton::HmiButton(QGraphicsItem *parent) : HmiComponent(parent)
  70. {
  71. m_color = Qt::gray;
  72. m_componentName = "Button";
  73. }
  74. QRectF HmiButton::boundingRect() const
  75. {
  76. return QRectF(0, 0, 65, 30);
  77. }
  78. void HmiButton::paintShape(QPainter *painter)
  79. {
  80. painter->setPen(Qt::NoPen);
  81. painter->setBrush(m_color);
  82. painter->drawRect(boundingRect());
  83. }
  84. // HmiIndicator 类实现
  85. HmiIndicator::HmiIndicator(QGraphicsItem *parent) : HmiComponent(parent)
  86. {
  87. m_color = Qt::green;
  88. m_componentName = "Indicator";
  89. }
  90. QRectF HmiIndicator::boundingRect() const
  91. {
  92. return QRectF(0, 0, 30, 30);
  93. }
  94. void HmiIndicator::paintShape(QPainter *painter)
  95. {
  96. painter->setPen(Qt::NoPen);
  97. painter->setBrush(m_color);
  98. painter->drawEllipse(boundingRect());
  99. }