pqAbstractItemViewEventPlayer.cxx 6.2 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
#include <QtDebug>
41 42
#include <QList>
#include <QListWidget>
43
#include <QWheelEvent>
Clinton Stimpson's avatar
 
Clinton Stimpson committed
44

45 46
#include "pqEventDispatcher.h"

Clinton Stimpson's avatar
 
Clinton Stimpson committed
47
/// Converts a string representation of a model index into the real thing
48
static QModelIndex OldGetIndex(QAbstractItemView& View, const QString& Name)
Clinton Stimpson's avatar
 
Clinton Stimpson committed
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
{
    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;
}

68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
static QModelIndex GetIndexByItemName(QAbstractItemView& View, const QString& Name)
{
  QListWidget* const listWidget = qobject_cast<QListWidget*>(&View);

  QModelIndex index;
  if(!listWidget)
    {
    return index;
    }
  QList<QListWidgetItem *> findResult = listWidget->findItems(Name,Qt::MatchExactly);
  if(findResult.count() > 0)
    {
    // in theory more than one item could match? Only return the index for
    // the first instance.
    index = View.model()->index(listWidget->row(findResult.first()), 0, index);
    }
    
  return index;
}

88 89 90 91 92 93 94 95 96 97 98 99 100 101
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
102 103 104
///////////////////////////////////////////////////////////////////////////////
// pqAbstractItemViewEventPlayer

Clinton Stimpson's avatar
 
Clinton Stimpson committed
105 106
pqAbstractItemViewEventPlayer::pqAbstractItemViewEventPlayer(QObject* p)
  : pqWidgetEventPlayer(p)
Clinton Stimpson's avatar
 
Clinton Stimpson committed
107 108 109 110 111 112 113 114 115 116 117
{
}

bool pqAbstractItemViewEventPlayer::playEvent(QObject* Object, const QString& Command, const QString& Arguments, bool& Error)
{
  QAbstractItemView* const object = qobject_cast<QAbstractItemView*>(Object);
  if(!object)
    {
    return false;
    }
    
118
  if(Command == "currentChanged")  // left to support old recordings
Clinton Stimpson's avatar
 
Clinton Stimpson committed
119
    {
120
    const QModelIndex index = OldGetIndex(*object, Arguments);
Clinton Stimpson's avatar
 
Clinton Stimpson committed
121 122 123 124
    if(!index.isValid())
      return false;
      
    object->setCurrentIndex(index);
125 126 127 128 129 130 131 132 133
    return true;
    }
  else if(Command == "currentChangedbyItemName")
    {
    const QModelIndex index = GetIndexByItemName(*object, Arguments);
    if(!index.isValid())
      return false;
      
    object->setCurrentIndex(index);
Clinton Stimpson's avatar
 
Clinton Stimpson committed
134 135
    return true;
    }
136 137 138 139 140 141 142 143 144 145 146
  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());
147
      qApp->notify(object, &ke);
148 149 150 151 152 153
      return true;
      }
    }
  else if(Command.startsWith("mouse"))
    {
    QStringList args = Arguments.split(',');
154
    if(args.size() == 6)
155 156 157
      {
      Qt::MouseButtons buttons = static_cast<Qt::MouseButton>(args[1].toInt());
      Qt::KeyboardModifiers keym = static_cast<Qt::KeyboardModifier>(args[2].toInt());
158 159
      int x = args[3].toInt();
      int y = args[4].toInt();
160 161 162 163 164 165 166
      QPoint pt;
      QHeaderView* header = qobject_cast<QHeaderView*>(object);
      if(header)
        {
        int idx = args[5].toInt();
        if(header->orientation() == Qt::Horizontal)
          {
167
          pt = QPoint(header->sectionPosition(idx)+4, 4);
168 169 170
          }
        else
          {
171
          pt = QPoint(4, header->sectionPosition(idx)+4);
172 173 174 175 176
          }
        }
      else
        {
        QModelIndex idx = GetIndex(object, args[5]);
Clinton Stimpson's avatar
 
Clinton Stimpson committed
177
        object->scrollTo(idx);
178 179 180
        QRect r = object->visualRect(idx);
        pt = r.topLeft() + QPoint(x,y);
        }
181 182 183 184 185 186 187 188 189
      if (Command == "mouseWheel")
        {
//           QEvent::Type type = QEvent::Wheel;
         int delta = args[0].toInt();
         QWheelEvent we(QPoint(x,y), delta, buttons, keym);
         QCoreApplication::sendEvent(Object, &we);
         return true;
        }
      Qt::MouseButton button = static_cast<Qt::MouseButton>(args[0].toInt());
190 191 192 193
      QEvent::Type type = QEvent::MouseButtonPress;
      type = Command == "mouseMove" ? QEvent::MouseMove : type;
      type = Command == "mouseRelease" ? QEvent::MouseButtonRelease : type;
      type = Command == "mouseDblClick" ? QEvent::MouseButtonDblClick : type;
194
      QMouseEvent e(type, pt, button, buttons, keym);
195
      qApp->notify(object->viewport(), &e);
196 197 198
      return true;
      }
    }
Clinton Stimpson's avatar
 
Clinton Stimpson committed
199 200 201 202 203
    
  qCritical() << "Unknown abstract item command: " << Command << "\n";
  Error = true;
  return true;
}