您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

76 行
1.9 KiB

  1. #include "button.h"
  2. #include <QPainter>
  3. #include <QStyleOptionGraphicsItem>
  4. #include <QMenu>
  5. Button::Button(const QString &type) : Item(type)
  6. {
  7. }
  8. QRectF Button::boundingRect() const
  9. {
  10. return QRectF(0, 0, 50, 50);
  11. }
  12. void Button::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *)
  13. {
  14. painter->setRenderHint(QPainter::Antialiasing);
  15. if (type_ == "按钮") {
  16. painter->drawRect(0,0,50,50);
  17. }
  18. if (!registerId_.isEmpty()) {
  19. painter->save();
  20. painter->setFont(QFont("Arial", 8));
  21. painter->setPen(Qt::black);
  22. QString text;
  23. if (registerValue_ > 0)
  24. {
  25. text = QString("%1\nON").arg(registerId());
  26. }
  27. else
  28. {
  29. text = QString("%1\nOFF").arg(registerId());
  30. }
  31. painter->drawText(QRectF(13, 13, 26, 26), Qt::AlignCenter, text);
  32. painter->restore();
  33. }
  34. if (option->state & QStyle::State_Selected) {
  35. QPen pen(Qt::DashLine);
  36. pen.setColor(Qt::blue);
  37. pen.setWidth(2);
  38. painter->setPen(pen);
  39. painter->setBrush(Qt::NoBrush);
  40. painter->drawRect(boundingRect());
  41. }
  42. }
  43. void Button::addMenuActions(QMenu *menu)
  44. {
  45. menu->addAction("置ON");
  46. menu->addAction("置OFF");
  47. }
  48. void Button::handleMenuAction(QAction *action)
  49. {
  50. if (action->text() == "复制") {
  51. emit requestCopy(this);
  52. }
  53. if (action->text() == "删除") {
  54. emit requestDelete(this);
  55. }
  56. if (action->text() == "对象"){
  57. emit requestBindRegister(this);
  58. }
  59. if (action->text() == "重置"){
  60. emit requestReset(this);
  61. }
  62. if (action->text() == "置ON"){
  63. emit requestSetON(this, true);
  64. }
  65. if (action->text() == "置OFF"){
  66. emit requestSetON(this, false);
  67. }
  68. }