#include "customwidget.h"
#include <QVBoxLayout>
#include <QLabel>
#include <QTextEdit>
#include <QTableWidget>
#include <QHeaderView>
#include <QGraphicsDropShadowEffect>

CustomWidget::CustomWidget(QWidget *parent)
    : QWidget{parent}
{
    QWidget *centralWidget = new QWidget();
    QGridLayout *mainLayout = new QGridLayout(this);
    mainLayout->addWidget(centralWidget);

    //实例阴影shadow
    QGraphicsDropShadowEffect *shadow = new QGraphicsDropShadowEffect(this);
    //设置阴影距离
    shadow->setOffset(0, 0);
    //设置阴影颜色
    shadow->setColor(QColor(44,44,44));
    //设置阴影圆角
    shadow->setBlurRadius(16);
    //给嵌套QDialog设置阴影
    centralWidget->setGraphicsEffect(shadow);

    QVBoxLayout *mainLayout1 = new QVBoxLayout(centralWidget);
    mainLayout1->setSpacing(5);  // 控件间间隔

    // 上1/3:QLabel
    QWidget *topWidget = new QWidget();
    mainLayout1->addWidget(topWidget, 1);  // 拉伸因子为1
    setupTopSection(topWidget);

    // 中1/3:两个垂直排列的QTextEdit
    QWidget *midWidget = new QWidget();
    mainLayout1->addWidget(midWidget, 1);
    setupMidSection(midWidget);

    // 下1/3:表格
    QWidget *bottomWidget = new QWidget();
    mainLayout1->addWidget(bottomWidget, 1);
    setupBottomSection(bottomWidget);
}

void CustomWidget::setupTopSection(QWidget *parent)
{
    //实例阴影shadow
    QGraphicsDropShadowEffect *shadow = new QGraphicsDropShadowEffect(this);
    //设置阴影距离
    shadow->setOffset(0, 0);
    //设置阴影颜色
    shadow->setColor(QColor(244,44,44));
    //设置阴影圆角
    shadow->setBlurRadius(16);

    QLabel *label = new QLabel("顶部标签", parent);
    label->setAlignment(Qt::AlignCenter);
    label->setGraphicsEffect(shadow);

    QVBoxLayout *layout = new QVBoxLayout(parent);
    layout->addWidget(label);
}

void CustomWidget::setupMidSection(QWidget *parent)
{
    //实例阴影shadow
    QGraphicsDropShadowEffect *shadow = new QGraphicsDropShadowEffect(this);
    //设置阴影距离
    shadow->setOffset(0, 0);
    //设置阴影颜色
    shadow->setColor(QColor(244,44,44));
    //设置阴影圆角
    shadow->setBlurRadius(16);

    QVBoxLayout *layout = new QVBoxLayout(parent);
    QTextEdit *textEdit1 = new QTextEdit();
    QTextEdit *textEdit2 = new QTextEdit();
    layout->addWidget(textEdit1, 1);  // 各占中间区域的一半高度
    layout->addWidget(textEdit2, 1);
}

void CustomWidget::setupBottomSection(QWidget *parent)
{
    //实例阴影shadow
    QGraphicsDropShadowEffect *shadow = new QGraphicsDropShadowEffect(this);
    //设置阴影距离
    shadow->setOffset(0, 0);
    //设置阴影颜色
    shadow->setColor(QColor(244,44,44));
    //设置阴影圆角
    shadow->setBlurRadius(16);

    QTableWidget *table = new QTableWidget(5, 2, parent);  // 5行2列
    table->setHorizontalHeaderLabels({"列1", "列2"});
    table->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);  // 列宽自适应
    table->setGraphicsEffect(shadow);
    QVBoxLayout *layout = new QVBoxLayout(parent);
    layout->addWidget(table);
}