Loading...
Searching...
No Matches
QtGui.h
Go to the documentation of this file.
#ifndef __cxxtest__QtGui_h__
#define __cxxtest__QtGui_h__
//
// The QtGui displays a simple progress bar using the Qt Toolkit. It
// has been tested with versions 2.x and 3.x.
//
// Apart from normal Qt command-line arguments, it accepts the following options:
// -minimized Start minimized, pop up on error
// -keep Don't close the window at the end
// -title TITLE Set the window caption
//
// If both are -minimized and -keep specified, GUI will only keep the
// window if it's in focus.
//
#include <cxxtest/Gui.h>
#include <qapplication.h>
#include <qglobal.h>
#include <qlabel.h>
#include <qlayout.h>
#include <qmessagebox.h>
#include <qpixmap.h>
#include <qprogressbar.h>
#include <qstatusbar.h>
namespace CxxTest
{
class QtGui : public GuiListener
{
public:
void enterGui( int &argc, char **argv )
{
parseCommandLine( argc, argv );
createApplication( argc, argv );
}
void enterWorld( const WorldDescription &wd )
{
createWindow( wd );
processEvents();
}
void guiEnterSuite( const char *suiteName )
{
showSuiteName( suiteName );
}
void guiEnterTest( const char *suiteName, const char *testName )
{
setCaption( suiteName, testName );
advanceProgressBar();
showTestName( testName );
showTestsDone( _progressBar->progress() );
processEvents();
}
void yellowBar()
{
setColor( 255, 255, 0 );
setIcon( QMessageBox::Warning );
getTotalTests();
processEvents();
}
void redBar()
{
if ( _startMinimized && _mainWindow->isMinimized() )
showNormal();
setColor( 255, 0, 0 );
setIcon( QMessageBox::Critical );
getTotalTests();
processEvents();
}
void leaveGui()
{
if ( keep() ) {
showSummary();
_application->exec();
}
else
_mainWindow->close( true );
}
private:
QString _title;
bool _startMinimized, _keep;
unsigned _numTotalTests;
QString _strTotalTests;
QApplication *_application;
QWidget *_mainWindow;
QVBoxLayout *_layout;
QProgressBar *_progressBar;
QStatusBar *_statusBar;
QLabel *_suiteName, *_testName, *_testsDone;
void parseCommandLine( int argc, char **argv )
{
_startMinimized = _keep = false;
_title = argv[0];
for ( int i = 1; i < argc; ++ i ) {
QString arg( argv[i] );
if ( arg == "-minimized" )
_startMinimized = true;
else if ( arg == "-keep" )
_keep = true;
else if ( arg == "-title" && (i + 1 < argc) )
_title = argv[++i];
}
}
void createApplication( int &argc, char **argv )
{
_application = new QApplication( argc, argv );
}
void createWindow( const WorldDescription &wd )
{
getTotalTests( wd );
createMainWindow();
createProgressBar();
createStatusBar();
setMainWidget();
if ( _startMinimized )
showMinimized();
else
showNormal();
}
void getTotalTests()
{
getTotalTests( tracker().world() );
}
void getTotalTests( const WorldDescription &wd )
{
_numTotalTests = wd.numTotalTests();
char s[WorldDescription::MAX_STRLEN_TOTAL_TESTS];
_strTotalTests = wd.strTotalTests( s );
}
void createMainWindow()
{
_mainWindow = new QWidget();
_layout = new QVBoxLayout( _mainWindow );
}
void createProgressBar()
{
_layout->addWidget( _progressBar = new QProgressBar( _numTotalTests, _mainWindow ) );
_progressBar->setProgress( 0 );
setColor( 0, 255, 0 );
setIcon( QMessageBox::Information );
}
void createStatusBar()
{
_layout->addWidget( _statusBar = new QStatusBar( _mainWindow ) );
_statusBar->addWidget( _suiteName = new QLabel( _statusBar ), 2 );
_statusBar->addWidget( _testName = new QLabel( _statusBar ), 4 );
_statusBar->addWidget( _testsDone = new QLabel( _statusBar ), 1 );
}
void setMainWidget()
{
_application->setMainWidget( _mainWindow );
}
void showMinimized()
{
_mainWindow->showMinimized();
}
void showNormal()
{
_mainWindow->showNormal();
centerWindow();
}
void setCaption( const QString &suiteName, const QString &testName )
{
_mainWindow->setCaption( _title + " - " + suiteName + "::" + testName + "()" );
}
void showSuiteName( const QString &suiteName )
{
_suiteName->setText( "class " + suiteName );
}
void advanceProgressBar()
{
_progressBar->setProgress( _progressBar->progress() + 1 );
}
void showTestName( const QString &testName )
{
_testName->setText( testName + "()" );
}
void showTestsDone( unsigned testsDone )
{
_testsDone->setText( asString( testsDone ) + " of " + _strTotalTests );
}
static QString asString( unsigned n )
{
return QString::number( n );
}
void setColor( int r, int g, int b )
{
QPalette palette = _progressBar->palette();
palette.setColor( QColorGroup::Highlight, QColor( r, g, b ) );
_progressBar->setPalette( palette );
}
void setIcon( QMessageBox::Icon icon )
{
#if QT_VERSION >= 0x030000
_mainWindow->setIcon( QMessageBox::standardIcon( icon ) );
#else // Qt version < 3.0.0
_mainWindow->setIcon( QMessageBox::standardIcon( icon, QApplication::style().guiStyle() ) );
#endif // QT_VERSION
}
void processEvents()
{
_application->processEvents();
}
void centerWindow()
{
QWidget *desktop = QApplication::desktop();
int xCenter = desktop->x() + (desktop->width() / 2);
int yCenter = desktop->y() + (desktop->height() / 2);
int windowWidth = (desktop->width() * 4) / 5;
int windowHeight = _mainWindow->height();
_mainWindow->setGeometry( xCenter - (windowWidth / 2), yCenter - (windowHeight / 2), windowWidth, windowHeight );
}
bool keep()
{
if ( !_keep )
return false;
if ( !_startMinimized )
return true;
return (_mainWindow == _application->activeWindow());
}
void showSummary()
{
QString summary = _strTotalTests + (_numTotalTests == 1 ? " test" : " tests");
if ( tracker().failedTests() )
summary = "Failed " + asString( tracker().failedTests() ) + " of " + summary;
else
summary = summary + " passed";
_mainWindow->setCaption( _title + " - " + summary );
_statusBar->removeWidget( _suiteName );
_statusBar->removeWidget( _testName );
_testsDone->setText( summary );
}
};
};
#endif // __cxxtest__QtGui_h__
Gui.h
i
int i
Definition cfEzgcd.cc:132
g
g
Definition cfModGcd.cc:4098
b
CanonicalForm b
Definition cfModGcd.cc:4111
CxxTest::GuiListener::GuiListener
GuiListener()
Definition Gui.h:16
CxxTest::QtGui
Definition QtGui.h:31
CxxTest::QtGui::_mainWindow
QWidget * _mainWindow
Definition QtGui.h:93
CxxTest::QtGui::showTestsDone
void showTestsDone(unsigned testsDone)
Definition QtGui.h:203
CxxTest::QtGui::_progressBar
QProgressBar * _progressBar
Definition QtGui.h:95
CxxTest::QtGui::_testsDone
QLabel * _testsDone
Definition QtGui.h:97
CxxTest::QtGui::createApplication
void createApplication(int &argc, char **argv)
Definition QtGui.h:115
CxxTest::QtGui::yellowBar
void yellowBar()
Definition QtGui.h:59
CxxTest::QtGui::parseCommandLine
void parseCommandLine(int argc, char **argv)
Definition QtGui.h:99
CxxTest::QtGui::showSummary
void showSummary()
Definition QtGui.h:254
CxxTest::QtGui::guiEnterSuite
void guiEnterSuite(const char *suiteName)
Definition QtGui.h:45
CxxTest::QtGui::showMinimized
void showMinimized()
Definition QtGui.h:172
CxxTest::QtGui::_startMinimized
bool _startMinimized
Definition QtGui.h:89
CxxTest::QtGui::centerWindow
void centerWindow()
Definition QtGui.h:234
CxxTest::QtGui::createStatusBar
void createStatusBar()
Definition QtGui.h:159
CxxTest::QtGui::setCaption
void setCaption(const QString &suiteName, const QString &testName)
Definition QtGui.h:183
CxxTest::QtGui::_strTotalTests
QString _strTotalTests
Definition QtGui.h:91
CxxTest::QtGui::_numTotalTests
unsigned _numTotalTests
Definition QtGui.h:90
CxxTest::QtGui::showSuiteName
void showSuiteName(const QString &suiteName)
Definition QtGui.h:188
CxxTest::QtGui::setMainWidget
void setMainWidget()
Definition QtGui.h:167
CxxTest::QtGui::advanceProgressBar
void advanceProgressBar()
Definition QtGui.h:193
CxxTest::QtGui::processEvents
void processEvents()
Definition QtGui.h:229
CxxTest::QtGui::_suiteName
QLabel * _suiteName
Definition QtGui.h:97
CxxTest::QtGui::enterGui
void enterGui(int &argc, char **argv)
Definition QtGui.h:33
CxxTest::QtGui::showNormal
void showNormal()
Definition QtGui.h:177
CxxTest::QtGui::getTotalTests
void getTotalTests(const WorldDescription &wd)
Definition QtGui.h:138
CxxTest::QtGui::_testName
QLabel * _testName
Definition QtGui.h:97
CxxTest::QtGui::_statusBar
QStatusBar * _statusBar
Definition QtGui.h:96
CxxTest::QtGui::setIcon
void setIcon(QMessageBox::Icon icon)
Definition QtGui.h:220
CxxTest::QtGui::getTotalTests
void getTotalTests()
Definition QtGui.h:133
CxxTest::QtGui::enterWorld
void enterWorld(const WorldDescription &wd)
Definition QtGui.h:39
CxxTest::QtGui::setColor
void setColor(int r, int g, int b)
Definition QtGui.h:213
CxxTest::QtGui::createWindow
void createWindow(const WorldDescription &wd)
Definition QtGui.h:120
CxxTest::QtGui::createProgressBar
void createProgressBar()
Definition QtGui.h:151
CxxTest::QtGui::redBar
void redBar()
Definition QtGui.h:67
CxxTest::QtGui::_keep
bool _keep
Definition QtGui.h:89
CxxTest::QtGui::createMainWindow
void createMainWindow()
Definition QtGui.h:145
CxxTest::QtGui::_title
QString _title
Definition QtGui.h:88
CxxTest::QtGui::keep
bool keep()
Definition QtGui.h:245
CxxTest::QtGui::leaveGui
void leaveGui()
Definition QtGui.h:77
CxxTest::QtGui::guiEnterTest
void guiEnterTest(const char *suiteName, const char *testName)
Definition QtGui.h:50
CxxTest::QtGui::_application
QApplication * _application
Definition QtGui.h:92
CxxTest::QtGui::showTestName
void showTestName(const QString &testName)
Definition QtGui.h:198
CxxTest::QtGui::asString
static QString asString(unsigned n)
Definition QtGui.h:208
CxxTest::QtGui::_layout
QVBoxLayout * _layout
Definition QtGui.h:94
CxxTest::WorldDescription
Definition Descriptions.h:54
CxxTest::WorldDescription::MAX_STRLEN_TOTAL_TESTS
@ MAX_STRLEN_TOTAL_TESTS
Definition Descriptions.h:62
CxxTest::WorldDescription::strTotalTests
char * strTotalTests(char *) const
Definition Descriptions.cpp:16
CxxTest::WorldDescription::numTotalTests
virtual unsigned numTotalTests(void) const =0
CxxTest
Definition Descriptions.cpp:7
CxxTest::s
char * s
Definition ValueTraits.h:143
CxxTest::tracker
TestTracker & tracker()
Definition TestTracker.h:111