src/gui/dialogs/qmessagebox.cpp
changeset 30 5dc02b23752f
parent 19 fcece45ef507
equal deleted inserted replaced
29:b72c6db6890b 30:5dc02b23752f
    94         TextEdit(QWidget *parent=0) : QTextEdit(parent) { }
    94         TextEdit(QWidget *parent=0) : QTextEdit(parent) { }
    95         void contextMenuEvent(QContextMenuEvent * e)
    95         void contextMenuEvent(QContextMenuEvent * e)
    96         {
    96         {
    97 #ifndef QT_NO_CONTEXTMENU
    97 #ifndef QT_NO_CONTEXTMENU
    98             QMenu *menu = createStandardContextMenu();
    98             QMenu *menu = createStandardContextMenu();
    99             menu->exec(e->globalPos());
    99             menu->setAttribute(Qt::WA_DeleteOnClose);
   100             delete menu;
   100             menu->popup(e->globalPos());
   101 #else
   101 #else
   102             Q_UNUSED(e);
   102             Q_UNUSED(e);
   103 #endif
   103 #endif
   104         }
   104         }
   105     };
   105     };
   120         layout->addWidget(textEdit);
   120         layout->addWidget(textEdit);
   121         setLayout(layout);
   121         setLayout(layout);
   122     }
   122     }
   123     void setText(const QString &text) { textEdit->setPlainText(text); }
   123     void setText(const QString &text) { textEdit->setPlainText(text); }
   124     QString text() const { return textEdit->toPlainText(); }
   124     QString text() const { return textEdit->toPlainText(); }
   125     QString label(DetailButtonLabel label)
       
   126         { return label == ShowLabel ? QMessageBox::tr("Show Details...")
       
   127                                     : QMessageBox::tr("Hide Details..."); }
       
   128 private:
   125 private:
   129     TextEdit *textEdit;
   126     TextEdit *textEdit;
   130 };
   127 };
   131 #endif // QT_NO_TEXTEDIT
   128 #endif // QT_NO_TEXTEDIT
       
   129 
       
   130 class DetailButton : public QPushButton
       
   131 {
       
   132 public:
       
   133     DetailButton(QWidget *parent) : QPushButton(label(ShowLabel), parent)
       
   134     {
       
   135         setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
       
   136     }
       
   137 
       
   138     QString label(DetailButtonLabel label) const
       
   139     { return label == ShowLabel ? QMessageBox::tr("Show Details...") : QMessageBox::tr("Hide Details..."); }
       
   140 
       
   141     void setLabel(DetailButtonLabel lbl)
       
   142     { setText(label(lbl)); }
       
   143 
       
   144     QSize sizeHint() const
       
   145     {
       
   146         ensurePolished();
       
   147         QStyleOptionButton opt;
       
   148         initStyleOption(&opt);
       
   149         const QFontMetrics fm = fontMetrics();
       
   150         opt.text = label(ShowLabel);
       
   151         QSize sz = fm.size(Qt::TextShowMnemonic, opt.text);
       
   152         QSize ret = style()->sizeFromContents(QStyle::CT_PushButton, &opt, sz, this).
       
   153                       expandedTo(QApplication::globalStrut());
       
   154         opt.text = label(HideLabel);
       
   155         sz = fm.size(Qt::TextShowMnemonic, opt.text);
       
   156         ret.expandedTo(style()->sizeFromContents(QStyle::CT_PushButton, &opt, sz, this).
       
   157                       expandedTo(QApplication::globalStrut()));
       
   158         return ret;
       
   159     }
       
   160 };
       
   161 
   132 
   162 
   133 class QMessageBoxPrivate : public QDialogPrivate
   163 class QMessageBoxPrivate : public QDialogPrivate
   134 {
   164 {
   135     Q_DECLARE_PUBLIC(QMessageBox)
   165     Q_DECLARE_PUBLIC(QMessageBox)
   136 
   166 
   183     QDialogButtonBox *buttonBox;
   213     QDialogButtonBox *buttonBox;
   184     QList<QAbstractButton *> customButtonList;
   214     QList<QAbstractButton *> customButtonList;
   185     QAbstractButton *escapeButton;
   215     QAbstractButton *escapeButton;
   186     QPushButton *defaultButton;
   216     QPushButton *defaultButton;
   187     QAbstractButton *clickedButton;
   217     QAbstractButton *clickedButton;
   188     QPushButton *detailsButton;
   218     DetailButton *detailsButton;
   189 #ifndef QT_NO_TEXTEDIT
   219 #ifndef QT_NO_TEXTEDIT
   190     QMessageBoxDetailsText *detailsText;
   220     QMessageBoxDetailsText *detailsText;
   191 #endif
   221 #endif
   192     bool compatMode;
   222     bool compatMode;
   193     bool autoAddOkButton;
   223     bool autoAddOkButton;
   433 void QMessageBoxPrivate::_q_buttonClicked(QAbstractButton *button)
   463 void QMessageBoxPrivate::_q_buttonClicked(QAbstractButton *button)
   434 {
   464 {
   435     Q_Q(QMessageBox);
   465     Q_Q(QMessageBox);
   436 #ifndef QT_NO_TEXTEDIT
   466 #ifndef QT_NO_TEXTEDIT
   437     if (detailsButton && detailsText && button == detailsButton) {
   467     if (detailsButton && detailsText && button == detailsButton) {
   438         detailsButton->setText(detailsText->isHidden() ? detailsText->label(HideLabel) : detailsText->label(ShowLabel));
   468         detailsButton->setLabel(detailsText->isHidden() ? HideLabel : ShowLabel);
   439         detailsText->setHidden(!detailsText->isHidden());
   469         detailsText->setHidden(!detailsText->isHidden());
   440         updateSize();
   470         updateSize();
   441     } else
   471     } else
   442 #endif
   472 #endif
   443     {
   473     {
  1903 
  1933 
  1904 void QMessageBoxPrivate::retranslateStrings()
  1934 void QMessageBoxPrivate::retranslateStrings()
  1905 {
  1935 {
  1906 #ifndef QT_NO_TEXTEDIT
  1936 #ifndef QT_NO_TEXTEDIT
  1907     if (detailsButton)
  1937     if (detailsButton)
  1908         detailsButton->setText(detailsText->isHidden() ? detailsText->label(HideLabel) : detailsText->label(ShowLabel));
  1938         detailsButton->setLabel(detailsText->isHidden() ? HideLabel : ShowLabel);
  1909 #endif
  1939 #endif
  1910 }
  1940 }
  1911 
  1941 
  1912 /*!
  1942 /*!
  1913     \obsolete
  1943     \obsolete
  2411         QGridLayout* grid = qobject_cast<QGridLayout*>(layout());
  2441         QGridLayout* grid = qobject_cast<QGridLayout*>(layout());
  2412         if (grid)
  2442         if (grid)
  2413             grid->addWidget(d->detailsText, grid->rowCount(), 0, 1, grid->columnCount());
  2443             grid->addWidget(d->detailsText, grid->rowCount(), 0, 1, grid->columnCount());
  2414         d->detailsText->hide();
  2444         d->detailsText->hide();
  2415     }
  2445     }
  2416     if (!d->detailsButton) {
  2446     if (!d->detailsButton)
  2417         d->detailsButton = new QPushButton(d->detailsText->label(ShowLabel), this);
  2447         d->detailsButton = new DetailButton(this);
  2418         QPushButton hideDetails(d->detailsText->label(HideLabel));
       
  2419         d->detailsButton->setFixedSize(d->detailsButton->sizeHint().expandedTo(hideDetails.sizeHint()));
       
  2420     }
       
  2421     d->detailsText->setText(text);
  2448     d->detailsText->setText(text);
  2422 }
  2449 }
  2423 #endif // QT_NO_TEXTEDIT
  2450 #endif // QT_NO_TEXTEDIT
  2424 
  2451 
  2425 /*!
  2452 /*!