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

124 line
3.1 KiB

  1. #include "customgraphics.h"
  2. #include <QPainter>
  3. #include <QGraphicsSceneMouseEvent>
  4. #include <QMenu>
  5. #include <QGraphicsScene>
  6. #include <QColorDialog>
  7. // HmiComponent 基类实现
  8. HmiComponent::HmiComponent(QGraphicsItem *parent) : QGraphicsObject(parent)
  9. {
  10. setFlag(QGraphicsItem::ItemIsMovable, true);
  11. setFlag(QGraphicsItem::ItemIsSelectable, true);
  12. setAcceptHoverEvents(true);
  13. }
  14. void HmiComponent::setColor(const QColor& color) {
  15. if (m_color != color) {
  16. m_color = color;
  17. update();
  18. }
  19. }
  20. QColor HmiComponent::color() const {
  21. return m_color;
  22. }
  23. void HmiComponent::setComponentName(const QString& name) {
  24. m_componentName = name;
  25. }
  26. QString HmiComponent::componentName() const {
  27. return m_componentName;
  28. }
  29. void HmiComponent::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
  30. {
  31. Q_UNUSED(option);
  32. Q_UNUSED(widget);
  33. painter->setRenderHint(QPainter::Antialiasing);
  34. paintShape(painter);
  35. if (isSelected()) {
  36. painter->setBrush(Qt::NoBrush);
  37. painter->setPen(QPen(Qt::darkRed, 2, Qt::DashLine));
  38. painter->drawRect(boundingRect());
  39. }
  40. }
  41. void HmiComponent::mousePressEvent(QGraphicsSceneMouseEvent *event)
  42. {
  43. // 右键不触发选中信号,留给 contextMenuEvent 处理
  44. if (event->button() == Qt::LeftButton) {
  45. emit selected(this);
  46. }
  47. QGraphicsObject::mousePressEvent(event);
  48. }
  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. QAction *appearanceAction = menu.addAction("改变外观");
  59. // 使用 connect 将菜单动作的触发连接到信号的发射,复制和删除
  60. connect(copyAction, &QAction::triggered, this, [this]() {
  61. emit copyRequested(this);
  62. });
  63. connect(deleteAction, &QAction::triggered, this, [this]() {
  64. emit deleteRequested(this);
  65. });
  66. connect(appearanceAction, &QAction::triggered, this, &HmiComponent::changeAppearance);
  67. // 在鼠标光标位置显示菜单
  68. menu.exec(event->screenPos());
  69. }
  70. void HmiComponent::changeAppearance()
  71. {
  72. emit appearanceChangedRequested(this);
  73. }
  74. HmiButton::HmiButton(QGraphicsItem *parent) : HmiComponent(parent)
  75. {
  76. m_color = Qt::gray;
  77. m_componentName = "Button";
  78. }
  79. QRectF HmiButton::boundingRect() const
  80. {
  81. return QRectF(0, 0, 65, 30);
  82. }
  83. void HmiButton::paintShape(QPainter *painter)
  84. {
  85. painter->setPen(Qt::NoPen);
  86. painter->setBrush(m_color);
  87. painter->drawRect(boundingRect());
  88. }
  89. HmiIndicator::HmiIndicator(QGraphicsItem *parent) : HmiComponent(parent)
  90. {
  91. m_color = Qt::green;
  92. m_componentName = "Indicator";
  93. }
  94. QRectF HmiIndicator::boundingRect() const
  95. {
  96. return QRectF(0, 0, 30, 30);
  97. }
  98. void HmiIndicator::paintShape(QPainter *painter)
  99. {
  100. painter->setPen(Qt::NoPen);
  101. painter->setBrush(m_color);
  102. painter->drawEllipse(boundingRect());
  103. }