自封装CMessageBox(app)

#include <QLabel>
#include <QPushButton>
#include <QMessageBox>
#include <QCheckBox>
#include <QHBoxLayout>
#include <QEvent>
#include <QApplication>
#include "StyleWidget/titlebar.h"
#include "cmessagebox.h"
#include "qdebug.h"
#include "qmath.h"
#include "qpainter.h"
#include "qpainterpath.h"

QMessageBox::StandardButton CMessageBox::information(QWidget *parent, const QString &title,
                                                       const QString &text, QMessageBox::StandardButtons buttons,
                                                       QMessageBox::StandardButton defaultButton)
{
    CMessageBox((QDialog *)parent, title, text, buttons, defaultButton);
    msgBox.setIcon(":/PNG/png/messagebox_information.png");
    if (msgBox.exec() == -1)
        return QMessageBox::Cancel;
    return msgBox.standardButton(msgBox.clickedButton());
}

QMessageBox::StandardButton CMessageBox::question(QWidget *parent, const QString &title,
                                                    const QString &text, QMessageBox::StandardButtons buttons,
                                                    QMessageBox::StandardButton defaultButton)
{
    CMessageBox msgBox((QDialog *)parent, title, text, buttons, defaultButton);
    msgBox.setIcon(":/PNG/png/messagebox_question.png");
    if (msgBox.exec() == -1)
        return QMessageBox::Cancel;
    return msgBox.standardButton(msgBox.clickedButton());
}

QMessageBox::StandardButton CMessageBox::warning(QWidget *parent, const QString &title,
                                                   const QString &text, QMessageBox::StandardButtons buttons,
                                                   QMessageBox::StandardButton defaultButton)
{
    CMessageBoxmsgBox((QDialog *)parent, title, text, buttons, defaultButton);
    msgBox.setIcon(":/PNG/png/messagebox_warning.png");
    if (msgBox.exec() == -1)
        return QMessageBox::Cancel;
    return msgBox.standardButton(msgBox.clickedButton());
}

QMessageBox::StandardButton CMessageBox::critical(QWidget *parent, const QString &title,
                                                    const QString &text, QMessageBox::StandardButtons buttons,
                                                    QMessageBox::StandardButton defaultButton)
{
    CMessageBoxmsgBox((QDialog *)parent, title, text, buttons, defaultButton);
    msgBox.setIcon(":/PNG/png/messagebox_critical.png");
    if (msgBox.exec() == -1)
        return QMessageBox::Cancel;
    return msgBox.standardButton(msgBox.clickedButton());
}

QMessageBox::StandardButton CMessageBox::success(QWidget *parent, const QString &title, const QString &text, QMessageBox::StandardButtons buttons, QMessageBox::StandardButton defaultButton)
{
    CMessageBoxmsgBox((QDialog *)parent, title, text, buttons, defaultButton);
    msgBox.setIcon(":/PNG/png/messagebox_success.png");
    if (msgBox.exec() == -1)
        return QMessageBox::Cancel;
    return msgBox.standardButton(msgBox.clickedButton());
}

int CMessageBox::information(QWidget *parent, const QString &title, const QString &text, QString button0, QString button1, QString button2)
{
    CMessageBoxmsgBox((QDialog *)parent, title, text, button0, button1, button2);
    msgBox.setIcon(":/PNG/png/messagebox_information.png");
    int ret = msgBox.exec();
    if (ret == -1)
        return QMessageBox::Cancel;
    return ret;
}

int CMessageBox::question(QWidget *parent, const QString &title, const QString &text, QString button0, QString button1, QString button2)
{
    CMessageBoxmsgBox((QDialog *)parent, title, text, button0, button1, button2);
    msgBox.setIcon(":/PNG/png/messagebox_question.png");
    int ret = msgBox.exec();
    if (ret == -1)
        return QMessageBox::Cancel;
    return ret;
}

int CMessageBox::warning(QWidget *parent, const QString &title, const QString &text, QString text0, QString text1, QString text2)
{
    CMessageBoxmsgBox((QDialog *)parent, title, text, text0, text1, text2);
    msgBox.setIcon(":/PNG/png/messagebox_warning.png");
    int ret = msgBox.exec();
    if (ret == -1)
        return QMessageBox::Cancel;
    return ret;
}

int CMessageBox::critical(QWidget *parent, const QString &title, const QString &text, QString button0, QString button1, QString button2)
{
    CMessageBoxmsgBox((QDialog *)parent, title, text, button0, button1, button2);
    msgBox.setIcon(":/PNG/png/messagebox_critical.png");
    int ret = msgBox.exec();
    if (ret == -1)
        return QMessageBox::Cancel;
    return ret;
}

int CMessageBox::success(QWidget *parent, const QString &title, const QString &text, QString button0, QString button1, QString button2)
{
    CMessageBoxmsgBox((QDialog *)parent, title, text, button0, button1, button2);
    msgBox.setIcon(":/PNG/png/messagebox_success.png");
    int ret = msgBox.exec();
    if (ret == -1)
        return QMessageBox::Cancel;
    return ret;
}

CMessageBox::CMessageBox()
{
    setMinimumSize(300, 150);
    m_pButtonBox = new QDialogButtonBox(this);

    initUI("");
}

CMessageBox::CMessageBox(QWidget *parent, bool): QDialog(parent)
{
    setMinimumSize(300, 150);
    m_pButtonBox = new QDialogButtonBox(this);

    initUI("");
}

CMessageBox::CMessageBox(QDialog *parent, const QString &title, const QString &text,
                             QMessageBox::StandardButtons buttons, QMessageBox::StandardButton defaultButton)
    : QDialog(parent)
{
    setWindowTitle(title);
    setMinimumSize(300, 150);
    m_pButtonBox = new QDialogButtonBox(this);
    m_pButtonBox->setStandardButtons(QDialogButtonBox::StandardButtons(int(buttons)));
    setDefaultButton(defaultButton);

    QPushButton *pYesButton = m_pButtonBox->button(QDialogButtonBox::Yes);
    if (pYesButton != NULL)
    {
        pYesButton->setObjectName("blueButton");
        pYesButton->setStyle(QApplication::style());
    }

    initUI(text);
}

CMessageBox::CMessageBox(QDialog *parent, const QString &title, const QString &text, QString button0, QString button1, QString button2)
    : QDialog(parent)
{
    m_isTextByText = true;
    setWindowTitle(title);
    setMinimumSize(300, 150);
    m_pButtonBox = new QDialogButtonBox(this);
    QDialogButtonBox::StandardButtons buttons = QDialogButtonBox::StandardButton::NoButton;
    if (button0.isEmpty())
        button0 = QDialogButtonBox::tr("OK");
    if (!button0.isEmpty())
    {
        buttons = buttons | QDialogButtonBox::StandardButton::Save;
    }
    if (!button1.isEmpty())
    {
        buttons = buttons | QDialogButtonBox::StandardButton::SaveAll;
    }
    if (!button2.isEmpty())
    {
        buttons = buttons | QDialogButtonBox::StandardButton::Cancel;
    }

    m_pButtonBox->setStandardButtons(buttons);

    if (!button0.isEmpty())
    {
        m_pButtonBox->button(QDialogButtonBox::StandardButton::Save)->setText(button0);
        m_pButtonBox->button(QDialogButtonBox::StandardButton::Save)->setObjectName("button0");
    }
    if (!button1.isEmpty())
    {
        m_pButtonBox->button(QDialogButtonBox::StandardButton::SaveAll)->setText(button1);
        m_pButtonBox->button(QDialogButtonBox::StandardButton::SaveAll)->setObjectName("button1");
    }
    if (!button2.isEmpty())
    {
        m_pButtonBox->button(QDialogButtonBox::StandardButton::Cancel)->setText(button2);
        m_pButtonBox->button(QDialogButtonBox::StandardButton::Cancel)->setObjectName("button2");
    }

    initUI(text);
}

CMessageBox::~CMessageBox()
{
}

void CMessageBox::changeEvent(QEvent *event)
{
    switch (event->type())
    {
    case QEvent::LanguageChange:
        translateUI();
        break;
    default:
        QDialog::changeEvent(event);
    }
}

void CMessageBox::closeEvent(QCloseEvent *event)
{
    Q_UNUSED(event);
}

void CMessageBox::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event);
}


void CMessageBox::initUI(QString text)
{
    m_pIconLabel = new QLabel(this);
    m_pLabel = new QLabel(this);

    QPixmap pixmap(":/PNG/png/messagebox_information.png");
    m_pIconLabel->setPixmap(pixmap);
    m_pIconLabel->setFixedSize(24, 24);
    m_pIconLabel->setScaledContents(true);

    m_pLabel->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
    m_pLabel->setObjectName("CMessageBoxlabel");
    m_pLabel->setOpenExternalLinks(true);
    m_pLabel->setText(text);

    m_pGridLayout = new QGridLayout();
    QHBoxLayout *VBoxLayout = new QHBoxLayout();
    VBoxLayout->addWidget(m_pIconLabel);
    VBoxLayout->addWidget(m_pLabel);
    m_pGridLayout->addLayout(VBoxLayout, 0, 0, 2, m_pGridLayout->columnCount(), Qt::AlignHCenter);
    //    m_pGridLayout->addWidget(m_pIconLabel, 0, 0, 2, 1, Qt::AlignVCenter);
    //    m_pGridLayout->addWidget(m_pLabel, 0, 1, 2, 1, Qt::AlignVCenter);
    m_pGridLayout->addWidget(m_pButtonBox, m_pGridLayout->rowCount(), 0, 1, m_pGridLayout->columnCount(), Qt::AlignHCenter);
    m_pGridLayout->setSizeConstraint(QLayout::SetNoConstraint);
    m_pGridLayout->setHorizontalSpacing(10);
    m_pGridLayout->setVerticalSpacing(10);
    m_pGridLayout->setContentsMargins(10, 10, 10, 10);
    m_pLayout = new QVBoxLayout(this);
    m_pLayout->addLayout(m_pGridLayout);

    translateUI();

    connect(m_pButtonBox, SIGNAL(clicked(QAbstractButton *)), this, SLOT(onButtonClicked(QAbstractButton *)));

    this->setWindowFlags(Qt::FramelessWindowHint | windowFlags());
    TitleBar *pTitleBar = new TitleBar(this);
    pTitleBar->setTitle(QString(this->windowTitle()));
    pTitleBar->setMiniButtonHide(true);
    pTitleBar->setMaxiButtonHide(true);
    pTitleBar->setCloseButtonHide(true);
    pTitleBar->setIconHide(true);
    this->installEventFilter(pTitleBar);
    m_pLayout->insertWidget(0, pTitleBar);
    m_pLayout->setContentsMargins(3, 3, 3, 9);
    this->setLayout(m_pLayout);
}

QMessageBox::StandardButton CMessageBox::standardButton(QAbstractButton *button) const
{
    return (QMessageBox::StandardButton)m_pButtonBox->standardButton(button);
}

void CMessageBox::setStandardButtons(QDialogButtonBox::StandardButtons buttons)
{
    m_pButtonBox->setStandardButtons(QDialogButtonBox::StandardButtons(int(buttons)));
}

void CMessageBox::setStandardButtons(QMessageBox::StandardButtons buttons)
{
    m_pButtonBox->setStandardButtons(QDialogButtonBox::StandardButtons(int(buttons)));
}

QAbstractButton *CMessageBox::clickedButton() const
{
    return m_pClickedButton;
}

int CMessageBox::execReturnCode(QAbstractButton *button)
{
    int nResult = m_pButtonBox->standardButton(button);
    return nResult;
}

void CMessageBox::onButtonClicked(QAbstractButton *button)
{
    int nResult = -1;
    if (m_isTextByText)
    {
        if (button->objectName() == "button0")
        {
            nResult = 0;
        }
        else if (button->objectName() == "button1")
        {
            nResult = 1;
        }
        else if (button->objectName() == "button2")
        {
            nResult = 2;
        }
        done(nResult);
    }
    else
    {
        m_pClickedButton = button;
        done(execReturnCode(button));
    }
}

void CMessageBox::setDefaultButton(QPushButton *button)
{
    if (!m_pButtonBox->buttons().contains(button))
        return;
    m_pDefaultButton = button;
    button->setDefault(true);
    button->setFocus();
}

void CMessageBox::setDefaultButton(QMessageBox::StandardButton button)
{
    setDefaultButton(m_pButtonBox->button(QDialogButtonBox::StandardButton(button)));
}

void CMessageBox::setTitle(const QString &title)
{
    setWindowTitle(title);
}

void CMessageBox::setMessageWindowIcon(QIcon icon)
{
    setWindowIcon(icon);
}

void CMessageBox::setText(const QString &text)
{
    m_pLabel->setText(text);
}

void CMessageBox::setIcon(const QString &icon)
{
    m_pIconLabel->setPixmap(QPixmap(icon));
}

void CMessageBox::addWidget(QWidget *pWidget)
{
    m_pLabel->hide();
    m_pGridLayout->addWidget(pWidget, 0, 1, 2, 1);
}

int CMessageBox::information()
{
    m_pIconLabel->setPixmap(QPixmap(":/PNG/png/messagebox_information.png"));
    return exec();
}

int CMessageBox::question()
{
    m_pIconLabel->setPixmap(QPixmap(":/PNG/png/messagebox_question.png"));
    return exec();
}

int CMessageBox::warning()
{
    m_pIconLabel->setPixmap(QPixmap(":/PNG/png/messagebox_warning.png"));
    return exec();
}

int CMessageBox::critical()
{
    m_pIconLabel->setPixmap(QPixmap(":/PNG/png/messagebox_critical.png"));
    return exec();
}

int CMessageBox::success()
{
    m_pIconLabel->setPixmap(QPixmap(":/PNG/png/messagebox_success.png"));
    return exec();
}

QPushButton *CMessageBox::button(QDialogButtonBox::StandardButton which)
{
    return m_pButtonBox->button(which);
}

QPushButton *CMessageBox::button(QMessageBox::StandardButton which)
{
    return m_pButtonBox->button(QDialogButtonBox::StandardButton(which));
}

void CMessageBox::setButtonText(QMessageBox::StandardButton button, const QString &text)
{
    m_pButtonBox->button(QDialogButtonBox::StandardButton(button))->setText(text);
}

  • 7
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值