comparison moreinformationdialog.cpp @ 290:c0c254f67346 more_information_dialog

Make our own message box with a more useful "more information" section than the Qt one
author Chris Cannam
date Mon, 21 Feb 2011 14:23:02 +0000
parents 1244dc3107cb
children 2e34e7ee7baf
comparison
equal deleted inserted replaced
275:1244dc3107cb 290:c0c254f67346
16 */ 16 */
17 17
18 #include "moreinformationdialog.h" 18 #include "moreinformationdialog.h"
19 19
20 #include <QMessageBox> 20 #include <QMessageBox>
21 #include <QLabel>
22 #include <QGridLayout>
23 #include <QTextEdit>
24 #include <QDialogButtonBox>
25 #include <QPushButton>
26 #include <QApplication>
27 #include <QStyle>
21 28
22 void 29 MoreInformationDialog::MoreInformationDialog(QString title,
23 MoreInformationDialog::critical(QWidget *parent, QString title, 30 QString head,
24 QString text, QString more) 31 QString text,
32 QString more,
33 QWidget *parent) :
34 QDialog(parent)
25 { 35 {
26 QMessageBox mb(QMessageBox::Critical, 36 setWindowTitle(title);
27 title, 37
28 text, 38 QGridLayout *layout = new QGridLayout;
29 QMessageBox::Ok, 39 layout->setSpacing(10);
30 parent, 40 setLayout(layout);
31 Qt::Dialog); 41
32 mb.setDetailedText(more); 42 m_iconLabel = new QLabel;
33 mb.exec(); 43 layout->addWidget(m_iconLabel, 0, 0);
44
45 QLabel *headLabel = new QLabel(QString("<qt><h3>%1</h3></qt>").arg(head));
46 layout->addWidget(headLabel, 0, 1);
47
48 QLabel *textLabel = new QLabel(text);
49 textLabel->setTextFormat(Qt::RichText);
50 layout->addWidget(textLabel, 1, 1);
51
52 QDialogButtonBox *bb = new QDialogButtonBox(QDialogButtonBox::Ok);
53 connect(bb, SIGNAL(accepted()), this, SLOT(accept()));
54 layout->addWidget(bb, 2, 0, 1, 2);
55
56 m_moreButton = bb->addButton(tr("More Details..."),
57 QDialogButtonBox::ActionRole);
58
59 connect(m_moreButton, SIGNAL(clicked()), this, SLOT(moreClicked()));
60
61 m_moreText = new QTextEdit();
62 m_moreText->setAcceptRichText(false);
63 m_moreText->document()->setPlainText(more);
64 m_moreText->setMinimumWidth(360);
65 m_moreText->setReadOnly(true);
66 m_moreText->setLineWrapMode(QTextEdit::NoWrap);
67
68 QFont font("Monospace");
69 font.setStyleHint(QFont::TypeWriter);
70 m_moreText->setFont(font);
71
72 layout->addWidget(m_moreText, 3, 0, 1, 2);
73
74 m_moreText->hide();
75
76 layout->setColumnStretch(1, 20);
77 setMinimumWidth(400);
78 }
79
80 MoreInformationDialog::~MoreInformationDialog()
81 {
34 } 82 }
35 83
36 void 84 void
37 MoreInformationDialog::information(QWidget *parent, QString title, 85 MoreInformationDialog::moreClicked()
38 QString text, QString more)
39 { 86 {
40 QMessageBox mb(QMessageBox::Information, 87 if (m_moreText->isVisible()) {
41 title, 88 m_moreText->hide();
42 text, 89 m_moreButton->setText(tr("Show Details..."));
43 QMessageBox::Ok, 90 } else {
44 parent, 91 m_moreText->show();
45 Qt::Dialog); 92 m_moreButton->setText(tr("Hide Details..."));
46 mb.setDefaultButton(QMessageBox::Ok); 93 }
47 mb.setDetailedText(more); 94 adjustSize();
48 mb.exec(); 95 }
96
97 void
98 MoreInformationDialog::setIcon(QIcon icon)
99 {
100 QStyle *style = qApp->style();
101 int iconSize = style->pixelMetric(QStyle::PM_MessageBoxIconSize, 0, this);
102 m_iconLabel->setPixmap(icon.pixmap(iconSize, iconSize));
49 } 103 }
50 104
51 void 105 void
52 MoreInformationDialog::warning(QWidget *parent, QString title, 106 MoreInformationDialog::critical(QWidget *parent, QString title, QString head,
53 QString text, QString more) 107 QString text, QString more)
54 { 108 {
55 QMessageBox mb(QMessageBox::Warning, 109 MoreInformationDialog d(title, head, text, more, parent);
56 title, 110 QStyle *style = qApp->style();
57 text, 111 d.setIcon(style->standardIcon(QStyle::SP_MessageBoxCritical, 0, &d));
58 QMessageBox::Ok, 112 d.exec();
59 parent,
60 Qt::Dialog);
61 mb.setDetailedText(more);
62 mb.exec();
63 } 113 }
64 114
115 void
116 MoreInformationDialog::information(QWidget *parent, QString title, QString head,
117 QString text, QString more)
118 {
119 MoreInformationDialog d(title, head, text, more, parent);
120 QStyle *style = qApp->style();
121 d.setIcon(style->standardIcon(QStyle::SP_MessageBoxInformation, 0, &d));
122 d.exec();
123 }
124
125 void
126 MoreInformationDialog::warning(QWidget *parent, QString title, QString head,
127 QString text, QString more)
128 {
129 MoreInformationDialog d(title, head, text, more, parent);
130 QStyle *style = qApp->style();
131 d.setIcon(style->standardIcon(QStyle::SP_MessageBoxWarning, 0, &d));
132 d.exec();
133 }
134