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.

197 rader
5.2 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::setCurrentSize(QSizeF size)
  106. {
  107. currentSize_ = size;
  108. }
  109. QSizeF Light::currentSize()
  110. {
  111. return currentSize_;
  112. }
  113. QString Light::onImagePath()
  114. {
  115. return onImagePath_;
  116. }
  117. QString Light::offImagePath()
  118. {
  119. return offImagePath_;
  120. }
  121. void Light::loadImage()
  122. {
  123. if (!onImagePath_.isEmpty()) {
  124. onPixmap_ = QPixmap(onImagePath_);
  125. }
  126. if (!offImagePath_.isEmpty()) {
  127. offPixmap_ = QPixmap(offImagePath_);
  128. }
  129. }
  130. void Light::mousePressEvent(QGraphicsSceneMouseEvent *event)
  131. {
  132. // 检查是否点击了调整手柄
  133. QRectF resizeHandle = QRectF(currentSize_.width() - resizeHandleSize_,
  134. currentSize_.height() - resizeHandleSize_,
  135. resizeHandleSize_, resizeHandleSize_);
  136. if (resizeHandle.contains(event->pos())) {
  137. resizing_ = true;
  138. resizeStartPos_ = event->scenePos();
  139. originalSize_ = currentSize_; // 当前大小
  140. event->accept();
  141. } else {
  142. // 否则调用基类处理(移动等)
  143. QGraphicsObject::mousePressEvent(event);
  144. }
  145. }
  146. void Light::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
  147. {
  148. if (resizing_) {
  149. // 计算新的尺寸
  150. QPointF delta = event->scenePos() - resizeStartPos_;
  151. qreal newWidth = originalSize_.width() + delta.x();
  152. qreal newHeight = originalSize_.height() + delta.y();
  153. // 限制最小尺寸
  154. if (newWidth < 20) newWidth = 20;
  155. if (newHeight < 20) newHeight = 20;
  156. // 更新当前尺寸
  157. currentSize_ = QSizeF(newWidth, newHeight);
  158. // 准备几何变化
  159. prepareGeometryChange();
  160. update(); // 更新显示
  161. event->accept();
  162. } else {
  163. QGraphicsObject::mouseMoveEvent(event);
  164. }
  165. }
  166. void Light::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
  167. {
  168. if (resizing_) {
  169. resizing_ = false;
  170. event->accept();
  171. } else {
  172. QGraphicsObject::mouseReleaseEvent(event);
  173. }
  174. }