From 2885e40a3ac1f5c570b74f4d518177ff34be5f4d Mon Sep 17 00:00:00 2001 From: Albert S Date: Sun, 21 Aug 2022 17:28:04 +0200 Subject: [PATCH] mainwindow: Add search history. Allow going up/down with arrow keys --- gui/mainwindow.cpp | 64 ++++++++++++++++++++++++++++++++++++++++++++++ gui/mainwindow.h | 6 ++++- 2 files changed, 69 insertions(+), 1 deletion(-) diff --git a/gui/mainwindow.cpp b/gui/mainwindow.cpp index f0ab02d..1ac3ab0 100644 --- a/gui/mainwindow.cpp +++ b/gui/mainwindow.cpp @@ -70,6 +70,8 @@ MainWindow::MainWindow(QWidget *parent, QString socketPath) policy.setRetainSizeWhenHidden(true); ui->btnOpenFailed->setSizePolicy(policy); + ui->txtSearch->installEventFilter(this); + this->ipcClientThread.start(); } @@ -437,6 +439,60 @@ void MainWindow::processShortcut(int key) } } +bool MainWindow::eventFilter(QObject *object, QEvent *event) +{ + if(object == ui->txtSearch && !searchHistory.empty()) + { + if(event->type() == QEvent::KeyPress) + { + QKeyEvent *keyEvent = static_cast(event); + if(keyEvent->key() == Qt::Key_Up) + { + if(this->currentSavedSearchText.isEmpty()) + { + this->currentSavedSearchText = ui->txtSearch->text(); + } + if(this->currentSearchHistoryIndex <= 0) + { + return true; + } + --this->currentSearchHistoryIndex; + QString text = this->searchHistory.at(this->currentSearchHistoryIndex); + ui->txtSearch->setText(text); + return true; + } + else if(keyEvent->key() == Qt::Key_Down) + { + if(this->currentSearchHistoryIndex == searchHistory.size() - 1) + { + if(!this->currentSavedSearchText.isEmpty()) + { + ui->txtSearch->setText(this->currentSavedSearchText); + this->currentSavedSearchText.clear(); + ++this->currentSearchHistoryIndex; + } + return true; + } + if(this->currentSearchHistoryIndex < searchHistory.size() - 1) + { + ++this->currentSearchHistoryIndex; + QString text = this->searchHistory.at(this->currentSearchHistoryIndex); + ui->txtSearch->setText(text); + } + return true; + } + else + { + this->currentSavedSearchText.clear(); + /* Off by one on purpose so Key_Up decrements it again and lands at + * the last entry */ + this->currentSearchHistoryIndex = this->searchHistory.size(); + } + } + } + return QMainWindow::eventFilter(object, event); +} + void MainWindow::keyPressEvent(QKeyEvent *event) { bool quit = @@ -600,6 +656,14 @@ void MainWindow::lineEditReturnPressed() ui->tabWidget->setCurrentIndex(0); } // TODO: validate q; + while(this->searchHistory.size() > 30) + { + this->searchHistory.removeFirst(); + } + this->searchHistory.append(q); + this->currentSearchHistoryIndex = this->searchHistory.size(); + this->currentSavedSearchText.clear(); + ui->treeResultsList->clear(); ui->lblSearchResults->setText("Searching..."); this->ui->txtSearch->setEnabled(false); diff --git a/gui/mainwindow.h b/gui/mainwindow.h index ee3d174..193ddcd 100644 --- a/gui/mainwindow.h +++ b/gui/mainwindow.h @@ -64,7 +64,11 @@ class MainWindow : public QMainWindow void initSettingsTabs(); int currentSelectedScale(); void processShortcut(int key); -private slots: + bool eventFilter(QObject *object, QEvent *event); + QVector searchHistory; + int currentSearchHistoryIndex = 0; + QString currentSavedSearchText; + private slots: void lineEditReturnPressed(); void treeSearchItemActivated(QTreeWidgetItem *item, int i); void showSearchResultsContextMenu(const QPoint &point);