| 1 | /****************************************************************************
|
|---|
| 2 | ** $Id: qerrormessage.cpp 2 2005-11-16 15:49:26Z dmik $
|
|---|
| 3 | **
|
|---|
| 4 | ** Implementation of a nice qInstallMsgHandler() handler
|
|---|
| 5 | **
|
|---|
| 6 | ** Created : 000527, after Kalle Dalheimer's birthday
|
|---|
| 7 | **
|
|---|
| 8 | ** Copyright (C) 1992-2000 Trolltech AS. All rights reserved.
|
|---|
| 9 | **
|
|---|
| 10 | ** This file is part of the dialogs module of the Qt GUI Toolkit.
|
|---|
| 11 | **
|
|---|
| 12 | ** This file may be distributed under the terms of the Q Public License
|
|---|
| 13 | ** as defined by Trolltech AS of Norway and appearing in the file
|
|---|
| 14 | ** LICENSE.QPL included in the packaging of this file.
|
|---|
| 15 | **
|
|---|
| 16 | ** This file may be distributed and/or modified under the terms of the
|
|---|
| 17 | ** GNU General Public License version 2 as published by the Free Software
|
|---|
| 18 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
|---|
| 19 | ** packaging of this file.
|
|---|
| 20 | **
|
|---|
| 21 | ** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition
|
|---|
| 22 | ** licenses may use this file in accordance with the Qt Commercial License
|
|---|
| 23 | ** Agreement provided with the Software.
|
|---|
| 24 | **
|
|---|
| 25 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
|
|---|
| 26 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
|---|
| 27 | **
|
|---|
| 28 | ** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
|
|---|
| 29 | ** information about Qt Commercial License Agreements.
|
|---|
| 30 | ** See http://www.trolltech.com/qpl/ for QPL licensing information.
|
|---|
| 31 | ** See http://www.trolltech.com/gpl/ for GPL licensing information.
|
|---|
| 32 | **
|
|---|
| 33 | ** Contact info@trolltech.com if any conditions of this licensing are
|
|---|
| 34 | ** not clear to you.
|
|---|
| 35 | **
|
|---|
| 36 | **********************************************************************/
|
|---|
| 37 |
|
|---|
| 38 | #include "qerrormessage.h"
|
|---|
| 39 |
|
|---|
| 40 | #ifndef QT_NO_ERRORMESSAGE
|
|---|
| 41 |
|
|---|
| 42 | #include "qapplication.h"
|
|---|
| 43 | #include "qcheckbox.h"
|
|---|
| 44 | #include "qdict.h"
|
|---|
| 45 | #include "qlabel.h"
|
|---|
| 46 | #include "qlayout.h"
|
|---|
| 47 | #include "qmessagebox.h"
|
|---|
| 48 | #include "qpushbutton.h"
|
|---|
| 49 | #include "qstringlist.h"
|
|---|
| 50 | #include "qstylesheet.h"
|
|---|
| 51 | #include "qtextview.h"
|
|---|
| 52 |
|
|---|
| 53 | #include <stdio.h>
|
|---|
| 54 | #include <stdlib.h>
|
|---|
| 55 |
|
|---|
| 56 | class QErrorMessageTextView : public QTextView
|
|---|
| 57 | {
|
|---|
| 58 | public:
|
|---|
| 59 | QErrorMessageTextView( QWidget *parent, const char *name )
|
|---|
| 60 | : QTextView( parent, name ) { }
|
|---|
| 61 |
|
|---|
| 62 | virtual QSize minimumSizeHint() const;
|
|---|
| 63 | virtual QSize sizeHint() const;
|
|---|
| 64 | };
|
|---|
| 65 |
|
|---|
| 66 | QSize QErrorMessageTextView::minimumSizeHint() const
|
|---|
| 67 | {
|
|---|
| 68 | return QSize( 50, 50 );
|
|---|
| 69 | }
|
|---|
| 70 |
|
|---|
| 71 | QSize QErrorMessageTextView::sizeHint() const
|
|---|
| 72 | {
|
|---|
| 73 | return QSize( 250, 75 );
|
|---|
| 74 | }
|
|---|
| 75 |
|
|---|
| 76 | /*! \class QErrorMessage
|
|---|
| 77 |
|
|---|
| 78 | \brief The QErrorMessage class provides an error message display dialog.
|
|---|
| 79 |
|
|---|
| 80 | \ingroup dialogs
|
|---|
| 81 | \ingroup misc
|
|---|
| 82 |
|
|---|
| 83 | This is basically a QLabel and a "show this message again" checkbox which
|
|---|
| 84 | remembers what not to show.
|
|---|
| 85 |
|
|---|
| 86 | There are two ways to use this class:
|
|---|
| 87 | \list 1
|
|---|
| 88 | \i For production applications. In this context the class can be used to
|
|---|
| 89 | display messages which you don't need the user to see more than once. To use
|
|---|
| 90 | QErrorMessage like this, you create the dialog in the usual way and call the
|
|---|
| 91 | message() slot, or connect signals to it.
|
|---|
| 92 |
|
|---|
| 93 | \i For developers. In this context the static qtHandler() installs
|
|---|
| 94 | a message handler using qInstallMsgHandler() and creates a QErrorMessage
|
|---|
| 95 | that displays qDebug(), qWarning() and qFatal() messages.
|
|---|
| 96 | \endlist
|
|---|
| 97 |
|
|---|
| 98 | In both cases QErrorMessage will queue pending messages, and display
|
|---|
| 99 | them (or not) in order, as soon as the user presses Enter or clicks OK
|
|---|
| 100 | after seeing each message.
|
|---|
| 101 |
|
|---|
| 102 | \img qerrormessage.png
|
|---|
| 103 |
|
|---|
| 104 | \sa QMessageBox QStatusBar::message()
|
|---|
| 105 | */
|
|---|
| 106 |
|
|---|
| 107 | static QErrorMessage * qtMessageHandler = 0;
|
|---|
| 108 |
|
|---|
| 109 | static void deleteStaticcQErrorMessage() // post-routine
|
|---|
| 110 | {
|
|---|
| 111 | if ( qtMessageHandler ) {
|
|---|
| 112 | delete qtMessageHandler;
|
|---|
| 113 | qtMessageHandler = 0;
|
|---|
| 114 | }
|
|---|
| 115 | }
|
|---|
| 116 |
|
|---|
| 117 | static bool metFatal = FALSE;
|
|---|
| 118 |
|
|---|
| 119 | void jump( QtMsgType t, const char * m )
|
|---|
| 120 | {
|
|---|
| 121 | if ( !qtMessageHandler )
|
|---|
| 122 | return;
|
|---|
| 123 |
|
|---|
| 124 | QString rich;
|
|---|
| 125 |
|
|---|
| 126 | switch ( t ) {
|
|---|
| 127 | case QtDebugMsg:
|
|---|
| 128 | default:
|
|---|
| 129 | rich = QErrorMessage::tr( "Debug Message:" );
|
|---|
| 130 | break;
|
|---|
| 131 | case QtWarningMsg:
|
|---|
| 132 | rich = QErrorMessage::tr( "Warning:" );
|
|---|
| 133 | break;
|
|---|
| 134 | case QtFatalMsg:
|
|---|
| 135 | rich = QErrorMessage::tr( "Fatal Error:" );
|
|---|
| 136 | }
|
|---|
| 137 | rich = QString( "<p><b>%1</b></p>" ).arg( rich );
|
|---|
| 138 | rich += QStyleSheet::convertFromPlainText( m,
|
|---|
| 139 | QStyleSheetItem::WhiteSpaceNormal );
|
|---|
| 140 |
|
|---|
| 141 | // ### work around text engine quirk
|
|---|
| 142 | if ( rich.endsWith("</p>") )
|
|---|
| 143 | rich.truncate( rich.length() - 4 );
|
|---|
| 144 |
|
|---|
| 145 | if ( !metFatal ) {
|
|---|
| 146 | qtMessageHandler->message( rich );
|
|---|
| 147 | metFatal = ( t == QtFatalMsg );
|
|---|
| 148 | }
|
|---|
| 149 | }
|
|---|
| 150 |
|
|---|
| 151 |
|
|---|
| 152 | /*! Constructs and installs an error handler window.
|
|---|
| 153 | The parent \a parent and name \a name are passed on to the QDialog
|
|---|
| 154 | constructor.
|
|---|
| 155 | */
|
|---|
| 156 |
|
|---|
| 157 | QErrorMessage::QErrorMessage( QWidget * parent, const char * name )
|
|---|
| 158 | : QDialog( parent, name )
|
|---|
| 159 | {
|
|---|
| 160 | QGridLayout * grid = new QGridLayout( this, 3, 2, 11, 6 );
|
|---|
| 161 | icon = new QLabel( this, "qt_icon_lbl" );
|
|---|
| 162 | #ifndef QT_NO_MESSAGEBOX
|
|---|
| 163 | icon->setPixmap( QMessageBox::standardIcon(QMessageBox::Information) );
|
|---|
| 164 | #endif
|
|---|
| 165 | grid->addWidget( icon, 0, 0, AlignTop );
|
|---|
| 166 | errors = new QErrorMessageTextView( this, "errors" );
|
|---|
| 167 | grid->addWidget( errors, 0, 1 );
|
|---|
| 168 | again = new QCheckBox( tr( "&Show this message again" ), this, "again" );
|
|---|
| 169 | again->setChecked( TRUE );
|
|---|
| 170 | grid->addWidget( again, 1, 1, AlignTop + AlignAuto );
|
|---|
| 171 | ok = new QPushButton( tr( "&OK" ), this, "ok" );
|
|---|
| 172 | connect( ok, SIGNAL(clicked()), this, SLOT(accept()) );
|
|---|
| 173 | ok->setFocus();
|
|---|
| 174 | grid->addMultiCellWidget( ok, 2, 2, 0, 1, AlignCenter );
|
|---|
| 175 | grid->setColStretch( 1, 42 );
|
|---|
| 176 | grid->setRowStretch( 0, 42 );
|
|---|
| 177 | pending = new QStringList;
|
|---|
| 178 | doNotShow = new QDict<int>;
|
|---|
| 179 | }
|
|---|
| 180 |
|
|---|
| 181 |
|
|---|
| 182 | /*! Destroys the object and frees any allocated resources. Notably,
|
|---|
| 183 | the list of "do not show again" messages is deleted. */
|
|---|
| 184 |
|
|---|
| 185 | QErrorMessage::~QErrorMessage()
|
|---|
| 186 | {
|
|---|
| 187 | if ( this == qtMessageHandler ) {
|
|---|
| 188 | qtMessageHandler = 0;
|
|---|
| 189 | QtMsgHandler tmp = qInstallMsgHandler( 0 );
|
|---|
| 190 | // in case someone else has later stuck in another...
|
|---|
| 191 | if ( tmp != jump )
|
|---|
| 192 | qInstallMsgHandler( tmp );
|
|---|
| 193 | }
|
|---|
| 194 |
|
|---|
| 195 | delete pending;
|
|---|
| 196 | delete doNotShow;
|
|---|
| 197 | }
|
|---|
| 198 |
|
|---|
| 199 |
|
|---|
| 200 | /*! \reimp */
|
|---|
| 201 |
|
|---|
| 202 | void QErrorMessage::done( int a )
|
|---|
| 203 | {
|
|---|
| 204 | int dummy = 0;
|
|---|
| 205 | if ( !again->isChecked() )
|
|---|
| 206 | doNotShow->insert( errors->text(), &dummy );
|
|---|
| 207 | if ( !nextPending() ) {
|
|---|
| 208 | QDialog::done( a );
|
|---|
| 209 | if ( this == qtMessageHandler && metFatal )
|
|---|
| 210 | exit( 1 );
|
|---|
| 211 | }
|
|---|
| 212 | }
|
|---|
| 213 |
|
|---|
| 214 |
|
|---|
| 215 | /*! Returns a pointer to a QErrorMessage object that outputs the
|
|---|
| 216 | default Qt messages. This function creates such an object, if there
|
|---|
| 217 | isn't one already.
|
|---|
| 218 | */
|
|---|
| 219 |
|
|---|
| 220 | QErrorMessage * QErrorMessage::qtHandler()
|
|---|
| 221 | {
|
|---|
| 222 | if ( !qtMessageHandler ) {
|
|---|
| 223 | qtMessageHandler = new QErrorMessage( 0, "automatic message handler" );
|
|---|
| 224 | qAddPostRoutine( deleteStaticcQErrorMessage ); // clean up
|
|---|
| 225 | #ifndef QT_NO_WIDGET_TOPEXTRA
|
|---|
| 226 | if ( qApp->mainWidget() )
|
|---|
| 227 | qtMessageHandler->setCaption( qApp->mainWidget()->caption() );
|
|---|
| 228 | #endif
|
|---|
| 229 | qInstallMsgHandler( jump );
|
|---|
| 230 | }
|
|---|
| 231 | return qtMessageHandler;
|
|---|
| 232 | }
|
|---|
| 233 |
|
|---|
| 234 |
|
|---|
| 235 | /*! \internal */
|
|---|
| 236 |
|
|---|
| 237 | bool QErrorMessage::nextPending()
|
|---|
| 238 | {
|
|---|
| 239 | while ( !pending->isEmpty() ) {
|
|---|
| 240 | QString p = *pending->begin();
|
|---|
| 241 | pending->remove( pending->begin() );
|
|---|
| 242 | if ( !p.isEmpty() && !doNotShow->find( p ) ) {
|
|---|
| 243 | errors->setText( p );
|
|---|
| 244 | return TRUE;
|
|---|
| 245 | }
|
|---|
| 246 | }
|
|---|
| 247 | return FALSE;
|
|---|
| 248 | }
|
|---|
| 249 |
|
|---|
| 250 |
|
|---|
| 251 | /*! Shows message \a m and returns immediately. If the user has requested
|
|---|
| 252 | that \a m not be shown, this function does nothing.
|
|---|
| 253 |
|
|---|
| 254 | Normally, \a m is shown at once, but if there are pending messages,
|
|---|
| 255 | \a m is queued for later display.
|
|---|
| 256 | */
|
|---|
| 257 |
|
|---|
| 258 | void QErrorMessage::message( const QString & m )
|
|---|
| 259 | {
|
|---|
| 260 | if ( doNotShow->find( m ) )
|
|---|
| 261 | return;
|
|---|
| 262 | pending->append( m );
|
|---|
| 263 | if ( !isVisible() && nextPending() )
|
|---|
| 264 | show();
|
|---|
| 265 | }
|
|---|
| 266 |
|
|---|
| 267 | #endif // QT_NO_ERRORMESSAGE
|
|---|