综合平台编辑器
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

118 righe
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. m_color = Qt::gray;
  71. m_componentName = "Button";
  72. }
  73. QRectF HmiButton::boundingRect() const {
  74. return QRectF(0, 0, 80, 30);
  75. }
  76. void HmiButton::paintShape(QPainter *painter) {
  77. painter->setPen(Qt::NoPen);
  78. painter->setBrush(m_color);
  79. painter->drawRect(boundingRect());
  80. }
  81. // HmiIndicator 类实现
  82. HmiIndicator::HmiIndicator(QGraphicsItem *parent) : HmiComponent(parent) {
  83. m_color = Qt::green;
  84. m_componentName = "Indicator";
  85. }
  86. QRectF HmiIndicator::boundingRect() const {
  87. return QRectF(0, 0, 30, 30);
  88. }
  89. void HmiIndicator::paintShape(QPainter *painter) {
  90. painter->setPen(Qt::NoPen);
  91. painter->setBrush(m_color);
  92. painter->drawEllipse(boundingRect());
  93. }