123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271 |
- /*=========================================================================
- Library: CTK
- Copyright (c) Kitware Inc.
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
- http://www.apache.org/licenses/LICENSE-2.0.txt
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- =========================================================================*/
- /*=========================================================================
- Program: Visualization Toolkit
- Module: $RCSfile: ctkTestApplication.cxx,v $
- Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
- All rights reserved.
- See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
- This software is distributed WITHOUT ANY WARRANTY; without even
- the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
- PURPOSE. See the above copyright notice for more information.
- =========================================================================*/
- /*-------------------------------------------------------------------------
- Copyright 2008 Sandia Corporation.
- Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
- the U.S. Government retains certain rights in this software.
- -------------------------------------------------------------------------*/
- // Qt includes
- #include <QTimer>
- #include <QWidget>
- #include <QKeyEvent>
- #include <QMouseEvent>
- // CTK includes
- #include "ctkTestApplication.h"
- // STD includes
- #include <iostream>
- int ctkTestApplication::Error = 0;
- //-----------------------------------------------------------------------------
- ctkTestApplication::ctkTestApplication(int _argc, char** _argv)
- {
- #if QT_VERSION > QT_VERSION_CHECK(5,0,0)
- qInstallMessageHandler(ctkTestApplication::messageHandler);
- #else
- qInstallMsgHandler(ctkTestApplication::messageHandler);
- #endif
- // CMake generated driver removes argv[0],
- // so let's put a dummy back in
- this->Argv.append("ctkTestApplication");
- for(int i=0; i<_argc; i++)
- {
- this->Argv.append(_argv[i]);
- }
- for(int j=0; j<this->Argv.size(); j++)
- {
- this->Argvp.append(this->Argv[j].data());
- }
- this->Argc = this->Argvp.size();
- this->App = new QApplication(this->Argc, this->Argvp.data());
- }
- //-----------------------------------------------------------------------------
- ctkTestApplication::~ctkTestApplication()
- {
- delete this->App;
- #if QT_VERSION > QT_VERSION_CHECK(5,0,0)
- qInstallMessageHandler(0);
- #else
- qInstallMsgHandler(0);
- #endif
- }
- //-----------------------------------------------------------------------------
- void ctkTestApplication::runTestSlot()
- {
- this->runTest();
- }
- //-----------------------------------------------------------------------------
- void ctkTestApplication::runTest()
- {
- }
- //-----------------------------------------------------------------------------
- int ctkTestApplication::exec(bool reportErrorsOnExit)
- {
- if(QCoreApplication::arguments().contains("--exit"))
- {
- QTimer::singleShot(100, QApplication::instance(),
- SLOT(quit()));
- }
- int ret = QApplication::exec();
- if (reportErrorsOnExit)
- {
- return Error + ret;
- }
- else
- {
- return ret;
- }
- }
- //-----------------------------------------------------------------------------
- #if QT_VERSION >= QT_VERSION_CHECK(5,0,0)
- void ctkTestApplication::messageHandler(QtMsgType type, const QMessageLogContext& /*context*/, const QString& msg)
- {
- #else
- void ctkTestApplication::messageHandler(QtMsgType type, const char *msgAsStr)
- {
- QString msg(msgAsStr);
- #endif
- switch(type)
- {
- case QtDebugMsg:
- std::cerr << "Debug: " << qPrintable(msg) << std::endl;
- break;
- case QtWarningMsg:
- std::cerr << "Warning: " << qPrintable(msg) << std::endl;
- Error++;
- break;
- case QtCriticalMsg:
- std::cerr << "Critical: " << qPrintable(msg) << std::endl;
- Error++;
- break;
- case QtFatalMsg:
- std::cerr << "Fatal: " << qPrintable(msg) << std::endl;
- abort();
- }
- }
- //-----------------------------------------------------------------------------
- void ctkTestApplication::delay(int ms)
- {
- if(ms > 0)
- {
- QTimer::singleShot(ms, QApplication::instance(), SLOT(quit()));
- QApplication::exec();
- }
- }
- //-----------------------------------------------------------------------------
- bool ctkTestApplication::simulateEvent(QWidget* w, QEvent* e)
- {
- bool status = QApplication::sendEvent(w, e);
- QApplication::processEvents();
- return status;
- }
- //-----------------------------------------------------------------------------
- void ctkTestApplication::keyUp(QWidget* w, Qt::Key key, Qt::KeyboardModifiers mod, int ms)
- {
- if(!w)
- return;
- delay(ms);
- QString text;
- char off = 'a';
- if(mod & Qt::ShiftModifier)
- off = 'A';
- if(key >= Qt::Key_A && key <= Qt::Key_Z)
- {
- text.append(QChar::fromLatin1(key - Qt::Key_A + off));
- }
- QKeyEvent e(QEvent::KeyRelease, key, mod, text);
- if(!simulateEvent(w, &e))
- {
- qWarning("keyUp not handled\n");
- }
- }
- //-----------------------------------------------------------------------------
- void ctkTestApplication::keyDown(QWidget* w, Qt::Key key, Qt::KeyboardModifiers mod, int ms)
- {
- if(!w)
- return;
- delay(ms);
- QString text;
- char off = 'a';
- if(mod & Qt::ShiftModifier)
- off = 'A';
- if(key >= Qt::Key_A && key <= Qt::Key_Z)
- {
- text.append(QChar::fromLatin1(key - Qt::Key_A + off));
- }
- QKeyEvent e(QEvent::KeyPress, key, mod, text);
- if(!simulateEvent(w, &e))
- {
- qWarning("keyDown not handled\n");
- }
- }
- //-----------------------------------------------------------------------------
- void ctkTestApplication::keyClick(QWidget* w, Qt::Key key, Qt::KeyboardModifiers mod, int ms)
- {
- delay(ms);
- keyDown(w, key, mod, 0);
- keyUp(w, key, mod, 0);
- }
- //-----------------------------------------------------------------------------
- void ctkTestApplication::mouseDown(QWidget* w, QPoint pos, Qt::MouseButton btn,
- Qt::KeyboardModifiers mod, int ms)
- {
- delay(ms);
- QMouseEvent e(QEvent::MouseButtonPress, pos, btn, btn, mod);
- if(!simulateEvent(w, &e))
- {
- qWarning("mouseDown not handled\n");
- }
- }
- //-----------------------------------------------------------------------------
- void ctkTestApplication::mouseUp(QWidget* w, QPoint pos, Qt::MouseButton btn,
- Qt::KeyboardModifiers mod, int ms)
- {
- delay(ms);
- QMouseEvent e(QEvent::MouseButtonRelease, pos, btn, btn, mod);
- if(!simulateEvent(w, &e))
- {
- qWarning("mouseUp not handled\n");
- }
- }
- //-----------------------------------------------------------------------------
- void ctkTestApplication::mouseMove(QWidget* w, QPoint pos, Qt::MouseButton btn,
- Qt::KeyboardModifiers mod, int ms)
- {
- delay(ms);
- QMouseEvent e(QEvent::MouseMove, pos, btn, btn, mod);
- if(!simulateEvent(w, &e))
- {
- qWarning("mouseMove not handled\n");
- }
- }
- //-----------------------------------------------------------------------------
- void ctkTestApplication::mouseClick(QWidget* w, QPoint pos, Qt::MouseButton btn,
- Qt::KeyboardModifiers mod, int ms)
- {
- delay(ms);
- mouseDown(w, pos, btn, mod, 0);
- mouseUp(w, pos, btn, mod, 0);
- }
- //-----------------------------------------------------------------------------
- void ctkTestApplication::mouseDClick(QWidget* w, QPoint pos, Qt::MouseButton btn,
- Qt::KeyboardModifiers mod, int ms)
- {
- delay(ms);
- QMouseEvent e(QEvent::MouseButtonDblClick, pos, btn, btn, mod);
- if(!simulateEvent(w, &e))
- {
- qWarning("mouseMove not handled\n");
- }
- }
|