pqEventPlayer.cxx 7.74 KB
Newer Older
Clinton Stimpson's avatar
 
Clinton Stimpson committed
1 2 3 4 5
/*=========================================================================

   Program: ParaView
   Module:    pqEventPlayer.cxx

6
   Copyright (c) 2005-2008 Sandia Corporation, Kitware Inc.
Clinton Stimpson's avatar
 
Clinton Stimpson committed
7 8 9
   All rights reserved.

   ParaView is a free software; you can redistribute it and/or modify it
Mathieu Westphal's avatar
Mathieu Westphal committed
10
   under the terms of the ParaView license version 1.2.
Clinton Stimpson's avatar
 
Clinton Stimpson committed
11

12
   See License_v1.2.txt for the full ParaView license.
Clinton Stimpson's avatar
 
Clinton Stimpson committed
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.

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

Clinton Stimpson's avatar
 
Clinton Stimpson committed
33 34
#include "pqEventPlayer.h"

Clinton Stimpson's avatar
 
Clinton Stimpson committed
35 36 37
#include "pqAbstractActivateEventPlayer.h"
#include "pqAbstractBooleanEventPlayer.h"
#include "pqAbstractDoubleEventPlayer.h"
Mathieu Westphal's avatar
Mathieu Westphal committed
38
#include "pq3DViewEventPlayer.h"
Clinton Stimpson's avatar
 
Clinton Stimpson committed
39 40
#include "pqAbstractIntEventPlayer.h"
#include "pqAbstractItemViewEventPlayer.h"
41
#include "pqAbstractMiscellaneousEventPlayer.h"
Clinton Stimpson's avatar
 
Clinton Stimpson committed
42 43
#include "pqAbstractStringEventPlayer.h"
#include "pqBasicWidgetEventPlayer.h"
Mathieu Westphal's avatar
Mathieu Westphal committed
44
#include "pqComboBoxEventPlayer.h"
45
#include "pqCommentEventPlayer.h"
Mathieu Westphal's avatar
Mathieu Westphal committed
46 47
#include "pqListViewEventPlayer.h"
#include "pqNativeFileDialogEventPlayer.h"
Clinton Stimpson's avatar
 
Clinton Stimpson committed
48
#include "pqObjectNaming.h"
49
#include "pqTabBarEventPlayer.h"
Mathieu Westphal's avatar
Mathieu Westphal committed
50
#include "pqTableViewEventPlayer.h"
Mathieu Westphal's avatar
Mathieu Westphal committed
51
#include "pqTreeViewEventPlayer.h"
Mathieu Westphal's avatar
Mathieu Westphal committed
52
#include "pqEventTypes.h"
Clinton Stimpson's avatar
 
Clinton Stimpson committed
53 54 55

#include <QApplication>
#include <QWidget>
56
#include <QDebug>
Clinton Stimpson's avatar
 
Clinton Stimpson committed
57

58
// ----------------------------------------------------------------------------
Clinton Stimpson's avatar
 
Clinton Stimpson committed
59 60 61 62
pqEventPlayer::pqEventPlayer()
{
}

63
// ----------------------------------------------------------------------------
Clinton Stimpson's avatar
 
Clinton Stimpson committed
64 65 66 67
pqEventPlayer::~pqEventPlayer()
{
}

68
// ----------------------------------------------------------------------------
69
void pqEventPlayer::addDefaultWidgetEventPlayers(pqTestUtility* util)
Clinton Stimpson's avatar
 
Clinton Stimpson committed
70
{
71
  addWidgetEventPlayer(new pqCommentEventPlayer(util));
Clinton Stimpson's avatar
 
Clinton Stimpson committed
72
  addWidgetEventPlayer(new pqBasicWidgetEventPlayer());
Clinton Stimpson's avatar
 
Clinton Stimpson committed
73 74 75 76 77 78
  addWidgetEventPlayer(new pqAbstractActivateEventPlayer());
  addWidgetEventPlayer(new pqAbstractBooleanEventPlayer());
  addWidgetEventPlayer(new pqAbstractDoubleEventPlayer());
  addWidgetEventPlayer(new pqAbstractIntEventPlayer());
  addWidgetEventPlayer(new pqAbstractItemViewEventPlayer());
  addWidgetEventPlayer(new pqAbstractStringEventPlayer());
Clinton Stimpson's avatar
 
Clinton Stimpson committed
79
  addWidgetEventPlayer(new pqTabBarEventPlayer());
Mathieu Westphal's avatar
Mathieu Westphal committed
80
  addWidgetEventPlayer(new pqComboBoxEventPlayer());
81
  addWidgetEventPlayer(new pqTreeViewEventPlayer());
Mathieu Westphal's avatar
Mathieu Westphal committed
82
  addWidgetEventPlayer(new pqTableViewEventPlayer());
Mathieu Westphal's avatar
Mathieu Westphal committed
83
  addWidgetEventPlayer(new pqListViewEventPlayer());
84
  addWidgetEventPlayer(new pqAbstractMiscellaneousEventPlayer());
85
  addWidgetEventPlayer(new pq3DViewEventPlayer("QGLWidget"));
86 87 88 89 90 91 92
  addWidgetEventPlayer(new pqNativeFileDialogEventPlayer(util));
}

// ----------------------------------------------------------------------------
QList<pqWidgetEventPlayer*> pqEventPlayer::players() const
{
  return this->Players;
Clinton Stimpson's avatar
 
Clinton Stimpson committed
93 94
}

95
// ----------------------------------------------------------------------------
Clinton Stimpson's avatar
 
Clinton Stimpson committed
96 97 98 99
void pqEventPlayer::addWidgetEventPlayer(pqWidgetEventPlayer* Player)
{
  if(Player)
    {
100 101 102 103 104 105 106 107
    // We Check if the Player has already been added previously
    int index =
      this->getWidgetEventPlayerIndex(QString(Player->metaObject()->className()));
    if (index != -1)
      {
      return;
      }

Clinton Stimpson's avatar
 
Clinton Stimpson committed
108 109
    this->Players.push_front(Player);
    Player->setParent(this);
Clinton Stimpson's avatar
 
Clinton Stimpson committed
110 111 112
    }
}

113 114
// ----------------------------------------------------------------------------
bool pqEventPlayer::removeWidgetEventPlayer(const QString& className)
Clinton Stimpson's avatar
 
Clinton Stimpson committed
115
{
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
  int index = this->getWidgetEventPlayerIndex(className);
  if (index == -1)
    {
    return false;
    }

  this->Players.removeAt(index);
  return true;
}

// ----------------------------------------------------------------------------
pqWidgetEventPlayer* pqEventPlayer::getWidgetEventPlayer(const QString& className)
{
  int index = this->getWidgetEventPlayerIndex(className);
  if (index == -1)
    {
    return 0;
    }

  return this->Players.at(index);
}

// ----------------------------------------------------------------------------
int pqEventPlayer::getWidgetEventPlayerIndex(const QString& className)
{
  for(int i = 0 ; i < this->Players.count() ; ++i)
    {
    if (this->Players.at(i)->metaObject()->className() == className)
      {
      return i;
      }
    }
  return -1;
}

// ----------------------------------------------------------------------------
Mathieu Westphal's avatar
Mathieu Westphal committed
152 153 154 155
void pqEventPlayer::playEvent(const QString& objectString,
                              const QString& command,
                              const QString& arguments,
                              bool& error)
156
{
Mathieu Westphal's avatar
Mathieu Westphal committed
157
  this->playEvent(objectString, command, arguments, pqEventTypes::ACTION_EVENT, error);
Mathieu Westphal's avatar
Mathieu Westphal committed
158 159 160 161 162 163 164 165 166 167
}

// ----------------------------------------------------------------------------
void pqEventPlayer::playEvent(const QString& objectString,
                              const QString& command,
                              const QString& arguments,
                              int eventType,
                              bool& error)
{
  emit this->eventAboutToBePlayed(objectString, command, arguments);
168
  // If we can't find an object with the right name, we're done ...
Mathieu Westphal's avatar
Mathieu Westphal committed
169
  QObject* const object = pqObjectNaming::GetObject(objectString);
170

171
  // Scroll bar depends on monitor's resolution
Mathieu Westphal's avatar
Mathieu Westphal committed
172
  if(!object && objectString.contains(QString("QScrollBar")))
173
    {
Mathieu Westphal's avatar
Mathieu Westphal committed
174 175
    emit this->eventPlayed(objectString, command, arguments);
    error = false;
176 177 178
    return;
    }

Mathieu Westphal's avatar
Mathieu Westphal committed
179
  if(!object && !command.startsWith("comment"))
Clinton Stimpson's avatar
 
Clinton Stimpson committed
180
    {
181 182
    qCritical() << pqObjectNaming::lastErrorMessage();
    emit this->errorMessage(pqObjectNaming::lastErrorMessage());
Mathieu Westphal's avatar
Mathieu Westphal committed
183
    error = true;
Clinton Stimpson's avatar
 
Clinton Stimpson committed
184 185 186
    return;
    }

187 188
  // Loop through players until the event gets handled ...
  bool accepted = false;
Mathieu Westphal's avatar
Mathieu Westphal committed
189 190
  bool tmpError = false;
  if (command.startsWith("comment"))
Clinton Stimpson's avatar
 
Clinton Stimpson committed
191
    {
192 193 194 195 196
    pqWidgetEventPlayer* widgetPlayer =
        this->getWidgetEventPlayer(QString("pqCommentEventPlayer"));
    pqCommentEventPlayer* commentPlayer =
        qobject_cast<pqCommentEventPlayer*>(widgetPlayer);
    if (commentPlayer)
Clinton Stimpson's avatar
 
Clinton Stimpson committed
197
      {
Mathieu Westphal's avatar
Mathieu Westphal committed
198
      accepted = commentPlayer->playEvent(object, command, arguments, tmpError);
199 200 201 202 203 204
      }
    }
  else
    {
    for(int i = 0; i != this->Players.size(); ++i)
      {
Mathieu Westphal's avatar
Mathieu Westphal committed
205
      accepted = this->Players[i]->playEvent(object, command, arguments, eventType, tmpError);
206 207 208 209
      if(accepted)
        {
        break;
        }
Clinton Stimpson's avatar
 
Clinton Stimpson committed
210 211 212
      }
    }

213 214
  // The event wasn't handled at all ...
  if(!accepted)
Clinton Stimpson's avatar
 
Clinton Stimpson committed
215
    {
Mathieu Westphal's avatar
Mathieu Westphal committed
216
    QString errorMessage =
217
        QString("Unhandled event %1 object %2\n")
Mathieu Westphal's avatar
Mathieu Westphal committed
218 219 220 221
          .arg(command, object ? object->objectName() : objectString);
    qCritical() << errorMessage;
    emit this->errorMessage(errorMessage);
    error = true;
222
    return;
Clinton Stimpson's avatar
 
Clinton Stimpson committed
223 224
    }

Mathieu Westphal's avatar
Mathieu Westphal committed
225 226
  // The event was handled, but there was a problem ...
  if(accepted && tmpError)
Clinton Stimpson's avatar
 
Clinton Stimpson committed
227
    {
Mathieu Westphal's avatar
Mathieu Westphal committed
228 229 230 231 232 233 234
    QString errorMessage =
        QString("Event error %1 object %2 with args:%3\n")
          .arg(command, object ? object->objectName() : objectString,
               arguments);
    qCritical() << errorMessage;
    emit this->errorMessage(errorMessage);
    error = true;
235
    return;
Clinton Stimpson's avatar
 
Clinton Stimpson committed
236 237
    }

238
  // The event was handled successfully ...
Mathieu Westphal's avatar
Mathieu Westphal committed
239 240
  emit this->eventPlayed(objectString, command, arguments);
  error = false;
Clinton Stimpson's avatar
 
Clinton Stimpson committed
241
}
Clinton Stimpson's avatar
 
Clinton Stimpson committed
242