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.

53 lines
1.6 KiB

  1. #include "coil.h"
  2. #include <QPainter>
  3. #include <QStyleOptionGraphicsItem>
  4. Coil::Coil(const QString &type) : Item(type)
  5. {
  6. }
  7. void Coil::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *)
  8. {
  9. painter->setRenderHint(QPainter::Antialiasing);
  10. if (state()) {
  11. painter->setBrush(Qt::green); // 激活状态
  12. } else {
  13. painter->setBrush(Qt::white); // 未激活状态
  14. }
  15. if (type_ == "线圈") {
  16. // 绘制线圈样式: 两边线段+中间椭圆
  17. painter->drawLine(-12, 0, -5, 0);
  18. painter->drawEllipse(QRectF(-5, -8, 10, 16));
  19. painter->drawLine(5, 0, 12, 0);
  20. // 画锚点
  21. painter->setBrush(Qt::darkGray);
  22. painter->setPen(Qt::NoPen);
  23. painter->drawEllipse(QPointF(-12, 0), 4, 4); // 左锚点
  24. painter->drawEllipse(QPointF(12, 0), 4, 4); // 右锚点
  25. }
  26. if (!registerId_.isEmpty()) {
  27. painter->save();
  28. painter->setFont(QFont("Arial", 8));
  29. painter->setPen(Qt::black);
  30. QString text = QString("%1: %2").arg(registerId()).arg(registerValue_);
  31. painter->drawText(boundingRect(), Qt::AlignBottom | Qt::AlignHCenter, text);
  32. painter->restore();
  33. }
  34. if (option->state & QStyle::State_Selected) {
  35. QPen pen(Qt::DashLine);
  36. pen.setColor(Qt::blue);
  37. pen.setWidth(2);
  38. painter->setPen(pen);
  39. painter->setBrush(Qt::NoBrush);
  40. painter->drawRect(boundingRect());
  41. }
  42. }
  43. bool Coil::state() const
  44. {
  45. return registerValue_ > 0;
  46. }