* ./hurricane/src/viewer:

- New: In ExceptionWidget, mimic more closely the behavior of a QDialog.
        Uses QTextEdit/QTextDocument for the trace. Static "all in one" methods,
        build from various exceptions/Errors & QString. Automatic window centering
        when the trace is displayed.
This commit is contained in:
Jean-Paul Chaput 2011-01-10 23:58:31 +00:00
parent 5273be039e
commit b8e42d2efc
4 changed files with 61 additions and 54 deletions

View File

@ -127,7 +127,7 @@ namespace Hurricane {
ostringstream where;
for ( size_t depth=0 ; depth<_stack.size() ; ++depth )
where << "<tt>[" << setw(2) << setfill('0') << depth << "] " << _stack[depth] << "</tt><br>\n";
where << "<tt>[" << setw(2) << setfill('0') << depth << "] " << _stack[depth] << "</tt><br>";
return where.str();
}

View File

@ -23,17 +23,20 @@
// x-----------------------------------------------------------------x
#include <csignal>
#include <memory>
#include <QCloseEvent>
#include <QLabel>
#include <QPushButton>
#include <QCheckBox>
#include <QTextEdit>
#include <QScrollArea>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QFrame>
#include <QFont>
#include <QFontMetrics>
#include "hurricane/Error.h"
#include "hurricane/Exception.h"
#include "hurricane/viewer/Graphics.h"
#include "hurricane/viewer/ExceptionWidget.h"
@ -41,13 +44,39 @@
namespace Hurricane {
using std::auto_ptr;
void ExceptionWidget::run ( Error& e )
{ run ( e.htmlWhat().c_str(), e.htmlWhere().c_str() ); }
void ExceptionWidget::run ( Exception& e )
{ run ( e.htmlWhat().c_str(), "" ); }
void ExceptionWidget::run ( exception& e )
{ run ( e.what() ); }
void ExceptionWidget::run ( const QString& what, const QString& where )
{
ExceptionWidget* ew = new ExceptionWidget();
ew->setMessage ( what );
if ( not where.isEmpty() )
ew->setTrace ( where );
if ( ew->exec() == QDialog::Rejected )
kill ( getpid(), SIGSEGV );
}
ExceptionWidget::ExceptionWidget ( QWidget* parent )
: QDialog (parent)
, _header (new QLabel())
, _message (new QLabel())
, _trace (new QLabel())
, _traceArea(new QScrollArea())
, _trace (new QTextEdit())
{
setAttribute ( Qt::WA_DeleteOnClose );
setModal ( true );
@ -61,15 +90,13 @@ namespace Hurricane {
_message->setTextFormat ( Qt::RichText );
_message->setText ( "<b>Oups! I did it again!</b>" );
_trace->setTextFormat ( Qt::RichText );
_trace->setText ( "<b>No program trace sets yet.</b>" );
_trace->setSizePolicy ( QSizePolicy::Ignored, QSizePolicy::Ignored );
_traceArea->setWidget ( _trace );
_traceArea->hide ();
//_traceArea->setSizePolicy ( QSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding) );
_traceArea->setMinimumSize ( QSize(700,500) );
_trace->setTextInteractionFlags ( Qt::TextBrowserInteraction );
_trace->setAcceptRichText ( true );
_trace->setHtml ( "<b>No program trace sets yet.</b>" );
_trace->setMinimumSize ( QSize(800,500) );
_trace->setLineWrapMode ( QTextEdit::NoWrap );
//_trace->setSizePolicy ( QSizePolicy::Ignored, QSizePolicy::Ignored );
_trace->hide ();
QCheckBox* showTrace = new QCheckBox ();
showTrace->setText ( "Show Program Trace" );
@ -107,7 +134,7 @@ namespace Hurricane {
vLayout1->addWidget ( _message , 0, Qt::AlignLeft );
vLayout1->addWidget ( separator );
vLayout1->addWidget ( showTrace , 0, Qt::AlignLeft );
vLayout1->addWidget ( _traceArea, 0, Qt::AlignLeft );
vLayout1->addWidget ( _trace , 0, Qt::AlignLeft );
vLayout1->addSpacing ( 10 );
vLayout1->addLayout ( hLayout2 , Qt::AlignCenter );
@ -128,9 +155,7 @@ namespace Hurricane {
void ExceptionWidget::closeEvent ( QCloseEvent* event )
{
event->ignore ();
}
{ event->ignore (); }
void ExceptionWidget::setMessage ( const QString& message )
@ -151,23 +176,15 @@ namespace Hurricane {
void ExceptionWidget::setTrace ( const QString& where )
{
QFont font = Graphics::getFixedFont(QFont::Bold);
QFontMetrics metrics = QFontMetrics ( font );
QSize textSize = metrics.size ( 0, where );
//textSize.setWidth ( textSize.width () / 2 );
textSize.setHeight ( textSize.height() + 70 );
_trace->setText ( where );
_trace->resize ( textSize );
}
{ _trace->setHtml ( where ); }
void ExceptionWidget::_showTrace ( int state )
{
if ( state == Qt::Checked ) _traceArea->show ();
else _traceArea->hide ();
if ( state == Qt::Checked ) _trace->show ();
else _trace->hide ();
adjustPosition ( NULL );
}

View File

@ -23,10 +23,8 @@
// x-----------------------------------------------------------------x
#include <csignal>
#include <iostream>
#include <iomanip>
#include "hurricane/Error.h"
#include "hurricane/viewer/Graphics.h"
#include "hurricane/viewer/ExceptionWidget.h"
@ -35,7 +33,6 @@
namespace Hurricane {
using std::cerr;
using std::endl;
using std::setw;
@ -80,33 +77,19 @@ namespace Hurricane {
return QApplication::notify ( object, event );
}
catch ( Error& e ) {
ExceptionWidget* ew = new ExceptionWidget ();
ew->setMessage ( e.htmlWhat ().c_str() );
ew->setTrace ( e.htmlWhere().c_str() );
if ( ew->exec() == QDialog::Rejected )
kill ( getpid(), SIGSEGV );
ExceptionWidget::run ( e );
}
catch ( Exception& e ) {
ExceptionWidget* ew = new ExceptionWidget ();
ew->setMessage ( e.htmlWhat().c_str() );
if ( ew->exec() == QDialog::Rejected )
kill ( getpid(), SIGSEGV );
ExceptionWidget::run ( e );
}
catch ( exception& e ) {
ExceptionWidget* ew = new ExceptionWidget ();
ew->setMessage ( e.what() );
if ( ew->exec() == QDialog::Rejected )
kill ( getpid(), SIGSEGV );
}
catch ( ... ) {
static const char* message =
"&nbsp;&nbsp;Unmanaged exception, neither a <b>Hurricane::Error</b><br>"
"nor a <b>std::exception</b>.";
ExceptionWidget* ew = new ExceptionWidget ();
ew->setMessage ( message );
if ( ew->exec() == QDialog::Rejected )
kill ( getpid(), SIGSEGV );
ExceptionWidget::run ( message );
}
return true;
}

View File

@ -26,17 +26,25 @@
#ifndef __HURRICANE_EXCEPTION_WIDGET__
#define __HURRICANE_EXCEPTION_WIDGET__
#include <exception>
#include <QDialog>
class QLabel;
class QScrollArea;
class QTextEdit;
namespace Hurricane {
class Error;
class Exception;
class ExceptionWidget : public QDialog {
Q_OBJECT;
public:
static void run ( Error& );
static void run ( Exception& );
static void run ( std::exception& );
static void run ( const QString&, const QString& where="" );
public:
ExceptionWidget ( QWidget* parent=NULL);
void setMessage ( const QString& );
@ -48,8 +56,7 @@ namespace Hurricane {
private:
QLabel* _header;
QLabel* _message;
QLabel* _trace;
QScrollArea* _traceArea;
QTextEdit* _trace;
};