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 +++++++++++++++++++++++++++++++++----------------------- src/dialog.h | 2 ++ src/main.cpp | 2 -- src/ui/dialog.ui | 21 ++++++++++++------ src/vmtree.cpp | 16 ++++++++++++++ src/vmtree.h | 18 ++++++++++++++++ 6 files changed, 89 insertions(+), 35 deletions(-) create mode 100644 src/vmtree.cpp create mode 100644 src/vmtree.h 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; } diff --git a/src/dialog.h b/src/dialog.h index d99c00b..a170d31 100644 --- a/src/dialog.h +++ b/src/dialog.h @@ -12,6 +12,7 @@ #include "sessiontreemodel.h" #include "sessionsiconholder.h" + namespace Ui { class Dialog; } @@ -59,6 +60,7 @@ class Dialog : public QDialog { void configClearButton(); void setListModel(SessionTreeModel *model); void keyPressEvent(QKeyEvent * e); + void selectFirstElement(); private slots: void on_pushButtonStart_clicked(); diff --git a/src/main.cpp b/src/main.cpp index 0b515cd..7dfdfc7 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -271,8 +271,6 @@ int main(int argc, char *argv[]) { w.setTheme(); - a.installEventFilter(&w); - w.setWindowFlags(Qt::FramelessWindowHint); if (cmdOptions.contains("pvs")) { pvsEnabled = true; diff --git a/src/ui/dialog.ui b/src/ui/dialog.ui index 1dd6625..ca93287 100644 --- a/src/ui/dialog.ui +++ b/src/ui/dialog.ui @@ -132,8 +132,8 @@ margin-bottom:0px;} <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd"> <html><head><meta name="qrichtext" content="1" /><style type="text/css"> p, li { white-space: pre-wrap; } -</style></head><body style=" font-family:'DejaVuSans'; font-size:9pt; font-weight:400; font-style:normal;"> -<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">Loading...</p></body></html> +</style></head><body style=" font-family:'Sans'; font-size:10pt; font-weight:400; font-style:normal;"> +<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" font-family:'DejaVuSans'; font-size:9pt;">Loading...</span></p></body></html> @@ -164,8 +164,8 @@ p, li { white-space: pre-wrap; } <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd"> <html><head><meta name="qrichtext" content="1" /><style type="text/css"> p, li { white-space: pre-wrap; } -</style></head><body style=" font-family:'DejaVuSans'; font-size:9pt; font-weight:400; font-style:normal;"> -<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">Loading...</p></body></html> +</style></head><body style=" font-family:'Sans'; font-size:10pt; font-weight:400; font-style:normal;"> +<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" font-family:'DejaVuSans'; font-size:9pt;">Loading...</span></p></body></html> @@ -266,7 +266,7 @@ p, li { white-space: pre-wrap; } - + 0 @@ -492,8 +492,8 @@ border:1px solid #999; <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd"> <html><head><meta name="qrichtext" content="1" /><style type="text/css"> p, li { white-space: pre-wrap; } -</style></head><body style=" font-family:'DejaVuSans'; font-size:9pt; font-weight:400; font-style:normal;"> -<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">Click on an item on the left side for more information</p></body></html> +</style></head><body style=" font-family:'Sans'; font-size:10pt; font-weight:400; font-style:normal;"> +<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" font-family:'DejaVuSans'; font-size:9pt;">Click on an item on the left side for more information</span></p></body></html> @@ -597,6 +597,13 @@ p, li { white-space: pre-wrap; } + + + VmTree + QTreeView +
../src/vmtree.h
+
+
diff --git a/src/vmtree.cpp b/src/vmtree.cpp new file mode 100644 index 0000000..e430c39 --- /dev/null +++ b/src/vmtree.cpp @@ -0,0 +1,16 @@ +#include "vmtree.h" + +#include +#include +#include +#include + +void VmTree::cursorDown() { + QKeyEvent* e = new QKeyEvent ( QEvent::KeyPress, Qt::Key_Down, Qt::NoModifier); + event(e); +} + +void VmTree::cursorUp() { + QKeyEvent* e = new QKeyEvent ( QEvent::KeyPress, Qt::Key_Up, Qt::NoModifier); + event(e); +} diff --git a/src/vmtree.h b/src/vmtree.h new file mode 100644 index 0000000..a80248d --- /dev/null +++ b/src/vmtree.h @@ -0,0 +1,18 @@ +#ifndef VMTREE_H + +#include +#include + +#define VMTREE_H + +class VmTree : public QTreeView { + Q_OBJECT + + public: + VmTree(QWidget* parent = 0) : QTreeView(parent) {}; + + void cursorDown(); + void cursorUp(); +}; + +#endif -- cgit v1.2.3-55-g7522