From fe2054a73c74b4462c1c528750223b01a6df5f9d Mon Sep 17 00:00:00 2001 From: Christian Klinger Date: Wed, 1 Jun 2016 10:33:41 +0200 Subject: left/right buttons work globally. --- src/dialog.cpp | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 51 insertions(+), 5 deletions(-) (limited to 'src/dialog.cpp') diff --git a/src/dialog.cpp b/src/dialog.cpp index 0b7fa48..0a34f6c 100644 --- a/src/dialog.cpp +++ b/src/dialog.cpp @@ -20,6 +20,8 @@ #include "vsession.h" #include "choosersettings.h" +#define UNUSED(x) (void)(x) + Dialog::Dialog(int defaultTab, QWidget *parent) : QDialog(parent), ui(new Ui::Dialog) { model_[0] = new SessionTreeModel(parent); @@ -70,6 +72,8 @@ Dialog::Dialog(int defaultTab, QWidget *parent) ui->buttonBugReport->setEnabled(false); QObject::connect(SessionsIconHolder::get(), SIGNAL(iconDownloaded(const QUrl&, const QIcon&)), this, SLOT(iconDownloaded(const QUrl&, const QIcon&))); + + ui->treeView->installEventFilter(this); } Dialog::~Dialog() { @@ -465,10 +469,14 @@ void Dialog::onTabButtonChanged(int tab) { // load the new list setListModel(model_[tab]); this->activeTab_ = tab; - ui->treeView->selectionModel()->clear(); - ui->treeView->selectionModel()->clearSelection(); - ui->treeView->selectionModel()->select(ui->treeView->model()->index(0, 0, ui->treeView->rootIndex()), QItemSelectionModel::Select); - ui->treeView->selectionModel()->setCurrentIndex(ui->treeView->model()->index(0, 0, ui->treeView->rootIndex()), QItemSelectionModel::Select); + + /* 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); + } void Dialog::on_filterEdit_textEdited() { @@ -658,11 +666,13 @@ void Dialog::addHelpAfterDownload(QNetworkReply* reply) { } void Dialog::keyPressEvent(QKeyEvent* event) { + qDebug() << "key pressed: " << event; switch(event->key()) { case Qt::Key_Return: this->on_pushButtonStart_clicked(); break; case Qt::Key_Escape: this->on_pushButtonAbort_clicked(); break; - case Qt::Key_H: this->on_helpNewsButton_clicked(); break; + case Qt::Key_H: this->on_helpNewsButton_clicked(); break; } + QDialog::keyPressEvent(event); } void Dialog::iconDownloaded(const QUrl& url, const QIcon&) { @@ -672,3 +682,39 @@ void Dialog::iconDownloaded(const QUrl& url, const QIcon&) { model_[TAB_ALL_VMS]->updateView(); } +void Dialog::on_leftButton() { + qDebug() << "Left Button"; + int i = activeTab_; + do { + i = (i - 1 + TAB_COUNT) % TAB_COUNT; + } while (!tabs_[i]->isEnabled()); + onTabButtonChanged(i); +} + +void Dialog::on_rightButton() { + qDebug() << "Right Button"; + int i = activeTab_; + do { + i = (i + 1) % TAB_COUNT; + } while (!tabs_[i]->isEnabled()); + onTabButtonChanged(i); +} + +/* 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) { + QKeyEvent *keyEvent = static_cast(event); + if (keyEvent->key() == Qt::Key_Left) { + on_leftButton(); + return true; /* drop this event */ + } else if (keyEvent->key() == Qt::Key_Right) { + on_rightButton(); + return true; + } else if (keyEvent->key() == Qt::Key_Space) { + qDebug() << "space button"; + } + } + return QDialog::eventFilter(target, event); +} -- cgit v1.2.3-55-g7522