Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

78 rader
2.1 KiB

  1. #include "comparator.h"
  2. #include <QPainter>
  3. #include <QStyleOptionGraphicsItem>
  4. Comparator::Comparator(const QString &type) : Item(type)
  5. {
  6. }
  7. QRectF Comparator::boundingRect() const
  8. {
  9. return QRectF(-22, -20, 44, 40);
  10. }
  11. void Comparator::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *)
  12. {
  13. painter->setRenderHint(QPainter::Antialiasing);
  14. if (type_ == "比较") {
  15. painter->drawRect(QRectF(-12, -8, 24, 16));
  16. painter->setFont(QFont("Arial", 8));
  17. painter->drawText(QRectF(-10, -8, 20, 16), Qt::AlignCenter, "CP");
  18. // 锚点
  19. painter->setBrush(Qt::darkGray);
  20. painter->setPen(Qt::NoPen);
  21. painter->drawEllipse(QPointF(-18, 0), 4, 4);
  22. painter->drawEllipse(QPointF(18, 0), 4, 4);
  23. }
  24. painter->save();
  25. painter->setFont(QFont("Arial", 8));
  26. painter->setPen(Qt::black);
  27. if (!registerId_.isEmpty()) {
  28. QString text = QString("%1: %2").arg(registerId_).arg(registerValue_);
  29. painter->drawText(QRectF(-20, -25, 40, 20), Qt::AlignCenter, text);
  30. }
  31. // 在右上角显示第二个寄存器
  32. if (!registerId2_.isEmpty()) {
  33. QString text = QString("%1: %2").arg(registerId2_).arg(registerValue2_);
  34. painter->drawText(QRectF(-20, 5, 40, 20), Qt::AlignCenter, text);
  35. }
  36. painter->restore();
  37. if (option->state & QStyle::State_Selected) {
  38. QPen pen(Qt::DashLine);
  39. pen.setColor(Qt::blue);
  40. pen.setWidth(2);
  41. painter->setPen(pen);
  42. painter->setBrush(Qt::NoBrush);
  43. painter->drawRect(boundingRect());
  44. }
  45. }
  46. void Comparator::setRegisterId(const QString &id)
  47. {
  48. if (registerId_.isEmpty())
  49. {
  50. registerId_ = id;
  51. }
  52. else if(registerId2_.isEmpty())
  53. {
  54. registerId2_ = id;
  55. }
  56. }
  57. void Comparator::setRegisterValue(const QString &registerId, quint16 value)
  58. {
  59. if (registerId_ == registerId)
  60. {
  61. registerValue_ = value;
  62. update(); // 触发重绘
  63. }
  64. if (registerId2_ == registerId)
  65. {
  66. registerValue2_ = value;
  67. update(); // 触发重绘
  68. }
  69. }