My Project  debian-1:4.1.1-p2+ds-4
QtGui.h
Go to the documentation of this file.
1 #ifndef __cxxtest__QtGui_h__
2 #define __cxxtest__QtGui_h__
3 
4 //
5 // The QtGui displays a simple progress bar using the Qt Toolkit. It
6 // has been tested with versions 2.x and 3.x.
7 //
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
12 //
13 // If both are -minimized and -keep specified, GUI will only keep the
14 // window if it's in focus.
15 //
16 
17 #include <cxxtest/Gui.h>
18 
19 #include <qapplication.h>
20 #include <qglobal.h>
21 #include <qlabel.h>
22 #include <qlayout.h>
23 #include <qmessagebox.h>
24 #include <qpixmap.h>
25 #include <qprogressbar.h>
26 #include <qstatusbar.h>
27 
28 namespace CxxTest
29 {
30  class QtGui : public GuiListener
31  {
32  public:
33  void enterGui( int &argc, char **argv )
34  {
35  parseCommandLine( argc, argv );
36  createApplication( argc, argv );
37  }
38 
39  void enterWorld( const WorldDescription &wd )
40  {
41  createWindow( wd );
42  processEvents();
43  }
44 
45  void guiEnterSuite( const char *suiteName )
46  {
47  showSuiteName( suiteName );
48  }
49 
50  void guiEnterTest( const char *suiteName, const char *testName )
51  {
52  setCaption( suiteName, testName );
54  showTestName( testName );
55  showTestsDone( _progressBar->progress() );
56  processEvents();
57  }
58 
59  void yellowBar()
60  {
61  setColor( 255, 255, 0 );
62  setIcon( QMessageBox::Warning );
63  getTotalTests();
64  processEvents();
65  }
66 
67  void redBar()
68  {
69  if ( _startMinimized && _mainWindow->isMinimized() )
70  showNormal();
71  setColor( 255, 0, 0 );
72  setIcon( QMessageBox::Critical );
73  getTotalTests();
74  processEvents();
75  }
76 
77  void leaveGui()
78  {
79  if ( keep() ) {
80  showSummary();
81  _application->exec();
82  }
83  else
84  _mainWindow->close( true );
85  }
86 
87  private:
88  QString _title;
90  unsigned _numTotalTests;
91  QString _strTotalTests;
92  QApplication *_application;
93  QWidget *_mainWindow;
94  QVBoxLayout *_layout;
95  QProgressBar *_progressBar;
96  QStatusBar *_statusBar;
98 
99  void parseCommandLine( int argc, char **argv )
100  {
101  _startMinimized = _keep = false;
102  _title = argv[0];
103 
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" )
109  _keep = true;
110  else if ( arg == "-title" && (i + 1 < argc) )
111  _title = argv[++i];
112  }
113  }
114 
115  void createApplication( int &argc, char **argv )
116  {
117  _application = new QApplication( argc, argv );
118  }
119 
120  void createWindow( const WorldDescription &wd )
121  {
122  getTotalTests( wd );
125  createStatusBar();
126  setMainWidget();
127  if ( _startMinimized )
128  showMinimized();
129  else
130  showNormal();
131  }
132 
134  {
135  getTotalTests( tracker().world() );
136  }
137 
138  void getTotalTests( const WorldDescription &wd )
139  {
143  }
144 
146  {
147  _mainWindow = new QWidget();
148  _layout = new QVBoxLayout( _mainWindow );
149  }
150 
152  {
153  _layout->addWidget( _progressBar = new QProgressBar( _numTotalTests, _mainWindow ) );
154  _progressBar->setProgress( 0 );
155  setColor( 0, 255, 0 );
156  setIcon( QMessageBox::Information );
157  }
158 
160  {
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 );
165  }
166 
168  {
169  _application->setMainWidget( _mainWindow );
170  }
171 
173  {
174  _mainWindow->showMinimized();
175  }
176 
177  void showNormal()
178  {
179  _mainWindow->showNormal();
180  centerWindow();
181  }
182 
183  void setCaption( const QString &suiteName, const QString &testName )
184  {
185  _mainWindow->setCaption( _title + " - " + suiteName + "::" + testName + "()" );
186  }
187 
188  void showSuiteName( const QString &suiteName )
189  {
190  _suiteName->setText( "class " + suiteName );
191  }
192 
194  {
195  _progressBar->setProgress( _progressBar->progress() + 1 );
196  }
197 
198  void showTestName( const QString &testName )
199  {
200  _testName->setText( testName + "()" );
201  }
202 
203  void showTestsDone( unsigned testsDone )
204  {
205  _testsDone->setText( asString( testsDone ) + " of " + _strTotalTests );
206  }
207 
208  static QString asString( unsigned n )
209  {
210  return QString::number( n );
211  }
212 
213  void setColor( int r, int g, int b )
214  {
215  QPalette palette = _progressBar->palette();
216  palette.setColor( QColorGroup::Highlight, QColor( r, g, b ) );
217  _progressBar->setPalette( palette );
218  }
219 
220  void setIcon( QMessageBox::Icon icon )
221  {
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() ) );
226 #endif // QT_VERSION
227  }
228 
230  {
231  _application->processEvents();
232  }
233 
235  {
236  QWidget *desktop = QApplication::desktop();
237  int xCenter = desktop->x() + (desktop->width() / 2);
238  int yCenter = desktop->y() + (desktop->height() / 2);
239 
240  int windowWidth = (desktop->width() * 4) / 5;
241  int windowHeight = _mainWindow->height();
242  _mainWindow->setGeometry( xCenter - (windowWidth / 2), yCenter - (windowHeight / 2), windowWidth, windowHeight );
243  }
244 
245  bool keep()
246  {
247  if ( !_keep )
248  return false;
249  if ( !_startMinimized )
250  return true;
251  return (_mainWindow == _application->activeWindow());
252  }
253 
254  void showSummary()
255  {
256  QString summary = _strTotalTests + (_numTotalTests == 1 ? " test" : " tests");
257  if ( tracker().failedTests() )
258  summary = "Failed " + asString( tracker().failedTests() ) + " of " + summary;
259  else
260  summary = summary + " passed";
261 
262  _mainWindow->setCaption( _title + " - " + summary );
263 
264  _statusBar->removeWidget( _suiteName );
265  _statusBar->removeWidget( _testName );
266  _testsDone->setText( summary );
267  }
268  };
269 };
270 
271 #endif // __cxxtest__QtGui_h__
CxxTest::QtGui::getTotalTests
void getTotalTests(const WorldDescription &wd)
Definition: QtGui.h:138
CxxTest::QtGui::createMainWindow
void createMainWindow()
Definition: QtGui.h:145
CxxTest::QtGui::setCaption
void setCaption(const QString &suiteName, const QString &testName)
Definition: QtGui.h:183
CxxTest::QtGui::createStatusBar
void createStatusBar()
Definition: QtGui.h:159
CxxTest::QtGui::setMainWidget
void setMainWidget()
Definition: QtGui.h:167
CxxTest::QtGui
Definition: QtGui.h:30
g
g
Definition: cfModGcd.cc:4031
CxxTest::TestTracker::failedTests
unsigned failedTests() const
Definition: TestTracker.h:37
Gui.h
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::processEvents
void processEvents()
Definition: QtGui.h:229
CxxTest::QtGui::_strTotalTests
QString _strTotalTests
Definition: QtGui.h:91
CxxTest::QtGui::_testName
QLabel * _testName
Definition: QtGui.h:97
CxxTest::QtGui::showMinimized
void showMinimized()
Definition: QtGui.h:172
b
CanonicalForm b
Definition: cfModGcd.cc:4044
CxxTest::s
char * s
Definition: ValueTraits.h:143
i
int i
Definition: cfEzgcd.cc:125
CxxTest::QtGui::yellowBar
void yellowBar()
Definition: QtGui.h:59
CxxTest::QtGui::showSummary
void showSummary()
Definition: QtGui.h:254
CxxTest::WorldDescription::strTotalTests
char * strTotalTests(char *) const
Definition: Descriptions.cpp:16
CxxTest::QtGui::_statusBar
QStatusBar * _statusBar
Definition: QtGui.h:96
CxxTest::QtGui::showTestsDone
void showTestsDone(unsigned testsDone)
Definition: QtGui.h:203
CxxTest::WorldDescription::numTotalTests
virtual unsigned numTotalTests(void) const =0
CxxTest::QtGui::getTotalTests
void getTotalTests()
Definition: QtGui.h:133
CxxTest::QtGui::_startMinimized
bool _startMinimized
Definition: QtGui.h:89
CxxTest::QtGui::_mainWindow
QWidget * _mainWindow
Definition: QtGui.h:93
CxxTest::QtGui::_keep
bool _keep
Definition: QtGui.h:89
CxxTest::QtGui::showNormal
void showNormal()
Definition: QtGui.h:177
CxxTest::QtGui::enterGui
void enterGui(int &argc, char **argv)
Definition: QtGui.h:33
CxxTest::QtGui::parseCommandLine
void parseCommandLine(int argc, char **argv)
Definition: QtGui.h:99
CxxTest::QtGui::showSuiteName
void showSuiteName(const QString &suiteName)
Definition: QtGui.h:188
CxxTest::QtGui::_suiteName
QLabel * _suiteName
Definition: QtGui.h:97
CxxTest::QtGui::_application
QApplication * _application
Definition: QtGui.h:92
CxxTest::QtGui::createApplication
void createApplication(int &argc, char **argv)
Definition: QtGui.h:115
CxxTest::WorldDescription
Definition: Descriptions.h:53
CxxTest::QtGui::setIcon
void setIcon(QMessageBox::Icon icon)
Definition: QtGui.h:220
CxxTest::QtGui::guiEnterTest
void guiEnterTest(const char *suiteName, const char *testName)
Definition: QtGui.h:50
CxxTest::QtGui::centerWindow
void centerWindow()
Definition: QtGui.h:234
CxxTest::QtGui::redBar
void redBar()
Definition: QtGui.h:67
CxxTest::QtGui::asString
static QString asString(unsigned n)
Definition: QtGui.h:208
CxxTest::WorldDescription::MAX_STRLEN_TOTAL_TESTS
Definition: Descriptions.h:62
CxxTest::QtGui::_progressBar
QProgressBar * _progressBar
Definition: QtGui.h:95
CxxTest::QtGui::createProgressBar
void createProgressBar()
Definition: QtGui.h:151
CxxTest::QtGui::_title
QString _title
Definition: QtGui.h:88
CxxTest
Definition: Descriptions.cpp:6
CxxTest::QtGui::advanceProgressBar
void advanceProgressBar()
Definition: QtGui.h:193
CxxTest::QtGui::guiEnterSuite
void guiEnterSuite(const char *suiteName)
Definition: QtGui.h:45
CxxTest::QtGui::showTestName
void showTestName(const QString &testName)
Definition: QtGui.h:198
CxxTest::QtGui::leaveGui
void leaveGui()
Definition: QtGui.h:77
CxxTest::QtGui::enterWorld
void enterWorld(const WorldDescription &wd)
Definition: QtGui.h:39
CxxTest::QtGui::_numTotalTests
unsigned _numTotalTests
Definition: QtGui.h:90
CxxTest::tracker
TestTracker & tracker()
Definition: TestTracker.h:111
CxxTest::QtGui::keep
bool keep()
Definition: QtGui.h:245
CxxTest::GuiListener
Definition: Gui.h:13
CxxTest::QtGui::_testsDone
QLabel * _testsDone
Definition: QtGui.h:97
CxxTest::QtGui::_layout
QVBoxLayout * _layout
Definition: QtGui.h:94