123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- #include "MaterialBox.h"
- MaterialBox::MaterialBox(int flag, QWidget *parent) : QWidget(parent) {
-
- boxes = {
- 2,
- 32,
- 100,
- 50,
- {15.5, 20.3},
- {1,0,1,1,0},
-
- };
- }
- void MaterialBox::paintEvent(QPaintEvent *event) {
- QPainter painter(this);
- painter.setRenderHint(QPainter::Antialiasing);
-
- painter.fillRect(this->rect(), Qt::white);
- }
- void MaterialBox::paintInitFrom(QWidget *parent) {
-
- const int containerWidthdp = parent->width() * 0.25;
- const int containerHeightdp = parent->height() * 0.05;
- const int containerWidth = parent->width() * 0.5;
- const int containerHeight = parent->height() *0.9;
- globalPixmap = QPixmap(parent->width(), parent->height());
- globalPixmap.fill(Qt::white);
-
- QPainter painter(&globalPixmap);
- painter.setRenderHint(QPainter::Antialiasing);
-
- painter.setPen(Qt::black);
- painter.setBrush(Qt::lightGray);
- painter.drawRect(containerWidthdp, containerHeightdp, containerWidth, containerHeight);
-
- int innerWidth = containerWidth - 10;
- int innerHeight = containerHeight - 10;
- int innerX = containerWidthdp + containerWidth*0.1;
- int innerY = containerHeightdp + 5;
-
- const int totalLayers = boxes.iLayersTotal;
- const float spacing = static_cast<float>((static_cast<float>(innerHeight -totalLayers)) /static_cast<float>(totalLayers));
- qDebug()<<spacing;
- float rectHeight = 2.5;
- const float rectWeight = static_cast<float>(innerWidth);
- if((0<=totalLayers)&&(totalLayers<=8)){
- rectHeight = 2.5;
- }else if((9<=totalLayers)&&(totalLayers<=16)){
- rectHeight = 1.5;
- }else{
- rectHeight = 1;
- }
-
- for(int i = 0; i < totalLayers; ++i) {
- const QColor color = boxes.iStatas[i] ? QColor(40, 129, 5) : QColor(255, 0, 0);
- const int yPos = innerY + i * (rectHeight + spacing);
- painter.setPen(Qt::NoPen);
- painter.setBrush(color);
- painter.drawRect(static_cast<float>(innerX), static_cast<float>(yPos), static_cast<float>(rectWeight), static_cast<float>(rectHeight));
- }
- }
- QPixmap MaterialBox::getGlobalPixmap() const {
- return globalPixmap;
- }
- void MaterialBox::initFrom(QWidget *parent){
- int ratio = parent->height()/100;
-
-
-
- const int containerWidthdp = parent->width() * 0.25;
- const int containerHeightdp = parent->height() * 0.05;
- const int containerWidth = parent->width() * 0.5;
- const int containerHeight = parent->height() *0.9;
-
- int innerWidth = containerWidth - 10*ratio;
- int innerHeight = containerHeight - 10*ratio;
- int innerX = containerWidthdp + containerWidth*0.1;
- int innerY = containerHeightdp + 5*ratio;
-
- const int totalLayers = boxes.iLayersTotal;
- const float spacing = static_cast<float>((static_cast<float>(innerHeight -totalLayers*ratio)) /static_cast<float>(totalLayers));
- qDebug()<<spacing;
- float rectHeight = 2.5*ratio;
- const float rectWeight = static_cast<float>(innerWidth);
- if((0<=totalLayers)&&(totalLayers<=8)){
- rectHeight = 2.5*ratio;
- }else if((9<=totalLayers)&&(totalLayers<=16)){
- rectHeight = 1.5*ratio;
- }else{
- rectHeight = 1*ratio;
- }
- BackgroundRect* backgroundRect = new BackgroundRect(containerWidthdp, containerHeightdp, containerWidth, containerHeight);
- scene = new QGraphicsScene(parent);
- view = new MaterialBoxGraphicsView(scene);
- scene->addItem(backgroundRect);
- for (int i = 0; i < totalLayers; ++i) {
- MaterialBoxDie* materialBoxDie = new MaterialBoxDie(i, 0, boxes.iStatas[i],rectWeight, rectHeight);
- materialBoxDie->setPos(innerX,(innerY+ i * (rectHeight + spacing) ));
- scene->addItem(materialBoxDie);
- }
- connect(view, &MaterialBoxGraphicsView::layerRightClicked, this, &MaterialBox::handleLayerRightClicked);
- view->resize(parent->width(), parent->height());
- view->resetTransform();
- view->setSceneRect(0, 0, parent->width(), parent->height());
- }
- void MaterialBox::handleLayerRightClicked(int layer) {
- qDebug() << "Right-clicked on layer:" << layer;
-
- }
|