Newer
Older
AutoCalibrationXC / frame / CustomMessageBox.cpp
tanyue 27 days ago 5 KB 20250717 初始提交
#include "CustomMessageBox.h"
#include "ui_CustomMessageBox.h"

static CustomMessageBox * self;

CustomMessageBox::CustomMessageBox(QWidget *parent, MSG_TYPE nType, MSG_TIP_TYPE nTipType) :
    QDialog(parent),
    ui(new Ui::CustomMessageBox)
{
    ui->setupUi(this);

    m_nType = nType;
    m_nTipType = nTipType;
    t1 = nullptr;

    setWindowFlags(Qt::Tool | Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);
    // 设置窗体关闭时自动释放内存
    this->setAttribute(Qt::WA_DeleteOnClose);

    initForm();
    initButtons();
    initIcons();
}

CustomMessageBox::~CustomMessageBox()
{
    if (t1) delete t1;
    delete ui;
}

int CustomMessageBox::success(QWidget * parent, const QString subTitle, const QString text)
{
    self = new CustomMessageBox(parent, MSG_TYPE::SA_OKS, MSG_TIP_TYPE::SA_SUCCESS);
    self->setSubTitle(subTitle);
    self->setTips(text);
    self->setModal(true);
    return self->exec();
}
int CustomMessageBox::failure(QWidget *parent, const QString subTitle, const QString text)
{
    self = new CustomMessageBox(parent, MSG_TYPE::SA_OKS, MSG_TIP_TYPE::SA_FAILED);
    self->setSubTitle(subTitle);
    self->setTips(text);
    self->setModal(true);
    return self->exec();
}
int CustomMessageBox::warning(QWidget *parent, const QString subTitle, const QString text)
{
    self = new CustomMessageBox(parent, MSG_TYPE::SA_OKS, MSG_TIP_TYPE::SA_WARNING);
    self->setSubTitle(subTitle);
    self->setTips(text);
    self->setModal(true);
    return self->exec();
}
int CustomMessageBox::tips(QWidget *parent, const QString subTitle, const QString text)
{
    self = new CustomMessageBox(parent, MSG_TYPE::SA_OKS, MSG_TIP_TYPE::SA_TIPS);
    self->setSubTitle(subTitle);
    self->setTips(text);
    self->setModal(true);
    return self->exec();
}
int CustomMessageBox::confirm(QWidget *parent, const QString subTitle, const QString text)
{
    self = new CustomMessageBox(parent, MSG_TYPE::SA_OKCANCEL, MSG_TIP_TYPE::SA_QUESTION);
    self->setSubTitle(subTitle);
    self->setTips(text);
    self->setModal(true);
    return self->exec();
}

void CustomMessageBox::initForm()
{
    // 加载样式表
    QString qssStyle = QssFileUtils::loadQssFileContent(":/qss/messageBox.css");
    if (qssStyle.isEmpty() == false) {
        this->setStyleSheet(qssStyle); // 仅本窗口生效
    }

    ui->btnConfirm->setProperty("class", "btnDefault");
    ui->btnCancel->setProperty("class", "btnOpposite");

    ui->labelSubTitle->setProperty("class", "subTitle");

    // 添加需要显示遮罩层的Dialog弹窗对象名
    QStringList maskNames = MaskWidget::instance()->names();
    if (maskNames.contains("CustomMessageBox") == false) {
        maskNames << "CustomMessageBox";
    }
    MaskWidget::instance()->setNames(maskNames);
}

void CustomMessageBox::initButtons()
{
    // 设置按钮显示
    switch (m_nType) {
    case MSG_TYPE::SA_OK:
        ui->btnCancel->setVisible(false);  // 隐藏“否”按钮
        ui->btnConfirm->setText("确定");
        break;

    case MSG_TYPE::SA_OKCANCEL:
        ui->btnCancel->setVisible(true);   // 显示“否”按钮
        ui->btnConfirm->setText("是");
        break;

    case MSG_TYPE::SA_OKS:
        ui->btnCancel->setVisible(false);  // 隐藏“否”按钮
        ui->btnConfirm->setText("确定");
        break;
    }
}
void CustomMessageBox::initIcons()
{
    iconList.append(QPixmap(":/image/msgbox/icon_box_success.png"));
    iconList.append(QPixmap(":/image/msgbox/icon_box_fail.png"));
    iconList.append(QPixmap(":/image/msgbox/icon_box_warning.png"));
    iconList.append(QPixmap(":/image/msgbox/icon_box_info.png"));
    iconList.append(QPixmap(":/image/msgbox/icon_box_confirm.png"));

    ui->labelIcon->setPixmap(iconList.at(m_nTipType));

    // 暂时隐藏
    ui->wdgtInput->hide();
}

void CustomMessageBox::setTips(const QString strTips)
{
    ui->labelText->setText(strTips);
}
void CustomMessageBox::setSubTitle(const QString subTitle)
{
    ui->labelSubTitle->setText(subTitle);
}

void CustomMessageBox::_startTimerClose(int sec)
{
    if (t1) {
        delete t1;
        t1 = nullptr;
    }

    // 定义线程
    t1 = new std::thread(&CustomMessageBox::ticktack, this, sec);
    t1->detach();
}

void CustomMessageBox::ticktack(int sec)
{
    int secTicktack = 0;
    while (false == this->isHidden()) {     // 如果窗体是显示的

        // 判断秒数相等,则关闭窗体
        if (secTicktack == sec) {
            done(1);
            this->close();
            break;
        }

        sleep(1);   // Sleep(1);
        secTicktack++;
        printf("%d   -   ticktack = %d\n", sec, secTicktack);
    }
}

void CustomMessageBox::on_btnClose_clicked()
{
    // .exec()结束后返回0
    done(0);
    this->close();
}

void CustomMessageBox::on_btnConfirm_clicked()
{
    // .exec()结束后返回1
    done(1);
    this->close();
}

void CustomMessageBox::on_btnCancel_clicked()
{
    // .exec()结束后返回0
    done(0);
    this->close();
}

void CustomMessageBox::mouseMoveEvent(QMouseEvent *event)
{
    // 如果鼠标是按下的
    if(m_bMousePressed)
    {
        // 移动窗体
        move(event->pos() - m_pMousePoint + pos());
    }
}

void CustomMessageBox::mousePressEvent(QMouseEvent *event)
{
    // 鼠标左键按下
    if(event->button() == Qt::LeftButton)
    {
        // 标志设置true
        m_bMousePressed = true;
        // 获取当前窗体位置
        m_pMousePoint = event->pos();
    }
}

void CustomMessageBox::mouseReleaseEvent(QMouseEvent *event)
{

    // 鼠标左键释放(松开)
    if(event->button() == Qt::LeftButton) {
        // 鼠标松开,标志设在false
        m_bMousePressed = false;
    }

}

void CustomMessageBox::showEvent(QShowEvent *)
{
    // 判断窗体类型,启动线程计时关闭窗体
    if(MSG_TYPE::SA_OK == m_nType) {
        _startTimerClose(2);
    }
}