pqEventDispatcher.h 5.85 KB
Newer Older
1 2 3
/*=========================================================================

   Program: ParaView
4
   Module:    pqEventDispatcher.h
5

6
   Copyright (c) 2005-2008 Sandia Corporation, Kitware Inc.
7 8 9
   All rights reserved.

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

12
   See License_v1.2.txt for the full ParaView license.
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
   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.

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

33 34
#ifndef _pqEventDispatcher_h
#define _pqEventDispatcher_h
35 36

#include "QtTestingExport.h"
37

38
#include <QObject>
39
#include <QTimer>
40
#include <QTime>
41
#include <QEventLoop>
42 43

class pqEventPlayer;
44
class pqEventSource;
45

46 47 48 49
/// pqEventDispatcher is responsible for taking each "event" from the test and
/// then "playing" it using the player. The dispatcher is the critical component
/// of this playback since it decides when it's time to dispatch the next
/// "event" from the test.
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
/// After an event is posted, there are two options:
/// \li the default option is to simply wait for a small amount of time before
/// processing the next event. The hope is that within that time any pending
/// requests such as timers, slots connected using Qt::QueuedConnection etc.
/// will be handled and all will work well. This however it fraught with
/// problems resulting is large number of random test failures especially in
/// large and complex applications such as ParaView. In such cases the better
/// option is the second option.
/// \li we only process already posted events (using a call to
/// QApplication::processEvents() and then rely on timers being registered using
/// registerTimer(). All these timers are explicitly timed-out, if active, before
/// processing the next event.
///
/// To enable the second mode simply set the eventPlaybackDelay to 0.
/// In either mode, all timers registered using registerTimer() will be
/// timed-out before dispatching next event.
66 67 68
///
/// To make it easier to register timers, one can directly use pqTimer instead
/// of QTimer.
69
class QTTESTING_EXPORT pqEventDispatcher : public QObject
70 71
{
  Q_OBJECT
72
  typedef QObject Superclass; 
73
public:
74
  pqEventDispatcher(QObject* parent=0);
75 76
  ~pqEventDispatcher();

77 78 79 80 81
  /// Retrieves events from the given event source, dispatching them to
  /// the given event player for test case playback. This call blocks until all
  /// the events from the source have been played back (or failure). Returns
  /// true if playback was successful.
  bool playEvents(pqEventSource& source, pqEventPlayer& player);
82

83 84 85 86 87
  /// Set the delay between dispatching successive events. Default is set using
  /// CMake variable QT_TESTING_EVENT_PLAYBACK_DELAY.
  static void setEventPlaybackDelay(int milliseconds);
  static int eventPlaybackDelay();

88
  /** Wait function provided for players that need to wait for the GUI
89 90 91
      to perform a certain action.
      Note: the minimum wait time is 100ms. This is set to avoid timiing issues
      on slow processors that hang tests.*/
92 93
  static void processEventsAndWait(int ms);

94 95 96 97
    /** proccessEvents method for widgets and paraview to use instead of
    calling Qt version, since that will break test playback*/
  static void processEvents(QEventLoop::ProcessEventsFlags flags = QEventLoop::AllEvents);

98 99 100
  /// register a timer that needs to be ensured to have timed-out after every
  /// event dispatch.
  static void registerTimer(QTimer* timer);
101

102 103 104 105
  /// Disables the behavior where more test events may be dispatched
  /// if Qt starts waiting in an event loop. Warning: Setting this to
  /// true will prevent modal dialogs from functioning correctly.
  static void deferEventsIfBlocked(bool defer);
106

107 108 109 110 111 112
  /// Return if the Dispatcher is not playing events
  bool isPaused() const;

  /// Return Dispatcher's status
  bool status() const;

113
signals:
114

115 116 117 118
  /// signal when playback starts
  void restarted();
  /// signal when playback pauses
  void paused();
119

120 121 122
protected slots:
  /// Plays a single event. this->PlayBackFinished and this->PlayBackStatus are
  /// updated by this method.
123
  void playEvent(int indent=0);
124
  void playEventOnBlocking();
125

126 127
  /// Called when the mainThread is about to block.
  void aboutToBlock();
128

129 130
  /// Called when the mainThread wakes up.
  void awake();
131

132 133 134 135
public slots:
  /// Change the TimeStep
  void setTimeStep(int value);
  /// Method to be able to stop/pause/play the current playback script
136
  void run(bool value);
137 138 139
  void stop();
  void oneStep();

140
protected:
141
  bool PlayingBlockingEvent;
142
  bool PlayBackFinished;
143
  bool PlayBackPaused;
144
  bool PlayBackStatus;
145 146
  bool PlayBackOneStep;
  bool PlayBackStoped;
147
  static bool DeferMenuTimeouts;
148 149 150 151 152
  /// This variable says that we should not continue to process test events
  /// when the application is blocked in a Qt event loop - it is either blocked
  /// in a modal dialog or is in a long wait while also processing events
  /// (such as when waiting from Insitu server @see pqLiveInsituManager).
  static bool DeferEventsIfBlocked;
153 154 155

  pqEventSource* ActiveSource;
  pqEventPlayer* ActivePlayer;
156
  QTimer BlockTimer;
157
};
158

159
#endif // !_pqEventDispatcher_h