📄 qmessagebox.cpp
字号:
/******************************************************************************** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.**** This file is part of the QtGui module of the Qt Toolkit.**** This file may be used under the terms of the GNU General Public** License version 2.0 as published by the Free Software Foundation** and appearing in the file LICENSE.GPL included in the packaging of** this file. Please review the following information to ensure GNU** General Public Licensing requirements will be met:** http://trolltech.com/products/qt/licenses/licensing/opensource/**** If you are unsure which license is appropriate for your use, please** review the following information:** http://trolltech.com/products/qt/licenses/licensing/licensingoverview** or contact the sales department at sales@trolltech.com.**** In addition, as a special exception, Trolltech gives you certain** additional rights. These rights are described in the Trolltech GPL** Exception version 1.0, which can be found at** http://www.trolltech.com/products/qt/gplexception/ and in the file** GPL_EXCEPTION.txt in this package.**** In addition, as a special exception, Trolltech, as the sole copyright** holder for Qt Designer, grants users of the Qt/Eclipse Integration** plug-in the right for the Qt/Eclipse Integration to link to** functionality provided by Qt Designer and its related libraries.**** Trolltech reserves all rights not expressly granted herein.**** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.******************************************************************************/#include <QtGui/qmessagebox.h>#ifndef QT_NO_MESSAGEBOX#include <QtGui/qdialogbuttonbox.h>#include "private/qlabel_p.h"#include <QtCore/qlist.h>#include <QtCore/qdebug.h>#include <QtGui/qstyle.h>#include <QtGui/qstyleoption.h>#include <QtGui/qgridlayout.h>#include <QtGui/qdesktopwidget.h>#include <QtGui/qpushbutton.h>#include <QtGui/qaccessible.h>#include <QtGui/qicon.h>#include <QtGui/qtextdocument.h>#include <QtGui/qapplication.h>#include <QtGui/qtextedit.h>#include <QtGui/qmenu.h>#include "qdialog_p.h"#include <QtGui/qfont.h>#include <QtGui/qfontmetrics.h>#include <QtGui/qclipboard.h>enum Button { Old_Ok = 1, Old_Cancel = 2, Old_Yes = 3, Old_No = 4, Old_Abort = 5, Old_Retry = 6, Old_Ignore = 7, Old_YesAll = 8, Old_NoAll = 9, Old_ButtonMask = 0xFF, NewButtonFlag = 0xFFFFFC00 };enum DetailButtonLabel { ShowLabel = 0, HideLabel = 1 };#ifndef QT_NO_TEXTEDITclass QMessageBoxDetailsText : public QWidget{public: class TextEdit : public QTextEdit { public: TextEdit(QWidget *parent=0) : QTextEdit(parent) { } void contextMenuEvent(QContextMenuEvent * e) {#ifdef QT_NO_CONTEXTMENU Q_UNUSED(e);#else QMenu *menu = createStandardContextMenu(); menu->exec(e->globalPos()); delete menu;#endif } }; QMessageBoxDetailsText(QWidget *parent=0) : QWidget(parent) { QVBoxLayout *layout = new QVBoxLayout; layout->setMargin(0); QFrame *line = new QFrame(this); line->setFrameShape(QFrame::HLine); line->setFrameShadow(QFrame::Sunken); layout->addWidget(line); textEdit = new TextEdit(); textEdit->setFixedHeight(100); textEdit->setFocusPolicy(Qt::NoFocus); textEdit->setReadOnly(true); layout->addWidget(textEdit); setLayout(layout); } void setText(const QString &text) { textEdit->setPlainText(text); } QString text() const { return textEdit->toPlainText(); } QString label(DetailButtonLabel label) { return label == ShowLabel ? QMessageBox::tr("Show Details...") : QMessageBox::tr("Hide Details..."); }private: TextEdit *textEdit;};#endif // QT_NO_TEXTEDIT#ifndef QT_NO_IMAGEFORMAT_XPM/* XPM */static const char * const qtlogo_xpm[] = {/* width height ncolors chars_per_pixel */"50 50 17 1",/* colors */" c #000000",". c #495808","X c #2A3304","o c #242B04","O c #030401","+ c #9EC011","@ c #93B310","# c #748E0C","$ c #A2C511","% c #8BA90E","& c #99BA10","* c #060701","= c #181D02","- c #212804","; c #61770A",": c #0B0D01","/ c None",/* pixels */"/$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$/","$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$","$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$","$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$","$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$","$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$","$$$$$$$$$$$$$$$$$$$$$$$+++$$$$$$$$$$$$$$$$$$$$$$$$","$$$$$$$$$$$$$$$$$$$@;.o=::=o.;@$$$$$$$$$$$$$$$$$$$","$$$$$$$$$$$$$$$$+#X* **X#+$$$$$$$$$$$$$$$$","$$$$$$$$$$$$$$$#oO* O **o#+$$$$$$$$$$$$$$","$$$$$$$$$$$$$&.* OO O*.&$$$$$$$$$$$$$","$$$$$$$$$$$$@XOO * OO X&$$$$$$$$$$$$","$$$$$$$$$$$@XO OO O **:::OOO OOO X@$$$$$$$$$$$","$$$$$$$$$$&XO O-;#@++@%.oOO X&$$$$$$$$$$","$$$$$$$$$$.O : *-#+$$$$$$$$+#- : O O*.$$$$$$$$$$","$$$$$$$$$#*OO O*.&$$$$$$$$$$$$+.OOOO **#$$$$$$$$$","$$$$$$$$+-OO O *;$$$$$$$$$$$&$$$$;* o+$$$$$$$$","$$$$$$$$#O* O .+$$$$$$$$$$@X;$$$+.O *#$$$$$$$$","$$$$$$$$X* -&$$$$$$$$$$@- :;$$$&- OX$$$$$$$$","$$$$$$$@*O *O#$$$$$$$$$$@oOO**;$$$# O*%$$$$$$$","$$$$$$$; -+$$$$$$$$$@o O OO ;+$$-O *;$$$$$$$","$$$$$$$. ;$$$$$$$$$@-OO OO X&$$;O .$$$$$$$","$$$$$$$o *#$$$$$$$$@o O O O-@$$$#O *o$$$$$$$","$$$$$$+= *@$$$$$$$@o* OO -@$$$$&: =$$$$$$$","$$$$$$+: :+$$$$$$@- *-@$$$$$$: :+$$$$$$","$$$$$$+: :+$$$$$@o* O *-@$$$$$$: :+$$$$$$","$$$$$$$= :@$$$$@o*OOO -@$$$$@: =+$$$$$$","$$$$$$$- O%$$$@o* O O O O-@$$$#* OX$$$$$$$","$$$$$$$. O *O;$$&o O*O* *O -@$$; O.$$$$$$$","$$$$$$$;* Oo+$$;O*O:OO-- Oo@+= *;$$$$$$$","$$$$$$$@* O O#$$$;*OOOo@@-O Oo;O* **@$$$$$$$","$$$$$$$$X* OOO-+$$$;O o@$$@- O O OX$$$$$$$$","$$$$$$$$#* * O.$$$$;X@$$$$@-O O O#$$$$$$$$","$$$$$$$$+oO O OO.+$$+&$$$$$$@-O o+$$$$$$$$","$$$$$$$$$#* **.&$$$$$$$$$$@o OO:#$$$$$$$$$","$$$$$$$$$+. O* O-#+$$$$$$$$+;O OOO:@$$$$$$$$$","$$$$$$$$$$&X *O -;#@++@#;=O O -@$$$$$$$$","$$$$$$$$$$$&X O O*O::::O OO Oo@$$$$$$$","$$$$$$$$$$$$@XOO OO O*X+$$$$$$","$$$$$$$$$$$$$&.* ** O :: *:#$$$$$$$","$$$$$$$$$$$$$$$#o*OO O Oo#@-OOO=#$$$$$$$$","$$$$$$$$$$$$$$$$+#X:* * O**X#+$$@-*:#$$$$$$$$$","$$$$$$$$$$$$$$$$$$$%;.o=::=o.#@$$$$$$@X#$$$$$$$$$$","$$$$$$$$$$$$$$$$$$$$$$$$+++$$$$$$$$$$$+$$$$$$$$$$$","$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$","$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$","$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$","$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$","$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$","/$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$/",};#endif // QT_NO_IMAGEFORMAT_XPMclass QMessageBoxPrivate : public QDialogPrivate{ Q_DECLARE_PUBLIC(QMessageBox)public: QMessageBoxPrivate() : escapeButton(0), defaultButton(0), clickedButton(0), detailsButton(0),#ifndef QT_NO_TEXTEDIT detailsText(0),#endif compatMode(false), autoAddOkButton(true), detectedEscapeButton(0), informativeLabel(0) { } void init(const QString &title = QString(), const QString &text = QString()); void _q_buttonClicked(QAbstractButton *); QAbstractButton *findButton(int button0, int button1, int button2, int flags); void addOldButtons(int button0, int button1, int button2); QAbstractButton *abstractButtonForId(int id) const; int execReturnCode(QAbstractButton *button); void detectEscapeButton(); void updateSize(); int layoutMinimumWidth(); void retranslateStrings(); static int showOldMessageBox(QWidget *parent, QMessageBox::Icon icon, const QString &title, const QString &text, int button0, int button1, int button2); static int showOldMessageBox(QWidget *parent, QMessageBox::Icon icon, const QString &title, const QString &text, const QString &button0Text, const QString &button1Text, const QString &button2Text, int defaultButtonNumber, int escapeButtonNumber); static QMessageBox::StandardButton showNewMessageBox(QWidget *parent, QMessageBox::Icon icon, const QString& title, const QString& text, QMessageBox::StandardButtons buttons, QMessageBox::StandardButton defaultButton); QLabel *label; QMessageBox::Icon icon; QLabel *iconLabel; QDialogButtonBox *buttonBox; QList<QAbstractButton *> customButtonList; QAbstractButton *escapeButton; QPushButton *defaultButton; QAbstractButton *clickedButton; QPushButton *detailsButton;#ifndef QT_NO_TEXTEDIT QMessageBoxDetailsText *detailsText;#endif bool compatMode; bool autoAddOkButton; QAbstractButton *detectedEscapeButton; QLabel *informativeLabel;};static QString *translatedTextAboutQt = 0;void QMessageBoxPrivate::init(const QString &title, const QString &text){ Q_Q(QMessageBox); if (!translatedTextAboutQt) { translatedTextAboutQt = new QString; } label = new QLabel; label->setObjectName(QLatin1String("qt_msgbox_label")); label->setTextInteractionFlags(Qt::TextInteractionFlags(q->style()->styleHint(QStyle::SH_MessageBox_TextInteractionFlags, 0, q))); label->setAlignment(Qt::AlignVCenter | Qt::AlignLeft); label->setOpenExternalLinks(true);#if defined(Q_WS_MAC) label->setContentsMargins(16, 0, 0, 8);#elif !defined(Q_WS_QWS) label->setContentsMargins(2, 0, 0, 0); label->setIndent(9);#endif icon = QMessageBox::NoIcon; iconLabel = new QLabel; iconLabel->setObjectName(QLatin1String("qt_msgboxex_icon_label")); iconLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); buttonBox = new QDialogButtonBox; buttonBox->setObjectName(QLatin1String("qt_msgbox_buttonbox")); buttonBox->setCenterButtons(q->style()->styleHint(QStyle::SH_MessageBox_CenterButtons, 0, q)); QObject::connect(buttonBox, SIGNAL(clicked(QAbstractButton*)), q, SLOT(_q_buttonClicked(QAbstractButton*))); QGridLayout *grid = new QGridLayout;#ifndef Q_WS_MAC grid->addWidget(iconLabel, 0, 0, 2, 1, Qt::AlignTop); grid->addWidget(label, 0, 1, 1, 1); // -- leave space for information label -- grid->addWidget(buttonBox, 2, 0, 1, 2);#else grid->setMargin(0); grid->setSpacing(0); q->setContentsMargins(24, 15, 24, 20); grid->addWidget(iconLabel, 0, 0, 2, 1, Qt::AlignTop | Qt::AlignLeft); grid->addWidget(label, 0, 1, 1, 1); // -- leave space for information label -- grid->setRowStretch(1, 100); grid->setRowMinimumHeight(2, 10); grid->addWidget(buttonBox, 3, 1, 1, 1);#endif grid->setSizeConstraint(QLayout::SetNoConstraint); q->setLayout(grid); if (!title.isEmpty() || !text.isEmpty()) { q->setWindowTitle(title); q->setText(text); } q->setModal(true);#ifdef Q_WS_MAC QFont f = q->font(); f.setBold(true); label->setFont(f);#endif retranslateStrings();}int QMessageBoxPrivate::layoutMinimumWidth(){ Q_Q(QMessageBox); q->layout()->activate(); return q->layout()->totalMinimumSize().width();}void QMessageBoxPrivate::updateSize(){ Q_Q(QMessageBox); if (!q->isVisible()) return; QSize screenSize = QApplication::desktop()->availableGeometry(QCursor::pos()).size();#ifdef Q_WS_QWS // the width of the screen, less the window border. int hardLimit = screenSize.width() - (q->frameGeometry().width() - q->geometry().width());#else int hardLimit = qMin(screenSize.width() - 480, 1000); // can never get bigger than this#endif#ifdef Q_WS_MAC int softLimit = qMin(screenSize.width()/2, 420);#elif defined(Q_WS_QWS) int softLimit = qMin(hardLimit, 500);#else // note: ideally on windows, hard and soft limits but it breaks compat int softLimit = qMin(screenSize.width()/2, 500);#endif if (informativeLabel) informativeLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored); label->setWordWrap(false); // makes the label return min size int width = layoutMinimumWidth(); if (width > softLimit) { label->setWordWrap(true); width = qMax(softLimit, layoutMinimumWidth()); if (width > hardLimit) { label->d_func()->ensureTextControl(); if (QTextControl *control = label->d_func()->control) { QTextOption opt = control->document()->defaultTextOption(); opt.setWrapMode(QTextOption::WrapAnywhere); control->document()->setDefaultTextOption(opt); } width = hardLimit; } } if (informativeLabel) { label->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored); if (layoutMinimumWidth() > hardLimit) { // longest word is really big, so wrap anywhere informativeLabel->d_func()->ensureTextControl(); if (QTextControl *control = informativeLabel->d_func()->control) { QTextOption opt = control->document()->defaultTextOption(); opt.setWrapMode(QTextOption::WrapAnywhere); control->document()->setDefaultTextOption(opt); } } QSizePolicy policy(QSizePolicy::Preferred, QSizePolicy::Preferred); policy.setHeightForWidth(label->wordWrap()); label->setSizePolicy(policy); policy.setHeightForWidth(true); informativeLabel->setSizePolicy(policy); } QFontMetrics fm(qApp->font("QWorkspaceTitleBar")); int windowTitleWidth = qMin(fm.width(q->windowTitle()) + 50, hardLimit); if (windowTitleWidth > width) width = windowTitleWidth; q->layout()->activate(); int height = (q->layout()->hasHeightForWidth()) ? q->layout()->totalHeightForWidth(width)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -