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.

49 lines
1.3 KiB

  1. #include "light.h"
  2. #include <QPainter>
  3. #include <QStyleOptionGraphicsItem>
  4. Light::Light(const QString &type) : Item(type)
  5. {
  6. }
  7. QRectF Light::boundingRect() const
  8. {
  9. return QRectF(0, 0, 50, 50);
  10. }
  11. void Light::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *)
  12. {
  13. painter->setRenderHint(QPainter::Antialiasing);
  14. if (state()) {
  15. painter->setBrush(Qt::green); // 激活状态
  16. } else {
  17. painter->setBrush(Qt::white); // 未激活状态
  18. }
  19. if (type_ == "指示灯") {
  20. painter->drawEllipse(0,0,50,50);
  21. }
  22. if (!registerId_.isEmpty()) {
  23. painter->save();
  24. painter->setFont(QFont("Arial", 8));
  25. painter->setPen(Qt::black);
  26. QString text = QString("%1").arg(registerId());
  27. painter->drawText(QRectF(13, 13, 26, 26), Qt::AlignCenter, text);
  28. painter->restore();
  29. }
  30. if (option->state & QStyle::State_Selected) {
  31. QPen pen(Qt::DashLine);
  32. pen.setColor(Qt::blue);
  33. pen.setWidth(2);
  34. painter->setPen(pen);
  35. painter->setBrush(Qt::NoBrush);
  36. painter->drawRect(boundingRect());
  37. }
  38. }
  39. bool Light::state() const
  40. {
  41. return registerValue_ > 0;
  42. }