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

   Program: ParaView
   Module:    pqAbstractItemViewEventPlayer.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
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 33 34 35
   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 "pqAbstractItemViewEventPlayer.h"

#include <QAbstractItemView>
36
#include <QHeaderView>
Clinton Stimpson's avatar
 
Clinton Stimpson committed
37 38
#include <QApplication>
#include <QMouseEvent>
39
#include <QKeyEvent>
Clinton Stimpson's avatar
 
Clinton Stimpson committed
40 41
#include <QtDebug>

42 43
#include "pqEventDispatcher.h"

Clinton Stimpson's avatar
 
Clinton Stimpson committed
44
/// Converts a string representation of a model index into the real thing
45
static QModelIndex OldGetIndex(QAbstractItemView& View, const QString& Name)
Clinton Stimpson's avatar
 
Clinton Stimpson committed
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
{
    QStringList rows = Name.split('/', QString::SkipEmptyParts);
    QString column;
    
    if(rows.size())
      {
      column = rows.back().split('|').at(1);
      rows.back() = rows.back().split('|').at(0);
      }
    
    QModelIndex index;
    for(int i = 0; i != rows.size(); ++i)
      {
      index = View.model()->index(rows[i].toInt(), column.toInt(), index);
      }
      
    return index;
}

65 66 67 68 69 70 71 72 73 74 75 76 77 78
static QModelIndex GetIndex(QAbstractItemView* View, const QString& Name)
{
  QStringList idxs = Name.split('/', QString::SkipEmptyParts);
  
  QModelIndex index;
  for(int i = 0; i != idxs.size(); ++i)
    {
    QStringList rowCol = idxs[i].split(':');
    index = View->model()->index(rowCol[0].toInt(), rowCol[1].toInt(), index);
    }
    
  return index;
}

Clinton Stimpson's avatar
 
Clinton Stimpson committed
79 80 81
///////////////////////////////////////////////////////////////////////////////
// pqAbstractItemViewEventPlayer

Clinton Stimpson's avatar
 
Clinton Stimpson committed
82 83
pqAbstractItemViewEventPlayer::pqAbstractItemViewEventPlayer(QObject* p)
  : pqWidgetEventPlayer(p)
Clinton Stimpson's avatar
 
Clinton Stimpson committed
84 85 86 87 88 89 90 91 92 93 94
{
}

bool pqAbstractItemViewEventPlayer::playEvent(QObject* Object, const QString& Command, const QString& Arguments, bool& Error)
{
  QAbstractItemView* const object = qobject_cast<QAbstractItemView*>(Object);
  if(!object)
    {
    return false;
    }
    
95
  if(Command == "currentChanged")  // left to support old recordings
Clinton Stimpson's avatar
 
Clinton Stimpson committed
96
    {
97
    const QModelIndex index = OldGetIndex(*object, Arguments);
Clinton Stimpson's avatar
 
Clinton Stimpson committed
98 99 100 101 102 103
    if(!index.isValid())
      return false;
      
    object->setCurrentIndex(index);
    return true;
    }
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
  else if(Command == "keyEvent")
    {
    QStringList data = Arguments.split(',');
    if(data.size() == 6)
      {
      QKeyEvent ke(static_cast<QEvent::Type>(data[0].toInt()),
                   data[1].toInt(),
                   static_cast<Qt::KeyboardModifiers>(data[2].toInt()),
                   data[3],
                   !!data[4].toInt(),
                   data[5].toInt());
      QCoreApplication::sendEvent(object, &ke);
      return true;
      }
    }
  else if(Command.startsWith("mouse"))
    {
    QStringList args = Arguments.split(',');
122
    if(args.size() == 6)
123 124 125 126
      {
      Qt::MouseButton button = static_cast<Qt::MouseButton>(args[0].toInt());
      Qt::MouseButtons buttons = static_cast<Qt::MouseButton>(args[1].toInt());
      Qt::KeyboardModifiers keym = static_cast<Qt::KeyboardModifier>(args[2].toInt());
127 128
      int x = args[3].toInt();
      int y = args[4].toInt();
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
      QPoint pt;
      QHeaderView* header = qobject_cast<QHeaderView*>(object);
      if(header)
        {
        int idx = args[5].toInt();
        if(header->orientation() == Qt::Horizontal)
          {
          pt = QPoint(header->sectionPosition(idx), 4);
          }
        else
          {
          pt = QPoint(4, header->sectionPosition(idx));
          }
        }
      else
        {
        QModelIndex idx = GetIndex(object, args[5]);
        QRect r = object->visualRect(idx);
        pt = r.topLeft() + QPoint(x,y);
        }
149 150 151 152
      QEvent::Type type = QEvent::MouseButtonPress;
      type = Command == "mouseMove" ? QEvent::MouseMove : type;
      type = Command == "mouseRelease" ? QEvent::MouseButtonRelease : type;
      type = Command == "mouseDblClick" ? QEvent::MouseButtonDblClick : type;
153
      QMouseEvent e(type, pt, button, buttons, keym);
154 155 156 157 158
      QCoreApplication::sendEvent(object->viewport(), &e);
      pqEventDispatcher::processEventsAndWait(1);
      return true;
      }
    }
Clinton Stimpson's avatar
 
Clinton Stimpson committed
159 160 161 162 163
    
  qCritical() << "Unknown abstract item command: " << Command << "\n";
  Error = true;
  return true;
}