113 lines
3.2 KiB
C++
113 lines
3.2 KiB
C++
#include "mainwindow.h"
|
|
#include "./ui_mainwindow.h"
|
|
|
|
#include "finder.h"
|
|
#include "grouped_finder.h"
|
|
#include "linear_finder.h"
|
|
#include "parallel_finder.h"
|
|
#include "timer.h"
|
|
#include "tree_finder.h"
|
|
#include "word_list_generator.h"
|
|
|
|
#include <sstream>
|
|
|
|
MainWindow::MainWindow(QWidget *parent)
|
|
: QMainWindow(parent), ui(new Ui::MainWindow) {
|
|
ui->setupUi(this);
|
|
|
|
ui->resultView->setModel(&result_model_);
|
|
ui->searchAlgorithmSelector->setModel(&search_algorithms_);
|
|
|
|
setupAlgorithmSelector();
|
|
generateWordList();
|
|
}
|
|
|
|
MainWindow::~MainWindow() { delete ui; }
|
|
|
|
void MainWindow::setupAlgorithmSelector() {
|
|
search_algorithms_.appendRow(new QStandardItem("Linear search"));
|
|
search_algorithms_.appendRow(new QStandardItem("Parallel search"));
|
|
search_algorithms_.appendRow(new QStandardItem("Tree search"));
|
|
search_algorithms_.appendRow(new QStandardItem("Grouped search"));
|
|
}
|
|
|
|
void MainWindow::generateWordList() {
|
|
Timer timer;
|
|
word_list_ = WordListGenerator::generate(ui->wordListSizeSelector->value());
|
|
timer.stop();
|
|
|
|
std::stringstream status_message;
|
|
status_message << "generated " << word_list_.size() << " words in " << timer;
|
|
ui->mainStatusBar->showMessage(QString::fromStdString(status_message.str()));
|
|
}
|
|
|
|
void MainWindow::search(const QString &search_term) {
|
|
if (search_term.isEmpty()) {
|
|
result_model_.setStringList(QStringList());
|
|
ui->mainStatusBar->clearMessage();
|
|
return;
|
|
}
|
|
|
|
Timer timer;
|
|
const auto results = finder_->find_prefix(search_term.toStdString());
|
|
timer.stop();
|
|
|
|
std::stringstream status_message;
|
|
status_message << "search took " << timer << ", found "
|
|
<< std::distance(results.begin(), results.end()) << " results";
|
|
ui->mainStatusBar->showMessage(QString::fromStdString(status_message.str()));
|
|
|
|
showResults(results);
|
|
}
|
|
|
|
void MainWindow::createSelectedFinder() {
|
|
auto selectedFinder = ui->searchAlgorithmSelector->currentIndex();
|
|
|
|
switch (selectedFinder) {
|
|
case 0:
|
|
default:
|
|
finder_ = std::make_unique<LinearFinder>(word_list_);
|
|
break;
|
|
case 1:
|
|
finder_ = std::make_unique<ParallelFinder>(word_list_);
|
|
break;
|
|
case 2:
|
|
finder_ = std::make_unique<TreeFinder>(word_list_);
|
|
break;
|
|
case 3:
|
|
finder_ = std::make_unique<GroupedFinder>(word_list_);
|
|
break;
|
|
}
|
|
}
|
|
|
|
void MainWindow::showResults(
|
|
const std::forward_list<const std::string *> &results) {
|
|
QStringList ui_results;
|
|
for (auto word : results) {
|
|
ui_results.append(QString::fromStdString(*word));
|
|
}
|
|
|
|
result_model_.setStringList(ui_results);
|
|
}
|
|
|
|
void MainWindow::on_searchInput_textChanged(const QString &search_term) {
|
|
QGuiApplication::setOverrideCursor(Qt::WaitCursor);
|
|
search(search_term);
|
|
QGuiApplication::restoreOverrideCursor();
|
|
}
|
|
|
|
void MainWindow::on_searchAlgorithmSelector_currentIndexChanged(int) {
|
|
QGuiApplication::setOverrideCursor(Qt::WaitCursor);
|
|
createSelectedFinder();
|
|
search(ui->searchInput->displayText());
|
|
QGuiApplication::restoreOverrideCursor();
|
|
}
|
|
|
|
void MainWindow::on_wordListSizeSelector_valueChanged(int) {
|
|
QGuiApplication::setOverrideCursor(Qt::WaitCursor);
|
|
generateWordList();
|
|
createSelectedFinder();
|
|
search(ui->searchInput->displayText());
|
|
QGuiApplication::restoreOverrideCursor();
|
|
}
|