1
#ifndef __cxxtest__QtGui_h__
2
#define __cxxtest__QtGui_h__
5
// The QtGui displays a simple progress bar using the Qt Toolkit. It
6
// has been tested with versions 2.x and 3.x.
8
// Apart from normal Qt command-line arguments, it accepts the following options:
9
// -minimized Start minimized, pop up on error
10
// -keep Don't close the window at the end
11
// -title TITLE Set the window caption
13
// If both are -minimized and -keep specified, GUI will only keep the
14
// window if it's in focus.
17
#include <cxxtest/Gui.h>
19
#include <qapplication.h>
23
#include <qmessagebox.h>
25
#include <qprogressbar.h>
26
#include <qstatusbar.h>
30
class QtGui : public GuiListener
33
void enterGui( int &argc, char **argv )
35
parseCommandLine( argc, argv );
36
createApplication( argc, argv );
39
void enterWorld( const WorldDescription &wd )
45
void guiEnterSuite( const char *suiteName )
47
showSuiteName( suiteName );
50
void guiEnterTest( const char *suiteName, const char *testName )
52
setCaption( suiteName, testName );
54
showTestName( testName );
55
showTestsDone( _progressBar->progress() );
61
setColor( 255, 255, 0 );
62
setIcon( QMessageBox::Warning );
69
if ( _startMinimized && _mainWindow->isMinimized() )
71
setColor( 255, 0, 0 );
72
setIcon( QMessageBox::Critical );
84
_mainWindow->close( true );
89
bool _startMinimized, _keep;
90
unsigned _numTotalTests;
91
QString _strTotalTests;
92
QApplication *_application;
95
QProgressBar *_progressBar;
96
QStatusBar *_statusBar;
97
QLabel *_suiteName, *_testName, *_testsDone;
99
void parseCommandLine( int argc, char **argv )
101
_startMinimized = _keep = false;
104
for ( int i = 1; i < argc; ++ i ) {
105
QString arg( argv[i] );
106
if ( arg == "-minimized" )
107
_startMinimized = true;
108
else if ( arg == "-keep" )
110
else if ( arg == "-title" && (i + 1 < argc) )
115
void createApplication( int &argc, char **argv )
117
_application = new QApplication( argc, argv );
120
void createWindow( const WorldDescription &wd )
127
if ( _startMinimized )
135
getTotalTests( tracker().world() );
138
void getTotalTests( const WorldDescription &wd )
140
_numTotalTests = wd.numTotalTests();
141
char s[WorldDescription::MAX_STRLEN_TOTAL_TESTS];
142
_strTotalTests = wd.strTotalTests( s );
145
void createMainWindow()
147
_mainWindow = new QWidget();
148
_layout = new QVBoxLayout( _mainWindow );
151
void createProgressBar()
153
_layout->addWidget( _progressBar = new QProgressBar( _numTotalTests, _mainWindow ) );
154
_progressBar->setProgress( 0 );
155
setColor( 0, 255, 0 );
156
setIcon( QMessageBox::Information );
159
void createStatusBar()
161
_layout->addWidget( _statusBar = new QStatusBar( _mainWindow ) );
162
_statusBar->addWidget( _suiteName = new QLabel( _statusBar ), 2 );
163
_statusBar->addWidget( _testName = new QLabel( _statusBar ), 4 );
164
_statusBar->addWidget( _testsDone = new QLabel( _statusBar ), 1 );
169
_application->setMainWidget( _mainWindow );
174
_mainWindow->showMinimized();
179
_mainWindow->showNormal();
183
void setCaption( const QString &suiteName, const QString &testName )
185
_mainWindow->setCaption( _title + " - " + suiteName + "::" + testName + "()" );
188
void showSuiteName( const QString &suiteName )
190
_suiteName->setText( "class " + suiteName );
193
void advanceProgressBar()
195
_progressBar->setProgress( _progressBar->progress() + 1 );
198
void showTestName( const QString &testName )
200
_testName->setText( testName + "()" );
203
void showTestsDone( unsigned testsDone )
205
_testsDone->setText( asString( testsDone ) + " of " + _strTotalTests );
208
static QString asString( unsigned n )
210
return QString::number( n );
213
void setColor( int r, int g, int b )
215
QPalette palette = _progressBar->palette();
216
palette.setColor( QColorGroup::Highlight, QColor( r, g, b ) );
217
_progressBar->setPalette( palette );
220
void setIcon( QMessageBox::Icon icon )
222
#if QT_VERSION >= 0x030000
223
_mainWindow->setIcon( QMessageBox::standardIcon( icon ) );
224
#else // Qt version < 3.0.0
225
_mainWindow->setIcon( QMessageBox::standardIcon( icon, QApplication::style().guiStyle() ) );
231
_application->processEvents();
236
QWidget *desktop = QApplication::desktop();
237
int xCenter = desktop->x() + (desktop->width() / 2);
238
int yCenter = desktop->y() + (desktop->height() / 2);
240
int windowWidth = (desktop->width() * 4) / 5;
241
int windowHeight = _mainWindow->height();
242
_mainWindow->setGeometry( xCenter - (windowWidth / 2), yCenter - (windowHeight / 2), windowWidth, windowHeight );
249
if ( !_startMinimized )
251
return (_mainWindow == _application->activeWindow());
256
QString summary = _strTotalTests + (_numTotalTests == 1 ? " test" : " tests");
257
if ( tracker().failedTests() )
258
summary = "Failed " + asString( tracker().failedTests() ) + " of " + summary;
260
summary = summary + " passed";
262
_mainWindow->setCaption( _title + " - " + summary );
264
_statusBar->removeWidget( _suiteName );
265
_statusBar->removeWidget( _testName );
266
_testsDone->setText( summary );
271
#endif // __cxxtest__QtGui_h__