Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

121 строка
3.2 KiB

  1. #include "comparator.h"
  2. #include <QMenu>
  3. #include <QPainter>
  4. #include <QStyleOptionGraphicsItem>
  5. Comparator::Comparator(const QString &type) : Item(type)
  6. {
  7. }
  8. QRectF Comparator::boundingRect() const
  9. {
  10. return QRectF(-22, -20, 44, 40);
  11. }
  12. void Comparator::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *)
  13. {
  14. painter->setRenderHint(QPainter::Antialiasing);
  15. if (state()) {
  16. painter->setBrush(Qt::green); // 激活状态
  17. } else {
  18. painter->setBrush(Qt::white); // 未激活状态
  19. }
  20. if (type_ == "比较") {
  21. painter->drawRect(QRectF(-12, -8, 24, 16));
  22. painter->setFont(QFont("Arial", 8));
  23. painter->drawText(QRectF(-10, -8, 20, 16), Qt::AlignCenter, compare_);
  24. // 锚点
  25. painter->setBrush(Qt::darkGray);
  26. painter->setPen(Qt::NoPen);
  27. painter->drawEllipse(QPointF(-18, 0), 4, 4);
  28. painter->drawEllipse(QPointF(18, 0), 4, 4);
  29. }
  30. painter->save();
  31. painter->setFont(QFont("Arial", 8));
  32. painter->setPen(Qt::black);
  33. if (!registerId_.isEmpty()) {
  34. QString text = QString("%1: %2").arg(registerId_).arg(registerValue_);
  35. painter->drawText(QRectF(-20, -25, 40, 20), Qt::AlignCenter, text);
  36. }
  37. // 在右上角显示第二个寄存器
  38. if (!registerId2_.isEmpty()) {
  39. QString text = QString("%1: %2").arg(registerId2_).arg(registerValue2_);
  40. painter->drawText(QRectF(-20, 5, 40, 20), Qt::AlignCenter, text);
  41. }
  42. painter->restore();
  43. if (option->state & QStyle::State_Selected) {
  44. QPen pen(Qt::DashLine);
  45. pen.setColor(Qt::blue);
  46. pen.setWidth(2);
  47. painter->setPen(pen);
  48. painter->setBrush(Qt::NoBrush);
  49. painter->drawRect(boundingRect());
  50. }
  51. }
  52. void Comparator::setRegisterId(const QString &id)
  53. {
  54. if (registerId_.isEmpty())
  55. {
  56. registerId_ = id;
  57. }
  58. else if(registerId2_.isEmpty())
  59. {
  60. registerId2_ = id;
  61. }
  62. }
  63. void Comparator::setRegisterValue(const QString &registerId, quint16 value)
  64. {
  65. if (registerId_ == registerId)
  66. {
  67. registerValue_ = value;
  68. update(); // 触发重绘
  69. }
  70. if (registerId2_ == registerId)
  71. {
  72. registerValue2_ = value;
  73. update(); // 触发重绘
  74. }
  75. }
  76. void Comparator::addMenuActions(QMenu *menu)
  77. {
  78. menu->addAction("比较");
  79. }
  80. void Comparator::handleMenuAction(QAction *action)
  81. {
  82. if (action->text() == "复制") {
  83. emit requestCopy(this);
  84. }
  85. if (action->text() == "删除") {
  86. emit requestDelete(this);
  87. }
  88. if (action->text() == "对象"){
  89. emit requestBindRegister(this);
  90. }
  91. if (action->text() == "比较"){
  92. emit requestCompare(this);
  93. }
  94. }
  95. void Comparator::setCompare(QString compare)
  96. {
  97. compare_ = compare;
  98. update();
  99. }
  100. bool Comparator::state() const
  101. {
  102. if (compare_ == ">") return registerValue_ > registerValue2_;
  103. if (compare_ == "<") return registerValue_ < registerValue2_;
  104. if (compare_ == "=") return registerValue_ == registerValue2_;
  105. if (compare_ == ">=") return registerValue_ >= registerValue2_;
  106. if (compare_ == "<") return registerValue_ <= registerValue2_;
  107. return false;
  108. }