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

116 line
2.9 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. void HmiComponent::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
  49. {
  50. // 右键点击时,先清空场景中的其他选中项,然后选中当前项
  51. scene()->clearSelection();
  52. setSelected(true);
  53. emit selected(this); // 同样可以发出选中信号,以便更新属性面板等
  54. QMenu menu;
  55. QAction *copyAction = menu.addAction("复制");
  56. QAction *deleteAction = menu.addAction("删除");
  57. // 使用 connect 将菜单动作的触发连接到信号的发射,“复制”和“删除”
  58. connect(copyAction, &QAction::triggered, this, [this]() {
  59. emit copyRequested(this);
  60. });
  61. connect(deleteAction, &QAction::triggered, this, [this]() {
  62. emit deleteRequested(this);
  63. });
  64. // 在鼠标光标位置显示菜单
  65. menu.exec(event->screenPos());
  66. }
  67. HmiButton::HmiButton(QGraphicsItem *parent) : HmiComponent(parent)
  68. {
  69. m_color = Qt::gray;
  70. m_componentName = "Button";
  71. }
  72. QRectF HmiButton::boundingRect() const
  73. {
  74. return QRectF(0, 0, 65, 30);
  75. }
  76. void HmiButton::paintShape(QPainter *painter)
  77. {
  78. painter->setPen(Qt::NoPen);
  79. painter->setBrush(m_color);
  80. painter->drawRect(boundingRect());
  81. }
  82. HmiIndicator::HmiIndicator(QGraphicsItem *parent) : HmiComponent(parent)
  83. {
  84. m_color = Qt::green;
  85. m_componentName = "Indicator";
  86. }
  87. QRectF HmiIndicator::boundingRect() const
  88. {
  89. return QRectF(0, 0, 30, 30);
  90. }
  91. void HmiIndicator::paintShape(QPainter *painter)
  92. {
  93. painter->setPen(Qt::NoPen);
  94. painter->setBrush(m_color);
  95. painter->drawEllipse(boundingRect());
  96. }