Mercurial > hg > easyhg
changeset 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 |
files | mainwindow.cpp moreinformationdialog.cpp moreinformationdialog.h |
diffstat | 3 files changed, 134 insertions(+), 39 deletions(-) [+] |
line wrap: on
line diff
--- a/mainwindow.cpp Mon Jan 31 17:23:10 2011 +0000 +++ b/mainwindow.cpp Mon Feb 21 14:23:02 2011 +0000 @@ -1606,7 +1606,7 @@ runner->hide(); MoreInformationDialog::information(this, tr("Pull complete"), - format1(report), output); + report, "", output); /*!!! report = format3(report, tr("The pull command output was:"), output);
--- 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(); +} +
--- a/moreinformationdialog.h Mon Jan 31 17:23:10 2011 +0000 +++ b/moreinformationdialog.h Mon Feb 21 14:23:02 2011 +0000 @@ -19,20 +19,45 @@ #define MORE_INFORMATION_DIALOG_H #include <QString> +#include <QDialog> -class QWidget; +class QLabel; +class QTextEdit; +class QPushButton; /** * Provide methods like the QMessageBox static methods, to call up - * dialogs with "More information" buttons in them + * dialogs with "More information" buttons in them. QMessageBox does + * have an optional additional-details field, but it doesn't behave + * quite as we'd like with regard to layout */ -class MoreInformationDialog +class MoreInformationDialog : public QDialog { + Q_OBJECT + public: - static void critical(QWidget *parent, QString title, QString text, QString more); - static void information(QWidget *parent, QString title, QString text, QString more); - static void warning(QWidget *parent, QString title, QString text, QString more); + MoreInformationDialog(QString title, + QString head, + QString text, + QString more, + QWidget *parent = 0); + + ~MoreInformationDialog(); + + void setIcon(QIcon); + + static void critical(QWidget *parent, QString title, QString head, QString text, QString more); + static void information(QWidget *parent, QString title, QString head, QString text, QString more); + static void warning(QWidget *parent, QString title, QString head, QString text, QString more); + +private slots: + void moreClicked(); + +private: + QLabel *m_iconLabel; + QPushButton *m_moreButton; + QTextEdit *m_moreText; }; #endif