Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

100 Zeilen
2.8 KiB

  1. #include "contact.h"
  2. #include <QPainter>
  3. #include <QStyleOptionGraphicsItem>
  4. #include <QMenu>
  5. Contact::Contact(const QString &type) : Item(type)
  6. {
  7. }
  8. void Contact::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *)
  9. {
  10. painter->setRenderHint(QPainter::Antialiasing);
  11. if (type_ == "常开") {
  12. if (state()) {
  13. painter->setPen(QPen(Qt::green, 2)); // 激活状态
  14. } else {
  15. painter->setPen(QPen(Qt::black, 1)); // 未激活状态
  16. }
  17. painter->drawLine(-12, 0, -4, 0);
  18. painter->drawLine(-4, -8, -4, 8);
  19. painter->drawLine(4, -8, 4, 8);
  20. painter->drawLine(4, 0, 12, 0);
  21. // 锚点
  22. painter->setBrush(Qt::darkGray);
  23. painter->setPen(Qt::NoPen);
  24. painter->drawEllipse(QPointF(-18, 0), 4, 4); // 左
  25. painter->drawEllipse(QPointF(18, 0), 4, 4); // 右
  26. }
  27. else if (type_ == "常闭") {
  28. if (!state()) {
  29. painter->setPen(QPen(Qt::green, 2)); // 激活状态
  30. } else {
  31. painter->setPen(QPen(Qt::black, 1)); // 未激活状态
  32. }
  33. painter->drawLine(-15, -10, 15, 10); // 对角线
  34. painter->drawLine(-12, 0, -4, 0);
  35. painter->drawLine(-4, -8, -4, 8);
  36. painter->drawLine(4, -8, 4, 8);
  37. painter->drawLine(4, 0, 12, 0);
  38. // 锚点
  39. painter->setBrush(Qt::darkGray);
  40. painter->setPen(Qt::NoPen);
  41. painter->drawEllipse(QPointF(-18, 0), 4, 4); // 左
  42. painter->drawEllipse(QPointF(18, 0), 4, 4); // 右
  43. }
  44. if (!registerId_.isEmpty()) {
  45. painter->save();
  46. painter->setFont(QFont("Arial", 8));
  47. painter->setPen(Qt::black);
  48. // 在元件底部居中绘制寄存器ID
  49. QString text = QString("%1: %2").arg(registerId()).arg(registerValue_);
  50. painter->drawText(boundingRect(), Qt::AlignBottom | Qt::AlignHCenter, text);
  51. painter->restore();
  52. }
  53. if (option->state & QStyle::State_Selected) {
  54. QPen pen(Qt::DashLine);
  55. pen.setColor(Qt::blue);
  56. pen.setWidth(2);
  57. painter->setPen(pen);
  58. painter->setBrush(Qt::NoBrush);
  59. painter->drawRect(boundingRect());
  60. }
  61. }
  62. bool Contact::state() const
  63. {
  64. return registerValue_ > 0;
  65. }
  66. void Contact::addMenuActions(QMenu *menu)
  67. {
  68. menu->addAction("置ON");
  69. menu->addAction("置OFF");
  70. }
  71. void Contact::handleMenuAction(QAction *action)
  72. {
  73. if (action->text() == "复制") {
  74. emit requestCopy(this);
  75. }
  76. if (action->text() == "删除") {
  77. emit requestDelete(this);
  78. }
  79. if (action->text() == "对象"){
  80. emit requestBindRegister(this);
  81. }
  82. if (action->text() == "重置"){
  83. emit requestReset(this);
  84. }
  85. if (action->text() == "置ON"){
  86. emit requestSetON(this, true);
  87. }
  88. if (action->text() == "置OFF"){
  89. emit requestSetON(this, false);
  90. }
  91. }