ctkTestApplication.cpp

Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Library:   CTK
00004  
00005   Copyright (c) 2010  Kitware Inc.
00006 
00007   Licensed under the Apache License, Version 2.0 (the "License");
00008   you may not use this file except in compliance with the License.
00009   You may obtain a copy of the License at
00010 
00011       http://www.commontk.org/LICENSE
00012 
00013   Unless required by applicable law or agreed to in writing, software
00014   distributed under the License is distributed on an "AS IS" BASIS,
00015   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00016   See the License for the specific language governing permissions and
00017   limitations under the License.
00018  
00019 =========================================================================*/
00020 /*=========================================================================
00021 
00022   Program:   Visualization Toolkit
00023   Module:    $RCSfile: ctkTestApplication.cxx,v $
00024 
00025   Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
00026   All rights reserved.
00027   See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
00028 
00029      This software is distributed WITHOUT ANY WARRANTY; without even
00030      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
00031      PURPOSE.  See the above copyright notice for more information.
00032 
00033 =========================================================================*/
00034 /*-------------------------------------------------------------------------
00035   Copyright 2008 Sandia Corporation.
00036   Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
00037   the U.S. Government retains certain rights in this software.
00038 -------------------------------------------------------------------------*/
00039 
00040 // Qt includes
00041 #include <QTimer>
00042 #include <QWidget>
00043 #include <QKeyEvent>
00044 #include <QMouseEvent>
00045 
00046 // CTK includes
00047 #include "ctkTestApplication.h"
00048 
00049 // STD includes
00050 #include <iostream>
00051 
00052 int ctkTestApplication::Error = 0;
00053 
00054 //-----------------------------------------------------------------------------
00055 ctkTestApplication::ctkTestApplication(int _argc, char** _argv)
00056 {
00057   qInstallMsgHandler(ctkTestApplication::messageHandler);
00058   
00059   // CMake generated driver removes argv[0], 
00060   // so let's put a dummy back in
00061   this->Argv.append("ctkTestApplication");
00062   for(int i=0; i<_argc; i++)
00063     {
00064     this->Argv.append(_argv[i]);
00065     }
00066   for(int j=0; j<this->Argv.size(); j++)
00067     {
00068     this->Argvp.append(this->Argv[j].data());
00069     }
00070   this->Argc = this->Argvp.size();
00071   this->App = new QApplication(this->Argc, this->Argvp.data());
00072 }
00073 
00074 //-----------------------------------------------------------------------------
00075 ctkTestApplication::~ctkTestApplication()
00076 {
00077   delete this->App;
00078   qInstallMsgHandler(0);
00079 }
00080 
00081 //-----------------------------------------------------------------------------
00082 void ctkTestApplication::runTestSlot()
00083 {
00084   this->runTest();
00085 }
00086 
00087 //-----------------------------------------------------------------------------
00088 void ctkTestApplication::runTest()
00089 {
00090 }
00091 
00092 //-----------------------------------------------------------------------------
00093 int ctkTestApplication::exec(bool reportErrorsOnExit)
00094 {
00095   if(QCoreApplication::arguments().contains("--exit"))
00096     {
00097     QTimer::singleShot(100, QApplication::instance(), 
00098                        SLOT(quit()));
00099     }
00100     
00101   int ret = QApplication::exec();
00102   if (reportErrorsOnExit)
00103     {
00104     return Error + ret;
00105     }
00106   else
00107     {
00108     return ret;
00109     }
00110 }
00111 
00112 //-----------------------------------------------------------------------------
00113 void ctkTestApplication::messageHandler(QtMsgType type, const char *msg)
00114 {
00115   switch(type)
00116   {
00117   case QtDebugMsg:
00118     std::cerr << "Debug: " << msg << std::endl;
00119     break;
00120   case QtWarningMsg:
00121     std::cerr << "Warning: " << msg << std::endl;
00122     Error++;
00123     break;
00124   case QtCriticalMsg:
00125     std::cerr << "Critical: " << msg << std::endl;
00126     Error++;
00127     break;
00128   case QtFatalMsg:
00129     std::cerr << "Fatal: " << msg << std::endl;
00130     abort();
00131   }
00132 }
00133 
00134 //-----------------------------------------------------------------------------
00135 void ctkTestApplication::delay(int ms)
00136 {
00137   if(ms > 0)
00138   {
00139     QTimer::singleShot(ms, QApplication::instance(), SLOT(quit()));
00140     QApplication::exec();
00141   }
00142 }
00143 
00144 //-----------------------------------------------------------------------------
00145 bool ctkTestApplication::simulateEvent(QWidget* w, QEvent* e)
00146 {
00147   bool status = QApplication::sendEvent(w, e);
00148   QApplication::processEvents();
00149   return status;
00150 }
00151 
00152 //-----------------------------------------------------------------------------
00153 void ctkTestApplication::keyUp(QWidget* w, Qt::Key key, Qt::KeyboardModifiers mod, int ms)
00154 {
00155   if(!w)
00156     return;
00157   delay(ms);
00158   QString text;
00159   char off = 'a';
00160   if(mod & Qt::ShiftModifier)
00161     off = 'A';
00162   if(key >= Qt::Key_A && key <= Qt::Key_Z)
00163     {
00164     text.append(QChar::fromAscii(key - Qt::Key_A + off));
00165     }
00166   QKeyEvent e(QEvent::KeyRelease, key, mod, text);
00167   if(!simulateEvent(w, &e))
00168     {
00169     qWarning("keyUp not handled\n");
00170     }
00171 }
00172 
00173 //-----------------------------------------------------------------------------
00174 void ctkTestApplication::keyDown(QWidget* w, Qt::Key key, Qt::KeyboardModifiers mod, int ms)
00175 {
00176   if(!w)
00177     return;
00178   delay(ms);
00179   QString text;
00180   char off = 'a';
00181   if(mod & Qt::ShiftModifier)
00182     off = 'A';
00183   if(key >= Qt::Key_A && key <= Qt::Key_Z)
00184     {
00185     text.append(QChar::fromAscii(key - Qt::Key_A + off));
00186     }
00187   QKeyEvent e(QEvent::KeyPress, key, mod, text);
00188   if(!simulateEvent(w, &e))
00189     {
00190     qWarning("keyDown not handled\n");
00191     }
00192 }
00193 
00194 //-----------------------------------------------------------------------------
00195 void ctkTestApplication::keyClick(QWidget* w, Qt::Key key, Qt::KeyboardModifiers mod, int ms)
00196 {
00197   delay(ms);
00198   keyDown(w, key, mod, 0);
00199   keyUp(w, key, mod, 0);
00200 }
00201 
00202 //-----------------------------------------------------------------------------
00203 void ctkTestApplication::mouseDown(QWidget* w, QPoint pos, Qt::MouseButton btn,
00204                         Qt::KeyboardModifiers mod, int ms)
00205 {
00206   delay(ms);
00207   QMouseEvent e(QEvent::MouseButtonPress, pos, btn, btn, mod);
00208   if(!simulateEvent(w, &e))
00209     {
00210     qWarning("mouseDown not handled\n");
00211     }
00212 }
00213 
00214 //-----------------------------------------------------------------------------
00215 void ctkTestApplication::mouseUp(QWidget* w, QPoint pos, Qt::MouseButton btn,
00216                       Qt::KeyboardModifiers mod, int ms)
00217 {
00218   delay(ms);
00219   QMouseEvent e(QEvent::MouseButtonRelease, pos, btn, btn, mod);
00220   if(!simulateEvent(w, &e))
00221     {
00222     qWarning("mouseUp not handled\n");
00223     }
00224 }
00225 
00226 //-----------------------------------------------------------------------------
00227 void ctkTestApplication::mouseMove(QWidget* w, QPoint pos, Qt::MouseButton btn,
00228                         Qt::KeyboardModifiers mod, int ms)
00229 {
00230   delay(ms);
00231   QMouseEvent e(QEvent::MouseMove, pos, btn, btn, mod);
00232   if(!simulateEvent(w, &e))
00233     {
00234     qWarning("mouseMove not handled\n");
00235     }
00236 }
00237 
00238 //-----------------------------------------------------------------------------
00239 void ctkTestApplication::mouseClick(QWidget* w, QPoint pos, Qt::MouseButton btn,
00240                          Qt::KeyboardModifiers mod, int ms)
00241 {
00242   delay(ms);
00243   mouseDown(w, pos, btn, mod, 0);
00244   mouseUp(w, pos, btn, mod, 0);
00245 }
00246 
00247 //-----------------------------------------------------------------------------
00248 void ctkTestApplication::mouseDClick(QWidget* w, QPoint pos, Qt::MouseButton btn,
00249                          Qt::KeyboardModifiers mod, int ms)
00250 {
00251   delay(ms);
00252   QMouseEvent e(QEvent::MouseButtonDblClick, pos, btn, btn, mod);
00253   if(!simulateEvent(w, &e))
00254     {
00255     qWarning("mouseMove not handled\n");
00256     }
00257 }
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines