pqTreeViewEventTranslator.cxx 7.25 KB
Newer Older
1 2 3
/*=========================================================================

   Program: ParaView
4
   Module:    pqTreeViewEventTranslator.cxx
5 6 7 8 9

   Copyright (c) 2005,2006 Sandia Corporation, Kitware Inc.
   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 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31

   See License_v1.2.txt for the full ParaView license.
   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.

========================================================================*/
32
#include "pqTreeViewEventTranslator.h"
33

34
#include <QDebug>
35
#include <QEvent>
36 37 38
#include <QKeyEvent>
#include <QTreeView>
#include <QVariant>
39 40

//-----------------------------------------------------------------------------
41
pqTreeViewEventTranslator::pqTreeViewEventTranslator(QObject* parentObject)
42 43 44 45 46
  : Superclass(parentObject)
{
}

//-----------------------------------------------------------------------------
47
pqTreeViewEventTranslator::~pqTreeViewEventTranslator()
48 49 50 51
{
}

//-----------------------------------------------------------------------------
52
bool pqTreeViewEventTranslator::translateEvent(
53 54
  QObject* object, QEvent* tr_event, bool& /*error*/)
{
55
  QTreeView* treeWidget = qobject_cast<QTreeView*>(object);
56 57 58
  if(!treeWidget)
    {
    // mouse events go to the viewport widget
59
    treeWidget = qobject_cast<QTreeView*>(object->parent());
60 61 62 63 64 65
    }
  if(!treeWidget)
    {
    return false;
    }

66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
  if (tr_event->type() == QEvent::KeyRelease)
    {
    QKeyEvent* ke = static_cast<QKeyEvent*>(tr_event);
    QModelIndex index = treeWidget->currentIndex();
    QString str_index = this->getIndexAsString(index);
    if (ke->key() == Qt::Key_Enter || ke->key() == Qt::Key_Return)
      {
      QVariant value = treeWidget->model()->data(index);
      qDebug() << value << str_index;
      emit this->recordEvent(treeWidget, "editAccepted",
                             QString("%1,%2").arg(str_index, value.toString()));
      }
    if (ke->key() == Qt::Key_Escape)
      {
      emit this->recordEvent(treeWidget, "editCancel", str_index);
      }
    if (ke->key() == Qt::Key_F2)
      {
      emit this->recordEvent(treeWidget, "edit", str_index);
      }
    }

88 89
  if (tr_event->type() == QEvent::FocusIn)
    {
90 91 92 93 94
    if(this->TreeView)
      {
      QObject::disconnect(this->TreeView, 0, this, 0);
      QObject::disconnect(this->TreeView->selectionModel(), 0, this, 0);
      }
95
    QObject::connect(treeWidget, SIGNAL(clicked(const QModelIndex&)),
96 97 98 99 100
      this, SLOT(onClicked(const QModelIndex&)));
    QObject::connect(treeWidget, SIGNAL(activated(const QModelIndex&)),
      this, SLOT(onActivated(const QModelIndex&)));
    QObject::connect(treeWidget, SIGNAL(doubleClicked(const QModelIndex&)),
      this, SLOT(onDoubleClicked(const QModelIndex&)));
101 102 103 104
    QObject::connect(treeWidget, SIGNAL(expanded(const QModelIndex&)),
      this, SLOT(onExpanded(const QModelIndex&)));
    QObject::connect(treeWidget, SIGNAL(collapsed(const QModelIndex&)),
      this, SLOT(onCollapsed(const QModelIndex&)));
105 106 107 108
    QObject::connect(treeWidget->selectionModel(),
      SIGNAL(currentChanged(const QModelIndex &, const QModelIndex &)),
      this, SLOT(onCurrentChanged(const QModelIndex&)));
    this->TreeView = treeWidget;
109 110 111 112 113
    }
  return true;
}

//-----------------------------------------------------------------------------
114
void pqTreeViewEventTranslator::onClicked(
115
  const QModelIndex& index)
116
{
117
  static QModelIndex oldIndex;
118
  QTreeView* treeWidget = qobject_cast<QTreeView*>(this->sender());
119
  QString str_index = this->getIndexAsString(index);
120 121 122 123 124 125 126
  if ( (index.model()->flags(index) & Qt::ItemIsUserCheckable) != 0)
    {
    // record the check state change if the item is user-checkable.
    emit this->recordEvent( treeWidget, "setCheckState",
      QString("%1,%3").arg(str_index).arg(
        index.model()->data(index,Qt::CheckStateRole).toInt()));
    }
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 152 153 154
  if ((treeWidget->editTriggers() & QAbstractItemView::SelectedClicked)
        == QAbstractItemView::SelectedClicked &&
      index == oldIndex)
    {
    emit this->recordEvent(treeWidget, "edit", str_index);
    }
  oldIndex = index;
}

//-----------------------------------------------------------------------------
void pqTreeViewEventTranslator::onActivated(const QModelIndex & index)
{
  QTreeView* treeWidget = qobject_cast<QTreeView*>(this->sender());
  QString str_index = this->getIndexAsString(index);
  emit this->recordEvent(treeWidget, "activate", str_index);
}

//-----------------------------------------------------------------------------
void pqTreeViewEventTranslator::onDoubleClicked(const QModelIndex& index)
{
  QTreeView* treeWidget = qobject_cast<QTreeView*>(this->sender());
  QString str_index = this->getIndexAsString(index);
  // record the check state change if the item is user-checkable.
  if ((treeWidget->editTriggers() & QAbstractItemView::DoubleClicked)
      == QAbstractItemView::DoubleClicked)
    {
    emit this->recordEvent(treeWidget, "edit", str_index);
    }
155
}
156 157 158 159

//-----------------------------------------------------------------------------
void pqTreeViewEventTranslator::onExpanded(const QModelIndex& index)
{
160
  QTreeView* treeWidget = qobject_cast<QTreeView*>(this->sender());
161 162

  // record the check state change if the item is user-checkable.
163 164
  emit this->recordEvent( treeWidget, "expand",
    this->getIndexAsString(index));
165 166 167 168 169
}

//-----------------------------------------------------------------------------
void pqTreeViewEventTranslator::onCollapsed(const QModelIndex& index)
{
170
  QTreeView* treeWidget = qobject_cast<QTreeView*>(this->sender());
171

172
  // record the check state change if the item is user-checkable.
173
  emit this->recordEvent( treeWidget, "collapse",
174 175 176 177 178 179
    this->getIndexAsString(index));
}

//-----------------------------------------------------------------------------
QString pqTreeViewEventTranslator::getIndexAsString(const QModelIndex& index)
{
180 181 182 183 184 185 186 187 188 189
  QModelIndex curIndex = index;
  QString str_index;
  while (curIndex.isValid())
    {
    str_index.prepend(QString("%1.%2.").arg(curIndex.row()).arg(curIndex.column()));
    curIndex = curIndex.parent();
    }

  // remove the last ".".
  str_index.chop(1);
190
  return str_index;
191 192
}

193 194 195
//-----------------------------------------------------------------------------
void pqTreeViewEventTranslator::onCurrentChanged(const QModelIndex& index)
{
196 197
  QTreeView* treeWidget = this->TreeView;

198 199
  if (treeWidget)
    {
200 201 202
    // record the check state change if the item is user-checkable.
    emit this->recordEvent(treeWidget,
      "setCurrent", this->getIndexAsString(index));
203 204
    }
}