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.

177 lines
5.0 KiB

  1. #include "light.h"
  2. #include <QPainter>
  3. #include <QStyleOptionGraphicsItem>
  4. #include <QMenu>
  5. #include <QFileDialog>
  6. #include <QGraphicsSceneMouseEvent>
  7. #include <QCursor>
  8. Light::Light(const QString &type) : Item(type)
  9. {
  10. }
  11. QRectF Light::boundingRect() const
  12. {
  13. return QRectF(0, 0, currentSize_.width(), currentSize_.height());
  14. }
  15. void Light::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *)
  16. {
  17. painter->setRenderHint(QPainter::Antialiasing);
  18. if (type_ == "指示灯") {
  19. if (state() && !onPixmap_.isNull()) {
  20. painter->drawPixmap(0, 0, currentSize_.width(), currentSize_.height(), onPixmap_);
  21. } else if (!state() && !offPixmap_.isNull()) {
  22. painter->drawPixmap(0, 0, currentSize_.width(), currentSize_.height(), offPixmap_);
  23. } else {
  24. if (state()) {
  25. painter->setBrush(Qt::green); // 激活状态
  26. } else {
  27. painter->setBrush(Qt::white); // 未激活状态
  28. }
  29. painter->drawEllipse(0, 0, currentSize_.width(), currentSize_.height());
  30. }
  31. }
  32. if (!registerId_.isEmpty()) {
  33. painter->save();
  34. painter->setFont(QFont("Arial", 8));
  35. painter->setPen(Qt::black);
  36. QString text = QString("%1").arg(registerId());
  37. painter->drawText(QRectF(0, 0, currentSize_.width(), currentSize_.height()), Qt::AlignCenter, text);
  38. painter->restore();
  39. }
  40. if (option->state & QStyle::State_Selected) {
  41. QPen pen(Qt::DashLine);
  42. pen.setColor(Qt::blue);
  43. pen.setWidth(2);
  44. painter->setPen(pen);
  45. painter->setBrush(Qt::NoBrush);
  46. painter->drawRect(boundingRect());
  47. }
  48. }
  49. bool Light::state() const
  50. {
  51. return registerValue_ > 0;
  52. }
  53. void Light::addMenuActions(QMenu *menu)
  54. {
  55. menu->addAction("ON外观设置");
  56. menu->addAction("OFF外观设置");
  57. }
  58. void Light::handleMenuAction(QAction *action)
  59. {
  60. if (action->text() == "复制") {
  61. emit requestCopy(this);
  62. }
  63. if (action->text() == "删除") {
  64. emit requestDelete(this);
  65. }
  66. if (action->text() == "对象"){
  67. emit requestBindRegister(this);
  68. }
  69. if (action->text() == "重置"){
  70. emit requestReset(this);
  71. }
  72. if (action->text() == "ON外观设置"){
  73. QString fileName = QFileDialog::getOpenFileName(nullptr,
  74. "选择图片",
  75. "",
  76. "图片文件(*.png *.jpg *.bmp)");
  77. if (!fileName.isEmpty())
  78. {
  79. setOnImage(fileName);
  80. }
  81. }
  82. if (action->text() == "OFF外观设置"){
  83. QString fileName = QFileDialog::getOpenFileName(nullptr,
  84. "选择图片",
  85. "",
  86. "图片文件(*.png *.jpg *.bmp)");
  87. if (!fileName.isEmpty())
  88. {
  89. setOffImage(fileName);
  90. }
  91. }
  92. }
  93. void Light::setOnImage(const QString& imagePath)
  94. {
  95. onImagePath_ = imagePath;
  96. loadImage();
  97. update();
  98. }
  99. void Light::setOffImage(const QString &imagePath)
  100. {
  101. offImagePath_ = imagePath;
  102. loadImage();
  103. update();
  104. }
  105. void Light::loadImage()
  106. {
  107. if (!onImagePath_.isEmpty()) {
  108. onPixmap_ = QPixmap(onImagePath_);
  109. }
  110. if (!offImagePath_.isEmpty()) {
  111. offPixmap_ = QPixmap(offImagePath_);
  112. }
  113. }
  114. void Light::mousePressEvent(QGraphicsSceneMouseEvent *event)
  115. {
  116. // 检查是否点击了调整手柄
  117. QRectF resizeHandle = QRectF(currentSize_.width() - resizeHandleSize_,
  118. currentSize_.height() - resizeHandleSize_,
  119. resizeHandleSize_, resizeHandleSize_);
  120. if (resizeHandle.contains(event->pos())) {
  121. resizing_ = true;
  122. resizeStartPos_ = event->scenePos();
  123. originalSize_ = currentSize_; // 当前大小
  124. event->accept();
  125. } else {
  126. // 否则调用基类处理(移动等)
  127. QGraphicsObject::mousePressEvent(event);
  128. }
  129. }
  130. void Light::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
  131. {
  132. if (resizing_) {
  133. // 计算新的尺寸
  134. QPointF delta = event->scenePos() - resizeStartPos_;
  135. qreal newWidth = originalSize_.width() + delta.x();
  136. qreal newHeight = originalSize_.height() + delta.y();
  137. // 限制最小尺寸
  138. if (newWidth < 20) newWidth = 20;
  139. if (newHeight < 20) newHeight = 20;
  140. // 更新当前尺寸
  141. currentSize_ = QSizeF(newWidth, newHeight);
  142. // 准备几何变化
  143. prepareGeometryChange();
  144. update(); // 更新显示
  145. event->accept();
  146. } else {
  147. QGraphicsObject::mouseMoveEvent(event);
  148. }
  149. }
  150. void Light::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
  151. {
  152. if (resizing_) {
  153. resizing_ = false;
  154. event->accept();
  155. } else {
  156. QGraphicsObject::mouseReleaseEvent(event);
  157. }
  158. }