From 55a4795271247ca01551a82b2cd2c5588701f7e6 Mon Sep 17 00:00:00 2001 From: Benjamin Long Date: Tue, 20 Mar 2012 11:25:49 -0400 Subject: [PATCH] Add events recorded to pqTreeViewEventTranaslator/Player Added "click", "DoubleClick", and "edit" event this functionality should maybe moved to paAbstractItemViewTranslator --- pqAbstractItemViewEventPlayer.cxx | 6 +++ pqTreeViewEventPlayer.cxx | 44 ++++++++++++++++++++- pqTreeViewEventTranslator.cxx | 65 +++++++++++++++++++++++++++++-- pqTreeViewEventTranslator.h | 4 +- 4 files changed, 113 insertions(+), 6 deletions(-) diff --git a/pqAbstractItemViewEventPlayer.cxx b/pqAbstractItemViewEventPlayer.cxx index cddd18e..2f54b3f 100644 --- a/pqAbstractItemViewEventPlayer.cxx +++ b/pqAbstractItemViewEventPlayer.cxx @@ -114,6 +114,12 @@ bool pqAbstractItemViewEventPlayer::playEvent(QObject* Object, const QString& Co { return false; } + + if (Command == "key" || Command == "editCancel" || Command == "editAccepted") + { + // We let the pqBasicWidgetEventPlayer do it ... + return false; + } if(Command == "currentChanged") // left to support old recordings { diff --git a/pqTreeViewEventPlayer.cxx b/pqTreeViewEventPlayer.cxx index 762da18..e08161d 100644 --- a/pqTreeViewEventPlayer.cxx +++ b/pqTreeViewEventPlayer.cxx @@ -32,6 +32,9 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "pqTreeViewEventPlayer.h" #include "pqEventDispatcher.h" +#include +#include +#include #include #include @@ -78,6 +81,11 @@ bool pqTreeViewEventPlayer::playEvent( return false; } + if (command == "key") + { + return false; + } + QRegExp regExp0("^([\\d\\.]+),(\\d+),(\\d+)$"); if (command == "setTreeItemCheckState" && regExp0.indexIn(arguments) != -1) { @@ -136,6 +144,33 @@ bool pqTreeViewEventPlayer::playEvent( } return true; } + else if(command == "edit") + { + QString str_index = arguments; + QModelIndex index = ::pqTreeViewEventPlayerGetIndex(str_index, treeView, error); + if (error) + { + return true; + } + treeView->edit(index); + return true; + } + else if (command == "editCancel") + { + QString str_index = arguments; + QModelIndex index = ::pqTreeViewEventPlayerGetIndex(str_index, treeView, error); + treeView->closePersistentEditor(index); + return true; + } + else if (command == "editAccepted") + { + QStringList list = arguments.split(','); + QModelIndex index = ::pqTreeViewEventPlayerGetIndex(list.at(0), treeView, error); + QVariant value = QVariant(list.at(1)); + treeView->model()->setData(index, value); + treeView->closePersistentEditor(index); + return true; + } else if (command == "expand" || command == "collapse") { QString str_index = arguments; @@ -147,7 +182,7 @@ bool pqTreeViewEventPlayer::playEvent( treeView->setExpanded(index, (command=="expand")); return true; } - else if (command == "setCurrent") + else if (command == "setCurrent" || command == "activate") { QString str_index = arguments; QModelIndex index = ::pqTreeViewEventPlayerGetIndex(str_index, treeView, error); @@ -156,6 +191,13 @@ bool pqTreeViewEventPlayer::playEvent( return true; } treeView->setCurrentIndex(index); + if (command == "activate") + { + QKeyEvent kd(QEvent::KeyPress, Qt::Key_Enter, Qt::NoModifier); + QKeyEvent ku(QEvent::KeyRelease, Qt::Key_Enter, Qt::NoModifier); + QCoreApplication::sendEvent(treeView, &kd); + QCoreApplication::sendEvent(treeView, &ku); + } return true; } return false; diff --git a/pqTreeViewEventTranslator.cxx b/pqTreeViewEventTranslator.cxx index 188b095..d6aced3 100644 --- a/pqTreeViewEventTranslator.cxx +++ b/pqTreeViewEventTranslator.cxx @@ -31,8 +31,11 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ========================================================================*/ #include "pqTreeViewEventTranslator.h" -#include +#include #include +#include +#include +#include //----------------------------------------------------------------------------- pqTreeViewEventTranslator::pqTreeViewEventTranslator(QObject* parentObject) @@ -60,6 +63,28 @@ bool pqTreeViewEventTranslator::translateEvent( return false; } + if (tr_event->type() == QEvent::KeyRelease) + { + QKeyEvent* ke = static_cast(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); + } + } + if (tr_event->type() == QEvent::FocusIn) { if(this->TreeView) @@ -67,9 +92,12 @@ bool pqTreeViewEventTranslator::translateEvent( QObject::disconnect(this->TreeView, 0, this, 0); QObject::disconnect(this->TreeView->selectionModel(), 0, this, 0); } - QObject::connect(treeWidget, SIGNAL(clicked(const QModelIndex&)), - this, SLOT(onItemChanged(const QModelIndex&))); + 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&))); QObject::connect(treeWidget, SIGNAL(expanded(const QModelIndex&)), this, SLOT(onExpanded(const QModelIndex&))); QObject::connect(treeWidget, SIGNAL(collapsed(const QModelIndex&)), @@ -83,9 +111,10 @@ bool pqTreeViewEventTranslator::translateEvent( } //----------------------------------------------------------------------------- -void pqTreeViewEventTranslator::onItemChanged( +void pqTreeViewEventTranslator::onClicked( const QModelIndex& index) { + static QModelIndex oldIndex; QTreeView* treeWidget = qobject_cast(this->sender()); QString str_index = this->getIndexAsString(index); if ( (index.model()->flags(index) & Qt::ItemIsUserCheckable) != 0) @@ -95,6 +124,34 @@ void pqTreeViewEventTranslator::onItemChanged( QString("%1,%3").arg(str_index).arg( index.model()->data(index,Qt::CheckStateRole).toInt())); } + 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(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(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); + } } //----------------------------------------------------------------------------- diff --git a/pqTreeViewEventTranslator.h b/pqTreeViewEventTranslator.h index 84a752a..85c7f0a 100644 --- a/pqTreeViewEventTranslator.h +++ b/pqTreeViewEventTranslator.h @@ -53,7 +53,9 @@ public: virtual bool translateEvent(QObject* Object, QEvent* Event, bool& Error); private slots: - void onItemChanged(const QModelIndex&); + void onClicked(const QModelIndex&); + void onActivated(const QModelIndex&); + void onDoubleClicked(const QModelIndex&); void onExpanded(const QModelIndex&); void onCollapsed(const QModelIndex&); void onCurrentChanged(const QModelIndex&); -- GitLab