Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

179 lignes
4.7 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::setCurrentSize(QSizeF size)
  96. {
  97. currentSize_ = size;
  98. }
  99. QSizeF Button::currentSize()
  100. {
  101. return currentSize_;
  102. }
  103. QString Button::imagePath()
  104. {
  105. return imagePath_;
  106. }
  107. void Button::loadImage()
  108. {
  109. if(!imagePath_.isEmpty()){
  110. customPixmap_ = QPixmap(imagePath_);
  111. // if (!customPixmap_.isNull()){
  112. // customPixmap_ = customPixmap_.scaled(50, 50, Qt::KeepAspectRatio, Qt::SmoothTransformation);
  113. // }
  114. }
  115. }
  116. void Button::mousePressEvent(QGraphicsSceneMouseEvent *event)
  117. {
  118. // 检查是否点击了调整手柄
  119. QRectF resizeHandle = QRectF(currentSize_.width() - resizeHandleSize_,
  120. currentSize_.height() - resizeHandleSize_,
  121. resizeHandleSize_, resizeHandleSize_);
  122. if (resizeHandle.contains(event->pos())) {
  123. resizing_ = true;
  124. resizeStartPos_ = event->scenePos();
  125. originalSize_ = currentSize_; // 当前大小
  126. event->accept();
  127. } else {
  128. // 否则调用基类处理(移动等)
  129. QGraphicsObject::mousePressEvent(event);
  130. }
  131. }
  132. void Button::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
  133. {
  134. if (resizing_) {
  135. // 计算新的尺寸
  136. QPointF delta = event->scenePos() - resizeStartPos_;
  137. qreal newWidth = originalSize_.width() + delta.x();
  138. qreal newHeight = originalSize_.height() + delta.y();
  139. // 限制最小尺寸
  140. if (newWidth < 20) newWidth = 20;
  141. if (newHeight < 20) newHeight = 20;
  142. // 更新当前尺寸
  143. currentSize_ = QSizeF(newWidth, newHeight);
  144. // 准备几何变化
  145. prepareGeometryChange();
  146. update(); // 更新显示
  147. event->accept();
  148. } else {
  149. QGraphicsObject::mouseMoveEvent(event);
  150. }
  151. }
  152. void Button::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
  153. {
  154. if (resizing_) {
  155. resizing_ = false;
  156. event->accept();
  157. } else {
  158. QGraphicsObject::mouseReleaseEvent(event);
  159. }
  160. }