From 06f468f8fd42745b75d1508528b5841a04dea4eb Mon Sep 17 00:00:00 2001 From: Michael Mandl Date: Sat, 23 Mar 2024 10:25:50 +0100 Subject: [PATCH] feat: add developer mode and incremental search cli options --- QVectorSearch/main.cpp | 24 ++++++++++++++++++++---- QVectorSearch/mainwindow.cpp | 32 ++++++++++++++++++++++++-------- QVectorSearch/mainwindow.h | 7 ++++++- QVectorSearch/mainwindow.ui | 8 ++++---- 4 files changed, 54 insertions(+), 17 deletions(-) diff --git a/QVectorSearch/main.cpp b/QVectorSearch/main.cpp index 58a0a63..71b14fd 100644 --- a/QVectorSearch/main.cpp +++ b/QVectorSearch/main.cpp @@ -1,10 +1,26 @@ #include "mainwindow.h" #include +#include int main(int argc, char *argv[]) { - QApplication a(argc, argv); - MainWindow w; - w.show(); - return a.exec(); + QApplication app(argc, argv); + + QCommandLineParser cli_parser; + cli_parser.addHelpOption(); + cli_parser.addVersionOption(); + + QCommandLineOption devModeOption({"d", "dev"}, "Run in dev mode"); + cli_parser.addOption(devModeOption); + + QCommandLineOption incrementalSearchOption({"i", "incremental"}, + "Enable incremental search"); + cli_parser.addOption(incrementalSearchOption); + + cli_parser.process(app); + + MainWindow main_window(nullptr, cli_parser.isSet(incrementalSearchOption), + cli_parser.isSet(devModeOption)); + main_window.show(); + return app.exec(); } diff --git a/QVectorSearch/mainwindow.cpp b/QVectorSearch/mainwindow.cpp index 9709e98..586ac28 100644 --- a/QVectorSearch/mainwindow.cpp +++ b/QVectorSearch/mainwindow.cpp @@ -12,14 +12,18 @@ #include -MainWindow::MainWindow(QWidget *parent) - : QMainWindow(parent), ui(new Ui::MainWindow) { +MainWindow::MainWindow(QWidget *parent, bool enableIncrementalSearch, + bool enableDevMode) + : QMainWindow(parent), ui(new Ui::MainWindow), + incremental_search_(enableIncrementalSearch) { ui->setupUi(this); ui->resultView->setModel(&result_model_); ui->wordListSourceSelector->setModel(&word_list_sources_); ui->searchAlgorithmSelector->setModel(&search_algorithms_); + setDevMode(enableDevMode); + setupAlgorithmSelector(); setupWordListSourceSelector(); setupWordList(); @@ -28,6 +32,17 @@ MainWindow::MainWindow(QWidget *parent) MainWindow::~MainWindow() { delete ui; } +void MainWindow::setDevMode(bool enable) { + ui->searchAlgorithmLabel->setVisible(enable); + ui->searchAlgorithmSelector->setVisible(enable); + + ui->selectWordListLabel->setVisible(enable); + ui->wordListSourceSelector->setVisible(enable); + + ui->selectWordListSizeLabel->setVisible(enable); + ui->wordListSizeSelector->setVisible(enable); +} + void MainWindow::setupAlgorithmSelector() { search_algorithms_.appendRow(new QStandardItem("Bucket search")); search_algorithms_.appendRow(new QStandardItem("Linear search")); @@ -141,16 +156,19 @@ void MainWindow::showResults(const WordRefList &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(); + if (!incremental_search_) { + return; + } + + 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()); QGuiApplication::restoreOverrideCursor(); } @@ -159,7 +177,6 @@ void MainWindow::on_wordListSourceSelector_currentIndexChanged(int) { clearResults(); setupWordList(); createSelectedFinder(); - // search(ui->searchInput->displayText()); QGuiApplication::restoreOverrideCursor(); } @@ -168,7 +185,6 @@ void MainWindow::on_wordListSizeSelector_valueChanged(int) { clearResults(); setupWordList(); createSelectedFinder(); - // search(ui->searchInput->displayText()); QGuiApplication::restoreOverrideCursor(); } diff --git a/QVectorSearch/mainwindow.h b/QVectorSearch/mainwindow.h index f686664..2d3370d 100644 --- a/QVectorSearch/mainwindow.h +++ b/QVectorSearch/mainwindow.h @@ -23,11 +23,16 @@ private: QStandardItemModel search_algorithms_; QStandardItemModel word_list_sources_; std::unique_ptr finder_; + bool incremental_search_; public: - MainWindow(QWidget *parent = nullptr); + MainWindow(QWidget *parent = nullptr, bool enableIncrementalSearch = false, + bool enableDevMode = false); ~MainWindow(); + /// Show additional widgets + void setDevMode(bool enable); + private: void setupAlgorithmSelector(); void setupWordListSourceSelector(); diff --git a/QVectorSearch/mainwindow.ui b/QVectorSearch/mainwindow.ui index 24c5000..2299d93 100644 --- a/QVectorSearch/mainwindow.ui +++ b/QVectorSearch/mainwindow.ui @@ -36,7 +36,7 @@ QLayout::SetDefaultConstraint - + <html><head/><body><p>Select word-list size</p></body></html> @@ -46,7 +46,7 @@ - + 0 @@ -79,14 +79,14 @@ - + Select search algorithm - + Select word-list source