373 lines
11 KiB
C++
373 lines
11 KiB
C++
/**
|
||
* @file PaiMessageBox.cpp
|
||
* @brief 实现自定义的MessageBox对话框
|
||
* @date 2012-03-06
|
||
*/
|
||
#include <QLabel>
|
||
#include <QHBoxLayout>
|
||
#include <QSizePolicy>
|
||
#include <QSpacerItem>
|
||
#include <QCloseEvent>
|
||
|
||
#include "PaiMessageBox.h"
|
||
#include "PaiPushButton.h"
|
||
// #include "Log.h"
|
||
#include "PaiTextEdit.h"
|
||
#include "GlobalUtility.h"
|
||
|
||
using namespace pai;
|
||
using namespace pai::gui;
|
||
|
||
PaiMessageBox::PaiMessageBox(QWidget *pParent) :
|
||
PaiDialog(pParent)
|
||
{
|
||
InitDialog();
|
||
}
|
||
|
||
PaiMessageBox::PaiMessageBox(Icon icon,
|
||
const QString & title,
|
||
const QString & text,
|
||
StandardButtons buttons,
|
||
QWidget *pParent,
|
||
Qt::WindowFlags flag) :
|
||
PaiDialog(pParent, flag)
|
||
{
|
||
InitDialog();
|
||
|
||
SetIcon(icon);
|
||
setWindowTitle(title);
|
||
SetText(text);
|
||
SetStandardButtons(buttons);
|
||
}
|
||
|
||
PaiMessageBox::PaiMessageBox(Icon icon,
|
||
const QString & title,
|
||
const QString & text,
|
||
const QString & details,
|
||
StandardButtons buttons,
|
||
QWidget *pParent,
|
||
Qt::WindowFlags flag) :
|
||
PaiDialog(pParent, flag)
|
||
{
|
||
InitDialog();
|
||
|
||
SetIcon(icon);
|
||
setWindowTitle(title);
|
||
SetText(text);
|
||
SetDetailedText(details);
|
||
SetStandardButtons(buttons);
|
||
}
|
||
|
||
PaiMessageBox::~PaiMessageBox()
|
||
{
|
||
}
|
||
|
||
void PaiMessageBox::InitDialog()
|
||
{
|
||
m_pDetailsTEdit = NULL; // no details area by default
|
||
|
||
// standard button position
|
||
m_BtnPos << Help // left
|
||
<< Reset << Retry << RestoreDefaults << NoButton // separate left and right with this flag
|
||
<< Apply << Open << Ok << Yes << YesToAll << Save << SaveAll << No << NoToAll << Abort << Ignore << Discard
|
||
<< Close << Cancel; // right
|
||
|
||
// escape button list
|
||
m_EscapeBtns << Cancel << Close << Ignore << Abort << Discard << No;
|
||
|
||
m_BtnNames.insert(Ok, tr("OK"));
|
||
m_BtnNames.insert(Save, tr("Save"));
|
||
m_BtnNames.insert(SaveAll, tr("Save All"));
|
||
m_BtnNames.insert(Open, tr("Open"));
|
||
m_BtnNames.insert(Yes, tr("Yes"));
|
||
m_BtnNames.insert(YesToAll, tr("Yes to All"));
|
||
m_BtnNames.insert(No, tr("No"));
|
||
m_BtnNames.insert(NoToAll, tr("No to All"));
|
||
m_BtnNames.insert(Abort, tr("Abort"));
|
||
m_BtnNames.insert(Retry, tr("Retry"));
|
||
m_BtnNames.insert(Ignore, tr("Ignore"));
|
||
m_BtnNames.insert(Close, tr("Close"));
|
||
m_BtnNames.insert(Cancel, tr("Cancel"));
|
||
m_BtnNames.insert(Discard, tr("Discard"));
|
||
m_BtnNames.insert(Help, tr("Help"));
|
||
m_BtnNames.insert(Apply, tr("Apply"));
|
||
m_BtnNames.insert(Reset, tr("Reset"));
|
||
m_BtnNames.insert(RestoreDefaults, tr("Restore Defaults"));
|
||
|
||
m_pIconLabel = new QLabel(GetContainer());
|
||
QSizePolicy sizePolicy(QSizePolicy::Fixed, QSizePolicy::Preferred);
|
||
sizePolicy.setHorizontalStretch(0);
|
||
sizePolicy.setVerticalStretch(0);
|
||
sizePolicy.setHeightForWidth(m_pIconLabel->sizePolicy().hasHeightForWidth());
|
||
m_pIconLabel->setSizePolicy(sizePolicy);
|
||
m_pIconLabel->setAlignment(QFlags< Qt::AlignmentFlag > (Qt::AlignLeading | Qt::AlignLeft | Qt::AlignVCenter));
|
||
|
||
m_pTextLabel = new QLabel(GetContainer());
|
||
m_pTextLabel->setAlignment(QFlags< Qt::AlignmentFlag > (Qt::AlignLeading | Qt::AlignLeft | Qt::AlignVCenter));
|
||
m_pTextLabel->setTextFormat(Qt::AutoText);
|
||
m_pTextLabel->setWordWrap(true);
|
||
|
||
m_pBtnWgt = new QWidget(GetContainer());
|
||
|
||
QFrame *pLine = new QFrame(GetContainer());
|
||
pLine->setFrameShape(QFrame::HLine);
|
||
pLine->setFrameShadow(QFrame::Sunken);
|
||
pLine->setMinimumHeight(10);
|
||
|
||
QGridLayout *pGrid = new QGridLayout(GetContainer());
|
||
pGrid->addWidget(m_pIconLabel, 0, 0, 1, 1);
|
||
pGrid->addWidget(m_pTextLabel, 0, 1, 1, 1);
|
||
pGrid->addItem(new QSpacerItem(2, 4, QSizePolicy::Minimum, QSizePolicy::Expanding), 1, 0, 1, 2);
|
||
pGrid->addWidget(pLine, 2, 0, 1, 2);
|
||
pGrid->addWidget(m_pBtnWgt, 3, 0, 1, 2);
|
||
pGrid->setContentsMargins(GRID_LEFT_MARGIN, GRID_TOP_MARGIN, GRID_RIGHT_MARGIN, GRID_BOTTOM_MARGIN);
|
||
pGrid->setVerticalSpacing(GRID_V_SPACING);
|
||
pGrid->setHorizontalSpacing(GRID_H_SPACING);
|
||
|
||
setMinimumSize(500, 160);
|
||
}
|
||
|
||
void PaiMessageBox::SetDetailedText(const QString & text)
|
||
{
|
||
if(!m_pDetailsTEdit)
|
||
{
|
||
m_pDetailsTEdit = new PaiTextEdit(this);
|
||
m_pDetailsTEdit->setReadOnly(true);
|
||
|
||
QGridLayout *pGrid = qobject_cast< QGridLayout* > (layout());
|
||
if(pGrid)
|
||
{
|
||
pGrid->removeItem(pGrid->itemAtPosition(1, 0));
|
||
pGrid->addWidget(m_pDetailsTEdit, 1, 1, 1, 1);
|
||
}
|
||
}
|
||
|
||
m_pDetailsTEdit->setText(text);
|
||
}
|
||
|
||
QString PaiMessageBox::GetDetailedText() const
|
||
{
|
||
if(m_pDetailsTEdit)
|
||
{
|
||
return m_pDetailsTEdit->toPlainText();
|
||
}
|
||
|
||
return QString();
|
||
}
|
||
|
||
PaiPushButton* PaiMessageBox::AddButton(StandardButton button)
|
||
{
|
||
if(!m_FlagToBtn.contains(button))
|
||
{
|
||
PaiPushButton *pStdBtn = new PaiPushButton(GetStandardButtonText(button), this);
|
||
connect(pStdBtn, SIGNAL(clicked()), this, SLOT(ButtonClicked()));
|
||
|
||
m_FlagToBtn.insert(button, pStdBtn);
|
||
return pStdBtn;
|
||
}
|
||
|
||
return m_FlagToBtn.value(button);
|
||
}
|
||
|
||
void PaiMessageBox::SetIcon(Icon icon)
|
||
{
|
||
switch(icon)
|
||
{
|
||
case Icon_Information:
|
||
m_pIconLabel->setPixmap(QPixmap(QString(":/information.png")));
|
||
break;
|
||
case Icon_Warning:
|
||
m_pIconLabel->setPixmap(QPixmap(QString(":/warning.png")));
|
||
break;
|
||
case Icon_Critical:
|
||
m_pIconLabel->setPixmap(QPixmap(QString(":/critical.png")));
|
||
break;
|
||
case Icon_Question:
|
||
m_pIconLabel->setPixmap(QPixmap(QString(":/question.png")));
|
||
break;
|
||
case Icon_None:
|
||
default:
|
||
m_pIconLabel->setPixmap(QPixmap());
|
||
break;
|
||
}
|
||
|
||
}
|
||
|
||
void PaiMessageBox::SetStandardButtons(StandardButtons buttons)
|
||
{
|
||
uint mask = FirstButton;
|
||
while(mask <= LastButton)
|
||
{
|
||
uint sb = buttons & mask;
|
||
mask <<= 1;
|
||
if(sb)
|
||
{
|
||
AddButton((StandardButton) sb);
|
||
}
|
||
}
|
||
|
||
// 当没有EscapeButton按钮时,CloseButton不起作用,所以隐藏
|
||
StandardButton eBtn = DetectedEscapeButton();
|
||
if(eBtn == NoButton)
|
||
{
|
||
SetTitleBarFlags((GetTitleBarFlags() & (~PaiTitleBar::CloseButtonHint)) | PaiTitleBar::LogoButtonHint);
|
||
}
|
||
|
||
RestoreButtonPos();
|
||
}
|
||
|
||
void PaiMessageBox::SetDefaultButton(StandardButton button)
|
||
{
|
||
PaiPushButton *pDefBtn = m_FlagToBtn.value(button);
|
||
if(pDefBtn)
|
||
{
|
||
// In some GUI styles a default button is drawn with an extra frame around it
|
||
// In this case if use setDefault() method here
|
||
// the default buttons have a slightly larger size hint
|
||
pDefBtn->setFocus();
|
||
}
|
||
}
|
||
|
||
void PaiMessageBox::SetText(const QString & text)
|
||
{
|
||
m_pTextLabel->setText(text);
|
||
}
|
||
|
||
void PaiMessageBox::SetStandardButtonName(StandardButton button, const QString & name)
|
||
{
|
||
m_BtnNames[button] = name;
|
||
|
||
PaiPushButton *pBtn = m_FlagToBtn.value(button);
|
||
if(pBtn)
|
||
{
|
||
pBtn->setText(name);
|
||
}
|
||
}
|
||
|
||
QString PaiMessageBox::GetStandardButtonText(StandardButton button)
|
||
{
|
||
return m_BtnNames.value(button);
|
||
}
|
||
|
||
void PaiMessageBox::RestoreButtonPos()
|
||
{
|
||
if(m_pBtnWgt->layout())
|
||
{
|
||
delete m_pBtnWgt->layout();
|
||
}
|
||
|
||
QHBoxLayout *pBtnHLayout = new QHBoxLayout;
|
||
pBtnHLayout->setContentsMargins(0, 0, 0, 0);
|
||
pBtnHLayout->setSpacing(GRID_H_SPACING);
|
||
|
||
// reset the layout
|
||
int btnCount = m_BtnPos.count();
|
||
for(int i = 0; i < btnCount; i++)
|
||
{
|
||
PaiPushButton *pStdBtn = m_FlagToBtn.value(m_BtnPos.at(i));
|
||
if(pStdBtn)
|
||
{
|
||
pBtnHLayout->addWidget(pStdBtn);
|
||
}
|
||
else if(m_BtnPos.at(i) == NoButton)
|
||
{
|
||
pBtnHLayout->addItem(new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum));
|
||
}
|
||
}
|
||
|
||
m_pBtnWgt->setLayout(pBtnHLayout);
|
||
}
|
||
|
||
PaiMessageBox::StandardButton PaiMessageBox::ShowNewMessageBox(QWidget *pParent,
|
||
PaiMessageBox::Icon icon,
|
||
const QString & title,
|
||
const QString & text,
|
||
PaiMessageBox::StandardButtons buttons,
|
||
PaiMessageBox::StandardButton defaultButton)
|
||
{
|
||
PaiMessageBox messageBox(icon, title, text, buttons, pParent);
|
||
messageBox.SetDefaultButton(defaultButton);
|
||
|
||
return (StandardButton) (messageBox.exec());
|
||
}
|
||
|
||
void PaiMessageBox::SetResutlButton(StandardButton button)
|
||
{
|
||
setResult(button);
|
||
|
||
// 打印日志
|
||
QString msg = tr("PaiMessageBox [") + GetStandardButtonText(button) + tr("] button clicked.");
|
||
// pai::log::Info(_FLF(msg.toStdString()));
|
||
}
|
||
|
||
void PaiMessageBox::ButtonClicked()
|
||
{
|
||
StandardButton button = m_FlagToBtn.key(qobject_cast< PaiPushButton* > (sender()));
|
||
|
||
emit ButtonClicked(button);
|
||
accept();
|
||
SetResutlButton(button);
|
||
}
|
||
|
||
void PaiMessageBox::closeEvent(QCloseEvent *pEvent)
|
||
{
|
||
StandardButton eBtn = DetectedEscapeButton();
|
||
if(eBtn == NoButton)
|
||
{
|
||
pEvent->ignore();
|
||
return;
|
||
}
|
||
|
||
QDialog::closeEvent(pEvent);
|
||
SetResutlButton(eBtn);
|
||
}
|
||
|
||
PaiMessageBox::StandardButton PaiMessageBox::DetectedEscapeButton()
|
||
{
|
||
foreach(StandardButton ebtn, m_EscapeBtns)
|
||
{
|
||
if(m_FlagToBtn.contains(ebtn))
|
||
{
|
||
return ebtn;
|
||
}
|
||
}
|
||
|
||
return NoButton;
|
||
}
|
||
|
||
PaiMessageBox::StandardButton PaiMessageBox::Information(QWidget *pParent,
|
||
const QString & title,
|
||
const QString & text,
|
||
StandardButtons buttons,
|
||
StandardButton defaultButton)
|
||
{
|
||
return ShowNewMessageBox(pParent, Icon_Information, title, text, buttons, defaultButton);
|
||
}
|
||
|
||
PaiMessageBox::StandardButton PaiMessageBox::Question(QWidget *pParent,
|
||
const QString & title,
|
||
const QString & text,
|
||
StandardButtons buttons,
|
||
StandardButton defaultButton)
|
||
{
|
||
return ShowNewMessageBox(pParent, Icon_Question, title, text, buttons, defaultButton);
|
||
}
|
||
|
||
PaiMessageBox::StandardButton PaiMessageBox::Warning(QWidget *pParent,
|
||
const QString & title,
|
||
const QString & text,
|
||
StandardButtons buttons,
|
||
StandardButton defaultButton)
|
||
{
|
||
return ShowNewMessageBox(pParent, Icon_Warning, title, text, buttons, defaultButton);
|
||
}
|
||
|
||
PaiMessageBox::StandardButton PaiMessageBox::Critical(QWidget *pParent,
|
||
const QString & title,
|
||
const QString & text,
|
||
StandardButtons buttons,
|
||
StandardButton defaultButton)
|
||
{
|
||
return ShowNewMessageBox(pParent, Icon_Critical, title, text, buttons, defaultButton);
|
||
}
|