/*
 * Copyright (C) 2020, KylinSoft Co., Ltd.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */
#include "introductionview.h"
#include <QPainterPath>

#include "windowhelper.h"

IntroductionView::IntroductionView(QWidget * parent)
    :QDialog(parent)
{
#ifdef UKUI_WINDOWHELPER_SUPPORT
    UkuiWindowHelper *ukuiWindowHelper = new UkuiWindowHelper(this);
    ukuiWindowHelper->removeTitleBar();
#else
    kdk::UkuiStyleHelper::self()->removeHeader(this);
#endif

    setFixedSize(parent->size());
    m_content = new IntroductionContent(this);
    m_content->setAccessibleName("kylin-aiassistant_IntroductionContent_m_content");
    QVBoxLayout *layout = new QVBoxLayout(this);
    layout->addStretch();
    layout->addWidget(m_content);
    layout->addStretch();
    this->setLayout(layout);
}

void IntroductionView::showView()
{
    IntroductionView * father = reinterpret_cast<IntroductionView *>(parent());
    int posX = father->x() + (father->width() - this->width()) / 2;
    int posY = father->y() + (father->height() - this->height()) / 2;
    move(posX,posY);
    if(isWayland){
        this->setGeometry(QRect(posX, posY, this->width(), this->height()));
    }
    this->exec();
}

void IntroductionView::hideView()
{
    hide();
}

void IntroductionView::closeView()
{
    m_isClosed = true;
    close();
}

bool IntroductionView::isClosed() const
{
    return m_isClosed;
}

IntroductionContent::IntroductionContent(QWidget * parent)
    :QWidget(parent)
{
    initChild();
    initLayout();
    initConnection();
    showAgreement();
}

void IntroductionContent::initChild()
{
    m_icon = new QLabel(this);
    m_icon->setAccessibleName("kylin-aiassistant_QLabel_m_icon");
    m_shortcut = new QLabel(tr("Beginner's Guide - Shortcut Keys"),this);
    m_shortcut->setAccessibleName("kylin-aiassistant_QLabel_m_shortcut");
    m_addModel = new QLabel(tr("Beginner's Guide - Model Addition"),this);
    m_addModel->setAccessibleName("kylin-aiassistant_QLabel_m_addModel");


    m_winAltCWid = new InnerWidget(this);
    m_winAltCWid->setAccessibleName("kylin-aiassistant_InnerWidget_m_winAltCWid");
    setPageWid = new InnerWidget(this);
    setPageWid->setAccessibleName("kylin-aiassistant_InnerWidget_setPageWid");
    m_nextBtn = new QPushButton(tr("Next"), this);
    m_nextBtn->setAccessibleName("kylin-aiassistant_QPushButton_m_nextBtn");
    m_previousBtn = new QPushButton(tr("Previous"));
    m_previousBtn->setAccessibleName("kylin-aiassistant_QPushButton_m_previousBtn");
    m_acceptBtn = new QPushButton(tr("Configure"));
    m_acceptBtn->setAccessibleName("kylin-aiassistant_QPushButton_m_acceptBtn");
    m_agreementPageWid = new QWidget(this);
    m_agreementPageWid->setAccessibleName("kylin-aiassistant_QWidget_m_agreementPageWid");
    m_agreementTitle = new QLabel(tr("Beginner's Guide - User Agreement"), this);
    m_agreementTitle->setAccessibleName("kylin-aiassistant_QLabel_m_agreementTitle");
    m_agreementInnerWid = new InnerWidget(this);
    m_agreementInnerWid->setAccessibleName("kylin-aiassistant_InnerWidget_m_agreementInnerWid");
    m_agreementContent = new QTextBrowser(this);
    m_agreementContent->setAccessibleName("kylin-aiassistant_QTextBrowser_m_agreementContent");
    m_agreementContent->setReadOnly(true);
    m_agreementContent->setSource(QUrl("qrc:/html/html/openKylin操作系统AI功能使用协议250228-0318.html"));
    m_agreementScroll = new QScrollArea(this);
    m_agreementScroll->setAccessibleName("kylin-aiassistant_QScrollArea_agreementScroll");
    m_disagreeBtn = new QPushButton(tr("disagree"), this);
    m_disagreeBtn->setAccessibleName("kylin-aiassistant_QPushButton_m_disagreeBtn");
    m_agreeBtn = new QPushButton(tr("agree and continue"), this);
    m_agreeBtn->setAccessibleName("kylin-aiassistant_QPushButton_m_agreeBtn");
    m_useNowBtn = new QPushButton(tr("try"), this);
    m_useNowBtn->setAccessibleName("kylin-aiassistant_QPushButton_m_useNowBtn");

}



void IntroductionContent::initLayout()
{
    this->setFocus();
    QFont font;
    font.setPixelSize(int(Globals::fontSize + 6));
    font.setBold(true);
    m_shortcut->setFont(font);
    m_addModel->setFont(font);
    m_agreementTitle->setFont(font);

    m_shortcut->setFixedWidth(334);
    m_shortcut->setWordWrap(true);
    m_addModel->setFixedWidth(334);
    m_addModel->setWordWrap(true);
    m_agreementTitle->setFixedWidth(334);
    m_agreementTitle->setWordWrap(true);
    m_shortcut->setAlignment(Qt::AlignCenter);
    m_addModel->setAlignment(Qt::AlignCenter);
    m_agreementTitle->setAlignment(Qt::AlignCenter);

    //协议页白框
    m_agreementInnerWid->setFixedSize(354,466);

    //协议页滚轮
    m_agreementContent->setFrameShape(QFrame::NoFrame);
    m_agreementScroll->setWidgetResizable(true);
    m_agreementScroll->setWidget(m_agreementContent);
    m_agreementScroll->setFrameShape(QFrame::NoFrame);

    QVBoxLayout *innerLayout = new QVBoxLayout;
    innerLayout->setContentsMargins(12, 8, 0, 0);
    innerLayout->addWidget(m_agreementScroll);
    m_agreementInnerWid->setLayout(innerLayout);


    //设置总体的用户协议页面布局
    QVBoxLayout *agreementLayout = new QVBoxLayout;
    agreementLayout->setContentsMargins(0, 0, 16, 16);
    // 添加白色框区域（滚动区域）
    agreementLayout->addWidget(m_agreementInnerWid); // 白色滚动区域放在下方
    m_agreementPageWid->setLayout(agreementLayout); // 设置布局到用户协议容器
    m_agreementPageWid->setFixedSize(354,466);

    // 应用图标
    QPixmap pixmap(":/icon/icon/guide.svg");
    QPixmap scaledPixmap = pixmap.scaled(32, 32, Qt::KeepAspectRatio);
    m_icon->setFixedSize(334,32);
    m_icon->setPixmap(scaledPixmap);
    m_icon->setAlignment(Qt::AlignCenter); // 图片居中显示

    //winc快捷键介绍
    QLabel *introduceWinC = new QLabel(tr(" Quickly activate / hide AI assistant view"));
    introduceWinC->setAccessibleName("kylin-aiassistant_QLabel_introduceWinC");
    introduceWinC->setFixedWidth(220);
    introduceWinC->setWordWrap(true);
    QLabel *winC = new QLabel("Super + C");
    winC->setAccessibleName("kylin-aiassistant_QLabel_winC");
    winC->setFixedHeight(20);

    // 创建和加载SVG图片
    QLabel *imageLabelOne = new QLabel();
    QSvgRenderer *renderer = new QSvgRenderer(QString(":/icon/icon/1.svg"), imageLabelOne);
    // 获取SVG的默认大小（确保图像不被缩放）
    QSize svgSize = renderer->defaultSize();
    QPixmap pixmapImageLabel(svgSize);
    pixmapImageLabel.fill(Qt::transparent);  // 确保pixmap是透明的
    QPainter painter(&pixmapImageLabel);
    renderer->render(&painter);  // 将SVG渲染到pixmap上
    imageLabelOne->setPixmap(pixmapImageLabel);
    imageLabelOne->setFixedSize(svgSize);  // 设置Label大小为SVG的原始尺寸

    //winc快捷键图片
    QPixmap pic(":/icon/icon/super-c.svg");
    QLabel * winCImage = new QLabel;
    winCImage->setAccessibleName("kylin-aiassistant_QLabel_winCImage");

    winCImage->setFixedSize(322, 96);
    winCImage->setPixmap(pic);
    winCImage->setAlignment(Qt::AlignCenter);

    // 创建和加载SVG图片
    QLabel *imageLabelTwo = new QLabel();
    QSvgRenderer *rendererTwo = new QSvgRenderer(QString(":/icon/icon/2.svg"), imageLabelTwo);
    // 获取SVG的默认大小（确保图像不被缩放）
    QSize svgSizeTwo = rendererTwo->defaultSize();
    QPixmap pixmapImageLabelTwo(svgSize);
    pixmapImageLabelTwo.fill(Qt::transparent);  // 确保pixmap是透明的
    QPainter painterTwo(&pixmapImageLabelTwo);
    rendererTwo->render(&painterTwo);  // 将SVG渲染到pixmap上
    imageLabelTwo->setPixmap(pixmapImageLabelTwo);
    imageLabelTwo->setFixedSize(svgSizeTwo);  // 设置Label大小为SVG的原始尺寸


    //winaltc快捷键介绍
    QLabel *introduceWinAltC = new QLabel(tr(" After selecting the text, wake / hide the floating bar"));
    introduceWinAltC->setAccessibleName("kylin-aiassistant_QLabel_introduceWinAltC");
    introduceWinAltC->setFixedWidth(220);
    introduceWinAltC->setWordWrap(true);

    QLabel *winAltC = new QLabel("Super + Alt + C");
    winAltC->setAccessibleName("kylin-aiassistant_QLabel_winAltC");
    winAltC->setFixedHeight(20);

    //winaltc快捷键图片
    QPixmap pic2(":/icon/icon/super-alt-c.svg");
    QLabel * winAltCImage = new QLabel;
    winAltCImage->setAccessibleName("kylin-aiassistant_QLabel_winAltCImage");
    winAltCImage->setFixedSize(322, 96);
    winAltCImage->setPixmap(pic2);
    winAltCImage->setAlignment(Qt::AlignCenter);

    QVBoxLayout *winAltCIntroLayout = new QVBoxLayout;
    winAltCIntroLayout->setContentsMargins(16, 20, 16, 20);
    winAltCIntroLayout->setSpacing(8);
    winAltCIntroLayout->addSpacing(18);
    winAltCIntroLayout->addWidget(imageLabelOne);  // 添加SVG图片
    winAltCIntroLayout->addWidget(introduceWinC);
    winAltCIntroLayout->addWidget(winC);
    winAltCIntroLayout->addSpacing(8);
    winAltCIntroLayout->addWidget(winCImage);
    winAltCIntroLayout->addSpacing(32);
    winAltCIntroLayout->addWidget(imageLabelTwo);  // 添加SVG图片
    winAltCIntroLayout->addWidget(introduceWinAltC);
    winAltCIntroLayout->addWidget(winAltC);
    winAltCIntroLayout->addSpacing(8);
    winAltCIntroLayout->addWidget(winAltCImage);

    m_winAltCWid->setFixedWidth(354);
    m_winAltCWid->setLayout(winAltCIntroLayout);


    //设置蓝色文字
    QPalette palette;
    palette.setColor(QPalette::Text, QColor(55,144,250,255));  // 设置字体颜色为蓝色
    QFont f;
    f.setBold(true);
    winC->setFont(f);
    winAltC->setFont(f);
    winC->setPalette(palette);
    winAltC->setPalette(palette);


    //添加模型提示文字
    setPageWid->setFixedSize(354,466);

    QString messageText;
    messageText = tr("Welcome to the AI Assistant!<br><br>Before first use, please select or configure an AI model.<br>We have prepared a <b>free trial account for you(Limited credit limit)</b>, which you can use immediately.<br>You can also enter the model settings and select other model schemes.<br><br>") + "<span style=\"color: rgba(0, 0, 0, 0.35)\">" + tr("Attention: Low performance devices running local models may be slower.") + "</span>";

    QLabel *setPageText = new QLabel();
    setPageText->setText(messageText);
    setPageText->setAccessibleName("kylin-aiassistant_QLabel_setPageText");
    setPageText->setWordWrap(true);
    setPageText->setTextFormat(Qt::RichText);
    QPixmap setPic(":/icon/icon/model-add.svg");
    QLabel * setPageImage = new QLabel;
    setPageImage->setAccessibleName("kylin-aiassistant_QLabel_setPageImage");

    setPageImage->setFixedSize(322, 144);
    setPageImage->setPixmap(setPic);
    setPageImage->setAlignment(Qt::AlignCenter);


    QVBoxLayout *setPageLayout = new QVBoxLayout;
    setPageLayout->setMargin(0);
    setPageLayout->setContentsMargins(16,16,16,16);
    setPageLayout->addWidget(setPageText);
    setPageLayout->addSpacing(36+40);
    setPageLayout->addWidget(setPageImage);
    setPageLayout->addStretch();
    setPageWid->setLayout(setPageLayout);

    //下一页
    m_nextBtn->setProperty("isImportant", true);
    m_useNowBtn->setProperty("isImportant", true);
    m_agreeBtn->setProperty("isImportant", true);

    // 初始化按钮尺寸
    updateButtonSizes();
    updateAgreementFontSize();

    //底部按钮区
    QHBoxLayout * footLayout = new QHBoxLayout;
    footLayout->setContentsMargins(20, 0, 14, 20);
    footLayout->addWidget(m_acceptBtn);
    footLayout->addStretch();
    footLayout->addWidget(m_previousBtn);
    footLayout->addWidget(m_nextBtn);
    footLayout->addWidget(m_useNowBtn);

    QWidget * footWid = new QWidget;
    footWid->setAccessibleName("kylin-aiassistant_QWidget_footWid");
    footWid->setLayout(footLayout);

    //内容布局
    QVBoxLayout * contentLayout = new QVBoxLayout;
    contentLayout->setContentsMargins(20, 0, 20, 0);
    contentLayout->setSpacing(8);
    contentLayout->addWidget(m_icon);
    contentLayout->addWidget(m_agreementTitle);
    contentLayout->addWidget(m_shortcut);
    contentLayout->addWidget(m_addModel);

    // 声明
    contentLayout->addSpacing(6);
    contentLayout->addWidget(m_agreementPageWid);
    //快捷键文字
    contentLayout->addWidget(m_winAltCWid);
    //添加模型文字
    contentLayout->addWidget(setPageWid);
    contentLayout->addStretch();
    contentLayout->setAlignment(Qt::AlignTop);

    showFirst();

    m_agreementPageWid->setVisible(false);

    //内容窗口
    contentWid = new QWidget;
    footWid->setAccessibleName("kylin-aiassistant_QWidget_contentWid");
    contentWid->setFixedWidth(376);
    contentWid->setLayout(contentLayout);


    //滚动区域
    QScrollArea * scroll = new QScrollArea;
    scroll->setAccessibleName("kylin-aiassistant_QScrollArea_scroll");
    scroll->setFixedSize(376,564);
    scroll->setWidgetResizable(true);
    scroll->setWidget(contentWid);

    // 设置滚动条策略
    scroll->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);  // 隐藏水平滚动条
    scroll->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);    // 垂直滚动条只在需要时显示

    //设置背景透明
    QPalette pa = scroll->palette();
    pa.setBrush(QPalette::Window, Qt::transparent);
    scroll->setPalette(pa);
    scroll->setFrameShape(QFrame::NoFrame);
    //设置滑动条透明
    scroll->verticalScrollBar()->setProperty("drawScrollBarGroove", false);

    //主要布局
    QVBoxLayout *agreementTextLayout = new QVBoxLayout;
     agreementTextLayout->setContentsMargins(16,4,16,16);
     agreementTextLayout->setSpacing(8);
     agreementTextLayout->addWidget(m_agreementContent);
     agreementTextLayout->setSpacing(8);
     m_agreementInnerWid->setLayout(agreementTextLayout);

    QVBoxLayout * mainLayout = new QVBoxLayout(this);
    mainLayout->setMargin(0);
    mainLayout->addWidget(scroll);
    mainLayout->addWidget(footWid);

    // 同时将用户协议页的按钮添加到底部按钮区域（footLayout）
    footLayout->addWidget(m_disagreeBtn);
    footLayout->addWidget(m_agreeBtn);
    // 初始先设为隐藏
    m_disagreeBtn->setVisible(false);
    m_agreeBtn->setVisible(false);

}



void IntroductionContent::initConnection()
{
    connect(m_useNowBtn, &QPushButton::clicked, [this](){
        if(m_agreementPageWid->isVisible()){
            g_configManager->updateConfig("is-first-install", true);
            exit(0);
        } else {
            IntroductionView *father = reinterpret_cast<IntroductionView *>(parent());
            father->closeView();
            Q_EMIT father->showMainQmlPage();
        }
    });

    connect(m_nextBtn, &QPushButton::clicked, this, &IntroductionContent::showSecond);
    connect(m_previousBtn, &QPushButton::clicked, this, &IntroductionContent::showFirst);
    connect(m_acceptBtn, &QPushButton::clicked, [&](){
        IntroductionView * father = reinterpret_cast<IntroductionView *>(parent());
        father->closeView();
        Q_EMIT father->openUKUIControlCenterView();
    });

    connect(g_ukuiGettings, &QGSettings::changed, [&](const QString &key){
        if(key == "systemFontSize"){
            QFont font;
            Globals::fontSize = g_ukuiGettings->get("system-font-size").toFloat();
            font.setBold(true);
            font.setPixelSize(int(Globals::fontSize + 6));
            m_shortcut->setFont(font);
            m_addModel->setFont(font);
            m_agreementTitle->setFont(font);
            // 更新按钮尺寸以适应新的字体大小
            updateButtonSizes();
            updateAgreementFontSize();
        }
    });
    connect(m_disagreeBtn, &QPushButton::clicked, this, &IntroductionContent::onDisagreeClicked);
    connect(m_agreeBtn, &QPushButton::clicked, this, &IntroductionContent::onAgreeClicked);
}




void IntroductionContent::showFirst()
{
    this->setFocus();
    m_agreementTitle->setVisible(false);
    m_agreementPageWid->setVisible(false);
    m_disagreeBtn->setVisible(false);
    m_agreeBtn->setVisible(false);
    m_shortcut->setVisible(true);
    m_winAltCWid->setVisible(true);
    m_addModel->setVisible(false);
    setPageWid->setVisible(false);
    m_nextBtn->setVisible(true);
    m_previousBtn->setVisible(false);
    m_acceptBtn->setVisible(false);
    m_useNowBtn->setVisible(false);

    m_acceptBtn->setDefault(false);
    m_agreeBtn->setDefault(false);
    m_nextBtn->setDefault(true);

}

void IntroductionContent::showSecond()
{
    this->setFocus();
    m_agreementTitle->setVisible(false);
    m_agreementPageWid->setVisible(false);
    m_disagreeBtn->setVisible(false);
    m_agreeBtn->setVisible(false);
    m_shortcut->setVisible(false);
    m_winAltCWid->setVisible(false);
    m_addModel->setVisible(true);
    setPageWid->setVisible(true);
    m_nextBtn->setVisible(false);
    m_previousBtn->setVisible(true);
    m_acceptBtn->setVisible(true);
    m_useNowBtn->setVisible(true);

    m_agreeBtn->setDefault(false);
    m_nextBtn->setDefault(false);
    m_acceptBtn->setDefault(true);
}

void IntroductionContent::showAgreement()
{
    // 隐藏其他页面控件
    m_shortcut->setVisible(false);
    m_winAltCWid->setVisible(false);
    m_addModel->setVisible(false);
    setPageWid->setVisible(false);
    m_nextBtn->setVisible(false);
    m_previousBtn->setVisible(false);
    m_acceptBtn->setVisible(false);
    m_useNowBtn->setVisible(false);

    // 显示用户协议页面和按钮
    m_agreementTitle->setVisible(true);
    m_agreementPageWid->setVisible(true);
    m_disagreeBtn->setVisible(true);
    m_agreeBtn->setVisible(true);

    m_nextBtn->setDefault(false);
    m_acceptBtn->setDefault(false);
    m_agreeBtn->setDefault(true);
}

void IntroductionContent::onDisagreeClicked()
{
    g_configManager->updateConfig("is-first-install", true);
    // 用户选择不同意，则关闭向导
    QCoreApplication::quit();
    QTimer::singleShot(0,QCoreApplication::quit);
}

void IntroductionContent::onAgreeClicked()
{
    g_configManager->updateConfig("is-first-install", false);
    // 用户同意后，显示原先的第一页（快捷键介绍）
    showFirst();
}

bool IntroductionContent::hasLocalModel()
{
    ModelConfig mc;
    const auto models = mc.getExtendedModels(DeployType::Ondevice);
    KyInfo() << "Determine whether the local model is available through getExtendedModels，get local models size:" << models.size();
    if (models.size() == 0) {
        return false;
    } else {
        return true;
    }
}

void IntroductionContent::updateButtonSizes()
{
    // 获取当前字体尺寸
    QFont font;
    font.setPixelSize(int(Globals::fontSize));

    // 计算按钮文字的最小宽度
    QFontMetrics fm(font);

    // 为每个按钮计算合适的宽度，增加一些边距
    int padding = 48; // 左右边距总和
    int minWidth = 88; // 最小宽度

    // 下一页按钮
    int nextWidth = qMax(minWidth, fm.horizontalAdvance(m_nextBtn->text()) + padding);
    m_nextBtn->setFixedSize(nextWidth, 36);

    // 上一页按钮
    int prevWidth = qMax(minWidth, fm.horizontalAdvance(m_previousBtn->text()) + padding);
    m_previousBtn->setFixedSize(prevWidth, 36);

    // 立即使用按钮
    int useNowWidth = qMax(minWidth, fm.horizontalAdvance(m_useNowBtn->text()) + padding);
    m_useNowBtn->setFixedSize(useNowWidth, 36);

    // 配置模型按钮
    int acceptWidth = qMax(minWidth, fm.horizontalAdvance(m_acceptBtn->text()) + padding);
    m_acceptBtn->setFixedSize(acceptWidth, 36);

    // 不同意按钮
    int disagreeWidth = qMax(minWidth, fm.horizontalAdvance(m_disagreeBtn->text()) + padding);
    m_disagreeBtn->setFixedSize(disagreeWidth, 36);

    // 同意并继续按钮
    int agreeWidth = qMax(minWidth, fm.horizontalAdvance(m_agreeBtn->text()) + padding);
    m_agreeBtn->setFixedSize(agreeWidth, 36);
}

void IntroductionContent::updateAgreementFontSize()
{
    // 计算基于当前系统字体大小的HTML字体大小
    int baseFontSize = int(Globals::fontSize);
    // 让HTML字体更明显地跟随系统字体变化
    // 使用更直接的比例，让字体变化更明显
    int htmlFontSize = qMax(10, baseFontSize); // 直接使用系统字体大小，但不小于10px

    qDebug() << "updateAgreementFontSize called, baseFontSize:" << baseFontSize << "htmlFontSize:" << htmlFontSize;

    // 直接设置QTextBrowser的字体
    QFont browserFont;
    browserFont.setPixelSize(htmlFontSize);
    m_agreementContent->setFont(browserFont);

    // 通过QTextDocument设置默认字体
    QTextDocument* doc = m_agreementContent->document();
    if (doc) {
        QFont docFont;
        docFont.setPixelSize(htmlFontSize);
        doc->setDefaultFont(docFont);
    }

    // 重新加载HTML文件以应用新的字体设置
    m_agreementContent->setSource(QUrl("qrc:/html/html/openKylin操作系统AI功能使用协议250228-0318.html"));

    // 再次设置字体确保生效
    m_agreementContent->setFont(browserFont);
    if (doc) {
        QFont docFont;
        docFont.setPixelSize(htmlFontSize);
        doc->setDefaultFont(docFont);
    }

    // 调试输出：检查字体是否设置成功
    QFont currentFont = m_agreementContent->font();
    qDebug() << "QTextBrowser font size after update:" << currentFont.pixelSize();

    // 强制更新显示
    m_agreementContent->update();
    m_agreementContent->repaint();
}

InnerWidget::InnerWidget(QWidget *parent)
{
    QPalette pale;
    pale.setColor(QPalette::Base, QColor("#F5F5F5"));
    this->setPalette(pale);
}

void InnerWidget::resizeEvent(QResizeEvent *event)
{
    Q_UNUSED(event);
    QPainterPath path;
    path.addRoundedRect(rect(), 6, 6);
    // 将路径转换为多边形，并设置为控件的 mask
    setMask(QRegion(path.toFillPolygon().toPolygon()));
    QWidget::resizeEvent(event);

}
void InnerWidget::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event);
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);

    // 将圆角半径由 12 改为 8
    QPainterPath path;
    path.addRoundedRect(rect(), 6, 6);

    painter.setClipPath(path);
    QColor bgColor = this->palette().color(QPalette::Base);
    painter.fillPath(path, bgColor);
}
