From f080d006973a442689b80775830657565bfb77d7 Mon Sep 17 00:00:00 2001 From: Michael Mandl Date: Fri, 22 Mar 2024 10:59:13 +0100 Subject: [PATCH] feat: don't search while typing --- QVectorSearch/mainwindow.cpp | 28 ++++++++++++++++++++-------- QVectorSearch/mainwindow.h | 1 + 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/QVectorSearch/mainwindow.cpp b/QVectorSearch/mainwindow.cpp index 90121a8..9709e98 100644 --- a/QVectorSearch/mainwindow.cpp +++ b/QVectorSearch/mainwindow.cpp @@ -23,6 +23,7 @@ MainWindow::MainWindow(QWidget *parent) setupAlgorithmSelector(); setupWordListSourceSelector(); setupWordList(); + createSelectedFinder(); } MainWindow::~MainWindow() { delete ui; } @@ -79,8 +80,7 @@ void MainWindow::loadWordList(std::filesystem::path path) { void MainWindow::search(const QString &search_term) { if (search_term.isEmpty()) { - result_model_.setStringList(QStringList()); - ui->mainStatusBar->clearMessage(); + clearResults(); return; } @@ -99,6 +99,7 @@ void MainWindow::search(const QString &search_term) { void MainWindow::createSelectedFinder() { auto selectedFinder = ui->searchAlgorithmSelector->currentIndex(); + Timer timer; switch (selectedFinder) { case 0: default: @@ -120,6 +121,12 @@ void MainWindow::createSelectedFinder() { finder_ = std::make_unique(word_list_); break; } + timer.stop(); + + std::stringstream status_message; + status_message << "finder initialized in " << timer << ", " + << word_list_.size() << " words loaded"; + ui->mainStatusBar->showMessage(QString::fromStdString(status_message.str())); } void MainWindow::showResults(const WordRefList &results) { @@ -131,32 +138,37 @@ void MainWindow::showResults(const WordRefList &results) { result_model_.setStringList(ui_results); } +void MainWindow::clearResults() { result_model_.setStringList({}); } + void MainWindow::on_searchInput_textChanged(const QString &search_term) { - QGuiApplication::setOverrideCursor(Qt::WaitCursor); - search(search_term); - QGuiApplication::restoreOverrideCursor(); + // QGuiApplication::setOverrideCursor(Qt::WaitCursor); + // search(search_term); + // QGuiApplication::restoreOverrideCursor(); } void MainWindow::on_searchAlgorithmSelector_currentIndexChanged(int) { QGuiApplication::setOverrideCursor(Qt::WaitCursor); + clearResults(); createSelectedFinder(); - search(ui->searchInput->displayText()); + // search(ui->searchInput->displayText()); QGuiApplication::restoreOverrideCursor(); } void MainWindow::on_wordListSourceSelector_currentIndexChanged(int) { QGuiApplication::setOverrideCursor(Qt::WaitCursor); + clearResults(); setupWordList(); createSelectedFinder(); - search(ui->searchInput->displayText()); + // search(ui->searchInput->displayText()); QGuiApplication::restoreOverrideCursor(); } void MainWindow::on_wordListSizeSelector_valueChanged(int) { QGuiApplication::setOverrideCursor(Qt::WaitCursor); + clearResults(); setupWordList(); createSelectedFinder(); - search(ui->searchInput->displayText()); + // search(ui->searchInput->displayText()); QGuiApplication::restoreOverrideCursor(); } diff --git a/QVectorSearch/mainwindow.h b/QVectorSearch/mainwindow.h index 9736b75..f686664 100644 --- a/QVectorSearch/mainwindow.h +++ b/QVectorSearch/mainwindow.h @@ -37,6 +37,7 @@ private: void search(const QString &search_term); void createSelectedFinder(); void showResults(const WordRefList &results); + void clearResults(); private slots: void on_searchInput_textChanged(const QString &search_term);