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.

162 Zeilen
4.1 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;
  35. if (registerId_.left(1) == "K")
  36. {
  37. text = QString("%1").arg(registerId_);
  38. } else {
  39. text = QString("%1: %2").arg(registerId_).arg(registerValue_);
  40. }
  41. painter->drawText(QRectF(-20, -25, 40, 20), Qt::AlignCenter, text);
  42. }
  43. // 显示第二个寄存器
  44. if (!registerId2_.isEmpty()) {
  45. QString text;
  46. if (registerId2_.left(1) == "K")
  47. {
  48. text = QString("%1").arg(registerId2_);
  49. } else {
  50. text = QString("%1: %2").arg(registerId2_).arg(registerValue2_);
  51. }
  52. painter->drawText(QRectF(-20, 5, 40, 20), Qt::AlignCenter, text);
  53. }
  54. painter->restore();
  55. if (option->state & QStyle::State_Selected) {
  56. QPen pen(Qt::DashLine);
  57. pen.setColor(Qt::blue);
  58. pen.setWidth(2);
  59. painter->setPen(pen);
  60. painter->setBrush(Qt::NoBrush);
  61. painter->drawRect(boundingRect());
  62. }
  63. }
  64. bool Comparator::setRegisterId(const QString &id)
  65. {
  66. if (registerId_.isEmpty())
  67. {
  68. registerId_ = id;
  69. return true;
  70. }
  71. else if(registerId2_.isEmpty())
  72. {
  73. registerId2_ = id;
  74. return true;
  75. }
  76. return false;
  77. }
  78. void Comparator::setRegisterValue(const QString &registerId, quint16 value)
  79. {
  80. if (registerId_ == registerId)
  81. {
  82. registerValue_ = value;
  83. update(); // 触发重绘
  84. }
  85. if (registerId2_ == registerId)
  86. {
  87. registerValue2_ = value;
  88. update(); // 触发重绘
  89. }
  90. }
  91. void Comparator::addMenuActions(QMenu *menu)
  92. {
  93. menu->addAction("比较");
  94. }
  95. void Comparator::handleMenuAction(QAction *action)
  96. {
  97. if (action->text() == "复制") {
  98. emit requestCopy(this);
  99. }
  100. if (action->text() == "删除") {
  101. emit requestDelete(this);
  102. }
  103. if (action->text() == "对象"){
  104. emit requestBindRegister(this);
  105. }
  106. if (action->text() == "比较"){
  107. emit requestCompare(this);
  108. }
  109. if (action->text() == "重置") {
  110. emit requestReset(this);
  111. }
  112. }
  113. void Comparator::setCompare(QString compare)
  114. {
  115. compare_ = compare;
  116. update();
  117. }
  118. bool Comparator::state() const
  119. {
  120. if (compare_ == ">") return registerValue_ > registerValue2_;
  121. if (compare_ == "<") return registerValue_ < registerValue2_;
  122. if (compare_ == "=") return registerValue_ == registerValue2_;
  123. if (compare_ == ">=") return registerValue_ >= registerValue2_;
  124. if (compare_ == "<=") return registerValue_ <= registerValue2_;
  125. return false;
  126. }
  127. QStringList Comparator::resetRegister()
  128. {
  129. QStringList unboundRegs;
  130. // 重置第一个寄存器
  131. if (!registerId_.isEmpty()) {
  132. unboundRegs << registerId_;
  133. registerId_.clear();
  134. }
  135. // 重置第二个寄存器
  136. if (!registerId2_.isEmpty()) {
  137. unboundRegs << registerId2_;
  138. registerId2_.clear();
  139. }
  140. // 清除寄存器值
  141. registerValue_ = 0;
  142. registerValue2_ = 0;
  143. update(); // 刷新显示
  144. return unboundRegs;
  145. }