No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

177 líneas
4.6 KiB

  1. #include "button.h"
  2. #include <QPainter>
  3. #include <QStyleOptionGraphicsItem>
  4. #include <QGraphicsSceneMouseEvent>
  5. #include <QMenu>
  6. #include <QFileDialog>
  7. #include <QCursor>
  8. Button::Button(const QString &type) : Item(type)
  9. {
  10. }
  11. QRectF Button::boundingRect() const
  12. {
  13. return QRectF(0, 0, currentSize_.width(), currentSize_.height());
  14. }
  15. void Button::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *)
  16. {
  17. painter->setRenderHint(QPainter::Antialiasing);
  18. if (type_ == "按钮") {
  19. if (!imagePath_.isEmpty() && !customPixmap_.isNull())
  20. {
  21. painter->drawPixmap(0, 0, currentSize_.width(), currentSize_.height(), customPixmap_);
  22. }
  23. else{
  24. painter->setBrush(Qt::green);
  25. painter->drawRect(0, 0, currentSize_.width(), currentSize_.height());
  26. }
  27. }
  28. if (!registerId_.isEmpty()) {
  29. painter->save();
  30. painter->setFont(QFont("Arial", 8));
  31. painter->setPen(Qt::red);
  32. QString text;
  33. if (registerValue_ > 0)
  34. {
  35. text = QString("%1\nON").arg(registerId());
  36. }
  37. else
  38. {
  39. text = QString("%1\nOFF").arg(registerId());
  40. }
  41. painter->drawText(QRectF(0, 0, currentSize_.width(), currentSize_.height()), Qt::AlignCenter, text);
  42. painter->restore();
  43. }
  44. if (option->state & QStyle::State_Selected) {
  45. QPen pen(Qt::DashLine);
  46. pen.setColor(Qt::blue);
  47. pen.setWidth(2);
  48. painter->setPen(pen);
  49. painter->setBrush(Qt::NoBrush);
  50. painter->drawRect(boundingRect());
  51. }
  52. }
  53. void Button::addMenuActions(QMenu *menu)
  54. {
  55. menu->addAction("置ON");
  56. menu->addAction("置OFF");
  57. menu->addAction("外观设置");
  58. }
  59. void Button::handleMenuAction(QAction *action)
  60. {
  61. if (action->text() == "复制") {
  62. emit requestCopy(this);
  63. }
  64. if (action->text() == "删除") {
  65. emit requestDelete(this);
  66. }
  67. if (action->text() == "对象"){
  68. emit requestBindRegister(this);
  69. }
  70. if (action->text() == "重置"){
  71. emit requestReset(this);
  72. }
  73. if (action->text() == "置ON"){
  74. emit requestSetON(this, true);
  75. }
  76. if (action->text() == "置OFF"){
  77. emit requestSetON(this, false);
  78. }
  79. if (action->text() == "外观设置"){
  80. QString fileName = QFileDialog::getOpenFileName(nullptr,
  81. "选择图片",
  82. "",
  83. "图片文件(*.png *.jpg *.bmp)");
  84. if (!fileName.isEmpty())
  85. {
  86. setCustomImage(fileName);
  87. }
  88. }
  89. }
  90. void Button::setCustomImage(const QString &imagePath)
  91. {
  92. imagePath_ = imagePath;
  93. loadImage();
  94. update();
  95. }
  96. void Button::setCurrentSize(QSizeF size)
  97. {
  98. currentSize_ = size;
  99. }
  100. QSizeF Button::currentSize()
  101. {
  102. return currentSize_;
  103. }
  104. QString Button::imagePath()
  105. {
  106. return imagePath_;
  107. }
  108. void Button::loadImage()
  109. {
  110. if(!imagePath_.isEmpty()){
  111. customPixmap_ = QPixmap(imagePath_);
  112. }
  113. }
  114. void Button::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 Button::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 Button::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
  151. {
  152. if (resizing_) {
  153. resizing_ = false;
  154. event->accept();
  155. } else {
  156. QGraphicsObject::mouseReleaseEvent(event);
  157. }
  158. }