diff 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
line wrap: on
line diff
--- a/moreinformationdialog.cpp	Mon Jan 31 17:23:10 2011 +0000
+++ b/moreinformationdialog.cpp	Mon Feb 21 14:23:02 2011 +0000
@@ -18,47 +18,117 @@
 #include "moreinformationdialog.h"
 
 #include <QMessageBox>
+#include <QLabel>
+#include <QGridLayout>
+#include <QTextEdit>
+#include <QDialogButtonBox>
+#include <QPushButton>
+#include <QApplication>
+#include <QStyle>
 
-void
-MoreInformationDialog::critical(QWidget *parent, QString title,
-				QString text, QString more)
+MoreInformationDialog::MoreInformationDialog(QString title,
+                                             QString head,
+                                             QString text,
+                                             QString more,
+                                             QWidget *parent) :
+    QDialog(parent)
 {
-    QMessageBox mb(QMessageBox::Critical,
-		   title,
-		   text,
-		   QMessageBox::Ok,
-		   parent,
-		   Qt::Dialog);
-    mb.setDetailedText(more);
-    mb.exec();
+    setWindowTitle(title);
+
+    QGridLayout *layout = new QGridLayout;
+    layout->setSpacing(10);
+    setLayout(layout);
+
+    m_iconLabel = new QLabel;
+    layout->addWidget(m_iconLabel, 0, 0);
+
+    QLabel *headLabel = new QLabel(QString("<qt><h3>%1</h3></qt>").arg(head));
+    layout->addWidget(headLabel, 0, 1);
+
+    QLabel *textLabel = new QLabel(text);
+    textLabel->setTextFormat(Qt::RichText);
+    layout->addWidget(textLabel, 1, 1);
+
+    QDialogButtonBox *bb = new QDialogButtonBox(QDialogButtonBox::Ok);
+    connect(bb, SIGNAL(accepted()), this, SLOT(accept()));
+    layout->addWidget(bb, 2, 0, 1, 2);
+
+    m_moreButton = bb->addButton(tr("More Details..."),
+                                 QDialogButtonBox::ActionRole);
+
+    connect(m_moreButton, SIGNAL(clicked()), this, SLOT(moreClicked()));
+
+    m_moreText = new QTextEdit();
+    m_moreText->setAcceptRichText(false);
+    m_moreText->document()->setPlainText(more);
+    m_moreText->setMinimumWidth(360);
+    m_moreText->setReadOnly(true);
+    m_moreText->setLineWrapMode(QTextEdit::NoWrap);
+
+    QFont font("Monospace");
+    font.setStyleHint(QFont::TypeWriter);
+    m_moreText->setFont(font);
+
+    layout->addWidget(m_moreText, 3, 0, 1, 2);
+
+    m_moreText->hide();
+
+    layout->setColumnStretch(1, 20);
+    setMinimumWidth(400);
+}
+
+MoreInformationDialog::~MoreInformationDialog()
+{
 }
 
 void
-MoreInformationDialog::information(QWidget *parent, QString title,
-				   QString text, QString more)
+MoreInformationDialog::moreClicked()
 {
-    QMessageBox mb(QMessageBox::Information,
-		   title,
-		   text,
-		   QMessageBox::Ok,
-		   parent,
-		   Qt::Dialog);
-    mb.setDefaultButton(QMessageBox::Ok);
-    mb.setDetailedText(more);
-    mb.exec();
+    if (m_moreText->isVisible()) {
+        m_moreText->hide();
+        m_moreButton->setText(tr("Show Details..."));
+    } else {
+        m_moreText->show();
+        m_moreButton->setText(tr("Hide Details..."));
+    }
+    adjustSize();
+}        
+
+void
+MoreInformationDialog::setIcon(QIcon icon)
+{
+    QStyle *style = qApp->style();
+    int iconSize = style->pixelMetric(QStyle::PM_MessageBoxIconSize, 0, this);
+    m_iconLabel->setPixmap(icon.pixmap(iconSize, iconSize));
 }
 
 void
-MoreInformationDialog::warning(QWidget *parent, QString title,
-			       QString text, QString more)
+MoreInformationDialog::critical(QWidget *parent, QString title, QString head,
+				QString text, QString more)
 {
-    QMessageBox mb(QMessageBox::Warning,
-		   title,
-		   text,
-		   QMessageBox::Ok,
-		   parent,
-		   Qt::Dialog);
-    mb.setDetailedText(more);
-    mb.exec();
+    MoreInformationDialog d(title, head, text, more, parent);
+    QStyle *style = qApp->style();
+    d.setIcon(style->standardIcon(QStyle::SP_MessageBoxCritical, 0, &d));
+    d.exec();
 }
 
+void
+MoreInformationDialog::information(QWidget *parent, QString title, QString head,
+                                   QString text, QString more)
+{
+    MoreInformationDialog d(title, head, text, more, parent);
+    QStyle *style = qApp->style();
+    d.setIcon(style->standardIcon(QStyle::SP_MessageBoxInformation, 0, &d));
+    d.exec();
+}
+
+void
+MoreInformationDialog::warning(QWidget *parent, QString title, QString head,
+                               QString text, QString more)
+{
+    MoreInformationDialog d(title, head, text, more, parent);
+    QStyle *style = qApp->style();
+    d.setIcon(style->standardIcon(QStyle::SP_MessageBoxWarning, 0, &d));
+    d.exec();
+}
+