diff options
-rw-r--r-- | src/gui/dialogs/qmessagebox.cpp | 49 |
1 files changed, 38 insertions, 11 deletions
diff --git a/src/gui/dialogs/qmessagebox.cpp b/src/gui/dialogs/qmessagebox.cpp index 121ba62..df8b525 100644 --- a/src/gui/dialogs/qmessagebox.cpp +++ b/src/gui/dialogs/qmessagebox.cpp @@ -118,14 +118,44 @@ public: } 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 +class DetailButton : public QPushButton +{ +public: + DetailButton(QWidget *parent) : QPushButton(label(ShowLabel), parent) + { + setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); + } + + QString label(DetailButtonLabel label) const + { return label == ShowLabel ? QMessageBox::tr("Show Details...") : QMessageBox::tr("Hide Details..."); } + + void setLabel(DetailButtonLabel lbl) + { setText(label(lbl)); } + + QSize sizeHint() const + { + ensurePolished(); + QStyleOptionButton opt; + initStyleOption(&opt); + const QFontMetrics fm = fontMetrics(); + opt.text = label(ShowLabel); + QSize sz = fm.size(Qt::TextShowMnemonic, opt.text); + QSize ret = style()->sizeFromContents(QStyle::CT_PushButton, &opt, sz, this). + expandedTo(QApplication::globalStrut()); + opt.text = label(HideLabel); + sz = fm.size(Qt::TextShowMnemonic, opt.text); + ret.expandedTo(style()->sizeFromContents(QStyle::CT_PushButton, &opt, sz, this). + expandedTo(QApplication::globalStrut())); + return ret; + } +}; + + class QMessageBoxPrivate : public QDialogPrivate { Q_DECLARE_PUBLIC(QMessageBox) @@ -181,7 +211,7 @@ public: QAbstractButton *escapeButton; QPushButton *defaultButton; QAbstractButton *clickedButton; - QPushButton *detailsButton; + DetailButton *detailsButton; #ifndef QT_NO_TEXTEDIT QMessageBoxDetailsText *detailsText; #endif @@ -421,7 +451,7 @@ void QMessageBoxPrivate::_q_buttonClicked(QAbstractButton *button) Q_Q(QMessageBox); #ifndef QT_NO_TEXTEDIT if (detailsButton && detailsText && button == detailsButton) { - detailsButton->setText(detailsText->isHidden() ? detailsText->label(HideLabel) : detailsText->label(ShowLabel)); + detailsButton->setLabel(detailsText->isHidden() ? HideLabel : ShowLabel); detailsText->setHidden(!detailsText->isHidden()); updateSize(); } else @@ -1891,7 +1921,7 @@ void QMessageBoxPrivate::retranslateStrings() { #ifndef QT_NO_TEXTEDIT if (detailsButton) - detailsButton->setText(detailsText->isHidden() ? detailsText->label(HideLabel) : detailsText->label(ShowLabel)); + detailsButton->setLabel(detailsText->isHidden() ? HideLabel : ShowLabel); #endif } @@ -2399,11 +2429,8 @@ void QMessageBox::setDetailedText(const QString &text) grid->addWidget(d->detailsText, grid->rowCount(), 0, 1, grid->columnCount()); d->detailsText->hide(); } - if (!d->detailsButton) { - d->detailsButton = new QPushButton(d->detailsText->label(ShowLabel), this); - QPushButton hideDetails(d->detailsText->label(HideLabel)); - d->detailsButton->setFixedSize(d->detailsButton->sizeHint().expandedTo(hideDetails.sizeHint())); - } + if (!d->detailsButton) + d->detailsButton = new DetailButton(this); d->detailsText->setText(text); } #endif // QT_NO_TEXTEDIT |