From 011d0933fa5a811da37fca99a81c31e103dcc35d Mon Sep 17 00:00:00 2001 From: Christian Klinger Date: Fri, 10 Jun 2016 12:12:14 +0200 Subject: improve keyboard behaviour. --- src/dialog.cpp | 65 +++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 39 insertions(+), 26 deletions(-) (limited to 'src/dialog.cpp') diff --git a/src/dialog.cpp b/src/dialog.cpp index f42ad65..cd2b9b6 100644 --- a/src/dialog.cpp +++ b/src/dialog.cpp @@ -48,6 +48,10 @@ Dialog::Dialog(int defaultTab, QWidget *parent) connect(centerTimer_, SIGNAL(timeout()), this, SLOT(onCenterTimer())); centerTimer_->start(1000); + ui->treeView->setFocusProxy(ui->filterEdit); + // ui->filterEdit->installEventFilter(this); + qApp->installEventFilter(this); + ui->helpBox->hide(); ui->newsBox->hide(); @@ -71,8 +75,6 @@ Dialog::Dialog(int defaultTab, QWidget *parent) QObject::connect(SessionsIconHolder::get(), SIGNAL(iconDownloaded(const QUrl&, const QIcon&)), this, SLOT(iconDownloaded(const QUrl&, const QIcon&))); - // ui->treeView->installEventFilter(this); - // this->installEventFilter(this); } Dialog::~Dialog() { @@ -451,8 +453,8 @@ void Dialog::onTabButtonChanged(int tab) { } // give focus to treeView - if (!ui->filterEdit->hasFocus()) { - ui->filterEdit->setFocus(); + if (!ui->treeView->hasFocus()) { + ui->treeView->setFocus(); } // Update pressed status of buttons @@ -470,11 +472,7 @@ void Dialog::onTabButtonChanged(int tab) { this->activeTab_ = tab; /* get the first element ad select it */ - QModelIndex root(ui->treeView->rootIndex()); - QModelIndex section(model_[tab]->index(0, 0, root)); - QModelIndex element(model_[tab]->index(0, 0, section)); - ui->treeView->selectionModel()->setCurrentIndex(element, QItemSelectionModel::Select); - ui->treeView->setFocus(Qt::OtherFocusReason); + selectFirstElement(); } @@ -706,26 +704,41 @@ void Dialog::on_spaceKey() { ui->treeView->expand(index); } } -/* install this event filter to the treeView to receive Left/Right button - * presses. (Usually treeView consumes left/right to use them as collapse/expand - * shortcuts */ -bool Dialog::eventFilter(QObject* target, QEvent *event) { - if (event->type() == QEvent::KeyPress) { - qDebug() << "keyboard event"; + +void Dialog::selectFirstElement() { + QModelIndex root(ui->treeView->rootIndex()); + QModelIndex section(model_[activeTab_]->index(0, 0, root)); + QModelIndex newIndex = QModelIndex(model_[activeTab_]->index(0, 0, section)); + + ui->treeView->selectionModel()->setCurrentIndex(newIndex, QItemSelectionModel::Select); + ui->treeView->setFocus(); +} + +/* install this filter to the filterEdit to listen for arrow keys */ +bool Dialog::eventFilter(QObject*, QEvent *event) { + if (event->type() == QEvent::KeyPress) { QKeyEvent *keyEvent = static_cast(event); - qDebug() << keyEvent; + bool fortv = false; + if (keyEvent->key() == Qt::Key_Right) { + on_rightKey(); + fortv = true; + } if (keyEvent->key() == Qt::Key_Left) { on_leftKey(); - return true; /* drop this event */ - } else if (keyEvent->key() == Qt::Key_Right) { - on_rightKey(); + fortv = true; + } + if (keyEvent->key() == Qt::Key_Up) { + ui->treeView->cursorUp(); + fortv = true; + } + if (keyEvent->key() == Qt::Key_Down) { + ui->treeView->cursorDown(); + fortv = true; + } + if (fortv) { + ui->treeView->setFocus(); return true; - } else if (keyEvent->key() == Qt::Key_Space) { - on_spaceKey(); - } else { - /* forward to the search box */ - ui->filterEdit->event(event); } - } - return QDialog::eventFilter(target, event); + } + return false; } -- cgit v1.2.3-55-g7522