MaterialBox.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #include "MaterialBox.h"
  2. // 构造函数
  3. MaterialBox::MaterialBox(int flag, QWidget *parent) : QWidget(parent)
  4. {
  5. boxes = {
  6. 2, // iCurrLayers
  7. 32, // iLayersTotal
  8. 100, // iTopLev
  9. 50, // iBottomLev
  10. {15.5, 20.3}, // 补全所有子成员
  11. {1,0,1,1,0},
  12. // ...后续成员保持相同
  13. };
  14. }
  15. // 绘制事件
  16. void MaterialBox::paintEvent(QPaintEvent *event)
  17. {
  18. QPainter painter(this);
  19. painter.setRenderHint(QPainter::Antialiasing);
  20. // 设置背景颜色
  21. painter.fillRect(this->rect(), Qt::white);
  22. }
  23. void MaterialBox::paintInitFrom(QWidget *parent)
  24. {
  25. // 主容器尺寸(占窗口80%)
  26. const int containerWidthdp = parent->width() * 0.25;
  27. const int containerHeightdp = parent->height() * 0.05;
  28. const int containerWidth = parent->width() * 0.5;
  29. const int containerHeight = parent->height() *0.9;
  30. globalPixmap = QPixmap(parent->width(), parent->height());
  31. globalPixmap.fill(Qt::white); // 填充背景色为白色
  32. // 创建 QPainter 以绘制到 QPixmap 上
  33. QPainter painter(&globalPixmap);
  34. painter.setRenderHint(QPainter::Antialiasing); // 启用抗锯齿
  35. // 绘制灰色背景框
  36. painter.setPen(Qt::black);
  37. painter.setBrush(Qt::lightGray);
  38. painter.drawRect(containerWidthdp, containerHeightdp, containerWidth, containerHeight);
  39. // 计算内部可用空间(留5像素边距)
  40. int innerWidth = containerWidth - 10; // 左右各5像素
  41. int innerHeight = containerHeight - 10; // 上下各5像素
  42. int innerX = containerWidthdp + containerWidth*0.1;
  43. int innerY = containerHeightdp + 5;
  44. // 根据层数自动计算间距和矩形高度
  45. const int totalLayers = boxes.iLayersTotal;
  46. const float spacing = static_cast<float>((static_cast<float>(innerHeight -totalLayers)) /static_cast<float>(totalLayers)); // 每个矩形之间的固定间距(像素)
  47. qDebug()<<spacing;
  48. float rectHeight = 2.5;
  49. const float rectWeight = static_cast<float>(innerWidth);
  50. if((0<=totalLayers)&&(totalLayers<=8)){
  51. rectHeight = 2.5;
  52. }else if((9<=totalLayers)&&(totalLayers<=16)){
  53. rectHeight = 1.5;
  54. }else{
  55. rectHeight = 1;
  56. }
  57. // 逐层绘制状态矩形
  58. for(int i = 0; i < totalLayers; ++i) {
  59. const QColor color = boxes.iStatas[i] ? QColor(40, 129, 5) : QColor(255, 0, 0); // 1=绿,0=红
  60. const int yPos = innerY + i * (rectHeight + spacing);
  61. painter.setPen(Qt::NoPen); // 去掉边框
  62. painter.setBrush(color);
  63. painter.drawRect(static_cast<float>(innerX), static_cast<float>(yPos), static_cast<float>(rectWeight), static_cast<float>(rectHeight));
  64. }
  65. }
  66. QPixmap MaterialBox::getGlobalPixmap() const {
  67. return globalPixmap;
  68. }
  69. void MaterialBox::initFrom(QWidget *parent){
  70. int ratio = parent->height()/100;
  71. // 主容器尺寸(占窗口80%)
  72. // qDebug()<<ratio;
  73. // qDebug()<<"1111"<<parent->width()<<parent->height();
  74. const int containerWidthdp = parent->width() * 0.25;
  75. const int containerHeightdp = parent->height() * 0.05;
  76. const int containerWidth = parent->width() * 0.5;
  77. const int containerHeight = parent->height() *0.9;
  78. // 计算内部可用空间(留5像素边距)
  79. int innerWidth = containerWidth - 10*ratio; // 左右各5像素
  80. int innerHeight = containerHeight - 10*ratio; // 上下各5像素
  81. int innerX = containerWidthdp + containerWidth*0.1;
  82. int innerY = containerHeightdp + 5*ratio;
  83. // 根据层数自动计算间距和矩形高度
  84. const int totalLayers = boxes.iLayersTotal;
  85. const float spacing = static_cast<float>((static_cast<float>(innerHeight -totalLayers*ratio)) /static_cast<float>(totalLayers)); // 每个矩形之间的固定间距(像素)
  86. qDebug()<<spacing;
  87. float rectHeight = 2.5*ratio;
  88. const float rectWeight = static_cast<float>(innerWidth);
  89. if((0<=totalLayers)&&(totalLayers<=8)){
  90. rectHeight = 2.5*ratio;
  91. }else if((9<=totalLayers)&&(totalLayers<=16)){
  92. rectHeight = 1.5*ratio;
  93. }else{
  94. rectHeight = 1*ratio;
  95. }
  96. BackgroundRect* backgroundRect = new BackgroundRect(containerWidthdp, containerHeightdp, containerWidth, containerHeight);
  97. scene = new QGraphicsScene(parent);
  98. view = new MaterialBoxGraphicsView(scene);
  99. scene->addItem(backgroundRect);
  100. for (int i = 0; i < totalLayers; ++i) {
  101. MaterialBoxDie* materialBoxDie = new MaterialBoxDie(i, 0, boxes.iStatas[i],rectWeight, rectHeight);
  102. materialBoxDie->setPos(innerX,(innerY+ i * (rectHeight + spacing) ));
  103. scene->addItem(materialBoxDie);
  104. }
  105. connect(view, &MaterialBoxGraphicsView::layerRightClicked, this, &MaterialBox::handleLayerRightClicked);
  106. view->resize(parent->width(), parent->height());
  107. view->resetTransform();
  108. view->setSceneRect(0, 0, parent->width(), parent->height());
  109. }
  110. void MaterialBox::handleLayerRightClicked(int layer) {
  111. qDebug() << "Right-clicked on layer:" << layer;
  112. // 在这里可以添加其他处理逻辑,例如发送信号到其他对象
  113. }