src/gui/dialogs/qmessagebox.cpp
changeset 30 5dc02b23752f
parent 19 fcece45ef507
--- a/src/gui/dialogs/qmessagebox.cpp	Wed Jun 23 19:07:03 2010 +0300
+++ b/src/gui/dialogs/qmessagebox.cpp	Tue Jul 06 15:10:48 2010 +0300
@@ -96,8 +96,8 @@
         {
 #ifndef QT_NO_CONTEXTMENU
             QMenu *menu = createStandardContextMenu();
-            menu->exec(e->globalPos());
-            delete menu;
+            menu->setAttribute(Qt::WA_DeleteOnClose);
+            menu->popup(e->globalPos());
 #else
             Q_UNUSED(e);
 #endif
@@ -122,14 +122,44 @@
     }
     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)
@@ -185,7 +215,7 @@
     QAbstractButton *escapeButton;
     QPushButton *defaultButton;
     QAbstractButton *clickedButton;
-    QPushButton *detailsButton;
+    DetailButton *detailsButton;
 #ifndef QT_NO_TEXTEDIT
     QMessageBoxDetailsText *detailsText;
 #endif
@@ -435,7 +465,7 @@
     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
@@ -1905,7 +1935,7 @@
 {
 #ifndef QT_NO_TEXTEDIT
     if (detailsButton)
-        detailsButton->setText(detailsText->isHidden() ? detailsText->label(HideLabel) : detailsText->label(ShowLabel));
+        detailsButton->setLabel(detailsText->isHidden() ? HideLabel : ShowLabel);
 #endif
 }
 
@@ -2413,11 +2443,8 @@
             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