Skip to content

Commit

Permalink
ENH: Add ability to record/playback a python script.
Browse files Browse the repository at this point in the history
      Also add ability to query QObject properties from python.
  • Loading branch information
Clinton Stimpson committed Oct 18, 2006
1 parent 236b416 commit fe6b1ed
Show file tree
Hide file tree
Showing 11 changed files with 814 additions and 11 deletions.
38 changes: 38 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@

PROJECT(QtTesting)

#OPTION(QT_TESTING_WITH_PYTHON "Build Testing with Python" ON)
SET(QT_TESTING_WITH_PYTHON OFF)
SET(QT_TESTING_WITH_XML ON)

IF(QT_TESTING_WITH_PYTHON)
INCLUDE_DIRECTORIES(
${PYTHON_INCLUDE_PATH}
)
SET(PYTHON_MOCS
pqPythonEventObserver.h
)
SET(PYTHON_SRCS
pqPythonEventObserver.cxx
pqPythonEventObserver.h
pqPythonEventSource.cxx
pqPythonEventSource.h
)
ENDIF(QT_TESTING_WITH_PYTHON)


INCLUDE_DIRECTORIES(
${QtTesting_BINARY_DIR}
${QtTesting_SOURCE_DIR}
Expand All @@ -20,15 +41,18 @@ QT4_WRAP_CPP(MOC_BUILT_SOURCES
pqDoubleSpinBoxEventTranslator.h
pqEventDispatcher.h
pqEventPlayer.h
pqEventSource.h
pqEventTranslator.h
pqLineEditEventTranslator.h
pqMenuEventTranslator.h
pqRecordEventsDialog.h
pqSpinBoxEventTranslator.h
pqStdoutEventObserver.h
pqThreadedEventSource.h
pqTabBarEventTranslator.h
pqWidgetEventTranslator.h
pqXMLEventObserver.h
${PYTHON_MOCS}
)

SET_DIRECTORY_PROPERTIES(PROPERTIES INCLUDE_DIRECTORIES "${include_dirs_tmp}")
Expand Down Expand Up @@ -88,6 +112,8 @@ ADD_LIBRARY(QtTesting
pqTabBarEventPlayer.h
pqTabBarEventTranslator.cxx
pqTabBarEventTranslator.h
pqThreadedEventSource.cxx
pqThreadedEventSource.h
pqWidgetEventPlayer.h
pqWidgetEventTranslator.h
pqXMLEventObserver.cxx
Expand All @@ -96,6 +122,7 @@ ADD_LIBRARY(QtTesting
pqXMLEventSource.h
${MOC_BUILT_SOURCES}
${UI_BUILT_SOURCES}
${PYTHON_SRCS}
)

SOURCE_GROUP("Generated" FILES
Expand All @@ -108,7 +135,18 @@ TARGET_LINK_LIBRARIES(QtTesting
${QT_QTTEST_LIBRARY}
)

IF(QT_TESTING_WITH_PYTHON)
TARGET_LINK_LIBRARIES(QtTesting
${PYTHON_LIBRARIES}
)
ENDIF(QT_TESTING_WITH_PYTHON)

CONFIGURE_FILE(${QtTesting_SOURCE_DIR}/QtTestingConfigure.h.in
${QtTesting_BINARY_DIR}/QtTestingConfigure.h)

INSTALL(TARGETS QtTesting
RUNTIME DESTINATION ${PV_INSTALL_BIN_DIR_CM24} COMPONENT Runtime
LIBRARY DESTINATION ${PV_INSTALL_LIB_DIR_CM24} COMPONENT Runtime
ARCHIVE DESTINATION ${PV_INSTALL_LIB_DIR_CM24} COMPONENT Development)


42 changes: 42 additions & 0 deletions QtTestingConfigure.h.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*=========================================================================

Program: ParaView
Module: QtTestingConfigure.h.in

Copyright (c) 2005,2006 Sandia Corporation, Kitware Inc.
All rights reserved.

ParaView is a free software; you can redistribute it and/or modify it
under the terms of the ParaView license version 1.1.

See License_v1.1.txt for the full ParaView license.
A copy of this license can be obtained by contacting
Kitware Inc.
28 Corporate Drive
Clifton Park, NY 12065
USA

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

=========================================================================*/

#ifndef _QtTestingConfigure_h
#define _QtTestingConfigure_h

#cmakedefine QT_TESTING_WITH_XML

#cmakedefine QT_TESTING_WITH_PYTHON

#endif // _QtTestingConfigure_h


27 changes: 22 additions & 5 deletions pqEventDispatcher.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ class pqEventDispatcher::pqImplementation
pqEventDispatcher::pqEventDispatcher() :
Implementation(new pqImplementation())
{
QObject::connect(this, SIGNAL(readyPlayNextEvent()),
this, SLOT(playNextEvent()));
}

pqEventDispatcher::~pqEventDispatcher()
Expand Down Expand Up @@ -99,23 +101,37 @@ void pqEventDispatcher::playEvents(pqEventSource& source, pqEventPlayer& player)
&this->Implementation->Timer,
SIGNAL(timeout()),
this,
SLOT(playNextEvent()));
SIGNAL(readyPlayNextEvent()));
#else
QObject::connect(
QAbstractEventDispatcher::instance(),
SIGNAL(aboutToBlock()),
this,
SLOT(playNextEvent()),
SIGNAL(readyPlayNextEvent()),
Qt::QueuedConnection);
#endif
}

void pqEventDispatcher::playNextEvent()
{

if(!this->Implementation->Source)
{
return;
}

QString object;
QString command;
QString arguments;
if(!this->Implementation->Source->getNextEvent(object, command, arguments))

// block signals as some event sources may interact with the event loop
this->blockSignals(true);

bool result = this->Implementation->Source->getNextEvent(
object, command, arguments);
this->blockSignals(false);

if(!result)
{
this->stopPlayback();
emit this->succeeded();
Expand Down Expand Up @@ -145,15 +161,16 @@ void pqEventDispatcher::stopPlayback()
&this->Implementation->Timer,
SIGNAL(timeout()),
this,
SLOT(playNextEvent()));
SIGNAL(readyPlayNextEvent()));
#else
QObject::disconnect(
QAbstractEventDispatcher::instance(),
SIGNAL(aboutToBlock()),
this,
SLOT(playNextEvent()));
SIGNAL(readyPlayNextEvent()));
#endif

this->Implementation->Source = 0;
this->Implementation->Player = 0;
}

1 change: 1 addition & 0 deletions pqEventDispatcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class QTTESTING_EXPORT pqEventDispatcher :
signals:
void succeeded();
void failed();
void readyPlayNextEvent();

private slots:
void playNextEvent();
Expand Down
9 changes: 3 additions & 6 deletions pqEventSource.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,13 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#define _pqEventSource_h

#include "QtTestingExport.h"

#include <QObject>
class QString;

/// Abstract interface for objects that can supply high-level testing events
class QTTESTING_EXPORT pqEventSource
class QTTESTING_EXPORT pqEventSource : public QObject
{
Q_OBJECT
public:
virtual ~pqEventSource() {}

Expand All @@ -50,10 +51,6 @@ class QTTESTING_EXPORT pqEventSource
QString& command,
QString& arguments) = 0;

protected:
pqEventSource() {}
pqEventSource(const pqEventSource&) {}
pqEventSource& operator=(const pqEventSource&) { return *this; }
};

#endif // !_pqEventSource_h
76 changes: 76 additions & 0 deletions pqPythonEventObserver.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*=========================================================================
Program: ParaView
Module: pqPythonEventObserver.cxx
Copyright (c) 2005,2006 Sandia Corporation, Kitware Inc.
All rights reserved.
ParaView is a free software; you can redistribute it and/or modify it
under the terms of the ParaView license version 1.1.
See License_v1.1.txt for the full ParaView license.
A copy of this license can be obtained by contacting
Kitware Inc.
28 Corporate Drive
Clifton Park, NY 12065
USA
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
=========================================================================*/

#include "pqPythonEventObserver.h"

#include <QTextStream>

////////////////////////////////////////////////////////////////////////////////////
// pqPythonEventObserver

pqPythonEventObserver::pqPythonEventObserver(QTextStream& stream) :
Stream(stream)
{
this->Stream << "#/usr/bin/env python\n\n";
this->Stream << "import QtTesting\n\n";
}

pqPythonEventObserver::~pqPythonEventObserver()
{
this->Stream.flush();
}

void pqPythonEventObserver::onRecordEvent(
const QString& Widget,
const QString& Command,
const QString& Arguments)
{

QString varname = this->Names[Widget];
if(varname == QString::null)
{
varname = QString("object%1").arg(this->Names.count());
this->Names.insert(Widget, varname);
QString objname("%1 = '%2'");
objname = objname.arg(varname);
objname = objname.arg(Widget);
this->Stream << objname << "\n";
}

QString pycommand("QtTesting.playCommand(%1, '%2', '%3')");
pycommand = pycommand.arg(varname);
pycommand = pycommand.arg(Command);
pycommand = pycommand.arg(Arguments);
this->Stream << pycommand << "\n";
}


75 changes: 75 additions & 0 deletions pqPythonEventObserver.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*=========================================================================
Program: ParaView
Module: pqPythonEventObserver.h
Copyright (c) 2005,2006 Sandia Corporation, Kitware Inc.
All rights reserved.
ParaView is a free software; you can redistribute it and/or modify it
under the terms of the ParaView license version 1.1.
See License_v1.1.txt for the full ParaView license.
A copy of this license can be obtained by contacting
Kitware Inc.
28 Corporate Drive
Clifton Park, NY 12065
USA
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
=========================================================================*/

#ifndef _pqPythonEventObserver_h
#define _pqPythonEventObserver_h

#include <QObject>
#include <QHash>
#include <QString>
class QTextStream;

/**
Observes high-level ParaView events, and serializes them to a stream as Python
for possible playback (as a test-case, demo, tutorial, etc). To use,
connect the onRecordEvent() slot to the pqEventTranslator::recordEvent()
signal.
\note Output is sent to the stream from this object's destructor, so you
must ensure that it goes out of scope before trying to playback the stream.
\sa pqEventTranslator, pqStdoutEventObserver, pqPythonEventSource.
*/

class pqPythonEventObserver :
public QObject
{
Q_OBJECT

public:
pqPythonEventObserver(QTextStream& Stream);
~pqPythonEventObserver();

public slots:
void onRecordEvent(
const QString& Widget,
const QString& Command,
const QString& Arguments);

private:
/// Stores a stream that will be used to store the Python output
QTextStream& Stream;
QHash<QString, QString> Names;
};

#endif // !_pqPythonEventObserver_h

0 comments on commit fe6b1ed

Please sign in to comment.