#include "mymessagebox.h" MessageBox::MessageBox(QWidget *parent, QMessageBox::Icon icon, const QString &title, const QString &text, QMessageBox::StandardButtons buttons, QMessageBox::StandardButton defaultButton, Work_Thread *thread) :customDialog(parent) { this->setWindowFlags(Qt::Dialog|Qt::FramelessWindowHint);//标题框隐藏 mMoveing = false; work_thread = thread; connect(work_thread,SIGNAL(SentInterface(int)),this,SLOT(UpdateInterface(int))); titleLabel=new QLabel(this); QFont font=titleLabel->font(); //font.setBold(true); font.setPointSize(18); titleLabel->setFont(font); titleLabel->setText(MB_Caption); titleLabel->setAlignment(Qt::AlignCenter); titleLabel->setStyleSheet("background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop:0 rgb(65,80,125), " "stop: 0.5 rgb(80,98,153)," "stop: 1 rgb(97, 118, 184));" "color:rgb(231,231,231); /*border:1px solid rgb(170, 170, 255);*/"); QPixmap TempPix=style()->standardPixmap(QStyle::SP_TitleBarCloseButton); closeButton=new QToolButton(this); closeButton->setIcon(TempPix); closeButton->setStyleSheet("background-color: rgb(97, 118, 184);"); font.setPointSize(22); this->setFont(font); 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->setStyle(QApplication::style()); } m_pIconLabel = new QLabel(this); if(icon == QMessageBox::Information) TempPix=style()->standardPixmap(QStyle::SP_MessageBoxInformation); else if(icon == QMessageBox::Warning) TempPix=style()->standardPixmap(QStyle::SP_MessageBoxWarning); else if(icon == QMessageBox::Critical) TempPix=style()->standardPixmap(QStyle::SP_MessageBoxCritical); else if(icon == QMessageBox::Question) TempPix=style()->standardPixmap(QStyle::SP_MessageBoxQuestion); else TempPix=QPixmap(""); m_pIconLabel->setPixmap(TempPix); m_pIconLabel->setMinimumSize(35,35); m_pIconLabel->setScaledContents(true); m_pLabel = new QLabel(this); m_pLabel->setAlignment(Qt::AlignVCenter | Qt::AlignLeft); font.setPointSize(24); m_pLabel->setFont(font); QString str; str = text; setQlabelMinimumWidth(m_pLabel,str); m_pLabel->setText(str); m_pGridLayout = new QGridLayout(this); m_pGridLayout->setSizeConstraint(QLayout::SetMinimumSize); m_pGridLayout->setHorizontalSpacing(5);//水平方向 m_pGridLayout->setVerticalSpacing(1); //垂直方向 m_pGridLayout->setContentsMargins(0, 0, 0, 20); m_pGridLayout->setRowMinimumHeight(1,15); m_pGridLayout->setRowMinimumHeight(4,15); m_pGridLayout->addWidget(titleLabel, 0, 0, 1, 30); m_pGridLayout->addWidget(closeButton, 0, 29, 1, 1); m_pGridLayout->addWidget(m_pIconLabel, 2, 1, 2, 2, Qt::AlignTop|Qt::AlignLeft); m_pGridLayout->addWidget(m_pLabel, 2, 3, 2, 26,Qt::AlignCenter); m_pGridLayout->addWidget(m_pButtonBox, 5, 0, 2, 30,Qt::AlignCenter); this->setLayout(m_pGridLayout); translateUI(); connect(closeButton, SIGNAL(clicked()), this, SLOT(btn_beep())); connect(m_pButtonBox, SIGNAL(clicked(QAbstractButton*)), this, SLOT(btn_beep())); connect(closeButton, SIGNAL(clicked()), this, SLOT(close())); connect(m_pButtonBox, SIGNAL(clicked(QAbstractButton*)), this, SLOT(onButtonClicked(QAbstractButton*))); } MessageBox::~MessageBox() { } void MessageBox::UpdateInterface(int interface) { this->closeCustom(false); } void MessageBox::setQlabelMinimumWidth(QLabel *Label, QString &text) { QFontMetrics fm(Label->font()); QRect rec = fm.boundingRect(text); QString str,testtext; int textWidth = rec.width(); int addcount = 0; str.clear(); testtext.clear(); testtext = text; if(textWidth >400) { int width1 = textWidth%400; if(width1 <80) { width1 = 350; } else { width1 = 400; } for(int i=0;i width1) { if(text.at(i).toLatin1()>='A' && text.at(i).toLatin1()<='Z') { testtext.insert(i+addcount," "); addcount++; str.clear(); } else if(text.at(i).toLatin1()>='a' && text.at(i).toLatin1()<='z') { if((text.at(i-1).toLatin1()>='a' && text.at(i-1).toLatin1()<='z')|| (text.at(i-1).toLatin1()>='A' && text.at(i-1).toLatin1()<='Z')) { testtext.insert(i+addcount,"- \r\n"); addcount= addcount+2; str.clear(); } else if(text.at(i-1).toLatin1() == ' ') { str.clear(); } else { testtext.insert(i+addcount," "); addcount++; str.clear(); } } else { testtext.insert(i+1+addcount," "); addcount++; str.clear(); } } } text = testtext; Label->setMinimumWidth(width1+20); Label->setWordWrap(true); } } void MessageBox::changeEvent(QEvent *event) { switch (event->type()) { case QEvent::LanguageChange: translateUI(); break; default: break; } } void MessageBox::translateUI() { QFont font; font.setPointSize(18); QPushButton *pYesButton = m_pButtonBox->button(QDialogButtonBox::Yes); if (pYesButton != NULL){ pYesButton->setFont(font); pYesButton->setFixedSize(70,40); pYesButton->setText(tr("是"));//Yes } QPushButton *pNoButton = m_pButtonBox->button(QDialogButtonBox::No); if (pNoButton != NULL){ pNoButton->setFont(font); pNoButton->setFixedSize(70,40); pNoButton->setText(tr("否"));//No } QPushButton *pOkButton = m_pButtonBox->button(QDialogButtonBox::Ok); if (pOkButton != NULL){ pOkButton->setFont(font); pOkButton->setFixedSize(70,40); pOkButton->setText(tr("确定"));//OK } QPushButton *pCancelButton = m_pButtonBox->button(QDialogButtonBox::Cancel); if (pCancelButton != NULL){ pCancelButton->setFixedSize(70,40); pCancelButton->setFont(font); pCancelButton->setText(tr("取消"));//Cancel } } QMessageBox::StandardButton MessageBox::standardButton(QAbstractButton *button) const { return (QMessageBox::StandardButton)m_pButtonBox->standardButton(button); } QAbstractButton *MessageBox::clickedButton() const { return m_pClickedButton; } int MessageBox::execReturnCode(QAbstractButton *button) { int nResult = m_pButtonBox->standardButton(button); return nResult; } void MessageBox::onButtonClicked(QAbstractButton *button) { m_pClickedButton = button; done(execReturnCode(button)); } void MessageBox::setDefaultButton(QPushButton *button) { if (!m_pButtonBox->buttons().contains(button)) return; m_pDefaultButton = button; button->setDefault(true); button->setFocus(); } void MessageBox::setDefaultButton(QMessageBox::StandardButton button) { setDefaultButton(m_pButtonBox->button(QDialogButtonBox::StandardButton(button))); } void MessageBox::btn_beep(void) { customDialog::buzzer_beep(50); } //*****************************************************************************8 //void MessageBox::mousePressEvent(QMouseEvent *event) //{ // mMoveing = true; // mMovePosition = event->globalPos() - pos(); // return QDialog::mousePressEvent(event); //} //void MessageBox::mouseMoveEvent(QMouseEvent *event) //{ // QPoint temp = event->globalPos()-mMovePosition; // if(temp.x()>800-this->width()) // temp.setX(800-this->width()); // else if(temp.x()<0) // temp.setX(0); // if(temp.y()>480-this->height()) // temp.setY(480-this->height()); // else if(temp.y()<0) // temp.setY(0); // if (mMoveing && (event->buttons() && Qt::LeftButton) // && temp.manhattanLength() > QApplication::startDragDistance()) // { // move(temp); // mMovePosition = event->globalPos() - pos(); // } // return QDialog::mouseMoveEvent(event); //} //void MessageBox::mouseReleaseEvent(QMouseEvent *event) //{ // mMoveing = false; //} /* int MessageBox::heightForWidth(int w) const { if (cache_dirty || cached_width != w) { MyLayout *that = (MyLayout*)this; int h = calculateHeightForWidth(w); that->cached_hfw = h; return h; } return cached_hfw; }*/ void MessageBox::setText(const QString & str) { QString text; text = str; setQlabelMinimumWidth(m_pLabel,text); m_pLabel->setText(text); } void MessageBox::set_yes_btn_text(bool en, QString str){ m_pButtonBox->button(QDialogButtonBox::Yes)->setEnabled(en); m_pButtonBox->button(QDialogButtonBox::Yes)->setText(str); }