pqAbstractItemViewEventPlayer.cxx 6.36 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
{
}

bool pqAbstractItemViewEventPlayer::playEvent(QObject* Object, const QString& Command, const QString& Arguments, bool& Error)
{
  QAbstractItemView* const object = qobject_cast<QAbstractItemView*>(Object);
  if(!object)
    {
    return false;
    }
117 118 119 120 121 122

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