MaterialBox.cpp 5.3 KB

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