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.

166 lines
4.5 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->drawRect(0, 0, currentSize_.width(), currentSize_.height());
  25. }
  26. }
  27. if (!registerId_.isEmpty()) {
  28. painter->save();
  29. painter->setFont(QFont("Arial", 8));
  30. painter->setPen(Qt::red);
  31. QString text;
  32. if (registerValue_ > 0)
  33. {
  34. text = QString("%1\nON").arg(registerId());
  35. }
  36. else
  37. {
  38. text = QString("%1\nOFF").arg(registerId());
  39. }
  40. painter->drawText(QRectF(0, 0, currentSize_.width(), currentSize_.height()), Qt::AlignCenter, text);
  41. painter->restore();
  42. }
  43. if (option->state & QStyle::State_Selected) {
  44. QPen pen(Qt::DashLine);
  45. pen.setColor(Qt::blue);
  46. pen.setWidth(2);
  47. painter->setPen(pen);
  48. painter->setBrush(Qt::NoBrush);
  49. painter->drawRect(boundingRect());
  50. }
  51. }
  52. void Button::addMenuActions(QMenu *menu)
  53. {
  54. menu->addAction("置ON");
  55. menu->addAction("置OFF");
  56. menu->addAction("外观设置");
  57. }
  58. void Button::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. emit requestSetON(this, true);
  74. }
  75. if (action->text() == "置OFF"){
  76. emit requestSetON(this, false);
  77. }
  78. if (action->text() == "外观设置"){
  79. QString fileName = QFileDialog::getOpenFileName(nullptr,
  80. "选择图片",
  81. "",
  82. "图片文件(*.png *.jpg *.bmp)");
  83. if (!fileName.isEmpty())
  84. {
  85. setCustomImage(fileName);
  86. }
  87. }
  88. }
  89. void Button::setCustomImage(const QString &imagePath)
  90. {
  91. imagePath_ = imagePath;
  92. loadImage();
  93. update();
  94. }
  95. void Button::loadImage()
  96. {
  97. if(!imagePath_.isEmpty()){
  98. customPixmap_ = QPixmap(imagePath_);
  99. // if (!customPixmap_.isNull()){
  100. // customPixmap_ = customPixmap_.scaled(50, 50, Qt::KeepAspectRatio, Qt::SmoothTransformation);
  101. // }
  102. }
  103. }
  104. void Button::mousePressEvent(QGraphicsSceneMouseEvent *event)
  105. {
  106. // 检查是否点击了调整手柄
  107. QRectF resizeHandle = QRectF(currentSize_.width() - resizeHandleSize_,
  108. currentSize_.height() - resizeHandleSize_,
  109. resizeHandleSize_,
  110. resizeHandleSize_);
  111. if (resizeHandle.contains(event->pos())) {
  112. resizing_ = true;
  113. resizeStartPos_ = event->scenePos();
  114. originalSize_ = currentSize_; // 当前大小
  115. event->accept();
  116. } else {
  117. // 否则调用基类处理(移动等)
  118. QGraphicsObject::mousePressEvent(event);
  119. }
  120. }
  121. void Button::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
  122. {
  123. if (resizing_) {
  124. // 计算新的尺寸
  125. QPointF delta = event->scenePos() - resizeStartPos_;
  126. qreal newWidth = originalSize_.width() + delta.x();
  127. qreal newHeight = originalSize_.height() + delta.y();
  128. // 限制最小尺寸
  129. if (newWidth < 20) newWidth = 20;
  130. if (newHeight < 20) newHeight = 20;
  131. // 更新当前尺寸
  132. currentSize_ = QSizeF(newWidth, newHeight);
  133. // 准备几何变化
  134. prepareGeometryChange();
  135. update(); // 更新显示
  136. event->accept();
  137. } else {
  138. QGraphicsObject::mouseMoveEvent(event);
  139. }
  140. }
  141. void Button::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
  142. {
  143. if (resizing_) {
  144. resizing_ = false;
  145. event->accept();
  146. } else {
  147. QGraphicsObject::mouseReleaseEvent(event);
  148. }
  149. }