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.

129 line
3.5 KiB

  1. #include "light.h"
  2. #include <QPainter>
  3. #include <QStyleOptionGraphicsItem>
  4. #include <QMenu>
  5. #include <QFileDialog>
  6. Light::Light(const QString &type) : Item(type)
  7. {
  8. }
  9. QRectF Light::boundingRect() const
  10. {
  11. return QRectF(0, 0, 50, 50);
  12. }
  13. void Light::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *)
  14. {
  15. painter->setRenderHint(QPainter::Antialiasing);
  16. if (type_ == "指示灯") {
  17. if (state() && !onPixmap_.isNull()) {
  18. painter->drawPixmap(0, 0, 50, 50, onPixmap_);
  19. } else if (!state() && !offPixmap_.isNull()) {
  20. painter->drawPixmap(0, 0, 50, 50, offPixmap_);
  21. } else {
  22. if (state()) {
  23. painter->setBrush(Qt::green); // 激活状态
  24. } else {
  25. painter->setBrush(Qt::white); // 未激活状态
  26. }
  27. painter->drawEllipse(0,0,50,50);
  28. }
  29. }
  30. if (!registerId_.isEmpty()) {
  31. painter->save();
  32. painter->setFont(QFont("Arial", 8));
  33. painter->setPen(Qt::black);
  34. QString text = QString("%1").arg(registerId());
  35. painter->drawText(QRectF(13, 13, 26, 26), Qt::AlignCenter, text);
  36. painter->restore();
  37. }
  38. if (option->state & QStyle::State_Selected) {
  39. QPen pen(Qt::DashLine);
  40. pen.setColor(Qt::blue);
  41. pen.setWidth(2);
  42. painter->setPen(pen);
  43. painter->setBrush(Qt::NoBrush);
  44. painter->drawRect(boundingRect());
  45. }
  46. }
  47. bool Light::state() const
  48. {
  49. return registerValue_ > 0;
  50. }
  51. void Light::addMenuActions(QMenu *menu)
  52. {
  53. menu->addAction("ON外观设置");
  54. menu->addAction("OFF外观设置");
  55. }
  56. void Light::handleMenuAction(QAction *action)
  57. {
  58. if (action->text() == "复制") {
  59. emit requestCopy(this);
  60. }
  61. if (action->text() == "删除") {
  62. emit requestDelete(this);
  63. }
  64. if (action->text() == "对象"){
  65. emit requestBindRegister(this);
  66. }
  67. if (action->text() == "重置"){
  68. emit requestReset(this);
  69. }
  70. if (action->text() == "ON外观设置"){
  71. QString fileName = QFileDialog::getOpenFileName(nullptr,
  72. "选择图片",
  73. "",
  74. "图片文件(*.png *.jpg *.bmp)");
  75. if (!fileName.isEmpty())
  76. {
  77. setOnImage(fileName);
  78. }
  79. }
  80. if (action->text() == "OFF外观设置"){
  81. QString fileName = QFileDialog::getOpenFileName(nullptr,
  82. "选择图片",
  83. "",
  84. "图片文件(*.png *.jpg *.bmp)");
  85. if (!fileName.isEmpty())
  86. {
  87. setOffImage(fileName);
  88. }
  89. }
  90. }
  91. void Light::setOnImage(const QString& imagePath)
  92. {
  93. onImagePath_ = imagePath;
  94. loadImage();
  95. update();
  96. }
  97. void Light::setOffImage(const QString &imagePath)
  98. {
  99. offImagePath_ = imagePath;
  100. loadImage();
  101. update();
  102. }
  103. void Light::loadImage()
  104. {
  105. if (!onImagePath_.isEmpty()) {
  106. onPixmap_ = QPixmap(onImagePath_);
  107. if (!onPixmap_.isNull()) {
  108. onPixmap_ = onPixmap_.scaled(50, 50, Qt::KeepAspectRatio, Qt::SmoothTransformation);
  109. }
  110. }
  111. if (!offImagePath_.isEmpty()) {
  112. offPixmap_ = QPixmap(offImagePath_);
  113. if (!offPixmap_.isNull()) {
  114. offPixmap_ = offPixmap_.scaled(50, 50, Qt::KeepAspectRatio, Qt::SmoothTransformation);
  115. }
  116. }
  117. }